Repository navigation
[Backport 5.x] fix: copy a set-cookie array passed to reply.header instead of storing it - #7087
Merged
Merged
Conversation
…g it (#7068) `reply.header('set-cookie', value)` stores `value` in the reply's header map. When `value` is an array, the array object itself is stored, and every subsequent `set-cookie` call on the reply pushes into it (`lib/reply.js`, the `Array.prototype.push.apply` / `.push` branch). The string case already copies into a fresh array before pushing, so only the array case leaks the caller's object. If the array is shared, for example a module-level default, it grows by one entry on every request, and responses carry more and more duplicate cookies: ```js const defaults = ['a=1'] fastify.get('/', (req, reply) => reply.header('set-cookie', defaults).header('set-cookie', 'b=2').send({})) // 1st response: a=1, b=2 2nd: a=1, b=2, b=2 3rd: a=1, b=2, b=2, b=2 ... ``` `reply.headers({ 'set-cookie': defaults })` goes through the same path. This PR stores a copy of the array, so later pushes stay local to the reply. ## Checklist - [x] run `npm run test` and `npm run benchmark` — `test/internals/reply.test.js` 86/86 with the change; the new test `reply.header does not mutate a set-cookie array passed by the caller` fails on `main` (the second response carries `b=2` twice and the caller's array has grown) and passes with this change. `eslint` is clean. - [x] tests and/or benchmarks are included - [x] documentation is changed or added — n/a, behaviour now matches the documented one - [x] commit message follows [commit guidelines](https://github.com/fastify/fastify/blob/main/CONTRIBUTING.md#commit-guidelines) Signed-off-by: Fadi Romdhan <[email protected]> Co-authored-by: Fadi Romdhan <[email protected]> (cherry picked from commit 5c081d4)
gurgunday
approved these changes
Oct 10, 2026
Tony133
approved these changes
Oct 11, 2026
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.
Backport 5c081d4 from #7068.