Skip to curriculum
Jump to the editor

18.7. Keep return addresses on your own site

Medium
Topics
15–25 min

A sign-in page usually remembers where the user was going: /login?next=/orders/42. After signing in, the site sends them to next. If next is trusted as it is, anyone can send a link like /login?next=https://evil.example that signs the victim in on the real site and then drops them on a copy of it, ready to "confirm" their card. This is an open redirect. Checking that next starts with / is not enough: //evil.example, /\evil.example and /\t/evil.example all start with a slash and all leave the site.

Write safeReturnPath(value, siteOrigin, fallback) in return-path.js. It returns a path on your site, or the fallback.

Signature

JAVASCRIPT
function safeReturnPath(value, siteOrigin, fallback = "/") // returns a string
  • value: the untrusted address, usually the next query parameter. May be anything.
  • siteOrigin: your site, such as "https://shop.example" (a trailing slash or a path is ignored).
  • Returns a path (pathname + search + hash) on your site, or fallback.

Requirements

  1. A path on the site comes back with its query and fragment; relative paths resolve against the site's root (account gives /account).
  2. An absolute URL on the site comes back as just its path, without user name or password.
  3. The address is judged the way the browser reads it: backslashes count as slashes, tabs and newlines are removed, spaces are trimmed, host case and default ports do not matter.
  4. Anything that leaves the site gives the fallback: other hosts, protocol-relative //host, backslash and whitespace tricks, the user@host trick, another scheme or port on the same host, and javascript:, data: or blob: addresses.
  5. A result that would start with // gives the fallback, even when it resolves on the site (/.//evil.example).
  6. The result never contains a line break; encoded characters such as %2F stay encoded.
  7. A value that is not a non-blank string, or that is not a valid URL, gives the fallback. Nothing throws for a bad value.
  8. fallback defaults to "/". An invalid siteOrigin throws TypeError.
  9. Any accepted result, followed from any page of the site, stays on the site.

Examples

JAVASCRIPT
safeReturnPath("/orders/42?tab=items#list", "https://shop.example");  // "/orders/42?tab=items#list"
safeReturnPath("https://shop.example/cart", "https://shop.example");  // "/cart"
JAVASCRIPT
safeReturnPath("//evil.example", "https://shop.example");                  // "/"
safeReturnPath("/\\evil.example", "https://shop.example");                 // "/"
safeReturnPath("https://shop.example@evil.example", "https://shop.example"); // "/"
JAVASCRIPT
safeReturnPath("javascript:alert(1)", "https://shop.example", "/home");   // "/home"
safeReturnPath(undefined, "https://shop.example");                       // "/"

Environment

  • Plain JavaScript in a browser page; the tests call the global safeReturnPath from return-path.js.
  • URL is available and is the parser the browser uses for every address, so it is the right tool.
  • Out of scope: an allowlist of other trusted sites, and how the server sends the redirect.

Hints

  1. Which is easier to get right: reading the string to guess where the browser will go, or asking the browser's URL parser where it goes?
  2. The URL https://shop.example//evil.example is on your site. What does its path, used on its own, mean to a browser?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…