Unverified Commit 1a07ce20 authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

fix: bodySizeLimit error responses + limit for non-multipart actions (#77746)

This PR fixes some bugs in our handling of
`serverActions.bodySizeLimit`. There's a couple fixes here, which can be
viewed commit by commit.

### 1. Uncaught exception error when exceeding the size limit
We were using `body.pipe(busboy)`, which does not forward errors from
the source to the sink, so the busboy stream would just hang, and we'd
log an error, but never produce a response. It seems like node is
(usually) smart enough to abort the request when this happens (because
it was triggering an uncaught exception), but we should be returning a
proper response instead. This is fixed by using
`require('node:stream').pipeline(body, busboy)`, which propagates errors
correctly.

### 2. Apply size limit to non-multipart fetch actions

We have a separate codepath for non-multipart fetch actions. This can
happen if the action arguments are simple enough that react doesn't need
to use multiple rows to serialize them -- generally, this means that
they're JSON-esque without any complex types like Promises, Maps, or
Sets.

This codepath was consuming the original request body, not the one piped
through the size limit transform. So we'd print the error (because the
transform is subscribed to the body), but still execute the action.

### 3. Tests

The likely reason that these bugs slipped through is that the tests were
only checking if an error was printed to the console, and not checking
if the server actually responded with an error. To prevent this, I've
added some assertions on the responses' status codes + checking whether
an error boundary was triggered. I've also tweaked the tests so that the
parts that submit actions can run in deploy mode.

The request interception code proved subtle/complicated enough that i
decided to pull it out into a separate helper. We have a whole bunch of
tests that intercept requests/responses using various ad-hoc tangles of
`page/browser.on('request', ...)` which i'd like to migrate to this
helper, because that'd make them easier to understand, but I'll leave
that for a follow up when there's time.
parent f43f86f3
...@@ -752,7 +752,7 @@ export async function handleAction({ ...@@ -752,7 +752,7 @@ export async function handleAction({
temporaryReferences = createTemporaryReferenceSet() temporaryReferences = createTemporaryReferenceSet()
const { Transform } = const { Transform, pipeline } =
require('node:stream') as typeof import('node:stream') require('node:stream') as typeof import('node:stream')
const defaultBodySizeLimit = '1 MB' const defaultBodySizeLimit = '1 MB'
...@@ -766,26 +766,32 @@ export async function handleAction({ ...@@ -766,26 +766,32 @@ export async function handleAction({
: 1024 * 1024 // 1 MB : 1024 * 1024 // 1 MB
let size = 0 let size = 0
const body = req.body.pipe( const sizeLimitTransform = new Transform({
new Transform({ transform(chunk, encoding, callback) {
transform(chunk, encoding, callback) { size += Buffer.byteLength(chunk, encoding)
size += Buffer.byteLength(chunk, encoding) if (size > bodySizeLimitBytes) {
if (size > bodySizeLimitBytes) { const { ApiError } = require('../api-utils')
const { ApiError } = require('../api-utils')
callback(
callback( new ApiError(
new ApiError( 413,
413, `Body exceeded ${bodySizeLimit} limit.\n` +
`Body exceeded ${bodySizeLimit} limit. `To configure the body size limit for Server Actions, see: https://nextjs.org/docs/app/api-reference/next-config-js/serverActions#bodysizelimit`
To configure the body size limit for Server Actions, see: https://nextjs.org/docs/app/api-reference/next-config-js/serverActions#bodysizelimit`
)
) )
return )
} return
}
callback(null, chunk) callback(null, chunk)
}, },
}) })
const sizeLimitedBody = pipeline(
req.body,
sizeLimitTransform,
// Avoid unhandled errors from `pipeline()` by passing an empty completion callback.
// We'll propagate the errors properly when consuming the stream.
() => {}
) )
if (isMultipartAction) { if (isMultipartAction) {
...@@ -796,7 +802,14 @@ export async function handleAction({ ...@@ -796,7 +802,14 @@ export async function handleAction({
limits: { fieldSize: bodySizeLimitBytes }, limits: { fieldSize: bodySizeLimitBytes },
}) })
body.pipe(busboy) // We need to use `pipeline(one, two)` instead of `one.pipe(two)` to propagate size limit errors correctly.
pipeline(
sizeLimitedBody,
busboy,
// Avoid unhandled errors from `pipeline()` by passing an empty completion callback.
// We'll propagate the errors properly when consuming the stream.
() => {}
)
boundActionArguments = await decodeReplyFromBusboy( boundActionArguments = await decodeReplyFromBusboy(
busboy, busboy,
...@@ -812,13 +825,13 @@ export async function handleAction({ ...@@ -812,13 +825,13 @@ export async function handleAction({
headers: { 'Content-Type': contentType }, headers: { 'Content-Type': contentType },
body: new ReadableStream({ body: new ReadableStream({
start: (controller) => { start: (controller) => {
body.on('data', (chunk) => { sizeLimitedBody.on('data', (chunk) => {
controller.enqueue(new Uint8Array(chunk)) controller.enqueue(new Uint8Array(chunk))
}) })
body.on('end', () => { sizeLimitedBody.on('end', () => {
controller.close() controller.close()
}) })
body.on('error', (err) => { sizeLimitedBody.on('error', (err) => {
controller.error(err) controller.error(err)
}) })
}, },
...@@ -865,7 +878,7 @@ export async function handleAction({ ...@@ -865,7 +878,7 @@ export async function handleAction({
} }
const chunks: Buffer[] = [] const chunks: Buffer[] = []
for await (const chunk of req.body) { for await (const chunk of sizeLimitedBody) {
chunks.push(Buffer.from(chunk)) chunks.push(Buffer.from(chunk))
} }
...@@ -1016,6 +1029,9 @@ export async function handleAction({ ...@@ -1016,6 +1029,9 @@ export async function handleAction({
} }
if (isFetchAction) { if (isFetchAction) {
// TODO: consider checking if the error is an `ApiError` and change status code
// so that we can respond with a 413 to requests that break the body size limit
// (but if we do that, we also need to make sure that whatever handles the non-fetch error path below does the same)
res.statusCode = 500 res.statusCode = 500
await executeRevalidates(workStore) await executeRevalidates(workStore)
const promise = Promise.reject(err) const promise = Promise.reject(err)
......
'use client'
export default function Error() {
return (
<div>
<h2 id="error">Something went wrong!</h2>
</div>
)
}
'use client' 'use client'
import { useActionState } from 'react'
import { accountForOverhead } from '../../account-for-overhead' import { accountForOverhead } from '../../account-for-overhead'
export default function Form({ action }) { export default function Form({ action }) {
const submit = (megaBytes) => const submit = (megaBytes) =>
action('a'.repeat(accountForOverhead(megaBytes))) action('a'.repeat(accountForOverhead(megaBytes)))
const [, submit1] = useActionState(() => submit(1), undefined)
const [, submit3] = useActionState(() => submit(3), undefined)
return ( return (
<> <>
<button id="size-1mb" onClick={() => submit(1)}> <button id="size-1mb" onClick={submit1}>
1mb 1mb
</button> </button>
<button id="size-2mb" onClick={() => submit(2)}> <button id="size-3mb" onClick={submit3}>
2mb 3mb
</button> </button>
</> </>
) )
......
'use client'
export default function Error() {
return (
<div>
<h2 id="error">Something went wrong!</h2>
</div>
)
}
...@@ -4,4 +4,7 @@ module.exports = { ...@@ -4,4 +4,7 @@ module.exports = {
logging: { logging: {
fetches: {}, fetches: {},
}, },
experimental: {
serverActions: { bodySizeLimit: '2mb' },
},
} }
import {
Request as PlaywrightRequest,
Response as PlaywrightResponse,
} from 'playwright'
import { inspect } from 'node:util'
import { Playwright } from '../browsers/playwright'
export type RequestMatcherObject = {
pathname: string
search?: string
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'HEAD' | 'OPTIONS'
}
export type RequestMatcher =
| RequestMatcherObject
| ((request: PlaywrightRequest) => Promise<boolean>)
/** Allows capturing responses to requests that happened as a result of running a callback. */
export function createRequestTracker(browser: Playwright) {
/** Run a callback and capture requests with the given `method` and `pathname`. */
async function captureResponse<T>(
action: () => Promise<T>,
{
request: requestMatcher,
timeoutMs = 5000,
}: {
request: RequestMatcher
timeoutMs?: number
}
): Promise<[result: T, captured: PlaywrightResponse]> {
const isMatchingRequest = async (request: PlaywrightRequest) => {
if (typeof requestMatcher === 'function') {
// This can be async to allow matching on things like `request.allHeaders()` which is a promise.
return await requestMatcher(request)
} else {
const url = new URL(request.url())
if (requestMatcher.pathname !== url.pathname) {
return false
}
if (requestMatcher.search !== undefined) {
if (url.search !== requestMatcher.search) {
return false
}
}
if (requestMatcher.method !== undefined) {
if (request.method() !== requestMatcher.method) {
return false
}
}
return true
}
}
const responseCtrl = promiseWithResolvers<PlaywrightResponse>()
let isSettled = false
let capturedRequest: PlaywrightRequest | undefined
const cleanups: (() => void)[] = []
// Make sure we clean up all event listeners and timers after we're done.
responseCtrl.promise.finally(() => {
isSettled = true
cleanups.forEach((cb) => cb())
})
// Listen for requests that match the criteria.
const onRequest = async (request: PlaywrightRequest) => {
if (!(await isMatchingRequest(request))) {
return
}
// If `capturedRequest` is already set, then we've got multiple requests that match the criteria.
// This is currently not supported, though if needed, we could extend the API
// to allow capturing multiple requests and returning them as an array.
if (capturedRequest) {
const criteriaDescription =
typeof requestMatcher === 'function'
? 'the specified criteria'
: inspect(requestMatcher)
return responseCtrl.reject(
new Error(
[
`Captured multiple requests that match ${criteriaDescription} during a \`captureResponse\` call:`,
...[capturedRequest, request].map(
(req) => ` - ${req.method} ${req.url}`
),
'This is currently not supported.',
].join('\n')
)
)
}
// We found a request that matches our criteria. Now we'll wait for a response.
capturedRequest = request
console.log(
`[request-tracker] request: ${request.method()} ${request.url()}` +
(['POST', 'PUT', 'PATCH'].includes(request.method())
? ` (content-type: ${request.headers()['content-type']})`
: '')
)
const onResponse = (response: PlaywrightResponse) => {
if (isSettled) {
return
}
if (response.request() === request) {
// We found a response to our request. We're done.
console.log(`[request-tracker] response: ${response.status()}`)
return responseCtrl.resolve(response)
}
}
browser.on('response', onResponse)
cleanups.push(() => browser.off('response', onResponse))
}
// Install the handler before running the action callback to avoid races.
browser.on('request', onRequest)
cleanups.push(() => browser.off('request', onRequest))
// Run the action callback. We expect this to result in requests being initiated.
// If this doesn't happen before the specified timeout, we'll error below.
const actionPromise = Promise.resolve().then(action)
const resultPromise = Promise.all([
actionPromise,
responseCtrl.promise,
] as const)
actionPromise.then(
() => {
// The action callback and the request it triggered can finish before this gets the chance to run.
// In that case, we don't need to install the timeout at all.
if (isSettled) {
return
}
// After the action callback resolves, start a timer.
// If we don't capture a request/response pair within that time limit, error.
const abortTimeoutId = setTimeout(() => {
if (isSettled) {
return
}
return responseCtrl.reject(
new Error(
capturedRequest === undefined
? `Did not intercept a request within ${timeoutMs}ms of the action callback finishing`
: `Did not intercept a response within ${timeoutMs}ms of the action callback finishing`
)
)
}, timeoutMs)
cleanups.push(() => clearTimeout(abortTimeoutId))
},
() => {
// If the action callback errored, we don't want to reject the response promise,
// because then jest would unnecessarily print both errors.
// We're returning a `Promise.all` that'll reject because `actionPromise` rejected,
// so we can just quietly resolve this with `null` -- it will never reach the caller anyway.
return responseCtrl.resolve(null!)
}
)
return resultPromise
}
return { captureResponse }
}
function promiseWithResolvers<T>() {
let resolve: (value: T) => void = undefined!
let reject: (error: unknown) => void = undefined!
const promise = new Promise<T>((_resolve, _reject) => {
resolve = _resolve
reject = _reject
})
return { promise, resolve, reject }
}
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