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
}
@@ -67,6 +67,65 @@ describe('safeNextPath', () => {
expect(safeNextPath('/ /evil.test')).toBe('/%20/evil.test')
expect(safeNextPath('/app/account?tab=x#s')).toBe('/app/account?tab=x#s')
})
// R4.12 (D3): the URL parser's own normalisation turns these into `//evil.test`.
it('refuses dot segments that normalise into a protocol-relative path', () => {
for (const raw of [
'/..//evil.test',
'/.//evil.test',
'/a/..//evil.test',
'/a/b/../..//evil.test',
'/..\\\\evil.test',
'/x/..\\/evil.test',
'/%2e%2e//evil.test',
'/%2E%2E//evil.test',
'/.%2e//evil.test',
'/a/%2e%2e//evil.test',
'/.\t.//evil.test',
'/..//evil.test/app?next=/app#x',
]) {
expect(safeNextPath(raw), raw).toBe('/app')
}
})
it('refuses external schemes and escapes in every spelling', () => {
for (const raw of [
'///evil.test',
'/\\/evil.test',
'\\\\evil.test',
'javascript:alert(1)',
'/javascript:alert(1)//',
'https:/evil.test',
'data:text/html,x',
'',
]) {
const result = safeNextPath(raw)
expect(result.startsWith('//'), raw).toBe(false)
expect(new URL(result, 'http://site.invalid').origin, raw).toBe('http://site.invalid')
}
expect(safeNextPath('///evil.test')).toBe('/app')
expect(safeNextPath('javascript:alert(1)')).toBe('/app')
})
it('keeps encoded slashes literal and legitimate internal paths unchanged', () => {
expect(safeNextPath('/%2F%2Fevil.test')).toBe('/%2F%2Fevil.test')
expect(safeNextPath('/%5C%5Cevil.test')).toBe('/%5C%5Cevil.test')
expect(safeNextPath('/app')).toBe('/app')
expect(safeNextPath('/editor')).toBe('/editor')
expect(safeNextPath('/app//account')).toBe('/app//account')
expect(safeNextPath('/app/../app/account')).toBe('/app/account')
expect(safeNextPath('/app/account?next=//evil.test')).toBe('/app/account?next=//evil.test')
expect(safeNextPath('/../app')).toBe('/app')
expect(safeNextPath('/..')).toBe('/')
expect(safeNextPath('/..//evil.test', '/fallback')).toBe('/fallback')
})
it('returns a value that is itself accepted unchanged (idempotent)', () => {
for (const raw of ['/app/account?tab=x#s', '/a/../b', '/%2e%2e/app', '/app//x']) {
const once = safeNextPath(raw)
expect(safeNextPath(once), raw).toBe(once)
}
})
})
describe('account validation', () => {
@@ -86,6 +86,20 @@ describe('LoginRoute', () => {
})
})
it('lands on the dashboard when next normalises into another host (R4.12)', async () => {
fake.auth.signInWithPassword.mockResolvedValue({
data: { session: fakeSession(), user: fakeSession().user },
error: null,
})
const user = userEvent.setup()
mount(`/login?next=${encodeURIComponent('/..//evil.test/app/account')}`)
await user.type(screen.getByLabelText(fr['auth.email']), 'alice@example.test')
await user.type(screen.getByLabelText(fr['auth.password']), 'CorrectHorse12')
await user.click(screen.getByRole('button', { name: fr['auth.login.submit'] }))
await waitFor(() => expect(screen.getByText('dashboard')).toBeInTheDocument())
expect(screen.queryByText('account')).not.toBeInTheDocument()
})
it('phrases a wrong password without leaking the server message', async () => {
fake.auth.signInWithPassword.mockResolvedValue({
data: { session: null, user: null },