Skip to content

stream: stop reusing a detached onread buffer - #67542

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
mcollina:stream-fix-onread-detached-buffer
Nov 21, 2026
Merged

nodejs-github-bot merged 1 commit into
nodejs:mainfrom
mcollina:stream-fix-onread-detached-buffer

Conversation

@mcollina

Copy link
Copy Markdown
Member

CustomBufferJSListener keeps only the raw address (uv_buf_t) of the
user-supplied onread buffer without retaining the backing store. If
application code transfers (and thereby detaches) that buffer in the onread
callback, for example by enqueuing a view into a byte ReadableStream, the
backing store is moved out from under the listener and the raw address is left
dangling. Reading subsequent peer data into that address then corrupts memory
the application owns, and if the backing store is later released it becomes a
use-after-free.

This retains the backing store in CustomBufferJSListener so the raw address
never dangles, and stops reading when the onread callback detaches the
buffer so the stale address is not handed back to libuv.

A regression test transfers the first received chunk into a byte
ReadableStream and asserts the retained chunk is not overwritten by a later
peer write.

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Nov 17, 2026
@mcollina
mcollina force-pushed the stream-fix-onread-detached-buffer branch from 1497a39 to 4cad0dd Compare November 17, 2026 13:54
@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.69%. Comparing base (45df6b7) to head (174d8f7).
⚠️ Report is 322 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67542      +/-   ##
==========================================
- Coverage   90.69%   90.69%   -0.01%     
==========================================
  Files         797      801       +4     
  Lines      278883   280905    +2022     
  Branches    53630    54254     +624     
==========================================
+ Hits       252934   254762    +1828     
+ Misses      16314    16271      -43     
- Partials     9635     9872     +237     
Files with missing lines Coverage Δ
lib/internal/stream_base_commons.js 95.03% <100.00%> (+0.17%) ⬆️
src/stream_base.cc 76.71% <100.00%> (+0.58%) ⬆️
src/stream_base.h 63.33% <100.00%> (+1.26%) ⬆️

... and 214 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcollina
mcollina force-pushed the stream-fix-onread-detached-buffer branch 4 times, most recently from a6e20a0 to 88af668 Compare November 17, 2026 18:32
CustomBufferJSListener keeps only the raw address (uv_buf_t) of the
user-supplied onread buffer, without retaining the backing store. If
application code transfers (and thereby detaches) that buffer during the
onread callback, for example by enqueuing a view into a byte
ReadableStream, the backing store is moved away from under the listener
and the raw address is left dangling. Reading subsequent peer data into
that address then corrupts memory the application owns, and if the
backing store is later released, it becomes a use-after-free.

Retain the backing store in CustomBufferJSListener so the raw address
never dangles, and stop reading when the onread callback detaches the
buffer so the stale address is not handed back to libuv.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
mcollina force-pushed the stream-fix-onread-detached-buffer branch from 88af668 to 174d8f7 Compare November 17, 2026 19:25
@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 21, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 21, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@mcollina mcollina added the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 21, 2026
@nodejs-github-bot
nodejs-github-bot merged commit 08eff13 into nodejs:main Nov 21, 2026
73 of 74 checks passed
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 08eff13

@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants