|
| 1 | +// Gateway connect pairing tests protect session exemptions and durable device grant bounds. |
| 2 | +import { describe, expect, test } from "vitest"; |
| 3 | +import { |
| 4 | + GATEWAY_CLIENT_MODES, |
| 5 | + GATEWAY_CLIENT_NAMES, |
| 6 | +} from "../../../../packages/gateway-protocol/src/client-info.js"; |
| 7 | +import { replaceConfigFile } from "../../../config/config.js"; |
| 8 | +import type { GatewayAuthConfig } from "../../../config/types.gateway.js"; |
| 9 | +import { ensureDeviceToken, getPairedDevice } from "../../../infra/device-pairing.js"; |
| 10 | +import { |
| 11 | + loadDeviceIdentity, |
| 12 | + openTrackedWs, |
| 13 | + pairDeviceIdentity, |
| 14 | +} from "../../device-authz.test-helpers.js"; |
| 15 | +import { CONTROL_UI_CLIENT, openTailscaleWs } from "../../server.auth.test-helpers.js"; |
| 16 | +import { |
| 17 | + connectReq, |
| 18 | + installGatewayTestHooks, |
| 19 | + startServer, |
| 20 | + startServerWithClient, |
| 21 | + testState, |
| 22 | + testTailscaleWhois, |
| 23 | +} from "../../test-helpers.js"; |
| 24 | + |
| 25 | +installGatewayTestHooks({ scope: "suite" }); |
| 26 | + |
| 27 | +await import("../../server.js"); |
| 28 | + |
| 29 | +const BACKEND_CLIENT = { |
| 30 | + id: GATEWAY_CLIENT_NAMES.GATEWAY_CLIENT, |
| 31 | + version: "1.0.0", |
| 32 | + platform: "node", |
| 33 | + mode: GATEWAY_CLIENT_MODES.BACKEND, |
| 34 | +} as const; |
| 35 | + |
| 36 | +const TUI_CLIENT = { |
| 37 | + id: GATEWAY_CLIENT_NAMES.TUI, |
| 38 | + version: "1.0.0", |
| 39 | + platform: "test", |
| 40 | + mode: GATEWAY_CLIENT_MODES.CLI, |
| 41 | +} as const; |
| 42 | + |
| 43 | +describe("gateway connect pairing exemptions", () => { |
| 44 | + test.each([ |
| 45 | + { |
| 46 | + name: "local backend self-call", |
| 47 | + client: BACKEND_CLIENT, |
| 48 | + approvedScopes: ["operator.pairing"], |
| 49 | + pairingClientId: GATEWAY_CLIENT_NAMES.CLI, |
| 50 | + pairingClientMode: GATEWAY_CLIENT_MODES.CLI, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "TUI operator client", |
| 54 | + client: TUI_CLIENT, |
| 55 | + approvedScopes: ["operator.read"], |
| 56 | + pairingClientId: GATEWAY_CLIENT_NAMES.TUI, |
| 57 | + pairingClientMode: GATEWAY_CLIENT_MODES.CLI, |
| 58 | + }, |
| 59 | + ])( |
| 60 | + "admits an auth-none $name before and after a narrower pairing row exists", |
| 61 | + async ({ name, client, approvedScopes, pairingClientId, pairingClientMode }) => { |
| 62 | + const started = await startServerWithClient(undefined, { auth: { mode: "none" } }); |
| 63 | + const identityName = `auth-none-${name.replaceAll(" ", "-")}`; |
| 64 | + const loaded = loadDeviceIdentity(identityName); |
| 65 | + let pairedWs: Awaited<ReturnType<typeof openTrackedWs>> | undefined; |
| 66 | + |
| 67 | + try { |
| 68 | + const unpaired = await connectReq(started.ws, { |
| 69 | + client, |
| 70 | + role: "operator", |
| 71 | + scopes: ["operator.write"], |
| 72 | + deviceIdentityPath: loaded.identityPath, |
| 73 | + skipDefaultAuth: true, |
| 74 | + prePairDevice: false, |
| 75 | + }); |
| 76 | + expect(unpaired.ok, JSON.stringify(unpaired)).toBe(true); |
| 77 | + started.ws.close(); |
| 78 | + |
| 79 | + await pairDeviceIdentity({ |
| 80 | + name: identityName, |
| 81 | + role: "operator", |
| 82 | + scopes: approvedScopes, |
| 83 | + clientId: pairingClientId, |
| 84 | + clientMode: pairingClientMode, |
| 85 | + }); |
| 86 | + const tokenBefore = await ensureDeviceToken({ |
| 87 | + deviceId: loaded.identity.deviceId, |
| 88 | + role: "operator", |
| 89 | + scopes: approvedScopes, |
| 90 | + }); |
| 91 | + expect(tokenBefore?.scopes).toEqual(approvedScopes); |
| 92 | + |
| 93 | + pairedWs = await openTrackedWs(started.port); |
| 94 | + const pairedConnect = await connectReq(pairedWs, { |
| 95 | + client, |
| 96 | + role: "operator", |
| 97 | + scopes: ["operator.write"], |
| 98 | + deviceIdentityPath: loaded.identityPath, |
| 99 | + skipDefaultAuth: true, |
| 100 | + prePairDevice: false, |
| 101 | + }); |
| 102 | + expect(pairedConnect.ok, JSON.stringify(pairedConnect)).toBe(true); |
| 103 | + |
| 104 | + const paired = await getPairedDevice(loaded.identity.deviceId); |
| 105 | + expect(paired?.approvedScopes).toEqual(approvedScopes); |
| 106 | + expect(paired?.tokens?.operator).toMatchObject({ |
| 107 | + token: tokenBefore?.token, |
| 108 | + scopes: approvedScopes, |
| 109 | + }); |
| 110 | + expect(paired?.lastSeenReason).toBe("connect"); |
| 111 | + } finally { |
| 112 | + pairedWs?.close(); |
| 113 | + started.ws.close(); |
| 114 | + await started.server.close(); |
| 115 | + started.envSnapshot.restore(); |
| 116 | + } |
| 117 | + }, |
| 118 | + ); |
| 119 | + |
| 120 | + test("keeps a narrow pairing row as the Tailscale Control UI scope cap", async () => { |
| 121 | + const tailscaleOrigin = "https://gateway.tailnet.ts.net"; |
| 122 | + const auth = { |
| 123 | + mode: "token", |
| 124 | + token: "secret", |
| 125 | + allowTailscale: true, |
| 126 | + } satisfies GatewayAuthConfig; |
| 127 | + testState.gatewayAuth = auth; |
| 128 | + testState.gatewayControlUi = { allowedOrigins: [tailscaleOrigin] }; |
| 129 | + testTailscaleWhois.value = { login: "peter", name: "Peter" }; |
| 130 | + await replaceConfigFile({ |
| 131 | + nextConfig: { |
| 132 | + gateway: { |
| 133 | + auth, |
| 134 | + controlUi: { allowedOrigins: [tailscaleOrigin] }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + afterWrite: { mode: "auto" }, |
| 138 | + }); |
| 139 | + const started = await startServer(undefined, { auth, controlUiEnabled: true }); |
| 140 | + const identityName = "tailscale-control-ui-scope-cap"; |
| 141 | + const paired = await pairDeviceIdentity({ |
| 142 | + name: identityName, |
| 143 | + role: "operator", |
| 144 | + scopes: ["operator.read"], |
| 145 | + clientId: CONTROL_UI_CLIENT.id, |
| 146 | + clientMode: CONTROL_UI_CLIENT.mode, |
| 147 | + }); |
| 148 | + let ws: Awaited<ReturnType<typeof openTailscaleWs>> | undefined; |
| 149 | + |
| 150 | + try { |
| 151 | + ws = await openTailscaleWs(started.port, { origin: tailscaleOrigin }); |
| 152 | + const response = await connectReq(ws, { |
| 153 | + skipDefaultAuth: true, |
| 154 | + prePairDevice: false, |
| 155 | + scopes: ["operator.write"], |
| 156 | + client: CONTROL_UI_CLIENT, |
| 157 | + deviceIdentityPath: paired.identityPath, |
| 158 | + }); |
| 159 | + expect(response.ok).toBe(false); |
| 160 | + expect(response.error?.details).toMatchObject({ |
| 161 | + reason: "scope-upgrade", |
| 162 | + approvedScopes: ["operator.read"], |
| 163 | + }); |
| 164 | + expect((await getPairedDevice(paired.identity.deviceId))?.approvedScopes).toEqual([ |
| 165 | + "operator.read", |
| 166 | + ]); |
| 167 | + } finally { |
| 168 | + ws?.close(); |
| 169 | + await started.server.close(); |
| 170 | + started.envSnapshot.restore(); |
| 171 | + testTailscaleWhois.value = null; |
| 172 | + } |
| 173 | + }); |
| 174 | +}); |
0 commit comments