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
@@ -1,6 +1,6 @@
|
||||
# CONTRACT — step-07 identity, auth, /app shell (whole step, for the fresh verifier)
|
||||
- date: 2026-10-11 | flow: ad-hoc step (user brief) | branch: feature/step-07-identity-app-shell
|
||||
- status: active
|
||||
- status: active (amended 2026-10-11 after verifier pass 1: criterion 3 strengthened, criterion 5 CHECK regex scoped to the AuthFailureKind union)
|
||||
- spec: docs/development/IDENTITY.md; user brief (below)
|
||||
|
||||
## REQUEST (user brief 2026-10-11, condensed — IMMUTABLE)
|
||||
@@ -13,14 +13,14 @@ Implement Step 7 keeping the current visual identity (logo, palette, cream/white
|
||||
2. Local tool routes unchanged and unauthenticated: HomeRoute, NewReportRoute, EditorRoute are rendered outside RequireAuth; their characterisation tests pass.
|
||||
CHECK: grep -n "LocalToolShell\|RequireAuth" apps/app/src/app/App.tsx | head; cd apps/app && corepack pnpm vitest run tests/unit/routes.HomeRoute.import.test.tsx tests/unit/routes.HomeRoute.archive.test.tsx tests/unit/routes.EditorRoute.test.tsx tests/unit/routes.NewReportRoute.test.tsx 2>&1 | tail -2
|
||||
EXPECT: the three local routes are inside LocalToolShell (path "*"), not under the /app RequireAuth element ; passed
|
||||
3. Guard: /app/* requires a session, redirects to /login?next=<same-origin path>; `next` never accepts an absolute or protocol-relative URL.
|
||||
CHECK: cd apps/app && corepack pnpm vitest run tests/unit/guards.RequireAuth.test.tsx tests/unit/auth.validation.test.ts 2>&1 | tail -2; grep -n "startsWith('//')" apps/app/src/auth/redirects.ts
|
||||
EXPECT: passed ; one match
|
||||
3. Guard: /app/* requires a session, redirects to /login?next=<same-origin path>; `next` never accepts an absolute, protocol-relative or parser-escaped (`/\host`, tab/newline) URL: the value is resolved against a sentinel origin and rebuilt from the parsed parts.
|
||||
CHECK: cd apps/app && corepack pnpm vitest run tests/unit/guards.RequireAuth.test.tsx tests/unit/auth.validation.test.ts 2>&1 | tail -2; grep -n "url.origin !== PROBE_ORIGIN" apps/app/src/auth/redirects.ts; node -e "const u=new URL('/\\\\evil.test','http://next.invalid');console.log(u.origin)"
|
||||
EXPECT: passed (incl. the backslash/tab cases) ; one match ; http://evil.test (proves the parser escape the check guards against)
|
||||
4. Auth flows call the client with the right inputs: sign-in (email, password), sign-up (metadata display_name + locale, emailRedirectTo ending in /auth/callback), reset (redirectTo /reset-password), updateUser({password}), signOut, signInWithOAuth(provider, redirectTo /auth/callback).
|
||||
CHECK: grep -n "emailRedirectTo\|RESET_PASSWORD_PATH\|AUTH_CALLBACK_PATH" apps/app/src/app/routes/auth/*.tsx apps/app/src/components/auth/OAuthButtons.tsx | wc -l; cd apps/app && corepack pnpm vitest run tests/unit/routes.LoginRoute.test.tsx tests/unit/routes.SignupRoute.test.tsx tests/unit/routes.AccountRoute.test.tsx 2>&1 | tail -2
|
||||
EXPECT: >= 4 ; passed
|
||||
5. Server errors are never shown raw: every GoTrue error goes through classifyAuthError → AuthFailure kind → `auth.failure.<kind>` key present in fr and en for every kind of the union.
|
||||
CHECK: grep -o "| '[a-z_]*'" packages/supabase/src/auth.ts | sed "s/| '//;s/'//" | sort -u > /tmp/kinds.txt; for k in $(cat /tmp/kinds.txt); do grep -q "'auth.failure.$k'" apps/app/src/i18n/fr.ts && grep -q "'auth.failure.$k'" apps/app/src/i18n/en.ts || echo MISSING $k; done
|
||||
CHECK: sed -n '/^export type AuthFailureKind =/,/^$/p' packages/supabase/src/auth.ts | grep -o "| '[a-z_]*'" | sed "s/| '//;s/'//" | sort -u > "$TMPDIR/kinds.txt"; for k in $(cat "$TMPDIR/kinds.txt"); do grep -q "'auth.failure.$k'" apps/app/src/i18n/fr.ts && grep -q "'auth.failure.$k'" apps/app/src/i18n/en.ts || echo MISSING $k; done
|
||||
EXPECT: no MISSING line (12 kinds)
|
||||
6. Email verification mandatory and password policy mirrored: config.toml enable_confirmations = true, minimum_password_length = 12, password_requirements lower_upper_letters_digits; client passwordSchema min 12 + the three classes.
|
||||
CHECK: grep -n "enable_confirmations = true\|minimum_password_length = 12\|lower_upper_letters_digits" supabase/config.toml | wc -l; grep -n "PASSWORD_MIN_LENGTH = 12\|\[a-z\]\|\[A-Z\]\|\[0-9\]" apps/app/src/auth/validation.ts | wc -l
|
||||
|
||||
@@ -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