From 677e393fc3e29578f6bf60521f6dcfb4e6d9e192 Mon Sep 17 00:00:00 2001 From: ArizeSky <97787713+ArizeSky@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:46:10 +0800 Subject: [PATCH] fix(webview): stop forwarding the session cookie on the websocket leg The WebSocket leg sent the browser's whole Cookie header to a trusted dashboard, including Codeman's own codeman_session, which the dashboard could then replay against the API. Filter it through filterCookieHeader like the HTTP leg, and omit the header when nothing remains. --- .changeset/webview-ws-cookie-leak.md | 5 ++ src/web/routes/webview-routes.ts | 6 +- test/routes/webview-routes.test.ts | 87 ++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 .changeset/webview-ws-cookie-leak.md diff --git a/.changeset/webview-ws-cookie-leak.md b/.changeset/webview-ws-cookie-leak.md new file mode 100644 index 00000000..c54863b3 --- /dev/null +++ b/.changeset/webview-ws-cookie-leak.md @@ -0,0 +1,5 @@ +--- +"aicodeman": patch +--- + +The WebSocket leg of a trusted web tab no longer forwards Codeman's own `codeman_session` cookie upstream, which the HTTP leg already stripped. diff --git a/src/web/routes/webview-routes.ts b/src/web/routes/webview-routes.ts index 579aada5..b7f85a0b 100644 --- a/src/web/routes/webview-routes.ts +++ b/src/web/routes/webview-routes.ts @@ -64,6 +64,7 @@ import { buildUpstreamRequestHeaders, capabilityFromReferer, extractFrameAncestors, + filterCookieHeader, isFramableCrossOrigin, isHtmlContentType, parseWebviewUrl, @@ -707,13 +708,16 @@ function proxyWebSocket(socket: WebSocket, req: FastifyRequest<{ Params: ProxyPa }; const protocols = req.headers['sec-websocket-protocol']; + // Trusted dashboards get the browser's own cookies, but never Codeman's + // session cookie: the HTTP leg strips it the same way. + const cookie = webview.trusted ? filterCookieHeader(req.headers.cookie, [AUTH_COOKIE_NAME]) : undefined; // `lookup` is absent from ws's ClientOptions typings but flows through // http.request to net.connect untouched, which is where the resolved // address is judged (see webview-egress.ts). const upstreamOptions: WsClientOptions & { lookup: EgressLookup } = { headers: { origin: upstream.origin, - ...(webview.trusted && req.headers.cookie ? { cookie: String(req.headers.cookie) } : {}), + ...(cookie ? { cookie } : {}), }, handshakeTimeout: WEBVIEW_WS_HANDSHAKE_TIMEOUT_MS, lookup: webviewEgressLookup, diff --git a/test/routes/webview-routes.test.ts b/test/routes/webview-routes.test.ts index 7e0f7b05..10ebd7e0 100644 --- a/test/routes/webview-routes.test.ts +++ b/test/routes/webview-routes.test.ts @@ -10,8 +10,11 @@ import Fastify, { type FastifyInstance } from 'fastify'; import fastifyCookie from '@fastify/cookie'; import fastifyWebsocket from '@fastify/websocket'; import fs from 'node:fs/promises'; +import { createServer, type IncomingHttpHeaders, type Server } from 'node:http'; +import type { AddressInfo } from 'node:net'; import os from 'node:os'; import path from 'node:path'; +import WebSocket, { WebSocketServer } from 'ws'; import { registerWebviewRoutes } from '../../src/web/routes/webview-routes.js'; import { installRouteErrorHandler } from '../../src/web/route-error-handler.js'; import { webviewCapabilities } from '../../src/webview-capabilities.js'; @@ -268,6 +271,90 @@ describe('proxy route', () => { }); }); +describe('proxy route: WebSocket cookie forwarding', () => { + let upstream: Server; + let upstreamPort: number; + let wss: WebSocketServer; + let appPort: number; + let upgradeHeaders: Promise; + let client: WebSocket | undefined; + + beforeEach(async () => { + let resolveUpgrade!: (headers: IncomingHttpHeaders) => void; + upgradeHeaders = new Promise((resolve) => { + resolveUpgrade = resolve; + }); + + // A real loopback dashboard: its upgrade handler is what records the cookie + // the proxy put on the handshake. + upstream = createServer(); + wss = new WebSocketServer({ noServer: true }); + upstream.on('upgrade', (req, socket, head) => { + resolveUpgrade(req.headers); + wss.handleUpgrade(req, socket, head, (upstreamSocket) => { + wss.emit('connection', upstreamSocket, req); + }); + }); + await new Promise((resolve) => upstream.listen(0, '127.0.0.1', resolve)); + upstreamPort = (upstream.address() as AddressInfo).port; + + // WebSocket upgrades cannot go through inject(); this app needs a real port. + await app.listen({ port: 0, host: '127.0.0.1' }); + appPort = (app.server.address() as AddressInfo).port; + }); + + afterEach(async () => { + client?.terminate(); + client = undefined; + for (const socket of wss.clients) socket.terminate(); + await new Promise((resolve) => wss.close(() => resolve())); + await new Promise((resolve) => upstream.close(() => resolve())); + await app.close(); + }); + + /** Wait for a promise without letting a missing handshake hang the test until vitest's timeout. */ + function within(promise: Promise, timeoutMs = 5000): Promise { + let timer!: NodeJS.Timeout; + const timeout = new Promise((_, reject) => { + timer = setTimeout(() => reject(new Error('timed out waiting for the upstream WebSocket handshake')), timeoutMs); + }); + return Promise.race([promise, timeout]).finally(() => clearTimeout(timer)); + } + + /** Create a trusted tab, open it for a capability, and connect the browser leg with `cookie`. */ + async function connectThroughProxy(cookie: string): Promise { + const created = await create({ + name: 'Trusted dashboard', + url: `http://127.0.0.1:${upstreamPort}/`, + trusted: true, + }); + expect(created.statusCode).toBe(200); + const opened = await app.inject({ method: 'POST', url: `/api/webviews/${created.json().data.id}/open` }); + const cap = capabilityFromProxyPath(opened.json().data.embedUrl)!; + + client = await new Promise((resolve, reject) => { + const socket = new WebSocket(`ws://127.0.0.1:${appPort}/webview/${cap}/ws`, { headers: { cookie } }); + socket.once('open', () => resolve(socket)); + socket.once('error', reject); + }); + } + + it('forwards only the dashboard cookie, never the Codeman session cookie', async () => { + await connectThroughProxy('codeman_session=SECRET; theme=dark'); + + const headers = await within(upgradeHeaders); + expect(headers.cookie).toBe('theme=dark'); + expect(headers.cookie).not.toContain('codeman_session'); + }); + + it('sends no cookie header when the browser only has the Codeman session cookie', async () => { + await connectThroughProxy('codeman_session=SECRET'); + + const headers = await within(upgradeHeaders); + expect(headers.cookie).toBeUndefined(); + }); +}); + describe('POST /api/webviews/probe', () => { it('reports an unreachable target as a normal answer, not a 500', async () => { // Port 1 is reserved and refuses instantly.