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

Validate `req.url` in renderer (#46923)

It's possible that the request is missing or having an invalid URL, and
got passed to the renderer.

Fixes NEXT-139.
parent 02eb34db
...@@ -740,6 +740,18 @@ async function renderToString(element: React.ReactElement) { ...@@ -740,6 +740,18 @@ async function renderToString(element: React.ReactElement) {
}) })
} }
function validateURL(url: string | undefined): string {
if (!url) {
throw new Error('Invalid request URL')
}
try {
new URL(url, 'http://n')
return url
} catch {
throw new Error('Invalid request URL')
}
}
export async function renderToHTMLOrFlight( export async function renderToHTMLOrFlight(
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse, res: ServerResponse,
...@@ -1750,8 +1762,7 @@ export async function renderToHTMLOrFlight( ...@@ -1750,8 +1762,7 @@ export async function renderToHTMLOrFlight(
Uint8Array Uint8Array
> = new TransformStream() > = new TransformStream()
// TODO-APP: validate req.url as it gets passed to render. const initialCanonicalUrl = validateURL(req.url)
const initialCanonicalUrl = req.url!
// Get the nonce from the incoming request if it has one. // Get the nonce from the incoming request if it has one.
const csp = req.headers['content-security-policy'] const csp = req.headers['content-security-policy']
......
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