Skip to content
Open
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
51 changes: 51 additions & 0 deletions src/runtime/server/ServerConfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,49 @@ fn validate_route_name(global: &JSGlobalObject, path: &[u8]) -> JsResult<()> {
Ok(())
}

/// The keys of `SSLConfig.bindv2.ts`.
const TLS_OPTION_KEYS: &[&str] = &[
"passphrase",
"dhParamsFile",
"serverName",
"servername",
"lowMemoryMode",
"rejectUnauthorized",
"requestCert",
"ca",
"cert",
"key",
"secureOptions",
"minVersion",
"maxVersion",
"keyFile",
"certFile",
"caFile",
"ALPNProtocols",
"ciphers",
"clientRenegotiationLimit",
"clientRenegotiationWindow",
"crl",
"allowPartialTrustChain",
"sessionTimeout",
"sigalgs",
"ecdhCurve",
];

fn first_top_level_tls_key(
global: &JSGlobalObject,
arg: JSValue,
) -> JsResult<Option<&'static str>> {
for &key in TLS_OPTION_KEYS {
if let Some(value) = arg.get(global, key)?
&& !value.is_null()
{
return Ok(Some(key));
}
}
Ok(None)
}

fn get_routes_object(global: &JSGlobalObject, arg: JSValue) -> JsResult<Option<JSValue>> {
for key in ["routes", "static"] {
if let Some(routes) = arg.get(global, key)? {
Expand Down Expand Up @@ -1241,6 +1284,14 @@ impl ServerConfig {
}

if let Some(tls) = arg.get_truthy(global, "tls")? {
if tls.is_object() {
if let Some(key) = first_top_level_tls_key(global, arg)? {
return Err(global.throw_invalid_arguments(format_args!(
"Bun.serve() received both \"tls\" and the top-level TLS option \"{key}\". \
Move \"{key}\" into the \"tls\" object.",
)));
}
}
Comment thread
robobun marked this conversation as resolved.
Resolved
if tls.is_falsey() {
args.ssl_config = None;
} else if tls.js_type().is_array() {
Expand Down
94 changes: 94 additions & 0 deletions test/js/bun/http/bun-serve-ssl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,100 @@ describe("Bun.serve SSL validations", () => {
}
});

describe("Bun.serve top-level TLS options mixed with tls", () => {
const tlsFixtures = join(import.meta.dir, "..", "..", "node", "tls", "fixtures");
const serverKey = readFileSync(join(tlsFixtures, "agent10-key.pem"), "utf8");
const serverCert = readFileSync(join(tlsFixtures, "agent10-cert.pem"), "utf8");
const clientCa = readFileSync(join(tlsFixtures, "ca5-cert.pem"), "utf8");
const fetch = () => new Response("Hello, world!");

const mixed = [
{
label: "mTLS keys at the top level next to tls: {cert, key}",
options: { ca: clientCa, requestCert: true, rejectUnauthorized: true, tls: { key: serverKey, cert: serverCert } },
key: "rejectUnauthorized",
},
{
label: "cert and key at the top level next to tls: {ca, requestCert}",
options: { key: serverKey, cert: serverCert, tls: { ca: clientCa, requestCert: true, rejectUnauthorized: true } },
key: "cert",
},
{
label: "top-level keys next to a tls object that only holds defaults",
options: { key: serverKey, cert: serverCert, requestCert: true, tls: { requestCert: false } },
key: "requestCert",
},
{
label: "top-level keys next to an empty tls object",
options: { key: serverKey, cert: serverCert, tls: {} },
key: "cert",
},
{
label: "top-level keys next to a tls array",
options: { ca: clientCa, tls: [{ key: serverKey, cert: serverCert }] },
key: "ca",
},
{
label: "the servername alias at the top level",
options: { servername: "localhost", tls: { key: serverKey, cert: serverCert } },
key: "servername",
},
];
for (const { label, options, key } of mixed) {
test(label, () => {
expect(() => Bun.serve({ port: 0, fetch, ...(options as any) })).toThrow(
`Bun.serve() received both "tls" and the top-level TLS option "${key}". Move "${key}" into the "tls" object.`,
);
});
}

test("top-level TLS keys set to undefined or null are not a mix", async () => {
using server = Bun.serve({
port: 0,
fetch,
ca: undefined,
requestCert: undefined,
cert: null,
rejectUnauthorized: null,
tls: { key: serverKey, cert: serverCert },
} as any);
const res = await globalThis.fetch(server.url, { tls: { rejectUnauthorized: false } });
expect(await res.text()).toBe("Hello, world!");
});

test("tls: false next to top-level keys is not a mix", async () => {
using server = Bun.serve({
port: 0,
fetch,
key: serverKey,
cert: serverCert,
tls: false,
} as any);
const res = await globalThis.fetch(server.url, { tls: { rejectUnauthorized: false } });
expect(await res.text()).toBe("Hello, world!");
});

test("the legacy top-level form alone still enforces mTLS", async () => {
using server = Bun.serve({
port: 0,
fetch,
key: serverKey,
cert: serverCert,
ca: clientCa,
requestCert: true,
rejectUnauthorized: true,
} as any);
const { promise, resolve } = Promise.withResolvers<string>();
const socket = tls.connect({ host: "127.0.0.1", port: server.port, rejectUnauthorized: false });
let received = "";
socket.on("secureConnect", () => socket.write("GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"));
socket.on("data", chunk => (received += chunk.toString()));
socket.on("error", () => {});
socket.on("close", () => resolve(received.split("\r\n")[0] || "connection closed without a response"));
expect(await promise).toBe("connection closed without a response");
});
});

describe("Bun.serve per-serverName client certificate policy", () => {
const tlsFixtures = join(import.meta.dir, "..", "..", "node", "tls", "fixtures");
const serverKey = readFileSync(join(tlsFixtures, "agent10-key.pem"), "utf8");
Expand Down
1 change: 0 additions & 1 deletion test/js/web/fetch/fetch.tls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,6 @@ describe.concurrent("fetch-tls", () => {
// 127.0.0.1, turning the timeout under test into ConnectionRefused.
hostname: "127.0.0.1",
port: 0,
rejectUnauthorized: false,
async fetch() {
async function* body() {
yield "Hello, ";
Expand Down
Loading