3.16.1: `queryCollection(event, …)` fails with 413 "Request body too large" when the *incoming* request has a body over ~586 KB
Environment
@nuxt/content3.16.1 (3.16.0 is fine)- Nuxt 4.5.2, Nitro preset
cloudflare_module, D1 database - Reproduced both locally (
wrangler devon the built output) and on a deployed Worker
Description
A server route that handles a request with a large body (for example a multipart photo upload) and then calls queryCollection(event, …) now fails with:
FetchError: [POST] "/__nuxt_content/services/query?v=…": 413 Request body too large
The route's own code never rejects the request. The error comes from Content's internal query call.
Cause
fetchContent() in runtime/internal/api.js builds the internal request's headers from the incoming request:
const headers = event ? getRequestHeaders(event) : {};
headers["accept-encoding"] = void 0;
...
return await event.$fetch(url, { ...options, headers: { ...headers, ...options.headers } })
That copies the incoming request's content-length onto the internal POST /__nuxt_content/<collection>/query. 3.16.1 added a guard in runtime/api/query.post.js:
const MAX_BODY_BYTES = MAX_SQL_QUERY_LENGTH * 6 + JSON_WRAPPER_OVERHEAD; // 600016
const contentLength = Number(getHeader(event, "content-length") || 0);
if (Number.isFinite(contentLength) && contentLength > MAX_BODY_BYTES) {
throw createError({ statusCode: 413, statusMessage: "Request body too large" });
}
So the guard is measuring the size of the outer request (the upload), not the SQL query it is supposed to limit. Any handler that receives a body over 600,016 bytes and then queries a collection returns 413 to the client. 3.16.0 has no such guard, so this is a regression.
Reproduction
- The shape of the failing handler (my real route is an order endpoint that reads a multipart photo upload, then looks up a collection document):
export default defineEventHandler(async (event) => { await readMultipartFormData(event) return queryCollection(event, 'services').path('/services/x').first() }) - POST a multipart body larger than ~600 KB (e.g. a 700 KB file) to it.
- Result on 3.16.1: 413 "Request body too large" from
/__nuxt_content/services/query. Same request with a body under ~586 KB succeeds. On 3.16.0 both succeed.
Suggested fix
Don't forward body-describing headers on the internal request. In fetchContent, drop content-length and transfer-encoding (and content-type is already overridden) from the copied headers, so the guard sees the real size of the SQL payload. Alternatively, have the guard measure the parsed SQL (it already caps sql length separately) rather than trusting a header that can be inherited.
Workaround
Pin @nuxt/content to 3.16.0.
Notes
I searched the existing issues for "Request body too large" and found nothing matching.
Thanks for tracing this to the inherited content-length header, that made it easy to confirm on main ✅
I would like to pick this up.
The plan is to stop fetchContent() from forwarding body framing headers like content-length and transfer-encoding to the internal request, and leave the MAX_BODY_BYTES guard as it is.
I will add a unit test that checks the headers passed to the internal $fetch for both the query and the dump fetch.