Skip to content

Commit 5c081d4

Browse files
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

File tree

‎lib/reply.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@ Reply.prototype.header = function (key, value = '') {
284284
} else {
285285
this[kReplyHeaders][key].push(value)
286286
}
287+
} else if (key === 'set-cookie' && Array.isArray(value)) {
288+
// Copy the array: later calls push into the stored one, and the caller's
289+
// array (often a module-level constant) must not grow across requests.
290+
this[kReplyHeaders][key] = value.slice()
287291
} else {
288292
this[kReplyHeaders][key] = value
289293
}

‎test/internals/reply.test.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,35 @@ test('reply can set multiple instances of same header', async t => {
10031003
t.assert.deepStrictEqual(result.headers.getSetCookie(), ['one', 'two'])
10041004
})
10051005

1006+
test('reply.header does not mutate a set-cookie array passed by the caller', async t => {
1007+
t.plan(5)
1008+
1009+
const fastify = require('../../')()
1010+
const defaults = ['a=1']
1011+
1012+
fastify.get('/headers', function (req, reply) {
1013+
reply
1014+
.header('set-cookie', defaults)
1015+
.header('set-cookie', 'b=2')
1016+
.send({})
1017+
})
1018+
1019+
const fastifyServer = await fastify.listen({ port: 0 })
1020+
t.after(() => fastify.close())
1021+
1022+
const first = await fetch(`${fastifyServer}/headers`)
1023+
t.assert.deepStrictEqual(first.headers.getSetCookie(), ['a=1', 'b=2'])
1024+
const second = await fetch(`${fastifyServer}/headers`)
1025+
t.assert.deepStrictEqual(second.headers.getSetCookie(), ['a=1', 'b=2'])
1026+
t.assert.deepStrictEqual(defaults, ['a=1'])
1027+
1028+
const reply = new Reply({ setHeader () {}, removeHeader () {} }, {}, {})
1029+
const cookies = ['one']
1030+
reply.header('set-cookie', cookies).header('set-cookie', 'two')
1031+
t.assert.deepStrictEqual(reply.getHeader('set-cookie'), ['one', 'two'])
1032+
t.assert.deepStrictEqual(cookies, ['one'])
1033+
})
1034+
10061035
test('reply.hasHeader returns correct values', async t => {
10071036
t.plan(2)
10081037

0 commit comments

Comments
 (0)