Repository navigation
Conversation
A context built from a `ca` option parsed every certificate of the list whole to know that it parses, and dropped the result. A certificate whose DER is a bundled root is known to parse, so it skips that parse. - us_find_bundled_root_cert() finds a bundled root by its bytes, in a table sorted by length and then by bytes. - The subject index of the bundled roots has a once of its own, so a reader of their DER does not build it. - bun:internal-for-testing counts the certificates that a `ca` option parsed.
…`ca` A context kept a heap copy of the DER of each certificate of its `ca` option. For a bundled root it now refers to the bytes that the binary embeds, as the default store does. That is 127 KB less for each context whose `ca` holds every bundled root.
|
Updated 8:50 AM PT - Nov 21st, 2026
✅ @robobun, your commit 61ba059110506fdbcc96ad289e6694bf7d7b8f25 passed in 🧪 To try this PR locally: bunx bun-pr 49387That installs a local version of the PR into your bun-49387 --bun |
|
Status: ready for review. CI runs. How to see the cost: run Test: |
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
Beyond the inline nit, I also checked the lookup table in us_find_bundled_root_cert (the first[] index is filled in reverse so it lands on the lowest index per length, and a length no root has falls through to an empty loop), the ownership of the static-data CRYPTO_BUFFER through sk_CRYPTO_BUFFER_sort_and_dedup/CRYPTO_BUFFER_free and the lazy set, and the caCertsParsed binding chain against the existing liveCount precedent — all consistent. Not run here: the build, so the presence of CRYPTO_BUFFER_new_from_static_data_unsafe in the vendored BoringSSL fork (not in this checkout) rests on CI.
Extended reasoning...
The change touches the TLS trust-store construction path in packages/bun-usockets (openssl.cpp, root_certs.cpp) so that a ca certificate byte-identical to a bundled Mozilla root is neither re-parsed nor copied, plus a test-only counter exposed through bun:internal-for-testing and ~300 lines of new tests. The security-relevant surface is which certificates a context trusts; the lookup requires an exact full-DER match, so the set of trusted bytes is unchanged and a near-miss (one byte changed, trailing byte, wrong tag) still takes the parse-and-validate path, which the new subprocess test asserts. The only surviving finding is a test.concurrent nit, and the ruled-out candidates (pointer into the binary, static destruction at shutdown, GC size estimate, atomic counter cost) are either pre-existing patterns or negligible.
|
Both review notes are applied.
On |
Problem
tls.createSecureContext({ ca: [...tls.rootCertificates, extra] })costs 12.97 M instructions (774 µs).fetchthrough an HTTP proxy builds one per CONNECT tunnel.us_collect_ca_pem(packages/bun-usockets/src/crypto/openssl.cpp:856) parses each certificate to validate it, then drops the result. It also copies each DER: 127 KB of a live context's 177 KB.Fix
test/js/node/tls/node-tls-internals.test.ts, 5 new tests, 2 fail on main (parse count, memory). Alsotest/js/node/tls/,fetch.tls.test.ts,proxy.test.ts.Background
careplaces a context's default trust store. The store keeps DER and parses a certificate when a chain names it (tls: embed root CAs as DER and parse them lazily, SIMD base64 for certificate PEM, bump BoringSSL #40862). The build-time parse makes a bad one fail the context, not a handshake.fetch.us_bun_socket_context_options_t.Downsides
cacontext of a process builds a table: 13,858 instructions, 10 KB.Notes
What changed
us_find_bundled_root_cert()(root_certs.cpp) returns the embedded copy of the bundled root with exactly the given bytes, or NULL. It keeps the 118 roots in the order of their lengths, with the index of the first root of each length. A certificate of another length is a miss with no read of its bytes. The table is built on the first call.us_collect_ca_pemasks it for each certificate. A hit becomes aCRYPTO_BUFFERover the embedded bytes (CRYPTO_BUFFER_new_from_static_data_unsafe, asX509_LAZY_CERT_SET_new_staticdoes for the default store). A miss is copied and parsed as before.us_get_bundled_root_cert_set) has astd::call_onceof its own. Before, each reader of the DER table built it. So the first read oftls.rootCertificatesno longer builds it: 2.00 M to 0.21 M instructions.bun:internal-for-testingexportssslCaCertsParsed(), besidesslCtxLiveCount(). It counts the certificates that acaoption parsed.The first two commits can be read one at a time. The first is the skip. The second is the reference to the embedded bytes. The two after them answer review notes: two tests run concurrently, and one comment is shorter.
Why the result is the same
X509_parse_from_bufferis a function of the bytes. The testrootCertificates are this checkout's root_certs.der, in orderbuilds anX509Certificatefrom each bundled root, so a root that does not parse fails CI. An encryptedCERTIFICATEblock is compared after it is decrypted. The lookup compares all the bytes. It does not read the CA store mode (--use-system-ca,--use-openssl-ca): those flags change what the default store holds, not which bytes parse.What it reaches
Each
caoption of each TLS context goes throughus_collect_ca_pem:node:tls,Bun.connect,Bun.listen,Bun.serve,fetchand its CONNECT tunnel, WebSocket,Bun.sql. A context with nocaaftertls.setDefaultCACertificates()goes through it too, because Bun gives that list to the context as itsca(src/js/node/tls.ts:543).Numbers
Linux x64, release builds of 98eae32 (main), 8870ab4 (first commit) and d173c07 (both commits).
NODE_EXTRA_CA_CERTSis not set.User instructions for one
tls.createSecureContext(), fromperf_event_openover the whole process. Each value is the difference of two process runs, and the median of 5 such values. The script builds only the list under test.ca: 1 certificate, not bundledca: 118 bundled rootsca: 118 bundled roots and 1 certificateca: the same in one stringca: 59 bundled roots, 59 othersca: 118 certificates, none bundledca: 118 bundled roots, each with 1 byte changedca:/etc/ssl/certs/ca-certificates.crtas one string (151 certificates)ca, aftertls.setDefaultCACertificates()of 118 bundled roots and 1 certificateWall time for the same call in µs. Each process run reports the best of 5 rounds of 200 calls. The table has the minimum and the median of 9 interleaved process runs.
ca: 118 bundled roots and 1 certificateca: 118 bundled rootsca: the same in one stringca: 118 certificates, none bundledca: 118 bundled roots, each with 1 byte changedca, aftertls.setDefaultCACertificates()ca: 1 certificateThe Bun 1.4.3 and Node columns are medians of 5 runs from a second batch. The worst case for the lookup costs 8 µs (1.1%): it reads each embedded root in full.
bench/snippets/tls-ca.mjs(new) has the same rows. Median of 3 alternating runs, µs per call:ca: 1 certificateca: 118 bundled rootsca: 118 bundled roots, 1 stringca: 118 bundled roots + 1 certificateca: half bundled roots, half notca: 118 certificatesca: 118 roots but for 1 byteca: the system bundlesetDefaultCACertificates: 118 bundled roots + 1fetch(url, { proxy, tls: { ca }, keepalive: false })through an HTTP proxy, 119 certificates, a new CONNECT tunnel for each request:Memory. RSS that 64 more live contexts of the 118 roots add, after two warm-up rounds: 11.06 to 11.09 MiB on main, 11.34 MiB with the first commit, 2.57 MiB with both (177 KB to 41 KB per context). Under ASAN: 14.3 to 14.9 MiB, then 3.6 to 4.0 MiB.
One use in a new process, total instructions, median of 11 runs:
ca(builds the default store)SSL_CERT_FILEset to a bundle of 151 certificatesThe 11 runs of one row span about 30 K to 50 K, so the first two rows show no change. The third has the table: a harness that runs the same code on
root_certs.dergives 13,858 instructions for it. The table is 1.9 KB for the roots and 8 KB for the index of lengths.Size:
text155,774,875 to 155,779,022 bytes.Tests
test/js/node/tls/node-tls-internals.test.ts, the blockbundled roots in a ca option. On release builds: main 3 pass and 2 fail, the first commit 4 pass and 1 fail, both commits 5 pass.only the certificates that are not bundled roots are parsedreadssslCaCertsParsed()in a new process for 10 lists, and for a context with nocaaftertls.setDefaultCACertificates(). It also pins which lists a context refuses. A root with a public key that does not parse is refused by the parse alone: the store's own index accepts it.a context holds no copy of the DER of a bundled rootbounds the RSS of 64 contexts (6 MiB, 8 MiB under ASAN).a chain that names a bundled root finds that root and no otherserves a certificate that names a bundled root as its issuer. A client whosecahas that root getsCERT_SIGNATURE_FAILURE. A client whosecahas another root of the same length getsUNABLE_TO_VERIFY_LEAF_SIGNATURE. Node 26.3 gives the same verdicts. The issuer is the RSA root that expires last among those that share a length with another root (2048 today).the other certificates of the list are trustedanda server names a bundled root to its clientspin results and pass on main.Also run on the debug build with ASAN: all 29 files of
test/js/node/tls/,test/js/web/fetch/fetch.tls.test.ts,test/js/bun/http/proxy.test.ts, twoproxy-stressfiles,test/js/web/websocket/websocket-proxy.test.ts,test/js/node/crypto/x509.test.ts,test/js/bun/http/bun-serve-ssl.test.ts,test/js/node/quic/quic-sni.test.ts,test/js/bun/net/socket.test.ts, and 53 Node tests (test-tls-*ca*,*root*,*cert*,test-crypto-x509). Seven tests fail, and the same seven fail on a release build of main on this machine: four innode-tls-connect.test.tsthat need the network, one innode-tls-server.test.ts, one infetch.tls.test.tsand one insocket.test.ts. The new block is clean under LeakSanitizer with the options of the CI runner.Not in this PR
caFilereads its file twice for each context (SSL_load_client_CA_file, thenSSL_CTX_load_verify_locations) and the first read drops each parsed certificate: 23.5 M instructions for the 118 roots. BoringSSL's file loaders also decide what a CA file can hold and which error a bad one gives, so this PR does not send the file throughus_collect_ca_pem.NODE_EXTRA_CA_CERTSparses each certificate of its file once per process and keeps the result: 12.67 M instructions for a bundle of 151 certificates.tls.getCACertificates("extra")reads that parsed list.node:quichas acaloader of its own (load_ca_store,src/runtime/node/quic/tls.rs). http, net, tls: consolidate the open fetch, Bun.serve, node:http, node:http2, HTTP/3, node:net and node:tls pull requests #46528 replaces it with the loader that this PR changes.addCACertparses each certificate and keeps the result: 10.9 M instructions for 118 calls.us_install_ca_buffers): 11.3 KB for the 118 roots. For a bundled root those bytes are in the binary too. http, net, tls: consolidate the open fetch, Bun.serve, node:http, node:http2, HTTP/3, node:net and node:tls pull requests #46528 and tls: choose, load and verify CA certificates as Node.js does #49360 both change that function, so this PR leaves it.us_get_openssl_default_cert_file) keeps its own hash set of the bundled roots. http, net, tls: consolidate the open fetch, Bun.serve, node:http, node:http2, HTTP/3, node:net and node:tls pull requests #46528 changes the lines beside it. It can callus_find_bundled_root_certafter that PR.Other open PRs
A file by file merge of this branch with the head of #46528 (36f10f4) and with the head of #49360 (c48e652) has no conflict, so they can land in either order.
#include <algorithm>is after<vector>for that reason.In #46528 the list of
tls.setDefaultCACertificates()becomes thecaof each native client too (ca_is_default), so each of those contexts passes it throughus_collect_ca_pemand gets this skip. The certificates of that list that are not bundled roots are still parsed again for each context.