Skip to content

http,http2: copy header arrays in setHeader - #67528

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
Ethan-Arrowood:http-appendheader-array-copy
Nov 19, 2026
Merged

nodejs-github-bot merged 1 commit into
nodejs:mainfrom
Ethan-Arrowood:http-appendheader-array-copy

Conversation

@Ethan-Arrowood

@Ethan-Arrowood Ethan-Arrowood commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

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.

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>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/http2
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added 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. labels Nov 16, 2026
@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ 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     
Files with missing lines Coverage Δ
lib/internal/http/outgoing.js 98.00% <100.00%> (+<0.01%) ⬆️
lib/internal/http2/compat.js 97.27% <100.00%> (+<0.01%) ⬆️

... 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.

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina added the baking-for-lts PRs whose changes need more time before an LTS backport. label Nov 19, 2026
@mcollina

Copy link
Copy Markdown
Member

I would lean towards a bug fix

@pimterry pimterry left a comment

Copy link
Copy Markdown
Member

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.

@pimterry pimterry 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 19, 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 19, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@Ethan-Arrowood Ethan-Arrowood added the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 19, 2026
@nodejs-github-bot
nodejs-github-bot merged commit 86b9e35 into nodejs:main Nov 19, 2026
83 checks passed
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 86b9e35

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

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

http: appendHeader() mutates the array passed to setHeader()

6 participants