Skip to content

Commit 90dd693

Browse files
author
Kevin Rajan
committed
fix(proxy): hard-bypass loopback for NO_PROXY/ProxyEnv
Always skip HTTP_PROXY/ALL_PROXY for localhost, 127.0.0.1, ::1, and 0.0.0.0 (strip [] so [::1] matches). Add proxy-env tests and a short AGENTS note that compiled Bun ignores NODE_OPTIONS DNS (docs only). Fork repair for #49 / anomalyco#37043+anomalyco#47041.
1 parent 696f41b commit 90dd693

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

‎packages/opencode/AGENTS.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,9 @@ Use `Effect.cached` when multiple concurrent callers should share a single in-fl
129129
Use `EffectBridge` for native or external callbacks (`@parcel/watcher`, `node-pty`, native `fs.watch`, plugin callbacks, etc.) that need to re-enter Effect services with instance/workspace context.
130130

131131
Plain async code should pass explicit context or stay inside an Effect fiber; do not add ambient instance context shims.
132+
133+
## Compiled binary networking
134+
135+
The compiled OpenCode binary is a Bun executable. `NODE_OPTIONS=--dns-result-order=ipv4first` is a Node.js flag and is ignored. Prefer Happy Eyeballs / `family: 0` in fetch, or run via `bun`/`node` if IPv4-first DNS is required.
136+
137+
Loopback URLs are not sent through `HTTP_PROXY`/`ALL_PROXY` (`packages/opencode/src/util/proxy-env.ts`). Compiled `fetch` may still honor Bun's env proxy independently of that helper.

‎packages/opencode/src/util/proxy-env.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ export function getProxyForUrl(input: string | URL) {
4747
return proxy.includes("://") ? proxy : `${protocol}://${proxy}`
4848
}
4949

50+
function isLoopbackHostname(hostname: string) {
51+
const host = hostname.toLowerCase().replace(/^\[|\]$/g, "")
52+
return host === "localhost" || host === "127.0.0.1" || host === "::1" || host === "0.0.0.0"
53+
}
54+
5055
function shouldProxy(hostname: string, port: number) {
56+
if (isLoopbackHostname(hostname)) return false
5157
const noProxy = env("no_proxy").toLowerCase()
5258
if (!noProxy) return true
5359
if (noProxy === "*") return false
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { afterEach, beforeEach, describe, expect, test } from "bun:test"
2+
import { getProxyForUrl } from "../../src/util/proxy-env"
3+
4+
const KEYS = ["ALL_PROXY", "all_proxy", "HTTP_PROXY", "http_proxy", "HTTPS_PROXY", "https_proxy", "NO_PROXY", "no_proxy"] as const
5+
const saved: Record<string, string | undefined> = {}
6+
7+
function snapshotEnv() {
8+
for (const key of KEYS) saved[key] = process.env[key]
9+
}
10+
11+
function restoreEnv() {
12+
for (const key of KEYS) {
13+
if (saved[key] === undefined) delete process.env[key]
14+
else process.env[key] = saved[key]
15+
}
16+
}
17+
18+
describe("getProxyForUrl loopback", () => {
19+
beforeEach(snapshotEnv)
20+
afterEach(restoreEnv)
21+
22+
test("does not proxy loopback even when ALL_PROXY is set", () => {
23+
process.env.ALL_PROXY = "http://127.0.0.1:18764"
24+
process.env.HTTP_PROXY = "http://127.0.0.1:18764"
25+
delete process.env.NO_PROXY
26+
delete process.env.no_proxy
27+
expect(getProxyForUrl("http://127.0.0.1:8317/v1")).toBeUndefined()
28+
expect(getProxyForUrl("http://localhost:8317/v1")).toBeUndefined()
29+
expect(getProxyForUrl("http://[::1]:8317/v1")).toBeUndefined()
30+
expect(getProxyForUrl("http://0.0.0.0:8317/v1")).toBeUndefined()
31+
})
32+
33+
test("strips brackets so [::1] matches loopback bypass", () => {
34+
process.env.ALL_PROXY = "http://proxy.example:8080"
35+
delete process.env.NO_PROXY
36+
delete process.env.no_proxy
37+
expect(getProxyForUrl("http://[::1]/")).toBeUndefined()
38+
})
39+
})

0 commit comments

Comments
 (0)