Repository navigation
fix(uploads): clear the 10MB body cap and order the four size limits - #940
Conversation
`src/middleware.ts` matches `/api/:path*`, so Next buffers a copy of every API write's body and truncates it past `experimental.proxyClientMaxBodySize` — silently, and without failing the request. Payload then reads a cut-off multipart upload and throws "Unexpected end of form", which the admin shows as "Something went wrong". Every upload collection is affected: files over 10 MB fail, files under it work. Reproduced against a dev server with a 15 MB upload: before, the log carries `Request body exceeded 10MB for /api/meditations` followed by busboy's `Unexpected end of form` and a 500; after, neither appears. This changes no ceiling. Payload's own `requestSizeLimit`, left at its 50 MB default, still refuses anything larger — opaquely, which is #939. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01GWq2gctjj4FP1QMsTnSjcA
The spec supplied its own floor, so the cross-file invariant it exists for was pinned in one direction only: raising `upload.limits.fileSize` alone left it green while uploads truncated again. It now reads the limit out of `src/payload.config.ts` as source text, the shape `job-schedules.spec.ts` already uses to pin a config value without building the whole config. Verified by failing it three ways: no `experimental` block, a lowered cap, and a renamed `fileSize` key. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01GWq2gctjj4FP1QMsTnSjcA
The Edge Cache section explains what the `/api/**` matcher buys on reads and said nothing about what it costs on writes, which is how #938 stayed unexplained. It also names the real upload ceiling, since the config's own 100 MB is not it. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01GWq2gctjj4FP1QMsTnSjcA
|
🚅 Deployed to the SahajCloud-pr-940 environment in sahajcloud
1 service not affected by this PR
|
|
🤖 no session running recent dispatchesWritten by the sydevs dispatcher. Sessions never edit this comment. |
sydevs-bot
left a comment
There was a problem hiding this comment.
🧐 Adversarial review
The diff is the two config lines #938 asked for, plus a guard, two pointer comments and a doc section. Nothing is smuggled in, and the shape survives the obvious objection: narrowing the matcher does not help, because meditations is itself a cacheable slug, so the reporter's own path would still clone.
One blind spot has no cheap fix, so it is not a finding: the guard pins our config literal, not that Next honoured the key. middlewareClientMaxBodySize was already renamed once, and a key Next ignores fails open to 10 MB with lint, tests and the build all green. Read the build log on the next Next upgrade.
Findings
- should-fix — the spec's floor is
fileSize, which is not the limit Payload enforces on a request, so #939 can re-arm #938 while it stays green.tests/unit/next-config-body-limit.spec.ts:33 - consider —
DEPLOYMENT.mdrestates the mechanism the config comment now owns.DEPLOYMENT.md:73 - consider — the pre-auth memory bound rose 10×, for anyone, not just an admin. Exposure report only.
next.config.mjs:22
What was checked
- Diff against
origin/main(4 files, +58/-0). #938's three spec'd changes are all present;DEPLOYMENT.mdis the one addition beyond it. src/plugins/cache/middleware.tsreturns early on non-GET, so every write clone is waste — andpolicy.tslistsmeditations,songsandimagesas cacheable.api/[...slug]/route.tsis generated, "DO NOT MODIFY". Together: no matcher or route-handler alternative exists.- The spec:
SRCandsourceOfexist, the regex has exactly one match insrc/payload.config.ts(line 362), and it reads the wrapped default export, so a wrapper droppingexperimentalfails it. - Both Preview links: 200, titled "Creating - Meditation" and "Meditations".
- No contract surface touched.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
The spec's floor was `upload.limits.fileSize`, which is Payload's per-file limit, not what it enforces on a request. #939 could raise `requestSizeLimit` above the Next cap and re-arm #938 with every gate green: Next would truncate again while the spec compared the wrong number. `requestSizeLimit` was inherited rather than written down, so nothing could pin it. It is now set to the value Payload already defaults to, changing no ceiling, and the spec reads both limits out of the config and requires the cap to clear each. Failed three ways before keeping: `requestSizeLimit` at 200 MB with `fileSize` at 100 MB (the scenario above), the key removed, and the `experimental` block removed. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_017en36CmFy4kx7qPDnBLA4j
`next.config.mjs` owns the chain — matcher, clone, truncation, the cap. Repeating it here gave it a second home to drift from. The ⚠ stays: the real ceiling is in neither config comment. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_017en36CmFy4kx7qPDnBLA4j
|
Addressed all three review threads. Pushed Adopted — the spec's floor was the wrong limit. Adopted — Routed — the pre-auth memory bound. An exposure report, not a guard request, and it belongs to the ceiling: recorded on #939. Nothing in this PR. Gate: lint, typecheck (src + tests), 2341 unit ✓. Also ran 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
|
@sydevs-bot Revise this PR so that our explicit file size limits are brought down such that no intended limit is above 50MB |
sydevs-bot
left a comment
There was a problem hiding this comment.
🧐 Adversarial review
Second pass, on @Ardnived's request that no intended limit exceed 50 MB. The diff declares the opposite. requestSizeLimit (50 MB) is the only ceiling that can fire; the Next cap and limits.fileSize are both 100 MB, and FILE_SIZE_LIMITS.video is 500 MB. Three of the four numbers a reader meets cannot fire — the incoherence #939 was filed for. The ask joins this PR, so #939 should narrow to its other half.
⚠ Lowering them improves no error message — see the fileSize thread before claiming #939's AC 2.
Findings
- blocker —
limits.fileSizeis 100 MB inside a 50 MB request.src/payload.config.ts:370 - blocker — the Next cap is twice what Payload can accept.
next.config.mjs:23 - should-fix — the spec pins
cap >= limit, not the ordering, so an unreachable limit stays green.tests/unit/next-config-body-limit.spec.ts:48 - consider — the new config comment narrates the change.
src/payload.config.ts:361 - Outside the diff, the fourth declaration:
FILE_SIZE_LIMITS.video= 500 MB (src/plugins/storage/uploadValidation.ts:11), enforced inafterChange(cloudflareStreamAdapter.ts:125), after the request limit already refused the body.audio(50 MB) also breaks oncefileSizedrops below it.
What was checked
- Installed
[email protected],[email protected]. Both caps count the whole raw body and compare exclusively (body-streams.js:98,processMultipart.js:160), so a cap equal torequestSizeLimitloses nothing Payload accepts. proxyClientMaxBodySizeis the live key, no upper clamp (config.js:717-793).limitsdoes reach busboy (processMultipart.js:10,19).- Ran the new spec: 3 passed,
fileSizeunreachable. validateFileUploadruns only for images and videos;audioanddocumentare declared, never passed.- Both Preview links 200. No contract touched.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
Both checks compare the whole raw body exclusively — body-streams.js:98 truncates above the cap, processMultipart.js:160 refuses above requestSizeLimit — so 50 MB passes every request Payload admits and breaks no upload that works today. It also halves the copy an anonymous POST /api/* holds before authentication. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SEDaexsLpUDE7iWpfkJ1Mj
processMultipart.js:160 throws once a request passes requestSizeLimit (50 MB), and fileSize bounds one file inside that request, so 100 MB could never fire. 48 MB leaves room for the boundary, part headers and _payload, which makes it the limit that actually refuses a large file. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SEDaexsLpUDE7iWpfkJ1Mj
Three lines read only to someone who saw the diff, which docs/rules/code-comments.md rules out. What stays is the coupling and what each number means. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SEDaexsLpUDE7iWpfkJ1Mj
video declared 500 MB and audio 50 MB, both above the 50 MB a request can carry, and both checked in afterChange — after processMultipart has already refused the body. Neither could fire. image and document are already under the ceiling and are unchanged. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SEDaexsLpUDE7iWpfkJ1Mj
cap >= each limit held while fileSize sat at 100 MB inside a 50 MB request, so the spec was green against the defect it exists for. It now pins category <= fileSize <= requestSizeLimit <= cap, reads FILE_SIZE_LIMITS so the fourth declaration is covered, and refuses a duplicate declaration rather than reading the first one it finds. Failed four ways before keeping: fileSize at 100 MB, video at 500 MB, a cap below the request limit, and a second fileSize in the config. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SEDaexsLpUDE7iWpfkJ1Mj
The ⚠ named two of them and the wrong binding one. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SEDaexsLpUDE7iWpfkJ1Mj
|
@Ardnived — done. No declared limit is above 50 MB, and the spec now pins the ordering. Pushed Adopted, all four threads:
Plus the review's last bullet: ⚠ This improves no error message. Every limit still fails as an opaque 500 — that half of #939 stands. Gate: lint, typecheck, 2345 unit ✓, 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
#936 landed a second `experimental` literal on main while this branch carried its own. Both merged cleanly — neither hunk touches the other — and the later literal replaced the earlier one wholesale, so `proxyClientMaxBodySize` was gone from the config Next receives. `tests/unit/next-config-body-limit.spec.ts` read `undefined` and failed on the merge commit CI tests, though the branch alone stayed green. Lint sees nothing: `no-dupe-keys` does not fire on this file. The two keys are now one literal, and the ⚠ above it says why a second is never safe here: both flags fail open. An unset cap truncates every /api write at 10 MB. An unset Turbopack flag restores the corrupt store. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VgKpwYjNuqhVdCS4zby42F
`CollectionConfig.upload` is `boolean | UploadConfig`, and that type declares no `limits` (`payload/dist/uploads/types.d.ts:137`, payload 3.90.2). The parser options come from `payload.config.bodyParser` and `payload.config.upload` alone — `addDataAndFileToRequest.js:32` — and the collection's own `upload` is read separately, for client-upload receipts. So the comment #940 left here promised a mechanism Payload does not have, and #939's fourth acceptance criterion asks it be made true or removed. Point it at `FILE_SIZE_LIMITS` instead, which is what actually varies per category and is enforced in the adapter. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01DimcZBWZLUXbtR8axHGmyU
Summary
/api/*write body at 10 MB, so Payload read a cut-off upload and threw "Unexpected end of form".Change outline
The four declarations, and what each is now
Each must sit under the next, or it can never fire. The spec pins the whole chain.
FILE_SIZE_LIMITS.image/.documentvalidateFileUpload, inafterChangeFILE_SIZE_LIMITS.videocloudflareStreamAdapter.ts:125FILE_SIZE_LIMITS.audioupload.limits.fileSizeupload.requestSizeLimitprocessMultipart.js:160, whole requestexperimental.proxyClientMaxBodySizebody-streams.js:98, truncationFlow — what a 23 MB admin upload hits, in order
The middleware never touches a write, but Next clones the body before the handler runs, so the matcher alone is what truncates.
Only non-GET requests clone at all, and the buffer grows with the real body rather than being allocated at the cap (
next/dist/server/body-streams.js:92-109,next/dist/server/web/sandbox/sandbox.js:91-94). Cost is roughly twice the body per concurrent upload, released when the request ends — and the cap matchingrequestSizeLimitexactly halves what an anonymousPOST /api/*can hold before auth.Preview
The reviewer needs to upload an oversized audio file; no seeded document is required, because creating one is the test.
mainthis fails with "Something went wrong".previewUrl.seedis unset for this repo, so nothing was seeded. Both links are create/list routes that work on an empty database.Making a 14 MB mp3 from a file already on this branch
tests/files/audio-42s.mp3is 717 KB and git-tracked. MP3 frames concatenate, so twenty copies play as one file:Checked:
filereportsMPEG ADTS, layer III, v1, 128 kbps, 44.1 kHz.Test results
pnpm lint,pnpm typecheck,pnpm typecheck:tests,pnpm test:unit(2345 passed)tests/unit/next-config-body-limit.spec.ts(7 ✓);tests/int/collections-smoke.int.spec.ts✓ (19), which boots the Payload config the upload options are set onsrc/payload.config.tsgains comments and two upload options, neither of which is schemaThe spec was failed four ways before being kept:
fileSizeback at 100 MB,videoback at 500 MB, a cap below the request limit, and a secondfileSizedeclaration in the config.Reproduced end to end against a dev server, which is the evidence that matters here, since no unit test exercises Next's request path:
Dev-server log, before and after, same 15 MB upload
Without the fix:
With the fix, the same request:
403 is the correct answer for an unauthenticated request. The point is that neither the truncation warning nor the busboy error appears, so the body reached Payload whole.
Manual verification
Notes for reviewer
What a reviewer actually loses: files between 48 and 50 MB. Nothing else narrows.
requestSizeLimitstays at the 52428800 Payload already applied by default — confirmed asDEFAULT_REQUEST_SIZE_LIMITin payload 3.90.2 — so the request ceiling has not moved.fileSizeat 48 MB leaves room inside it for the multipart boundary, part headers and_payload, which is what makes it the limit that fires rather than a number that never could.The two limits above 50 MB were already unreachable.
limits.fileSizeat 100 MB sat inside a 50 MB request.FILE_SIZE_LIMITS.videoat 500 MB is checked inafterChange, afterprocessMultiparthas already refused the body. Lowering them changes no upload that works today; it makes the declarations honest.⚠ Lowering them improves no error message.
payload/dist/utilities/addDataAndFileToRequest.js:39rethrows Payload's 413 asnew APIError(message)with no status, whichAPIErrordefaults to 500 — and a 500 is notisPublic, so the message is suppressed. Every one of these limits still fails opaquely. That is #939, which this PR does not close. #938's premise that oversized bodies "already get Payload's own 413" is the thing #939 corrects.Review findings, and what I did with each
Second round — @Ardnived's ask that no intended limit exceed 50 MB (all adopted):
limits.fileSize100 MB inside a 50 MB request →50331648.ad6ca8752428800.a7b1915cap >= each limit, which stays green for an unreachable limit. It pins the ordering now,category <= fileSize <= requestSizeLimit <= cap, readsFILE_SIZE_LIMITS, and refuses a duplicate declaration.5528fbb1dfdb3fFILE_SIZE_LIMITS.video(500 MB) and.audio(50 MB, above the newfileSize).ce0cc23First round:
104857600, so it pinned only Next's half. It reads the limits out ofsrc/payload.config.tsas source text, the shapetests/unit/job-schedules.spec.tsalready uses.next.config.mjs.DEPLOYMENT.md's Edge Cache section explained what the/api/**matcher buys on reads and nothing about what it costs on writes.Dismissed:
zRouteHas(next/dist/server/config-schema.js:45-60) allowsheader/query/cookie/hostand no method, and path cannot stand in —POST /api/meditationsshares its path with the cacheableGET. The one header dodge would silently drop edge caching for any client sendingContent-Typeon a GET.src/plugins/cache/middleware.ts. That would be a third home for one fact. It is innext.config.mjs, whichDEPLOYMENT.mdpoints at.Security review: re-run on the
src/payload.config.tsandsrc/plugins/storage/paths. No findings. It confirmed the middleware never parses the cloned body (no new sink at the edge), thatuseTempFilesis unset so no pre-auth disk write exists, and that every limit change here is strictly more restrictive with no check skipped.Closes #938
Generated by Claude Code