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:
parent
da87d2a11f
commit
08b7292ac4
289 changed files with 4837 additions and 31 deletions
|
|
@ -22,7 +22,7 @@ mod gateway;
|
|||
#[cfg(feature = "protocol-minecraft")]
|
||||
mod proxy;
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
pub use proxy::{ClientIpRejection, validate_client_ip};
|
||||
pub use proxy::{ClientIpRejection, parse_v2_header, validate_client_ip};
|
||||
|
||||
/// Multiplier applied to the per-IP limit when sizing the global connection
|
||||
/// budget: `4 x workers x max_connections_per_ip` in-flight connections.
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@ use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
|||
use std::time::Duration;
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
mod inbound;
|
||||
pub mod inbound;
|
||||
pub use inbound::parse_v2_header;
|
||||
pub(super) use inbound::read_v2_header;
|
||||
|
||||
/// Reason a PROXY header client address is rejected as an identity.
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@ use tokio::io::AsyncReadExt;
|
|||
use tokio::net::TcpStream;
|
||||
|
||||
const SIGNATURE: [u8; 12] = *b"\r\n\r\n\0\r\nQUIT\n";
|
||||
const FIXED_HEADER_BYTES: usize = 16;
|
||||
/// Length of the fixed part that precedes the address block.
|
||||
pub const FIXED_HEADER_BYTES: usize = 16;
|
||||
|
||||
/// Reads and validates an inbound PROXY protocol v2 header.
|
||||
///
|
||||
|
|
@ -26,17 +27,10 @@ pub(crate) async fn read_v2_header(
|
|||
}
|
||||
|
||||
async fn read_v2_header_inner(stream: &mut TcpStream, max_header_bytes: usize) -> anyhow::Result<SocketAddr> {
|
||||
let mut fixed = [0u8; FIXED_HEADER_BYTES];
|
||||
stream.read_exact(&mut fixed).await?;
|
||||
let mut header = vec![0u8; FIXED_HEADER_BYTES];
|
||||
stream.read_exact(&mut header).await?;
|
||||
|
||||
if fixed[..12] != SIGNATURE {
|
||||
anyhow::bail!("invalid inbound PROXY protocol signature");
|
||||
}
|
||||
if fixed[12] != 0x21 {
|
||||
anyhow::bail!("inbound PROXY protocol requires version 2 PROXY command");
|
||||
}
|
||||
|
||||
let payload_len = usize::from(u16::from_be_bytes([fixed[14], fixed[15]]));
|
||||
let payload_len = usize::from(u16::from_be_bytes([header[14], header[15]]));
|
||||
let total_len = FIXED_HEADER_BYTES
|
||||
.checked_add(payload_len)
|
||||
.ok_or_else(|| anyhow::anyhow!("inbound PROXY header length overflow"))?;
|
||||
|
|
@ -44,7 +38,44 @@ async fn read_v2_header_inner(stream: &mut TcpStream, max_header_bytes: usize) -
|
|||
anyhow::bail!("inbound PROXY header exceeds configured limit");
|
||||
}
|
||||
|
||||
let address_len = match fixed[13] {
|
||||
header.resize(total_len, 0);
|
||||
stream.read_exact(&mut header[FIXED_HEADER_BYTES..]).await?;
|
||||
let (client, consumed) = parse_v2_header(&header, max_header_bytes)?;
|
||||
debug_assert_eq!(consumed, total_len, "parser must consume exactly one header");
|
||||
Ok(client)
|
||||
}
|
||||
|
||||
/// Parses a complete inbound PROXY v2 header from `buf`.
|
||||
///
|
||||
/// Pure and total: every read is bounds-checked, so arbitrary bytes can only
|
||||
/// produce an error, never a panic (covered by the `proxy_v2` fuzz target).
|
||||
///
|
||||
/// # Errors
|
||||
/// Fails on a short buffer, bad signature or command, unsupported address
|
||||
/// family, TLVs, and on a header larger than `max_header_bytes`.
|
||||
pub fn parse_v2_header(buf: &[u8], max_header_bytes: usize) -> anyhow::Result<(SocketAddr, usize)> {
|
||||
if buf.len() < FIXED_HEADER_BYTES {
|
||||
anyhow::bail!("inbound PROXY header is truncated");
|
||||
}
|
||||
if buf[..12] != SIGNATURE {
|
||||
anyhow::bail!("invalid inbound PROXY protocol signature");
|
||||
}
|
||||
if buf[12] != 0x21 {
|
||||
anyhow::bail!("inbound PROXY protocol requires version 2 PROXY command");
|
||||
}
|
||||
|
||||
let payload_len = usize::from(u16::from_be_bytes([buf[14], buf[15]]));
|
||||
let total_len = FIXED_HEADER_BYTES
|
||||
.checked_add(payload_len)
|
||||
.ok_or_else(|| anyhow::anyhow!("inbound PROXY header length overflow"))?;
|
||||
if total_len > max_header_bytes {
|
||||
anyhow::bail!("inbound PROXY header exceeds configured limit");
|
||||
}
|
||||
if buf.len() < total_len {
|
||||
anyhow::bail!("inbound PROXY header is truncated");
|
||||
}
|
||||
|
||||
let address_len = match buf[13] {
|
||||
0x11 => 12,
|
||||
0x21 => 36,
|
||||
family => anyhow::bail!("unsupported inbound PROXY address family/protocol 0x{family:02x}"),
|
||||
|
|
@ -56,22 +87,21 @@ async fn read_v2_header_inner(stream: &mut TcpStream, max_header_bytes: usize) -
|
|||
anyhow::bail!("inbound PROXY v2 TLVs are not supported");
|
||||
}
|
||||
|
||||
let mut payload = vec![0u8; payload_len];
|
||||
stream.read_exact(&mut payload).await?;
|
||||
match fixed[13] {
|
||||
let payload = &buf[FIXED_HEADER_BYTES..total_len];
|
||||
let client = match buf[13] {
|
||||
0x11 => {
|
||||
let source = Ipv4Addr::new(payload[0], payload[1], payload[2], payload[3]);
|
||||
let port = u16::from_be_bytes([payload[8], payload[9]]);
|
||||
Ok(SocketAddr::V4(SocketAddrV4::new(source, port)))
|
||||
SocketAddr::V4(SocketAddrV4::new(source, port))
|
||||
},
|
||||
0x21 => {
|
||||
_ => {
|
||||
let source_bytes: [u8; 16] = payload[..16]
|
||||
.try_into()
|
||||
.map_err(|_| anyhow::anyhow!("invalid inbound PROXY IPv6 source address"))?;
|
||||
let source = Ipv6Addr::from(source_bytes);
|
||||
let port = u16::from_be_bytes([payload[32], payload[33]]);
|
||||
Ok(SocketAddr::V6(SocketAddrV6::new(source, port, 0, 0)))
|
||||
SocketAddr::V6(SocketAddrV6::new(source, port, 0, 0))
|
||||
},
|
||||
family => anyhow::bail!("unsupported inbound PROXY address family/protocol 0x{family:02x}"),
|
||||
}
|
||||
};
|
||||
Ok((client, total_len))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue