Skip to content

Commit

Permalink
fix(proxy): allow empty string http_proxy env. (oven-sh#5464)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanaasagi authored and paperdave committed Sep 18, 2023
1 parent 584de91 commit e7a1f0b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/env_loader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,24 @@ pub const Loader = struct {

if (url.isHTTP()) {
if (this.map.get("http_proxy") orelse this.map.get("HTTP_PROXY")) |proxy| {
if (proxy.len > 0) http_proxy = URL.parse(proxy);
if (proxy.len > 0 and !strings.eqlComptime(proxy, "\"\"") and !strings.eqlComptime(proxy, "''")) {
http_proxy = URL.parse(proxy);
}
}
} else {
if (this.map.get("https_proxy") orelse this.map.get("HTTPS_PROXY")) |proxy| {
if (proxy.len > 0) http_proxy = URL.parse(proxy);
if (proxy.len > 0 and !strings.eqlComptime(proxy, "\"\"") and !strings.eqlComptime(proxy, "''")) {
http_proxy = URL.parse(proxy);
}
}
}

// NO_PROXY filter
if (http_proxy != null) {
if (this.map.get("no_proxy") orelse this.map.get("NO_PROXY")) |no_proxy_text| {
if (no_proxy_text.len == 0) return http_proxy;
if (no_proxy_text.len == 0 or strings.eqlComptime(no_proxy_text, "\"\"") or strings.eqlComptime(no_proxy_text, "''")) {
return http_proxy;
}

var no_proxy_list = std.mem.split(u8, no_proxy_text, ",");
var next = no_proxy_list.next();
Expand Down
29 changes: 29 additions & 0 deletions test/js/bun/http/proxy.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
import { gc } from "harness";
import fs from "fs";
import { tmpdir } from "os";
import path from "path";
import { bunExe } from "harness";

let proxy, auth_proxy, server;

Expand Down Expand Up @@ -168,3 +170,30 @@ it("proxy non-TLS auth can fail", async () => {
}
}
});

it.each([
[undefined, undefined],
["", ""],
["''", "''"],
['""', '""'],
])("test proxy env, http_proxy=%s https_proxy=%s", async (http_proxy, https_proxy) => {
const path = `${tmpdir()}/bun-test-http-proxy-env-${Date.now()}.ts`;
fs.writeFileSync(path, 'await fetch("https://example.com");');

const { stdout, stderr, exitCode } = Bun.spawnSync({
cmd: [bunExe(), "run", path],
env: {
http_proxy: http_proxy,
https_proxy: https_proxy,
},
stdout: "pipe",
stderr: "pipe",
});

try {
expect(stderr.includes("FailedToOpenSocket: Was there a typo in the url or port?")).toBe(false);
expect(exitCode).toBe(0);
} finally {
fs.unlinkSync(path);
}
});

0 comments on commit e7a1f0b

Please sign in to comment.