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

Validate the router state header on the server (#46925)

Ensure that the router tree state header is one single, valid value.

Fixes NEXT-136.
parent b3220fe3
...@@ -740,6 +740,22 @@ async function renderToString(element: React.ReactElement) { ...@@ -740,6 +740,22 @@ async function renderToString(element: React.ReactElement) {
}) })
} }
function parseFlightRouterState(stateHeader: string | string[] | undefined) {
if (typeof stateHeader === 'undefined') {
return undefined
}
if (Array.isArray(stateHeader)) {
throw new Error(
'Multiple router state headers were sent. This is not allowed.'
)
}
try {
return JSON.parse(stateHeader)
} catch (err) {
throw new Error('The router state header was sent but could not be parsed.')
}
}
function validateURL(url: string | undefined): string { function validateURL(url: string | undefined): string {
if (!url) { if (!url) {
throw new Error('Invalid request URL') throw new Error('Invalid request URL')
...@@ -835,24 +851,22 @@ export async function renderToHTMLOrFlight( ...@@ -835,24 +851,22 @@ export async function renderToHTMLOrFlight(
} }
// don't modify original query object // don't modify original query object
query = Object.assign({}, query) query = { ...query }
stripInternalQueries(query)
const isPrefetch = const isPrefetch =
req.headers[NEXT_ROUTER_PREFETCH.toLowerCase()] !== undefined req.headers[NEXT_ROUTER_PREFETCH.toLowerCase()] !== undefined
// TODO-APP: verify the tree is valid // TODO-APP: verify the tree is valid
// TODO-APP: verify query param is single value (not an array)
// TODO-APP: verify tree can't grow out of control // 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. * Router state provided from the client-side router. Used to handle rendering from the common layout down.
*/ */
let providedFlightRouterState: FlightRouterState = isFlight let providedFlightRouterState: FlightRouterState = isFlight
? req.headers[NEXT_ROUTER_STATE_TREE.toLowerCase()] ? parseFlightRouterState(
? JSON.parse( req.headers[NEXT_ROUTER_STATE_TREE.toLowerCase()]
req.headers[NEXT_ROUTER_STATE_TREE.toLowerCase()] as string
) )
: undefined : undefined
: undefined
/** /**
* The tree created in next-app-loader that holds component segments and modules * The tree created in next-app-loader that holds component segments and modules
...@@ -871,8 +885,6 @@ export async function renderToHTMLOrFlight( ...@@ -871,8 +885,6 @@ export async function renderToHTMLOrFlight(
const searchParamsProps = { searchParams: query } const searchParamsProps = { searchParams: query }
stripInternalQueries(query)
const LayoutRouter = const LayoutRouter =
ComponentMod.LayoutRouter as typeof import('../client/components/layout-router').default ComponentMod.LayoutRouter as typeof import('../client/components/layout-router').default
const RenderFromTemplateContext = const RenderFromTemplateContext =
......
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