Skip to content

Make URLRoute matching linear in path length, whatever the routes #602

Closed

Description

@JuroOravec

If an app or an extension defines a route with two parameters next to each other, a long request path that does not match can take the server far longer to reject than its length suggests. URLRoute is the type that apps and extensions use to define an HTTP route, and Citry matches each request path against the routes with regular expressions. This issue asks for matching that stays fast whatever the routes look like, instead of fast only when each route is written with care.

Citry's own routes for component JavaScript and CSS (cache/...) had this shape. 0.7.0 fixed them with the dotless_params field, which makes a chosen parameter stop at the next . (GHSA-g63f-8h9g-m392). Routes that an app or an extension defines are unchanged, as that advisory says.

Adjacent parameters

A {name} parameter matches any characters except /. When two parameters sit next to each other, with nothing between them or only characters a parameter can also match (such as .), the regular expression engine tries every way to split the path between them. Two ways to get there:

  1. In one path. A route such as "files/{name}.{ext}" or "{year}{month}".
  2. Across a parent and a child. A child route's path is joined to its parent's by plain concatenation (flatten_routes). A parent "files/{name}" with a child ".{ext}" becomes files/{name}.{ext}. Neither route looks wrong on its own, and URLRoute sees only its own path when it is built.

What we want

  1. Fast matching. Matching or rejecting a path takes time in proportion to the path's length, for every route. It is not yet shown that a regular expression can do this and keep today's results; the change may need a hand-written matcher. Where a path could split between parameters in more than one way, keep today's split (the first parameter takes as much as it can) unless the plan lists the route shapes whose values change, with the old and new values.
  2. Reject ambiguous routes when routes are registered. A route whose parameters touch with no text between them ({a}{b}) has no single correct split. Reject it with a ValueError that names the route, and names both routes when a parent and a child make the join. This refuses a route that matches today, so the changelog says so. Parameters with text between them ({name}.{ext}) keep working under the fast rule. The check belongs where the full path is known, when the routes are registered or mounted in a web framework (not on every request), because URLRoute.__post_init__ cannot see the parent.
  3. Documentation. The URLRoute docstring says how a parameter ends, what happens with adjacent parameters, what dotless_params does (only the matched route's own setting applies, not a parent's), and that a child's path is joined to its parent's as plain text. Its example route, "cache/{class_id}.{script_type}", has the adjacent-parameter shape and no dotless_params; change the example.

Work around it today

Limit the length of request paths in front of Citry, as the advisory suggests. In your own routes, put / between parameters. If a . separates them, list every parameter that comes before a . in dotless_params:

URLRoute(
    "files/{name}.{ext}",
    handler=serve_file,
    dotless_params=frozenset({"name"}),
)

For a parent and a child, set dotless_params on the child route that has the handler; a parent's setting does not apply to its children's paths. This changes where the split falls: files/report.v2.txt gives name="report" and ext="v2.txt", where it gave name="report.v2" and ext="txt" before. dotless_params helps only for .. For another separator, put / between the parameters.

Security pass

The change alters how request paths are routed, so it needs the security pass that CLAUDE.md asks for before a change merges (Mechanism 7), including the host-rule cases that GHSA-h8v6-cw3f-jv68 covers.

Done when

  • A test builds routes with adjacent parameters, in one path and across a parent and child, and shows that the time to reject a long non-matching path grows in proportion to its length (compare two sizes; do not depend on a stopwatch limit).
  • {a}{b} is rejected with an error that names the route or routes.
  • Citry's own routes and the Django, Flask, FastAPI, ASGI and WSGI integrations match as before.
  • The URLRoute docstring and the page on extension routes describe the rule, and the changelog says what changed for an app that defines routes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    v0.9.0

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions