Skip to content

Add a helper to check that a redirect URL stays on the site #568

Done

Description

@JuroOravec

What this is for

An event handler often redirects to a URL it took from the request, such as a next parameter after a login:

return actions.Redirect(data.next)

That URL can point to another site. Today the docs ask app authors to check it first and point to Django's url_has_allowed_host_and_scheme as an example. A check written by hand is easy to get wrong: startswith("/") accepts //other.example/ and /\other.example/, which browsers load from another site. Citry should ship one tested check that every app can use.

Proposal

Two public functions, for example in citry.ext.events (the exact module is open):

def is_local_url(
    url: str | None,
    *,
    allowed_hosts: Iterable[str] = (),
    require_https: bool = False,
) -> bool: ...


def local_url_or(
    url: str | None,
    fallback: str = "/",
    *,
    allowed_hosts: Iterable[str] = (),
    require_https: bool = False,
) -> str: ...
  • is_local_url() answers True when a browser would load the URL from this site: a relative URL, or an absolute http/https URL whose host is in allowed_hosts. It follows Django's url_has_allowed_host_and_scheme, which is a good model because its cases come from years of reports.
  • local_url_or() returns url when is_local_url() accepts it, and fallback otherwise, so a handler can write actions.Redirect(local_url_or(data.next, "/")).

The check must parse the URL the way a browser does, not the way urllib.parse does:

  • strip leading and trailing whitespace, then reject a URL that starts with a control character, as Django does; remove tabs and newlines anywhere, as browsers do;
  • read \ as /, so /\host and \\host count as another site;
  • treat //host/path as an absolute URL on host, and reject ///path and http:///x as Django does;
  • reject any absolute URL that carries userinfo (user@), as Django does, since its host part never matches an allowed host;
  • accept only the http and https schemes for an absolute URL; javascript:, data:, and any other scheme are rejected.

Error modes

  • url is None or "": is_local_url() returns False, and local_url_or() returns fallback.
  • url is not a string and not None: TypeError, since that is a bug in the calling code, not bad input from the request.
  • url cannot be parsed (for example http://[::1): False, never an exception, so request data cannot crash the handler.
  • allowed_hosts is empty: only relative URLs pass. Host matching ignores case (Django compares exactly); a port is part of the host and must match, as in Django. A trailing dot (example.com.) and an internationalized name are compared as written, with no normalization, so list each spelling you accept.
  • allowed_hosts is a single string: it is treated as one host, as Django does, not iterated character by character.
  • require_https=True: an absolute http:// URL, and a scheme-relative //host URL, are rejected even on an allowed host.
  • fallback is not itself accepted by is_local_url(): ValueError on the call, so a mistyped fallback fails in the first test rather than sending visitors off the site.

Later option: Redirect(url, same_host=True)

A flag on the action itself would be easier to discover:

return actions.Redirect(data.next, same_host=True)

The name is same_host, not same_site, because on the web "same site" includes subdomains, while this check compares the host.

The server can check the URL against the request's host before it answers a no-JavaScript form post with a 303. On the JavaScript path, though, the browser runtime applies the action, so it must run the same check before location.assign(). Today's runtime rejects a redirect action with an unknown field and applies none of the result's actions, so the server must not send the flag to a runtime that does not say it supports it. That adds a field to the Events v1 redirect action and a way for the runtime to announce support, which changes the protocol in packages/protocol/events/v1/. It therefore needs a written plan before any code: the wire field, what a runtime that does not know it does, and what the visitor sees when the check fails (no navigation and a call error, or navigation to a fallback).

Done when

  • is_local_url() and local_url_or() are public, documented, and tested against Django's case list plus the \ and control-character cases above.
  • docs_site/content/security.md and the Redirect section of docs_site/content/events/actions.md show the helper in place of the pointer to Django.

Activity

  1. added
    next-sprint
    on Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    next-sprint

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions