Skip to content

fix(net): keep guest TLS aborts from crashing Bun hosts - #154

Open
guillemborrell wants to merge 1 commit into
earendil-works:mainfrom
guillemborrell:fix/bun-tls-guest-abort
Open

guillemborrell wants to merge 1 commit into
earendil-works:mainfrom
guillemborrell:fix/bun-tls-guest-abort

Conversation

@guillemborrell

Copy link
Copy Markdown
Contributor

Follow-up to #152.

When a guest closes an intercepted TLS flow while the host still has ciphertext pending, the host process crashes on Bun. Rejecting the MITM certificate is enough to trigger it: custom CA bundles, certifi, pinning, or any HTTPS in a VM without the MITM CA mounted (vfs: null).

The pending guest write in GuestTlsStream fails with "guest closed", and the wrapped stream emits error. That stream has no error listener:

  • Node forwards the error to the TLSSocket, so nothing breaks.
  • Bun raises it as an uncaught exception, and the process exits, taking every VM it owns with it.

The endNT → shutdown → _write → waitForFlowResume stack shows the trigger is the final write from tlsSocket.end() after handleTcpClose.

Fix

Add an error listener on the guest stream that destroys only that flow's TLS socket, and emit a debug message for errors other than guest-closed. This is a no-op on Node.

Test

tls-mitm-sni.test.ts gains a VM-free case:

  1. A TLS client trusting an unrelated CA rejects the MITM leaf certificate.
  2. The guest flow is closed through handleTcpClose({ destroy: false }) while the host's writes are flow-control paused.
  3. The test asserts that no uncaught exception reaches the process.
Bun 1.4.0 Node 26.7.0
before fails (GuestClosedError uncaught) passes
after passes passes

On a real VM (macOS arm64, krun): an HTTPS attempt in a deny-all VM, and a guest curl --cacert <unrelated CA> in an allowlist VM, each crashed a Bun host before this change. After it, the host survives and keeps serving, on both runtimes.

node --test test/*.test.ts: 643 pass, 13 skipped, 4 fail. The four failures (clock-sync, vfs-many-mounts, vfs chmod, exec deleteFile) also fail on main on this machine.

When a guest closes an intercepted TLS flow while the host still has
ciphertext pending (e.g. after rejecting the MITM certificate), the pending
guest write fails with "guest closed" and the wrapped GuestTlsStream emits
'error'. Node forwards it to the TLS socket; Bun does not, so it became an
uncaught exception that terminated the host process and every VM in it.

Listen for the stream error and destroy only that flow's TLS socket. Adds a
VM-free regression test that reproduces the crash on Bun.
@guillemborrell
guillemborrell marked this pull request as ready for review October 5, 2026 17:46

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.

1 participant