Skip to content

fetch: support PKCS#12 client certificates - #41958

Open
bsreeram08 wants to merge 2 commits into
oven-sh:mainfrom
bsreeram08:claude/fetch-pfx-mtls
Open

bsreeram08 wants to merge 2 commits into
oven-sh:mainfrom
bsreeram08:claude/fetch-pfx-mtls

Conversation

@bsreeram08

Copy link
Copy Markdown

Summary

  • add native fetch(url, { tls: { pfx, passphrase } }) support for BufferSource, BunFile, and arrays
  • normalize PKCS#12 private keys and certificate chains into SSLConfig, preserving connection-pool identity isolation
  • reject empty/invalid PFX input and ambiguous PFX plus PEM credentials
  • fail closed when certificate-chain serialization fails and zero transient PFX, passphrase, and private-key buffers
  • document PKCS#12 client certificates and add public types

Before this change, Bun accepted the tls object but did not consume pfx, so PKCS#12 client authentication failed during the TLS handshake.

Fixes #17543.

Tests

  • bun bd test test/js/web/fetch/fetch.tls.test.ts — 48 passed
  • BUN_JSC_validateExceptionChecks=1 BUN_JSC_dumpSimulatedThrows=1 bun bd test test/js/web/fetch/fetch.tls.test.ts -t PKCS#12 — 14 passed
  • bun test test/integration/bun-types/bun-types.test.ts -t "TypeScript latest" — passed
  • USE_SYSTEM_BUN=1 bun test test/js/web/fetch/fetch.tls.test.ts -t PKCS#12 — 13 failed on the released Bun, confirming the regression coverage exercises the new behavior

The unfiltered bun-types suite also has an unrelated existing Blob diagnostic mismatch in fixture/24154.ts; the focused TypeScript-latest fixture check passes.

Add binary PFX parsing, certificate-chain handling, validation, and secure cleanup to fetch TLS configuration. Include pooling, input-form, error-path, type, and documentation coverage.\n\nTests: bun bd test test/js/web/fetch/fetch.tls.test.ts\nTests: BUN_JSC_validateExceptionChecks=1 BUN_JSC_dumpSimulatedThrows=1 bun bd test test/js/web/fetch/fetch.tls.test.ts -t PKCS#12\nTests: bun test test/integration/bun-types/bun-types.test.ts -t 'TypeScript latest'
@bsreeram08
bsreeram08 requested a review from alii as a code owner September 8, 2026 10:11

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@coderabbitai

coderabbitai Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: oven-sh/bun/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 9eeb7f94-8e9d-4de2-b30e-b994b80c8932
📥 Commits

Reviewing files that changed from the base of the PR and between f9d11a7 and 5bd468e.

📒 Files selected for processing (7)
  • docs/runtime/networking/fetch.mdx
  • packages/bun-types/bun.d.ts
  • packages/bun-usockets/src/crypto/openssl.c
  • src/jsc/generated.rs
  • src/runtime/socket/SSLConfig.rs
  • test/integration/bun-types/fixture/fetch.ts
  • test/js/web/fetch/fetch.tls.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.


Walkthrough

Changes

Fetch TLS options now accept PKCS## Walkthrough

Changes

Fetch TLS options now accept PKCS#12/PFX client identities as binary data or BunFile values. Runtime code parses archives, supports passphrases and certificate chains, rejects incompatible PEM options, and clears sensitive buffers. Documentation, type fixtures, and tests cover the new behavior.

PKCS#12 fetch TLS support

Layer / File(s) Summary
Expose PFX TLS configuration
docs/runtime/networking/fetch.mdx, packages/bun-types/bun.d.ts, src/runtime/socket/SSLConfig.bindv2.ts, src/jsc/generated.rs
The TLS API and documentation define pfx, passphrase behavior, supported values, and PFX/PEM exclusivity.
Harden PKCS#12 extraction
packages/bun-usockets/src/crypto/openssl.c
PKCS#12 parsing reports extra-chain allocation and certificate-write failures, clears sensitive data, and resets outputs on failure.
Parse and validate PFX options
src/runtime/socket/SSLConfig.rs
Runtime configuration reads binary PFX inputs, rejects invalid forms and combinations, parses credentials and chains, and clears temporary sensitive buffers.
Validate supported PFX usage
test/integration/bun-types/fixture/fetch.ts, test/js/web/fetch/fetch.tls.test.ts
Fixtures and tests cover input types, passphrases, connection pooling, certificate chains, and rejection cases.

Suggested reviewers: jarred-sumner, cirospaciari

Priority: ➖ Normal

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 5bd46

No actionable issue remains from this review; the change is ready to merge after normal checks.

Security Architecture Review

Security architecture risk: 🔵 Low · up to 5bd46

PKCS#12 credentials are normalized into the existing TLS identity configuration, with guarded cleanup and connection-pool separation. No introduced security defect was established in the inspected fetch path. Risk remains low rather than minimal because the shared conversion also serves other TLS entrypoints whose complete behavior was not assessed.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The demonstrated source of PFX bytes is caller-selected JavaScript TLS material, not a network peer supplying an archive. BunFile inputs are read synchronously under the process's existing filesystem authority. A failure at this native parsing boundary could affect the Bun process and its credential material; remote or tenant-level reachability depends on how an application supplies those options.

Security Findings and Attack Paths

  • observed — No Security findings are retained in the canonical brief. The private-key retention candidate was rejected, and the inspected ownership path confirms that published key arrays reach sensitive freeing through SSLConfig destruction. This resolves that candidate, not the remaining broader coverage limitations.

Trust Boundaries and Controls

  • observed — Normalized PFX identities participate in the existing configuration identity checks. Equality and hashing include key, certificate-chain, CA, and verification settings; pooled HTTP sockets reject a different configuration hash. HTTP/2 reuse also passes the configuration into its session-matching checks.

Resilience and Maintainability Implications

  • observed — New transient PFX copies are zeroed on destruction; the PFX passphrase has guarded sensitive cleanup; parser-owned private-key output is zeroed before release. The native extractor additionally cleanses its private-key BIO and resets failed output pointers and lengths.
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely identifies the main change: adding PKCS#12 client certificate support to fetch.
Description check ✅ Passed The description explains the change and verification results in detail. It uses “Summary” and “Tests” instead of the template headings, but it covers both required topics.
Linked Issues check ✅ Passed Issue #17543 requests PKCS#12 certificate support in fetch TLS options, including pfx and an optional passphrase. SSLConfig::from_generated parses BufferSource, BunFile, and arrays into client key…
Out of Scope Changes check ✅ Passed The implementation, OpenSSL cleanup, type declarations, documentation, and tests all support PKCS#12 fetch TLS credentials requested by #17543. The change summary shows no unrelated changes.

Comment @coderabbitai help to get the list of available commands.

M4xymm added a commit to ravendb/ravendb-nodejs-client that referenced this pull request Sep 15, 2026
Bun's fetch silently ignores the `tls.pfx` option (oven-sh/bun#41958,
oven-sh/bun#17543), so a PFX certificate configured through authOptions
was never presented and the request went out uncertified. Its node:https
implementation does honour `pfx` (oven-sh/bun#14417), so route those
requests there instead.

BunHttpUtil installs a fetch-compatible transport backed by node:https as
the request `fetcher` - the same seam conventions.customFetch uses - when
a PKCS#12 archive is configured on Bun. PEM material keeps using Bun's
`tls` fetch option, which works. The transport covers what the client asks
of fetch: methods, headers, string/binary/FormData/stream request bodies,
streamed response bodies, AbortSignal, and content-encoding decompression.
It is built lazily, once per executor, and closed in dispose().

Before Bun 1.4.0 neither transport presents a client certificate (node:https
ignored the certificate and the ca as well), so DocumentStore.initialize()
now throws an actionable error there instead of failing per request.

RavenCommand.send passes a node:stream Readable body through when the
fetcher declares it accepts one, so streamed attachment uploads keep
working on that path.

Drops the now-obsolete console.warn from PfxCertificate.toBunTlsOptions
and documents the Bun runtime, including the pre-existing Changes API gap
(Bun's built-in WebSocket ignores the ws package's TLS options).
# Conflicts:
#	test/integration/bun-types/fixture/fetch.ts

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add PKCS#12 (.p12) Support in Fetch TLS Options

1 participant