Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
20 changes: 12 additions & 8 deletions src/http/HTTPThread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<IS_SSL>()
.connect_socket(client, unix_path.slice());
}

if IS_SSL {
'custom_ctx: {
Expand All @@ -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());
Expand Down Expand Up @@ -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 {
Expand All @@ -583,6 +582,11 @@ impl HttpThread {
return result.map(|o| o.map(|s| s.cast_ssl::<IS_SSL>()));
}
}
if !unix_path.is_empty() {
return self
.context::<IS_SSL>()
.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
Expand Down
43 changes: 41 additions & 2 deletions test/js/web/fetch/fetch.unix.test.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -244,3 +244,42 @@
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.
const res = await fetch("https://localhost/", { unix, tls: { ca: validTls.cert } });
expect(await res.text()).toBe("ok");
expect(res.status).toBe(200);
});

Check warning on line 262 in test/js/web/fetch/fetch.unix.test.ts

View check run for this annotation

Claude / Claude Code Review

Cache-hit unix_path arm has no test coverage

The fix adds a `!unix_path.is_empty()` arm to both the SSL-context cache-hit path (HTTPThread.rs:507) and the cache-miss path (HTTPThread.rs:569), but each new test performs exactly one fetch with a fresh `tls` config, so only the cache-miss arm ever runs — deleting the cache-hit arm would leave both tests green. Adding a second identical fetch after the first `expect(res.status).toBe(200)` in the "honors tls.ca" test would exercise the cache-hit arm (SSLConfig interning is content-based, and th
Comment thread
robobun marked this conversation as resolved.
Resolved

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);
});
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Loading