Skip to content
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

bug: Fields truncated when submitting form using Server Actions #59877

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 8 additions & 1 deletion packages/next/src/server/app-render/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,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 },
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unsure what to do here since busboy is pr. field whereas next is for the whole body.

})
req.pipe(bb)

bound = await decodeReplyFromBusboy(bb, serverModuleMap)
Expand Down
36 changes: 36 additions & 0 deletions test/e2e/app-dir/actions/app-action-size-limit-invalid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Comment on lines +151 to +154
Copy link
Member

Choose a reason for hiding this comment

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

How about the 3mb one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When clicking the 3mb I think it said size = 2097152 since it is truncating the payload..

I guess it should throw instead, but busboy does not support that.

})
}
}
)
42 changes: 42 additions & 0 deletions test/e2e/app-dir/actions/app/form/page.js
Original file line number Diff line number Diff line change
@@ -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 (
<>
<form action={action}>
<input
type="hidden"
name="payload"
value={'a'.repeat(1024 * 1024 * 1)}
/>
<button type="submit" id="size-1mb">
SUBMIT 1mb
</button>
</form>
<form action={action}>
<input
type="hidden"
name="payload"
value={'a'.repeat(1024 * 1024 * 2)}
/>
<button type="submit" id="size-2mb">
SUBMIT 2mb
</button>
</form>
<form action={action}>
<input
type="hidden"
name="payload"
value={'a'.repeat(1024 * 1024 * 3)}
/>
<button type="submit" id="size-3mb">
SUBMIT 3mb
</button>
</form>
</>
)
}