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

Merged
merged 3 commits into from
May 6, 2025
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
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')

// 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
20 changes: 11 additions & 9 deletions src/build/functions/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const writeHandlerFile = async (ctx: PluginContext, { matchers, name }: NextDefi

const copyHandlerDependencies = async (
ctx: PluginContext,
{ name, files, wasm }: NextDefinition,
{ name, env, files, wasm }: NextDefinition,
) => {
const srcDir = join(ctx.standaloneDir, ctx.nextDistDir)
const destDir = join(ctx.edgeFunctionsDir, getHandlerName({ name }))
Expand All @@ -132,6 +132,11 @@ const copyHandlerDependencies = async (

const outputFile = join(destDir, `server/${name}.js`)

// Prepare environment variables for draft-mode (i.e. __NEXT_PREVIEW_MODE_ID, __NEXT_PREVIEW_MODE_SIGNING_KEY, __NEXT_PREVIEW_MODE_ENCRYPTION_KEY)
for (const [key, value] of Object.entries(env)) {
parts.push(`process.env.${key} = '${value}';`)
}

if (wasm?.length) {
for (const wasmChunk of wasm ?? []) {
const data = await readFile(join(srcDir, wasmChunk.filePath))
Expand Down Expand Up @@ -161,16 +166,16 @@ const buildHandlerDefinition = (
ctx: PluginContext,
{ name, matchers, page }: NextDefinition,
): Array<ManifestFunction> => {
const fun = getHandlerName({ name })
const funName = name.endsWith('middleware')
const functionHandlerName = getHandlerName({ name })
const functionName = name.endsWith('middleware')
? 'Next.js Middleware Handler'
: `Next.js Edge Handler: ${page}`
const cache = name.endsWith('middleware') ? undefined : ('manual' as const)
const generator = `${ctx.pluginName}@${ctx.pluginVersion}`

return augmentMatchers(matchers, ctx).map((matcher) => ({
function: fun,
name: funName,
function: functionHandlerName,
name: functionName,
pattern: matcher.regexp,
cache,
generator,
Expand All @@ -183,10 +188,7 @@ export const clearStaleEdgeHandlers = async (ctx: PluginContext) => {

export const createEdgeHandlers = async (ctx: PluginContext) => {
const nextManifest = await ctx.getMiddlewareManifest()
const nextDefinitions = [
...Object.values(nextManifest.middleware),
// ...Object.values(nextManifest.functions)
]
const nextDefinitions = [...Object.values(nextManifest.middleware)]
await Promise.all(nextDefinitions.map((def) => createEdgeHandler(ctx, def)))

const netlifyDefinitions = nextDefinitions.flatMap((def) => buildHandlerDefinition(ctx, def))
Expand Down
Loading