Repository navigation
Description
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()answersTruewhen a browser would load the URL from this site: a relative URL, or an absolutehttp/httpsURL whose host is inallowed_hosts. It follows Django'surl_has_allowed_host_and_scheme, which is a good model because its cases come from years of reports.local_url_or()returnsurlwhenis_local_url()accepts it, andfallbackotherwise, so a handler can writeactions.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/\hostand\\hostcount as another site; - treat
//host/pathas an absolute URL onhost, and reject///pathandhttp:///xas 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
httpandhttpsschemes for an absolute URL;javascript:,data:, and any other scheme are rejected.
Error modes
urlisNoneor"":is_local_url()returnsFalse, andlocal_url_or()returnsfallback.urlis not a string and notNone:TypeError, since that is a bug in the calling code, not bad input from the request.urlcannot be parsed (for examplehttp://[::1):False, never an exception, so request data cannot crash the handler.allowed_hostsis 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_hostsis a single string: it is treated as one host, as Django does, not iterated character by character.require_https=True: an absolutehttp://URL, and a scheme-relative//hostURL, are rejected even on an allowed host.fallbackis not itself accepted byis_local_url():ValueErroron 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()andlocal_url_or()are public, documented, and tested against Django's case list plus the\and control-character cases above.docs_site/content/security.mdand theRedirectsection ofdocs_site/content/events/actions.mdshow the helper in place of the pointer to Django.
Activity