Skip to main content
This document outlines important security considerations and best practices when using the Auth0 Next.js SDK.

Cookies and Security

The SDK uses HTTP-only, secure cookies for session management with built-in protections against common attacks. All cookies are automatically configured with security flags:
boolean
default:"true"
Always enabled. Prevents client-side JavaScript from accessing the cookie, reducing the attack surface for XSS (Cross-Site Scripting) attacks.This flag cannot be disabled.
string
default:"Lax"
Set to Lax by default to help mitigate CSRF (Cross-Site Request Forgery) attacks.
  • Lax: Cookies sent with top-level navigation and same-site requests
  • Strict: Cookies only sent with same-site requests
  • None: Cookies sent with all requests (requires Secure flag)
Learn more: Browser Behavior Changes: What Developers Need to Know
boolean
Automatically set to true when APP_BASE_URL uses https://.Ensures cookies are only transmitted over HTTPS connections, preventing interception over insecure networks.Important: When using dynamic base URLs in production, the SDK enforces secure: true. Explicitly setting secure: false will throw InvalidConfigurationError.
Customize cookie settings via configuration or environment variables:
Environment variables:
Never disable HttpOnly. The SDK always sets httpOnly: true and does not allow it to be disabled.

Session Security

Session Encryption

All session data is encrypted using AES-256-GCM before being stored in cookies. Generate a strong secret:
Keep your AUTH0_SECRET secure:
  • Never commit to source control
  • Use different secrets for each environment
  • Rotate secrets periodically
  • Minimum 32 bytes (64 hex characters)

Rolling Sessions

The SDK uses rolling sessions by default, which automatically extends the session expiry on each request. Benefits:
  • Active users stay logged in
  • Idle sessions expire
  • Reduces re-authentication friction
Security consideration: Rolling sessions generate a Set-Cookie header on every request that touches the session. This means:
Never cache responses that read the session, even if the content appears safe to cache. The Set-Cookie header contains sensitive session data and must not be cached by CDNs or edge networks.

Session Duration

Configure session timeouts to balance security and user experience:
  • Rolling duration: How long the session lasts without activity
  • Absolute duration: Maximum session lifetime regardless of activity
Best practice: Set absoluteDuration to match your security requirements. High-security applications should use shorter durations (e.g., 1-4 hours).

Stateful Sessions

For enhanced security or large sessions, store session data server-side:
Benefits:
  • No 4KB cookie size limit
  • Server-side session revocation
  • Reduced client-side data exposure
See Database sessions for details.

Token Security

Access Token Storage

Access tokens are stored encrypted in the session cookie (or session store).
Never expose access tokens to the client unless absolutely necessary. Use server-side API routes to call external APIs:

Refresh Token Security

Refresh tokens are long-lived credentials that can obtain new access tokens. Best practices:
Only request refresh tokens when needed:
Configure in Auth0 Dashboard:
  1. Go to Applications > Your App > Advanced Settings
  2. Enable Refresh Token Rotation
  3. Enable Refresh Token Reuse Detection
This issues a new refresh token on each use and invalidates the old one, detecting token theft.
Configure in Auth0 Dashboard:
  1. Go to Applications > Your App > Advanced Settings
  2. Set Refresh Token Expiration based on security requirements
  3. Enable Inactivity Expiration to expire tokens after periods of inactivity

Token Refresh Buffer

Refresh tokens proactively to avoid expiration mid-request:

XSS Protection

Error Message Handling

Critical: OAuth errors may contain reflected user input via the error and error_description query parameters.Never render these values directly without escaping to prevent XSS attacks.
See the OWASP XSS Prevention Cheat Sheet for proper escaping techniques.

Content Security Policy

Implement CSP headers to mitigate XSS:

CSRF Protection

The SDK provides built-in CSRF protection through:
  1. SameSite cookies: Default Lax setting prevents CSRF attacks
  2. State parameter: Validated on OAuth callback
  3. Transaction cookies: Bind authentication state to the session

Custom CSRF Protection

For additional protection on custom routes:

Input Validation

Always validate user inputs, especially redirect URLs, to prevent open redirect vulnerabilities.

Safe Redirect Handling

beforeSessionSaved Hook Validation

DPoP (Demonstrating Proof-of-Possession)

DPoP binds access tokens to cryptographic key pairs, preventing token theft and replay attacks.

Enable DPoP

Benefits:
  • Prevents token theft (stolen tokens are useless without private key)
  • Prevents replay attacks (proof is bound to request)
  • Enhanced security for high-value transactions
Key management:
Protect DPoP private keys:
  • Store in environment variables or secrets manager
  • Never expose to client-side code
  • Rotate keys periodically
  • Use hardware security modules (HSM) for production
See DPoP Examples for details.

Caching Security

Critical: Never cache responses that require authentication or touch the session.
Many hosting providers (Vercel, Netlify) cache responses at the edge. Caching authenticated responses can:
  1. Expose session cookies via cached Set-Cookie headers
  2. Leak user data to other users
  3. Bypass authentication checks

What NOT to Cache

Never cache responses from:
  • auth0.getSession()
  • auth0.getAccessToken()
  • useUser() hook
  • Any route that checks authentication
  • Rolling session responses (contain Set-Cookie)

Safe Caching

Only cache:
  • Public, unauthenticated content
  • Static assets
  • API responses that don’t require authentication

Dynamic Base URLs

For preview environments (Vercel, Netlify), the SDK can infer the base URL from the request host.
Security note: The Host header is untrusted user input. Auth0’s Allowed Callback URLs act as the security boundary. If the inferred host is not registered in Auth0, the authorization request will be rejected.
Security enforcements:
  1. Secure cookies enforced: When using dynamic base URLs in production, secure: false throws InvalidConfigurationError
  2. Callback URL validation: Auth0 validates the callback URL against your registered URLs
  3. Protocol inference: HTTPS is assumed for security
Best practice: Use static APP_BASE_URL in production:

Logout Security

OIDC Logout

The SDK supports OIDC logout with optional id_token_hint:
Security tradeoff: See OIDC logout privacy configuration for details.

Backchannel Logout

Implement backchannel logout for server-initiated session termination:
  1. Configure backchannel logout URI in Auth0 Dashboard:
    • https://yourdomain.com/auth/backchannel-logout
  2. The SDK automatically handles logout token validation
  3. Sessions are terminated immediately
See Auth0 Back-Channel Logout for details.

Vulnerability Reporting

Do not report security vulnerabilities on the public GitHub issue tracker.
Please report security issues through Auth0’s Responsible Disclosure Program.

Security Checklist

  • Use strong AUTH0_SECRET (32+ bytes, hex-encoded)
  • Rotate secrets periodically
  • Never commit secrets to source control
  • Use different secrets per environment
  • Enable Refresh Token Rotation in Auth0 Dashboard
  • Store tokens server-side only
  • Request minimum required scopes
  • Use token refresh buffer
  • Implement token refresh error handling
  • Consider DPoP for high-security apps
  • Validate all redirect URLs
  • Escape OAuth error messages
  • Sanitize user inputs in hooks
  • Implement CSRF tokens for custom forms
  • Use allowlists for redirects
  • Never cache authenticated responses
  • Check hosting provider caching rules
  • Set appropriate Cache-Control headers
  • Verify Set-Cookie not cached
  • Register all callback URLs in Auth0 Dashboard
  • Enable Refresh Token Rotation
  • Configure appropriate token lifetimes
  • Enable MFA for sensitive operations
  • Review Auth0 logs regularly

Additional Resources