Skip to content

ws / WebSocket: tls.pfx is ignored, so a PKCS#12 client certificate is never presented #42322

Open

Description

@M4xymm

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:repro

server.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 }) })                 // OPEN

Additional information

Activity

  1. robobun commented on Sep 11, 2026

    @robobun
    Collaborator

    Reproduced: tls: { pfx } and agent: new https.Agent({ pfx }) on the ws shim connect with no client certificate.
    Fix in progress: #42325

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions