You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 7ff1181
Browse filesBrowse the repository at this point in the historyBrowse files
Fix use-after-free of stream error objects parked in ByteStream (#31963)
### What does this PR do?
Fixes a use-after-free found by Fuzzilli (fingerprint
`StructureID.h(93)`, `ASSERTION FAILED: decontaminate()` in
`StructureID::decode()`).
`StreamError::JSValue` held a raw `JSValue` with no GC root. When an S3
download stream failed before a consumer attached (missing credentials
fail synchronously inside `.stream()`), the error object was parked in
`ByteStream.pending.result` across event-loop turns. A GC before the
stream was consumed freed it, so `.text()` rejected with whatever now
lived in the recycled cell, or crashed on a dead cell. Deterministic on
an unfixed build:
```js
const stream = Bun.S3Client.file("some-key").stream();
Bun.gc(true);
for (let i = 0; i < 100; i++) new TypeError("decoy " + i);
await stream.text().catch(e => e); // rejects with TypeError: decoy 0 (recycled cell)
```
The fetch, request body size limit, and
`Body::ValueError::to_stream_error` producers had the same shape.
### The fix
`StreamError::JSValue` now holds `jsc::strong::Optional`, which keeps
the value rooted while held and releases it on `Drop` (implements the
existing TODO on the variant).
This deletes most of the surrounding machinery: `to_js_weak` becomes
`to_js`, the `WasStrong` enum and every consumer-side `if was_strong ==
Strong { unprotect() }` branch are gone, the unused `WeakJSValue`
variant is gone, `StreamError` is no longer `Clone`,
`RequestContext::on_pipe` drops its raw-pointer
`scopeguard::defer!`/`unsafe` block (the by-value result drops naturally
in the trait impl), and `ByteStream::on_data` moves a parked error
instead of cloning it.
### How did you verify your code works?
- New test `test/js/bun/s3/s3-stream-error-gc.test.ts` fails on the
unfixed build (rejects with a recycled decoy object) and passes with
this change.
- The original fuzzer script no longer asserts.
- Ran the S3 test directory, `test/js/web/streams/streams.test.js`,
`test/js/web/fetch/{body-stream,body-mixin-errors,fetch.stream}.test.ts`,
`test/js/web/streams/readable-stream-blob-consumed.test.ts`, and
`test/js/bun/http/serve-pending-promise-abort-leak.test.ts` against the
debug build; only behavioral delta vs an unmodified build is the new
test passing (the remaining failures are pre-existing 5s debug+ASAN
timeouts that reproduce identically on the unmodified debug build).
- `cargo clippy -p bun_runtime` and `bun run rust:check-all` pass.
0 commit comments