mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-20 04:59:34 +01:00
fix(deepseek): make the Harness web tab render, not just authenticate
`dsh web` gates its own index behind a per-process launch token it prints on boot, and only that URL mints the `dsh-auth-*` cookie every later request needs. The managed tab stores the clean URL, so nothing on the browser side could present the token: the iframe rendered dsh's 401 text, a blank tab. Read the token out of the captured boot output, trade it for the cookie once at startup (`redirect: 'manual'`, since the 303 is what carries Set-Cookie), and let the proxy present it on both the HTTP and WebSocket legs. Scoped to the exact loopback authority the cookie was minted for; a token-less banner (an older dsh) keeps the previous behaviour untouched. The banner is scanned on its own 256 KB budget rather than the 16 KB error-tail buffer: a chatty profile can fill that cap before the banner prints. Authentication alone was not enough. dsh ships `<base href="./">`, which a browser resolves against the DOCUMENT url — and the proxy's runtime shim rewrites that url to strip the `/webview/<cap>/` prefix. The base therefore landed on Codeman's root: every module script 404'd and the page painted its inline `#151517` background with nothing on it (the black screen that replaced the white one). `rewriteHtml` now resolves a relative base against the proxy prefix before the mask can run, leaving a cross-origin base exactly as written. Verified against dsh 0.2.1-rc.5 and a live Codeman build: bare GET 401, captured cookie 200, proxied `/api` WebSocket 101, and a headless-Chrome load of the proxied tab boots the full Harness UI with no console errors.
This commit is contained in:
1 parent
e99b42c387
commit
0b3087918f
8 files changed
+474
-9
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"aicodeman": patch
|
||||
---
|
||||
|
||||
The DeepSeek Harness web tab opens on the dashboard again instead of a blank 401 page. `dsh web` now gates its own index behind a one-time launch token it prints on boot; the supervisor reads that token out of the captured output, trades it for the session cookie once at startup, and the webview proxy presents the cookie on both the HTTP and WebSocket legs — scoped to the loopback authority it was minted for. A dsh without the gate keeps working unchanged.
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"aicodeman": patch
|
||||
---
|
||||
|
||||
Web tabs whose dashboard ships a relative `<base href>` render again. The proxy rewrites the document URL to strip its `/webview/<cap>/` prefix, and a relative base is resolved against that URL — so `./` (what Vite builds emit, DeepSeek Harness among them) pointed at Codeman's root: every module script 404'd and the page painted its background with nothing on it. The rewrite now resolves such a base against the proxy prefix before the mask runs; a cross-origin base is left exactly as the author wrote it.
|
||||
@@ -288,6 +288,18 @@ dsh fences its `/api` behind a browser-trust check on the request authority, and
|
||||
Codeman web tab reaches it through Codeman's own origin via the webview proxy, not
|
||||
directly. Without it the page renders and every API call fails.
|
||||
|
||||
Since dsh's browser-auth gate, the index is not public either: boot prints
|
||||
`dsh web: http://127.0.0.1:<port>/?token=<one-time token>` and only a request
|
||||
carrying that token is answered with the `dsh-auth-*` cookie that authorizes
|
||||
everything after it. The saved tab deliberately keeps the clean URL, so the
|
||||
browser can never present that token, and the tab used to open on dsh's 401 page.
|
||||
`src/deepseek-web-server.ts` now reads the token out of the captured boot output,
|
||||
trades it for the cookie once at startup, and `webview-routes.ts` presents that
|
||||
cookie on both the HTTP and WebSocket legs — scoped to the exact loopback
|
||||
authority it was minted for, so a repointed or stale tab cannot carry it
|
||||
elsewhere. A dsh without the gate prints a token-less banner and keeps working
|
||||
unchanged.
|
||||
|
||||
## 7. Docker and remote cases
|
||||
|
||||
Docker cases work: the agent image installs `dsh` and bootstraps a `dsh-tui`
|
||||
|
||||
+158
-2
@@ -27,6 +27,11 @@
|
||||
* - **Failures reported, not swallowed.** The shell tab used to be where the
|
||||
* stack trace landed. With no tab, the spawn's own output is captured and
|
||||
* handed back to the caller instead.
|
||||
* - **The launch token traded, not forwarded.** `dsh web` gates its own index
|
||||
* behind a one-time token it prints on stdout and exchanges for a browser
|
||||
* cookie. The tab stores a clean URL, so nothing on the browser side could
|
||||
* ever present that token and every load answered 401. This module trades it
|
||||
* here, once, and hands the cookie to the proxy (`getDeepSeekWebCookie`).
|
||||
*/
|
||||
|
||||
import { spawn, type ChildProcess } from 'node:child_process';
|
||||
@@ -53,6 +58,23 @@ const READY_POLL_MS = 400;
|
||||
const KILL_GRACE_MS = 3_000;
|
||||
/** Bound on captured child output, so a chatty boot cannot grow without limit. */
|
||||
const OUTPUT_CAP = 16_384;
|
||||
/**
|
||||
* Bound on the boot-output window searched for the launch banner.
|
||||
*
|
||||
* Deliberately larger and separate from {@link OUTPUT_CAP}: that one only has to
|
||||
* keep an error tail, while this one must survive a chatty plugin tree's boot
|
||||
* logs long enough to see a banner printed after them. 256 KB of one-line output
|
||||
* is far beyond anything a real boot prints, and a bound still exists because a
|
||||
* wedged child can print forever.
|
||||
*/
|
||||
const BANNER_SCAN_CAP = 262_144;
|
||||
|
||||
/** How long boot waits for `dsh web` to print the launch URL it gates itself on. */
|
||||
const LAUNCH_URL_WAIT_MS = 4_000;
|
||||
/** Bound on the one-shot token→cookie exchange (loopback, so this is generous). */
|
||||
const TOKEN_EXCHANGE_TIMEOUT_MS = 5_000;
|
||||
/** Cookie name `dsh web` mints for a browser session that presented the token. */
|
||||
const DSH_AUTH_COOKIE_PREFIX = 'dsh-auth-';
|
||||
|
||||
export interface DeepSeekWebStatus {
|
||||
running: boolean;
|
||||
@@ -67,6 +89,18 @@ interface RunningServer {
|
||||
port: number;
|
||||
authority: string;
|
||||
output: () => string;
|
||||
/**
|
||||
* Boot output scanned for the launch banner, on its own (larger) budget than
|
||||
* `output`. Read through this rather than `output` so a banner printed after
|
||||
* the error-tail cap filled is still found.
|
||||
*/
|
||||
banner: () => string;
|
||||
/**
|
||||
* The `dsh-auth-*` cookie bought with this process's launch token, once the
|
||||
* exchange succeeded. Null on a dsh old enough to serve its index without a
|
||||
* token, which is why "no cookie" is not a startup failure.
|
||||
*/
|
||||
cookie: string | null;
|
||||
}
|
||||
|
||||
let current: RunningServer | null = null;
|
||||
@@ -105,6 +139,101 @@ async function answersHttp(port: number): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The authenticated URL `dsh web` prints once it is up.
|
||||
*
|
||||
* Its browser-auth gate made the index private: boot writes
|
||||
* `dsh web: http://127.0.0.1:<port>/?token=<one-time token>` and only a request
|
||||
* carrying that token is answered with the cookie that authorizes everything
|
||||
* after it. The token is per process, so it can be neither guessed nor carried
|
||||
* across restarts — it has to be read out of the boot output.
|
||||
*
|
||||
* Null for output without one, which is the older-dsh case and stays working.
|
||||
*
|
||||
* @param output - captured stdout+stderr so far.
|
||||
* @returns absolute http(s) URL carrying a `token`, or null.
|
||||
*/
|
||||
export function parseLaunchUrl(output: string): string | null {
|
||||
// Strip SGR sequences first: a coloured banner would otherwise hide the URL
|
||||
// behind escape codes and turn a perfectly working server into a 401. Built
|
||||
// from the code point rather than written as `\x1b` so the linter's
|
||||
// no-control-regex rule does not read it as an accidental control character.
|
||||
const plain = output.replace(new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`, 'g'), '');
|
||||
const match = /dsh web:\s*(\S+)/.exec(plain);
|
||||
if (!match) return null;
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(match[1]);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (url.protocol !== 'http:' && url.protocol !== 'https:') return null;
|
||||
return url.searchParams.get('token') ? url.href : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trade the launch URL for the `dsh-auth-*` cookie the proxy will present.
|
||||
*
|
||||
* `redirect: 'manual'` is load-bearing: the 303 is the response that carries
|
||||
* `Set-Cookie`, and following it would burn the token on a redirect to `./`
|
||||
* while dropping the only credential the exchange produces.
|
||||
*
|
||||
* @param launchUrl - token-bearing URL from {@link parseLaunchUrl}.
|
||||
* @returns `name=value` pair ready for a Cookie header, or null.
|
||||
*/
|
||||
async function exchangeLaunchToken(launchUrl: string): Promise<string | null> {
|
||||
try {
|
||||
const response = await fetch(launchUrl, {
|
||||
redirect: 'manual',
|
||||
signal: AbortSignal.timeout(TOKEN_EXCHANGE_TIMEOUT_MS),
|
||||
});
|
||||
for (const header of response.headers.getSetCookie()) {
|
||||
const pair = header.split(';', 1)[0]?.trim();
|
||||
if (pair?.startsWith(DSH_AUTH_COOKIE_PREFIX)) return pair;
|
||||
}
|
||||
return null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for the banner, then exchange it.
|
||||
*
|
||||
* Never fails startup: a dsh without the gate prints a token-less banner, which
|
||||
* is answered as "nothing to exchange" immediately rather than after the whole
|
||||
* wait, and a gate that changes shape must not turn a serving UI into a boot
|
||||
* failure.
|
||||
*
|
||||
* @param running - the just-started server, read for its captured output.
|
||||
* @returns the cookie to hand the proxy, or null.
|
||||
*/
|
||||
async function captureSessionCookie(running: RunningServer): Promise<string | null> {
|
||||
const deadline = Date.now() + LAUNCH_URL_WAIT_MS;
|
||||
while (Date.now() < deadline) {
|
||||
const banner = running.banner();
|
||||
const launchUrl = parseLaunchUrl(banner);
|
||||
if (launchUrl) {
|
||||
const cookie = await exchangeLaunchToken(launchUrl);
|
||||
// The URL is a credential: never log it, only the fact that it did not work.
|
||||
if (!cookie) {
|
||||
console.warn(
|
||||
'[DeepSeek web] the launch token was printed but could not be exchanged for a session cookie; ' +
|
||||
"the tab will show dsh's 401 page until `dsh web` is restarted"
|
||||
);
|
||||
}
|
||||
return cookie;
|
||||
}
|
||||
// Only a printed http(s) URL settles this: it is the older dsh, which needs
|
||||
// no cookie. Any other line carrying the prefix (a diagnostic, a truncated
|
||||
// write) is not a banner, so the wait continues instead of giving up early.
|
||||
const printed = /dsh web:\s*(\S+)/.exec(banner)?.[1];
|
||||
if (printed && /^https?:\/\//.test(printed)) return null;
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Signal the whole process group.
|
||||
*
|
||||
@@ -134,6 +263,22 @@ export function getDeepSeekWebStatus(): DeepSeekWebStatus {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The cookie this server's launch token bought, for the proxy to present.
|
||||
*
|
||||
* Keyed by `port` because the cookie is signed for the authority it was minted
|
||||
* under (`127.0.0.1:<port>`): handing it to a web tab pointed at some other
|
||||
* server would neither authenticate nor be that server's to receive. Null also
|
||||
* covers a dsh old enough to need no cookie at all.
|
||||
*
|
||||
* @param port - loopback port the upstream request is bound for.
|
||||
* @returns `name=value` cookie pair, or null.
|
||||
*/
|
||||
export function getDeepSeekWebCookie(port: number): string | null {
|
||||
if (!current || current.port !== port) return null;
|
||||
return current.cookie;
|
||||
}
|
||||
|
||||
/** Stop the background server, if one is running. Safe to call when none is. */
|
||||
export async function stopDeepSeekWeb(): Promise<void> {
|
||||
const running = current;
|
||||
@@ -223,8 +368,15 @@ async function startDeepSeekWebLocked(dshDir: string, authority: string): Promis
|
||||
// The pipes must be drained whether or not anyone reads them: a full pipe
|
||||
// blocks the child. Storage is capped; draining is not.
|
||||
let output = '';
|
||||
// Scanned for the launch banner separately from `output`. That cap exists to
|
||||
// bound the error tail, and a chatty profile can fill it before the banner is
|
||||
// printed — losing the banner there produces exactly the 401 tab this module
|
||||
// exists to prevent, so the two buffers must not share a limit.
|
||||
let banner = '';
|
||||
const capture = (chunk: Buffer) => {
|
||||
if (output.length < OUTPUT_CAP) output += chunk.toString('utf-8');
|
||||
const text = chunk.toString('utf-8');
|
||||
if (output.length < OUTPUT_CAP) output += text;
|
||||
if (banner.length < BANNER_SCAN_CAP) banner += text;
|
||||
};
|
||||
child.stdout?.on('data', capture);
|
||||
child.stderr?.on('data', capture);
|
||||
@@ -241,7 +393,7 @@ async function startDeepSeekWebLocked(dshDir: string, authority: string): Promis
|
||||
if (current?.child === child) current = null;
|
||||
});
|
||||
|
||||
const running: RunningServer = { child, port, authority, output: () => output };
|
||||
const running: RunningServer = { child, port, authority, output: () => output, banner: () => banner, cookie: null };
|
||||
current = running;
|
||||
|
||||
const deadline = Date.now() + READY_TIMEOUT_MS;
|
||||
@@ -255,6 +407,10 @@ async function startDeepSeekWebLocked(dshDir: string, authority: string): Promis
|
||||
return { ok: false, error: tail ? `dsh web exited during startup: ${tail}` : 'dsh web exited during startup' };
|
||||
}
|
||||
if (await answersHttp(port)) {
|
||||
// Bought before this call resolves, so the first proxied page load
|
||||
// already carries a credential — a tab opened a moment earlier would
|
||||
// otherwise get the 401 this whole module exists to prevent.
|
||||
running.cookie = await captureSessionCookie(running);
|
||||
return { ok: true, port, url: `http://127.0.0.1:${port}`, reused: false };
|
||||
}
|
||||
await new Promise((r) => setTimeout(r, READY_POLL_MS));
|
||||
|
||||
@@ -50,6 +50,7 @@ import { readWebviews, writeWebviews } from '../../webview-store.js';
|
||||
import { webviewCapabilities } from '../../webview-capabilities.js';
|
||||
import { egressBlockedReason, webviewEgressLookup, webviewFetch, type EgressLookup } from '../webview-egress.js';
|
||||
import { blockedWebviewHostReason } from '../webview-egress-policy.js';
|
||||
import { getDeepSeekWebCookie } from '../../deepseek-web-server.js';
|
||||
import { ApiErrorCode, createErrorResponse } from '../../types.js';
|
||||
import type { Webview, WebviewOpenData, WebviewProbe } from '../../types.js';
|
||||
import { AUTH_COOKIE_NAME } from '../middleware/auth.js';
|
||||
@@ -426,6 +427,31 @@ function proxyHttp(
|
||||
return proxyRequest(req, reply, req.params.cap, req.params['*'] ?? '', basePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* The credential a Codeman-managed dashboard needs on every upstream request.
|
||||
*
|
||||
* `dsh web` now gates its index and `/api` behind a cookie minted for the
|
||||
* authority it was started on, and only the URL it prints on boot carries the
|
||||
* token that buys it. The managed DeepSeek tab deliberately stores the clean
|
||||
* URL, so the browser can never hold that cookie: `deepseek-web-server.ts`
|
||||
* exchanges the token at startup, and this is where the supervisor's result
|
||||
* reaches the request.
|
||||
*
|
||||
* Strictly scoped to the exact loopback origin the cookie was minted for. A
|
||||
* web tab the user repointed at another host, or a stale tab still naming a
|
||||
* previous port, must not be handed a credential that is not its own.
|
||||
*
|
||||
* @param webview - the saved tab being proxied.
|
||||
* @param upstream - resolved upstream URL for this request.
|
||||
* @returns `name=value` cookie pair, or undefined when none applies.
|
||||
*/
|
||||
function managedDashboardCookie(webview: Webview, upstream: URL): string | undefined {
|
||||
if (webview.managed !== 'deepseek-web') return undefined;
|
||||
if (upstream.hostname !== '127.0.0.1') return undefined;
|
||||
const port = Number(upstream.port || (upstream.protocol === 'https:' ? 443 : 80));
|
||||
return getDeepSeekWebCookie(port) ?? undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Proxy one request to the dashboard behind `cap`, serving `wildcard` as the
|
||||
* upstream path. Split out from the route handler so the 404 fallback (which has
|
||||
@@ -468,12 +494,14 @@ async function proxyRequest(
|
||||
}
|
||||
|
||||
const hasBody = req.method !== 'GET' && req.method !== 'HEAD';
|
||||
const injectedCookie = managedDashboardCookie(webview, upstream);
|
||||
const headers = buildUpstreamRequestHeaders(req.headers, upstream, {
|
||||
forwardCookies: webview.trusted,
|
||||
sessionCookieName: AUTH_COOKIE_NAME,
|
||||
refererPath:
|
||||
typeof req.headers.referer === 'string' ? stripProxyPrefix(req.headers.referer, cap, basePath) : undefined,
|
||||
relayBodyLength: hasBody && req.body !== undefined,
|
||||
...(injectedCookie ? { injectedCookie } : {}),
|
||||
});
|
||||
|
||||
// #237: the timeout bounds TIME-TO-HEADERS only. A plain AbortSignal.timeout on
|
||||
@@ -707,13 +735,19 @@ 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 =
|
||||
managedDashboardCookie(webview, upstream) ??
|
||||
(webview.trusted && req.headers.cookie ? String(req.headers.cookie) : 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,
|
||||
|
||||
@@ -311,7 +311,19 @@ export function filterCookieHeader(cookie: string | undefined, drop: string[]):
|
||||
export function buildUpstreamRequestHeaders(
|
||||
incoming: Record<string, string | string[] | undefined>,
|
||||
upstream: URL,
|
||||
opts: { forwardCookies: boolean; sessionCookieName: string; refererPath?: string; relayBodyLength?: boolean }
|
||||
opts: {
|
||||
forwardCookies: boolean;
|
||||
sessionCookieName: string;
|
||||
refererPath?: string;
|
||||
relayBodyLength?: boolean;
|
||||
/**
|
||||
* A credential the SERVER holds for this dashboard, replacing whatever the
|
||||
* browser sent. `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`).
|
||||
*/
|
||||
injectedCookie?: string;
|
||||
}
|
||||
): Record<string, string> {
|
||||
const headers: Record<string, string> = {};
|
||||
|
||||
@@ -343,6 +355,11 @@ export function buildUpstreamRequestHeaders(
|
||||
if (cookie) headers['cookie'] = cookie;
|
||||
}
|
||||
|
||||
// After the relay above, so a browser copy can never shadow it: these cookies
|
||||
// are scoped to the browser's origin, and the authority this request is for is
|
||||
// one it has never been able to store anything against.
|
||||
if (opts.injectedCookie) headers['cookie'] = opts.injectedCookie;
|
||||
|
||||
// Present as if the browser were talking to the dashboard directly. Apps that
|
||||
// check Origin on writes (CSRF defenses) need this to match their own origin.
|
||||
headers['origin'] = upstream.origin;
|
||||
@@ -646,6 +663,45 @@ try{
|
||||
}catch(e){}})();</script>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a page's own RELATIVE `<base href>` absolute under the proxy prefix.
|
||||
*
|
||||
* A base element is resolved against the document's URL, and {@link runtimeUrlShim}
|
||||
* deliberately rewrites that URL to strip the proxy prefix. So a page shipping
|
||||
* `<base href="./">` — which is what a Vite build does, DeepSeek Harness among
|
||||
* them — has its base re-resolved to Codeman's ROOT the moment the shim runs:
|
||||
* every module script, stylesheet and `fetch('./api')` then asks the Codeman
|
||||
* origin instead of the dashboard. The visible symptom is not an error page but
|
||||
* a blank one: the markup and inline `<style>` paint (dsh paints a dark
|
||||
* `#151517`), the module 404s, and nothing else ever renders.
|
||||
*
|
||||
* Resolving it here, while the prefix is still known, keeps the author's target
|
||||
* (the document as it existed before masking) and is idempotent: an
|
||||
* already-absolute path resolves to itself. Cross-origin bases are the author
|
||||
* pointing at a CDN and are left exactly as written.
|
||||
*
|
||||
* @param html - document with attributes already rebased.
|
||||
* @param prefix - proxy prefix the document was served from.
|
||||
* @returns the document with a relative base href made absolute.
|
||||
*/
|
||||
function rebaseRelativeBase(html: string, prefix: string): string {
|
||||
return html.replace(
|
||||
/(<base\b[^>]*?\bhref\s*=\s*)(["'])([^"']*)\2/i,
|
||||
(match: string, head: string, quote: string, href: string) => {
|
||||
let resolved: URL;
|
||||
try {
|
||||
// A stand-in origin: only the path is kept, and a different origin here
|
||||
// is exactly the "absolute base" case this must not touch.
|
||||
resolved = new URL(href, `http://codeman.invalid${prefix}`);
|
||||
} catch {
|
||||
return match;
|
||||
}
|
||||
if (resolved.origin !== 'http://codeman.invalid') return match;
|
||||
return `${head}${quote}${resolved.pathname}${resolved.search}${resolved.hash}${quote}`;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject `<base href="/webview/<cap>/">` plus the runtime URL shim, and rebase
|
||||
* root-absolute `src`/`href`/`action` attributes, which `<base>` alone does not
|
||||
@@ -659,9 +715,12 @@ export function rewriteHtml(html: string, capability: string, basePath = ''): st
|
||||
const prefix = proxyPrefixFor(capability, basePath);
|
||||
|
||||
// Fresh regexes per call: module-level /g patterns carry `lastIndex` between calls.
|
||||
const rebased = html
|
||||
.replace(/(\s(?:src|href|action)\s*=\s*")\/(?!\/)/gi, `$1${prefix}`)
|
||||
.replace(/(\s(?:src|href|action)\s*=\s*')\/(?!\/)/gi, `$1${prefix}`);
|
||||
const rebased = rebaseRelativeBase(
|
||||
html
|
||||
.replace(/(\s(?:src|href|action)\s*=\s*")\/(?!\/)/gi, `$1${prefix}`)
|
||||
.replace(/(\s(?:src|href|action)\s*=\s*')\/(?!\/)/gi, `$1${prefix}`),
|
||||
prefix
|
||||
);
|
||||
|
||||
// A page that ships its own <base> keeps it (overriding it would break the
|
||||
// author's intent), but it STILL needs the shim, which is the layer that
|
||||
|
||||
@@ -12,10 +12,23 @@
|
||||
* 2. The supervisor tracks at most ONE server. The status accessor is what every
|
||||
* caller reads to decide whether to start another, so "no server" must report
|
||||
* as absent rather than as a half-populated record.
|
||||
* 3. `dsh web` now gates its index behind a launch token it prints on boot. The
|
||||
* supervisor is the only party that can trade it, and the cookie it produces
|
||||
* is authority-bound: getting that wrong is a blank tab, not a visible error.
|
||||
*/
|
||||
import { describe, expect, it, beforeEach } from 'vitest';
|
||||
import { describe, expect, it, beforeEach, afterEach } from 'vitest';
|
||||
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { DeepSeekWebStartSchema } from '../src/web/schemas.js';
|
||||
import { getDeepSeekWebStatus, resetDeepSeekWebForTest, stopDeepSeekWeb } from '../src/deepseek-web-server.js';
|
||||
import {
|
||||
getDeepSeekWebCookie,
|
||||
getDeepSeekWebStatus,
|
||||
parseLaunchUrl,
|
||||
resetDeepSeekWebForTest,
|
||||
startDeepSeekWeb,
|
||||
stopDeepSeekWeb,
|
||||
} from '../src/deepseek-web-server.js';
|
||||
|
||||
describe('DeepSeekWebStartSchema: the authority reaching --trusted-host', () => {
|
||||
it('accepts the authority shapes a browser can actually report', () => {
|
||||
@@ -71,3 +84,144 @@ describe('DeepSeek web supervisor: status', () => {
|
||||
expect(getDeepSeekWebStatus().running).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('parseLaunchUrl: the one-time token dsh prints on boot', () => {
|
||||
it('pulls the URL out of the banner a real boot writes', () => {
|
||||
const output = ['dsh web: http://127.0.0.1:3080/?token=0Yb3sJk9Qm2xLd7Tf1Vc', 'ready'].join('\n');
|
||||
expect(parseLaunchUrl(output)).toBe('http://127.0.0.1:3080/?token=0Yb3sJk9Qm2xLd7Tf1Vc');
|
||||
});
|
||||
|
||||
it('sees through SGR colouring, which would otherwise hide the token', () => {
|
||||
const output = '\u001b[36mdsh web:\u001b[0m \u001b[4mhttp://127.0.0.1:3080/?token=abc123\u001b[0m\n';
|
||||
expect(parseLaunchUrl(output)).toBe('http://127.0.0.1:3080/?token=abc123');
|
||||
});
|
||||
|
||||
it('returns null for a token-less banner, which is the older dsh', () => {
|
||||
expect(parseLaunchUrl('dsh web: http://127.0.0.1:3080/\n')).toBeNull();
|
||||
});
|
||||
|
||||
it('refuses anything that is not an http(s) URL carrying a token', () => {
|
||||
expect(parseLaunchUrl('')).toBeNull();
|
||||
expect(parseLaunchUrl('booting...')).toBeNull();
|
||||
expect(parseLaunchUrl('dsh web: javascript:alert(1)?token=x')).toBeNull();
|
||||
expect(parseLaunchUrl('dsh web: not a url?token=x')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('DeepSeek web supervisor: the launch-token cookie', () => {
|
||||
let dir: string;
|
||||
|
||||
beforeEach(() => {
|
||||
resetDeepSeekWebForTest();
|
||||
dir = mkdtempSync(join(tmpdir(), 'codeman-dsh-web-'));
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await stopDeepSeekWeb();
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
/**
|
||||
* A stand-in for `dsh web` with the browser-auth gate: prints the banner,
|
||||
* refuses `/` without a cookie, and trades the token for one 303.
|
||||
*/
|
||||
const FAKE_DSH = `#!/usr/bin/env node
|
||||
const http = require('node:http');
|
||||
const portArg = process.argv.indexOf('--port');
|
||||
const port = Number(process.argv[portArg + 1]);
|
||||
const token = 'test-launch-token';
|
||||
const cookie = 'dsh-auth-test=v1.payload.sig';
|
||||
process.stdout.write('dsh web: http://127.0.0.1:' + port + '/?token=' + token + '\\n');
|
||||
http
|
||||
.createServer((req, res) => {
|
||||
const url = new URL(req.url, 'http://127.0.0.1:' + port);
|
||||
if (url.pathname === '/' && url.searchParams.get('token') === token) {
|
||||
res.writeHead(303, { location: './', 'set-cookie': cookie + '; Path=/; HttpOnly' });
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
if (req.headers.cookie === cookie) {
|
||||
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
|
||||
res.end('<!doctype html><title>harness</title>');
|
||||
return;
|
||||
}
|
||||
res.writeHead(401, { 'content-type': 'text/plain' });
|
||||
res.end('dsh web authentication required');
|
||||
})
|
||||
.listen(port, '127.0.0.1');
|
||||
`;
|
||||
|
||||
it('trades the boot token for the cookie, scoped to its own port', async () => {
|
||||
writeFileSync(join(dir, 'dsh'), FAKE_DSH, { mode: 0o755 });
|
||||
|
||||
const result = await startDeepSeekWeb(dir, 'localhost:3000');
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
|
||||
// Before the exchange this page is exactly the blank tab being fixed: 401.
|
||||
expect((await fetch(`http://127.0.0.1:${result.port}/`)).status).toBe(401);
|
||||
|
||||
expect(getDeepSeekWebCookie(result.port)).toBe('dsh-auth-test=v1.payload.sig');
|
||||
// A credential minted for this authority must never travel to another port.
|
||||
expect(getDeepSeekWebCookie(result.port + 1)).toBeNull();
|
||||
|
||||
const authed = await fetch(`http://127.0.0.1:${result.port}/`, {
|
||||
headers: { cookie: getDeepSeekWebCookie(result.port) as string },
|
||||
});
|
||||
expect(authed.status).toBe(200);
|
||||
}, 20_000);
|
||||
|
||||
it('leaves the old, ungated dsh working: no token means no cookie, not a failure', async () => {
|
||||
writeFileSync(
|
||||
join(dir, 'dsh'),
|
||||
'#!/usr/bin/env node\nconst http = require("node:http");\nconst port = Number(process.argv[process.argv.indexOf("--port") + 1]);\nprocess.stdout.write("dsh web: http://127.0.0.1:" + port + "/\\n");\nhttp.createServer((_q, s) => s.end("ok")).listen(port, "127.0.0.1");\n',
|
||||
{ mode: 0o755 }
|
||||
);
|
||||
|
||||
const result = await startDeepSeekWeb(dir, 'localhost:3000');
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(getDeepSeekWebCookie(result.port)).toBeNull();
|
||||
}, 20_000);
|
||||
|
||||
// The error-tail buffer is capped at 16 KB, and a chatty plugin tree can fill
|
||||
// it before dsh prints its banner. Scanning that buffer alone lost the token
|
||||
// and put the tab back on the 401 page, so the banner is matched on its own
|
||||
// (larger) budget.
|
||||
it('still finds a banner printed after more than the error-tail cap', async () => {
|
||||
writeFileSync(
|
||||
join(dir, 'dsh'),
|
||||
`#!/usr/bin/env node
|
||||
const http = require('node:http');
|
||||
const port = Number(process.argv[process.argv.indexOf('--port') + 1]);
|
||||
const token = 'late-banner-token';
|
||||
const cookie = 'dsh-auth-late=v1.sig';
|
||||
process.stdout.write('warn: plugin noise '.repeat(1100) + '\\n');
|
||||
http
|
||||
.createServer((req, res) => {
|
||||
const url = new URL(req.url, 'http://127.0.0.1:' + port);
|
||||
if (url.searchParams.get('token') === token) {
|
||||
res.writeHead(303, { location: './', 'set-cookie': cookie + '; Path=/' });
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
if (req.headers.cookie === cookie) {
|
||||
res.writeHead(200, { 'content-type': 'text/html' });
|
||||
res.end('<html>ok</html>');
|
||||
return;
|
||||
}
|
||||
res.writeHead(401, { 'content-type': 'text/plain' });
|
||||
res.end('dsh web authentication required');
|
||||
})
|
||||
.listen(port, '127.0.0.1');
|
||||
setTimeout(() => process.stdout.write('dsh web: http://127.0.0.1:' + port + '/?token=' + token + '\\n'), 300);
|
||||
`,
|
||||
{ mode: 0o755 }
|
||||
);
|
||||
|
||||
const result = await startDeepSeekWeb(dir, 'localhost:3000');
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(getDeepSeekWebCookie(result.port)).toBe('dsh-auth-late=v1.sig');
|
||||
}, 20_000);
|
||||
});
|
||||
@@ -238,6 +238,27 @@ describe('buildUpstreamRequestHeaders', () => {
|
||||
expect(headers.authorization).toBeUndefined();
|
||||
});
|
||||
|
||||
// `dsh web` mints its session cookie for the loopback authority the proxy
|
||||
// dials, which the browser has never visited and cannot store anything for.
|
||||
// The supervisor holds it instead, and it must win over the browser's copy.
|
||||
it('replaces the browser cookies with a server-held credential', () => {
|
||||
const headers = buildUpstreamRequestHeaders({ cookie: 'codeman_session=SECRET; stale=1' }, upstream, {
|
||||
forwardCookies: true,
|
||||
sessionCookieName: 'codeman_session',
|
||||
injectedCookie: 'dsh-auth-p=v1.payload.sig',
|
||||
});
|
||||
expect(headers.cookie).toBe('dsh-auth-p=v1.payload.sig');
|
||||
});
|
||||
|
||||
it('sends the server-held credential even when browser cookies are not forwarded', () => {
|
||||
const headers = buildUpstreamRequestHeaders({}, upstream, {
|
||||
forwardCookies: false,
|
||||
sessionCookieName: 'codeman_session',
|
||||
injectedCookie: 'dsh-auth-p=v1.payload.sig',
|
||||
});
|
||||
expect(headers.cookie).toBe('dsh-auth-p=v1.payload.sig');
|
||||
});
|
||||
|
||||
it('presents Origin/Referer as if the browser talked to the dashboard directly', () => {
|
||||
const headers = buildUpstreamRequestHeaders({ origin: 'https://codeman.local' }, upstream, {
|
||||
forwardCookies: false,
|
||||
@@ -345,6 +366,25 @@ describe('rewriteHtml', () => {
|
||||
expect(out.match(/<base/g)).toHaveLength(1);
|
||||
});
|
||||
|
||||
// A Vite build ships `<base href="./">`, and the runtime shim rewrites the
|
||||
// document URL to strip the prefix — so "./" would resolve to Codeman's root
|
||||
// and every module script would 404 behind an otherwise-painted page.
|
||||
it('resolves a page-relative <base> against the prefix before the shim masks the URL', () => {
|
||||
const out = rewriteHtml('<html><head><base href="./"></head></html>', CAP);
|
||||
expect(out).toContain(`<base href="${PREFIX}">`);
|
||||
expect(out.match(/<base/g)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('keeps a sub-path in a relative <base> inside the proxy prefix', () => {
|
||||
const out = rewriteHtml('<html><head><base target="_blank" href="./sub/"></head></html>', CAP);
|
||||
expect(out).toContain(`<base target="_blank" href="${PREFIX}sub/">`);
|
||||
});
|
||||
|
||||
it('leaves a cross-origin <base> exactly as the author wrote it', () => {
|
||||
const out = rewriteHtml('<html><head><base href="https://cdn.example/x/"></head></html>', CAP);
|
||||
expect(out).toContain('<base href="https://cdn.example/x/">');
|
||||
});
|
||||
|
||||
it('still injects the runtime shim when the page ships its own <base>', () => {
|
||||
// The shim is the only layer that catches runtime-built URLs, so an early
|
||||
// return on an existing <base> would silently break those pages.
|
||||
|
||||
Reference in new issue
Block a user