GeezerVison is showing github.com — http,http2: copy header arrays in setHeader by Ethan-Arrowood · Pull Request #67528 · nodejs/node
Conversation
setHeader() stores an array value by reference. appendHeader() then pushes new values into that same array mutating the stored reference. A frozen array makes appendHeader() throw a TypeError.
This makes setHeader() store a copy of an array value, so the response owns its header arrays. appendHeader() keeps pushing in place, which is now safe. The HTTP/2 compat API is also updated.
getHeader() and getHeaders() still return the stored array. Mutating that array still changes the response, as the getHeaders() docs describe.
Fixes: #67526
Semver
I'm unsure if this should be a patch fix or a major breaking change. Technically we never documented that the response shares the array reference. So we could just consider it a fix. However, if anyone was relying on this behavior it could break them which is why I'm personally leaning towards marking this as a semver major change.
If we go with major and want a fix for older release lines, a smaller patch could copy inside appendHeader() instead. That fixes the mutation and the frozen-array throw without changing setHeader(). The cost is a copy on every append, so repeated appends to one header will be a performance hit.
Tests
- New
test/parallel/test-http-outgoing-append-header-array.js covers setHeader(), appendHeader(), setHeaders(), and writeHead() with plain and frozen arrays.
test/parallel/test-http2-compat-serverresponse-headers.js gets the same check for HTTP/2.
- I changed an existing test,
test/parallel/test-http2-compat-get-headers.js. It asserted that getHeaders()['set-cookie'] is the exact array passed to setHeader(). It now compares against the stored array instead. It still checks that getHeaders() is a shallow copy whose array values are the response's own.
Notes
AI-assisted (Claude Opus 5.5, my setup). The words are my own, per the Human Voice policy.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
setHeader() stored the caller's array and that led to mutation
issues with appendHeader() pushing new values into it. The caller
would see values added to that array, and if the array was frozen
appendHeader() would throw a TypeError.
This fix copies the array value instead so the response instance
owns it completely. getHeader() still return the stored array.
It also applies the same change to HTTP2 compat api.
Fixes: nodejs#67526
Assisted-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Ethan Arrowood <[email protected]>
|
Review requested:
- @nodejs/http
- @nodejs/http2
- @nodejs/net
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
codecov
Bot
commented
Nov 16, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.66%. Comparing base (428dd86) to head (26f810e).
⚠️ Report is 91 commits behind head on main.
Additional details and impacted files
@@ Coverage Diff @@
## main #67528 +/- ##
==========================================
- Coverage 90.67% 90.66% -0.01%
==========================================
Files 799 799
Lines 280314 280321 +7
Branches 54128 54127 -1
==========================================
- Hits 254171 254159 -12
- Misses 16273 16285 +12
- Partials 9870 9877 +7
... and 28 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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
I would lean towards a bug fix
|
Ethan-Arrowood reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, happy to treat this as a bug fix.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Ethan-Arrowood reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
aduh95
pushed a commit
that referenced
this pull request
Nov 20, 2026
setHeader() stored the caller's array and that led to mutation
issues with appendHeader() pushing new values into it. The caller
would see values added to that array, and if the array was frozen
appendHeader() would throw a TypeError.
This fix copies the array value instead so the response instance
owns it completely. getHeader() still return the stored array.
It also applies the same change to HTTP2 compat api.
Fixes: #67526
Assisted-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Ethan Arrowood <[email protected]>
PR-URL: #67528
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Tim Perry <[email protected]>
Reviewed-By: Gürgün Dayıoğlu <[email protected]>
gabylb
pushed a commit
to gabylb/node
that referenced
this pull request
Nov 27, 2026
setHeader() stored the caller's array and that led to mutation
issues with appendHeader() pushing new values into it. The caller
would see values added to that array, and if the array was frozen
appendHeader() would throw a TypeError.
This fix copies the array value instead so the response instance
owns it completely. getHeader() still return the stored array.
It also applies the same change to HTTP2 compat api.
Fixes: nodejs#67526
Assisted-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Ethan Arrowood <[email protected]>
PR-URL: nodejs#67528
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Tim Perry <[email protected]>
Reviewed-By: Gürgün Dayıoğlu <[email protected]>
Reviewers
mcollina
mcollina approved these changes
pimterry
pimterry approved these changes
+1 more reviewer
Reviewers whose approvals may not affect merge requirements
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Labels
baking-for-lts
PRs whose changes need more time before an LTS backport.
http
Issues and PRs related to the http subsystem.
http2
Issues and PRs related to the http2 subsystem.
needs-ci
PRs that need a full CI run.
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|