Skip to content

test: fuzz and differential tests for request canonicalization (+3 fixes) - #27

Merged
chaodu-agent merged 4 commits into
mainfrom
test/canonicalization-fuzz
Nov 17, 2026
Merged

chaodu-agent merged 4 commits into
mainfrom
test/canonicalization-fuzz

Conversation

@chaodu-agent

Copy link
Copy Markdown
Contributor

Closes #19

What this adds

  • Fuzz targets (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 in fuzz/corpus/, one file per class in the issue.
  • One set of invariants (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 with http and url (the parser the forwarder builds the upstream URL with) and assert that an accepted input is never rewritten.
  • Differential tests: ap-core/tests/canon_differential.rs (fixed-seed generator, no new RNG dependency) and ap-proxy/src/canon_diff_tests.rs, which runs the real pipeline against the existing mock upstream and asserts that the path the policy matched (audit path) is byte-identical to the path the upstream received, and that every other request is refused with the upstream untouched.
  • Regression tables (ap-core/tests/canon_regression.rs) for each class in the issue.
  • Matrix rows: sidecar C1-1..C1-7 (41 -> 48), passthrough PT-18, PT-19 (16 -> 18). Row-count assertions updated.
  • CI (.github/workflows/fuzz.yml): 45 s per target on PRs touching canon.rs, the shared invariants or fuzz/; 300 s per target nightly; manual dispatch with a seconds input. ci.yml is 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-fix canon.rs crashes canon_host and canon_path within 60 s.

# Input Expected Actual before the fix Impact
1 host 127.0.0.0x1, r.0x.0xb (hex last label) refused as an IP spelling canonical_host accepted. The url crate reads 127.0.0.0x1 as 127.0.0.1, and inet_aton behind getaddrinfo does too The "no IP literal" rule for route hosts, egress allowlist entries and CONNECT targets had a hole. Not exploitable from a request: the egress allowlist and route hosts are operator config compared byte for byte, so an attacker cannot select such a host. An operator typo or a hostile config edit could have pointed a route at loopback. Defense in depth.
2 path /v1/..;/admin, /v1/.;/x; %255c, %2500, %25%32%66 refused as path_ambiguous accepted and forwarded raw (only %252e and %252f were covered) Policy bypass, conditional on the upstream. A prefix grant /v1/ covers /v1/..;/admin at the gate. An upstream that drops ;params (servlet containers) resolves it to /admin; an upstream that decodes twice reads %255c as 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.
3 path with non-ASCII bytes, {, }, " (also <, >, backtick) matched path equals forwarded path policy matched the raw bytes (/é), the url crate sent /%C3%A9 Violates the "forward exactly what was checked" principle. No bypass found: grant prefixes are ASCII and the rewrite happens inside a segment, so the prefix decision is the same.

Fixes in canon.rs: the numeric-TLD rule now covers 0x<hex>; path_ambiguous judges 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 400 path_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

  • hyper merges two equal Content-Length headers and drops Content-Length when Transfer-Encoding is present before our code runs, so framing_refused never sees those two cases. The matrix rows assert the observable property instead: refused, or forwarded with one framing header only. Different Content-Length values, obs-fold, bare CR, NUL and space-before-colon are refused by hyper (400).
  • canonical_host still accepts a malformed xn-- label that url refuses. Nothing is rewritten and allowlist matching is byte for byte, so the invariant allows this one disagreement.
  • The audit path field 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.
  • Sidecar matrix driver: 48 PASS of 48 (canonicalization 7/7). Passthrough matrix driver: 18 PASS of 18; cargo test ... --test matrix_passthrough passes. The --test matrix cargo target cannot find its binaries under this cargo's new build/<pkg>/<hash>/out layout (its bin_dir() assumes deps/); that is the environment, not this change, so I ran the same test executable from target/debug/deps/ and it passes.
  • Release build; hook greps (__ap_probe, __ap_test_panic, AP_TEST_UPSTREAM_CA, AP_TEST_EGRESS_RESOLVE) find nothing; ap-egress does not depend on ap-broker.
  • Fuzz, 300 s per target, from the committed seeds: canon_path 17,335,327 execs, canon_host 30,400,105, canon_origin 51,598,522, canon_headers 23,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.

…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.
@chaodu-agent

Copy link
Copy Markdown
Contributor Author

Round 2 of my own review of this PR, fixed in f956c1f.

Rule implemented. path_ambiguous now percent-decodes to a fixed point (hex in either case, by value, so %2E, %2%45 and layer-1 bytes that spell a new escape are all handled). Up to 4 layers; a path still decodable after that is refused. Refused: any byte a decode produces that is ., /, \, NUL or a control (any layer), or ; from layer 2 on; %u escapes; a decoded layer that is not valid UTF-8 (overlong %c0%ae, %e0%80%af, lone %ff); a . or .. segment, ignoring any ; parameter, in every layer (so ..%3b reads as ..;). Forwarded bytes are never rewritten.

Finding Status
MEDIUM: encoded ; after dot segments (..%3b, .%3b., ..%253b, ..%3b%2f, %2e%2e%3b) Fixed
MEDIUM: layered encodings with uppercase inner hex (%25%32%46, %25%32%45, %25%35%43, %2%45) Fixed (no text match on lowercased input any more)
LOW: decode bounded at 3 layers then fail-open Fixed (5-layer dot refused)
LOW: overlong UTF-8, %u002e, ..%3b Fixed
LOW: 502 exempt from "upstream untouched" assertion in the differential test Fixed: asserted for every non-200
LOW: passthrough pipeline not covered by the generator differential Skipped: both pipelines share path_ambiguous and upstream_url; the passthrough matrix rows (PT-18) are extended with the new forms instead

Tests. New table-driven regression cases (layered, mixed hex case, encoded NUL/controls, overlong/invalid UTF-8, encoded ; variants, ; in every segment passing); fuzz seeds; wider generator atoms; an independent decode-to-fixed-point oracle in the shared invariants used by both the fuzz target and the differential test (oracle demands refusal => path_ambiguous must refuse); extra sidecar C1-1/C1-2 and passthrough PT-18 wire rows. One old assertion that overlong %C0%AE passes was flipped to refused.

Verified on macmini: fmt, clippy (default, stage1-probe, matrix-upstream) with -D warnings, cargo test --workspace, stage1-probe lib tests, release hook greps and the ap-egress/ap-broker check, sidecar matrix 48/48, passthrough matrix 18/18 (row counts unchanged; new paths added inside existing rows), four fuzz targets 300 s each with 0 crashes. Accepted-input regression cases (%41, %C3%A9, 100%25, a;b, %2541) are unchanged. The --test matrix cargo wrapper still hits its existing build-dir lookup issue; its driver passes.

Behaviour note: paths with percent-encoded invalid UTF-8 (e.g. %ff) or encoded control bytes (%0a) now get path_ambiguous. Closes #19 kept.

@chaodu-agent
chaodu-agent merged commit 8a644ac into main Nov 17, 2026
5 of 6 checks passed
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.

Fuzz and differential tests for request canonicalization (parse_ap_path, host/path/header handling)

1 participant