Skip to content

add additional x-middleware-set-cookie filtering #75561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/next/src/server/send-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export async function sendResponse(

// Copy over the response headers.
response.headers?.forEach((value, name) => {
// `x-middleware-set-cookie` is an internal header not needed for the response
if (name.toLowerCase() === 'x-middleware-set-cookie') {
return
}

// The append handling is special cased for `set-cookie`.
if (name.toLowerCase() === 'set-cookie') {
// TODO: (wyattjoh) replace with native response iteration when we can upgrade undici
Expand Down
6 changes: 6 additions & 0 deletions test/e2e/app-dir/app-middleware/app-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ describe('app-dir with middleware', () => {
const response = await next.fetch('/rsc-cookies/cookie-options')
expect(response.status).toBe(200)
expect(response.headers.get('x-middleware-set-cookie')).toBeNull()

const response2 = await next.fetch('/cookies/api')
expect(response2.status).toBe(200)
expect(response2.headers.get('x-middleware-set-cookie')).toBeNull()
expect(response2.headers.get('set-cookie')).toBeDefined()
expect(response2.headers.get('set-cookie')).toContain('example')
})

it('should ignore x-middleware-set-cookie as a request header', async () => {
Expand Down
11 changes: 11 additions & 0 deletions test/e2e/app-dir/app-middleware/app/cookies/api/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { NextResponse } from 'next/server'

export function GET() {
const response = new NextResponse()
response.cookies.set({
name: 'example',
value: 'example',
})

return response
}
Loading