From 4ce9abde7d7c3afc05efba1f84d9c10f6e035099 Mon Sep 17 00:00:00 2001 From: ArizeSky <97787713+ArizeSky@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:08:19 +0800 Subject: [PATCH] fix(deepseek): scan every dsh web line for the launch URL The banner parser and the boot wait each took the FIRST `dsh web:` match, so a diagnostic such as `dsh web: loading profiles` printed before the banner made parsing return null on every poll and boot gave up after four seconds. Scan every line and take the first payload that parses as an http(s) URL, and only scan complete lines in the wait loop so a banner split across two output chunks is waited for instead of being parsed truncated. --- .changeset/dsh-web-launch-token.md | 2 +- docs/deepseek-integration.md | 7 +++- src/deepseek-web-server.ts | 63 +++++++++++++++++++++--------- test/deepseek-web-server.test.ts | 51 ++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 21 deletions(-) diff --git a/.changeset/dsh-web-launch-token.md b/.changeset/dsh-web-launch-token.md index d0090a06..05673f1b 100644 --- a/.changeset/dsh-web-launch-token.md +++ b/.changeset/dsh-web-launch-token.md @@ -2,4 +2,4 @@ "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. The cookie is only presented to a tab whose owner holds the privileged-commands grant, and in multi-user mode only to the owner whose request started the process: `managed` is a client-supplied field and the live port is public, so the saved record alone is not an entitlement. A dsh without the gate keeps working unchanged. +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. The banner reader scans every `dsh web:` line (diagnostics share the prefix) and only complete lines (a split write is waited for), so a `dsh web: loading profiles` line no longer makes boot give up. The cookie is only presented to a tab whose owner holds the privileged-commands grant, and in multi-user mode only to the owner whose request started the process: `managed` is a client-supplied field and the live port is public, so the saved record alone is not an entitlement. A dsh without the gate keeps working unchanged. diff --git a/docs/deepseek-integration.md b/docs/deepseek-integration.md index c575ea8b..d4068f2a 100644 --- a/docs/deepseek-integration.md +++ b/docs/deepseek-integration.md @@ -297,8 +297,11 @@ browser can never present that token, and the tab used to open on dsh's 401 page 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. +elsewhere. The reader scans every `dsh web:` line (dsh prints diagnostics such as +`dsh web: loading profiles` under the same prefix before the banner) and only +complete lines, so a banner split across two output chunks is waited for rather +than parsed truncated. A dsh without the gate prints a token-less banner and +keeps working unchanged. The cookie is also bound to a person, not to a URL. `managed` is accepted from the client on `POST/PATCH /api/webviews` and `GET /api/deepseek/web` publishes the live diff --git a/src/deepseek-web-server.ts b/src/deepseek-web-server.ts index b66ba2c5..b0149d2e 100644 --- a/src/deepseek-web-server.ts +++ b/src/deepseek-web-server.ts @@ -146,6 +146,39 @@ async function answersHttp(port: number): Promise { } } +/** + * Remove SGR sequences so a coloured banner still parses. + * + * A coloured banner would otherwise hide the URL behind escape codes and turn a + * perfectly working server into a 401. + */ +function stripSgr(output: string): string { + // 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. + return output.replace(new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`, 'g'), ''); +} + +/** + * The first `dsh web:` payload that parses as an absolute http(s) URL. + * + * Lines whose payload is not a URL are skipped, which is what makes a + * `dsh web: loading profiles` diagnostic harmless. Null when no line carries one. + */ +function firstLaunchBannerUrl(output: string): string | null { + // Fresh per call: a module-level /g pattern carries lastIndex between calls. + const line = /dsh web:\s*(\S+)/g; + for (const match of stripSgr(output).matchAll(line)) { + let url: URL; + try { + url = new URL(match[1]); + } catch { + continue; + } + if (url.protocol === 'http:' || url.protocol === 'https:') return url.href; + } + return null; +} + /** * The authenticated URL `dsh web` prints once it is up. * @@ -155,27 +188,18 @@ async function answersHttp(port: number): Promise { * 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. * + * Scans EVERY `dsh web:` line and returns the first one that is a usable URL, + * because dsh also prints diagnostics under the same prefix before the banner. + * * 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; + const href = firstLaunchBannerUrl(output); + if (!href) return null; + return new URL(href).searchParams.get('token') ? href : null; } /** @@ -218,8 +242,12 @@ async function exchangeLaunchToken(launchUrl: string): Promise { async function captureSessionCookie(running: RunningServer): Promise { const deadline = Date.now() + LAUNCH_URL_WAIT_MS; while (Date.now() < deadline) { + // Only COMPLETE lines: a chunk boundary can cut the token in half, and + // exchanging a truncated one would burn the token on a request that cannot + // authenticate. The next poll picks up the rest of the line. const banner = running.banner(); - const launchUrl = parseLaunchUrl(banner); + const complete = banner.slice(0, banner.lastIndexOf('\n') + 1); + const launchUrl = parseLaunchUrl(complete); if (launchUrl) { const cookie = await exchangeLaunchToken(launchUrl); // The URL is a credential: never log it, only the fact that it did not work. @@ -234,8 +262,7 @@ async function captureSessionCookie(running: RunningServer): Promise setTimeout(resolve, 100)); } return null; diff --git a/test/deepseek-web-server.test.ts b/test/deepseek-web-server.test.ts index bac6f349..1e67167f 100644 --- a/test/deepseek-web-server.test.ts +++ b/test/deepseek-web-server.test.ts @@ -96,6 +96,14 @@ describe('parseLaunchUrl: the one-time token dsh prints on boot', () => { expect(parseLaunchUrl(output)).toBe('http://127.0.0.1:3080/?token=abc123'); }); + // dsh prints diagnostics under the same prefix before the banner. Taking the + // FIRST match made the real URL invisible and boot gave up 4 seconds later. + it('skips a diagnostic line and keeps scanning for the URL', () => { + expect(parseLaunchUrl('dsh web: loading profiles\ndsh web: http://127.0.0.1:3080/?token=abc\n')).toBe( + 'http://127.0.0.1:3080/?token=abc' + ); + }); + 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(); }); @@ -211,6 +219,49 @@ http // 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. + // The banner can arrive in two stdout writes: `capture()` sees two chunks and + // neither is a complete line. Scanning the raw buffer either missed it or, with + // a partial URL that happened to parse, exchanged a truncated token. + it('waits for a banner split across two output chunks', 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 = 'split-token'; +const cookie = 'dsh-auth-split=v1.sig'; +process.stdout.write('dsh web: loading profiles\\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('ok'); + return; + } + res.writeHead(401, { 'content-type': 'text/plain' }); + res.end('dsh web authentication required'); + }) + .listen(port, '127.0.0.1', () => { + const line = 'dsh web: http://127.0.0.1:' + port + '/?token=' + token + '\\n'; + process.stdout.write(line.slice(0, 30)); + setTimeout(() => process.stdout.write(line.slice(30)), 150); + }); +`, + { 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-split=v1.sig'); + }, 20_000); + it('still finds a banner printed after more than the error-tail cap', async () => { writeFileSync( join(dir, 'dsh'),