#3633·headroom

[BUG] opencode plugin routes ALL HTTP traffic through the proxy — breaks built-in WebFetch and every Node child process (npx/npm/Node MCPs)

Author: christestetCreated Sep 17, 2026Updated Sep 28, 2026
Labelsbug

Summary

The OpenCode plugin's transport layer routes all outbound HTTP traffic — not just LLM API traffic — through the Headroom proxy. Two propagation paths:

  1. In-process: the plugin patches globalThis.fetch in the opencode (Bun) process with a shouldRoute() filter that returns true for every non-loopback http(s) URL. opencode's built-in WebFetch tool does client-side fetch() in that same process, so every webfetch request is rewritten to the proxy and rejected.
  2. Child processes: the plugin wraps child_process.spawn/exec/execFile/fork and injects NODE_OPTIONS=--import=<...>/hook-shim/handler.js plus HEADROOM_OPENCODE_TRANSPORT_PROXY_URL into every spawned child. The hook-shim installs the same transport patch (it contains its own copy of shouldRoute() + globalThis.fetch =) into every Node child process. That breaks npx, 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 plugin array in opencode.json
  • Upstream: OpenAI-compatible vLLM endpoint

Repro A — built-in WebFetch

  1. Start headroom proxy, register the opencode plugin.

  2. Ask the agent to fetch any page:

    > use webfetch on https://example.com
    StatusCode: non 2xx status code (400 GET https://example.com)
  3. curl https://example.com from the same session's bash tool works fine (200) — curl is a non-Node child.

Manual proof at the proxy level:

bash
$ curl -s -H "x-headroom-base-url: https://example.com" http://127.0.0.1:8787/
{"detail":"Rejected unsafe upstream base URL"}   # status 400

The 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)

  1. Same setup. In the agent's shell (which inherits the injected env — env shows NODE_OPTIONS=--import=file://...headroom/providers/opencode/hook-shim/handler.js):

    bash
    $ npx ctx7@latest library react hooks
    npm error code E400
    npm error 400 Bad Request - GET https://registry.npmjs.org/ctx7
  2. Proxy 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'
  3. Counter-proof — clearing the injected shim fixes it:

    bash
    $ 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:

javascript
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-url values would turn an LLM proxy into a general HTTP forwarder — that widens exactly the SSRF surface the Rejected unsafe upstream base URL guard 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/provider references), 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:

javascript
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 copies process.env, so setting it in the parent via installProcessEnv-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 curl via 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