Skip to content

fix(routing)!: match routes and Django reverse() in linear time, and refuse ambiguous route paths (#602) - #640

Merged
JuroOravec merged 4 commits into
mainfrom
fix/602-segment-route-matcher
Nov 14, 2026
Merged

JuroOravec merged 4 commits into
mainfrom
fix/602-segment-route-matcher

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Closes #602.

Citry now matches every request path against its routes with its own segment matcher, in every web framework integration, and Django's reverse() on Citry routes takes time in step with the length of the values. Both are stricter about route paths and values that have no single meaning.

What changes for apps

  • Two parameters side by side are refused. A URLRoute path such as "{year}{month}", or a parent path ending in a parameter joined to a child path starting with one ("files/{name}" + "{ext}"), raises ValueError naming the route (both routes for a parent and child) when asgi_app(), wsgi_app(), the Flask or FastAPI mount(), or Django's urlpatterns() mounts the routes. Before, such a route matched and the first parameter took all but the last character.
  • A repeated parameter name ("{id}/{id}") raises ValueError at mount instead of a regular expression error later.
  • dotless_params must be a frozenset of names; anything else raises TypeError when the URLRoute is built.
  • Django reverse() raises NoReverseMatch for a value that is empty, holds a /, or holds a . for a dotless parameter. Before, it could return a URL that the route splits into different values (cache/a.b.js for class_id="a.b"). Every other call returns the same URL as before.
  • Routes with text between their parameters match the same paths with the same values as before.

How it works

  • util/routing.py: one cached parser splits a full route path into segments and refuses repeated or adjacent parameters. check_route_table() checks every handler route's full path at mount. The matcher splits a segment with a search from the right, a split at each . (Citry's cache routes), or a general walk for other dotless mixes; each takes time in step with the segment's length. The regex is no longer used to match requests.
  • contrib/django.py: the pattern class still matches requests with Citry's matcher. For reverse() it holds route_reverse_regex(), where each parameter is an atomic group that stops at the first place the following text can start, plus one converter per parameter that refuses a value the route could never give back. When every value passes, the values are a split the route accepts, so the regex accepts the URL.
  • Docs: the URLRoute docstring, the "Serve extension routes" section (new "Stop a value at a dot" and "Separate two parameters" subsections), Raises: text on the adapters, and two design-doc lines. Changelog under Unreleased with an upgrade note.

Tests

  • tests/test_route_pattern_linear.py: the matcher against the earlier regex (tests/_route_oracle.py) on generated routes, refusal messages, dotless_params rules and type, and growth tests that compare an input with one eight times as long.
  • tests/test_route_path_rules.py: every adapter refuses an adjacent-parameter route at mount and leaves the app and instance unchanged.
  • tests/test_django_route_reverse.py: reverse() against the earlier regex on generated routes and values (same URL when every value passes, refused otherwise, and the route matches each returned URL), and growth tests for reverse() and for the regex alone.
  • Route-related tests pass on Django 4.2.30, 5.2.19 and 6.0.6; ruff and mypy (including --platform linux) are clean.

Review

Independent reviews covered the matcher, the mount checks, the Django reverse() change, the security pass for request routing and URL building, and a separate prose pass. One case is recorded as unproven: an author-written parent re_path that refuses its own text but matches a longer prefix reaching into the Citry part.

🤖 Generated with Claude Code

@JuroOravec
JuroOravec force-pushed the fix/602-segment-route-matcher branch from 854b704 to 89f7471 Compare November 14, 2026 13:37
JuroOravec and others added 4 commits November 14, 2026 14:38
… with Citry's matcher (#602)

Every adapter now matches request paths with Citry's own segment matcher;
the regex from compile_route_pattern is kept only for Django's reverse()
and URL checks. A route whose full path puts two parameters side by side
(`{a}{b}`, in one path or across a parent and child) or repeats a
parameter name raises ValueError naming the route(s) when asgi_app(),
wsgi_app(), the Flask and FastAPI mount(), or Django's urlpatterns()
mount the routes. Other routes match the same paths with the same values.

Two quick split paths (a search from the right, and a split at each `.`
for Citry's cache-route shape) keep matching fast; the general walk
serves the remaining dotless mixes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ck dotless_params type (#602)

The regex the Django integration keeps for each Citry route now puts each
parameter in an atomic group that stops at the first place the following
text can start, so reverse() searches the URL it builds in time in step
with its length. One converter per parameter checks the URL with Citry's
own matcher, reading all of the call's values from reverse() itself, so
reverse() keeps returning the URLs it returned before.

URLRoute now raises TypeError when dotless_params is not a frozenset of
parameter names. compile_route_pattern is removed; tests keep the earlier
regex as a reference in tests/_route_oracle.py.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… Django's frame (#602)

The converter for each Citry route parameter now refuses a value that is
empty, holds a `/`, or holds a `.` for a dotless parameter, and keeps no
state and reads nothing from Django's internals. With every value
passing, the values themselves are a split the route accepts, so the
linear regex accepts the URL and reverse() returns the URL it returned
before. Other values now raise NoReverseMatch, where the earlier regex
could return a URL that the route splits into different values.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec force-pushed the fix/602-segment-route-matcher branch from 89f7471 to d7d2ee1 Compare November 14, 2026 13:38
@JuroOravec
JuroOravec merged commit 31038d7 into main Nov 14, 2026
14 of 28 checks passed
@JuroOravec
JuroOravec deleted the fix/602-segment-route-matcher branch November 14, 2026 14:00
JuroOravec added a commit that referenced this pull request Nov 14, 2026
…ight merged changes (#682)

Add one record under docs/security/reviews/ for each of the eight published
0.8.0 advisories (GHSA-4hj5, -8xvf, -7qpw, -g658, -x6rm, -h524, -xmx5,
-xg8c) and for each of the pull requests that Mechanism 7, step 7 owes one:
#610, #640, #651, #652, #653, #658, #674 and #675. Each record says only what
the public pull request, the published advisory, the merged code and its tests
show, and lists what was not checked.

The reviews README describes how a record for a published advisory differs
from a pull request record. The Mechanism 7 list in CLAUDE.md gains the two
places that GHSA-7qpw added: parser rules browsers differ on, and the page's
declared encoding.

Co-authored-by: Claude Opus 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.

Make URLRoute matching linear in path length, whatever the routes

1 participant