Prevent Open Redirects with Safe Destination Validation
An open redirect sends a browser to a destination taken from a request without adequate validation. Prefer fixed local paths or server-managed destination IDs; when external URLs are essential, validate parsed URL components against an explicit allowlist.
Why a trusted starting link can still be unsafe
A login flow may accept a returnUrl and redirect there after authentication. An attacker can alter that value while keeping the initial link on the legitimate service domain, then send the user to a lookalike page that asks for credentials again. The browser follows a Location response; the server is not necessarily fetching the URL. That makes this different from SSRF, where the server makes an outbound request, although both may involve untrusted URL input. OWASP: Unvalidated Redirects and Forwards, MITRE CWE-601
Choose the narrowest destination design
If the destination is known, use a fixed local route. If users must return to a requested page inside the app, use the framework’s official local-URL validation or redirect helper and fall back to a safe local page when validation fails. ASP.NET Core’s LocalRedirect rejects non-local URLs; IsLocalUrl can check one before redirecting. Other frameworks have their own helpers.
If a workflow needs a few external destinations, accept a server-managed ID such as destination=help-center and map it to a registered URL. This is safer than accepting an arbitrary address. Keep authorization checks even when IDs are enumerable.
Only when arbitrary external URLs are essential, parse them with a maintained URL parser compatible with the redirect API and browser interpretation. Compare scheme, canonical host, and effective port to an explicit allowlist; constrain the path when needed. Reject userinfo and ambiguous inputs, then redirect to the same validated URL. Do not validate with raw prefix/suffix string checks: trusted.example.attacker.test is a different host, and userinfo can make a URL visually confusing. OWASP URL validation guidance, Microsoft ASP.NET Core guidance
Verify the redirect behavior
Inventory redirect endpoints and every input source, including query parameters, forms, cookies, tokens, and request headers. Decide whether each flow needs a fixed local path, a destination ID, or a parsed external-URL allowlist. Preserve legitimate login, payment, invitation, and OAuth destinations; reject unapproved values or use a safe local fallback. Do not “repair” invalid input with ad hoc regular-expression substitutions.
In an isolated development or test environment, check expected destinations and rejected origins, host-confusion cases, ambiguous or encoded forms, and empty values. Inspect the actual Location response so it never names an unexpected external host. Log only what is needed to diagnose rejected input, with secrets and tokens removed. An exit-warning page that shows the real destination and requires a deliberate click can add friction, but it does not replace destination validation.
Sources: OWASP Cheat Sheet, Microsoft Learn, MITRE CWE-601.