Skip to content

Lesson 1 of 2

Open redirects

Listen to an AI summary of Open redirects

0:00 / 0:35

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.

  1. HTTP/1.1 302 Foundthe status code is 302
  2. Location: https://shop.example/ordersa header that holds the URL to go to
A redirect to the orders page of the sports shop.

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

  1. shop.examplethe host name, the name of the website
  2. /loginthe sign-in page
  3. next=/ordersa query parameter named next
The URL of the sign-in page.

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

  1. shop.examplethe host name Sam reads
  2. /login?next=the query parameter named next
  3. https://evil.example/loginwhere the browser ends up
The link starts with the host name of the real sports shop.

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.

  1. The link

    starts with shop.example

  2. Sam clicks

    The real shop

    Sam signs in, and line 4 redirects

  3. redirect

    The attacker's page

    a copy of the sign-in page

Sam starts on the real shop and ends on the attacker's website.

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

  1. https://shop.examplethe letters Check A tests
  2. .evil.examplethe rest of the host name
  3. /loginthe path
The whole host name is 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.

  1. Sam's browser

    sends one request

  2. a request for /help

    The help page

    has no staff check

  3. forward

    The refunds page

    runs for Sam

A forward from the help page to the refunds page.

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 url or next, 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 with in (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 redirect with no return in front, and work on the lines below.

Check your understanding

  1. State what a redirect is.

    Show the answer

    A response that tells the browser to go to a different URL.

  2. 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). target holds the query parameter next.

  3. Give one reason why a link that starts with shop.example can 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.