fix(webview): keep the browser's cookies on the websocket leg

The HTTP leg puts the server-held dsh credential in front of the browser's own
cookies; the upgrade leg replaced them with it. A managed dsh tab is trusted, so
its other cookies must survive there too. Filter the request the way the HTTP
leg does and merge the credential in front.

Order is the point: a `Cookie` header can name the same cookie twice, and the
first value is the one parsers keep, so appending the credential let a stale
browser copy of the same dsh cookie shadow the one the running process minted -
the tab would come back to the 401 this path exists to prevent. Both legs now
lead with it, and a route-level test asserts the bytes the upstream sees on the
upgrade, not only on the HTTP request.
This commit is contained in:
ArizeSky committed 2026-11-16 16:30:25 +08:00
1 parent 44a10cff39
commit 2a3496b7b3
6 files changed
+119 -22

No files matched your search

+10 -4
View File
@@ -67,8 +67,10 @@ import {
buildUpstreamRequestHeaders,
capabilityFromReferer,
extractFrameAncestors,
filterCookieHeader,
isFramableCrossOrigin,
isHtmlContentType,
mergeCookieHeader,
parseWebviewUrl,
proxyPrefixFor,
resolveUpstreamUrl,
@@ -759,10 +761,14 @@ function proxyWebSocket(socket: WebSocket, req: FastifyRequest<{ Params: ProxyPa
const protocols = req.headers['sec-websocket-protocol'];
// The same credential the HTTP leg presents. A managed dashboard's realtime
// channel is gated exactly like its index, so without this the page loads
// and then sits there silently disconnected.
const cookie =
(await managedDashboardCookie(webview, upstream)) ??
(webview.trusted && req.headers.cookie ? String(req.headers.cookie) : undefined);
// and then sits there silently disconnected. A managed tab is trusted, so
// the browser's other cookies ride along behind it — filtered like the HTTP
// leg, which never forwards Codeman's own session cookie upstream.
const injectedCookie = await managedDashboardCookie(webview, upstream);
const browserCookie = webview.trusted && req.headers.cookie ? String(req.headers.cookie) : undefined;
const cookie = injectedCookie
? mergeCookieHeader(filterCookieHeader(browserCookie, [AUTH_COOKIE_NAME]), injectedCookie)
: browserCookie;
// `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).
+25 -12
View File
@@ -363,6 +363,20 @@ export function rewriteSetCookie(cookie: string, capability: string, secureConte
return out.join('; ');
}
/**
* Put a server-held credential in front of the cookies the browser sent.
*
* Order matters: a `Cookie` header can name the same cookie twice, and parsers
* that collapse it into a map keep the FIRST value (RFC 6265 §5.4 leaves the
* choice open; the common Node parsers take the first). The browser may still
* hold a stale copy of the same dsh cookie from an earlier proxied response, so
* the credential minted by the running process must lead, with everything else
* preserved behind it.
*/
export function mergeCookieHeader(browserCookie: string | undefined, injectedCookie: string): string {
return browserCookie ? `${injectedCookie}; ${browserCookie}` : injectedCookie;
}
/** Drop named cookies from a `Cookie` request header, returning undefined if none remain. */
export function filterCookieHeader(cookie: string | undefined, drop: string[]): string | undefined {
if (!cookie) return undefined;
@@ -384,12 +398,12 @@ export function buildUpstreamRequestHeaders(
refererPath?: string;
relayBodyLength?: boolean;
/**
* A credential the SERVER holds for this dashboard, appended to whatever
* browser cookies were forwarded. `dsh web` mints its session cookie for a
* loopback authority the browser never visits, so the proxy is the only party
* that can present it (see `deepseek-web-server.ts`); the managed tab is
* trusted, which means the browser may also carry other cookies dsh set, and
* those must survive.
* A credential the SERVER holds for this dashboard, put in front of whatever
* browser cookies were forwarded (see `mergeCookieHeader` for why the order
* matters). `dsh web` mints its session cookie for a loopback authority the
* browser never visits, so the proxy is the only party that can present it
* (see `deepseek-web-server.ts`); the managed tab is trusted, which means the
* browser may also carry other cookies dsh set, and those must survive.
*/
injectedCookie?: string;
}
@@ -424,13 +438,12 @@ export function buildUpstreamRequestHeaders(
if (cookie) headers['cookie'] = cookie;
}
// Appended after the relay above, so a browser copy can never shadow it: this
// cookie is scoped to the authority the request is for, one the browser has
// never been able to store anything against. Other cookies are kept: a managed
// dsh tab is trusted, so dsh's own non-auth cookies may legitimately be there.
// In front of the relayed cookies rather than after them, so a stale browser
// copy of the same cookie cannot shadow the one minted by the running process
// (the parser keeps the first value). Other cookies are kept: a managed dsh tab
// is trusted, so dsh's own non-auth cookies may legitimately be there.
if (opts.injectedCookie) {
const existing = headers['cookie'];
headers['cookie'] = existing ? `${existing}; ${opts.injectedCookie}` : opts.injectedCookie;
headers['cookie'] = mergeCookieHeader(headers['cookie'], opts.injectedCookie);
}
// Present as if the browser were talking to the dashboard directly. Apps that