Lesson 1 of 2
Open redirects
Listen to an AI summary of Open redirects
What is a redirect
A browser sends a server a request, and the server answers with a message called a response. A redirect is a response that tells the browser to go to a different URL.
A status code is a number at the start of a response that says how the request went. The status code 404 means the page was not found. The status code 302 means the page is at another URL.
The header named Location holds the URL to go to. The browser sends a new request to the URL in the Location header.
The sports shop after sign-in
Sam, a customer, opens the orders page of the sports shop before signing in. The shop sends Sam to the sign-in page and keeps the page Sam wanted in the URL. The picture cuts the URL of the sign-in page into pieces.
https://shop.example/login?next=/orders
- shop.examplethe host name, the name of the website
- /loginthe sign-in page
- next=/ordersa query parameter named next
The query parameter next holds /orders, the page to open after sign-in.
After Sam signs in, the server runs the Python below. Follow the value of next from line 3 to line 4:
def login(): # the lines that check the password are left out target = request.args["next"] return redirect(target)
Line 3 reads the query parameter next from the request, so target holds /orders. Line 4 passes target to redirect, a function that builds a redirect. redirect puts target in the Location header, and Sam's browser opens the orders page.
The source and the sink
Line 3 is the source, where the URL comes in. Line 4 is the sink, where the code sends the browser to the URL. No line between the two checks the URL.
What is an open redirect
The person who writes a link chooses every query parameter in the link. An attacker writes the link in the picture and sends the link to Sam in an email.
https://shop.example/login?next=https://evil.example/login
- shop.examplethe host name Sam reads
- /login?next=the query parameter named next
- https://evil.example/loginwhere the browser ends up
Sam reads shop.example, trusts the link and signs in on the real shop. Line 3 reads https://evil.example/login into target. Line 4 redirects Sam's browser to the attacker's website.
The link
starts with shop.example
- Sam clicks
The real shop
Sam signs in, and line 4 redirects
- redirect
The attacker's page
a copy of the sign-in page
The attacker's page is a copy of the shop's sign-in page with the words "Wrong password, try again". Sam types the password again, and the attacker now has Sam's password. The fake page is an example of phishing.
An open redirect (CWE-601) is a redirect to a URL taken from the request, so an attacker chooses where the browser goes.
Examples of defences
A safer redirect takes no URL from the request. The query parameter holds a short name, such as orders. The code looks the name up in an allowlist. A name that is not on the allowlist goes to one default page.
Where the query parameter must hold a URL, safer code allows only a path. The path must start with one /. A browser reads //evil.example and /\evil.example as the website evil.example, so the code refuses both.
Checking a host name
Some redirects must go to another website, such as the website that takes payment for the sports shop. The code then compares the host name of the URL with the host names on an allowlist. The Python shows two checks for one host name, and only Check B compares the whole host name:
# Check A if target.startswith("https://shop.example"): return redirect(target) # Check B host = urlparse(target).hostname if host == "shop.example": return redirect(target)
Check A uses startswith, which tests only the first letters of the URL. The URL in the picture passes Check A.
https://shop.example.evil.example/login
- https://shop.examplethe letters Check A tests
- .evil.examplethe rest of the host name
- /loginthe path
shop.example.evil.example, a website the attacker owns.Check B uses urlparse, a URL parser. The part named hostname is the whole host name. shop.example.evil.example is not equal to shop.example, so Check B refuses the URL. A full check also tests that the scheme is https, since urlparse gives the same host name for a URL that starts http or javascript. A browser reads a backslash in a URL as a slash and urlparse does not, so safer code also refuses a URL that holds a backslash.
The URL https://[email protected] also passes Check A. A browser reads the text before @ as a username, so the host name is evil.example.
Forwards
A forward passes a request to another page on the same server without telling the browser. The browser sends one request and gets one response. A redirect makes the browser send a second request.
The sports shop has a refunds page at /staff/refunds. The shop checks that a person is a member of staff on every request for a path that starts with /staff.
The help page of the shop forwards to the page named in a query parameter. Sam is not a member of staff, and opens shop.example/help?page=/staff/refunds.
Sam's browser
sends one request
- a request for /help
The help page
has no staff check
- forward
The refunds page
runs for Sam
The path of Sam's request is /help, so the staff check does not run. The forward then runs the refunds page for Sam. Safer code takes the page to forward to from an allowlist.
Code after a redirect
The function redirect builds a response. redirect does not stop the lines below from running. The Python is the refunds page, and line 3 has no return:
def refunds(): if not is_staff(user): redirect("/denied") pay_refund(order)
Line 3 builds a redirect for a person who is not a member of staff, and the function continues. Line 4 pays the refund for every person. The safer line 3 is return redirect("/denied"). The word return ends the function, so line 4 runs only for staff.
Examples of what to look for
- A redirect to a URL read from a request, such as a query parameter named
urlornext, with no allowlist. - A path from a request with no check that the path starts with one
/and not//or/\. - A host name checked with
startswith, or within(the name is anywhere in the URL), in place of a URL parser and==. - A forward to a page named in a query parameter.
- A
redirectwith noreturnin front, and work on the lines below.
Check your understanding
State what a redirect is.
Show the answer
A response that tells the browser to go to a different URL.
Identify the line in the sign-in code that redirects to a URL from a query parameter.
Show the answer
Line 4,
return redirect(target).targetholds the query parameternext.Give one reason why a link that starts with
shop.examplecan end on another website.Show the answer
The shop redirects the browser to the URL in the query parameter
next, and the attacker wrote the query parameter.
Practise it
Challenges with a weakness this lesson teaches.
- Payday remindersBrightledger, IntermediateOpen
- Mentions and notificationsChirp, IntermediateOpen