guard/fuzz/fuzz_targets/proxy_v2.rs
loki5512344 08b7292ac4
test: fuzz the HTTP and PROXY v2 parsers, make PROXY parsing pure
- PROXY v2 header parsing is now the pure, total function
  `parse_v2_header(buf, limit) -> (SocketAddr, consumed)`; the async reader
  only reads bytes and delegates. This makes the parser reachable from tests
  and fuzz targets and keeps every index bounds-checked.
- tests/proxy_v2_parser.rs: valid v4/v6 headers, bad signature, non-PROXY
  command, TLVs, every truncated prefix, oversized header, unsupported
  family, short address block, and a deterministic sweep over family/length.
- fuzz/: cargo-fuzz targets `http_head` (scan_head + evaluate) and `proxy_v2`
  with invariants (no panic, no out-of-buffer consumption, family matches the
  header). Seeds for the HTTP head keep coverage useful; `make fuzz` runs
  60 s per target. Both targets ran clean (7M and 3M executions).
- docs/testing.md documents the fuzz workflow; .gitignore covers the
  generated corpus/artifacts.
2026-11-16 12:47:25 +01:00

25 lines
928 B
Rust

#![no_main]
//! Fuzz target for the inbound PROXY protocol v2 header parser.
//!
//! The parser runs before authentication on a connection controlled by a
//! trusted proxy, so arbitrary bytes must produce an error, never a panic or
//! an out-of-bounds read.
use libfuzzer_sys::fuzz_target;
use rampart::engine::tunnel::parse_v2_header;
fuzz_target!(|data: &[u8]| {
const LIMIT: usize = 512;
if let Ok((client, consumed)) = parse_v2_header(data, LIMIT) {
assert!(consumed <= data.len(), "parser consumed more than the buffer");
assert!(consumed <= LIMIT, "parser accepted a header above the limit");
assert!(consumed >= 16, "a parsed header is at least the fixed part");
// The address must be in the family advertised by the header byte.
let family = data[13];
assert!(
(family == 0x11 && client.is_ipv4()) || (family == 0x21 && client.is_ipv6()),
"address family does not match the header"
);
}
});