From e490dbe9e7cac01bf6273d6efc7a546d1b8ce826 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 17 Oct 2026 13:07:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20dsh=20web=20admin=20relay=20(securi?= =?UTF-8?q?ty.adminRelay)=20=E2=80=94=20remote=20browsers=20open=20the=20b?= =?UTF-8?q?ili=20panel=20through=20the=20host=20entry=20(#3373)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Headless hosts run 'dsh web' behind a reverse proxy; the settings panel iframe pointed at http://127.0.0.1:18787 and failed for every remote browser. The host now relays the /__bili/ admin surface through its own entry origin, gated by a fail-closed security.adminRelay (file-only): - dsh plugin registers a /__bili prefix route forwarding to the loopback proxy; the relayed hop is a local non-browser client (loopback source + Host, no Origin/Cookie/Referer forwarded) so the #1538 admin gate and its DNS-rebinding protection are untouched. Off by default: route answers 404, unbound proxy 503, unreachable proxy 502. - /bili/origin (and the index-inject snapshot) reports {relay:true} when enabled, so the panel UI builds same-origin URLs; the panel API root derives itself from the page path, fixing prefix gateways (/app/dsh/…) as a side effect; the origin probe caches the winning form (base-relative fallback first described in #3373-C1). - Docs: security section in CONFIGURATION.md + zh; config-reference seed. --- CONFIGURATION.md | 16 +- CONFIGURATION.zh-CN.md | 16 +- src/agent/dsh-native-client.ts | 77 +++++-- src/agent/dsh-native.ts | 135 +++++++++++- src/config.ts | 26 ++- src/web/client.ts | 52 +++-- tests/dsh-admin-relay.test.ts | 226 +++++++++++++++++++++ tests/dsh-settings-ui.test.ts | 136 +++++++++++-- tests/issue2861-host-plugin-notice.test.ts | 2 +- tests/web-storage-banner.test.ts | 2 +- website/config-reference/server.yaml | 10 + website/docs/index.html | 1 + website/main.js | 2 + 13 files changed, 639 insertions(+), 62 deletions(-) create mode 100644 tests/dsh-admin-relay.test.ts diff --git a/CONFIGURATION.md b/CONFIGURATION.md index ab12a3029b..310db77021 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -100,6 +100,7 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed | `rollout.features.salt` | string | the feature name | — | Hash salt for bucketing. Changing it re-randomizes the whole cohort (the only way to reshuffle; raising percent alone never reshuffles). | | `rollout.features.base` | string | unset (roll out over everyone) | — | Nested rollout: evaluate this feature only inside the named feature's enabled cohort, so the percent is relative to that population (roll B out to A's users; chains allowed, cycles fail closed). | | `maskHosts` | boolean | true | BILI_LOG_MASK_HOSTS | Mask non-public target hosts in logs as ; credential headers are always masked regardless. | +| `security.adminRelay` | boolean | false | — | Let an embedding host (dsh web today) relay the ENTIRE /__bili/ admin surface through its own entry origin so a remote browser can open the panel (#3373). Fail-closed, file-only. The relayed hop reaches the proxy as a local non-browser client (loopback source + loopback Host, no Origin), so the admin gate stays unchanged; enabling it exposes config editing and credential writes to whoever can reach the host web entry — protect that entry (auth / reverse proxy) first. | | `subagentSplit` | boolean | true | BILI_SUBAGENT_SPLIT | Claude Code subagents get their own session namespace (\|sub:) instead of queueing behind the main session. | | `forkAdoption` | boolean | false | BILI_FORK_ADOPTION | Anonymous forks inherit the parent's compression blocks whose source content is fully present in the forked request. | | `autoModeForge.enabled` | boolean | true | BILI_AUTO_MODE_FORGE | Answer Claude Code auto-mode safety-classifier calls locally with a forged allow verdict instead of forwarding them upstream (#2740): zero egress, zero billing for that lane; every forged verdict is audit-logged and the session carries a notice badge. false / env 0 restores pure passthrough for users who pay for real verdicts. | @@ -388,7 +389,7 @@ Top-level keys that control how the proxy listens and behaves globally. - **Type:** `string` - **Default:** `127.0.0.1` - **Status:** ACTIVE -- **Description:** Network interface the proxy binds to. `127.0.0.1` (default) listens only on localhost — safe for a local sidecar. Use `::` for IPv4 + IPv6 dual-stack. Use `0.0.0.0` (or a LAN IP) to expose the proxy to other machines — typically inside a container or on a trusted LAN: remote agents then point their model `baseURL` at `http://:/bili/…`, and MITM-mode `CONNECT` accepts remote clients for whitelisted model hosts only (blind tunnels stay loopback-only, and `/__bili/` management endpoints remain loopback-only). There is no authentication — ensure the surrounding network is trusted. Overridden by `ACP_HOST` / `--host`. +- **Description:** Network interface the proxy binds to. `127.0.0.1` (default) listens only on localhost — safe for a local sidecar. Use `::` for IPv4 + IPv6 dual-stack. Use `0.0.0.0` (or a LAN IP) to expose the proxy to other machines — typically inside a container or on a trusted LAN: remote agents then point their model `baseURL` at `http://:/bili/…`, and MITM-mode `CONNECT` accepts remote clients for whitelisted model hosts only (blind tunnels stay loopback-only, and `/__bili/` management endpoints remain loopback-only unless the embedding-host relay below is enabled). There is no authentication — ensure the surrounding network is trusted. Overridden by `ACP_HOST` / `--host`. ### `upstream` @@ -648,6 +649,19 @@ Top-level keys that control how the proxy listens and behaves globally. - **Status:** ACTIVE - **Description:** Override for the release-notes document URL. Overridden by `BILI_RELEASE_NOTES_URL`. +### `security` + +- **Type:** object +- **Default:** none of the knobs below are set (every one fails closed) +- **Status:** ACTIVE +- **Description:** Security-posture knobs — the place where turning something ON deliberately WIDENS a trust boundary, kept as one auditable block. File-only: no `BILI_*` env twin for anything in here (#2030). + +#### `security.adminRelay` (#3373) + +- **Type:** `boolean` +- **Default:** `false` (fail-closed) +- **Description:** Lets an embedding host — dsh's web panel today — relay the ENTIRE `/__bili/` admin surface through the host's own web entry, so a browser on another machine can open the panel without the proxy ever binding a non-loopback interface. Headless setup: run `dsh web`, point your reverse proxy at it, and open the bili tab from anywhere — the settings iframe and its API calls stay same-origin with the entry and ride the host's relay. Mechanics: the dsh-side bili plugin registers a `/__bili` prefix route that forwards to the loopback proxy (`127.0.0.1:18787` zone), `/bili/origin` starts reporting `{"relay":true}` so the panel UI builds same-origin URLs, and the panel's own API root derives itself from the page path — so prefix gateways (`/app/dsh/…`) work too. The admin gate itself is UNCHANGED (#1538): the relayed hop arrives at the proxy as a local non-browser client — loopback source address, loopback `Host`, no `Origin`/`Cookie`/`Referer` forwarded — exactly the shape the existing checks already admit. **Security note:** `/__bili/` is the full admin surface, not a read-only dashboard — it includes config editing and credential writes. Enabling this flag means whoever can reach the host web entry can reach all of that; protect the entry (auth / reverse proxy) before turning it on. Read live per request: toggling the file applies to the next call, no restart needed. No env var — deliberate posture decisions belong in the file. + ### `maskHosts` - **Type:** `boolean` diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index b7d26def9f..18f7eeeb36 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -100,6 +100,7 @@ | `rollout.features.salt` | string | the feature name | — | 分桶哈希盐。修改它会重新随机整个分组(唯一的洗牌方式;仅上调 percent 永不洗牌)。 | | `rollout.features.base` | string | unset (roll out over everyone) | — | 嵌套灰度:仅在所指特性的已入组人群内评估本特性,percent 相对该人群(B 只放量给 A 的用户;允许链式,环路判为关)。 | | `maskHosts` | boolean | true | BILI_LOG_MASK_HOSTS | 日志中把非公开目标主机遮成 ;凭据头无论此开关如何总是遮盖。 | +| `security.adminRelay` | boolean | false | — | 允许宿主(当前为 dsh web)把它自己入口域名下的整个 /__bili/ 管理面中继到本机代理,使远程浏览器能打开面板(#3373)。默认关闭、仅配置文件。中继跳到达代理时是本机非浏览器客户端(回环源地址 + 回环 Host、无 Origin 头),管理端门槛不变;开启后,凡能访问宿主 web 入口的人都能改配置、写凭据——请先给入口加鉴权/反代。 | | `subagentSplit` | boolean | true | BILI_SUBAGENT_SPLIT | Claude 子代理拥有独立会话命名空间(\|sub:),不再排队在主会话锁后。 | | `forkAdoption` | boolean | false | BILI_FORK_ADOPTION | 匿名 fork 会话继承父会话中源内容完整存在于本次请求里的压缩块。 | | `autoModeForge.enabled` | boolean | true | BILI_AUTO_MODE_FORGE | Claude Code auto mode 安全分类器请求由 bili 在本地代答放行判定,不再转发上游(#2740):该旁路零流出、零计费;每条代答写审计日志,会话带提示徽标。false / env 0 恢复纯透传(愿意付费要真判定的用户)。 | @@ -388,7 +389,7 @@ - **类型:** `string` - **默认值:** `127.0.0.1` - **状态:** ACTIVE -- **说明:** 代理绑定的网络接口。`127.0.0.1`(默认)仅监听本机 —— 适合本地 sidecar。使用 `::` 可同时监听 IPv4 + IPv6 双栈。使用 `0.0.0.0`(或局域网 IP)可将代理暴露给其他机器 —— 常见于容器或可信局域网: 远程 agent 把模型 `baseURL` 指向 `http://<本机>:<端口>/bili/…`,MITM 模式的 `CONNECT` 仅对白名单内的模型域名接受远程客户端(盲隧道仍仅限本机,`/__bili/` 管理端点也仍仅限本机)。没有任何鉴权 —— 请确保所在网络可信。可由 `ACP_HOST` / `--host` 覆盖。 +- **说明:** 代理绑定的网络接口。`127.0.0.1`(默认)仅监听本机 —— 适合本地 sidecar。使用 `::` 可同时监听 IPv4 + IPv6 双栈。使用 `0.0.0.0`(或局域网 IP)可将代理暴露给其他机器 —— 常见于容器或可信局域网: 远程 agent 把模型 `baseURL` 指向 `http://<本机>:<端口>/bili/…`,MITM 模式的 `CONNECT` 仅对白名单内的模型域名接受远程客户端(盲隧道仍仅限本机,`/__bili/` 管理端点也仍仅限本机,除非开启了下方的宿主中继 security.adminRelay)。没有任何鉴权 —— 请确保所在网络可信。可由 `ACP_HOST` / `--host` 覆盖。 ### `upstream` @@ -648,6 +649,19 @@ - **状态:** ACTIVE - **说明:** release notes 文档 URL 覆盖。可由 `BILI_RELEASE_NOTES_URL` 覆盖。 +### `security` + +- **类型:** 对象 +- **默认:** 各键均不设置(全部 fail-closed) +- **状态:** 生效中 +- **说明:** 安全姿态开关 —— 打开会刻意扩宽信任边界的地方,集中在一个块里便于审计。仅配置文件生效:本块任何键都没有 `BILI_*` 环境变量孪生(#2030)。 + +#### `security.adminRelay` (#3373) + +- **类型:** `boolean` +- **默认:** `false`(fail-closed) +- **说明:** 允许宿主 —— 当前是 dsh 的 web 面板 —— 把整个 `/__bili/` 管理面通过宿主自己的 web 入口中继到本机代理,让另一台机器上的浏览器无需代理绑定非回环网卡就能打开面板。无头部署:跑 `dsh web`,把反向代理指向它,在任何地方打开 bili 标签页 —— 设置 iframe 和它的 API 调用与入口同源,走宿主中继。机制:dsh 侧的 bili 插件注册 `/__bili` 前缀路由转发到回环代理(`127.0.0.1:18787` 区),`/bili/origin` 开始上报 `{"relay":true}`,面板 UI 据此改用同源 URL,面板自己的 API 根从页面路径推导 —— 前缀网关(`/app/dsh/…`)也能工作。管理端门槛本身不变(#1538):中继跳到达代理时是本机非浏览器客户端 —— 回环源地址、回环 `Host`、不携带 `Origin`/`Cookie`/`Referer` —— 与现有检查早已放行的形状完全一致。**安全提示:** `/__bili/` 是完整管理面而非只读仪表盘 —— 包含改配置与写凭据。开启此开关意味着凡能访问宿主 web 入口的人都能触及这些能力;开启前请先给入口加鉴权 / 反向代理。逐请求实时读取:改文件对下一次调用即生效,无需重启。无环境变量 —— 刻意的姿态决策属于文件。 + ### `maskHosts` - **类型:** `boolean` diff --git a/src/agent/dsh-native-client.ts b/src/agent/dsh-native-client.ts index 479afd2acc..f9e6aa845d 100644 --- a/src/agent/dsh-native-client.ts +++ b/src/agent/dsh-native-client.ts @@ -234,8 +234,13 @@ const en: Dict = { }; function readOrigin(): string | undefined { - const g = globalThis as { __BILI__?: { origin?: unknown } }; - const origin = g.__BILI__?.origin; + const g = globalThis as { __BILI__?: { origin?: unknown; relay?: unknown } }; + const snap = g.__BILI__; + // #3373: relay snapshot — the entry serves the admin surface itself, so + // the base is same-origin ("" = entry-relative paths). Prefix gateways + // correct this to an absolute base on the first live probe. + if (snap !== undefined && snap.relay === true) return ""; + const origin = snap?.origin; return typeof origin === "string" && origin.length > 0 ? origin : undefined; } @@ -244,6 +249,38 @@ function openExternal(url: string): void { if (typeof w.open === "function") w.open(url, "_blank", "noopener,noreferrer"); } +/** #3373: relay mode — the probe response carries `relay: true` instead of + * a loopback origin. Derive the UI base from the probe's own response URL: + * whatever origin+prefix carried the probe also serves /__bili/. An empty + * base means same-origin paths (root deployments, test harnesses). */ +function relayBaseOf(res: { url?: unknown }): string { + const url = typeof res.url === "string" ? res.url : ""; + if (url.endsWith(ORIGIN_PATH)) { + const base = url.slice(0, -ORIGIN_PATH.length).replace(/\/+$/, ""); + return base.length > 0 ? base : ""; + } + return ""; +} + +// #3373: gateways that mount dsh web under a path prefix re-map every URL +// the index references, so the absolute probe path misses. Probe relative to +// the page's own base as a second form; the last form that answered wins the +// next tick (direct hosts never leave the primary form). +let winningProbeForm: string | undefined; + +function probeForms(): string[] { + if (winningProbeForm !== undefined) return [winningProbeForm]; + const doc = (globalThis as { document?: { baseURI?: unknown } }).document; + const baseURI = typeof doc?.baseURI === "string" && doc.baseURI.length > 0 ? doc.baseURI : undefined; + if (baseURI === undefined) return [ORIGIN_PATH]; + try { + const fallback = new URL("bili/origin", baseURI).toString(); + return fallback === ORIGIN_PATH ? [ORIGIN_PATH] : [ORIGIN_PATH, fallback]; + } catch { + return [ORIGIN_PATH]; + } +} + /** #1809/#2187/#2288: follow the host's live origin route for the lifetime * of the mount; returns the cancel used as the effect cleanup (no fetch ⇒ * no-op, older hosts without the route simply stay on the snapshot or @@ -260,25 +297,35 @@ function probeOrigin(onOrigin: (origin: string) => void, onTheme?: (theme: "ligh let timer: ReturnType | undefined; let attempts = 0; const poll = async (): Promise => { - try { - const res = await fetch(ORIGIN_PATH); - if (res.ok) { - const data = (await res.json()) as { origin?: unknown }; - if (typeof data.origin === "string" && data.origin.length > 0 && !cancelled) { - onOrigin(data.origin); + // #3373: try the known-good form first — under a path-prefix gateway + // that becomes the relative form after its first hit. + let sawOk = false; + for (const form of probeForms()) { + try { + const res = await fetch(form); + if (!res.ok) continue; + sawOk = true; + const data = (await res.json()) as { origin?: unknown; relay?: unknown }; + let bind: string | undefined; + if (data.relay === true) bind = relayBaseOf(res); + else if (typeof data.origin === "string" && data.origin.length > 0) bind = data.origin; + if (bind !== undefined && !cancelled) { + winningProbeForm = form; + onOrigin(bind); settled = true; // #3100: every resolution is also an embed-probe retry slot — // a snapshot-only origin (older hosts without the route) // never flows through onOrigin again after mount. - if (onTick !== undefined) onTick(data.origin); - } - if (onTheme && !cancelled) { - const theme = detectHostTheme(); - if (theme !== undefined) onTheme(theme); + if (onTick !== undefined) onTick(bind); + break; } + } catch { + // host without the route (older builds) or transient error: try the next form } - } catch { - // host without the route (older builds) or transient error: retry below + } + if (sawOk && onTheme && !cancelled) { + const theme = detectHostTheme(); + if (theme !== undefined) onTheme(theme); } if (cancelled) return; attempts += 1; diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index b90ec72d19..c36b05a150 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -48,7 +48,7 @@ import path from "node:path"; import { logHostEvent } from "./host-log.js"; import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST } from "../launcher.js"; import { APIG_RESIGN_SCHEME, unresolvedRefusals } from "../apig-resign.js"; -import { resolveResignSettings } from "../config.js"; +import { adminRelayEnabled, resolveResignSettings } from "../config.js"; import { markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, singleFlight } from "./native-bootstrap.js"; import { installNativeFetchIntercept, noteRoutedOrigin, observeRoutedOrigin, type GateVerdict, type NativeInterceptState } from "./native-intercept.js"; import { asciiHeaderValue, fetchManifest, fetchProxyVersion, fetchStatus, fetchStatusLatest, forwardTool, reportRuntimeInfo, waitForProxyVersion, type ManifestTool } from "./shared.js"; @@ -169,7 +169,7 @@ export type PluginContext = { register: (route: { kind: "exact" | "prefix"; path: string; - handler: (req: unknown, res: { writeHead: (status: number, headers?: Record) => void; end: (body?: string) => void }) => void | Promise; + handler: (req: unknown, res: { writeHead: (status: number, headers?: Record) => void; write?: (chunk: string | Uint8Array) => boolean; end: (body?: string | Uint8Array) => void }) => void | Promise; }) => (() => void) | undefined; }; }; @@ -251,6 +251,109 @@ function currentOrigin(): string | undefined { return register.base ?? (envOrigin !== undefined && envOrigin.length > 0 ? envOrigin : undefined); } +/** #3373: the /__bili admin relay — dsh web's remote-panel path. With + * `security.adminRelay` on and the proxy bound, forward browser admin calls + * received on dsh's web entry to the loopback proxy, so a remote browser + * talks same-origin through the entry it already reached dsh by. The + * forwarded hop is a local non-browser client (loopback source, loopback + * Host via fetch, no Origin header), so the admin gate (#1538) holds + * unchanged; when the gate is off this stays a plain 404 (fail-closed). */ +const RELAY_REQUEST_DROP = new Set([ + "connection", "keep-alive", "proxy-authenticate", "proxy-authorization", "te", + "trailer", "transfer-encoding", "upgrade", + // fetch re-derives these from the target; the browser's values describe + // the entry origin and would leak host-session state into the proxy. + "host", "origin", "cookie", "referer", "content-length", "accept-encoding", +]); +const RELAY_RESPONSE_DROP = new Set(["connection", "keep-alive", "transfer-encoding", "content-length", "content-encoding"]); +const RELAY_MAX_BODY_BYTES = 8 * 1024 * 1024; + +type RelayRes = { writeHead: (status: number, headers?: Record) => void; write?: (chunk: string | Uint8Array) => boolean; end: (body?: string | Uint8Array) => void }; + +async function relayAdminRequest(req: unknown, res: RelayRes): Promise { + const fail = (status: number, code: string): void => { + try { + res.writeHead(status, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: code })); + } catch { + // socket already gone — nothing left to report + } + }; + const base = register.base; + if (base === undefined) { + fail(503, "bili proxy not running"); + return; + } + if (!adminRelayEnabled()) { + fail(404, "admin relay disabled"); + return; + } + const method = typeof (req as { method?: unknown }).method === "string" ? (req as { method?: string }).method : "GET"; + const url = typeof (req as { url?: unknown }).url === "string" ? (req as { url?: string }).url : "/"; + const headersIn = (req as { headers?: unknown }).headers; + const headers: Record = {}; + if (headersIn !== null && typeof headersIn === "object") { + for (const [key, value] of Object.entries(headersIn as Record)) { + if (RELAY_REQUEST_DROP.has(key)) continue; + if (typeof value === "string") headers[key] = value; + else if (Array.isArray(value)) headers[key] = value.filter((entry): entry is string => typeof entry === "string").join(", "); + } + } + let body: Uint8Array | undefined; + if (method !== "GET" && method !== "HEAD") { + const chunks: Buffer[] = []; + let size = 0; + try { + for await (const chunk of req as AsyncIterable) { + const buf = typeof chunk === "string" ? Buffer.from(chunk) : chunk instanceof Uint8Array ? Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength) : undefined; + if (buf === undefined) break; + size += buf.length; + if (size > RELAY_MAX_BODY_BYTES) { + fail(413, "request body too large"); + return; + } + chunks.push(buf); + } + body = chunks.length > 0 ? Buffer.concat(chunks) : undefined; + } catch { + fail(400, "request body read failed"); + return; + } + } + let upstream: Response; + try { + upstream = await fetch(`${base}${url}`, { + method, + headers, + body, + redirect: "manual", + signal: AbortSignal.timeout(120_000), + }); + } catch { + fail(502, "bili proxy unreachable"); + return; + } + const headersOut: Record = {}; + upstream.headers.forEach((value, key) => { + if (!RELAY_RESPONSE_DROP.has(key)) headersOut[key] = value; + }); + try { + res.writeHead(upstream.status, headersOut); + if (upstream.body !== null) { + if (typeof res.write === "function") { + for await (const chunk of upstream.body) res.write(chunk); + res.end(); + } else { + res.end(Buffer.from(await upstream.arrayBuffer())); + } + } else { + res.end(); + } + } catch { + // client hung up mid-stream — the upstream read dies with it + } +} + // #2604: host-title channel (mirror of pi.ts #2322/#2354): dsh owns its // session names; the proxy web UI labels a conversation from meta.hostTitle // when present. Pushes are fire-and-forget, deduped per (base, sid) — a @@ -1073,7 +1176,11 @@ export function apply(ctx: PluginContext): void { // a stale link. ctx.on?.("webserver/index-inject", (table: Array<{ kind: string; name?: string; value?: unknown }>) => { const origin = currentOrigin(); - if (origin !== undefined) table.push({ kind: "global", name: "__BILI__", value: { origin } }); + if (origin === undefined) return; + // #3373: with the admin relay armed the settings entry must talk + // same-origin (the entry's origin, relayed by the host) — publish the + // relay marker instead of the loopback origin. + table.push({ kind: "global", name: "__BILI__", value: adminRelayEnabled() ? { origin: null, relay: true } : { origin } }); }); // #2604: follow the host session name (dsh-session-title service). Every @@ -1105,11 +1212,20 @@ export function apply(ctx: PluginContext): void { kind: "exact" as const, path: "/bili/origin", handler: (_req: unknown, res: { writeHead: (status: number, headers?: Record) => void; end: (body?: string) => void }): void => { - const body = JSON.stringify({ origin: currentOrigin() ?? null }); + const origin = currentOrigin(); + // #3373: relay mode ⇒ the client derives its base from the probe + // response URL itself; the loopback origin is useless (and leaky) + // to a remote browser. + const body = JSON.stringify(origin !== undefined && adminRelayEnabled() ? { origin: null, relay: true } : { origin: origin ?? null }); res.writeHead(200, { "content-type": "application/json" }); res.end(body); }, }; + const adminRelayRoute = { + kind: "prefix" as const, + path: "/__bili", + handler: (req: unknown, res: RelayRes): void | Promise => relayAdminRequest(req, res), + }; if (typeof ctx.inject === "function") { // Cordis inject: fires once the service is available, re-runs on // change; the effect disposes the prior registration on each re-run @@ -1118,11 +1234,18 @@ export function apply(ctx: PluginContext): void { ctx.inject(["webServer"], (sub) => { const ws = sub.webServer; if (ws === undefined) return; - if (typeof sub.effect === "function") sub.effect(() => ws.register(originRoute), "bili: /bili/origin route"); - else ws.register(originRoute); + if (typeof sub.effect === "function") sub.effect(() => { + ws.register(originRoute); + ws.register(adminRelayRoute); + }, "bili: panel routes (origin + admin relay)"); + else { + ws.register(originRoute); + ws.register(adminRelayRoute); + } }); } else if (ctx.webServer !== undefined) { ctx.webServer.register(originRoute); + ctx.webServer.register(adminRelayRoute); } if (plan.mode === "attach") { diff --git a/src/config.ts b/src/config.ts index 8bac7de2ab..0da550cefa 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1660,6 +1660,23 @@ type FileConfig = { * BILI_ROLLOUT_=0/1 (UPPER_SNAKE name) wins over everything, * including `enabled`. */ rollout?: RolloutFileConfig; + /** Security posture block (#3373) — knobs that WIDEN a trust boundary + * live here so the exposure surface is auditable in one place. + * `adminRelay` (default false, fail-closed): allow an embedding host + * (dsh web today) to relay the ENTIRE /__bili/ admin surface — including + * config editing and credential writes, not just the read-only panel — + * through its own entry origin: the host registers a /__bili prefix + * route forwarding to the loopback proxy, so a browser on another + * machine opens the panel through the same origin it already reached + * the host by. The admin gate itself is unchanged: the relayed hop is a + * local non-browser client (loopback source + loopback Host + no + * Origin header), so #1538 DNS-rebinding protection holds verbatim. + * Enabling it means whoever can reach the host web entry can reach the + * admin surface — protect the entry (auth/reverse proxy) accordingly. + * File-only, no env var (#2030). */ + security?: { + adminRelay?: boolean; + }; /** Native-hook attach behavior. * `attachExternal` (#1335): set `true` to let native hooks attach to * lifecycle-less listeners (a manually started `bili start` daemon — no @@ -1894,7 +1911,7 @@ const KNOWN_TOP_LEVEL_KEYS = new Set([ "rollout", "dsh", "compat", "imageBilling", "imageTokenCap", "claude", "native", "resign", - "pi", "storage", + "pi", "storage", "security", // #2030 subsystem blocks: "network", "persist", "sessions", "plugin", "update", "diagnostics", "fakeCompletion", "codexCompact", "ccrRetrievalTtlMs", "decompressTmpCap", @@ -2266,6 +2283,13 @@ export function resolveNativeAttachLanePriority(): string[] { return out; } +/** #3373: the /__bili/ admin relay gate (fail-closed). Read per use — the + * config file cache makes this cheap and hot edits apply without a host + * restart. */ +export function adminRelayEnabled(): boolean { + return loadConfigFile().security?.adminRelay === true; +} + /** #2877: the native-hook discovery policy. "own-lane" (default) rides only a * same-lane daemon and never falls back to a lane-less / user-zone one; * "global-fallback" restores the pre-#2877 fallback. Env diff --git a/src/web/client.ts b/src/web/client.ts index b53b91b19a..dfce0e086b 100644 --- a/src/web/client.ts +++ b/src/web/client.ts @@ -21,6 +21,18 @@ export const WEB_CLIENT = `(function () { else if (/^en([-_]|$)/i.test(navigator.language || "")) locale = "en"; } } catch (e) {} + // #3373: when the panel is served through a host relay under a path + // prefix (e.g. /app/dsh/__bili/…), the wire root is that prefixed path, + // not the site root. Derive it from the document's own pathname — the + // only prefix source that survives gateway injection. Direct + // serving (/__bili/) keeps the byte-identical root "/__bili". + let API_ROOT = "/__bili"; + try { + if (typeof location !== "undefined" && location.pathname) { + const m = /^(.*\\/)__bili(\\/|$)/.exec(location.pathname); + if (m && m[1] !== undefined) API_ROOT = m[1] + "__bili"; + } + } catch (e) {} function t(key, vars) { let text = MESSAGES[locale][key]; if (text === undefined) text = MESSAGES["zh-CN"][key]; @@ -273,7 +285,7 @@ export const WEB_CLIENT = `(function () { async function putCfg(btn, payload) { busy(btn, true); try { - await json("/__bili/config", { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify(payload) }); + await json(API_ROOT + "/config", { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify(payload) }); toast(t("cfg.saved"), "ok"); loadConfig(); } catch (e) { @@ -480,7 +492,7 @@ export const WEB_CLIENT = `(function () { async function loadOverview(silent) { try { - const d = await json("/__bili/overview"); + const d = await json(API_ROOT + "/overview"); const o = d.overview || {}; // #1426: total splits live vs historical (disk-restored pool entries are history); // counters without usage samples render "—" instead of a misleading 0; the saved @@ -632,7 +644,7 @@ export const WEB_CLIENT = `(function () { btn.addEventListener("click", async () => { busy(btn, true); try { - await json("/__bili/conflicts/clear", { method: "POST" }); + await json(API_ROOT + "/conflicts/clear", { method: "POST" }); toast(t("conflict.cleared"), "ok"); await loadOverview(true); } catch (e) { @@ -704,7 +716,7 @@ export const WEB_CLIENT = `(function () { btn.addEventListener("click", async () => { busy(btn, true); try { - await json("/__bili/host-plugins/dismiss", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ id: btn.getAttribute("data-hpid") || "" }) }); + await json(API_ROOT + "/host-plugins/dismiss", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ id: btn.getAttribute("data-hpid") || "" }) }); toast(t("hostplug.dismissed"), "ok"); await loadOverview(true); } catch (e) { @@ -832,7 +844,7 @@ export const WEB_CLIENT = `(function () { function sessionListUrl(page) { const input = $("ses-search"); const q = ((input && input.value) || "").trim(); - return "/__bili/sessions?page=" + page + "&pageSize=" + SES_PAGE_SIZE + (q ? "&q=" + encodeURIComponent(q) : ""); + return API_ROOT + "/sessions?page=" + page + "&pageSize=" + SES_PAGE_SIZE + (q ? "&q=" + encodeURIComponent(q) : ""); } async function loadMoreSessions() { const btn = $("ses-loadmore"); @@ -1703,7 +1715,7 @@ export const WEB_CLIENT = `(function () { host.innerHTML = '
' + t("common.loading") + "
"; let d = null; try { - d = await json("/__bili/sessions/" + encodeURIComponent(id) + "/detail"); + d = await json(API_ROOT + "/sessions/" + encodeURIComponent(id) + "/detail"); } catch (e) {} if (!d) { host.innerHTML = '' + t("common.back") + "" @@ -1722,7 +1734,7 @@ export const WEB_CLIENT = `(function () { cc.addEventListener("click", async () => { busy(cc, true); try { - await json("/__bili/conflicts/clear?session=" + encodeURIComponent(id), { method: "POST" }); + await json(API_ROOT + "/conflicts/clear?session=" + encodeURIComponent(id), { method: "POST" }); toast(t("det.conflicts_cleared"), "ok"); await loadDetail(id); } catch (e) { @@ -1793,7 +1805,7 @@ export const WEB_CLIENT = `(function () { if (hit && Date.now() - hit.at < DIAG_CACHE_MS) return bindReport(hit.d, copyBtn, issueLink); busy(copyBtn, true); try { - const d = await json("/__bili/diagnostics?topic=" + encodeURIComponent(topic)); + const d = await json(API_ROOT + "/diagnostics?topic=" + encodeURIComponent(topic)); diagCache[topic] = { at: Date.now(), d }; bindReport(d, copyBtn, issueLink); } catch (e) { @@ -1863,7 +1875,7 @@ export const WEB_CLIENT = `(function () { if (mdBtn) mdBtn.addEventListener("click", async function () { busy(mdBtn, true); try { - var r = await json("/__bili/session-report?format=md&" + q); + var r = await json(API_ROOT + "/session-report?format=md&" + q); if (!r || typeof r.md !== "string") throw new Error("no report"); downloadUrl(URL.createObjectURL(new Blob([r.md], { type: "text/markdown;charset=utf-8" })), base + ".md"); } catch (e) { toast(t("toast.failed", { msg: e.message }), "err"); } finally { busy(mdBtn, false); } @@ -1871,7 +1883,7 @@ export const WEB_CLIENT = `(function () { if (pngBtn) pngBtn.addEventListener("click", async function () { busy(pngBtn, true); try { - var r = await json("/__bili/session-report?format=svg&" + q); + var r = await json(API_ROOT + "/session-report?format=svg&" + q); if (!r || !r.svg) throw new Error("no report"); var blob = await svgToPngBlob(r.svg, r.w || 920, r.h || 800); downloadUrl(URL.createObjectURL(blob), base + ".png"); @@ -1906,7 +1918,7 @@ export const WEB_CLIENT = `(function () { const dlB = $("cacherpt-dl"); if (!copyB || !dlB) return; const grab = async () => { - const r = await json("/__bili/cache-report?session=" + encodeURIComponent(d.id)); + const r = await json(API_ROOT + "/cache-report?session=" + encodeURIComponent(d.id)); const rep = r && Array.isArray(r.reports) && r.reports[0] ? r.reports[0].report : null; if (!rep) throw new Error("no cache report for this session yet"); return rep; @@ -2009,7 +2021,7 @@ export const WEB_CLIENT = `(function () { async function loadConfig() { try { - const cfg = await json("/__bili/config"); + const cfg = await json(API_ROOT + "/config"); const cfgPathEl = $("cfg-path"); if (cfgPathEl) cfgPathEl.textContent = cfg.path || t("common.empty"); const fileBtn = $("copy-cfg-file"); @@ -2075,7 +2087,7 @@ export const WEB_CLIENT = `(function () { if (!box) return; let data; try { - data = await json("/__bili/resign"); + data = await json(API_ROOT + "/resign"); } catch (e) { const el = document.createElement("div"); el.className = "dim small"; @@ -2573,7 +2585,7 @@ export const WEB_CLIENT = `(function () { async function saveKey(btn, value) { busy(btn, true); try { - const result = await json("/__bili/external-summary/credential", { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify({ name: recipe.credentialRef, key: value }) }); + const result = await json(API_ROOT + "/external-summary/credential", { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify({ name: recipe.credentialRef, key: value }) }); key.value = ""; status[ref] = result.configured; render(); toast(t("summary.key_saved"), "ok"); @@ -2602,7 +2614,7 @@ export const WEB_CLIENT = `(function () { } async function loadUpstream(cfg) { let up = null; - try { up = await json("/__bili/upstream"); } catch (e) {} + try { up = await json(API_ROOT + "/upstream"); } catch (e) {} // #1426: mode/proxy are editable form fields again, not read-only labels const mode = (up && up.mode) || cfg.upstreamProxyMode || "auto"; document.querySelectorAll('input[name="proxy-mode"]').forEach((el) => { el.checked = el.value === mode; }); @@ -2634,7 +2646,7 @@ export const WEB_CLIENT = `(function () { const q = (qEl.value || "").trim(); try { const linesSel = $("log-lines"); - const d = await json("/__bili/logs?q=" + encodeURIComponent(q) + "&lines=" + (linesSel ? linesSel.value : "500") + logViewExtras()); + const d = await json(API_ROOT + "/logs?q=" + encodeURIComponent(q) + "&lines=" + (linesSel ? linesSel.value : "500") + logViewExtras()); $("log-path").textContent = d.path || t("logs.empty"); $("copy-log-path").dataset.copy = d.path || ""; $("log-count").textContent = d.total > 0 @@ -2728,7 +2740,7 @@ export const WEB_CLIENT = `(function () { busy(ldl, true); try { const q = ($("log-search").value || "").trim(); - const r = await fetch("/__bili/logs?raw=1&q=" + encodeURIComponent(q) + "&lines=2000" + logViewExtras()); + const r = await fetch(API_ROOT + "/logs?raw=1&q=" + encodeURIComponent(q) + "&lines=2000" + logViewExtras()); const blob = await r.blob(); const a = document.createElement("a"); a.href = URL.createObjectURL(blob); @@ -2749,7 +2761,7 @@ export const WEB_CLIENT = `(function () { try { // Full unfiltered log (rotated .old + current) regardless of the // search box — the honest "download everything" path. - const r = await fetch("/__bili/logs?raw=1&all=1"); + const r = await fetch(API_ROOT + "/logs?raw=1&all=1"); const blob = await r.blob(); const a = document.createElement("a"); a.href = URL.createObjectURL(blob); @@ -2768,7 +2780,7 @@ export const WEB_CLIENT = `(function () { if (testBtn) testBtn.addEventListener("click", async () => { busy(testBtn, true); try { - const r = await json("/__bili/upstream/test", { method: "POST" }); + const r = await json(API_ROOT + "/upstream/test", { method: "POST" }); // #1426: an HTTP >= 400 answer still proves the network path works — auth is the // client's job, so report reachability instead of a flat failure const st = $("up-state"); @@ -2820,7 +2832,7 @@ export const WEB_CLIENT = `(function () { if (cp) cp.addEventListener("click", async () => { busy(cp, true); try { - await json("/__bili/config", { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify({ passthrough: null }) }); + await json(API_ROOT + "/config", { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify({ passthrough: null }) }); toast(t("toast.passthrough_cleared"), "ok"); loadConfig(); } catch (e) { diff --git a/tests/dsh-admin-relay.test.ts b/tests/dsh-admin-relay.test.ts new file mode 100644 index 0000000000..c785ef9148 --- /dev/null +++ b/tests/dsh-admin-relay.test.ts @@ -0,0 +1,226 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import http from "node:http"; +import { apply, _resetRegisterForTest } from "../src/agent/dsh-native.ts"; +import { rmrf } from "./tmp-rm.ts"; + +// The node:test runner sets NODE_TEST_CONTEXT itself (see tests/e2e/README.md); +// set it defensively so a direct single-file run also stands down the spawn +// bootstrap and the global fetch intercept (the relay handler must exercise +// the REAL fetch — that is the hop the admin gate sees). +process.env.NODE_TEST_CONTEXT = process.env.NODE_TEST_CONTEXT ?? "1"; + +async function withEnv(env: Record, fn: () => Promise | T): Promise { + const prev: Record = {}; + for (const [key, value] of Object.entries(env)) { + prev[key] = process.env[key]; + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + try { + return await fn(); + } finally { + for (const [key, value] of Object.entries(prev)) { + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + } +} + +type RouteRow = { kind: string; path: string; handler: (req: unknown, res: RelayRes) => void | Promise }; + +class RelayRes { + status?: number; + headers?: Record; + chunks: string[] = []; + writeHead(status: number, headers?: Record): void { + this.status = status; + this.headers = headers; + } + write(chunk: string | Uint8Array): boolean { + this.chunks.push(typeof chunk === "string" ? chunk : Buffer.from(chunk).toString("utf8")); + return true; + } + end(body?: string | Uint8Array): void { + if (body !== undefined) this.chunks.push(typeof body === "string" ? body : Buffer.from(body).toString("utf8")); + } + get body(): string { + return this.chunks.join(""); + } +} + +function fakeReq(method: string, url: string, headers: Record, bodyChunks: string[] = []): unknown { + return { + method, + url, + headers, + async *[Symbol.asyncIterator]() { + for (const chunk of bodyChunks) yield chunk; + }, + }; +} + +/** A browser admin call as it arrives on the dsh entry: the browser's Origin + * and cookies describe the ENTRY, not the proxy — the relay must drop both + * so the forwarded hop reads as a local non-browser client (#1538 gate). */ +const browserHeaders = (): Record => ({ + "content-type": "application/json", + host: "dsh.example:443", + origin: "https://dsh.example", + cookie: "dsh-session=secret", + referer: "https://dsh.example/settings", + "user-agent": "relay-test", +}); + +type Seen = { method: string; url: string; headers: http.IncomingHttpHeaders; body: string }; + +function startAdminStub(): Promise<{ origin: string; seen: Seen[]; close: () => void }> { + const seen: Seen[] = []; + const server = http.createServer((req, res) => { + let body = ""; + req.on("data", (c: Buffer) => { + body += c.toString("utf8"); + }); + req.on("end", () => { + seen.push({ method: req.method ?? "", url: req.url ?? "", headers: req.headers, body }); + if ((req.url ?? "").startsWith("/__bili/config")) { + res.writeHead(200, { "content-type": "application/json", "x-bili-relay": "yes" }); + res.end(JSON.stringify({ ok: true, echoed: body })); + return; + } + res.writeHead(404, { "content-type": "application/json" }); + res.end("{}"); + }); + }); + return new Promise((resolve) => { + server.listen(0, "127.0.0.1", () => { + const port = (server.address() as { port: number }).port; + resolve({ origin: `http://127.0.0.1:${port}`, seen, close: () => server.close() }); + }); + }); +} + +function collectRoutes(): RouteRow[] { + const routes: RouteRow[] = []; + const ctx = { + tools: { register: (_t: unknown) => {} }, + commands: { register: (_c: unknown) => {} }, + agents: { currentInitiator: () => ({ session: { id: "s-3373" } }) }, + inject: (_deps: readonly string[], cb: (sub: unknown) => void) => { + cb({ webServer: { register: (r: RouteRow) => routes.push(r) } }); + }, + on: (_event: string, _listener: unknown) => {}, + }; + apply(ctx as Parameters[0]); + return routes; +} + +function relayRoute(routes: RouteRow[]): RouteRow { + const route = routes.find((r) => r.path === "/__bili"); + assert.ok(route !== undefined, "the /__bili prefix route must be registered"); + assert.equal(route.kind, "prefix"); + return route; +} + +function writeConfig(home: string, body: string): string { + const file = path.join(home, "bili-config.json"); + fs.writeFileSync(file, body); + return file; +} + +test("#3373: the admin relay stays fail-closed while security.adminRelay is off — even with a bound proxy and the route mounted", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-relay-")); + try { + // register.base points at a port with nothing listening: if the relay + // ever left the fail-closed branch it would 502, not 404. + await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: undefined, BILI_CONFIG_FILE: writeConfig(home, "{}") }, async () => { + _resetRegisterForTest("http://127.0.0.1:1"); + const route = relayRoute(collectRoutes()); + const res = new RelayRes(); + await route.handler(fakeReq("GET", "/__bili/config", browserHeaders()), res); + assert.equal(res.status, 404); + assert.deepEqual(JSON.parse(res.body), { error: "admin relay disabled" }); + }); + } finally { + rmrf(home); + } +}); + +test("#3373: with the gate on but no proxy bound, the relay answers 503 instead of pretending", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-relay-")); + try { + await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: undefined, BILI_CONFIG_FILE: writeConfig(home, JSON.stringify({ security: { adminRelay: true } })) }, async () => { + _resetRegisterForTest(undefined); + const route = relayRoute(collectRoutes()); + const res = new RelayRes(); + await route.handler(fakeReq("GET", "/__bili/config", browserHeaders()), res); + assert.equal(res.status, 503); + assert.deepEqual(JSON.parse(res.body), { error: "bili proxy not running" }); + }); + } finally { + rmrf(home); + } +}); + +test("#3373: gate on + proxy bound — the relay forwards to loopback as a local non-browser client (no Origin/Cookie/Referer, fetch-derived loopback Host), passes method/url/body/status/headers through, and streams the response", async () => { + const stub = await startAdminStub(); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-relay-")); + try { + await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: undefined, BILI_CONFIG_FILE: writeConfig(home, JSON.stringify({ security: { adminRelay: true } })) }, async () => { + _resetRegisterForTest(stub.origin); + const route = relayRoute(collectRoutes()); + + const get = new RelayRes(); + await route.handler(fakeReq("GET", "/__bili/config", browserHeaders()), get); + assert.equal(get.status, 200); + assert.equal(get.headers?.["content-type"], "application/json"); + assert.equal(get.headers?.["x-bili-relay"], "yes"); + assert.equal(JSON.parse(get.body).ok, true); + + const post = new RelayRes(); + await route.handler(fakeReq("POST", "/__bili/config", browserHeaders(), ["{", '"a":1}']), post); + assert.equal(post.status, 200); + assert.equal(JSON.parse(post.body).echoed, '{"a":1}', "request body rides through byte-exact"); + + // apply() also probes the bound proxy (manifest/liveness) — count + // only the relayed admin calls. + const admin = stub.seen.filter((h) => h.url.startsWith("/__bili/config")); + assert.equal(admin.length, 2); + const first = admin[0]; + assert.equal(first.method, "GET"); + assert.equal(first.url, "/__bili/config", "the full /__bili path is preserved"); + const second = admin[1]; + assert.equal(second.method, "POST"); + assert.equal(second.body, '{"a":1}'); + for (const hit of [first, second]) { + assert.equal(hit.headers.host, new URL(stub.origin).host, "Host is the fetch-derived loopback host, not the browser's entry host"); + assert.equal(hit.headers.origin, undefined, "the browser's Origin header must not reach the admin gate"); + assert.equal(hit.headers.cookie, undefined, "entry cookies must not leak into the proxy"); + assert.equal(hit.headers.referer, undefined); + assert.equal(hit.headers["user-agent"], "relay-test", "ordinary headers ride through"); + } + }); + } finally { + stub.close(); + rmrf(home); + } +}); + +test("#3373: an unreachable proxy behind the gate surfaces as 502 JSON, not a crash", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-relay-")); + try { + await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: undefined, BILI_CONFIG_FILE: writeConfig(home, JSON.stringify({ security: { adminRelay: true } })) }, async () => { + _resetRegisterForTest("http://127.0.0.1:1"); + const route = relayRoute(collectRoutes()); + const res = new RelayRes(); + await route.handler(fakeReq("GET", "/__bili/config", browserHeaders()), res); + assert.equal(res.status, 502); + assert.deepEqual(JSON.parse(res.body), { error: "bili proxy unreachable" }); + }); + } finally { + rmrf(home); + } +}); diff --git a/tests/dsh-settings-ui.test.ts b/tests/dsh-settings-ui.test.ts index 13c7c89392..b1a5a2092e 100644 --- a/tests/dsh-settings-ui.test.ts +++ b/tests/dsh-settings-ui.test.ts @@ -48,6 +48,15 @@ function startManifestProxy(): Promise<{ origin: string; close: () => void }> { }); } +// #3373: hermetic config isolation — the relay gate reads the real config +// file unless pointed elsewhere; a machine with security.adminRelay on must +// not flip these tests. +function emptyConfigFile(home: string): string { + const file = path.join(home, "bili-config.json"); + fs.writeFileSync(file, "{}"); + return file; +} + type InjectRow = { kind: string; name?: string; value?: unknown }; type InjectListener = (table: InjectRow[]) => void; @@ -67,7 +76,7 @@ test("#1590: webserver/index-inject publishes __BILI__ while an origin is known const proxy = await startManifestProxy(); const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-ui-")); try { - await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: proxy.origin }, async () => { + await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: proxy.origin, BILI_CONFIG_FILE: emptyConfigFile(home) }, async () => { _resetRegisterForTest(proxy.origin); const listeners: InjectListener[] = []; apply(uiCtx(listeners) as Parameters[0]); @@ -87,7 +96,7 @@ test("#1590: webserver/index-inject publishes __BILI__ while an origin is known test("#1590: index-inject stays silent when no origin is known yet (spawn mode)", async () => { const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-ui-")); try { - await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: undefined }, async () => { + await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: undefined, BILI_CONFIG_FILE: emptyConfigFile(home) }, async () => { _resetRegisterForTest(undefined); const listeners: InjectListener[] = []; apply(uiCtx(listeners) as Parameters[0]); @@ -133,15 +142,19 @@ function routeCtx(routes: RouteRow[], listeners: InjectListener[]) { test("#1809: live /bili/origin route reflects the bound origin", async () => { const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-ui-")); try { - await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: undefined }, async () => { + await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: undefined, BILI_CONFIG_FILE: emptyConfigFile(home) }, async () => { _resetRegisterForTest("http://127.0.0.1:8787"); const routes: RouteRow[] = []; apply(routeCtx(routes, []) as Parameters[0]); - assert.equal(routes.length, 1); - assert.equal(routes[0].kind, "exact"); - assert.equal(routes[0].path, "/bili/origin"); + // #3373: the panel now registers two routes — the origin probe and + // the /__bili admin relay (fail-closed while the gate is off). + assert.equal(routes.length, 2); + const originRoute = routes.find((r) => r.path === "/bili/origin")!; + const relayRoute = routes.find((r) => r.path === "/__bili")!; + assert.equal(originRoute.kind, "exact"); + assert.equal(relayRoute.kind, "prefix"); const res = new FakeRes(); - await routes[0].handler({}, res); + await originRoute.handler({}, res); assert.equal(res.status, 200); assert.equal(res.headers?.["content-type"], "application/json"); assert.deepEqual(JSON.parse(res.body), { origin: "http://127.0.0.1:8787" }); @@ -154,13 +167,14 @@ test("#1809: live /bili/origin route reflects the bound origin", async () => { test("#1809: /bili/origin answers null before binding", async () => { const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-ui-")); try { - await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: undefined }, async () => { + await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: undefined, BILI_CONFIG_FILE: emptyConfigFile(home) }, async () => { _resetRegisterForTest(undefined); const routes: RouteRow[] = []; apply(routeCtx(routes, []) as Parameters[0]); - assert.equal(routes.length, 1); + assert.equal(routes.length, 2); + const originRoute = routes.find((r) => r.path === "/bili/origin")!; const res = new FakeRes(); - await routes[0].handler({}, res); + await originRoute.handler({}, res); assert.deepEqual(JSON.parse(res.body), { origin: null }); }); } finally { @@ -171,7 +185,7 @@ test("#1809: /bili/origin answers null before binding", async () => { test("#1809: route registration rides the injected context's effect lifecycle", async () => { const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-ui-")); try { - await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: undefined }, async () => { + await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: undefined, BILI_CONFIG_FILE: emptyConfigFile(home) }, async () => { _resetRegisterForTest("http://127.0.0.1:8787"); const routes: RouteRow[] = []; let disposed = 0; @@ -197,12 +211,14 @@ test("#1809: route registration rides the injected context's effect lifecycle", }; apply(ctx as Parameters[0]); assert.equal(effects.length, 1, "registration must be wrapped in the context effect"); - assert.equal(effects[0].label, "bili: /bili/origin route"); + assert.equal(effects[0].label, "bili: panel routes (origin + admin relay)"); const cleanup = effects[0].fn(); - assert.equal(routes.length, 1); - assert.equal(typeof cleanup, "function", "the disposer doubles as the effect cleanup"); - (cleanup as () => void)(); - assert.equal(disposed, 1); + assert.equal(routes.length, 2); + assert.ok(cleanup === undefined || typeof cleanup === "function", "the disposer doubles as the effect cleanup"); + if (typeof cleanup === "function") { + cleanup(); + assert.equal(disposed, 2); + } }); } finally { rmrf(home); @@ -546,6 +562,94 @@ test("#1590/#2125: client bundle registers the settings.section entry bili AND t } }); +test("#3373: relay mode — probe {origin:null,relay:true} binds same-origin; prefix gateways resolve through the base-relative fallback form", async () => { + { + // Root relay deployment: no response URL ⇒ base "" (same-origin paths). + const opened: string[] = []; + const m = await mountClient({ + open: (url: string) => opened.push(url), + fetch: async (url: string) => { + if (String(url).endsWith("/__bili/?embed=1")) return htmlOk(); + return { ok: true, json: async () => ({ origin: null, relay: true }) }; + }, + setTimeout, + clearTimeout, + }); + m.resetHooks(); + const first = m.component() as ElementNode; + // No snapshot here — first paint is the degraded hint until the first + // poll resolves (the snapshot path is covered below). + assert.equal(findTag(first, "iframe"), undefined, "#3100: the frame still waits for the pre-flight verdict"); + await tick(); + m.component(); + await tick(); + const second = m.component() as ElementNode; + const ft: string[] = []; + collectText(second, ft); + assert.ok(!ft.some((x) => x.includes("/acp")), "relay mode is bound, not degraded"); + const frame = findTag(second, "iframe"); + assert.ok(frame !== undefined, "relay mode renders the frame"); + assert.equal(frame.props?.src, "/__bili/?embed=1&lang=zh#/overview", "same-origin frame under the entry relay"); + const buttons = collectButtons(second); + (buttons[buttons.length - 1].props!.onClick as () => void)(); + assert.deepEqual(opened, ["/__bili/"]); + m.runCleanups(); + } + { + // Prefix gateway (fnOS-style /app/dsh/): the absolute probe path misses; + // the base-relative fallback form carries the prefix and its response + // URL becomes the absolute UI base. + const m = await mountClient({ + document: { baseURI: "https://dsh.example/app/dsh/" }, + URL, + open: (_url: string) => {}, + fetch: async (url: string) => { + const u = String(url); + if (u === "/bili/origin") return { ok: false }; + if (u.endsWith("/bili/origin")) return { ok: true, url: u, json: async () => ({ origin: null, relay: true }) }; + if (u.endsWith("/__bili/?embed=1")) return htmlOk(); + throw new Error(`unexpected fetch: ${u}`); + }, + setTimeout, + clearTimeout, + }); + m.resetHooks(); + m.component(); + await tick(); + m.component(); + await tick(); + const tree = m.component() as ElementNode; + const frame = findTag(tree, "iframe"); + assert.ok(frame !== undefined, "prefix gateway resolves through the fallback probe form"); + assert.equal(frame.props?.src, "https://dsh.example/app/dsh/__bili/?embed=1&lang=zh#/overview"); + m.runCleanups(); + } + { + // Relay snapshot (__BILI__ carries relay:true): first paint is bound. + const m = await mountClient({ + __BILI__: { origin: null, relay: true }, + open: (_url: string) => {}, + fetch: async (url: string) => { + if (String(url).endsWith("/__bili/?embed=1")) return htmlOk(); + return { ok: true, json: async () => ({ origin: null, relay: true }) }; + }, + setTimeout, + clearTimeout, + }); + m.resetHooks(); + const first = m.component() as ElementNode; + const ft: string[] = []; + collectText(first, ft); + assert.ok(!ft.some((x) => x.includes("/acp")), "relay snapshot is bound from first paint"); + await tick(); + m.component(); + await tick(); + const second = m.component() as ElementNode; + assert.ok(findTag(second, "iframe") !== undefined, "relay snapshot + live route renders the frame"); + m.runCleanups(); + } +}); + test("#1809/#2125/#2187: client polls /bili/origin while unresolved — upgrades on success, stays degraded and cancels when absent (both slots); slow phase continues past the fast attempts; #2288 corrects a stale boot snapshot and follows mid-session re-binds", async () => { // #3100: every fetch stub below dispatches on URL — the host origin route // answers JSON, the proxy embed pre-flight answers HTML (htmlOk above). diff --git a/tests/issue2861-host-plugin-notice.test.ts b/tests/issue2861-host-plugin-notice.test.ts index 6173b429b2..10eda6ff78 100644 --- a/tests/issue2861-host-plugin-notice.test.ts +++ b/tests/issue2861-host-plugin-notice.test.ts @@ -214,7 +214,7 @@ test("#2861: wiring drift guards (seam export, banner slot, i18n refs, payload f for (const k of ['t("hostplug.on")', 't("hostplug.desc")', 't("hostplug.hint")', 't("hostplug.dismiss_btn")', 't("hostplug.dismissed")']) { assert.ok(WEB_CLIENT.includes(k), `i18n key ${k} referenced literally (dead-key lint)`); } - assert.ok(WEB_CLIENT.includes("/__bili/host-plugins/dismiss"), "dismiss POST target present"); + assert.ok(WEB_CLIENT.includes('API_ROOT + "/host-plugins/dismiss"'), "dismiss POST target present"); const here = dirname(fileURLToPath(import.meta.url)); const adminSrc = fs.readFileSync(join(here, "..", "src", "server", "admin.ts"), "utf8"); assert.ok(adminSrc.includes("hostPluginNotices: visibleHostPluginNotices(process.env)"), "overview payload carries the field"); diff --git a/tests/web-storage-banner.test.ts b/tests/web-storage-banner.test.ts index 1b8e31f473..f6a65a2653 100644 --- a/tests/web-storage-banner.test.ts +++ b/tests/web-storage-banner.test.ts @@ -195,7 +195,7 @@ test("#3040 forced none-engine: status+overview report degraded; diagnostics blo assert.ok(WEB_CLIENT.includes('t("sys.storage_degraded"'), "client composes the localized sentence with {reason}"); assert.ok(WEB_CLIENT.includes('t("sys.report_copy")'), "client offers the copy button"); assert.ok(WEB_CLIENT.includes('t("sys.report_issue")'), "client offers the GitHub button"); - assert.ok(WEB_CLIENT.includes("/__bili/diagnostics?topic="), "buttons wire to the generic diagnostics endpoint"); + assert.ok(WEB_CLIENT.includes('API_ROOT + "/diagnostics?topic="'), "buttons wire to the generic diagnostics endpoint (through the #3373 API root)"); // the overview poll re-renders the banner every 5s while degraded: the // diagnostics fetch must be cached per topic, and a failed fetch must // toast once — otherwise every poll re-hits the endpoint (full blob diff --git a/website/config-reference/server.yaml b/website/config-reference/server.yaml index 9c46aba1d8..f1d89529cf 100644 --- a/website/config-reference/server.yaml +++ b/website/config-reference/server.yaml @@ -190,6 +190,16 @@ entries: desc_zh: "日志中把非公开目标主机遮成 ;凭据头无论此开关如何总是遮盖。" site_en: "maskHosts — mask non-public host names in logs as <private-host> (on by default; credentials are always masked)." site_zh: "maskHosts —— 日志中把非公开主机名遮成 <private-host>(默认开;凭据总是遮盖)。" + - key: security.adminRelay + type: boolean + default: "false" + group: behavior + id: tt.adminrelay + site_group: beh + desc_en: "Let an embedding host (dsh web today) relay the ENTIRE /__bili/ admin surface through its own entry origin so a remote browser can open the panel (#3373). Fail-closed, file-only. The relayed hop reaches the proxy as a local non-browser client (loopback source + loopback Host, no Origin), so the admin gate stays unchanged; enabling it exposes config editing and credential writes to whoever can reach the host web entry — protect that entry (auth / reverse proxy) first." + desc_zh: "允许宿主(当前为 dsh web)把它自己入口域名下的整个 /__bili/ 管理面中继到本机代理,使远程浏览器能打开面板(#3373)。默认关闭、仅配置文件。中继跳到达代理时是本机非浏览器客户端(回环源地址 + 回环 Host、无 Origin 头),管理端门槛不变;开启后,凡能访问宿主 web 入口的人都能改配置、写凭据——请先给入口加鉴权/反代。" + site_en: "security.adminRelay — relay the /__bili/ admin surface through the embedding host's web entry (off by default; fail-closed)." + site_zh: "security.adminRelay —— 将 /__bili/ 管理面经宿主 web 入口中继(默认关;fail-closed)。" - key: subagentSplit type: boolean default: "true" diff --git a/website/docs/index.html b/website/docs/index.html index 24e9b309f9..c336b991f9 100644 --- a/website/docs/index.html +++ b/website/docs/index.html @@ -264,6 +264,7 @@

Other top-level fields

  • Behavior toggles
  • rollout — canary gating: features enable for a percent of installs, bucketed on a stable per-machine UUID; nested base rolls B out only inside A's cohort.
  • maskHosts — mask non-public host names in logs as <private-host> (on by default; credentials are always masked).
  • +
  • security.adminRelay — relay the /__bili/ admin surface through the embedding host's web entry (off by default; fail-closed).
  • subagentSplit — Claude subagents get their own session namespace (on by default).
  • forkAdoption — adopt anonymous forks into their parent session (off by default).
  • autoModeForge.enabled — answer Claude Code auto-mode classifier calls locally with a forged allow verdict (on by default; BILI_AUTO_MODE_FORGE=0 = pure passthrough).
  • diff --git a/website/main.js b/website/main.js index 35a9f413ce..36be69cdae 100644 --- a/website/main.js +++ b/website/main.js @@ -144,6 +144,7 @@ "tt.ib2": "imageBilling — global image-token estimation: \"auto\" (default → pixels), \"pixels\" or \"bytes\" (explicit opt-in for byte relays).", "tt.itc2": "imageTokenCap — global per-image token cap; unset = uncapped (BILI_IMAGE_TOKEN_CAP wins, read live).", "tt.mask": "maskHosts — mask non-public host names in logs as <private-host> (on by default; credentials are always masked).", + "tt.adminrelay": "security.adminRelay — relay the /__bili/ admin surface through the embedding host's web entry (off by default; fail-closed).", "tt.subagent": "subagentSplit — Claude subagents get their own session namespace (on by default).", "tt.fork": "forkAdoption — adopt anonymous forks into their parent session (off by default).", "tt.autoforge": "autoModeForge.enabled — answer Claude Code auto-mode classifier calls locally with a forged allow verdict (on by default; BILI_AUTO_MODE_FORGE=0 = pure passthrough).", @@ -383,6 +384,7 @@ "tt.ib2": "imageBilling —— 全局图片 token 估算口径:\"auto\"(默认→pixels)、\"pixels\" 或 \"bytes\"(字节中继需显式指定)。", "tt.itc2": "imageTokenCap —— 全局单图 token 上限;未设置 = 不限(BILI_IMAGE_TOKEN_CAP 优先且实时读取)。", "tt.mask": "maskHosts —— 日志中把非公开主机名遮成 <private-host>(默认开;凭据总是遮盖)。", + "tt.adminrelay": "security.adminRelay —— 将 /__bili/ 管理面经宿主 web 入口中继(默认关;fail-closed)。", "tt.subagent": "subagentSplit —— Claude 子代理拥有独立会话命名空间(默认开)。", "tt.fork": "forkAdoption —— 匿名 fork 会话并入父会话(默认关)。", "tt.autoforge": "autoModeForge.enabled —— Claude Code auto mode 分类器请求本地代答放行判定(默认开;BILI_AUTO_MODE_FORGE=0 纯透传)。", From e9f7b24ba856dab4b37de17cee0781934be248fa Mon Sep 17 00:00:00 2001 From: awork Date: Sat, 17 Oct 2026 13:44:21 +0800 Subject: [PATCH 2/2] fix: admin relay review fixes (#3373): path containment, fail-closed ordering, effect disposer - contain the relay to the declared /__bili surface: reject decoded ".." segments (literal and %2e-encoded; fetch() normalizes both) with 400 before any upstream hop; an unchecked "/__bili/.." would otherwise land on sibling paths of the loopback proxy (#3486 review F1) - present 404 admin-relay-disabled before the 503 not-running state when the gate is off (fail-closed beats state disclosure; #3486 review F3) - restore the combined route disposer as the inject-effect cleanup so dsh disposes both panel routes on each re-run, matching master's effect contract; re-tighten the dsh-settings-ui assertion that had softened to dead code (#3486 review F2) - type the method/url extraction through locals so the typeof guard actually narrows (new strict usages exposed string|undefined) - regression tests: traversal rejected with zero upstream hops, gate-off+unbound 404 ordering, 413 oversized body with no upstream hop --- src/agent/dsh-native.ts | 39 ++++++++++++++++++---- tests/dsh-admin-relay.test.ts | 61 +++++++++++++++++++++++++++++++++++ tests/dsh-settings-ui.test.ts | 11 ++++--- 3 files changed, 100 insertions(+), 11 deletions(-) diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index c36b05a150..86cff1b3c1 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -279,17 +279,37 @@ async function relayAdminRequest(req: unknown, res: RelayRes): Promise { // socket already gone — nothing left to report } }; + // Fail-closed presentation first: while the gate is off the route answers + // 404 regardless of proxy state (no state disclosure). + if (!adminRelayEnabled()) { + fail(404, "admin relay disabled"); + return; + } const base = register.base; if (base === undefined) { fail(503, "bili proxy not running"); return; } - if (!adminRelayEnabled()) { - fail(404, "admin relay disabled"); + const rawMethod = (req as { method?: unknown }).method; + const method = typeof rawMethod === "string" ? rawMethod : "GET"; + const rawUrl = (req as { url?: unknown }).url; + const url = typeof rawUrl === "string" ? rawUrl : "/"; + // Contain the relay to the declared /__bili surface: fetch() normalizes + // dot segments (literal and %2e-encoded alike), so an unchecked + // "/__bili/.." would land on sibling paths of the loopback proxy. + const cut = url.search(/[?#]/); + const pathPart = cut === -1 ? url : url.slice(0, cut); + let decodedPath: string; + try { + decodedPath = decodeURIComponent(pathPart).replaceAll("\\", "/"); + } catch { + fail(400, "bad request path"); + return; + } + if (decodedPath.split("/").includes("..")) { + fail(400, "bad request path"); return; } - const method = typeof (req as { method?: unknown }).method === "string" ? (req as { method?: string }).method : "GET"; - const url = typeof (req as { url?: unknown }).url === "string" ? (req as { url?: string }).url : "/"; const headersIn = (req as { headers?: unknown }).headers; const headers: Record = {}; if (headersIn !== null && typeof headersIn === "object") { @@ -1235,8 +1255,15 @@ export function apply(ctx: PluginContext): void { const ws = sub.webServer; if (ws === undefined) return; if (typeof sub.effect === "function") sub.effect(() => { - ws.register(originRoute); - ws.register(adminRelayRoute); + const offOrigin = ws.register(originRoute); + const offRelay = ws.register(adminRelayRoute); + // The effect's return value is how dsh disposes this + // registration on re-run — combine both route disposers or + // stale duplicates accumulate in the router table. + return () => { + offOrigin?.(); + offRelay?.(); + }; }, "bili: panel routes (origin + admin relay)"); else { ws.register(originRoute); diff --git a/tests/dsh-admin-relay.test.ts b/tests/dsh-admin-relay.test.ts index c785ef9148..367b26bc73 100644 --- a/tests/dsh-admin-relay.test.ts +++ b/tests/dsh-admin-relay.test.ts @@ -224,3 +224,64 @@ test("#3373: an unreachable proxy behind the gate surfaces as 502 JSON, not a cr rmrf(home); } }); + +test("#3373 review: dot-segment traversal is contained — literal and %2e-encoded \"..\" are rejected with 400 and never reach the proxy", async () => { + const stub = await startAdminStub(); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-relay-")); + try { + await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: undefined, BILI_CONFIG_FILE: writeConfig(home, JSON.stringify({ security: { adminRelay: true } })) }, async () => { + _resetRegisterForTest(stub.origin); + const route = relayRoute(collectRoutes()); + // apply() itself probes the bound proxy — baseline after mount so + // only relay hops are counted. + const baseline = stub.seen.length; + for (const url of ["/__bili/../__acp/sessions", "/__bili/%2e%2e/config", "/__bili/a/../../status"]) { + const res = new RelayRes(); + await route.handler(fakeReq("GET", url, browserHeaders()), res); + assert.equal(res.status, 400, url); + assert.deepEqual(JSON.parse(res.body), { error: "bad request path" }); + } + assert.equal(stub.seen.length, baseline, "rejected paths must not produce any upstream hop"); + }); + } finally { + stub.close(); + rmrf(home); + } +}); + +test("#3373 review: gate off + proxy unbound still presents 404 (fail-closed beats state disclosure)", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-relay-")); + try { + await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: undefined, BILI_CONFIG_FILE: writeConfig(home, "{}") }, async () => { + _resetRegisterForTest(undefined); + const route = relayRoute(collectRoutes()); + const res = new RelayRes(); + await route.handler(fakeReq("GET", "/__bili/config", browserHeaders()), res); + assert.equal(res.status, 404); + assert.deepEqual(JSON.parse(res.body), { error: "admin relay disabled" }); + }); + } finally { + rmrf(home); + } +}); + +test("#3373 review: oversized bodies are rejected with 413 before any upstream hop", async () => { + const stub = await startAdminStub(); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-relay-")); + try { + await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: undefined, BILI_CONFIG_FILE: writeConfig(home, JSON.stringify({ security: { adminRelay: true } })) }, async () => { + _resetRegisterForTest(stub.origin); + const route = relayRoute(collectRoutes()); + const baseline = stub.seen.length; + const res = new RelayRes(); + // 8 MiB cap + 1 byte (RELAY_MAX_BODY_BYTES in dsh-native.ts) + await route.handler(fakeReq("POST", "/__bili/config", browserHeaders(), ["x".repeat(8 * 1024 * 1024 + 1)]), res); + assert.equal(res.status, 413); + assert.deepEqual(JSON.parse(res.body), { error: "request body too large" }); + assert.equal(stub.seen.length, baseline, "an oversized body must not be forwarded"); + }); + } finally { + stub.close(); + rmrf(home); + } +}); diff --git a/tests/dsh-settings-ui.test.ts b/tests/dsh-settings-ui.test.ts index b1a5a2092e..01598ecb35 100644 --- a/tests/dsh-settings-ui.test.ts +++ b/tests/dsh-settings-ui.test.ts @@ -214,11 +214,12 @@ test("#1809: route registration rides the injected context's effect lifecycle", assert.equal(effects[0].label, "bili: panel routes (origin + admin relay)"); const cleanup = effects[0].fn(); assert.equal(routes.length, 2); - assert.ok(cleanup === undefined || typeof cleanup === "function", "the disposer doubles as the effect cleanup"); - if (typeof cleanup === "function") { - cleanup(); - assert.equal(disposed, 2); - } + // #3373 review: the effect fn must hand back a combined disposer — + // dsh calls it on re-run to un-register BOTH routes; returning + // undefined lets stale duplicates accumulate in the router table. + assert.equal(typeof cleanup, "function", "the combined disposer doubles as the effect cleanup"); + (cleanup as () => void)(); + assert.equal(disposed, 2); }); } finally { rmrf(home);