Skip to content

Commit c4e8389

Browse files
committed
add additional x-middleware-set-cookie filtering (#75561)
Previously when we removed this from the response we only did so for requests that flowed through middleware and static handlers. We should ensure it's filtered in `sendResponse` as well. The header is only needed internally.
1 parent b010112 commit c4e8389

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

packages/next/src/server/send-response.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ export async function sendResponse(
3636

3737
// Copy over the response headers.
3838
response.headers?.forEach((value, name) => {
39+
// `x-middleware-set-cookie` is an internal header not needed for the response
40+
if (name.toLowerCase() === 'x-middleware-set-cookie') {
41+
return
42+
}
43+
3944
// The append handling is special cased for `set-cookie`.
4045
if (name.toLowerCase() === 'set-cookie') {
4146
// TODO: (wyattjoh) replace with native response iteration when we can upgrade undici

test/e2e/app-dir/app-middleware/app-middleware.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ describe('app-dir with middleware', () => {
191191
const response = await next.fetch('/rsc-cookies/cookie-options')
192192
expect(response.status).toBe(200)
193193
expect(response.headers.get('x-middleware-set-cookie')).toBeNull()
194+
195+
const response2 = await next.fetch('/cookies/api')
196+
expect(response2.status).toBe(200)
197+
expect(response2.headers.get('x-middleware-set-cookie')).toBeNull()
198+
expect(response2.headers.get('set-cookie')).toBeDefined()
199+
expect(response2.headers.get('set-cookie')).toContain('example')
194200
})
195201

196202
it('should ignore x-middleware-set-cookie as a request header', async () => {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NextResponse } from 'next/server'
2+
3+
export function GET() {
4+
const response = new NextResponse()
5+
response.cookies.set({
6+
name: 'example',
7+
value: 'example',
8+
})
9+
10+
return response
11+
}

0 commit comments

Comments
 (0)