fix(host): keep plugin routes anonymous on 0.2.3 and make the search walk deterministic

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.
This commit is contained in:
Menghuan1918 committed 2026-11-25 01:20:24 +08:00
1 parent 795beabe22
commit f9cf2e0b66
7 files changed
+27 -3

No files matched your search

+1
View File
@@ -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),
+1
View File
@@ -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 }
+1 -1
View File
@@ -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 }
}
/**
+1 -1
View File
@@ -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
+5
View File
@@ -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<void>
}
/** 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<void>
}
+8 -1
View File
@@ -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
+10
View File
@@ -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)) {