Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions src/runtime/socket/Handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,20 @@ impl SocketConfig {
mode: SocketMode,
) -> JsResult<SocketConfig> {
let generated = GeneratedSocketConfig::from_js(global_object, opts)?;
if matches!(generated.tls, GeneratedTls::Object(_)) {
// The `tls` dictionary accepts any object, including an array.
// `Bun.serve` takes an array of TLS options for SNI; `Bun.listen`
// and `Bun.connect` do not. An array has none of the named fields,
// so it would silently build a plaintext socket.
Comment thread
robobun marked this conversation as resolved.
Resolved
if let Some(tls) = opts.get(global_object, "tls")? {
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
if tls.js_type().is_array() {
return Err(global_object.throw_invalid_arguments(format_args!(
"Expected \"tls\" to be an object or a boolean, not an array. \
Bun.listen and Bun.connect do not accept an array of TLS options"
)));
}
}
}
Self::from_generated(vm, global_object, generated, mode)
}
}
Expand Down
45 changes: 45 additions & 0 deletions test/js/bun/net/socket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,51 @@ it("should throw on empty unix path from truthy non-string value", () => {
expect(() => Bun.connect({ unix: [] as any, socket })).toThrow("SocketOptions.unix must be a string");
});

it("should throw on a tls array instead of starting a plaintext socket", async () => {
const socket = { data() {}, open() {}, close() {} };
// An array has none of the TLSOptions fields. Without the check it parses as
// an empty config and the listener comes up as plain TCP.
const message = 'Expected "tls" to be an object or a boolean, not an array';
for (const tlsOption of [[tls], [tls, { serverName: "a.test", ...tls }], []]) {
expect(() => Bun.listen({ hostname: "127.0.0.1", port: 0, tls: tlsOption as any, socket })).toThrow(message);
expect(() => Bun.connect({ hostname: "127.0.0.1", port: 1, tls: tlsOption as any, socket })).toThrow(message);
}
Comment thread
robobun marked this conversation as resolved.
Resolved

// The same entry as a plain object still produces a TLS listener.
using server = Bun.listen({
hostname: "127.0.0.1",
port: 0,
tls,
socket: {
data(s) {
s.write("hello");
s.end();
},
open() {},
close() {},
},
});
const { promise, resolve } = Promise.withResolvers<string>();
let received = "";
await Bun.connect({
hostname: "127.0.0.1",
port: server.port,
tls: { rejectUnauthorized: false },
socket: {
open(s) {
s.write("ping");
},
data(_s, chunk) {
received += chunk.toString();
},
close() {
resolve(received);
},
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
},
});
expect(await promise).toBe("hello");
});

it("reading .listener on a closed client socket does not use-after-free handlers", async () => {
// Client-mode Handlers is heap-allocated per-connect and freed in
// markInactive once the socket closes. `socket.listener` read
Expand Down
Loading