feat: HTTP protocol plugin + XDP globals patching from config

protocol-http (compile-time feature):
- HttpHandler: incremental HTTP/1.1 request parse (fragment-safe),
  header size/timeout limits, method whitelist, Host required,
  blocked_paths, optional User-Agent requirement
- [protocol.http] config section; registry wiring behind feature

XDP globals:
- src/xdp/globals.rs: XdpGlobals + build_rodata_image() mirroring
  xdp/core/config.h layout; set via OpenMapMut::set_initial_value()
  before load ([xdp] section: ports, udp policy, throttle, challenge)
- wire set_globals into rampart binary startup

fix: gate preflight() behind xdp feature (dead code without it)
This commit is contained in:
Loki 2026-08-24 12:18:22 +02:00
parent ce98fcefb5
commit ef75986983
Signed by: boba
GPG key ID: 6CDE87BC9A778D97
14 changed files with 972 additions and 22 deletions

112
src/protocol/http/mod.rs Normal file
View file

@ -0,0 +1,112 @@
//! HTTP/1.1 protocol handler (feature `protocol-http`).
//!
//! Первый plugin-реализаций [`ProtocolHandler`]: читает request-head
//! постепенно (TCP приходит фрагментами), проверяет его по политике из
//! секции `[protocol.http]` и возвращает upstream для туннелирования.
//! Голова читается через `peek`, чтобы байты остались в сокете и движок
//! проксировал запрос на backend без потерь.
mod parser;
use crate::config::HttpProtocolConfig;
use crate::protocol::{ProtocolHandler, Upstream};
use std::future::Future;
use std::pin::Pin;
use std::time::Duration;
use tokio::net::TcpStream;
/// Обработчик HTTP/1.1: verdict Allow → tunnel на upstream, Reject → drop.
pub struct HttpProtocolHandler {
policy: parser::Policy,
timeout: Duration,
upstream: String,
}
impl HttpProtocolHandler {
/// Собирает обработчик из конфига `[protocol.http]` и списка upstream'ов.
#[must_use]
pub fn new(http: &HttpProtocolConfig, upstreams: &[String]) -> Self {
Self {
policy: parser::Policy {
max_header_bytes: http.max_header_bytes,
blocked_paths: http.blocked_paths.clone(),
require_user_agent: http.require_user_agent,
},
timeout: Duration::from_secs(http.timeout_secs),
upstream: upstreams.first().cloned().unwrap_or_else(|| "127.0.0.1:80".to_string()),
}
}
/// Накапливает request-head через `peek` до терминатора или лимита.
///
/// # Errors
/// Таймаут, закрытие клиента, превышение `max_header_bytes` или ошибка I/O.
async fn read_head(&self, stream: &TcpStream) -> anyhow::Result<Vec<u8>> {
let limit = self.policy.max_header_bytes;
if limit == 0 {
anyhow::bail!("protocol.http.max_header_bytes must be positive");
}
let mut buf = vec![0u8; limit];
let mut len = 0usize;
let accumulate = async {
loop {
if let Some(head_len) = parser::scan_head(&buf[..len]) {
buf.truncate(head_len);
return Ok(buf);
}
if len >= limit {
anyhow::bail!("request head exceeds {limit} bytes");
}
let n = stream.peek(&mut buf[len..]).await?;
if n == 0 {
anyhow::bail!("client closed connection before head was complete");
}
len += n;
}
};
match tokio::time::timeout(self.timeout, accumulate).await {
Ok(result) => result,
Err(_) => anyhow::bail!("timed out reading request head"),
}
}
}
impl ProtocolHandler for HttpProtocolHandler {
fn name(&self) -> &'static str {
"http"
}
fn handshake<'a>(
&'a self,
stream: &'a mut TcpStream,
) -> Pin<Box<dyn Future<Output = anyhow::Result<Upstream>> + Send + 'a>> {
Box::pin(async move {
let head = self.read_head(stream).await?;
match parser::evaluate(&head, &self.policy) {
parser::Verdict::Allow { .. } => Ok(Upstream::new(self.upstream.clone())),
parser::Verdict::Reject { reason } => Err(anyhow::anyhow!("http: {reason}")),
}
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn zero_limit_fails_fast_without_io() {
let config = HttpProtocolConfig::default();
let mut broken = config.clone();
broken.max_header_bytes = 0;
let handler = HttpProtocolHandler::new(&broken, &[]);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.expect("bind");
let client = tokio::net::TcpStream::connect(listener.local_addr().expect("addr"))
.await
.expect("connect");
let (server, _) = listener.accept().await.expect("accept");
drop(listener);
assert!(handler.read_head(&server).await.is_err());
drop(client);
}
}

196
src/protocol/http/parser.rs Normal file
View file

@ -0,0 +1,196 @@
//! Чистый парсер и валидатор HTTP/1.x request-head для edge-фильтра.
//!
//! Модуль не знает про I/O: на вход — накопленный заголовок, на выходе —
//! [`Verdict`]. Это позволяет покрывать логику юнит-тестами без сокетов.
/// Политика проверки, снятая с секции `[protocol.http]`.
#[derive(Debug, Clone)]
pub struct Policy {
pub max_header_bytes: usize,
pub blocked_paths: Vec<String>,
pub require_user_agent: bool,
}
/// Результат проверки головы запроса.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Verdict {
Allow { method: String, path: String, host: String },
Reject { reason: String },
}
const ALLOWED_METHODS: [&str; 7] = ["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH"];
const HEADER_TERMINATOR: &[u8] = b"\r\n\r\n";
/// Ищет конец головы запроса (`\r\n\r\n`) и возвращает её полную длину.
#[must_use]
pub fn scan_head(buf: &[u8]) -> Option<usize> {
buf.windows(HEADER_TERMINATOR.len())
.position(|window| window == HEADER_TERMINATOR)
.map(|pos| pos + HEADER_TERMINATOR.len())
}
/// Разбирает и валидирует голову запроса согласно политике.
#[must_use]
pub fn evaluate(head: &[u8], policy: &Policy) -> Verdict {
let Ok(text) = std::str::from_utf8(head) else {
return reject("request head is not valid utf-8");
};
let mut lines = text.split("\r\n");
let Some(request_line) = lines.next().filter(|line| !line.is_empty()) else {
return reject("empty request line");
};
let mut parts = request_line.split(' ');
let (Some(method), Some(target), Some(version)) = (parts.next(), parts.next(), parts.next()) else {
return reject("malformed request line");
};
if parts.next().is_some() {
return reject("malformed request line");
}
if !matches!(version, "HTTP/1.0" | "HTTP/1.1") {
return reject("unsupported http version");
}
if !ALLOWED_METHODS.contains(&method) {
return reject("method not allowed");
}
let mut host: Option<&str> = None;
let mut user_agent: Option<&str> = None;
for line in lines {
if line.is_empty() {
continue;
}
let Some((name, value)) = line.split_once(':') else {
return reject("malformed header line");
};
let value = value.trim();
if name.eq_ignore_ascii_case("host") {
host = Some(value);
} else if name.eq_ignore_ascii_case("user-agent") {
user_agent = Some(value);
}
}
let path = match target.split_once('?') {
Some((path, _)) => path,
None => target,
};
if policy
.blocked_paths
.iter()
.any(|prefix| path.starts_with(prefix.as_str()))
{
return reject("path is blocklisted");
}
let Some(host) = host.filter(|value| !value.is_empty()) else {
return reject("missing host header");
};
if policy.require_user_agent && user_agent.is_none_or(str::is_empty) {
return reject("missing user-agent header");
}
Verdict::Allow {
method: method.to_string(),
path: path.to_string(),
host: host.to_string(),
}
}
fn reject(reason: &str) -> Verdict {
Verdict::Reject {
reason: reason.to_string(),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn policy() -> Policy {
Policy {
max_header_bytes: 8192,
blocked_paths: Vec::new(),
require_user_agent: false,
}
}
#[test]
fn scan_finds_full_head_length() {
assert_eq!(scan_head(b"GET / HTTP/1.1\r\n\r\n"), Some(18));
assert_eq!(scan_head(b"GET / HTTP/1.1\r\n"), None);
}
#[test]
fn valid_request_is_allowed() {
let head = b"POST /api/items?limit=5 HTTP/1.1\r\nHost: example.com\r\n\r\n";
let verdict = evaluate(head, &policy());
assert_eq!(
verdict,
Verdict::Allow {
method: "POST".to_string(),
path: "/api/items".to_string(),
host: "example.com".to_string()
}
);
}
#[test]
fn unknown_method_is_rejected() {
let head = b"TRACE / HTTP/1.1\r\nHost: h\r\n\r\n";
assert!(matches!(evaluate(head, &policy()), Verdict::Reject { .. }));
}
#[test]
fn missing_host_is_rejected() {
let head = b"GET / HTTP/1.1\r\nUser-Agent: t\r\n\r\n";
assert!(matches!(evaluate(head, &policy()), Verdict::Reject { .. }));
}
#[test]
fn http2_version_is_rejected() {
let head = b"GET / HTTP/2.0\r\nHost: h\r\n\r\n";
assert!(matches!(evaluate(head, &policy()), Verdict::Reject { .. }));
}
#[test]
fn malformed_line_is_rejected() {
assert!(matches!(
evaluate(b"nonsense\r\n\r\n", &policy()),
Verdict::Reject { .. }
));
assert!(matches!(
evaluate(b"GET / HTTP/1.1\r\nbroken\r\n\r\n", &policy()),
Verdict::Reject { .. }
));
}
#[test]
fn non_utf8_head_is_rejected() {
assert!(matches!(evaluate(&[0xff, 0xfe], &policy()), Verdict::Reject { .. }));
}
#[test]
fn blocked_path_prefix_is_rejected() {
let mut custom = policy();
custom.blocked_paths = vec!["/admin".to_string()];
assert!(matches!(
evaluate(b"GET /admin/panel HTTP/1.1\r\nHost: h\r\n\r\n", &custom),
Verdict::Reject { .. }
));
assert!(matches!(
evaluate(b"GET /public HTTP/1.1\r\nHost: h\r\n\r\n", &custom),
Verdict::Allow { .. }
));
}
#[test]
fn required_user_agent_enforced() {
let mut custom = policy();
custom.require_user_agent = true;
let head = b"GET / HTTP/1.1\r\nHost: h\r\n\r\n";
assert!(matches!(evaluate(head, &custom), Verdict::Reject { .. }));
let head_with_ua = b"GET / HTTP/1.1\r\nHost: h\r\nUser-Agent: curl/8\r\n\r\n";
assert!(matches!(evaluate(head_with_ua, &custom), Verdict::Allow { .. }));
}
}