fix(app): safeNextPath refuses a next value that normalises into //host (/..//, /%2e%2e//); dot-segment, scheme, encoding and consumer tests

This commit is contained in:
bchanot committed 2026-10-21 18:15:08 +02:00
1 parent ff122d1924
commit 51a5a80192
3 files changed
+90 -8

No files matched your search

+17 -8
View File
@@ -10,20 +10,29 @@ export function absoluteUrl(path: string): string {
/** Sentinel origin: anything that resolves elsewhere is not a path. */
const PROBE_ORIGIN = 'http://next.invalid'
/** The value parsed against the sentinel origin, or null when it escapes it. */
function probe(value: string): URL | null {
try {
const url = new URL(value, PROBE_ORIGIN)
return url.origin === PROBE_ORIGIN ? url : null
} catch {
return null
}
}
/**
* The `next` query value is only honoured when it is a same-origin path.
* Resolving it against a sentinel origin catches every escape the URL
* parser knows (scheme, `//host`, `/\host`, tab/newline-stripped forms);
* the returned value is rebuilt from the parsed parts, never the raw string.
* Normalisation can itself produce `//host` (`/..//host`, `/%2e%2e//host`),
* so the rebuilt value must survive the same test as an internal path.
*/
export function safeNextPath(raw: string | null, fallback = APP_HOME_PATH) {
if (!raw || !raw.startsWith('/')) return fallback
let url: URL
try {
url = new URL(raw, PROBE_ORIGIN)
} catch {
return fallback
}
if (url.origin !== PROBE_ORIGIN) return fallback
return url.pathname + url.search + url.hash
const url = probe(raw)
if (!url) return fallback
const path = url.pathname + url.search + url.hash
if (path.startsWith('//') || !probe(path)) return fallback
return path
}