Skip to content

feat: judge one template attribute by the strict-CSP output rules - #471

Merged
JuroOravec merged 1 commit into
mainfrom
feat/csp-attribute-problem
Oct 28, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
feat/csp-attribute-problem

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

What this does

Part of #181. When an app sets security_csp="warn" or "strict", citry check and 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.

from citry_core.html_transform import _csp_attribute_problem

_csp_attribute_problem("button", "onclick", "go()")       # "native-event"
_csp_attribute_problem("a", "href", "javascript:x")  # "javascript-url"
_csp_attribute_problem("a", "href", "/home")              # None
  • "native-event": a name longer than two characters that starts with on. This keeps the page check's prefix rule (option A in the design), so a custom name such as online matches too.
  • "javascript-url": action, formaction, href, src or xlink:href, or data on <object>, whose value starts with javascript: once its character references are decoded.
  • raw_value is the value as written in the template, or None for an attribute with no value. Tag and attribute names are compared without ASCII case.
  • One attribute has at most one problem, because no URL attribute name starts with on. A test checks that.
  • It never raises. A lone surrogate reads as U+FFFD, which never makes a match on its own.

The function is private (_ prefix), like the other output-check helpers that only citry calls, so it adds no public citry_core API and needs no changelog entry. The lint rule that walks templates and reports citry.template.csp-incompatible comes in a separate PR, and it carries the user-facing changelog entry.

Prior art

  • The rules already exist in Rust, in crates/citry_html_transform/src/output_policy.rs: is_native_event_attribute, is_url_attribute, is_javascript_url and decode_html_attribute. check_output_html runs 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.rs already binds two close relatives: _classify_dependency_attribute, which judges one attribute of a structured Script or Style but does not decode references, and _is_javascript_url. The new binding follows their pattern: private name, lossy string reading, and a re-export in citry_core/html_transform/__init__.py outside __all__.
  • The design note for Show CSP problems in the editor and citry check before a page renders #181 rejected two alternatives. Rebuilding an HTML string from the template and running check_output_html on 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.
  • PyO3: _csp_attribute_problem is registered in crates/citry_core_py/src/lib.rs.
  • _rust.pyi: a stub returning Literal["native-event", "javascript-url"] | None.
  • Python wrapper: citry_core/html_transform/__init__.py re-exports it as a private name.
  • Tests:
    • Rust: each case goes through both the new function and 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, data on <object> only, and value-less attributes.
    • Python: test_html_transformer.py makes the same comparison through the binding, and tests the lone surrogate case.
  • Agent docs: crates/citry_core_py/AGENTS.md lists 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, a native-event finding marks the attribute name, and a javascript-url finding marks the value.

Checklist

  • Tests added
  • CHANGELOG.md: not needed, the function is private
  • Docs: none for users; AGENTS.md updated
  • Cross-language contract stays in step (see above)

Local evidence on macOS arm64, Python 3.14.9, debug build: cargo fmt --check, cargo clippy -D warnings on citry_html_transform and citry_core_py, cargo test -p citry_html_transform (50 passed in the library tests), test_html_transformer.py (24 passed), ruff, and mypy on citry_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

`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
JuroOravec merged commit 73e94cb into main Oct 28, 2026
34 of 38 checks passed
@JuroOravec
JuroOravec deleted the feat/csp-attribute-problem branch October 28, 2026 11:27
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]>
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