Repository navigation
http,http2: copy header arrays in setHeader - #67528
Merged
nodejs-github-bot merged 1 commit intoNov 19, 2026
Merged
Conversation
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 <noreply@anthropic.com> Signed-off-by: Ethan Arrowood <ethan@arrowood.dev>
Collaborator
|
Review requested:
|
bjohansebas
approved these changes
Nov 16, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
Member
|
I would lean towards a bug fix |
pimterry
approved these changes
Nov 19, 2026
pimterry
left a comment
Member
There was a problem hiding this comment.
LGTM, happy to treat this as a bug fix.
Collaborator
gurgunday
approved these changes
Nov 19, 2026
Collaborator
|
Landed in 86b9e35 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
setHeader()stores an array value by reference.appendHeader()then pushes new values into that same array mutating the stored reference. A frozen array makesappendHeader()throw aTypeError.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()andgetHeaders()still return the stored array. Mutating that array still changes the response, as thegetHeaders()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 changingsetHeader(). The cost is a copy on every append, so repeated appends to one header will be a performance hit.Tests
test/parallel/test-http-outgoing-append-header-array.jscoverssetHeader(),appendHeader(),setHeaders(), andwriteHead()with plain and frozen arrays.test/parallel/test-http2-compat-serverresponse-headers.jsgets the same check for HTTP/2.test/parallel/test-http2-compat-get-headers.js. It asserted thatgetHeaders()['set-cookie']is the exact array passed tosetHeader(). It now compares against the stored array instead. It still checks thatgetHeaders()is a shallow copy whose array values are the response's own.Notes
value.slice()rather thanArrayPrototypeSlice(). The lint rule that forbids prototype primordials in the HTTP and HTTP/2 files still lists the oldlib/_http_*.jspaths, so it doesn't coverlib/internal/http/. I'm opening a separate PR to update it: tools: apply http lint rule to lib/internal/http #67529appendHeader()inwriteHead(). With this change, those copies wouldn't be needed.AI-assisted (Claude Opus 5.5, my setup). The words are my own, per the Human Voice policy.