How to Prevent Open Redirect Vulnerabilities
Open redirect vulnerabilities occur when an authentication flow accepts untrusted user input to determine the post-login destination URL, enabling attackers to launch convincing phishing attacks or steal credentials. This article outlines the primary security checks and technical controls developers must implement to validate redirect destinations, enforce safe routing, and protect user sessions during authentication.
1. Enforce Relative Paths and Same-Origin Policies
Restricting redirects exclusively to the application's origin eliminates the risk of off-site redirection. Developers should verify that the redirect target is a strictly relative path.
- Single Leading Slash: Ensure the path begins with a
single forward slash (
/). - Block Protocol-Relative URLs: Disallow paths
beginning with double slashes (
//example.com), which browsers interpret as network-path references inheriting the current protocol. - Block Backslashes: Disallow backslashes
(
\), which some browsers and web servers normalize to forward slashes, bypassing simple string checks.
2. Implement Strict Domain Allowlisting
When redirection to external domains or subdomains is necessary, match the target against a strict, server-side allowlist.
- Exact Matching: Compare the parsed domain using
exact string equality rather than substring or regex matches (e.g.,
verifying
trusted.comrather than checking if the input containstrusted.com, which allows bypasses likeattacker-trusted.com). - Subdomain Validation: If allowing arbitrary
subdomains, anchor the check to prevent suffix manipulation (e.g.,
*.example.com).
3. Use Native URL Parsing Libraries
Manual parsing via regular expressions often fails due to complex URL
specifications. Always use standard, hardened URL parsing APIs provided
by the platform (such as Python’s urllib.parse, Node.js
new URL(), or Java's java.net.URI).
- Extract the hostname, scheme, and path explicitly using the parser.
- Restrict permissible schemes strictly to
httpandhttpsto block dangerous schemes likejavascript:,data:, orvbscript:.
4. Utilize Server-Side Indirect Identifiers
Instead of passing raw URLs through user-controllable parameters
(e.g., ?returnUrl=https://...), map permissible
destinations to server-side identifiers or keys.
- Assign static keys to common post-login landing locations (e.g.,
?next=dashboardor?next=profile). - For dynamic workflows, store the target URL in a server-side session, associate it with an ephemeral, unpredictable token, and pass only that token through the query string.
5. Cryptographically Sign Redirect Parameters
If raw URLs must be forwarded across unauthenticated states (such as across third-party identity providers in OAuth or SAML flows), tamper-proof the value.
- Generate a Hash-based Message Authentication Code (HMAC) of the redirect URL using a secure server-side secret.
- Transmit the HMAC alongside the URL (e.g.,
?returnUrl=...&signature=...). - Recompute and verify the signature before issuing the HTTP redirect response, rejecting any input where the signature fails validation.
- Utilize state parameters in OAuth 2.0 / OpenID Connect flows to bind the redirect securely to the user’s current session.
6. Implement Interstitial Warning Pages
If the application must redirect users to dynamic, user-provided external destinations where allowlisting is not possible, insert an intermediate confirmation page. The interstitial screen must explicitly display the full destination domain and require manual user confirmation before executing the redirect.