Agent Wranglers

APS-40Recommended

Redirect only to your own pages

A redirect that takes its target from the address must only go to your own pages or an allow-list.

How to do it

Accept only paths that start with a single /, or IDs that map to known targets. Reject full addresses, //host, backslashes and control characters. Attackers use open redirects to make phishing links look trusted.

Examples

Plain text example

/login?next=https://evil.example.net
/login?next=//evil.example.net
/login?next=/\evil.example.net
/login?next=/%09/evil.example.net

JavaScript example

const isSafeNext = next => typeof next === 'string' && /^\/(?!\/)/.test(next) && !/[\\\x00-\x1f]/.test(next);
res.redirect(isSafeNext(req.query.next) ? req.query.next : '/');

Python example

def is_safe_next(target: str) -> bool:
    return (target.startswith("/") and not target.startswith("//")
            and not any(ch == "\\" or ord(ch) < 32 for ch in target))

return redirect(target if is_safe_next(target) else "/")

How to check

The agent sets the return address to an outside site in several forms. Every one lands on the home page or an error.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/application-security/browser-security-headers/block-open-redirects">Agent Wranglers application security playbook</a>. In code you write, add a comment next to it with the same link.