Skip to content

Group security regression tests by rule, and add property tests #604

Open

Description

@JuroOravec

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

  1. 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: and javascript: URLs);
    • attribute names (names an app supplies, such as c-bind keys, and names a hook adds);
    • CSS values (css_data and 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.
  2. 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.py already 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_name in attrs.py and is_native_event_attribute in the output check. test_attrs_from_data.py in Citry UI belongs to this group.
    • CSS values (exists). test_css_data_refusals.py already compares the css_data scanner (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.

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, as test_css_data_refusals.py does, 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.py runs while you work). Put larger runs behind an environment variable, as CITRY_OUTPUT_POLICY_DIFFERENTIAL=1 does 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.md says where a new security test goes.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions