Skip to content

fix: guard native fetch intercept against third-party re-arm (#1158 L1) - #1187

Merged
ranxianglei merged 2 commits into
masterfrom
2026-09-23_native-fetch-guard
Sep 23, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-09-23_native-fetch-guard

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Sep 23, 2026 •

Copy link
Copy Markdown
Owner

What

installNativeFetchIntercept now installs globalThis.fetch as a guarded accessor instead of a plain assignment. Any third party that later writes globalThis.fetch (e.g. dsh-http-proxy's settings-refresh re-apply) becomes the downstream of a fresh chain top, so model traffic keeps routing through bili instead of being silently un-routed.

Root cause (L1 of #1158)

dsh-http-proxy 0.1.3 captures originalFetch = globalThis.fetch once at apply time and, on every settings hot-reload (refresh()), writes globalThis.fetch = createRoutingFetch(entry.fetch, originalFetch, hosts) — using the frozen pre-bili capture. Load order (dsh-http-proxy first, bili second) means bili's patch wraps dsh's routing fetch; the first settings change after startup re-arms dsh with the bare fetch and kicks bili out of the chain. The session keeps working (requests go direct through dsh's chain), so compression silently stops — zero error, zero proxied request.

Reproduced live on the Windows 10 VM (issue #1158 repro, run5): after a 13s settings touch, bili.log shows zero forwarded requests for the whole task, while the task completes normally.

Fix

  • makeChain(downstream) parameterizes the patch body; warned/replacedOrigins stay shared across re-armed links.
  • If the fetch property is configurable (Node default), install Object.defineProperty with a get/set pair:
    • get → the current chain top (always ours);
    • set → re-chain: top = makeChain(v) — the third-party function becomes our downstream.
    • Re-arm limit (16) so two self-healers can't fight forever; past the limit the guard degrades to the classic behavior (last writer wins).
    • Non-function and self writes are ignored.
    • Evict visibility: every third-party install logs [bili-native] third-party globalThis.fetch install detected (#1158) — re-chaining as downstream (evict attempt N) — "un-routed by a third party" is now diagnosable instead of silent.
  • Escape hatch: BILI_RECLAIM_FETCH_PATCH=0 restores the classic direct install (no guard) — for setups that need the third-party egress on top; see the egress note below.
  • Non-configurable host property → classic direct install (old behavior, no guard).
  • _resetForTest() restores the pre-install data-property descriptor captured at install time.

The dsh flow after this change: dsh re-arm writes its routing fetch → our setter re-chains it → host model calls read globalThis.fetch (our top) → URL rewritten to the bili proxy → dispatched through dsh's routing fetch (loopback target, outside proxyHosts, goes direct to the local bili proxy).

⚠️ Egress note (from the #1158 reporter's follow-up)

While the guard holds, claimed model traffic is dispatched by the bili proxy itself — it no longer rides the third-party chain's egress. In particular: a SOCKS5 proxy configured in dsh-http-proxy stops receiving claimed traffic, and bili's own upstream proxying supports HTTP proxies only (SOCKS5 is rejected/ignored — Clash users should use the HTTP form of a mixed port). If you need the third-party egress back: set BILI_RECLAIM_FETCH_PATCH=0 and configure the egress at bili's level ("proxy": "http://…"). Documented in CONFIGURATION.md / CONFIGURATION.zh-CN.md.

Verification

  • Windows 10 VM, same repro as run5 (dsh 0.1.5-rc.3 profile headless, bili plugin active, settings touch at t+13s): with this patch the requests after the touch keep flowing through bili — forward POST + [plugin] [acp-usage] with 99% cache hit all the way to task completion. Control (unpatched 0.1.139, run5): zero proxied requests after the touch.
  • The reporter independently confirmed the same mechanism on their host (chained-fix experiment, 方式一安装的dsh 原生插件:llm-pi-ai 传输(OpenAI SDK + options.fetch 注入)绕过 globalThis.fetch 拦截,模型请求不进代理、压缩完全不生效 #1158 comment): 6/6 model-api fetch probes intercepted, 6/6 processTurn in bili.log, prefix cache 99–100%, tool channel alive — compression, nudge and budget all restored.
  • New tests/native-intercept-selfheal.test.ts: 5 tests — frozen-bare-fetch reset re-chains; third-party wrapper becomes downstream (model URL rewritten through it, non-model URLs pass through untouched); guarded property is transparent when nobody fights; non-function/self writes ignored; BILI_RECLAIM_FETCH_PATCH=0 keeps the classic direct install (third party wins).
  • Full pre-flight on the branch head: npm run typecheck ✅, npm test 2356 pass / 0 fail (2 gated E2E skips) ✅, npm run build ✅.

Scope

This is the host-side defense for L1 (bili keeps routing no matter what a third-party fetch patch does). The reporter's chained-fix for dsh-http-proxy itself (blind overwrite → chain onto the current global) is the upstream half of the defense-in-depth — tracked in the issue. L3 (commandcode provider freezing fetchImpl at construction) needs its own upstream fix; L2 is downgraded to watch (gate 6/6 pass).

Refs #1158

dsh-http-proxy 0.1.3 re-applies by writing its module-load-time frozen
originalFetch over globalThis.fetch (settings hot-reload triggers refresh),
silently un-routing every model request away from bili while the session
keeps working. Reproduced live on Windows (issue #1158, run5: zero proxied
requests after a settings touch; task still completed).

Guard the property instead of trusting the assignment to survive: install a
configurable accessor whose setter re-chains any third-party fetch as the
downstream of a fresh chain top, so model traffic keeps routing through
bili. Re-arm limit (16) stops two self-healers from fighting forever;
non-configurable host properties keep the classic direct install. Verified
on the same Windows VM with the same repro: requests keep flowing through
bili after the settings refresh (plugin mode, acp-usage recorded).
@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-23_native-fetch-guard (ec33af3)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-1187

Each push to this PR publishes a new version under the pr-1187 npm tag.

Option B — Download artifact

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

Install instructions are refreshed on each push.

…the fetch guard (#1158)

Reporter follow-up on #1158: (1) an escape hatch for setups that NEED the
third-party egress on top (bili's upstream proxying is HTTP-only — a SOCKS5
egress configured in dsh-http-proxy stops riding claimed traffic while the
guard holds); BILI_RECLAIM_FETCH_PATCH=0 restores the classic direct
install. (2) Evict attempts now log a [bili-native] warning with a running
count, so 'un-routed by a third party' is diagnosable instead of silent.
Documented in CONFIGURATION.md/zh with the egress note.
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.

1 participant