Skip to content

feat(events): add is_local_url() and local_url_or() to keep redirects on the site - #866

Merged
JuroOravec merged 2 commits into
mainfrom
events/is-local-url
Nov 24, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
events/is-local-url

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

(Opus 5.5)

Adds is_local_url() and local_url_or() to citry.ext.events, so a handler can check a next URL from the request before it redirects:

from citry.ext.events import actions, local_url_or

return actions.Redirect(local_url_or(data.next, "/"))

Closes #568.

API and where it lives

The signatures are the ones the issue proposes. Both functions live in a new module, citry/ext/events/local_urls.py, and are exported from citry.ext.events beside get_event_url(). Why there and not elsewhere:

  • citry.ext.events.actions holds only the action constructors (its docstring says so, and every name in it is a capitalized action), so a plain helper function would be the odd one out.
  • citry.ext.events already exports the plain helper get_event_url(), and the docs that need the check are the Events docs.
  • The root citry namespace is for core component APIs; this check exists for actions.Redirect.

How the check reads a URL

It follows Django's url_has_allowed_host_and_scheme, but reads the URL the way the WHATWG URL parser (and so location.assign() and a Location header) does:

  • A URL with any control character anywhere fails (C0, DEL and C1), including a tab or newline. Django drops tabs and newlines and accepts; this check refuses them because actions.Redirect raises ValueError on them, so local_url_or() never hands Redirect a URL it raises on.
  • Only ASCII spaces are stripped from the ends, as the browser strips them. A URL that starts or ends with other whitespace (U+00A0, U+3000) fails. Django strips all Unicode whitespace; the browser proof below found that Chromium reads //shop.example\u00a0\u3000 as a host other than shop.example, so the check must look at the same text the browser reads.
  • A URL that starts with a Unicode control or format character fails, as in Django.
  • \ is read as /; the scheme comes from the shared WHATWG scheme reader (citry/_urls.py) that Redirect and the output checks use.
  • Only http: and https: pass, and only as scheme://host; https:next, https:/x, ///host, https:///host fail.
  • Any @ in the host part fails, read both with \ as a delimiter and without (Django's double check).
  • Host comparison lowercases ASCII letters only (Unicode case mapping can differ from IDNA mapping), and compares everything else as written: port, trailing dot, IDN spelling.
  • allowed_hosts as a single string is one host; a generator is read once.
  • local_url_or() checks the fallback on every call and raises ValueError when it fails, so a bad fallback shows up in the first test.
  • No 2048-character limit like Django's: the check is linear and does no Unicode normalization, so a long URL costs nothing special.

Deliberate differences from Django are marked in the test table (django_agrees=False), and a test runs Django on every case to keep the table honest.

Browser proof

A one-off script (not committed) built about 32,000 URLs from the test cases, all short combinations of /, \, :, @, https:, hosts, spaces and full-width slashes, and 40,000 random token strings. For every URL the check accepts (with and without allowed_hosts, with and without require_https), it resolved the URL with new URL(url, base) against an https: and an http: page in Chromium 158, Firefox 159 and WebKit 27.2, and checked the result is http:/https: on an allowed host (or the page's host for a relative URL). The first run found the Unicode-whitespace case above; after the fix, zero URLs leak in all three browsers.

Docs

  • docs_site/content/security.md ("Check redirect URLs") and the warning in docs_site/content/events/actions.md now show local_url_or() in place of the pointer to Django.
  • The Redirect docstring, docs/design/trust_boundaries.md (Redirect row) and the action table in docs/design/events.md point at the helper.
  • Both functions are in the Events API reference (docs_site/reference.yml).

Versioning

Adds two public names and changes no existing behavior, so it is a minor-release feature, listed under citry 1.3.0 in CHANGELOG.md. No contract version or wire format changes. The later Redirect(url, same_host=True) option from the issue changes the Events protocol and is not part of this PR.

Checks

  • tests/test_events_local_urls.py (the case table, 93 URLs,, Django comparison, error modes, and a test that Redirect accepts every URL the check passes), plus the existing Events actions, redirect scheme and routes tests: pass.
  • ruff check, ruff format --check, mypy on packages/py/citry/citry (also with --platform linux): clean.
  • scripts/validate.py, the docs config tests and python -m docs_site build-check: pass; the new cross-references resolve in the built pages.

Review

An independent reviewer ran a security pass, the advisory-survey check, the versioning check and a prose pass; a separate agent cold-read the docs.

  • Security: no URL found that the helper accepts and a browser loads from another site. The reviewer fuzzed about 48,000 more URLs (C1 controls, Unicode case and dot lookalikes, full-width delimiters, lone surrogates, percent-encoded dots and slashes) through both redirect paths: location.assign with JavaScript, and the server-built Location without it, including real 303 navigations, in Chromium, Firefox and WebKit. Odd allowed_hosts entries ("*", ".shop.example") match only themselves. No string makes the helper raise, and Redirect accepts every value local_url_or() returns.
  • Fixed during the review: C1 control characters in the middle of a URL passed although the docs said "any control character"; they are now refused. The docs' comparison with Django was corrected, the docs say that allowed_hosts entries are literal, and the fallback and port rules are spelled out. The cold read also caught that is_local_url() does not take "the same arguments" as local_url_or() (it has no fallback). The line numbers in the Redirect trust-boundary row were refreshed.
  • Versioning: adds names only; minor release (1.3.0). Once released, the accept and refuse rules are covered behavior, so a later tightening ships only as a security fix.

🤖 Generated with Claude Code

JuroOravec and others added 2 commits November 24, 2026 19:18
… on the site

A handler that redirects to a `next` URL from the request needs a check
that reads the URL the way a browser does. The two helpers follow
Django's url_has_allowed_host_and_scheme, read "\" as "/", refuse extra
slashes before a host, userinfo, schemes other than http and https, and
any control character, and compare hosts against an allowed list.

The security and Events actions docs now point at the helper.

Closes #568

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Refuse C1 control characters too, so "a control character" means every
one. Correct the docs' comparison with Django, say that allowed_hosts
entries are literal hosts, and spell out the fallback and port rules.
Refresh the line numbers in the Redirect trust-boundary row.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit 61e83a3 into main Nov 24, 2026
34 checks passed
@JuroOravec
JuroOravec deleted the events/is-local-url branch November 24, 2026 19:37
JuroOravec added a commit that referenced this pull request Nov 24, 2026
#871 (#878)

Records the Mechanism 7 security reviews of six merged Events changes, in
the form docs/security/reviews/README.md describes. Each record is built
from the pull request description, the merged diff and its tests; where a
description does not record something the template asks for, the record
says so.

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant