Repository navigation
fix(deps): accept a str subclass URL such as SafeString in Script and Style - #799
Merged
Merged
Conversation
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&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
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)
What
Script(url=...)andStyle(url=...)now accept astrsubclass, such as the DjangoSafeStringURL that django-compressor (and citry-django-compressor) produce.Before, the
Scriptwas 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_descriptorneeds an exactstr.How
Dependency.__setattr__(incitry/ext/dependencies/types.py) turns astr-subclassurlinto a plainstrwhenever the field is set: at construction, throughdataclasses.replace(), and when a hook assigns a new URL later. The conversion is done by_plain_url_text:__html__is copied withexact_str, so no method the subclass overrides (__eq__,startswith,lower,__str__) is called.__html__(Markup,SafeString) is decoded the wayescape_attribute_valuealready writes it into an attribute. SoMarkup("/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 undersecurity_javascript, the serialization check that the writtensrc/hrefmatchesurl, and the integrity pinning. The safe mark is not trusted. APathor other non-strvalue keeps its current behavior.The serialize-time mismatch check is unchanged and still refuses a real mismatch, including a subclass URL that skips
__setattr__throughobject.__setattr__.Out of scope:
contentandattrsvalues. They are author-only (app code), andattrsvalues already follow the decode-when-written rule. ASafeStringinattrs["type"],attrs["integrity"]or a nonce is still refused on the Vue path, and so is a lazy (non-str) URL such asreverse_lazy(...).Tests
11 new tests. 9 fail on
mainand pass here; the 2 mismatch guards pass on both.tests/test_deps_types.py::TestUrlStrSubclass:SafeStringURL serializes (Script and Style).replace()and after a later assignment.__eq__,__str__,startswithandlowercannot hide ajavascript:URL fromsecurity_javascript="forbid".MarkupURL with&is stored, rendered and serialized as the browser reads it.Markup("javascript:...")is still refused._renderand for a subclass that bypassed the conversion.tests/test_vue_events.py: aSafeStringScript and Style in aDependenciesclass reach the Events payload throughdefault_events_produceras plainstr.Checks run against citry-core 2.1.2 from PyPI:
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_subclassandtest_contrib_django.packages/py/citry/citryis 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 andbrowser_renderdescriptors, the policy, integrity mode, the cache round trip,copy/replace,Dependenciesstring 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
MarkupURLs. 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.mdrows 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