mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-20 04:59:34 +01:00
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:
1 parent
44a10cff39
commit
2a3496b7b3
6 files changed
+119
-22
No files matched your search
@@ -9,6 +9,9 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
import Fastify, { type FastifyInstance } from 'fastify';
|
||||
import fastifyCookie from '@fastify/cookie';
|
||||
import fastifyWebsocket from '@fastify/websocket';
|
||||
import WebSocket, { WebSocketServer } from 'ws';
|
||||
import { once } from 'node:events';
|
||||
import type { AddressInfo } from 'node:net';
|
||||
import fs from 'node:fs/promises';
|
||||
import http from 'node:http';
|
||||
import os from 'node:os';
|
||||
@@ -83,6 +86,15 @@ afterEach(async () => {
|
||||
|
||||
const create = (payload: Record<string, unknown>) => app.inject({ method: 'POST', url: '/api/webviews', payload });
|
||||
|
||||
/** Wait for something a relayed connection reports asynchronously. */
|
||||
async function waitUntil(check: () => boolean, timeoutMs = 2000): Promise<void> {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
while (!check()) {
|
||||
if (Date.now() > deadline) throw new Error('condition not met in time');
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
}
|
||||
}
|
||||
|
||||
describe('GET /api/webviews', () => {
|
||||
it('starts empty and reports the frame budget the client must honour', async () => {
|
||||
const res = await app.inject({ method: 'GET', url: '/api/webviews' });
|
||||
@@ -415,6 +427,55 @@ describe('managed DeepSeek web cookie', () => {
|
||||
expect(upstream.cookies).toEqual([COOKIE]);
|
||||
expect(getDeepSeekWebCookie).toHaveBeenCalledWith(upstream.port, 'carol');
|
||||
});
|
||||
|
||||
/**
|
||||
* A loopback listener that completes the upgrade and records the cookie on the
|
||||
* handshake. The realtime leg is a separate code path from `proxyRequest`, so
|
||||
* the HTTP assertions above say nothing about what it puts on the wire.
|
||||
*/
|
||||
async function startWebSocketUpstream() {
|
||||
const cookies: Array<string | undefined> = [];
|
||||
const server = http.createServer();
|
||||
const wss = new WebSocketServer({ noServer: true });
|
||||
server.on('upgrade', (req, socket, head) => {
|
||||
cookies.push(req.headers.cookie);
|
||||
wss.handleUpgrade(req, socket, head, (client) => client.close());
|
||||
});
|
||||
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
|
||||
const address = server.address();
|
||||
if (!address || typeof address === 'string') throw new Error('ws upstream has no address');
|
||||
const upstream = {
|
||||
port: address.port,
|
||||
cookies,
|
||||
close: () =>
|
||||
new Promise<void>((resolve) => {
|
||||
wss.close();
|
||||
server.close(() => resolve());
|
||||
}),
|
||||
};
|
||||
upstreams.push(upstream);
|
||||
return upstream;
|
||||
}
|
||||
|
||||
it('presents it on the websocket leg too, ahead of the browser cookies', async () => {
|
||||
const upstream = await startWebSocketUpstream();
|
||||
mintCookieOn(upstream.port, undefined);
|
||||
const cap = await openManaged(`http://127.0.0.1:${upstream.port}/`);
|
||||
|
||||
// Upgrades need a real listener: inject() cannot carry them.
|
||||
await app.listen({ port: 0, host: '127.0.0.1' });
|
||||
const { port } = app.server.address() as AddressInfo;
|
||||
const client = new WebSocket(`ws://127.0.0.1:${port}/webview/${cap}/ws`, {
|
||||
headers: { cookie: 'codeman_session=SECRET; dsh-ui=dark' },
|
||||
});
|
||||
await once(client, 'open');
|
||||
await waitUntil(() => upstream.cookies.length > 0);
|
||||
|
||||
// The credential leads so a stale browser copy cannot win, other dashboard
|
||||
// cookies survive, and Codeman's own session cookie never goes upstream.
|
||||
expect(upstream.cookies).toEqual([`${COOKIE}; dsh-ui=dark`]);
|
||||
client.close();
|
||||
});
|
||||
});
|
||||
|
||||
describe('proxy HTML rewrite', () => {
|
||||
|
||||
@@ -243,13 +243,13 @@ describe('buildUpstreamRequestHeaders', () => {
|
||||
// dials, which the browser has never visited and cannot store anything for.
|
||||
// The supervisor holds it instead. The managed tab is trusted, so the browser's
|
||||
// other dsh cookies are still present and must be kept alongside it.
|
||||
it('appends the server-held credential to the filtered browser cookies', () => {
|
||||
it('puts the server-held credential ahead of the filtered browser cookies', () => {
|
||||
const headers = buildUpstreamRequestHeaders({ cookie: 'codeman_session=SECRET; dash=1' }, upstream, {
|
||||
forwardCookies: true,
|
||||
sessionCookieName: 'codeman_session',
|
||||
injectedCookie: 'dsh-auth-p=v1.payload.sig',
|
||||
});
|
||||
expect(headers.cookie).toBe('dash=1; dsh-auth-p=v1.payload.sig');
|
||||
expect(headers.cookie).toBe('dsh-auth-p=v1.payload.sig; dash=1');
|
||||
});
|
||||
|
||||
it('keeps every non-Codeman browser cookie when injecting the server credential', () => {
|
||||
@@ -262,7 +262,22 @@ describe('buildUpstreamRequestHeaders', () => {
|
||||
injectedCookie: 'dsh-auth-p=v1.payload.sig',
|
||||
}
|
||||
);
|
||||
expect(headers.cookie).toBe('dsh-ui=dark; dsh-settings=z; dsh-auth-p=v1.payload.sig');
|
||||
expect(headers.cookie).toBe('dsh-auth-p=v1.payload.sig; dsh-ui=dark; dsh-settings=z');
|
||||
});
|
||||
|
||||
// A `Cookie` header may name the same cookie twice, and the first value is the
|
||||
// one parsers keep. The browser can still hold a dsh cookie from an earlier
|
||||
// proxied response, so the server's fresh copy has to lead or the stale one wins
|
||||
// and the tab goes back to the 401 this whole path exists to prevent.
|
||||
it('keeps the server credential in front of a stale browser copy of the same name', () => {
|
||||
const headers = buildUpstreamRequestHeaders({ cookie: 'dsh-auth-p=STALE; dsh-ui=dark' }, upstream, {
|
||||
forwardCookies: true,
|
||||
sessionCookieName: 'codeman_session',
|
||||
injectedCookie: 'dsh-auth-p=FRESH',
|
||||
});
|
||||
// The stale copy is still forwarded behind it (dropping it would need its own
|
||||
// name parsing), but the parser sees FRESH first, which is what authenticates.
|
||||
expect(headers.cookie).toBe('dsh-auth-p=FRESH; dsh-auth-p=STALE; dsh-ui=dark');
|
||||
});
|
||||
|
||||
it('sends the server-held credential even when browser cookies are not forwarded', () => {
|
||||
|
||||
Reference in new issue
Block a user