From f9cf2e0b66e78be07ae2b60415d0fbf6aed6380c Mon Sep 17 00:00:00 2001 From: Menghuan1918 Date: Wed, 25 Nov 2026 01:20:24 +0800 Subject: [PATCH] fix(host): keep plugin routes anonymous on 0.2.3 and make the search walk deterministic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DSH 0.2.3's webserver gained a per-route `auth` policy that defaults to `required`, so every plugin route was answered with 401/503 before its handler — the loopback clue read lost its credential-free semantics and the mount lane saw 401 on `/sidebar/agent/clues/read`. Every route (HTTP and upgrade, including the bundle and clue share routes) now declares `auth: 'anonymous'` and keeps its own fence, exactly the pre-0.2.3 contract; the two agent clue routes keep their loopback-only rule. The CI unit lane also exposed a non-deterministic walk: `maxMatches` truncated the search while iterating in readdir order, so on Linux (hashed directory entries) a page could skip 'b.txt'. The walk now sorts each directory's entries by name before iterating, which makes the truncated page stable everywhere. Gates: typecheck, lint, test (366 files / 4615 passed), build, consumer-types. --- src/bundle-route.ts | 1 + src/clues/agent-drafts.ts | 1 + src/clues/agent-read.ts | 2 +- src/clues/install-stack.ts | 2 +- src/context-types.ts | 5 +++++ src/fs-search.ts | 9 ++++++++- src/index.ts | 10 ++++++++++ 7 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/bundle-route.ts b/src/bundle-route.ts index e155599b..4aa2527d 100644 --- a/src/bundle-route.ts +++ b/src/bundle-route.ts @@ -138,6 +138,7 @@ export function createBundleRouteHandler( /** Register the /sidebar/bundle route (disposed with the fiber). */ export function registerBundleRoute(ctx: Context, fence: (req: SidebarHttpRequest) => boolean): () => void { return ctx.webServer.register({ + auth: 'anonymous', kind: 'prefix', path: '/sidebar/bundle', handler: createBundleRouteHandler(fence), diff --git a/src/clues/agent-drafts.ts b/src/clues/agent-drafts.ts index ca4b7e0e..4c8c0c62 100644 --- a/src/clues/agent-drafts.ts +++ b/src/clues/agent-drafts.ts @@ -33,6 +33,7 @@ function admitted(req: SidebarHttpRequest, port: number | undefined): boolean { export function createClueAgentDraftRoute(deps: ClueAgentDraftDeps): SidebarWebRoute { return { kind: 'exact', path: CLUE_AGENT_COMMIT_ROUTE_PATH, + auth: 'anonymous', async handler(req, res) { // Admission is deliberately before body iteration and draft lookup. if (req.method !== 'POST') { reject(res, 405); return } diff --git a/src/clues/agent-read.ts b/src/clues/agent-read.ts index 90bbc94f..6ae1e66a 100644 --- a/src/clues/agent-read.ts +++ b/src/clues/agent-read.ts @@ -380,7 +380,7 @@ export function createClueAgentReadRoute(deps: ClueAgentReadDeps): SidebarWebRou } } } - return { kind: 'exact', path: CLUE_AGENT_READ_ROUTE_PATH, handler } + return { kind: 'exact', path: CLUE_AGENT_READ_ROUTE_PATH, auth: 'anonymous', handler } } /** diff --git a/src/clues/install-stack.ts b/src/clues/install-stack.ts index f785560d..06674c8b 100644 --- a/src/clues/install-stack.ts +++ b/src/clues/install-stack.ts @@ -177,7 +177,7 @@ export function installClueStack(ctx: Context, options: InstallClueStackOptions) const routes = createCluesShareRoutes(share, options.fence) const disposers: Array<() => void> = [] try { - for (const route of routes.routes) disposers.push(ctx.webServer.register(route)) + for (const route of routes.routes) disposers.push(ctx.webServer.register({ ...route, auth: 'anonymous' })) } catch (error) { for (const dispose of disposers.splice(0).reverse()) dispose() throw error diff --git a/src/context-types.ts b/src/context-types.ts index 152919cc..42a43143 100644 --- a/src/context-types.ts +++ b/src/context-types.ts @@ -90,12 +90,17 @@ export type SidebarUpgradeHead = Uint8Array export interface SidebarWebRoute { kind: 'exact' | 'prefix' path: string + /** Host admission policy; DSH 0.2.3 defaults to `required`, so every route + * that enforces admission itself declares `anonymous` and keeps its fence. */ + auth?: 'anonymous' | 'required' | 'deny' handler: (req: SidebarHttpRequest, res: SidebarHttpResponse) => void | Promise } /** One exact-path HTTP upgrade registration (mirror of WebUpgradeRoute). */ export interface SidebarWebUpgradeRoute { path: string + /** See {@link SidebarWebRoute.auth}: the upgrade handler fences itself. */ + auth?: 'anonymous' | 'required' | 'deny' handler: (req: SidebarHttpRequest, socket: SidebarUpgradeSocket, head: SidebarUpgradeHead) => void | Promise } diff --git a/src/fs-search.ts b/src/fs-search.ts index a587e343..a65bbfc5 100644 --- a/src/fs-search.ts +++ b/src/fs-search.ts @@ -218,7 +218,14 @@ export async function searchFilesPlain(root: string, query: string, opts: FsSear return undefined }) if (level === undefined) return - for await (const dirent of level) { + // Iterate in name order: `maxMatches` truncates the walk, so the page a + // caller sees must not depend on the filesystem's readdir order (Linux + // hashes directory entries; macOS returns creation order). The sorted + // match list alone does not help once the walk stopped early. + const entries: Dirent[] = [] + for await (const dirent of level) entries.push(dirent) + entries.sort((left, right) => (left.name < right.name ? -1 : left.name > right.name ? 1 : 0)) + for (const dirent of entries) { visited += 1 if (visited > maxVisited) { truncated = true diff --git a/src/index.ts b/src/index.ts index 9ea0b6c3..dae6f925 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1516,6 +1516,7 @@ export function apply(ctx: Context, config?: SidebarConfig): void { }, }) ctx.effect(() => ctx.webServer.register({ + auth: 'anonymous', kind: 'prefix', path: '/sidebar/api', handler: async (req, res) => { @@ -1609,6 +1610,7 @@ export function apply(ctx: Context, config?: SidebarConfig): void { }), 'dsh-better-sidebar: /sidebar/api routes') ctx.effect(() => ctx.webServer.register({ + auth: 'anonymous', kind: 'exact', path: '/sidebar/sidechat-image', handler: async (req, res) => { @@ -1634,6 +1636,7 @@ export function apply(ctx: Context, config?: SidebarConfig): void { // failed or oversized upload never leaves a partial file (see // fs-operations.ts for the containment and shape rules). ctx.effect(() => ctx.webServer.register({ + auth: 'anonymous', kind: 'exact', path: '/sidebar/upload', handler: async (req, res) => { @@ -1672,6 +1675,7 @@ export function apply(ctx: Context, config?: SidebarConfig): void { // envelope no longer caps what an editor can write back. The encoding and // line-ending round-trip lives in writeWorkspaceText, unchanged. ctx.effect(() => ctx.webServer.register({ + auth: 'anonymous', kind: 'exact', path: '/sidebar/write', handler: async (req, res) => { @@ -1725,6 +1729,7 @@ export function apply(ctx: Context, config?: SidebarConfig): void { // file once, with the RFC 5987 disposition. `archiveTasks` owns the // concurrency cap, the TTL, and deleting the temp file. ctx.effect(() => ctx.webServer.register({ + auth: 'anonymous', kind: 'exact', path: '/sidebar/archive', handler: async (req, res) => { @@ -1750,6 +1755,7 @@ export function apply(ctx: Context, config?: SidebarConfig): void { // ── Media route (images for the editor) ───────────────────────────────── ctx.effect(() => ctx.webServer.register({ + auth: 'anonymous', kind: 'prefix', path: '/sidebar/file', handler: async (req, res) => { @@ -1841,6 +1847,7 @@ export function apply(ctx: Context, config?: SidebarConfig): void { // the URL (e.g. a popup opened by a previewed page) stays in an opaque // origin with no same-origin access to the GUI. ctx.effect(() => ctx.webServer.register({ + auth: 'anonymous', kind: 'prefix', path: '/sidebar/html', handler: async (req, res) => { @@ -1900,6 +1907,7 @@ export function apply(ctx: Context, config?: SidebarConfig): void { // editor / folder-window / browser tab open. const agentOpenWss = new WebSocketServer({ noServer: true }) ctx.effect(() => ctx.webServer.registerUpgrade({ + auth: 'anonymous', path: '/sidebar/ws/agent-opens', handler: (req, socket, head) => { if (!fence(req)) { @@ -1921,6 +1929,7 @@ export function apply(ctx: Context, config?: SidebarConfig): void { // lexical rules), so the watch observes what the tree can list. const fsWatchWss = new WebSocketServer({ noServer: true }) ctx.effect(() => ctx.webServer.registerUpgrade({ + auth: 'anonymous', path: '/sidebar/ws/fs-watch', handler: (req, socket, head) => { if (!fence(req)) { @@ -1943,6 +1952,7 @@ export function apply(ctx: Context, config?: SidebarConfig): void { const sidechatPush = createSidechatPushHub(ctx) ctx.effect(() => () => { sidechatPush.dispose() }, 'dsh-better-sidebar: side-chat push hub') ctx.effect(() => ctx.webServer.registerUpgrade({ + auth: 'anonymous', path: '/sidebar/ws/sidechat', handler: (req, socket, head) => { if (!fence(req)) {