Skip to content

Commit

Permalink
bug: Fields truncated when submitting form using Server Actions (#59877)
Browse files Browse the repository at this point in the history
When using Server Actions with a form the fields are getting truncated
at 1MB because of `busboy`'s default `fieldSize` limit of 1MB.

This PR tries to solve #59277
however there is a mismatch about `fieldSize` and `bodySize`. I have
tried creating a PR for `busboy`
mscdex/busboy#351 to allow configuring a max
size for the entire body.

### TODO:

- [ ] Figure out if this is acceptable
- [ ] Throw error when `bodySizeLimit` is hit.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

Fixes #59277, closes #61462.

---------

Co-authored-by: Shu Ding <g@shud.in>
  • Loading branch information
TryingToImprove and shuding committed Mar 18, 2024
1 parent 107cfbf commit af5b31c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
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 @@ -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)
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')
})
}
}
)
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>
</>
)
}

0 comments on commit af5b31c

Please sign in to comment.