Skip to content

fix(deps): accept a str subclass URL such as SafeString in Script and Style - #799

Merged
JuroOravec merged 3 commits into
mainfrom
fix/asset-url-str-subclass
Nov 19, 2026
Merged

JuroOravec merged 3 commits into
mainfrom
fix/asset-url-str-subclass

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

(Opus 5.5)

What

Script(url=...) and Style(url=...) now accept a str subclass, such as the Django SafeString URL that django-compressor (and citry-django-compressor) produce.

Before, the Script was accepted when built, but a page that Vue renders or an Events response failed with "Prepared browser asset URL differs from its structured dependency URL". The check in _browser_loader_descriptor needs an exact str.

How

Dependency.__setattr__ (in citry/ext/dependencies/types.py) turns a str-subclass url into a plain str whenever the field is set: at construction, through dataclasses.replace(), and when a hook assigns a new URL later. The conversion is done by _plain_url_text:

  • A value without __html__ is copied with exact_str, so no method the subclass overrides (__eq__, startswith, lower, __str__) is called.
  • A value with __html__ (Markup, SafeString) is decoded the way escape_attribute_value already writes it into an attribute. So Markup("/a.js?x=1&y=2") still loads /a.js?x=1&y=2, and the static output is the same as in 1.0.3.

After that, every check reads the same exact text the browser gets: the escaping, the javascript: URL check under security_javascript, the serialization check that the written src/href matches url, and the integrity pinning. The safe mark is not trusted. A Path or other non-str value keeps its current behavior.

The serialize-time mismatch check is unchanged and still refuses a real mismatch, including a subclass URL that skips __setattr__ through object.__setattr__.

Out of scope: content and attrs values. They are author-only (app code), and attrs values already follow the decode-when-written rule. A SafeString in attrs["type"], attrs["integrity"] or a nonce is still refused on the Vue path, and so is a lazy (non-str) URL such as reverse_lazy(...).

Tests

11 new tests. 9 fail on main and pass here; the 2 mismatch guards pass on both.

  • tests/test_deps_types.py::TestUrlStrSubclass:
    • A Django SafeString URL serializes (Script and Style).
    • The URL stays plain after replace() and after a later assignment.
    • A subclass that overrides __eq__, __str__, startswith and lower cannot hide a javascript: URL from security_javascript="forbid".
    • A Markup URL with & is stored, rendered and serialized as the browser reads it.
    • Markup("javascript:...") is still refused.
    • The mismatch check still fires, for a patched _render and for a subclass that bypassed the conversion.
  • tests/test_vue_events.py: a SafeString Script and Style in a Dependencies class reach the Events payload through default_events_producer as plain str.

Checks run against citry-core 2.1.2 from PyPI:

  • The related suites pass: 1166 tests across test_deps*, test_serialize_security, test_browser_render, test_output_policy, test_output_html, test_vue_events, test_owned_file_integrity, test_asset_cors, test_attribute_safe_string_subclass and test_contrib_django.
  • ruff 0.14.13 (the locked version): check and format are clean.
  • mypy 1.20.2 on packages/py/citry/citry is clean, both natively and with --platform linux.

Security review (Mechanism 7 and 8)

An independent reviewer covered URL attributes, JavaScript and JSON, and the opt-in output checks. It listed every route a dependency URL takes to the page: the static render, render_json, the Events and browser_render descriptors, the policy, integrity mode, the cache round trip, copy/replace, Dependencies string entries, and citry-ui quality routes. It found no hole; bypassing the conversion is possible only from the app's own code (author-only).

The first round found that plain copying would stop decoding Markup URLs. That would have changed static output and gone against the documented attribute rule, so the change now decodes as described above. The Mechanism 8 check against the advisory survey (trusted-shape, xss-url, sanitizer-bypass) found that none of those kinds of bug is introduced. The prose pass findings are folded in.

docs/design/trust_boundaries.md rows are updated, and the changelog entry is under Unreleased.

Versioning

A patch release. This fixes input that used to fail, and output for every input that worked before is unchanged. No contract version or public format changes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01KUhrHaXagmW5ZxpRAMwyom

JuroOravec and others added 3 commits November 19, 2026 14:47
django-compressor passes its output URL as a Django SafeString. The
Script was accepted when built, but serialization then refused it with
"Prepared browser asset URL differs from its structured dependency URL",
because the check requires an exact str.

Dependency now stores a plain str copy of a str subclass url whenever
the field is set (construction, dataclasses.replace, or a later
assignment), using str.__str__ so no method the subclass overrides is
called. Every later check reads that copy: the escaping, the
security_javascript javascript: URL check, the serialization comparison
with the written src or href, and integrity pinning.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KUhrHaXagmW5ZxpRAMwyom
A value with __html__ is written into an attribute with its character
references decoded, so Markup("?a=1&amp;b=2") loads ?a=1&b=2. Store a
dependency url that way too, so its rendering stays as it was in 1.0.3
and the static page, the Events payload and security_javascript read
the same text. Adds an end-to-end Events producer test with Django's
SafeString, and rewords the changelog entry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KUhrHaXagmW5ZxpRAMwyom
@JuroOravec
JuroOravec merged commit 497da50 into main Nov 19, 2026
34 checks passed
@JuroOravec
JuroOravec deleted the fix/asset-url-str-subclass branch November 19, 2026 14:55
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