mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 17:46:06 +00:00
fix(invitations): throttle unauthenticated invitation lookups and harden redirects
The invitation preview surfaces (/invitations/accept and /register?invitationToken=) are the
only unauthenticated reads of invitation data, and each render costs two database queries.
They are now rate limited before the lookup can touch the database: a generous per-IP bucket
that bounds enumeration across tokens, plus a tight per-IP+token bucket that stops repeated
probing of a single invitation. Tokens are hashed before they reach the rate_limits table.
A throttled lookup says so ("we couldn't check this invitation right now") instead of claiming
the invitation is invalid, and signed-in acceptance is not gated by it.
The callback sanitizer also checked only the origin, which is not enough: an attacker can
smuggle a host into the path of an otherwise same-origin URL — new URL('https://app//evil.com')
keeps our origin but yields a pathname of //evil.com, which navigation sinks resolve as
protocol-relative and follow off-site. Paths are now required to be rooted at a single slash,
and the login redirect re-checks at the sink.
getClientIp is split so server components that only have `await headers()` resolve the client
IP through the same trusted-proxy logic as route handlers.
This commit is contained in:
+17
-1
@@ -19,12 +19,28 @@ export function getSafeCallbackUrl(
|
||||
try {
|
||||
const parsed = new URL(value, baseOrigin);
|
||||
if (parsed.origin !== baseOrigin) return fallback;
|
||||
return `${parsed.pathname}${parsed.search}${parsed.hash}`;
|
||||
const path = `${parsed.pathname}${parsed.search}${parsed.hash}`;
|
||||
// The origin check alone is not enough: an attacker can smuggle their own host into
|
||||
// the path of an otherwise same-origin URL. `new URL('https://app.example.com//evil.com')`
|
||||
// has our origin but a pathname of `//evil.com`, which every navigation sink below
|
||||
// resolves as protocol-relative and follows off-site.
|
||||
return isSafeRelativePath(path) ? path : fallback;
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* True when a path is safe to hand to a navigation sink (`router.push`, `<Link href>`,
|
||||
* `NextResponse.redirect`): rooted at a single `/`, so it can only ever stay on this origin.
|
||||
*
|
||||
* `//evil.com` and `/\evil.com` are protocol-relative — browsers fill in the current
|
||||
* scheme and navigate to `evil.com`.
|
||||
*/
|
||||
export function isSafeRelativePath(value: string): boolean {
|
||||
return value.startsWith('/') && !value.startsWith('//') && !value.startsWith('/\\');
|
||||
}
|
||||
|
||||
/** True when a sanitized path points at the invitation acceptance route. */
|
||||
export function isInvitationCallbackUrl(path: string): boolean {
|
||||
return path.startsWith('/invitations/accept');
|
||||
|
||||
Reference in New Issue
Block a user