18.7. Keep return addresses on your own site
Topics
This question is in
18. URLs, storage and safe HTMLA 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
function safeReturnPath(value, siteOrigin, fallback = "/") // returns a stringvalue: the untrusted address, usually thenextquery 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, orfallback.
Requirements
- A path on the site comes back with its query and fragment; relative paths resolve against the site's root (
accountgives/account). - An absolute URL on the site comes back as just its path, without user name or password.
- 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.
- Anything that leaves the site gives the fallback: other hosts, protocol-relative
//host, backslash and whitespace tricks, theuser@hosttrick, another scheme or port on the same host, andjavascript:,data:orblob:addresses. - A result that would start with
//gives the fallback, even when it resolves on the site (/.//evil.example). - The result never contains a line break; encoded characters such as
%2Fstay encoded. - 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. fallbackdefaults to"/". An invalidsiteOriginthrowsTypeError.- Any accepted result, followed from any page of the site, stays on the site.
Examples
safeReturnPath("/orders/42?tab=items#list", "https://shop.example"); // "/orders/42?tab=items#list"
safeReturnPath("https://shop.example/cart", "https://shop.example"); // "/cart"safeReturnPath("//evil.example", "https://shop.example"); // "/"
safeReturnPath("/\\evil.example", "https://shop.example"); // "/"
safeReturnPath("https://shop.example@evil.example", "https://shop.example"); // "/"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
safeReturnPathfromreturn-path.js. URLis 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
- 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?
- The URL
https://shop.example//evil.exampleis 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