Skip to content

Commit 23cf56a

Browse files
Ignore unsupported proxy schemes for env dispatcher
1 parent 7d5ca30 commit 23cf56a

3 files changed

Lines changed: 36 additions & 14 deletions

File tree

src/infra/net/proxy-env.test.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,22 @@ describe("resolveEnvHttpProxyAgentOptions", () => {
114114
},
115115
},
116116
{
117-
name: "uses ALL_PROXY for both protocols",
118-
env: { ALL_PROXY: "socks5://all-proxy.test:1080" } as NodeJS.ProcessEnv,
117+
name: "uses HTTP ALL_PROXY for both protocols",
118+
env: { ALL_PROXY: "http://all-proxy.test:1080" } as NodeJS.ProcessEnv,
119119
expected: {
120-
httpProxy: "socks5://all-proxy.test:1080",
121-
httpsProxy: "socks5://all-proxy.test:1080",
120+
httpProxy: "http://all-proxy.test:1080",
121+
httpsProxy: "http://all-proxy.test:1080",
122122
},
123123
},
124+
{
125+
name: "ignores unsupported SOCKS ALL_PROXY for EnvHttpProxyAgent",
126+
env: { ALL_PROXY: "socks5://all-proxy.test:1080" } as NodeJS.ProcessEnv,
127+
expected: undefined,
128+
},
124129
{
125130
name: "lets protocol-specific proxy override ALL_PROXY",
126131
env: {
127-
ALL_PROXY: "socks5://all-proxy.test:1080",
132+
ALL_PROXY: "http://all-proxy.test:1080",
128133
HTTP_PROXY: "http://http-proxy.test:8080",
129134
HTTPS_PROXY: "http://https-proxy.test:8443",
130135
} as NodeJS.ProcessEnv,
@@ -137,7 +142,7 @@ describe("resolveEnvHttpProxyAgentOptions", () => {
137142
name: "treats empty lower-case all_proxy as authoritative over upper-case ALL_PROXY",
138143
env: {
139144
all_proxy: "",
140-
ALL_PROXY: "socks5://upper-all-proxy.test:1080",
145+
ALL_PROXY: "http://upper-all-proxy.test:1080",
141146
} as NodeJS.ProcessEnv,
142147
expected: undefined,
143148
},

src/infra/net/proxy-env.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ function normalizeProxyEnvValue(value: string | undefined): string | null | unde
2525
return trimmed.length > 0 ? trimmed : null;
2626
}
2727

28+
function isHttpProxyUrl(value: string | undefined): value is string {
29+
if (!value) {
30+
return false;
31+
}
32+
try {
33+
const parsed = new URL(value);
34+
return parsed.protocol === "http:" || parsed.protocol === "https:";
35+
} catch {
36+
return false;
37+
}
38+
}
39+
40+
function normalizeEnvHttpProxyAgentUrl(value: string | undefined): string | undefined {
41+
return isHttpProxyUrl(value) ? value : undefined;
42+
}
43+
2844
export type EnvHttpProxyAgentProxyOptions = {
2945
httpProxy?: string;
3046
httpsProxy?: string;
@@ -76,9 +92,10 @@ function resolveEnvAllProxyUrl(env: NodeJS.ProcessEnv): string | undefined {
7692
export function resolveEnvHttpProxyAgentOptions(
7793
env: NodeJS.ProcessEnv = process.env,
7894
): EnvHttpProxyAgentProxyOptions | undefined {
79-
const allProxy = resolveEnvAllProxyUrl(env);
80-
const httpProxy = resolveEnvHttpProxyUrl("http", env) ?? allProxy;
81-
const httpsProxy = resolveEnvHttpProxyUrl("https", env) ?? httpProxy;
95+
const allProxy = normalizeEnvHttpProxyAgentUrl(resolveEnvAllProxyUrl(env));
96+
const httpProxy = normalizeEnvHttpProxyAgentUrl(resolveEnvHttpProxyUrl("http", env)) ?? allProxy;
97+
const httpsProxy =
98+
normalizeEnvHttpProxyAgentUrl(resolveEnvHttpProxyUrl("https", env)) ?? httpProxy;
8299
const options: EnvHttpProxyAgentProxyOptions = {
83100
...(httpProxy ? { httpProxy } : {}),
84101
...(httpsProxy ? { httpsProxy } : {}),

src/infra/net/undici-global-dispatcher.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,11 @@ describe("ensureGlobalUndiciEnvProxyDispatcher", () => {
241241
expect(getCurrentDispatcher()).toBeInstanceOf(EnvHttpProxyAgent);
242242
});
243243

244-
it("installs EnvHttpProxyAgent with explicit ALL_PROXY fallback options", () => {
244+
it("installs EnvHttpProxyAgent with explicit HTTP ALL_PROXY fallback options", () => {
245245
vi.mocked(hasEnvHttpProxyAgentConfigured).mockReturnValue(true);
246246
vi.mocked(resolveEnvHttpProxyAgentOptions).mockReturnValue({
247-
httpProxy: "socks5://proxy.test:1080",
248-
httpsProxy: "socks5://proxy.test:1080",
247+
httpProxy: "http://proxy.test:1080",
248+
httpsProxy: "http://proxy.test:1080",
249249
});
250250

251251
ensureGlobalUndiciEnvProxyDispatcher();
@@ -254,8 +254,8 @@ describe("ensureGlobalUndiciEnvProxyDispatcher", () => {
254254
const next = getCurrentDispatcher() as { options?: Record<string, unknown> };
255255
expect(next).toBeInstanceOf(EnvHttpProxyAgent);
256256
expect(next.options).toEqual({
257-
httpProxy: "socks5://proxy.test:1080",
258-
httpsProxy: "socks5://proxy.test:1080",
257+
httpProxy: "http://proxy.test:1080",
258+
httpsProxy: "http://proxy.test:1080",
259259
});
260260
});
261261

0 commit comments

Comments
 (0)