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..84e909efed67 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, tempDir, tmpdirSync, tls as validTls } from "harness"; import { request } from "http"; import { join } from "path"; const tmp_dir = tmpdirSync(); @@ -244,3 +244,45 @@ it("handle redirect to non-unix", async () => { expect(await response.text()).toBe("world"); } }); + +describe("tls over unix socket", () => { + it("honors tls.ca for server verification", async () => { + 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 }, + 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. 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 () => { + using dir = tempDir("fetch-unix-mtls", {}); + const unix = join(String(dir), "s.sock"); + 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); + }); +});