DEV Community

Why 'contains our hostname' fails as an open redirect check

The first fix for an open redirect is nearly always the wrong one. Someone sees that a login page sends users wherever the next parameter says, adds a check that the URL contains the trusted hostname, and closes the ticket.

A scenario

Picture a login page at app.example.com. After sign-in it redirects to the value of next. The guard reads: the URL must contain app.example.com.

All of these pass:

  • https://app.example.com.evil.example/
  • https://evil.example/?app.example.com
  • https://app************@evil.example/

The first one puts the trusted name inside a domain the attacker owns. The second hides it in the query string. The third relies on the URL userinfo syntax: text before the @ is a username, so the browser connects to evil.example and treats app.example.com as a login name.

Why "starts with" does not rescue it

Changing the test to "starts with https://app.example.com" kills the second payload and leaves the first and third.

A relative-path rule has its own hole. //evil.example starts with a slash, and browsers read it as a scheme-relative link to another host. Some also normalise /\evil.example the same way.

What holds up

String matching is the wrong tool. Parse the URL and make decisions on the parts:

from urllib.parse import urlparse
ALLOWED = {"app.example.com"}
def safe_next(value):
    u = urlparse(value)
    return u.scheme == "https" and u.hostname in ALLOWED

hostname is the real host after the userinfo and port are stripped, which is why this rejects the @ trick. Comparing it with set membership means no partial matches.

Better options exist. Accept only a relative path with exactly one leading slash, and reject // and backslash forms. Or accept no URL at all: the parameter carries a short name such as dashboard, and the server maps it to a page.

Test it before you ship it

A fix is worth keeping only if it survives the list above. Put the three payloads and //evil.example into a unit test for the redirect helper, and add a case for a legitimate path such as /account/settings so the guard does not block real users.

Tests like these take a few minutes and stop the contains-check from creeping back in a later refactor.

Why it matters

On its own, an open redirect is a modest finding. What makes it worth fixing is trust. The link starts on a real domain, the sign-in is real, and the user then lands on a page the attacker controls. In SSO flows it can also help other bugs along.

Practise

The free open redirect lab on Codelivly has a sloppy check in a sample SSO portal, so you can see the bypass work before you write the fix. The SSRF lab shows the server-side version of the same URL validation mistake. If you want to turn this kind of finding into a report, the Bug Bounty Ebook Bundle covers recon, payloads and playbooks.

Top comments (1)

The @ trick is the payload most developers miss, Rocky - the first two are the ones people guess, but app************@evil.example/ looks safe to anyone who reads left to right, and the browser quietly treats the trusted name as a username. That is the example I would show a teammate first. Your fix is the right shape: parse, then decide on urlparse's hostname after userinfo and port are stripped, and compare by set membership so no partial match sneaks through. The part I would add is a warning about the single-slash relative rule - it only holds if you also reject backslash forms, because some browsers normalise /\evil.example into a scheme-relative hop to another host. One question on the allowlist: set membership rejects subdomains too, so a legit redirect to a subdomain of app.example.com would break. Have you seen a real app where that tradeoff bit someone - a legit subdomain redirect that the strict allowlist blocked?

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.