Require strict websocket query params for authorization
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
8e88da9add
commit
6098003dad
4 files changed
+58
-14
No files matched your search
@@ -68,6 +68,7 @@ Runtime command semantics:
|
||||
- Launch URLs include an ephemeral WebSocket token so only the opened browser session can attach.
|
||||
- Connections missing the token (or using a wrong token) are rejected by the runtime.
|
||||
- Connections with duplicate token query parameters are also rejected to avoid ambiguous auth parsing.
|
||||
- Connections with unexpected query parameters are rejected (auth mode requires only `token`; no-auth mode allows no query params).
|
||||
- WebSocket control connections are accepted only on the root runtime path (`/`) to keep the API surface narrow.
|
||||
- Browser renderer talks through a typed `NativeApi` adapter over that WebSocket.
|
||||
- Runtime currently enforces a single active browser client (new client replaces old one).
|
||||
|
||||
@@ -53,11 +53,11 @@ async function terminateProcess(processRef, timeoutMs = 5_000) {
|
||||
await waitForProcessExit(processRef);
|
||||
}
|
||||
|
||||
function waitForUnauthorizedCloseWithoutMessages(socket, timeoutMs = 10_000) {
|
||||
function waitForUnauthorizedCloseWithoutMessages(socket, label, timeoutMs = 10_000) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let messageCount = 0;
|
||||
const timer = setTimeout(() => {
|
||||
reject(new Error("Smoke test failed: websocket close event timed out."));
|
||||
reject(new Error(`Smoke test failed: ${label} websocket close event timed out.`));
|
||||
}, timeoutMs);
|
||||
|
||||
socket.addEventListener("message", () => {
|
||||
@@ -68,7 +68,7 @@ function waitForUnauthorizedCloseWithoutMessages(socket, timeoutMs = 10_000) {
|
||||
if (event.code !== 4001) {
|
||||
reject(
|
||||
new Error(
|
||||
`Smoke test failed: expected unauthorized close code 4001, received ${event.code}.`,
|
||||
`Smoke test failed: expected unauthorized close code 4001 for ${label}, received ${event.code}.`,
|
||||
),
|
||||
);
|
||||
return;
|
||||
@@ -76,7 +76,7 @@ function waitForUnauthorizedCloseWithoutMessages(socket, timeoutMs = 10_000) {
|
||||
if (messageCount > 0) {
|
||||
reject(
|
||||
new Error(
|
||||
`Smoke test failed: unauthorized websocket received ${messageCount} message(s) before close.`,
|
||||
`Smoke test failed: unauthorized websocket ${label} received ${messageCount} message(s) before close.`,
|
||||
),
|
||||
);
|
||||
return;
|
||||
@@ -85,7 +85,7 @@ function waitForUnauthorizedCloseWithoutMessages(socket, timeoutMs = 10_000) {
|
||||
});
|
||||
socket.addEventListener("error", () => {
|
||||
clearTimeout(timer);
|
||||
reject(new Error("Smoke test failed: websocket client error before close."));
|
||||
reject(new Error(`Smoke test failed: ${label} websocket client error before close.`));
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1433,34 +1433,41 @@ async function main() {
|
||||
|
||||
const unauthorizedWsUrl = `${parsedWsUrl.origin}${parsedWsUrl.pathname}`;
|
||||
const unauthorizedWs = new WebSocket(unauthorizedWsUrl);
|
||||
await waitForUnauthorizedCloseWithoutMessages(unauthorizedWs);
|
||||
await waitForUnauthorizedCloseWithoutMessages(unauthorizedWs, "missing-token");
|
||||
|
||||
const wrongTokenWs = new WebSocket(
|
||||
`${parsedWsUrl.origin}${parsedWsUrl.pathname}?token=wrong-token`,
|
||||
);
|
||||
await waitForUnauthorizedCloseWithoutMessages(wrongTokenWs);
|
||||
await waitForUnauthorizedCloseWithoutMessages(wrongTokenWs, "wrong-token");
|
||||
|
||||
const emptyTokenWs = new WebSocket(`${parsedWsUrl.origin}${parsedWsUrl.pathname}?token=`);
|
||||
await waitForUnauthorizedCloseWithoutMessages(emptyTokenWs);
|
||||
await waitForUnauthorizedCloseWithoutMessages(emptyTokenWs, "empty-token");
|
||||
|
||||
const whitespaceTokenWs = new WebSocket(
|
||||
`${parsedWsUrl.origin}${parsedWsUrl.pathname}?token=%20%20`,
|
||||
);
|
||||
await waitForUnauthorizedCloseWithoutMessages(whitespaceTokenWs);
|
||||
await waitForUnauthorizedCloseWithoutMessages(whitespaceTokenWs, "whitespace-token");
|
||||
|
||||
const duplicateTokenWs = new WebSocket(
|
||||
`${parsedWsUrl.origin}${parsedWsUrl.pathname}?token=${encodeURIComponent(
|
||||
parsedWsUrl.searchParams.get("token") ?? "",
|
||||
)}&token=wrong-token`,
|
||||
);
|
||||
await waitForUnauthorizedCloseWithoutMessages(duplicateTokenWs);
|
||||
await waitForUnauthorizedCloseWithoutMessages(duplicateTokenWs, "duplicate-token");
|
||||
|
||||
const extraParamTokenWs = new WebSocket(
|
||||
`${parsedWsUrl.origin}${parsedWsUrl.pathname}?token=${encodeURIComponent(
|
||||
parsedWsUrl.searchParams.get("token") ?? "",
|
||||
)}&debug=1`,
|
||||
);
|
||||
await waitForUnauthorizedCloseWithoutMessages(extraParamTokenWs, "extra-param-token");
|
||||
|
||||
const wrongPathTokenWs = new WebSocket(
|
||||
`${parsedWsUrl.origin}/unexpected?token=${encodeURIComponent(
|
||||
parsedWsUrl.searchParams.get("token") ?? "",
|
||||
)}`,
|
||||
);
|
||||
await waitForUnauthorizedCloseWithoutMessages(wrongPathTokenWs);
|
||||
await waitForUnauthorizedCloseWithoutMessages(wrongPathTokenWs, "wrong-path-token");
|
||||
|
||||
const ws = new WebSocket(wsUrl);
|
||||
await new Promise((resolve, reject) => {
|
||||
@@ -1649,7 +1656,10 @@ async function main() {
|
||||
parsedWsUrl.searchParams.get("token") ?? "",
|
||||
)}&token=wrong-token`,
|
||||
);
|
||||
await waitForUnauthorizedCloseWithoutMessages(duplicateTokenWhileConnectedWs);
|
||||
await waitForUnauthorizedCloseWithoutMessages(
|
||||
duplicateTokenWhileConnectedWs,
|
||||
"duplicate-token-while-connected",
|
||||
);
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
const timer = setTimeout(
|
||||
|
||||
@@ -1031,6 +1031,21 @@ describe("runtimeApiServer", () => {
|
||||
expect(duplicateTokenClose.code).toBe(4001);
|
||||
expect(duplicateTokenMessageCount).toBe(0);
|
||||
|
||||
const extraParamTokenUrl = `${authorizedUrl.origin}${authorizedUrl.pathname}?token=secret-token&debug=1`;
|
||||
const extraParamTokenClient = new WebSocket(extraParamTokenUrl);
|
||||
let extraParamTokenMessageCount = 0;
|
||||
extraParamTokenClient.on("message", () => {
|
||||
extraParamTokenMessageCount += 1;
|
||||
});
|
||||
const extraParamTokenClose = await withTimeout(
|
||||
new Promise<{ code: number }>((resolve, reject) => {
|
||||
extraParamTokenClient.once("close", (code) => resolve({ code }));
|
||||
extraParamTokenClient.once("error", (error) => reject(error));
|
||||
}),
|
||||
);
|
||||
expect(extraParamTokenClose.code).toBe(4001);
|
||||
expect(extraParamTokenMessageCount).toBe(0);
|
||||
|
||||
const wrongPathTokenUrl = `${authorizedUrl.origin}/unexpected?token=secret-token`;
|
||||
const wrongPathTokenClient = new WebSocket(wrongPathTokenUrl);
|
||||
let wrongPathTokenMessageCount = 0;
|
||||
@@ -1073,6 +1088,20 @@ describe("runtimeApiServer", () => {
|
||||
expect(wrongPathClose.code).toBe(4001);
|
||||
expect(wrongPathMessageCount).toBe(0);
|
||||
|
||||
const unexpectedQueryClient = new WebSocket(`${server.wsUrl}?debug=1`);
|
||||
let unexpectedQueryMessageCount = 0;
|
||||
unexpectedQueryClient.on("message", () => {
|
||||
unexpectedQueryMessageCount += 1;
|
||||
});
|
||||
const unexpectedQueryClose = await withTimeout(
|
||||
new Promise<{ code: number }>((resolve, reject) => {
|
||||
unexpectedQueryClient.once("close", (code) => resolve({ code }));
|
||||
unexpectedQueryClient.once("error", (error) => reject(error));
|
||||
}),
|
||||
);
|
||||
expect(unexpectedQueryClose.code).toBe(4001);
|
||||
expect(unexpectedQueryMessageCount).toBe(0);
|
||||
|
||||
const authorizedClient = await connectClient(server.wsUrl);
|
||||
const hello = await authorizedClient.nextMessage();
|
||||
expect(hello.type).toBe("hello");
|
||||
|
||||
@@ -496,11 +496,15 @@ export async function startRuntimeApiServer(
|
||||
}
|
||||
|
||||
if (!authToken) {
|
||||
return true;
|
||||
return request.searchParams.size === 0;
|
||||
}
|
||||
|
||||
const tokens = request.searchParams.getAll("token");
|
||||
return tokens.length === 1 && tokens[0] === authToken;
|
||||
return (
|
||||
request.searchParams.size === 1 &&
|
||||
tokens.length === 1 &&
|
||||
tokens[0] === authToken
|
||||
);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user