Repository navigation
feat: judge one template attribute by the strict-CSP output rules - #471
Merged
Merged
Conversation
`citry check` and the editor will report CSP problems in a template's own text (#181), and must agree with the check that runs on the finished page. csp_attribute_problem() judges one attribute as written: a name longer than two characters that starts with `on` is a native event handler, and a URL attribute whose value, after character references are decoded, starts with `javascript:` is a javascript: URL. It calls the same predicates and decoder as check_output_html(), so there is one copy of the rules. - citry_html_transform gains csp_attribute_problem() and CspAttributeProblem. A test runs each case through the new function and through the page check and requires the same answer, including references, a tab inside the scheme, uppercase names and tags, every URL attribute, `data` on `<object>` only, and value-less attributes. Another test checks that no URL attribute name starts with `on`, which is why one attribute has at most one problem. - citry_core exposes it as the private html_transform._csp_attribute_problem(tag, name, raw_value), which returns "native-event", "javascript-url" or None, with its `_rust.pyi` stub, the citry_core_py AGENTS.md entry, and Python tests that compare it with _check_output_html. The lint rule that walks templates and reports citry.template.csp-incompatible comes in a separate change. Part of #181. Co-Authored-By: Claude Opus 5.5 <[email protected]>
JuroOravec
added a commit
that referenced
this pull request
Oct 28, 2026
…editor With security_csp set to "warn" or "strict", citry check and the editor now report what a strict Content Security Policy blocks, at the place a template writes it: an inline <script> or <style>, an on* attribute such as onclick, and a javascript: URL. "warn" gives warnings and "strict" errors; with the default "off" nothing is reported. Each fixed attribute goes through _csp_attribute_problem from #471, which uses the render-time check's own predicates and decoder, and <c-raw> content goes through the page check, so there is one copy of the rules. The engine's security_csp reaches both surfaces through TemplateLintInfo, which leaves the field out while it is off. A template shared by several components gets its strictest owner's severity. The check reads only fixed text. Citry bindings and HTML passed to a component in an attribute stay with the render-time check. Vue bindings, attributes on component tags, and content the browser reads as text (<noscript>, <iframe>, <xmp>, <noembed>, <noframes>, <plaintext>) are skipped. v-pre regions are checked, because Vue still sets the attributes it leaves as written, and <c-element is="..."> is checked as the element it names. The new code citry.template.csp-incompatible is off by default. Tests compare the static rule with the render-time check on about 45 snippets and record the deliberate differences. Fixes #181 Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
What this does
Part of #181. When an app sets
security_csp="warn"or"strict",citry checkand the editor will report CSP problems in a template's own text, before a page renders. Those reports must agree with the check that runs on the finished page. This PR adds the one piece that needs Rust: a function that judges a single attribute by the same rules the page check uses."native-event": a name longer than two characters that starts withon. This keeps the page check's prefix rule (option A in the design), so a custom name such asonlinematches too."javascript-url":action,formaction,href,srcorxlink:href, ordataon<object>, whose value starts withjavascript:once its character references are decoded.raw_valueis the value as written in the template, orNonefor an attribute with no value. Tag and attribute names are compared without ASCII case.on. A test checks that.The function is private (
_prefix), like the other output-check helpers that onlycitrycalls, so it adds no publiccitry_coreAPI and needs no changelog entry. The lint rule that walks templates and reportscitry.template.csp-incompatiblecomes in a separate PR, and it carries the user-facing changelog entry.Prior art
crates/citry_html_transform/src/output_policy.rs:is_native_event_attribute,is_url_attribute,is_javascript_urlanddecode_html_attribute.check_output_htmlruns them over a whole page (perf: run the strict CSP and JavaScript-policy checks in Rust (560 ms to 99 ms on a large page) #289), and the new function calls the same four, so there is no second copy of the rules.crates/citry_core_py/src/html_transform.rsalready binds two close relatives:_classify_dependency_attribute, which judges one attribute of a structuredScriptorStylebut does not decode references, and_is_javascript_url. The new binding follows their pattern: private name, lossy string reading, and a re-export incitry_core/html_transform/__init__.pyoutside__all__.check_output_htmlon it makes byte ranges hard to map back. Copying the predicates to Python would create a second copy of the rules.What else had to change (cross-language check)
src/lang/*.rs(LangImpl): not involved. This is the HTML output check, not the template compiler._csp_attribute_problemis registered incrates/citry_core_py/src/lib.rs._rust.pyi: a stub returningLiteral["native-event", "javascript-url"] | None.citry_core/html_transform/__init__.pyre-exports it as a private name.check_output_html, and the two must give the same answer. The cases cover references, a tab inside the scheme, uppercase names and tags, every URL attribute,dataon<object>only, and value-less attributes.test_html_transformer.pymakes the same comparison through the binding, and tests the lone surrogate case.crates/citry_core_py/AGENTS.mdlists the new private helper.Notes for the lint rule
The function agrees with the page check one attribute at a time. The lint rule still decides which attributes the browser reads as attributes at all. The page check reads no tags inside
<noscript>,<xmp>,<iframe>,<noembed>or<noframes>, but it does read an<a onclick>inside an SVG<title>, which the template parser treats as text. To point at the same text as the page check, anative-eventfinding marks the attribute name, and ajavascript-urlfinding marks the value.Checklist
CHANGELOG.md: not needed, the function is privateAGENTS.mdupdatedLocal evidence on macOS arm64, Python 3.14.9, debug build:
cargo fmt --check,cargo clippy -D warningsoncitry_html_transformandcitry_core_py,cargo test -p citry_html_transform(50 passed in the library tests),test_html_transformer.py(24 passed), ruff, and mypy oncitry_core. An independent review compared the function with the page check on about 50 rendered templates, including unquoted, single-quoted and multi-line values, and found no difference for any attribute the template parser accepts.🤖 Generated with Claude Code