Commit 3f6d40b1 authored by devjiwonchoi's avatar devjiwonchoi

Return 400 instead of 500 for unparseable Next-Router-State-Tree header

When a client sends an invalid or incompatible Next-Router-State-Tree
header (e.g. cross-version same-origin navigation from Next 14 to 16),
the server now returns 400 instead of 500. This is semantically correct
(client sent a bad header) and avoids CDN caching of 500 responses.
parent f98a3f11
import { DecodeError } from '../../shared/lib/utils'
import { parseAndValidateFlightRouterState } from './parse-and-validate-flight-router-state'
describe('parseAndValidateFlightRouterState', () => {
it('should return undefined when stateHeader is undefined', () => {
expect(parseAndValidateFlightRouterState(undefined)).toBeUndefined()
})
it('should parse a valid flight router state header', () => {
const validState = [['', 'b', 'c', null], {}]
const encoded = encodeURIComponent(JSON.stringify(validState))
const result = parseAndValidateFlightRouterState(encoded)
expect(result).toEqual(validState)
})
it('should throw DecodeError for multiple headers (array)', () => {
expect(() => parseAndValidateFlightRouterState(['a', 'b'])).toThrow(
DecodeError
)
})
it('should throw DecodeError when header is too large', () => {
const oversized = 'x'.repeat(20 * 2000 + 1)
expect(() => parseAndValidateFlightRouterState(oversized)).toThrow(
DecodeError
)
})
it('should throw DecodeError for invalid JSON', () => {
expect(() => parseAndValidateFlightRouterState('not-valid-json')).toThrow(
DecodeError
)
})
it('should throw DecodeError for valid JSON that fails schema validation', () => {
// Simulates v14 schema with boolean 5th element instead of number
const v14State = [['', 'b', 'c', null], {}, null, null, true]
const encoded = encodeURIComponent(JSON.stringify(v14State))
expect(() => parseAndValidateFlightRouterState(encoded)).toThrow(
DecodeError
)
})
})
import type { FlightRouterState } from '../../shared/lib/app-router-types' import type { FlightRouterState } from '../../shared/lib/app-router-types'
import { DecodeError } from '../../shared/lib/utils'
import { flightRouterStateSchema } from './types' import { flightRouterStateSchema } from './types'
import { assert } from 'next/dist/compiled/superstruct' import { assert } from 'next/dist/compiled/superstruct'
...@@ -18,7 +19,7 @@ export function parseAndValidateFlightRouterState( ...@@ -18,7 +19,7 @@ export function parseAndValidateFlightRouterState(
return undefined return undefined
} }
if (Array.isArray(stateHeader)) { if (Array.isArray(stateHeader)) {
throw new Error( throw new DecodeError(
'Multiple router state headers were sent. This is not allowed.' 'Multiple router state headers were sent. This is not allowed.'
) )
} }
...@@ -29,14 +30,17 @@ export function parseAndValidateFlightRouterState( ...@@ -29,14 +30,17 @@ export function parseAndValidateFlightRouterState(
// This is around 2,000 nested or parallel route segment states: // This is around 2,000 nested or parallel route segment states:
// '{"children":["",{}]}'.length === 20. // '{"children":["",{}]}'.length === 20.
if (stateHeader.length > 20 * 2000) { if (stateHeader.length > 20 * 2000) {
throw new Error('The router state header was too large.') throw new DecodeError('The router state header was too large.')
} }
try { try {
const state = JSON.parse(decodeURIComponent(stateHeader)) const state = JSON.parse(decodeURIComponent(stateHeader))
assert(state, flightRouterStateSchema) assert(state, flightRouterStateSchema)
return state return state
} catch { } catch (cause) {
throw new Error('The router state header was sent but could not be parsed.') throw new DecodeError(
'The router state header was sent but could not be parsed.',
{ cause }
)
} }
} }
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