Unverified Commit cbc511ba authored by Shu Ding's avatar Shu Ding Committed by GitHub

Verify router state query params can't grow out of control (#47094)

Not certainly sure about the reasonable limitation of this header. For example https://app-dir.vercel.app/context/books sends a state query of size ~110, consider an app with 100x more complicated nested, parallel and named segments, we still have a 3x extra room which should be enough.

Fixes NEXT-137.
parent cb729c10
......@@ -782,6 +782,16 @@ function parseAndValidateFlightRouterState(
'Multiple router state headers were sent. This is not allowed.'
)
}
// We limit the size of the router state header to ~40kb. This is to prevent
// a malicious user from sending a very large header and slowing down the
// resolving of the router state.
// This is around 2,000 nested or parallel route segment states:
// '{"children":["",{}]}'.length === 20.
if (stateHeader.length > 20 * 2000) {
throw new Error('The router state header was too large.')
}
try {
return flightRouterStateSchema.parse(JSON.parse(stateHeader))
} catch {
......@@ -885,7 +895,6 @@ export async function renderToHTMLOrFlight(
const isPrefetch =
req.headers[NEXT_ROUTER_PREFETCH.toLowerCase()] !== undefined
// TODO-APP: verify tree can't grow out of control
/**
* Router state provided from the client-side router. Used to handle rendering from the common layout down.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment