Repository navigation
Commit 5c081d4
fix: copy a set-cookie array passed to reply.header instead of storing 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]>1 parent b7eb53e commit 5c081d4
2 files changed
Lines changed: 33 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
284 | 284 | | |
285 | 285 | | |
286 | 286 | | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
287 | 291 | | |
288 | 292 | | |
289 | 293 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1003 | 1003 | | |
1004 | 1004 | | |
1005 | 1005 | | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
1006 | 1035 | | |
1007 | 1036 | | |
1008 | 1037 | | |
| |||
0 commit comments