Repository navigation
Open
Description
The problem
A security regression test sits next to the change that added it. To see everything that proves one rule, such as which URL schemes a link or a redirect may carry, you search several places: test_redirect_schemes.py and redirect-scheme.test.mjs for redirects, test_url_schemes.py in Citry UI for component links, and the Rust cases in the output check for finished pages. A new input that needs the same rule gets a test only if its author knows every one of those places.
What we want
- Group the tests by rule. For each rule, one place that lists every function applying it and the tests that prove it: a folder or test module per rule, or one table of cases that each test runs against every function that applies the rule. Start with:
- URL schemes (links, forms, redirects,
data:andjavascript:URLs); - attribute names (names an app supplies, such as
c-bindkeys, and names a hook adds); - CSS values (
css_dataand the code that reads it); - tags the browser reads as raw text or code (
script,style,iframe,xmp, and the other tags in the output check's list); - request and size limits on routes.
- URL schemes (links, forms, redirects,
- Add and extend property tests. A property test generates many inputs and checks that a rule which must always hold still holds. Three rules read untrusted text with their own code, and a hand-written list of cases leaves gaps there:
- URL schemes (extend).
test_redirect_schemes.pyalready compares the redirect reader with a reference on seeded random URLs. Extend that to every function that judges a scheme, with the spellings browsers accept (letter case, tabs and line breaks the browser removes, character references, leading spaces and control characters). - Attribute names (new). Generate names with white space, control characters, quotes, byte-order marks, lone surrogates, and upper- and lower-case spellings. Compare each function that judges an attribute name with one reference rule. The plan lists those functions; today they include
validate_html_attr_nameinattrs.pyandis_native_event_attributein the output check.test_attrs_from_data.pyin Citry UI belongs to this group. - CSS values (exists).
test_css_data_refusals.pyalready compares thecss_datascanner (the code that reads a value character by character) with a reference tokenizer (a slow, simple copy of the rule used to check the fast one) on seeded random values. Move its generator to a shared helper so other tests can use it.
- URL schemes (extend).
Cost and scope
- The tests protect against a later change that loosens a check without anyone noticing. They run on functions only, with no browser and no network.
- Keep a short list of fixed cases that pins each reference function, as the CSS test does, so a wrong reference fails loudly.
- Use seeded
random.Random, astest_css_data_refusals.pydoes, so a failure repeats. Add Hypothesis (a library that generates inputs and shrinks a failing one) only if shrinking proves worth a new test dependency. - Keep each property test to a few seconds in the fast profile (the quick set of checks that
scripts/check.pyruns while you work). Put larger runs behind an environment variable, asCITRY_OUTPUT_POLICY_DIFFERENTIAL=1does for the output-check comparison. - When a rule already has a test in the place that owns the rule, move or link that test instead of writing a second one. Do not repeat a proof at every layer.
Done when
- Each rule above has one place that names the functions applying it and the tests for them.
- The attribute-name property test exists, the scheme test covers every function that judges a scheme, and the CSS generator is shared. Each runs within the time budget and fails when its check is made looser (try one change by hand per rule).
-
docs/codebase.mdsays where a new security test goes.
Metadata
Metadata
Assignees
Labels
No labels
Activity