From 52ab729bf0fac9077c6edb3d09cf0e586bb80871 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 26 Jul 2026 05:57:02 +0000 Subject: [PATCH 1/4] fetch: honor tls.ca/cert/key over unix sockets HttpThread::connect returned early for unix sockets before the custom SSL_CTX path ran, so ca/cert/key (which are baked into an SSL_CTX) were silently dropped while per-request options like rejectUnauthorized and servername continued to work. Dial unix through the custom SSL_CTX when one is needed, same as TCP. --- src/http/HTTPThread.rs | 20 ++++++++----- test/js/web/fetch/fetch.unix.test.ts | 45 ++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src/http/HTTPThread.rs b/src/http/HTTPThread.rs index fde7ec23e2f1..d9fab299245a 100644 --- a/src/http/HTTPThread.rs +++ b/src/http/HTTPThread.rs @@ -481,13 +481,8 @@ impl HttpThread { // `bun_ptr::RawSlice` (encapsulated outlives-holder invariant) so the // borrow of `client` ends before we hand `&mut client` to // `connect_socket`. Backing storage is `client.unix_socket_path`, which - // `connect_socket` does not touch. + // neither `connect_socket` nor the `'custom_ctx` block touches. let unix_path = bun_ptr::RawSlice::new(client.unix_socket_path.slice()); - if !unix_path.is_empty() { - return self - .context::() - .connect_socket(client, unix_path.slice()); - } if IS_SSL { 'custom_ctx: { @@ -509,7 +504,9 @@ impl HttpThread { client.set_custom_ssl_ctx(entry.ctx); let ctx = entry.ctx_mut(); // Keepalive is now supported for custom SSL contexts - return if let Some(url) = client.http_proxy.clone() { + return if !unix_path.is_empty() { + ctx.connect_socket(client, unix_path.slice()) + } else if let Some(url) = client.http_proxy.clone() { ctx.connect(client, url.hostname, url.get_port_auto()) } else { let (hn, pt) = (client.url.hostname, client.url.get_port_auto()); @@ -569,7 +566,9 @@ impl HttpThread { client.set_custom_ssl_ctx(ctx_nn); // Keepalive is now supported for custom SSL contexts - let result = if let Some(url) = client.http_proxy.clone() { + let result = if !unix_path.is_empty() { + custom_context.connect_socket(client, unix_path.slice()) + } else if let Some(url) = client.http_proxy.clone() { if url.protocol.is_empty() || url.has_http_like_protocol() { custom_context.connect(client, url.hostname, url.get_port_auto()) } else { @@ -583,6 +582,11 @@ impl HttpThread { return result.map(|o| o.map(|s| s.cast_ssl::())); } } + if !unix_path.is_empty() { + return self + .context::() + .connect_socket(client, unix_path.slice()); + } if let Some(url) = client.http_proxy.clone() { if !url.href.is_empty() { // https://github.com/oven-sh/bun/issues/11343 diff --git a/test/js/web/fetch/fetch.unix.test.ts b/test/js/web/fetch/fetch.unix.test.ts index cfc83aca783e..64ffc8f49739 100644 --- a/test/js/web/fetch/fetch.unix.test.ts +++ b/test/js/web/fetch/fetch.unix.test.ts @@ -1,7 +1,7 @@ import { serve, ServeOptions, Server } from "bun"; -import { afterAll, expect, it } from "bun:test"; +import { afterAll, describe, expect, it } from "bun:test"; import { mkdirSync, rmSync } from "fs"; -import { isWindows, tmpdirSync } from "harness"; +import { isWindows, tls as validTls, tmpdirSync } from "harness"; import { request } from "http"; import { join } from "path"; const tmp_dir = tmpdirSync(); @@ -244,3 +244,44 @@ it("handle redirect to non-unix", async () => { expect(await response.text()).toBe("world"); } }); + +describe("tls over unix socket", () => { + const sockPath = () => join(tmpdirSync(), "s.sock"); + + it("honors tls.ca for server verification", async () => { + const unix = sockPath(); + using server = Bun.serve({ + unix, + tls: { cert: validTls.cert, key: validTls.key }, + fetch: () => new Response("ok"), + }); + // Passing the server's self-signed cert as `ca` must make verification + // succeed, exactly as it does for the same server over TCP. + const res = await fetch("https://localhost/", { unix, tls: { ca: validTls.cert } }); + expect(await res.text()).toBe("ok"); + expect(res.status).toBe(200); + server.stop(true); + }); + + it("presents tls.cert + tls.key to a requestCert server (mTLS)", async () => { + const unix = sockPath(); + using server = Bun.serve({ + unix, + tls: { + cert: validTls.cert, + key: validTls.key, + ca: validTls.cert, + requestCert: true, + rejectUnauthorized: true, + }, + fetch: () => new Response("ok-mtls"), + }); + const res = await fetch("https://localhost/", { + unix, + tls: { ca: validTls.cert, cert: validTls.cert, key: validTls.key }, + }); + expect(await res.text()).toBe("ok-mtls"); + expect(res.status).toBe(200); + server.stop(true); + }); +}); From 4d16b0b54313ab71e7c88b7eef49f5a6022db78d Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Sun, 26 Jul 2026 05:58:54 +0000 Subject: [PATCH 2/4] [autofix.ci] apply automated fixes --- test/js/web/fetch/fetch.unix.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/js/web/fetch/fetch.unix.test.ts b/test/js/web/fetch/fetch.unix.test.ts index 64ffc8f49739..48ded74089b8 100644 --- a/test/js/web/fetch/fetch.unix.test.ts +++ b/test/js/web/fetch/fetch.unix.test.ts @@ -1,7 +1,7 @@ import { serve, ServeOptions, Server } from "bun"; import { afterAll, describe, expect, it } from "bun:test"; import { mkdirSync, rmSync } from "fs"; -import { isWindows, tls as validTls, tmpdirSync } from "harness"; +import { isWindows, tmpdirSync, tls as validTls } from "harness"; import { request } from "http"; import { join } from "path"; const tmp_dir = tmpdirSync(); From 0b82aee7206933a117a733c69eccd2b0b98d2dd0 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 26 Jul 2026 06:09:39 +0000 Subject: [PATCH 3/4] test: use tempDir, drop redundant server.stop alongside using --- test/js/web/fetch/fetch.unix.test.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/test/js/web/fetch/fetch.unix.test.ts b/test/js/web/fetch/fetch.unix.test.ts index 48ded74089b8..37cdb9f0f4f1 100644 --- a/test/js/web/fetch/fetch.unix.test.ts +++ b/test/js/web/fetch/fetch.unix.test.ts @@ -1,7 +1,7 @@ import { serve, ServeOptions, Server } from "bun"; import { afterAll, describe, expect, it } from "bun:test"; import { mkdirSync, rmSync } from "fs"; -import { isWindows, tmpdirSync, tls as validTls } from "harness"; +import { isWindows, tempDir, tmpdirSync, tls as validTls } from "harness"; import { request } from "http"; import { join } from "path"; const tmp_dir = tmpdirSync(); @@ -246,10 +246,9 @@ it("handle redirect to non-unix", async () => { }); describe("tls over unix socket", () => { - const sockPath = () => join(tmpdirSync(), "s.sock"); - it("honors tls.ca for server verification", async () => { - const unix = sockPath(); + using dir = tempDir("fetch-unix-tls", {}); + const unix = join(String(dir), "s.sock"); using server = Bun.serve({ unix, tls: { cert: validTls.cert, key: validTls.key }, @@ -260,11 +259,11 @@ describe("tls over unix socket", () => { const res = await fetch("https://localhost/", { unix, tls: { ca: validTls.cert } }); expect(await res.text()).toBe("ok"); expect(res.status).toBe(200); - server.stop(true); }); it("presents tls.cert + tls.key to a requestCert server (mTLS)", async () => { - const unix = sockPath(); + using dir = tempDir("fetch-unix-mtls", {}); + const unix = join(String(dir), "s.sock"); using server = Bun.serve({ unix, tls: { @@ -282,6 +281,5 @@ describe("tls over unix socket", () => { }); expect(await res.text()).toBe("ok-mtls"); expect(res.status).toBe(200); - server.stop(true); }); }); From d18e65dfa88a35ff6aec9ad03b381cb9202ca5c2 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 26 Jul 2026 06:33:05 +0000 Subject: [PATCH 4/4] test: second fetch with same tls config to cover the SSL_CTX cache-hit arm --- test/js/web/fetch/fetch.unix.test.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/js/web/fetch/fetch.unix.test.ts b/test/js/web/fetch/fetch.unix.test.ts index 37cdb9f0f4f1..84e909efed67 100644 --- a/test/js/web/fetch/fetch.unix.test.ts +++ b/test/js/web/fetch/fetch.unix.test.ts @@ -255,10 +255,13 @@ describe("tls over unix socket", () => { fetch: () => new Response("ok"), }); // Passing the server's self-signed cert as `ca` must make verification - // succeed, exactly as it does for the same server over TCP. - const res = await fetch("https://localhost/", { unix, tls: { ca: validTls.cert } }); - expect(await res.text()).toBe("ok"); - expect(res.status).toBe(200); + // succeed, exactly as it does for the same server over TCP. Second fetch + // with the same config goes through the per-config SSL_CTX cache. + for (let i = 0; i < 2; i++) { + const res = await fetch("https://localhost/", { unix, tls: { ca: validTls.cert } }); + expect(await res.text()).toBe("ok"); + expect(res.status).toBe(200); + } }); it("presents tls.cert + tls.key to a requestCert server (mTLS)", async () => {