fix(gateway): block cached device token override fallback

This commit is contained in:
Peter Steinberger
2026-03-08 00:57:03 +00:00
parent a2cb80b9c4
commit f236742dc1
2 changed files with 25 additions and 2 deletions

View File

@@ -402,6 +402,26 @@ describe("GatewayClient connect auth payload", () => {
client.stop();
});
it("uses explicit shared password and does not inject stored device token", () => {
loadDeviceAuthTokenMock.mockReturnValue({ token: "stored-device-token" });
const client = new GatewayClient({
url: "ws://127.0.0.1:18789",
password: "shared-password", // pragma: allowlist secret
});
client.start();
const ws = getLatestWs();
ws.emitOpen();
emitConnectChallenge(ws);
expect(connectFrameFrom(ws)).toMatchObject({
password: "shared-password", // pragma: allowlist secret
});
expect(connectFrameFrom(ws).token).toBeUndefined();
expect(connectFrameFrom(ws).deviceToken).toBeUndefined();
client.stop();
});
it("uses stored device token when shared token is not provided", () => {
loadDeviceAuthTokenMock.mockReturnValue({ token: "stored-device-token" });
const client = new GatewayClient({

View File

@@ -254,9 +254,12 @@ export class GatewayClient {
? loadDeviceAuthToken({ deviceId: this.opts.deviceIdentity.deviceId, role })?.token
: null;
// Keep shared gateway credentials explicit. Persisted per-device tokens only
// participate when no explicit shared token is provided.
// participate when no explicit shared token/password is provided.
const resolvedDeviceToken =
explicitDeviceToken ?? (!explicitGatewayToken ? (storedToken ?? undefined) : undefined);
explicitDeviceToken ??
(!(explicitGatewayToken || this.opts.password?.trim())
? (storedToken ?? undefined)
: undefined);
// Legacy compatibility: keep `auth.token` populated for device-token auth when
// no explicit shared token is present.
const authToken = explicitGatewayToken ?? resolvedDeviceToken;