fix(app): safeNextPath resolves next= against a sentinel origin (closes the /\host and tab/newline open redirect); tests; contract amended
This commit is contained in:
1 parent
aeea1f2e9b
commit
1aed64b4ef
3 files changed
+29
-9
No files matched your search
@@ -7,11 +7,23 @@ export function absoluteUrl(path: string): string {
|
||||
return new URL(path, window.location.origin).toString()
|
||||
}
|
||||
|
||||
/** Sentinel origin: anything that resolves elsewhere is not a path. */
|
||||
const PROBE_ORIGIN = 'http://next.invalid'
|
||||
|
||||
/**
|
||||
* The `next` query value is only honoured when it is a same-origin path:
|
||||
* never a scheme, never a protocol-relative URL.
|
||||
* 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.
|
||||
*/
|
||||
export function safeNextPath(raw: string | null, fallback = APP_HOME_PATH) {
|
||||
if (!raw || !raw.startsWith('/') || raw.startsWith('//')) return fallback
|
||||
return raw
|
||||
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
|
||||
}
|
||||
@@ -59,6 +59,14 @@ describe('safeNextPath', () => {
|
||||
expect(safeNextPath('//evil.test')).toBe('/app')
|
||||
expect(safeNextPath('app')).toBe('/app')
|
||||
})
|
||||
|
||||
it('refuses the backslash and control-character escapes the URL parser accepts', () => {
|
||||
expect(safeNextPath('/\\evil.test')).toBe('/app')
|
||||
expect(safeNextPath('/\t/evil.test')).toBe('/app')
|
||||
expect(safeNextPath('/\n/evil.test/x')).toBe('/app')
|
||||
expect(safeNextPath('/ /evil.test')).toBe('/%20/evil.test')
|
||||
expect(safeNextPath('/app/account?tab=x#s')).toBe('/app/account?tab=x#s')
|
||||
})
|
||||
})
|
||||
|
||||
describe('account validation', () => {
|
||||
|
||||
Reference in new issue
Block a user