{"success":true,"name":"validate-redirect-targets-don-t-let-your-site-bounce-users-t","description":"Writing code that redirects users to a URL taken from a parameter","category":"ai-brimind","path":"ai-brimind/validate-redirect-targets-don-t-let-your-site-bounce-users-t/SKILL.md","file":"SKILL.md","content":"---\nname: validate-redirect-targets-don-t-let-your-site-bounce-users-t\ndescription: \"Writing code that redirects users to a URL taken from a parameter\"\n---\n\n# Validate Redirect Targets: Don't Let Your Site Bounce Users to Attackers\n\n## When to use\nA route redirects the user based on a value from the request — like ?next=, ?returnUrl=, or a redirect field after login or checkout.\n\n## Example request\nLogin redirects to whatever ?next= contains → phishers send victims a real link on your domain that lands on their fake site after login; targets must be validated (open redirect). Checking only that the URL 'contains' your domain → evil.com/yourdomain.com and yourdomain.com.evil.com pass naive string checks; parse the URL properly and compare exact hosts (weak validation). Allowing protocol-relative or javascript: targets → //evil.com and javascript: URLs slip past simple checks; allow only safe schemes and known destinations (scheme filtering). Emails with tracking redirects open to any target → your legit domain becomes a laundering service for malicious links; redirect endpoints need allowlists too (redirect abuse).\n\n## Instructions\nAct like a developer who knows an open redirect turns your trustworthy domain into a phishing tool — the victim clicks a link that genuinely starts on your site, which is exactly why it works. Wherever a redirect target comes from request data, refuse to forward blindly. Prefer the strictest option that fits: redirect only to a fixed set of known paths chosen server-side by a code (like ?dest=dashboard mapped internally), or accept only relative paths within your own app after normalizing them so tricks like protocol-relative //host, backslashes, or encoded characters cannot smuggle an external destination. If external destinations are truly required, parse the URL with the platform's URL parser — never substring checks — and compare the exact host against an allowlist, permitting only http and https schemes. Apply the same rule to every bouncer in the system: login next-parameters, logout returns, payment success URLs, and email click-tracking endpoints. On failure, fall back to a safe default page rather than erroring. Test with the classic payloads — an absolute external URL, //evil.com, a javascript: scheme, and a lookalike host — and confirm each lands on your fallback. Only ship once every redirect target is either internal-by-construction or allowlisted-by-exact-host, and explain how this stops your domain from lending its reputation to someone else's attack.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #582\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}