Reject websocket connections on non-root paths
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
144143544a
commit
76be201215
3 files changed
+58
-5
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user