Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <private-host>; 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 (<session>\|sub:<agent-id>) 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. |
Expand Down Expand Up @@ -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://<this-host>:<port>/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://<this-host>:<port>/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`

Expand Down Expand Up @@ -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`
Expand Down
16 changes: 15 additions & 1 deletion CONFIGURATION.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | 日志中把非公开目标主机遮成 <private-host>;凭据头无论此开关如何总是遮盖。 |
| `security.adminRelay` | boolean | false | — | 允许宿主(当前为 dsh web)把它自己入口域名下的整个 /__bili/ 管理面中继到本机代理,使远程浏览器能打开面板(#3373)。默认关闭、仅配置文件。中继跳到达代理时是本机非浏览器客户端(回环源地址 + 回环 Host、无 Origin 头),管理端门槛不变;开启后,凡能访问宿主 web 入口的人都能改配置、写凭据——请先给入口加鉴权/反代。 |
| `subagentSplit` | boolean | true | BILI_SUBAGENT_SPLIT | Claude 子代理拥有独立会话命名空间(<session>\|sub:<agent-id>),不再排队在主会话锁后。 |
| `forkAdoption` | boolean | false | BILI_FORK_ADOPTION | 匿名 fork 会话继承父会话中源内容完整存在于本次请求里的压缩块。 |
| `autoModeForge.enabled` | boolean | true | BILI_AUTO_MODE_FORGE | Claude Code auto mode 安全分类器请求由 bili 在本地代答放行判定,不再转发上游(#2740):该旁路零流出、零计费;每条代答写审计日志,会话带提示徽标。false / env 0 恢复纯透传(愿意付费要真判定的用户)。 |
Expand Down Expand Up @@ -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`

Expand Down Expand Up @@ -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`
Expand Down
77 changes: 62 additions & 15 deletions src/agent/dsh-native-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
Expand All @@ -260,25 +297,35 @@ function probeOrigin(onOrigin: (origin: string) => void, onTheme?: (theme: "ligh
let timer: ReturnType<typeof setTimeout> | undefined;
let attempts = 0;
const poll = async (): Promise<void> => {
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;
Expand Down
Loading
Loading