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).