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.
This commit is contained in:
Loki 2026-11-16 12:47:25 +01:00
parent da87d2a11f
commit 08b7292ac4
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
289 changed files with 4837 additions and 31 deletions

3781
fuzz/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

29
fuzz/Cargo.toml Normal file
View file

@ -0,0 +1,29 @@
[package]
name = "rampart-fuzz"
version = "0.0.0"
publish = false
edition = "2024"
[package.metadata]
cargo-fuzz = true
[dependencies]
libfuzzer-sys = "0.4"
rampart = { path = "..", default-features = false, features = ["protocol-http", "protocol-minecraft"] }
[[bin]]
name = "http_head"
path = "fuzz_targets/http_head.rs"
test = false
doc = false
bench = false
[[bin]]
name = "proxy_v2"
path = "fuzz_targets/proxy_v2.rs"
test = false
doc = false
bench = false
# Isolate the fuzz crate from the parent package.
[workspace]

View file

@ -0,0 +1,30 @@
#![no_main]
//! Fuzz target for the HTTP/1.x request-head parser.
//!
//! `scan_head` and `evaluate` consume attacker-controlled bytes on every
//! connection, so they must never panic and must stay within the buffer.
use libfuzzer_sys::fuzz_target;
use rampart::protocol::http::parser::{Policy, evaluate, scan_head};
fuzz_target!(|data: &[u8]| {
if let Some(head_len) = scan_head(data) {
assert!(head_len <= data.len(), "scan_head returned an out-of-bounds length");
let policy = Policy {
max_header_bytes: 8192,
blocked_paths: vec!["/admin".to_string()],
require_user_agent: true,
};
let _ = evaluate(&data[..head_len], &policy);
}
// The whole buffer (not only a terminated head) must also be safe: a
// truncated head is evaluated defensively by callers.
let policy = Policy {
max_header_bytes: 8192,
blocked_paths: vec![],
require_user_agent: false,
};
let _ = evaluate(data, &policy);
});

View file

@ -0,0 +1,25 @@
#![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"
);
}
});

View file

@ -0,0 +1,3 @@
GET / HTTP/1.1
ost:om
User-Agent: Accep

View file

@ -0,0 +1,2 @@
GGET / HTTP/1.1
Hc G

View file

@ -0,0 +1,17 @@
GET / HTTP/1.1
H-ot-t:
:
H-ot-t:
.1
:
1 */*1
A
Co-t:
.1
H-ot:
!
eLe-

View file

@ -0,0 +1,4 @@
GET / HTTP/1.1
e.co:
ept:

View file

@ -0,0 +1,3 @@
GET / HTTP/1.1
Ho]t:t

View file

@ -0,0 +1,2 @@
POST /#dQmin HTTP/1.1
Hosv:

View file

@ -0,0 +1,2 @@
POPOST in HTTP/1.1
Hot

View file

@ -0,0 +1,2 @@
GE / HTTP/1.1
exa

View file

@ -0,0 +1 @@
T / H`TP /*

View file

@ -0,0 +1,6 @@
GET , HTTP/1.1
Hosr-AgJnt: fuzz
Act: example.com
User-AgJnt: fuzz
Accept: */*

View file

@ -0,0 +1,3 @@
GET / HTTP/1.1
Ho]t:
󷐢(0.1

View file

@ -0,0 +1,36 @@
GET / HTTP/1.1
H-ot-t:
.1
:

1
H-ot-t:
.1
:
1 */*1
A
Co-t:
.1
H-o :
1t:
.11
H-ot-t:
.1
:
1 */*1
A
Co-t:
.1
H-o :
1t:
.1
:

1
H-ot-t:
.1
:
1 */*1
eLe-

View file

@ -0,0 +1,3 @@
GET / HTTP/1.1
H,G:T
UsermgJAKt:

View file

@ -0,0 +1,5 @@
GET / HTTP/1.1
Host:: ex /0HTTP/1.1TP/1.1
Host: example.com
User-Agent: f

View file

@ -0,0 +1,5 @@
GET / HTTP/1.1
Host: example.com
User-AgJnt: fuzz
Accept: */*
/

View file

@ -0,0 +1,6 @@
GET /1 HTTP/1.1
osHt:
AcceTTP/1.1
osHt:
AccTTP1.

View file

@ -0,0 +1,2 @@
G)T / HTTP/1.1
H

View file

@ -0,0 +1,3 @@
GET , HTTP/1.1
Hosr-AgJot:

View file

@ -0,0 +1,3 @@
GET / HTTP/1.1
Ho-t:
AcceT

View file

@ -0,0 +1,3 @@
GET / HTTP/1.1
H1.t:יuzzept:*/*

View file

@ -0,0 +1,3 @@
GET / HTTP/1.1
*/*

View file

@ -0,0 +1,3 @@
GET / HTTP/1.1
Host: exGET / HT
User-Agent:

View file

@ -0,0 +1,13 @@
GET / HTTP/1.1
H-ot:
Act*/*1
H
Co-t:*/*1
H
:
AP/1.1
H-o:
0
e

View file

@ -0,0 +1,3 @@
GET , HTTP/1.1
HHot:z 

View file

@ -0,0 +1,4 @@
POST /admin HTTP/1.1
H
Cgth: 0

View file

@ -0,0 +1,2 @@
POST n HTTP/1.1
H[to:sTP1 Host

View file

@ -0,0 +1,2 @@
.At

View file

@ -0,0 +1,2 @@
P qi HTTP/1.0

View file

@ -0,0 +1,4 @@
GET / HTTP/1.1
Host:
Acce

View file

@ -0,0 +1,9 @@
GET / HTTP/1.1
H-ot:
/*1
H
Co-t:*/*1
-t:/1.1
H-ot:0555
e

View file

@ -0,0 +1,4 @@
GET / HTTP/1.1
st:pl
UsermgJAKt:*

View file

@ -0,0 +1,3 @@
GET / HTTP/1.1
sx:mpl/c

View file

@ -0,0 +1,9 @@
GET /1 HTTP/1.1
osHt:TT1
osHt:
AcceTTP/1.1
osHt:
AccP/1.1
osHt:
AccTTP/1.1

View file

@ -0,0 +1,3 @@
GET / HTTP/1.1
ost: excom
User-Ageot: fPOTep

View file

@ -0,0 +1,7 @@
GET / HTTP/1.1
H-ot:
AcGGG/1.1
Host:
Ac Ggetx: 0
eLe

View file

@ -0,0 +1,2 @@
GET /' HTTP/1.1
zost:

View file

@ -0,0 +1,4 @@
POST HTTP/1.1
Host: x: x
Co: 0

View file

@ -0,0 +1,4 @@
GET UUUUW/ HTTP/1.1
Host:Acce
Host:AccepHTTP.1
Host:Accep

View file

@ -0,0 +1,5 @@
GET / HTTP/1.1
Host: eampl
UsermgJAKt: fl
UsermgJAKt: fuzz : */*

View file

@ -0,0 +1,5 @@
GET / HTTP/1.1
Host:xa.com
User-AgJnt: fuzz
Accept: */*

View file

@ -0,0 +1,8 @@
GET / HTTP/1.1
Host:
: f
GET / HTTP/1.1
H
o]t:
󷐢(0.1

View file

@ -0,0 +1,4 @@
n HTTP/1.0
Hsx ov
C

View file

@ -0,0 +1 @@
P

View file

@ -0,0 +1,5 @@
GET / HTTP/1.1
H-ot:
Accep :t*/*1 h: 0
eLe

Some files were not shown because too many files have changed in this diff Show more