# loginpage.org — Full AI Agent Reference Canonical Base URL: https://loginpage.org ## Methodology - **Login Scanner**: Inspects public HTTPS login endpoints with SSRF-hardened outbound proxy isolation, evaluating security headers (HSTS, CSP frame-ancestors, X-Frame-Options), Set-Cookie flags (HttpOnly, Secure, SameSite), OAuth/redirect hygiene, and WebAuthn Passkeys / password-manager autofill ergonomics. - **Auth Cost & MAU Cliff Calculator**: SMS OTP estimate assumes 10% of MAU receive 1.5 US/EU SMS messages/month (MAU × 0.10 × 1.5 × carrier rate). ## Hardening Skills ### Better Auth + Next.js + PostgreSQL Hardening Guide (https://loginpage.org/skills/better-auth-nextjs-hardening) Stack: Next.js 15, Better Auth, PostgreSQL Tags: better-auth, nextjs, self-hosted, passkeys ## Why This Matters Self-hosting Better Auth gives you **$0 per-MAU pricing** and full ownership of your user tables in PostgreSQL, but you are responsible for transport security, origin validation, and brute-force protection. ## Hardening Checklist for AI Agents 1. **Pin `trustedOrigins` explicitly** — never reflect `req.headers.origin` dynamically. Include only exact production HTTPS origins. 2. **Require `BETTER_AUTH_SECRET` (>= 32 random bytes)** from environment variables; fail startup if missing. 3. **Force secure session cookies in production** (`useSecureCookies: process.env.NODE_ENV === "production"`, `SameSite: "lax"`, `HttpOnly: true`). 4. **Enable built-in rate limiting** on `/api/auth/sign-in/*`, `/api/auth/sign-up/*`, and `/api/auth/forget-password` backed by PostgreSQL or Redis. 5. **Validate `callbackURL` / `redirectTo`** against an allowlist of relative paths (`^/[^/]`) or exact `trustedOrigins` to prevent Open Redirects. 6. **Enable Passkeys (`passkey()` plugin)** with matching `rpID` and `origin`, and set `autoComplete="username webauthn"` on the login email field. ## Reference Configuration (`src/lib/auth.ts`) ```ts import { betterAuth } from "better-auth"; import { passkey } from "better-auth/plugins/passkey"; import { Pool } from "pg"; if (!process.env.BETTER_AUTH_SECRET || process.env.BETTER_AUTH_SECRET.length < 32) { throw new Error("BETTER_AUTH_SECRET must be at least 32 characters"); } export const auth = betterAuth({ database: new Pool({ connectionString: process.env.DATABASE_URL }), secret: process.env.BETTER_AUTH_SECRET, baseURL: process.env.NEXT_PUBLIC_APP_URL, trustedOrigins: ["https://app.example.com"], emailAndPassword: { enabled: true, minPasswordLength: 10, }, rateLimit: { enabled: true, window: 60, max: 10, storage: "database", }, advanced: { useSecureCookies: process.env.NODE_ENV === "production", defaultCookieAttributes: { httpOnly: true, sameSite: "lax", }, }, plugins: [ passkey({ rpID: "app.example.com", rpName: "Example App", origin: "https://app.example.com", }), ], }); ``` ## Verification Steps - Confirm `GET /api/auth/ok` returns `200` only over HTTPS in production. - Confirm `Set-Cookie` contains `HttpOnly; Secure; SameSite=Lax`. - Test `/api/auth/sign-in/email?callbackURL=https://evil.com` — verify it is rejected or stripped to `/`. ### OAuth Open Redirect, CSRF & Session Cookie Hardening (https://loginpage.org/skills/oauth-open-redirect-csrf) Stack: FastAPI, Next.js, Express, OAuth 2.0 Tags: open-redirect, csrf, cookies, headers ## Why This Matters Attackers abuse unvalidated `?next=`, `?redirect_to=`, and `?returnUrl=` parameters on `/login` to phish users after a legitimate OAuth or password login, or bypass cookie isolation when `HttpOnly`, `Secure`, and `SameSite` flags are missing. ## Hardening Checklist for AI Agents 1. **Strictly validate post-login redirects** — accept only relative paths starting with a single `/` and NOT `//` or `/\`, or match against an exact hostname allowlist. 2. **Enforce `__Host-` prefix for session cookies** in HTTPS production (`Secure; HttpOnly; SameSite=Lax; Path=/`, no `Domain` attribute). 3. **Send `Strict-Transport-Security: max-age=31536000; includeSubDomains`** over HTTPS. 4. **Block clickjacking** on `/login` with `Content-Security-Policy: frame-ancestors 'none'` AND `X-Frame-Options: DENY`. 5. **Add `X-Content-Type-Options: nosniff` and `Referrer-Policy: strict-origin-when-cross-origin`** so OAuth `state`/`code` query parameters never leak in `Referer` headers. ## Reference Python / TypeScript Safe Redirect Validator ```python from urllib.parse import urlparse def safe_redirect_target(raw_target: str | None, fallback: str = "/dashboard") -> str: if not raw_target: return fallback candidate = raw_target.strip() # Reject protocol-relative //evil.com, backslash tricks /\evil.com, and full URLs if not candidate.startswith("/") or candidate.startswith("//") or candidate.startswith("/\\"): return fallback parsed = urlparse(candidate) if parsed.scheme or parsed.netloc: return fallback return candidate ``` ```ts export function safeRedirectTarget(rawTarget: string | null | undefined, fallback = "/dashboard"): string { if (!rawTarget) return fallback; const candidate = rawTarget.trim(); if (!candidate.startsWith("/") || candidate.startsWith("//") || candidate.startsWith("/\\")) { return fallback; } try { const parsed = new URL(candidate, "https://placeholder.local"); if (parsed.origin !== "https://placeholder.local") return fallback; return `${parsed.pathname}${parsed.search}${parsed.hash}`; } catch { return fallback; } } ``` ## Verification Steps - Test `GET /login?next=https://example.invalid` and `GET /login?redirect_to=//example.invalid` — confirm `Location` never points to `example.invalid`. - Inspect `curl -I https://example.com/login` — verify `Strict-Transport-Security`, `X-Frame-Options`, `Content-Security-Policy: frame-ancestors`, and `X-Content-Type-Options: nosniff`. ### Passkeys Conditional UI & Password Manager Autofill Hygiene (https://loginpage.org/skills/passkeys-conditional-ui) Stack: WebAuthn, HTML, TypeScript Tags: passkeys, webauthn, autofill, ux ## Why This Matters Broken `