Repository navigation
feat(events): add is_local_url() and local_url_or() to keep redirects on the site - #866
Merged
Merged
Conversation
… 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
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
(Opus 5.5)
Adds
is_local_url()andlocal_url_or()tocitry.ext.events, so a handler can check anextURL from the request before it redirects: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 fromcitry.ext.eventsbesideget_event_url(). Why there and not elsewhere:citry.ext.events.actionsholds 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.eventsalready exports the plain helperget_event_url(), and the docs that need the check are the Events docs.citrynamespace is for core component APIs; this check exists foractions.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 solocation.assign()and aLocationheader) does:actions.RedirectraisesValueErroron them, solocal_url_or()never handsRedirecta URL it raises on.//shop.example\u00a0\u3000as a host other thanshop.example, so the check must look at the same text the browser reads.\is read as/; the scheme comes from the shared WHATWG scheme reader (citry/_urls.py) thatRedirectand the output checks use.http:andhttps:pass, and only asscheme://host;https:next,https:/x,///host,https:///hostfail.@in the host part fails, read both with\as a delimiter and without (Django's double check).allowed_hostsas a single string is one host; a generator is read once.local_url_or()checks the fallback on every call and raisesValueErrorwhen it fails, so a bad fallback shows up in the first test.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 withoutallowed_hosts, with and withoutrequire_https), it resolved the URL withnew URL(url, base)against anhttps:and anhttp:page in Chromium 158, Firefox 159 and WebKit 27.2, and checked the result ishttp:/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 indocs_site/content/events/actions.mdnow showlocal_url_or()in place of the pointer to Django.Redirectdocstring,docs/design/trust_boundaries.md(Redirect row) and the action table indocs/design/events.mdpoint at the helper.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 laterRedirect(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 thatRedirectaccepts every URL the check passes), plus the existing Events actions, redirect scheme and routes tests: pass.ruff check,ruff format --check,mypyonpackages/py/citry/citry(also with--platform linux): clean.scripts/validate.py, the docs config tests andpython -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.
location.assignwith JavaScript, and the server-builtLocationwithout it, including real 303 navigations, in Chromium, Firefox and WebKit. Oddallowed_hostsentries ("*",".shop.example") match only themselves. No string makes the helper raise, andRedirectaccepts every valuelocal_url_or()returns.allowed_hostsentries are literal, and the fallback and port rules are spelled out. The cold read also caught thatis_local_url()does not take "the same arguments" aslocal_url_or()(it has no fallback). The line numbers in theRedirecttrust-boundary row were refreshed.🤖 Generated with Claude Code