diff --git a/packages/next/src/server/app-render/action-handler.ts b/packages/next/src/server/app-render/action-handler.ts index 7737ee045b814..60fd6fe6bbe64 100644 --- a/packages/next/src/server/app-render/action-handler.ts +++ b/packages/next/src/server/app-render/action-handler.ts @@ -506,8 +506,15 @@ export async function handleAction({ if (isMultipartAction) { if (isFetchAction) { + const readableLimit = serverActions?.bodySizeLimit ?? '1 MB' + const limit = require('next/dist/compiled/bytes').parse( + readableLimit + ) const busboy = require('busboy') - const bb = busboy({ headers: req.headers }) + const bb = busboy({ + headers: req.headers, + limits: { fieldSize: limit }, + }) req.pipe(bb) bound = await decodeReplyFromBusboy(bb, serverModuleMap) diff --git a/test/e2e/app-dir/actions/app-action-size-limit-invalid.test.ts b/test/e2e/app-dir/actions/app-action-size-limit-invalid.test.ts index a54b3b723e6ed..6056dde281211 100644 --- a/test/e2e/app-dir/actions/app-action-size-limit-invalid.test.ts +++ b/test/e2e/app-dir/actions/app-action-size-limit-invalid.test.ts @@ -117,6 +117,42 @@ createNextDescribe( : '' }, 'yes') }) + + it('should respect the size set in serverActions.bodySizeLimit when submitting form', async function () { + await next.patchFile( + 'next.config.js', + ` + module.exports = { + experimental: { + serverActions: { bodySizeLimit: '2mb' } + }, + } + ` + ) + await next.stop() + await next.build() + await next.start() + + const logs: string[] = [] + next.on('stdout', (log) => { + logs.push(log) + }) + next.on('stderr', (log) => { + logs.push(log) + }) + + const browser = await next.browser('/form') + await browser.elementByCss('#size-1mb').click() + + await check(() => { + return logs.some((log) => log.includes('size = 1048576')) ? 'yes' : '' + }, 'yes') + + await browser.elementByCss('#size-2mb').click() + await check(() => { + return logs.some((log) => log.includes('size = 2097152')) ? 'yes' : '' + }, 'yes') + }) } } ) diff --git a/test/e2e/app-dir/actions/app/form/page.js b/test/e2e/app-dir/actions/app/form/page.js new file mode 100644 index 0000000000000..7e9be67eecc19 --- /dev/null +++ b/test/e2e/app-dir/actions/app/form/page.js @@ -0,0 +1,42 @@ +async function action(formData) { + 'use server' + const payload = formData.get('payload').toString() + console.log('size =', payload.length) +} + +export default function Page() { + return ( + <> +
+ + +
+
+ + +
+
+ + +
+ + ) +}