Unverified Commit bda5dc3b authored by Kevin Mårtensson's avatar Kevin Mårtensson Committed by GitHub

fix: replace original request body after middleware execution (#77662)

In https://github.com/vercel/next.js/pull/77553 we fixed reading the
request body in middleware using the `nodejs` runtime. However, this
caused issues with subsequent reads like in server actions. In sandbox,
[we
run](https://github.com/vercel/next.js/blob/1e62ce2c61048ddc0297f1a4f268894541975521/packages/next/src/server/web/sandbox/sandbox.ts#L146-L148)
`.finalize()` after middleware is executed so we should do the same
here.

Fixes https://github.com/vercel/next.js/issues/77646

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 1abba59a
......@@ -1687,11 +1687,26 @@ export default class NextNodeServer extends BaseServer<
const adapterFn: typeof import('./web/adapter').adapter =
middlewareModule.default || middlewareModule
result = await adapterFn({
handler: middlewareModule.middleware || middlewareModule,
request: requestData,
page: 'middleware',
})
const hasRequestBody =
!['HEAD', 'GET'].includes(params.request.method) &&
Boolean(requestData.body)
try {
result = await adapterFn({
handler: middlewareModule.middleware || middlewareModule,
request: {
...requestData,
body: hasRequestBody
? requestData.body.cloneBodyStream()
: undefined,
},
page: 'middleware',
})
} finally {
if (hasRequestBody) {
requestData.body.finalize()
}
}
} else {
const { run } = require('./web/sandbox') as typeof import('./web/sandbox')
......
process.env.TEST_NODE_MIDDLEWARE = 'true'
require('./app-action-form-state.test')
/* eslint-disable jest/no-standalone-expect */
import { nextTestSetup } from 'e2e-utils'
import { FileRef, nextTestSetup } from 'e2e-utils'
import { check } from 'next-test-utils'
import { join } from 'path'
describe('app-dir action useActionState', () => {
const { next } = nextTestSetup({
files: __dirname,
overrideFiles: process.env.TEST_NODE_MIDDLEWARE
? {
'middleware.js': new FileRef(join(__dirname, 'middleware-node.js')),
}
: {},
dependencies: {
nanoid: '4.0.1',
},
......
process.env.TEST_NODE_MIDDLEWARE = 'true'
require('./app-action.test')
process.env.TEST_NODE_MIDDLEWARE = 'true'
require('./app-action-size-limit-invalid.test')
import { NextInstance, nextTestSetup } from 'e2e-utils'
import { FileRef, NextInstance, nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
import { createRequestTracker } from 'e2e-utils/request-tracker'
import stripAnsi from 'strip-ansi'
import { accountForOverhead } from './account-for-overhead'
import { join } from 'path'
const CONFIG_ERROR =
'Server Actions Size Limit must be a valid number or filesize format larger than 1MB'
......@@ -10,6 +11,11 @@ const CONFIG_ERROR =
describe('app-dir action size limit invalid config', () => {
const { next, isNextStart, isNextDeploy, skipped } = nextTestSetup({
files: __dirname,
overrideFiles: process.env.TEST_NODE_MIDDLEWARE
? {
'middleware.js': new FileRef(join(__dirname, 'middleware-node.js')),
}
: {},
skipStart: true,
dependencies: {
nanoid: '4.0.1',
......@@ -43,7 +49,8 @@ describe('app-dir action size limit invalid config', () => {
`
module.exports = {
experimental: {
serverActions: { bodySizeLimit: -3000 }
serverActions: { bodySizeLimit: -3000 },
nodeMiddleware: true
},
}
`
......@@ -61,7 +68,8 @@ describe('app-dir action size limit invalid config', () => {
`
module.exports = {
experimental: {
serverActions: { bodySizeLimit: 'testmb' }
serverActions: { bodySizeLimit: 'testmb' },
nodeMiddleware: true
},
}
`
......@@ -79,7 +87,8 @@ describe('app-dir action size limit invalid config', () => {
`
module.exports = {
experimental: {
serverActions: { bodySizeLimit: '-3000mb' }
serverActions: { bodySizeLimit: '-3000mb' },
nodeMiddleware: true
},
}
`
......
/* eslint-disable jest/no-standalone-expect */
import { nextTestSetup } from 'e2e-utils'
import { FileRef, nextTestSetup } from 'e2e-utils'
import {
assertHasRedbox,
retry,
......@@ -20,6 +20,11 @@ describe('app-dir action handling', () => {
const { next, isNextDev, isNextStart, isNextDeploy, isTurbopack } =
nextTestSetup({
files: __dirname,
overrideFiles: process.env.TEST_NODE_MIDDLEWARE
? {
'middleware.js': new FileRef(join(__dirname, 'middleware-node.js')),
}
: {},
dependencies: {
nanoid: '4.0.1',
'server-only': 'latest',
......
// Ensure that https://github.com/vercel/next.js/issues/56286 is fixed.
import { NextResponse } from 'next/server'
export async function middleware(req) {
if (req.nextUrl.pathname.includes('rewrite-to-static-first')) {
req.nextUrl.pathname = '/static/first'
return NextResponse.rewrite(req.nextUrl)
}
return NextResponse.next()
}
/**
* @type {import('next/server').MiddlewareConfig}
*/
export const config = {
runtime: 'nodejs',
}
......@@ -9,12 +9,3 @@ export async function middleware(req) {
return NextResponse.next()
}
/**
* @type {import('next/server').MiddlewareConfig}
*/
export const config = {
// Ensure that middleware doesn't interfere with the request body parsing for
// this test fixture.
matcher: ['/((?!decode-req-body).*)'],
}
......@@ -5,6 +5,7 @@ module.exports = {
fetches: {},
},
experimental: {
nodeMiddleware: true,
serverActions: { bodySizeLimit: '2mb' },
},
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment