Repository navigation
test: fuzz and differential tests for request canonicalization (+3 fixes) - #27
Conversation
…s (issue #19) Adds, with no change to production code: - fuzz/ (cargo-fuzz, outside the root workspace): canon_path, canon_host, canon_origin and canon_headers, with a small seed corpus. The invariants live in crates/ap-core/tests/common/invariants.rs and are shared with the cargo tests. - crates/ap-core/tests/canon_differential.rs: fixed-seed generator over the issue classes, checked against the http and url parsers. - crates/ap-core/tests/canon_regression.rs: table-driven cases for every class in the issue. - crates/ap-proxy/src/canon_diff_tests.rs: pipeline test with the mock upstream, asserting the policy-matched path is byte-identical to the path the upstream receives. - Matrix rows C1-1..C1-7 (sidecar, 41 -> 48) and PT-18, PT-19 (passthrough, 16 -> 18). Known failing at this commit, on purpose (the fix is the next commit): the tests reproduce three findings: (1) canonical_host accepts a hex last label such as 127.0.0.0x1, which url reads as IPv4; (2) path_ambiguous accepts ..;x dot segments and %255c, %2500, %25%32%66; (3) paths with non-ASCII bytes, braces or a double quote are matched raw but forwarded percent-encoded. Run on macmini: cargo test -p ap-core --tests fails generated_paths, generated_hosts.
1. canonical_host accepted a hex last label (127.0.0.0x1, a.0x7f). The url crate and
inet_aton read these as IPv4. The numeric-TLD rule now covers 0x<hex>.
2. path_ambiguous accepted dot segments carrying a ; parameter (/v1/..;/admin, read as .. by
servlet containers) and double encodings other than %252e/%252f (%255c, %2500, %25%32%66).
Dot segments are now judged on the part before ;, and percent-decoding is applied up to
three times looking for %2e %2f %5c %00.
3. Paths with non-ASCII bytes, DEL, space, " < > backtick { } were matched raw but sent
percent-encoded by the url crate, so the matched path was not the forwarded path. They are
now refused as path_ambiguous.
Also: invariants.rs skips paths that do not start with / (url maps an empty path to /); the
seed literal is grouped for clippy; PT-06 needle /ap/ becomes /ap/apk_ so a repeated prefix in
a forwarded path is not read as a leaked key (real keys stay needles of their own).
Verified on macmini: cargo test -p ap-core --tests and ap-proxy canon_diff pass; sidecar
matrix 48/48, passthrough matrix 18/18; fmt and clippy clean.
Fuzz run on macmini (nightly 1.102, cargo-fuzz 0.13.2, 300 s per target, seeds from fuzz/corpus): canon_path 17,335,327 execs, canon_host 30,400,105, canon_origin 51,598,522, canon_headers 23,182,834; 0 crashes. Against the pre-fix canon.rs from an empty corpus, canon_host and canon_path each crash within 60 s.
… encoded ';', overlong UTF-8) Review round 2 on PR 27. path_ambiguous now percent-decodes layer by layer to a fixed point (max 4, still decodable => refuse) and refuses bytes produced by a decode that are . / \ NUL controls (any layer) or ; (layer 2+), %u escapes, invalid/overlong UTF-8, and dot segments (before any ;) in every layer. Adds regression tables, fuzz seeds, an independent decode oracle in the shared invariants, wider generator atoms, matrix rows, and asserts no upstream hit on every non-200 in the real-forwarder differential. Verified on macmini: fmt, clippy (default, stage1-probe, matrix-upstream), cargo test --workspace, stage1-probe lib (75), release hook greps, sidecar matrix 48/48, passthrough matrix 18/18, four fuzz targets 300s each, 0 crashes.
|
Round 2 of my own review of this PR, fixed in f956c1f. Rule implemented.
Tests. New table-driven regression cases (layered, mixed hex case, encoded NUL/controls, overlong/invalid UTF-8, encoded Verified on macmini: fmt, clippy (default, stage1-probe, matrix-upstream) with Behaviour note: paths with percent-encoded invalid UTF-8 (e.g. |
Closes #19
What this adds
fuzz/, cargo-fuzz, outside the root workspace so default builds are unchanged):canon_path(incl. the/ap/<key>/<route>/...form),canon_host,canon_origin(request target + Host),canon_headers(caller key, surrogate, framing). Small hand-written seed corpus infuzz/corpus/, one file per class in the issue.crates/ap-core/tests/common/invariants.rs) shared by the fuzz targets and the cargo tests, so a fuzz crash and a test failure name the same input. They compare our verdict withhttpandurl(the parser the forwarder builds the upstream URL with) and assert that an accepted input is never rewritten.ap-core/tests/canon_differential.rs(fixed-seed generator, no new RNG dependency) andap-proxy/src/canon_diff_tests.rs, which runs the real pipeline against the existing mock upstream and asserts that the path the policy matched (auditpath) is byte-identical to the path the upstream received, and that every other request is refused with the upstream untouched.ap-core/tests/canon_regression.rs) for each class in the issue..github/workflows/fuzz.yml): 45 s per target on PRs touchingcanon.rs, the shared invariants orfuzz/; 300 s per target nightly; manual dispatch with a seconds input.ci.ymlis untouched and stays manual.Findings (three real gaps, fixed in a separate commit, tests first)
Commit 1 adds the tests and is red on purpose; commit 2 (
fix:) turns it green. Each was found by the differential test or the fuzz target; from an empty corpus the pre-fixcanon.rscrashescanon_hostandcanon_pathwithin 60 s.127.0.0.0x1,r.0x.0xb(hex last label)canonical_hostaccepted. Theurlcrate reads127.0.0.0x1as 127.0.0.1, andinet_atonbehind getaddrinfo does too/v1/..;/admin,/v1/.;/x;%255c,%2500,%25%32%66path_ambiguous%252eand%252fwere covered)/v1/covers/v1/..;/adminat the gate. An upstream that drops;params(servlet containers) resolves it to/admin; an upstream that decodes twice reads%255cas a backslash. None of the configured upstreams is known to do either, so this is latent, not demonstrated against a real upstream. No route/key confusion or header smuggling found.{,},"(also<,>, backtick)/é), theurlcrate sent/%C3%A9Fixes in
canon.rs: the numeric-TLD rule now covers0x<hex>;path_ambiguousjudges dot segments on the part before;, decodes up to three times looking for%2e %2f %5c %00, and refuses bytes the forwarder would re-encode.Behaviour change for reviewers to veto: a request whose path carries raw non-ASCII bytes,
{ } " < > \`` now gets 400path_ambiguous` instead of being forwarded percent-encoded. Clients that percent-encode (as RFC 3986 requires) are unaffected. The alternative (forward the encoded form and match on it) is a larger change and I did not take it.Observed, not changed
Content-Lengthheaders and dropsContent-LengthwhenTransfer-Encodingis present before our code runs, soframing_refusednever sees those two cases. The matrix rows assert the observable property instead: refused, or forwarded with one framing header only. DifferentContent-Lengthvalues, obs-fold, bare CR, NUL and space-before-colon are refused by hyper (400).canonical_hoststill accepts a malformedxn--label thaturlrefuses. Nothing is rewritten and allowlist matching is byte for byte, so the invariant allows this one disagreement.pathfield is request data: a key-shaped string an agent places in a later path segment is logged as is. The passthrough matrix row PT-06 had a needle/ap/; it is now/ap/apk_so a legitimately repeated/ap/prefix in a forwarded path is not read as a leaked key (real keys remain needles of their own)./ap/<k>/<r>and/ap/<k>/<r>/both give route path/.Verification (macmini, stable 1.100.0; nightly 1.102 + cargo-fuzz 0.13.2 installed there for this, they were absent)
cargo fmt --all --check,cargo clippy --workspace --all-targets -- -D warnings,cargo clippy -p ap-proxy --all-targets --features stage1-probe -- -D warnings: clean.cargo test --workspace: all pass (ap-core 177 unit + differential 4 + regression 12, ap-proxy lib 70).cargo test -p ap-proxy --features stage1-probe --lib: 75 pass.cargo test ... --test matrix_passthroughpasses. The--test matrixcargo target cannot find its binaries under this cargo's newbuild/<pkg>/<hash>/outlayout (itsbin_dir()assumesdeps/); that is the environment, not this change, so I ran the same test executable fromtarget/debug/deps/and it passes.__ap_probe,__ap_test_panic,AP_TEST_UPSTREAM_CA,AP_TEST_EGRESS_RESOLVE) find nothing; ap-egress does not depend on ap-broker.canon_path17,335,327 execs,canon_host30,400,105,canon_origin51,598,522,canon_headers23,182,834. 0 crashes after the fix.The fuzz workflow is new and has not run on GitHub yet; its first run is this PR's checks.