Skip to content

refactor: read URL schemes with one Rust reader on the server (#601) - #718

Merged
JuroOravec merged 2 commits into
mainfrom
refactor/one-url-scheme-reader
Nov 15, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
refactor/one-url-scheme-reader

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Follow-up to #601 for citry 1.0.0, implementing the approved C10 plan (v2).

Merge order: this PR should merge last, right before the 1.0.0 release candidate. See "Expected red pyodide-smoke" below.

What changes

Each server-side check that read a URL's scheme had its own copy of the rule: four in Python and three in Rust. Now one Rust function reads it the way the browser's URL parser does, and every server-side check calls it.

  • The reader: url_scheme and url_scheme_is live in crates/citry_html_transform/src/url_scheme.rs. They skip leading U+0000 to U+0020, drop tab, LF and CR anywhere, accept an ASCII letter followed by ASCII letters, digits, +, - or . up to :, and compare letters as ASCII only. They decode no character references. Callers keep decoding exactly where they did before (decode_html_attribute, fix(ui): read a Markup URL the way Citry writes it in the link checks #697's attribute_text and _written_attribute_text).
  • Rust callers: is_javascript_url now calls url_scheme_is (letter_matches is deleted). decode_data_url_document and refresh_data_url keep their own trimming and slicing and ask the reader only "is it data", so their answers stay the same.
  • Python: a private PyO3 function, _read_url_scheme (stub in _rust.pyi), is wrapped once in the new private module citry._urls. actions.redirect_url_scheme (Redirect), routes._resolve_redirect_url (the no-JavaScript form path) and Citry UI's check_link_url and is_image_url_allowed call it. Their own scheme readers and constants are deleted.
  • Browser copies: redirectScheme in client.js and isImageUrlAllowed in CAvatar and CImage are not edited, so runtime.js does not change. They are tested against the same table.
  • Shared table: crates/citry_html_transform/tests/fixtures/url_schemes.json holds 93 rows. The Rust tests, the binding, every Python caller, the JS redirect test, the Citry UI browser image rule and a Playwright test all read it.
  • Lock: tests/test_urls_module.py locks the names and signatures of citry._urls for 1.x, because Citry UI imports the module.

The one change users can see

security_csp, security_javascript and citry check no longer report javaſcript:, javascrıpt: or javascrİpt:, including when written as ſ or ı in attribute text. Browsers read these as relative URLs and run nothing. A Playwright test clicks such links in Chromium, Firefox and WebKit and checks that nothing runs, next to a control link that does run. This makes the check less strict, which the 1.x rules allow. The changelog has one entry. Redirect, the Events redirect resolver, Citry UI and the data: readers keep their exact answers.

Expected red pyodide-smoke

The pyodide-smoke job builds citry and citry-ui from the checkout, but installs the published citry-core named in docs_site/static/playground/runtime.json (2.0.0). citry._urls imports the new _read_url_scheme at module level, and citry-core 2.0.0 does not have it. So the job fails on this PR, and on every later PR, until the docs release after 1.0.0 updates runtime.json.

The job is not a required check. The required checks (check, citry-ui-wheel-build) use the citry-core wheel built from the checkout. Moving the import inside a function to keep the job green would hide a real version mismatch, so this PR does not do that. That is why it should merge last, right before the release candidate.

Not in this PR (the release PR owns these)

  • The citry-core version (2.1.0, per the maintainer's decision), the citry-core== pin in citry, and uv.lock. Cargo.lock gains one line, for a serde_json dev-dependency of citry_html_transform (already pinned in the workspace).
  • Citry UI's floor citry>=1.0.0,<2. Until then, Citry UI on main declares citry>=0.9.0 but needs citry._urls. No Citry UI release is planned before 1.0.0.

Checks

  • cargo test -p citry_html_transform -p citry_core_py, cargo clippy, cargo fmt: pass. The Rust tests compare the reader with a separately written reference reader over the table and over 20,000 strings from a fixed seed. The alphabet includes ſ, ı, İ, U+212A, U+000B, U+007F, U+00A0, U+FEFF and U+200B.
  • The data: readers: a test locks their answers. Before locking them, I ran the same inputs through the code on origin/main and through this branch, and the answers were identical.
  • scripts/check.py --profile fast: PASSED, every phase. It ran before the review edits; after them I reran the focused Rust, Python and JS checks and mypy.
  • mypy, also with --platform linux: clean.
  • CITRY_OUTPUT_POLICY_DIFFERENTIAL=1 run of the output-policy tests: pass. The Python reference regex is now ASCII-only, and the page generator rarely picks the lookalike letters, so most generated URLs still read as javascript:.
  • New Playwright test on Chromium, Firefox and WebKit: 24 passed. It runs in the existing PR e2e job (Chromium) and the weekly three-browser run. Firefox refuses to parse da\tta: and da\nta: at all, so they load nothing; the test allows that for those two named rows only.
  • One-off timing, not committed: check_output_html in a release build on a 470 KB page with 10,000 URL attributes. Median 9.57 ms and 9.66 ms after the change, against 9.71 ms and 9.76 ms before. No slowdown; the budget was 5%.

Independent review

  • Security and technical review (Mechanism 6, 7 and 8, high effort): no holes found. The reviewer ran Playwright probes in all three browsers (about 110 raw URLs, 33 entity-encoded attribute texts, and an iframe run test) and found no case where a browser reads a scheme the reader does not. A 200,000-string comparison of the old and new Python readers found no differences apart from the three lookalike spellings. Mechanism 8: the reviewer read the advisory survey and checked xss-url, xss-url-svg, sanitizer-bypass, open-redirect, redos, header-inj and client-url. The change introduces none of them. Findings fixed in this PR:
    • Stale line numbers in docs/design/trust_boundaries.md rows whose files this change moved: updated.
    • citry._urls and url_scheme.rs overclaimed "every server-side check": they now name the callers.
    • The e2e test accepted any URL that failed to parse, and its https: base could hide a relative row. It now runs against two bases and allows parse failures only for the two named Firefox rows.
    • The Rust table test could skip a row written over several lines without noticing. It now counts every row in the file.
    • Removing the Redirect regex from the regex timing test left no Python-level timing test for long input. Added one.
    • Left as is: Citry UI's table test repeats the data:image/ rule it tests. The scheme and the rest still come from the table, so the reader itself is tested.
  • Prose pass: found six wrong or stale facts and eleven wording problems, all fixed. Among them, it removed the stale "fix(ui): read a Markup URL the way Citry writes it in the link checks #697 exception" row from trust_boundaries.md, which fix(ui): read a Markup URL the way Citry writes it in the link checks #697 asked to delete on merge.
  • docs-cold-reader was not run, because no docs page or public docstring changed (citry._urls is private).

🤖 Generated with Claude Code

Every server-side check that reads a URL's scheme now calls one Rust
function, `url_scheme` / `url_scheme_is` in
crates/citry_html_transform/src/url_scheme.rs, written to the WHATWG URL
rules. It decodes no character references; callers keep decoding where
they did.

- The output checks (`is_javascript_url`) and the `data:` document and
  meta refresh readers use it directly.
- A private PyO3 function, `_read_url_scheme`, is wrapped once in the new
  `citry._urls`, which `Redirect`, the no-JavaScript redirect resolver
  and Citry UI's link and image checks now call.
- The browser copies (client.js `redirectScheme`, CAvatar/CImage
  `isImageUrlAllowed`) are unchanged and tested against a shared table,
  crates/citry_html_transform/tests/fixtures/url_schemes.json, as are the
  Rust reader, the binding and every Python caller. A Playwright test
  checks the table against real browsers.

The output checks no longer report `javascript:` spelled with the long s
or a dotted or dotless i, which browsers read as relative URLs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec JuroOravec mentioned this pull request Nov 15, 2026
4 tasks done
…me-reader

# Conflicts:
#	docs/design/trust_boundaries.md
@JuroOravec
JuroOravec merged commit b668c34 into main Nov 15, 2026
1 of 18 checks passed
@JuroOravec
JuroOravec deleted the refactor/one-url-scheme-reader branch November 15, 2026 21:53
JuroOravec added a commit that referenced this pull request Nov 16, 2026
* docs: record the security reviews of #713, #714, #718 and #722

Adds the Mechanism 7 step 7 records for four merged pull requests:
GHSA-x773 (#713, reserved attribute names as c-bind keys), #714
(out-of-date pages and the rolling-deploy check), #718 (one Rust URL
scheme reader), and GHSA-7fjh (#722, reserved data-* names in Citry UI
attribute maps). The two advisory records name the advisory and the
affected and fixed versions; the facts come from the pull requests.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* docs: link the #720 follow-up from the #714 review record

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

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.

1 participant