Unverified Commit 7bcef687 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

upgrade to path-to-regexp@6.3.0 (#80123)

Upgrades `path-to-regexp` to address backing concerns from
`CVE-2024-45296`. As a result, this requires some additional
pre-processing on the paths we generate for interception routes, to
ensure we pass a compliant value into `path-to-regexp`.
parent 8b7f4c50
......@@ -291,7 +291,7 @@
"p-limit": "3.1.0",
"p-queue": "6.6.2",
"path-browserify": "1.0.1",
"path-to-regexp": "6.1.0",
"path-to-regexp": "6.3.0",
"picomatch": "4.0.1",
"postcss-flexbugs-fixes": "5.0.2",
"postcss-modules-extract-imports": "3.0.0",
......
import { pathToRegexp } from 'next/dist/compiled/path-to-regexp'
import { NEXT_URL } from '../client/components/app-router-headers'
import {
extractInterceptionRouteInformation,
isInterceptionRouteAppPath,
} from '../shared/lib/router/utils/interception-routes'
import type { Rewrite } from './load-custom-routes'
import { safePathToRegexp } from '../shared/lib/router/utils/route-match-utils'
// a function that converts normalised paths (e.g. /foo/[bar]/[baz]) to the format expected by pathToRegexp (e.g. /foo/:bar/:baz)
function toPathToRegexpPath(path: string): string {
......@@ -41,7 +41,7 @@ export function generateInterceptionRoutesRewrites(
// pathToRegexp returns a regex that matches the path, but we need to
// convert it to a string that can be used in a header value
// to the format that Next/the proxy expects
let interceptingRouteRegex = pathToRegexp(normalizedInterceptingRoute)
let interceptingRouteRegex = safePathToRegexp(normalizedInterceptingRoute)
.toString()
.slice(2, -3)
......
import type { Token } from 'next/dist/compiled/path-to-regexp'
/**
* Route pattern normalization utilities for path-to-regexp compatibility.
*
* path-to-regexp 6.3.0+ introduced stricter validation that rejects certain
* patterns commonly used in Next.js interception routes. This module provides
* normalization functions to make Next.js route patterns compatible with the
* updated library while preserving all functionality.
*/
/**
* Internal separator used to normalize adjacent parameter patterns.
* This unique marker is inserted between adjacent parameters and stripped out
* during parameter extraction to avoid conflicts with real URL content.
*/
const PARAM_SEPARATOR = '_NEXTSEP_'
/**
* Detects if a route pattern needs normalization for path-to-regexp compatibility.
*/
export function hasAdjacentParameterIssues(route: string): boolean {
if (typeof route !== 'string') return false
// Check for interception route markers followed immediately by parameters
// Pattern: /(.):param, /(..):param, /(...):param, /(.)(.):param etc.
// These patterns cause "Must have text between two parameters" errors
if (/\/\(\.{1,3}\):[^/\s]+/.test(route)) {
return true
}
// Check for basic adjacent parameters without separators
// Pattern: :param1:param2 (but not :param* or other URL patterns)
if (/:[a-zA-Z_][a-zA-Z0-9_]*:[a-zA-Z_][a-zA-Z0-9_]*/.test(route)) {
return true
}
return false
}
/**
* Normalizes route patterns that have adjacent parameters without text between them.
* Inserts a unique separator that can be safely stripped out later.
*/
export function normalizeAdjacentParameters(route: string): string {
let normalized = route
// Handle interception route patterns: (.):param -> (.)_NEXTSEP_:param
normalized = normalized.replace(
/(\([^)]*\)):([^/\s]+)/g,
`$1${PARAM_SEPARATOR}:$2`
)
// Handle other adjacent parameter patterns: :param1:param2 -> :param1_NEXTSEP_:param2
normalized = normalized.replace(/:([^:/\s)]+)(?=:)/g, `:$1${PARAM_SEPARATOR}`)
return normalized
}
/**
* Normalizes tokens that have repeating modifiers (* or +) but empty prefix and suffix.
*
* path-to-regexp 6.3.0+ introduced validation that throws:
* "Can not repeat without prefix/suffix"
*
* This occurs when a token has modifier: '*' or '+' with both prefix: '' and suffix: ''
*/
export function normalizeTokensForRegexp(tokens: Token[]): Token[] {
return tokens.map((token) => {
// Token union type: Token = string | TokenObject
// Literal path segments are strings, parameters/wildcards are objects
if (
typeof token === 'object' &&
token !== null &&
// Not all token objects have 'modifier' property (e.g., simple text tokens)
'modifier' in token &&
// Only repeating modifiers (* or +) cause the validation error
// Other modifiers like '?' (optional) are fine
(token.modifier === '*' || token.modifier === '+') &&
// Token objects can have different shapes depending on route pattern
'prefix' in token &&
'suffix' in token &&
// Both prefix and suffix must be empty strings
// This is what causes the validation error in path-to-regexp
token.prefix === '' &&
token.suffix === ''
) {
// Add minimal prefix to satisfy path-to-regexp validation
// We use '/' as it's the most common path delimiter and won't break route matching
// The prefix gets used in regex generation but doesn't affect parameter extraction
return {
...token,
prefix: '/',
}
}
return token
})
}
/**
* Strips normalization separators from extracted route parameters.
* Used by both server and client code to clean up parameters after route matching.
*/
export function stripParameterSeparators(
params: Record<string, any>
): Record<string, any> {
const cleaned: Record<string, any> = {}
for (const [key, value] of Object.entries(params)) {
if (typeof value === 'string') {
// Remove the separator if it appears at the start of parameter values
cleaned[key] = value.replace(new RegExp(`^${PARAM_SEPARATOR}`), '')
} else if (Array.isArray(value)) {
// Handle array parameters (from repeated route segments)
cleaned[key] = value.map((item) =>
typeof item === 'string'
? item.replace(new RegExp(`^${PARAM_SEPARATOR}`), '')
: item
)
} else {
cleaned[key] = value
}
}
return cleaned
}
......@@ -2,6 +2,7 @@ import type { Token } from 'next/dist/compiled/path-to-regexp'
import { parse, tokensToRegexp } from 'next/dist/compiled/path-to-regexp'
import { parse as parseURL } from 'url'
import isError from './is-error'
import { normalizeTokensForRegexp } from './route-pattern-normalizer'
interface ParseResult {
error?: any
......@@ -34,6 +35,22 @@ function reportError({ route, parsedPath }: ParseResult, err: any) {
}
}
/**
* Safe wrapper around tokensToRegexp that handles path-to-regexp 6.3.0+ validation errors.
*/
function safeTokensToRegexp(tokens: Token[]): RegExp {
try {
return tokensToRegexp(tokens)
} catch (error) {
if (isError(error)) {
// Try to normalize tokens with repeating modifiers but no prefix/suffix
const normalizedTokens = normalizeTokensForRegexp(tokens)
return tokensToRegexp(normalizedTokens)
}
throw error
}
}
/**
* Attempts to parse a given route with `path-to-regexp` and returns an object
* with the result. Whenever an error happens on parse, it will print an error
......@@ -55,7 +72,11 @@ export function tryToParsePath(
}
result.tokens = parse(result.parsedPath)
result.regexStr = tokensToRegexp(result.tokens).source
// Use safe wrapper instead of proactive detection
if (result.tokens) {
result.regexStr = safeTokensToRegexp(result.tokens).source
}
} catch (err) {
reportError(result, err)
result.error = err
......
import type { Key } from 'next/dist/compiled/path-to-regexp'
import { pathToRegexp } from 'next/dist/compiled/path-to-regexp'
import { regexpToFunction } from 'next/dist/compiled/path-to-regexp'
import { pathToRegexp } from 'next/dist/compiled/path-to-regexp'
interface Options {
/**
......
......@@ -4,7 +4,6 @@ import type { NextParsedUrlQuery } from '../../../../server/request-meta'
import type { RouteHas } from '../../../../lib/load-custom-routes'
import type { BaseNextRequest } from '../../../../server/base-http'
import { compile, pathToRegexp } from 'next/dist/compiled/path-to-regexp'
import { escapeStringRegexp } from '../../escape-regexp'
import { parseUrl } from './parse-url'
import {
......@@ -13,6 +12,7 @@ import {
} from './interception-routes'
import { getCookieParser } from '../../../../server/api-utils/get-cookie-parser'
import type { Params } from '../../../../server/request/params'
import { safePathToRegexp, safeCompile } from './route-match-utils'
/**
* Ensure only a-zA-Z are used for param names for proper interpolating
......@@ -156,7 +156,7 @@ export function compileNonPath(value: string, params: Params): string {
// the value needs to start with a forward-slash to be compiled
// correctly
return compile(`/${value}`, { validate: false })(params).slice(1)
return safeCompile(`/${value}`, { validate: false })(params).slice(1)
}
export function parseDestination(args: {
......@@ -222,20 +222,20 @@ export function prepareDestination(args: {
const destParams: (string | number)[] = []
const destPathParamKeys: Key[] = []
pathToRegexp(destPath, destPathParamKeys)
safePathToRegexp(destPath, destPathParamKeys)
for (const key of destPathParamKeys) {
destParams.push(key.name)
}
if (destHostname) {
const destHostnameParamKeys: Key[] = []
pathToRegexp(destHostname, destHostnameParamKeys)
safePathToRegexp(destHostname, destHostnameParamKeys)
for (const key of destHostnameParamKeys) {
destParams.push(key.name)
}
}
const destPathCompiler = compile(
const destPathCompiler = safeCompile(
destPath,
// we don't validate while compiling the destination since we should
// have already validated before we got to this point and validating
......@@ -248,7 +248,7 @@ export function prepareDestination(args: {
let destHostnameCompiler
if (destHostname) {
destHostnameCompiler = compile(destHostname, { validate: false })
destHostnameCompiler = safeCompile(destHostname, { validate: false })
}
// update any params in query values
......
/**
* Client-safe utilities for route matching that don't import server-side
* utilities to avoid bundling issues with Turbopack
*/
import type {
Key,
TokensToRegexpOptions,
ParseOptions,
TokensToFunctionOptions,
} from 'next/dist/compiled/path-to-regexp'
import {
pathToRegexp,
compile,
regexpToFunction,
} from 'next/dist/compiled/path-to-regexp'
import {
hasAdjacentParameterIssues,
normalizeAdjacentParameters,
stripParameterSeparators,
} from '../../../../lib/route-pattern-normalizer'
/**
* Client-safe wrapper around pathToRegexp that handles path-to-regexp 6.3.0+ validation errors.
* This includes both "Can not repeat without prefix/suffix" and "Must have text between parameters" errors.
*/
export function safePathToRegexp(
route: string | RegExp | Array<string | RegExp>,
keys?: Key[],
options?: TokensToRegexpOptions & ParseOptions
): RegExp {
if (typeof route !== 'string') {
return pathToRegexp(route, keys, options)
}
// Check if normalization is needed and cache the result
const needsNormalization = hasAdjacentParameterIssues(route)
const routeToUse = needsNormalization
? normalizeAdjacentParameters(route)
: route
try {
return pathToRegexp(routeToUse, keys, options)
} catch (error) {
// Only try normalization if we haven't already normalized
if (!needsNormalization) {
try {
const normalizedRoute = normalizeAdjacentParameters(route)
return pathToRegexp(normalizedRoute, keys, options)
} catch (retryError) {
// If that doesn't work, fall back to original error
throw error
}
}
throw error
}
}
/**
* Client-safe wrapper around compile that handles path-to-regexp 6.3.0+ validation errors.
* No server-side error reporting to avoid bundling issues.
*/
export function safeCompile(
route: string,
options?: TokensToFunctionOptions & ParseOptions
) {
// Check if normalization is needed and cache the result
const needsNormalization = hasAdjacentParameterIssues(route)
const routeToUse = needsNormalization
? normalizeAdjacentParameters(route)
: route
try {
return compile(routeToUse, options)
} catch (error) {
// Only try normalization if we haven't already normalized
if (!needsNormalization) {
try {
const normalizedRoute = normalizeAdjacentParameters(route)
return compile(normalizedRoute, options)
} catch (retryError) {
// If that doesn't work, fall back to original error
throw error
}
}
throw error
}
}
/**
* Client-safe wrapper around regexpToFunction that automatically cleans parameters.
*/
export function safeRegexpToFunction<
T extends Record<string, any> = Record<string, any>,
>(regexp: RegExp, keys?: Key[]): (pathname: string) => { params: T } | false {
const originalMatcher = regexpToFunction<T>(regexp, keys || [])
return (pathname: string) => {
const result = originalMatcher(pathname)
if (!result) return false
// Clean parameters before returning
return {
...result,
params: stripParameterSeparators(result.params as any) as T,
}
}
}
/**
* Safe wrapper for route matcher functions that automatically cleans parameters.
* This is client-safe and doesn't import path-to-regexp.
*/
export function safeRouteMatcher<T extends Record<string, any>>(
matcherFn: (pathname: string) => false | T
): (pathname: string) => false | T {
return (pathname: string) => {
const result = matcherFn(pathname)
if (!result) return false
// Clean parameters before returning
return stripParameterSeparators(result) as T
}
}
import type { Group } from './route-regex'
import { DecodeError } from '../../utils'
import type { Params } from '../../../../server/request/params'
import { safeRouteMatcher } from './route-match-utils'
export interface RouteMatchFn {
(pathname: string): false | Params
......@@ -17,7 +18,7 @@ export function getRouteMatcher({
re,
groups,
}: RouteMatcherOptions): RouteMatchFn {
return (pathname: string) => {
const rawMatcher = (pathname: string) => {
const routeMatch = re.exec(pathname)
if (!routeMatch) return false
......@@ -43,4 +44,7 @@ export function getRouteMatcher({
return params
}
// Wrap with safe matcher to handle parameter cleaning
return safeRouteMatcher(rawMatcher)
}
......@@ -12,7 +12,6 @@ import type {
import type { BuildManifest } from '../../../server/get-page-files'
import type { AppBuildManifest } from '../../../build/webpack/plugins/app-build-manifest-plugin'
import type { PagesManifest } from '../../../build/webpack/plugins/pages-manifest-plugin'
import { pathToRegexp } from 'next/dist/compiled/path-to-regexp'
import type { ActionManifest } from '../../../build/webpack/plugins/flight-client-entry-plugin'
import type { NextFontManifest } from '../../../build/webpack/plugins/next-font-manifest-plugin'
import type { REACT_LOADABLE_MANIFEST } from '../constants'
......@@ -54,6 +53,7 @@ import {
removeRouteSuffix,
} from '../../../server/dev/turbopack-utils'
import { tryToParsePath } from '../../../lib/try-to-parse-path'
import { safePathToRegexp } from '../router/utils/route-match-utils'
import type { Entrypoints } from '../../../build/swc/types'
interface InstrumentationDefinition {
......@@ -715,7 +715,7 @@ export class TurbopackManifestLoader {
)) {
for (const matcher of fun.matchers) {
if (!matcher.regexp) {
matcher.regexp = pathToRegexp(matcher.originalSource, [], {
matcher.regexp = safePathToRegexp(matcher.originalSource, [], {
delimiter: '/',
sensitive: false,
strict: true,
......
......@@ -1430,8 +1430,8 @@ importers:
specifier: 1.0.1
version: 1.0.1
path-to-regexp:
specifier: 6.1.0
version: 6.1.0
specifier: 6.3.0
version: 6.3.0
picomatch:
specifier: 4.0.1
version: 4.0.1
......@@ -12938,11 +12938,8 @@ packages:
path-to-regexp@3.3.0:
resolution: {integrity: sha512-qyCH421YQPS2WFDxDjftfc1ZR5WKQzVzqsp4n9M2kQhVOo/ByahFoUNJfl58kOcEGfQ//7weFTDhm+ss8Ecxgw==}
path-to-regexp@6.1.0:
resolution: {integrity: sha512-h9DqehX3zZZDCEm+xbfU0ZmwCGFCAAraPJWMXJ4+v32NjZJilVg3k1TcKsRgIb8IQ/izZSaydDc1OhJCZvs2Dw==}
path-to-regexp@6.2.1:
resolution: {integrity: sha512-JLyh7xT1kizaEvcaXOQwOc2/Yhw6KZOvPf1S8401UyLk86CU79LN3vl7ztXGm/pZ+YjoyAJ4rxmHwbkBXJX+yw==}
path-to-regexp@6.3.0:
resolution: {integrity: sha512-Yhpw4T9C6hPpgPeA28us07OJeqZ5EzQTkbfwuhsUg0c237RomFoETJgmp2sa3F/41gfLE6G5cqcYwznmeEeOlQ==}
path-type@1.1.0:
resolution: {integrity: sha512-S4eENJz1pkiQn9Znv33Q+deTOKmbl+jj1Fl+qiP/vYezj+S8x+J3Uo0ISrx/QoEvIlOaDWJhPaRd1flJ9HXZqg==}
......@@ -21695,7 +21692,7 @@ snapshots:
'@types/path-to-regexp@1.7.0':
dependencies:
path-to-regexp: 6.1.0
path-to-regexp: 6.3.0
'@types/picomatch@2.3.3': {}
......@@ -24578,7 +24575,7 @@ snapshots:
msgpack-lite: 0.1.26
node-abort-controller: 3.1.1
opentracing: 0.14.7
path-to-regexp: 0.1.7
path-to-regexp: 0.1.12
protobufjs: 7.2.4
retry: 0.13.1
semver: 7.6.3
......@@ -30282,7 +30279,7 @@ snapshots:
headers-polyfill: 4.0.3
is-node-process: 1.2.0
outvariant: 1.4.2
path-to-regexp: 6.2.1
path-to-regexp: 6.3.0
strict-event-emitter: 0.5.1
type-fest: 4.18.3
yargs: 17.7.2
......@@ -31175,9 +31172,7 @@ snapshots:
path-to-regexp@3.3.0: {}
path-to-regexp@6.1.0: {}
path-to-regexp@6.2.1: {}
path-to-regexp@6.3.0: {}
path-type@1.1.0:
dependencies:
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