Skip to content

[Backport 5.x] fix: copy a set-cookie array passed to reply.header instead of storing it - #7087

Merged
Tony133 merged 2 commits into
5.xfrom
backport-7068-to-5.x
Oct 12, 2026
Merged

Tony133 merged 2 commits into
5.xfrom
backport-7068-to-5.x

Conversation

@github-actions

@github-actions github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown

Backport 5c081d4 from #7068.

…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)
@Tony133
Tony133 merged commit 38d2b63 into 5.x Oct 12, 2026
60 checks passed
@Tony133
Tony133 deleted the backport-7068-to-5.x branch October 12, 2026 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants