Skip to content

fix: ensure internal x-middleware-set-cookie header is not passed on to lambda #2891

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions edge-runtime/lib/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export function mergeMiddlewareCookies(middlewareResponse: Response, lambdaReque
const middlewareCookies = middlewareResponse.headers.get('x-middleware-set-cookie')

if (middlewareCookies) {
// Next expects internal headers to be omitted when cookies are set by the middleware
// See: https://github.com/vercel/next.js/blob/005db43079c7b59fd8c2594e8362761dc4cb3211/test/e2e/app-dir/app-middleware/app-middleware.test.ts#L197-L207
middlewareResponse.headers.delete('x-middleware-set-cookie')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does final response reaching client (browser/curl etc) would have actual set-cookie with this removal? from what I can see mergeMiddlewareCookies only sets cookie header:

const newRequestCookies = mergeMiddlewareCookies(edgeResponse, newRequest)
if (newRequestCookies) {
newRequest.headers.set('Cookie', newRequestCookies)
}

so I wonder if this change as-is wouldn't result in clients no longer actually receiving set-cookie headers?


// Targets commas that are not followed by whitespace
// See: https://github.com/vercel/next.js/blob/e6145d3a37bb4c7b481fd58e05cdff9046ace8ad/packages/next/src/server/web/spec-extension/response.ts#L58-L66
const regex = new RegExp(/,(?!\s)/)
Expand Down
9 changes: 9 additions & 0 deletions tests/test-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,15 @@
"tests": [
"router autoscrolling on navigation bugs Should apply scroll when loading.js is used"
]
},
{
"file": "test/e2e/app-dir/app-middleware/app-middleware.test.ts",
"reason": "Relies on access to environment variables set on the edge",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"tests": [
"app-dir with middleware Mutate request headers for Serverless Functions Supports draft mode",
"app-dir with middleware Mutate request headers for Edge Functions Supports draft mode",
"app-dir with middleware Mutate request headers for next/headers Supports draft mode"
]
}
],
"failures": [
Expand Down
Loading