Repository navigation
Description
What version of Bun is running?
1.4.2
What platform is your computer?
Windows 11 Pro 26200 (x64). Also reproduced by inspection of the same shim on other platforms.
What steps can reproduce the bug?
Bun's ws shim reads TLS material from options.tls (and from agent), and that path works
for PEM. It does not work for a PKCS#12 archive: tls.pfx / tls.passphrase are accepted
without error and then dropped, so the socket connects and presents no client certificate.
node:https on the same Bun build presents the same archive correctly, which makes the
gap specific to the WebSocket TLS layer.
Certificates:
openssl req -x509 -newkey rsa:2048 -nodes -keyout ca.key -out ca.pem -days 365 -subj "/CN=Repro Test CA"
openssl req -newkey rsa:2048 -nodes -keyout server.key -out server.csr -subj "/CN=localhost"
printf "subjectAltName=DNS:localhost,IP:127.0.0.1\nextendedKeyUsage=serverAuth\n" > server.ext
openssl x509 -req -in server.csr -CA ca.pem -CAkey ca.key -CAcreateserial -out server.pem -days 365 -extfile server.ext
openssl req -newkey rsa:2048 -nodes -keyout client.key -out client.csr -subj "/CN=repro-client"
printf "extendedKeyUsage=clientAuth\n" > client.ext
openssl x509 -req -in client.csr -CA ca.pem -CAkey ca.key -CAcreateserial -out client.pem -days 365 -extfile client.ext
openssl pkcs12 -export -out client.pfx -inkey client.key -in client.pem -certfile ca.pem -passout pass:reproserver.mjs, run with node server.mjs - an https + wss server that requires a client
certificate and logs the CN it sees:
import { createServer } from "node:https";
import { readFileSync } from "node:fs";
import { WebSocketServer } from "ws";
const server = createServer({
cert: readFileSync("server.pem"),
key: readFileSync("server.key"),
ca: readFileSync("ca.pem"),
requestCert: true,
rejectUnauthorized: true
}, (req, res) => {
res.end(req.socket.getPeerCertificate()?.subject?.CN ?? "(none)");
});
new WebSocketServer({ server }).on("connection", (ws, req) => {
console.log("[server] wss connection, client CN =", req.socket.getPeerCertificate()?.subject?.CN ?? "(none)");
ws.send("hello");
});
server.on("tlsClientError", (e) => console.log("[server] tlsClientError:", e.message.split("\n")[0]));
server.listen(8445, "127.0.0.1", () => console.log("[server] listening on 8445"));client.mjs, run with bun client.mjs - same archive, two transports:
import { readFileSync } from "node:fs";
import { request } from "node:https";
import { WebSocket } from "ws";
const pfx = { pfx: readFileSync("client.pfx"), passphrase: "repro", ca: readFileSync("ca.pem") };
const viaHttps = await new Promise((resolve) => {
const req = request({ host: "127.0.0.1", port: 8445, path: "/", method: "GET", ...pfx }, (res) => {
let b = ""; res.on("data", (c) => b += c); res.on("end", () => resolve("OK body=" + b));
});
req.on("error", (e) => resolve("ERROR " + e.message));
req.end();
});
console.log("node:https + pfx :", viaHttps);
const viaWs = await new Promise((resolve) => {
const ws = new WebSocket("wss://127.0.0.1:8445", { ...pfx, tls: pfx });
const t = setTimeout(() => resolve("TIMEOUT"), 5000);
ws.on("open", () => { clearTimeout(t); resolve("OPEN"); ws.close(); });
ws.on("error", (e) => { clearTimeout(t); resolve("ERROR " + e.message); });
});
console.log("ws + pfx (flat and tls) :", viaWs);
process.exit(0);What is the expected behavior?
tls.pfx / tls.passphrase unseal the archive and present its certificate, the same way
node:https already does on Bun, and the same way Node's ws does. On Node 24.11.1 both
lines print a success and the server logs client CN = repro-client:
node:https + pfx : OK body=repro-client
ws + pfx (flat and tls) : OPEN
What do you see instead?
On Bun 1.4.2 the node:https request authenticates and the WebSocket does not:
node:https + pfx : OK body=repro-client
ws + pfx (flat and tls) : ERROR WebSocket connection to 'wss://127.0.0.1:8445/' failed: Connection ended
Server side, for the WebSocket attempt only:
[server] tlsClientError: ...:SSL routines:tls_process_client_certificate:peer did not return a certificate
An https.Agent carrying the archive is dropped the same way:
new WebSocket(url, { agent: new Agent({ pfx, passphrase: "repro", ca }) }) // Connection ended
new WebSocket(url, { agent: new Agent({ cert, key, ca }) }) // OPENAdditional information
- PEM through the same option works and presents the certificate correctly, so this is
specific to PKCS#12:new WebSocket(url, { tls: { cert, key, ca } })opens and the server
logsclient CN = repro-clienton Bun 1.4.2. - This is not the same defect as WebSocket npm fails TLS handshake with self signed certificates #31396 / ws: forward top-level TLS options (rejectUnauthorized, ca, ...) to the connection #31397. Those cover top-level TLS options being
dropped in favour ofoptions.tls; landing ws: forward top-level TLS options (rejectUnauthorized, ca, ...) to the connection #31397 would forwardpfxinto a TLS layer that
still has nopfxsupport, so this case would keep failing. Worth fixing together. - Same family as the
fetchside, fetch: support PKCS#12 client certificates #41958 and Add PKCS#12 (.p12) Support in Fetch TLS Options #17543 (tls.pfxignored byfetch), which
pfxoption ofhttps.Agentisn't respected #14417 notesnode:httpsdoes not share. - Impact: the RavenDB Node.js client authenticates with either PEM or PKCS#12. On Bun its
Changes API (awss://connection throughws) cannot authenticate at all today, and even
after ws: forward top-level TLS options (rejectUnauthorized, ca, ...) to the connection #31397 the PKCS#12 half would stay broken.
Activity
robobun commentedon Sep 11, 2026
Reproduced:
tls: { pfx }andagent: new https.Agent({ pfx })on thewsshim connect with no client certificate.Fix in progress: #42325