diff --git a/apps/t3/scripts/smoke-test.mjs b/apps/t3/scripts/smoke-test.mjs index c6daf59f36..169ac7db3d 100644 --- a/apps/t3/scripts/smoke-test.mjs +++ b/apps/t3/scripts/smoke-test.mjs @@ -1390,6 +1390,13 @@ async function main() { ); await waitForUnauthorizedCloseWithoutMessages(duplicateTokenWs); + const wrongPathTokenWs = new WebSocket( + `${parsedWsUrl.origin}/unexpected?token=${encodeURIComponent( + parsedWsUrl.searchParams.get("token") ?? "", + )}`, + ); + await waitForUnauthorizedCloseWithoutMessages(wrongPathTokenWs); + const ws = new WebSocket(wsUrl); await new Promise((resolve, reject) => { let sawHello = false; diff --git a/apps/t3/src/runtimeApiServer.test.ts b/apps/t3/src/runtimeApiServer.test.ts index 4e5e3735d1..18cdecc7a0 100644 --- a/apps/t3/src/runtimeApiServer.test.ts +++ b/apps/t3/src/runtimeApiServer.test.ts @@ -1031,6 +1031,48 @@ describe("runtimeApiServer", () => { expect(duplicateTokenClose.code).toBe(4001); expect(duplicateTokenMessageCount).toBe(0); + const wrongPathTokenUrl = `${authorizedUrl.origin}/unexpected?token=secret-token`; + const wrongPathTokenClient = new WebSocket(wrongPathTokenUrl); + let wrongPathTokenMessageCount = 0; + wrongPathTokenClient.on("message", () => { + wrongPathTokenMessageCount += 1; + }); + const wrongPathTokenClose = await withTimeout( + new Promise<{ code: number }>((resolve, reject) => { + wrongPathTokenClient.once("close", (code) => resolve({ code })); + wrongPathTokenClient.once("error", (error) => reject(error)); + }), + ); + expect(wrongPathTokenClose.code).toBe(4001); + expect(wrongPathTokenMessageCount).toBe(0); + + const authorizedClient = await connectClient(server.wsUrl); + const hello = await authorizedClient.nextMessage(); + expect(hello.type).toBe("hello"); + authorizedClient.socket.close(); + }); + + it("rejects websocket connections on non-root paths without auth", async () => { + const server = await startRuntimeApiServer({ + port: 0, + launchCwd: process.cwd(), + }); + servers.push(server); + + const wrongPathClient = new WebSocket(`${server.wsUrl}/unexpected`); + let wrongPathMessageCount = 0; + wrongPathClient.on("message", () => { + wrongPathMessageCount += 1; + }); + const wrongPathClose = await withTimeout( + new Promise<{ code: number }>((resolve, reject) => { + wrongPathClient.once("close", (code) => resolve({ code })); + wrongPathClient.once("error", (error) => reject(error)); + }), + ); + expect(wrongPathClose.code).toBe(4001); + expect(wrongPathMessageCount).toBe(0); + const authorizedClient = await connectClient(server.wsUrl); const hello = await authorizedClient.nextMessage(); expect(hello.type).toBe("hello"); diff --git a/apps/t3/src/runtimeApiServer.ts b/apps/t3/src/runtimeApiServer.ts index d2cad5406d..db42e423d8 100644 --- a/apps/t3/src/runtimeApiServer.ts +++ b/apps/t3/src/runtimeApiServer.ts @@ -485,16 +485,20 @@ export async function startRuntimeApiServer( }); const isAuthorizedConnection = (requestUrl: string | undefined) => { - if (!authToken) { - return true; - } - if (!requestUrl) { - return false; + return authToken === null; } try { const request = new URL(requestUrl, "ws://127.0.0.1"); + if (request.pathname !== "/") { + return false; + } + + if (!authToken) { + return true; + } + const tokens = request.searchParams.getAll("token"); return tokens.length === 1 && tokens[0] === authToken; } catch {