[BUG] opencode plugin routes ALL HTTP traffic through the proxy — breaks built-in WebFetch and every Node child process (npx/npm/Node MCPs)
Summary
The OpenCode plugin's transport layer routes all outbound HTTP traffic — not just LLM API traffic — through the Headroom proxy. Two propagation paths:
- In-process: the plugin patches
globalThis.fetchin the opencode (Bun) process with ashouldRoute()filter that returnstruefor every non-loopback http(s) URL. opencode's built-inWebFetchtool does client-sidefetch()in that same process, so every webfetch request is rewritten to the proxy and rejected. - Child processes: the plugin wraps
child_process.spawn/exec/execFile/forkand injectsNODE_OPTIONS=--import=<...>/hook-shim/handler.jsplusHEADROOM_OPENCODE_TRANSPORT_PROXY_URLinto every spawned child. The hook-shim installs the same transport patch (it contains its own copy ofshouldRoute()+globalThis.fetch =) into every Node child process. That breaksnpx,npm, Node-based CLI tools (e.g. the context7 CLI) and Node MCP servers.
Non-Node children (curl, gh, python, ...) are unaffected, which makes the failure look inconsistent and is painful to debug.
Environment
- headroom-ai 0.37.0 (pipx), proxy on
127.0.0.1:8787 - OpenCode 1.18.31 (bun 1.3.14), plugin registered via
pluginarray inopencode.json - Upstream: OpenAI-compatible vLLM endpoint
Repro A — built-in WebFetch
Start
headroom proxy, register the opencode plugin.Ask the agent to fetch any page:
> use webfetch on https://example.com StatusCode: non 2xx status code (400 GET https://example.com)curl https://example.comfrom the same session's bash tool works fine (200) — curl is a non-Node child.
Manual proof at the proxy level:
$ curl -s -H "x-headroom-base-url: https://example.com" http://127.0.0.1:8787/
{"detail":"Rejected unsafe upstream base URL"} # status 400The plugin rewrites fetch("https://example.com") to http://127.0.0.1:8787/ with x-headroom-base-url: https://example.com; the proxy (correctly, SSRF guard) refuses non-LLM upstreams.
Repro B — npx / npm / Node CLIs (context7 as example)
Same setup. In the agent's shell (which inherits the injected env —
envshowsNODE_OPTIONS=--import=file://...headroom/providers/opencode/hook-shim/handler.js):$ npx ctx7@latest library react hooks npm error code E400 npm error 400 Bad Request - GET https://registry.npmjs.org/ctx7Proxy log shows npm's packument request arriving at the LLM proxy:
event=proxy_inbound_request method=GET path=/ctx7 ... headers={... "x-headroom-base-url": "https://registry.npmjs.org" ...} WARNING - rejecting unsafe x-headroom-base-url: 'https://registry.npmjs.org'Counter-proof — clearing the injected shim fixes it:
$ NODE_OPTIONS= npx ctx7@latest library react hooks 1. Title: React Context7-compatible library ID: /reactjs/react.dev ...
Root cause
shouldRoute() in entry.opencode.js (duplicated in hook-shim/handler.js) is not a filter but an unconditional router:
function shouldRoute(url, proxy) {
if (url.protocol !== "http:" && url.protocol !== "https:") return false;
if (isLoopback(url.hostname)) return false;
if (url.origin === proxy.origin) return false;
return true; // ← every external host, including registry.npmjs.org, example.com, ...
}The hook-shim then applies the same routing inside every Node child via the injected NODE_OPTIONS --import, so npm's make-fetch-happen traffic (which honors no proxy env, it's patched at fetch level) lands on the proxy and dies on the upstream-safety guard.
Impact
While the plugin is active: built-in WebFetch is dead, npx/npm fail with E400, any Node-based MCP server or CLI doing plain HTTP(S) breaks. Only the LLM calls — the thing the proxy is meant to see — and non-Node binaries work.
Proposed fix: opt-out in shouldRoute()
I considered the alternatives first:
- Proxy-side passthrough of unknown
x-headroom-base-urlvalues would turn an LLM proxy into a general HTTP forwarder — that widens exactly the SSRF surface theRejected unsafe upstream base URLguard exists to prevent. Not a good trade. - Plugin-side allowlist of provider baseURLs is not feasible: the plugin bundle has no knowledge of the opencode provider config (no
baseURL/providerreferences), and route-all is intentional for multi-provider support (#1651, #1760).
So the minimal, backward-compatible lever is an exclude-list opt-out in shouldRoute(), applied in both copies (plugin bundle + hook-shim) so in-process fetch and Node children behave identically:
function routeExcluded(url) {
const raw = process.env.HEADROOM_OPENCODE_ROUTE_EXCLUDE; // must be propagated by withShimEnv()
if (!raw) return false;
const host = url.hostname.toLowerCase();
return raw.split(",").some((entry) => {
const e = entry.trim().toLowerCase().replace(/^\*\./, "");
if (!e) return false;
return host === e || host.endsWith(`.${e}`);
});
}
function shouldRoute(url, proxy) {
if (url.protocol !== "http:" && url.protocol !== "https:") return false;
if (isLoopback(url.hostname)) return false;
if (url.origin === proxy.origin) return false;
if (routeExcluded(url)) return false; // new
return true;
}HEADROOM_OPENCODE_ROUTE_EXCLUDE=registry.npmjs.org,.corp.internal(comma-separated hosts / domain suffixes)- Default empty → current behavior, zero breaking change
withShimEnv()must propagate the variable into child env (it already copiesprocess.env, so setting it in the parent viainstallProcessEnv-style injection suffices)- Optionally: consider excluding well-known non-LLM registries (
registry.npmjs.org) by default — debatable, the opt-out alone is the safe fix
Workarounds (current)
- WebFetch: run
curlvia the bash tool, or replace the built-in with a custom tool that shells out to a non-Node fetcher (works because the networking leaves the patched process). - npx/Node CLIs: prefix with
NODE_OPTIONS=to strip the injected shim for that invocation.
Related
- #2646 — tool-search deferral hiding WebFetch (different mechanism, fixed)
- #2115 — WebFetch/WebSearch output compression protection
- #2850 — hook-shim wheel packaging broke Node MCPs (same injection path, different failure)
- #1651 / #1760 — per-request upstream override, the reason route-all exists
Source: headroomlabs-ai/headroom