Repository navigation
Description
What happens
When security_csp or security_javascript is set, serialize() checks
the finished page and reports what it finds. Two parts of those reports
are wrong for any element that is not the outermost tag of a component.
The finding names the wrong owner. The check should name the
component that wrote the element. It names a component only when the
element itself carries that component's data-cid-* attribute, which
Citry puts on the component's outermost tags. Anything nested inside them
is reported as "settled render output":
from citry import Citry, Component
app = Citry(autodiscover=False, security_javascript="warn")
class Card(Component):
citry = app
template = """
<div>
<a href="javascript:void(0)">x</a>
</div>
"""
Card().render().serialize(deps_strategy="ignore")RuntimeWarning: security_javascript='warn' found 1 client behavior requirement(s):
- settled render output, attribute 'href', settled HTML bytes 40:58: a javascript: URL in 'href' remains executable browser code
With the <a> as the template's only tag, the same warning names Card.
On a page with many components, the reader has to find the element by its
byte offsets.
omit mode misreports a submit button inside a form. In
security_javascript="omit" mode, Citry leaves out the page's JavaScript
and warns about controls that then do nothing (the "handler-only control"
rule). A submit button inside a <form> still submits the form without
JavaScript, so it should pass. The check does not know that the button is
inside the form, so it warns:
app = Citry(autodiscover=False, security_javascript="omit")
class SaveForm(Component):
citry = app
template = """
<form action="/save" method="post">
<button @click="track()">Save</button>
</form>
"""
SaveForm().render().serialize(deps_strategy="ignore")RuntimeWarning: security_javascript='omit' found 1 client behavior requirement(s):
- settled render output, settled HTML bytes 62:68: this control has a browser handler but no native navigation or submission fallback
By the code, the same applies to <input>, <select> and <textarea>
inside a form (not run). Only a control with its own form="..."
attribute counts as inside one.
Both outputs were printed on main and on a build that contains #289
(perf/csp-checks-in-rust); they are the same.
Cause
Both rules need to know which elements enclose the current one, but the
check reads the page as a flat list of tags.
- On
main,scan_output_htmlin
packages/py/citry/citry/_output_html.py:60-80returns every tag as a
top-level node with an emptybody. The walkers in
_javascript_policy.pyand_csp_validation.pypass
inherited_componentandinherited_instance(and the JavaScript
walker alsoinherited_inside_form) down throughbody
(_javascript_policy.py:452-462), so those values never reach a
nested tag. The walk over the finished page starts with
inherited_inside_form=False(_javascript_policy.py:224). - On perf: run the strict CSP and JavaScript-policy checks in Rust (560 ms to 99 ms on a large page) #289's branch, the Rust check takes the owner only from the tag's own
attributes (component_ids,
crates/citry_html_transform/src/output_policy.rs:470), and Python picks
the first known one (own_instance,_output_policy.py:197).
is_handler_only_control(output_policy.rs:662-701) says in its doc
comment that it does not know whether a<form>encloses the control.
#289 kept this behavior on purpose, so that its test could show the Rust
check reports exactly what the Python check reported.
Who hits it
- Anyone who reads a
security_csporsecurity_javascriptreport on a
page where the reported element sits inside a component's outer tag,
which is likely most findings on a real page. - Anyone using
security_javascript="omit"with a handler on a submit
button or form field inside a<form>. The warning is wrong, and the
page works without JavaScript.
Fix direction
Build on #289. The Rust scanner already runs html5ever's tree builder
to read the page as the browser does, so the check can track which
elements are open while it reads the page. The open elements should come
from that tree builder, not from matching start and end tag names, because
implied end tags (<p>, <li>) and misnested tags make a hand-kept stack
differ from the browser's tree. For each tag, the check records the
nearest enclosing tag that carries a known data-cid-* attribute, and
whether a <form> is open around it. The check then passes both values
with each finding, and is_handler_only_control uses the form value
next to the form attribute.
#289 keeps a frozen copy of the Python rules in
tests/_output_policy_reference.py, and a test compares the Rust check
with it. That copy and the comparison test change in the same way. Add a
test for each of the two cases above.
Activity