Skip to content

fix: gate pi project prompt packs on project trust (#2529) - #2537

Open
ranxianglei wants to merge 1 commit into
masterfrom
2026-10-09_pi-project-trust-packs
Open

ranxianglei wants to merge 1 commit into
masterfrom
2026-10-09_pi-project-trust-packs

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #2529

What this changes

An untrusted pi project could plant .billion-context/packs/<name>.json to shadow a same-named user/builtin prompt pack and inject its own surface text (tool descriptions / prompt sections / nudge text) into ACP system prompts — pack precedence is project > user > builtin, so a project pack wins over a trusted one. The proxy resolved packs per request with no trust input.

This gates the project pack source on pi's project-trust decision, failing closed.

Mechanism

  • src/agent/pi.ts — Ctx gains an optional isProjectTrusted?: () => boolean | Promise<boolean>; new projectTrustedOf(ctx) returns true only for an explicit true (missing method / throw / non-boolean ⇒ untrusted). The before_provider_headers handler stamps x-bili-project-trusted: "1"|"0" on every pi plugin-mode model request.
  • src/compress-settings.ts — resolveCompressSurface(Detailed)(s, dirs?, includeProject?): when includeProject === false the resolver chain drops the project dir and keeps the user + builtin sources; the default (undefined) preserves today's chain byte-for-byte. New pure predicate piProjectPacksAllowed(agent, trustedHeader): non-pi lanes always allow; pi allows only on the literal "1".
  • src/server/handle.ts — the per-request pack resolution now passes piProjectPacksAllowed(pluginAgentHeader(req.headers), headerValue(req, "x-bili-project-trusted")). Resolution is per-request (no process-lifetime cache), so a mid-session trust flip re-derives on the very next request — no stale state.

Behavior changes (old → new)

  1. Untrusted pi project packs: always applied → skipped whenever ctx.isProjectTrusted() !== true (including the signal being unavailable). User + builtin packs stay active.
  2. Trusted pi projects: unchanged — project packs still resolve and shadow as before.
  3. Non-pi lanes + wire/launcher-mode pi: unchanged (no x-bili-plugin agent header / no trust channel there). Fail-closed applies only where the signal exists.

Scope note (see #2529)

The migrated issue also names a .pi/acp.json ACP-config loader facet. That loader does not exist in billion-context — it lives in the standalone billion-context-pi, tracked by its open PR ranxianglei/billion-context-pi#625. This PR covers the facet that actually lives here: project prompt-pack injection. No .pi/acp.json code path is touched.

Residual

Wire/launcher-mode pi (no extension ctx) has no project-trust channel, so it keeps today's behavior. Hardening that mode needs a trust signal that mode does not expose — out of scope here, flagged for follow-up if desired.

Tests

New tests/pi-project-trust.test.ts (9 cases): the allow/block decision matrix, fail-closed detection (missing method / throw / non-boolean), the same-name shadowing attack itself, omitted-argument back-compat, project-only pack loss → default, builtin packs still available to untrusted contexts, and per-request trust-flip re-resolution. Existing pack/surface suites are unchanged and green.

Rebased on current master; typecheck + targeted suites green locally, full regression runs on CI.

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-09_pi-project-trust-packs (fc85797)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2758-pr.2537.444f645

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr2537.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

继续 看上面消息

An untrusted pi project could plant .billion-context/packs/<name>.json to shadow
a same-named user/builtin pack and inject its surface text into ACP system
prompts (pack precedence project > user > builtin). The pi extension now stamps
x-bili-project-trusted per request from ctx.isProjectTrusted() (fail closed on
anything but an explicit true); the proxy then drops the PROJECT pack source for
untrusted pi contexts while keeping user/builtin packs active. Other lanes and
wire/launcher-mode pi are unchanged (no trust channel exists there). Resolution
is per-request, so a mid-session trust flip re-derives on the next request.
@ranxianglei
ranxianglei force-pushed the 2026-10-09_pi-project-trust-packs branch from 8708f80 to fc85797 Compare October 10, 2026 01:34
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

Review 结论:修法正确、已验证、CI 全绿(26/26 @ fc8579704)。代码可合;合并前需 owner 就「#2529 关闭语义」拍板(见下方 ⚠️)。

已做动作:

  1. 逐行通读完整 diff(4 文件 +186/-10),对照 kernel 源码确认 builtinSource/createDirPackSource 导出(kernel/src/index.ts:95-96)与 defaultPackSources 的 project→user→builtin 优先级(kernel/src/packs.ts:426-436)。
  2. 确认全仓 pack 解析点唯一(src/server/handle.ts:552);wire-mode 残留声明核实准确——x-bili-plugin 仅 toolsReady(plugin mode)时 stamp,tool-reg 失败回落 wire mode 时 agent=undefined → 不受限,与 PR 描述一致。
  3. 分支落后 master 60 commits。核查重叠区(pi.ts 的 model-header 消毒、handle.ts 的 [bug] Claude Code 的 WebSearch 子请求被当成主对话:nudge 基准被搜索的 usage 重锚,导致提前折叠;每次搜索还被注入 bili 工具 #2500/开启 bili 后「规矩与 skill 好像没被使用」? #2419)与本 PR 改动区域不相交、无语义冲突;rebase 到最新 master(4fce5d3fb)后 cherry-pick 干净应用 → 新 head fc8579704;本地 typecheck + 定向套件(pi-project-trust / compress-prompt-pack / prompt-pack-tool-prompts,22 例)全过;push 后 CI 26/26 绿(旧 head 亦曾 26/26 绿)。

八问逐项:

  1. 是什么问题:不受信任的 pi 项目可在仓库根放 .billion-context/packs/<name>.json,shadow 用户/内置同名 pack——向 ACP 压缩系统提示词注入内容。
  2. 什么原因导致的:pack 解析链(kernel defaultPackSources)没有信任输入;project 目录优先是设计如此,但 pi 的信任决策从未接入这条链——缺的不是解析逻辑,是信任闸门。
  3. 影响范围与严重程度:中。需同时满足:全局配置了 promptPack + cwd 为不受信仓库 + 攻击者放置同名 pack 文件。未配置 promptPack 的用户不受影响。
  4. 怎么修复的:不改解析机制,在 host 侧加一道信任门——extension 经 ctx.isProjectTrusted() 读 pi 自身信任状态,stamp 内部 header x-bili-project-trusted;resolver 非 "1" 时跳过 project 目录。fail-closed:函数缺失/抛错/非布尔一律视为不受信。门只作用于 agent=pi 的 plugin mode,其余 lane 走原路径。
  5. 是否有回归问题:未发现。逐项审过的既有行为:非 pi lane(allowed=true → includeProject=true → defaultPackSources 参数与旧调用完全相同 → 解析结果字节一致,test 钉住);受信 pi 不变;wire-mode pi 不变(documented residual);warn 文案默认路径渲染一致("project/user/builtin");resolveCompressSurfaceDetailed 第三参可选,既有调用者不受影响;无 kernel 变更,golden 无 churn。行为变更披露(有意):不受信项目的 pi plugin mode 下,project 级 pack 从「总是生效」变为「跳过」(回落 user/builtin/default)——即本修复目标本身,旧值→新值如上,原因:安全修复。
  6. 是否破坏缓存:否。只改变「不受信 + 配置了 promptPack」时的取源文件,不触碰消息前缀字节,无新缓存键维度。
  7. 是否改了设计方向:否。沿用既有 x-bili-plugin-* 内部 header 家族;解析仍归 kernel;host policy 语义不变。
  8. 是否引入新配置:无用户可见配置。一个协议内部 header x-bili-project-trusted(manifest 未声明,无 wire-contract/golden 变化)。

⚠️ review 中发现的新面(已带来源标记评论到 #2529,等拍板):项目级 .pi/acp.json delegate 切片(src/agent/pi-subagents.ts:140-145 → pi-subagents/src/config.ts:256-260)同样无信任门——不受信仓库可用 <cwd>/.pi/acp.json {"delegatePrompt":"…"} 整体替换 acp_delegate 系统提示词,另有 enabled:false DoS、forceEnable:true 绕过 #415 stand-down。与 #2529 同机制族、不同面。本 PR 写的是 "Closes #2529",直接合掉会让 tracker 在一个面仍未修的状态下关闭。两个选项:①扩本 PR 范围把 acp.json 门一起覆盖(推荐,同机制族应一个 issue 管);②本 PR 原样保留,另开 follow-up。请拍板。

其他注记:

判定:代码本身 solid 且验证充分;唯一待决项是上面 ⚠️ 的范围问题。security 类按 §7.4 人工合并——owner 拍板范围后即可合。

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Security][pi] Gate project ACP config and packs on Pi project trust

1 participant