From fc61cddc1683ae0d4fd42138f660ad6306de7603 Mon Sep 17 00:00:00 2001 From: finley08 Date: Mon, 5 Oct 2026 21:34:30 +0800 Subject: [PATCH] =?UTF-8?q?fix(routes):=20HTTP=20routes=20ride=20the=20pag?= =?UTF-8?q?e=20base=20again=20=E2=80=94=20the=20desktop=20shell=20is=20cro?= =?UTF-8?q?ss-origin=20to=20the=20injected=20transport=20origin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The prefix fix routed every route through `__DSH_TRANSPORT__.streamBaseUrl`. For WebSockets that base is required (a custom scheme cannot host `ws:`, and the shell page's host is the literal `app`, whose DNS lookup never completes), but for HTTP it is a DIFFERENT origin than the page: the Electron shell serves the GUI from `dsh-app://app/` while `streamBaseUrl` is the Host's `http://127.0.0.1:` (`streamBaseUrl: new URL(hostUrl).origin` in the shell's `main.js`, via `DESKTOP_IPC.boot`). Measured on DSH 0.2.0-rc.2 + Electron shell: - `OPTIONS /sidebar/api/fs.trees` (Origin: dsh-app://app, preflight for the plugin's JSON POSTs) → `405 method not allowed`, no `Access-Control-Allow-*` header; the actual POST answers `400` with no CORS header either. - The shell's `protocol.handle('dsh-app', …)` forwards every NON-STATIC `dsh-app://app` path to the Host (`forwardWebRequest(request, hostUrl, hostCookie)`), so `dsh-app://app/sidebar/api/…` reaches the Host same-origin — the exact shape `fetch('/sidebar/api/…')` produced before the prefix fix. Consequence in the desktop window: every plugin request is blocked by the browser and surfaces as "Failed to fetch" — file tree (the whole sidebar body), git, editor, uploads, archive, chunk loading. The web profile in a browser is unaffected (there the page origin IS the transport origin), which is why the mount lane cannot see it. HTTP now resolves against `document.baseURI` — which carries a reverse-proxy directory prefix too, so one rule covers both deployments — and falls back to the transport base when no page base exists (specs / SSR). WebSockets keep the transport base. `tests/base-path.spec.ts` pins the split per transport, including the shell case and the no-page-base fallback. --- src/client/desktop-env.ts | 26 +++++++++++++++++++++++++ src/client/host-route-url.ts | 31 +++++++++++++++++++++--------- tests/base-path.spec.ts | 37 +++++++++++++++++++++++++++++++----- 3 files changed, 80 insertions(+), 14 deletions(-) diff --git a/src/client/desktop-env.ts b/src/client/desktop-env.ts index 601b7d23..677810ad 100644 --- a/src/client/desktop-env.ts +++ b/src/client/desktop-env.ts @@ -30,3 +30,29 @@ export function hostTransportBase(): string { if (base !== undefined && base !== '') return base return typeof document !== 'undefined' && typeof document.baseURI === 'string' ? document.baseURI : '' } + +/** + * Base for the plugin's own HTTP routes (`/sidebar/api/*`, `/sidebar/file`, + * `/sidebar/bundle/*`, …): the PAGE's own base, not the injected transport + * origin. + * + * The desktop shell makes those two different — page `dsh-app://app/`, injected + * `streamBaseUrl` = the Host's `http://127.0.0.1:`. HTTP must stay on the + * page: the shell's `protocol.handle` forwards every non-static `dsh-app://app` + * path to the Host same-origin (that is how `fetch('/sidebar/api/…')` reached it + * before the prefix fix), while the Host's plugin routes answer no + * `Access-Control-Allow-*` headers, so a cross-origin `fetch` to the injected + * origin is blocked by the browser outright ("Failed to fetch"). WebSockets go + * the other way round — see `hostTransportBase` and `host-route-url.ts`. + * + * `document.baseURI` carries a reverse-proxy directory prefix too, so one rule + * covers both deployments. + * @returns A URL string usable as the base argument of `new URL`; the transport + * base only when no page base exists (specs, SSR). + */ +export function hostHttpBase(): string { + const base = typeof document !== 'undefined' ? document.baseURI : undefined + if (typeof base === 'string' && base !== '') return base + return hostTransportBase() +} + diff --git a/src/client/host-route-url.ts b/src/client/host-route-url.ts index bfd57c55..bafad7b7 100644 --- a/src/client/host-route-url.ts +++ b/src/client/host-route-url.ts @@ -6,16 +6,29 @@ * reverse-proxy directory (`https://host/dataops/proxy/3080/`) sends every * request OUTSIDE the prefix: the JSON API answers 404, lazy chunks never * load, media and HTML previews stay blank, and the WebSockets close with - * 1006. Resolving a RELATIVE path against the injected transport base keeps - * the request under the page's own directory — the same shape DSH's own client - * uses (`new URL(REMOTE_STREAM_MUX_PATH.slice(1), __DSH_TRANSPORT__?.streamBaseUrl - * ?? document.baseURI)` in `@deepseek-ai/dsh-api-gateway`). + * 1006. Resolving a RELATIVE path against the page's own base keeps the + * request under the page's own directory. * - * The base is normalized to a directory first (search/hash dropped, trailing - * slash forced) so a launch-token query cannot leak into the route and a - * prefix written without its final slash cannot eat the last path segment. + * The two transports need DIFFERENT bases, and the desktop shell is why: + * + * - **HTTP** rides the PAGE base (`document.baseURI`). In the Electron shell + * the page is `dsh-app://app/`, and the shell's protocol handler forwards + * every non-static path to the Host (`protocol.handle` → `forwardWebRequest` + * in the shell's `main.js`), so `dsh-app://app/sidebar/api/…` reaches the + * Host same-origin — the exact shape `fetch('/sidebar/api/…')` produced + * before the prefix fix. The shell also publishes + * `__DSH_TRANSPORT__.streamBaseUrl` = the Host's real origin + * (`new URL(hostUrl).origin`), which is a DIFFERENT origin from the page: + * sending the plugin's JSON POSTs there makes them cross-origin, and the + * plugin's routes answer no `Access-Control-Allow-*` headers (the shell's own + * UI avoids this by going through the same-origin forwarding), so the browser + * rejects them — every tree/git/editor request fails as "Failed to fetch". + * - **WebSocket** needs that injected base: a custom scheme cannot host `ws:`, + * and `dsh-app://app`'s host is the literal string `app`, whose DNS lookup + * never completes (see `desktop-env.ts`). The Host accepts the shell page's + * origin on those sockets. */ -import { hostTransportBase } from './desktop-env.ts' +import { hostHttpBase, hostTransportBase } from './desktop-env.ts' /** Placeholder origin for the non-DOM case (specs / SSR): resolution needs an * absolute base, but the caller never issues the resulting request. */ @@ -30,7 +43,7 @@ const PLACEHOLDER_ORIGIN = 'http://dsh.internal/' * prefix so resolution stays pure. * @returns The absolute route URL (the base's origin + any prefix included). */ -export function hostRouteUrl(path: string, baseUrl: string = hostTransportBase()): URL { +export function hostRouteUrl(path: string, baseUrl: string = hostHttpBase()): URL { return new URL(path.replace(/^\/+/, ''), directoryBase(baseUrl)) } diff --git a/tests/base-path.spec.ts b/tests/base-path.spec.ts index babec038..bf6cccb5 100644 --- a/tests/base-path.spec.ts +++ b/tests/base-path.spec.ts @@ -84,16 +84,43 @@ describe('hostRouteUrl / hostWebSocketUrl', () => { .toBe('https://host.test/sidebar/api/fs.tree') }) - it('prefers the shell transport base over the document base (desktop shell)', () => { + it('splits the base per transport in the desktop shell: HTTP rides the page, WS the injected origin', async () => { // 2718725: the Electron shell serves the GUI from `dsh-app://app/`, whose - // host is the literal string `app` — resolving against it never connects. + // host is the literal string `app` — resolving a WebSocket against it never + // connects, so sockets use the injected Host origin. vi.stubGlobal('__DSH_TRANSPORT__', { streamBaseUrl: 'http://127.0.0.1:4199/' }) - // The default parameter is exercised on purpose (undefined = "read it"). - expect(hostRouteUrl('sidebar/api/fs.tree', undefined).href) - .toBe('http://127.0.0.1:4199/sidebar/api/fs.tree') + await withPageBase('dsh-app://app/', () => { + // HTTP stays on the page: the shell's protocol handler forwards every + // non-static dsh-app path to the Host same-origin, and the plugin's own + // routes carry no CORS headers — a cross-origin POST to the injected + // origin is rejected by the browser ("Failed to fetch"). + expect(hostRouteUrl('sidebar/api/fs.tree', undefined).href) + .toBe('dsh-app://app/sidebar/api/fs.tree') + expect(hostRouteUrl('sidebar/bundle/editor.js', undefined).href) + .toBe('dsh-app://app/sidebar/bundle/editor.js') + }) expect(hostWebSocketUrl('sidebar/ws/fs-watch', undefined).href) .toBe('ws://127.0.0.1:4199/sidebar/ws/fs-watch') }) + + it('falls back to the injected base for HTTP when the page has none (specs, SSR)', () => { + vi.stubGlobal('__DSH_TRANSPORT__', { streamBaseUrl: 'http://127.0.0.1:4199/' }) + const document = globalThis.document as { baseURI?: string } + const previous = document.baseURI + Object.defineProperty(document, 'baseURI', { value: '', configurable: true, writable: true }) + try { + expect(hostRouteUrl('sidebar/api/fs.tree', undefined).href) + .toBe('http://127.0.0.1:4199/sidebar/api/fs.tree') + } finally { + Object.defineProperty(document, 'baseURI', { value: previous, configurable: true, writable: true }) + } + }) + + it('keeps the injected base for HTTP on an ordinary http(s) page (same origin)', () => { + vi.stubGlobal('__DSH_TRANSPORT__', { streamBaseUrl: 'http://127.0.0.1:4199/' }) + expect(hostRouteUrl('sidebar/api/fs.tree', 'http://127.0.0.1:4199/').href) + .toBe('http://127.0.0.1:4199/sidebar/api/fs.tree') + }) }) describe('the real call sites resolve through the page prefix', () => {