Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export async function createHttpAgent(
): Promise<ProxyAgent> {
const insecure = cfg.get<boolean>("coder.insecure", false);
const proxyStrictSSL = cfg.get<boolean>("http.proxyStrictSSL", true);
const proxyAuthorization = cfg.get<string>("http.proxyAuthorization");
const httpNoProxy = cfg.get<string>("http.noProxy");
const proxyAuthorization = cfg.get<string | null>("http.proxyAuthorization");
const httpNoProxy = cfg.get<string[]>("http.noProxy");

const certFile = expandPath(
String(cfg.get("coder.tlsCertFile") ?? "").trim(),
Expand All @@ -56,7 +56,7 @@ export async function createHttpAgent(
url,
cfg.get("http.proxy"),
cfg.get("coder.proxyBypass"),
httpNoProxy,
httpNoProxy?.map((noProxy) => noProxy.trim())?.join(","),
);
},
headers,
Expand Down
41 changes: 39 additions & 2 deletions test/unit/api/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ describe("createHttpAgent", () => {
it("uses http.noProxy as fallback when coder.proxyBypass is not set", async () => {
const cfg = new MockConfigurationProvider();
cfg.set("http.proxy", proxy);
cfg.set("http.noProxy", "internal.example.com");
cfg.set("http.noProxy", ["internal.example.com"]);

const agent = await createHttpAgent(cfg);

Expand All @@ -214,7 +214,7 @@ describe("createHttpAgent", () => {
const cfg = new MockConfigurationProvider();
cfg.set("http.proxy", proxy);
cfg.set("coder.proxyBypass", "primary.example.com");
cfg.set("http.noProxy", "fallback.example.com");
cfg.set("http.noProxy", ["fallback.example.com"]);

const agent = await createHttpAgent(cfg);

Expand All @@ -225,5 +225,42 @@ describe("createHttpAgent", () => {
await agent.getProxyForUrl("https://fallback.example.com", mockRequest),
).toBe(proxy);
});

it("trims and joins multiple http.noProxy entries", async () => {
const cfg = new MockConfigurationProvider();
cfg.set("http.proxy", proxy);
cfg.set("http.noProxy", [" first.example.com ", "second.example.com "]);

const agent = await createHttpAgent(cfg);

expect(
await agent.getProxyForUrl("https://first.example.com", mockRequest),
).toBe("");
expect(
await agent.getProxyForUrl("https://second.example.com", mockRequest),
).toBe("");
expect(
await agent.getProxyForUrl("https://other.example.com", mockRequest),
).toBe(proxy);
});

interface NoProxyTestCase {
name: string;
noProxy: string[] | undefined;
}
it.each<NoProxyTestCase>([
{ name: "undefined", noProxy: undefined },
{ name: "empty array", noProxy: [] },
])("uses proxy when http.noProxy is $name", async ({ noProxy }) => {
const cfg = new MockConfigurationProvider();
cfg.set("http.proxy", proxy);
cfg.set("http.noProxy", noProxy);

const agent = await createHttpAgent(cfg);

expect(
await agent.getProxyForUrl("https://example.com", mockRequest),
).toBe(proxy);
});
});
});