Skip to main content

waf_proxy/
lib.rs

1// SPDX-FileCopyrightText: 2026 0x00spor3
2// SPDX-License-Identifier: Apache-2.0
3
4pub mod config;
5pub mod metrics;
6pub mod tls;
7
8use std::convert::Infallible;
9use std::net::SocketAddr;
10use std::path::Path;
11use std::pin::Pin;
12use std::sync::atomic::{AtomicU64, Ordering};
13use std::sync::{Arc, RwLock};
14use std::task::{Context, Poll};
15use std::time::{Instant, SystemTime};
16
17use http_body_util::combinators::BoxBody;
18use http_body_util::{BodyExt, Full};
19use hyper::body::{Body, Bytes, Frame, Incoming};
20use hyper::service::service_fn;
21use hyper::{HeaderMap, Request, Response, Uri};
22use hyper_util::client::legacy::connect::HttpConnector;
23use hyper_util::client::legacy::Client;
24use hyper_util::rt::{TokioExecutor, TokioIo};
25use hyper_util::server::conn::auto;
26use tokio::net::TcpListener;
27use tokio_rustls::TlsAcceptor;
28
29use crate::metrics::{Metrics, Outcome};
30use tls::TlsCertSource;
31use tracing::{debug, error, info, warn};
32
33use waf_core::{
34    ClientIpResolver, Config, FailMode, IpSource, Normalized, RateLimitState, RequestContext,
35    ResilienceConfig, StateStore, WafModule,
36};
37use waf_detection::{
38    crs::CrsModule,
39    evasion::EvasionModule,
40    graphql::GraphqlModule, grpc::GrpcModule, header_injection::HeaderInjectionModule, ldap::LdapModule,
41    lfi_rfi::LfiRfiModule,
42    mail::MailModule, nosql::NosqlModule, path_traversal::PathTraversalModule,
43    rate_limit::RateLimitModule,
44    rce::RceModule, request_smuggling::RequestSmugglingModule, scanner::ScannerModule,
45    sqli::SqliModule, ssi::SsiModule, ssrf::SsrfModule, ssti::SstiModule, xss::XssModule,
46    xxe::XxeModule, ContentPrefilter,
47};
48use waf_normalizer::Normalizer;
49use waf_pipeline::{NoopLogger, Pipeline, PipelineVerdict};
50use waf_wasm::{WasmModule, WasmOptions};
51
52pub type HyperBoxBody = BoxBody<Bytes, hyper::Error>;
53
54/// A factory that (re)builds the injected detection modules. Called ONCE at bind and again
55/// on every config reload — so modules injected by an embedder (BOUNDARY §4) SURVIVE a
56/// SIGHUP and are re-`init`'d, instead of being dropped (the pre-0.3 behaviour). It returns
57/// a `Result` as a UNIT: on error the whole reload is aborted and the last-good `Reloadable`
58/// (which still holds the working modules) is kept — the modules are never dropped on a
59/// failed rebuild. A boxed closure so an embedder can capture its own (enterprise) config.
60pub type ModuleFactory =
61    dyn Fn() -> Result<Vec<Box<dyn WafModule>>, Box<dyn std::error::Error + Send + Sync>>
62        + Send
63        + Sync;
64
65/// Headers that must not be forwarded verbatim to the backend (RFC 7230).
66const HOP_BY_HOP: &[&str] = &[
67    "connection",
68    "host", // re-set by hyper from the target URI
69    "keep-alive",
70    "proxy-authenticate",
71    "proxy-authorization",
72    "te",
73    "trailers",
74    "transfer-encoding",
75    "upgrade",
76];
77
78static REQUEST_COUNTER: AtomicU64 = AtomicU64::new(0);
79
80fn next_request_id() -> String {
81    let n = REQUEST_COUNTER.fetch_add(1, Ordering::Relaxed);
82    format!("req-{n:016x}")
83}
84
85pub fn full_body(data: impl Into<Bytes>) -> HyperBoxBody {
86    Full::new(data.into())
87        .map_err(|never| match never {})
88        .boxed()
89}
90
91/// A buffered body that emits one DATA frame, then one TRAILERS frame. A plain `Full`
92/// cannot carry trailers; gRPC puts its status in HTTP/2 trailers (`grpc-status`/
93/// `grpc-message`), so relaying them requires this. Used only when trailers are present —
94/// the non-gRPC path keeps using `full_body` (byte-identical to before).
95struct FramedBody {
96    data: Option<Bytes>,
97    trailers: Option<HeaderMap>,
98}
99
100impl Body for FramedBody {
101    type Data = Bytes;
102    type Error = Infallible;
103
104    fn poll_frame(
105        mut self: Pin<&mut Self>,
106        _cx: &mut Context<'_>,
107    ) -> Poll<Option<Result<Frame<Bytes>, Infallible>>> {
108        if let Some(d) = self.data.take() {
109            return Poll::Ready(Some(Ok(Frame::data(d))));
110        }
111        if let Some(t) = self.trailers.take() {
112            return Poll::Ready(Some(Ok(Frame::trailers(t))));
113        }
114        Poll::Ready(None)
115    }
116}
117
118/// Box a buffered body, attaching `trailers` when present. With no trailers this is exactly
119/// `full_body` (so the non-gRPC datapath is unchanged); with trailers it is a `FramedBody`.
120fn body_with_trailers(data: Bytes, trailers: Option<HeaderMap>) -> HyperBoxBody {
121    match trailers {
122        None => full_body(data),
123        Some(t) => FramedBody { data: Some(data), trailers: Some(t) }
124            .map_err(|never| match never {})
125            .boxed(),
126    }
127}
128
129/// Collect a body into `(bytes, trailers)` — the trailer-preserving alternative to
130/// `collect().to_bytes()`. Keeps the buffered model (so the body is still inspectable)
131/// while not discarding the trailers that follow it (Step-0 invariant).
132async fn collect_with_trailers<B>(body: B) -> Result<(Bytes, Option<HeaderMap>), B::Error>
133where
134    B: Body<Data = Bytes>,
135{
136    let collected = body.collect().await?;
137    let trailers = collected.trailers().cloned();
138    Ok((collected.to_bytes(), trailers))
139}
140
141/// A gRPC request, by Content-Type (`application/grpc`, `+proto`, `-web`, …). Such requests
142/// are forwarded over h2c with their trailers relayed; everything else takes the unchanged
143/// h1 path.
144fn is_grpc_request(parts: &hyper::http::request::Parts) -> bool {
145    parts
146        .headers
147        .get(hyper::header::CONTENT_TYPE)
148        .and_then(|v| v.to_str().ok())
149        .map(|ct| ct.trim_start().starts_with("application/grpc"))
150        .unwrap_or(false)
151}
152
153fn parse_cookies(headers: &[(String, String)]) -> Vec<(String, String)> {
154    headers
155        .iter()
156        .filter(|(name, _)| name.eq_ignore_ascii_case("cookie"))
157        .flat_map(|(_, value)| {
158            value.split(';').filter_map(|pair| {
159                let mut parts = pair.splitn(2, '=');
160                let key = parts.next()?.trim().to_string();
161                let val = parts.next().unwrap_or("").trim().to_string();
162                Some((key, val))
163            })
164        })
165        .collect()
166}
167
168fn build_context(
169    parts: &hyper::http::request::Parts,
170    body: &Bytes,
171    client_addr: SocketAddr,
172    ip_resolver: &ClientIpResolver,
173) -> RequestContext {
174    let path = parts.uri.path().to_string();
175    let query = parts.uri.query().map(str::to_string);
176    let method = parts.method.to_string();
177    let http_version = format!("{:?}", parts.version);
178
179    let headers: Vec<(String, String)> = parts
180        .headers
181        .iter()
182        .filter_map(|(name, value)| {
183            value.to_str().ok().map(|v| (name.to_string(), v.to_string()))
184        })
185        .collect();
186
187    let cookies = parse_cookies(&headers);
188
189    let normalized = Normalized::default();
190
191    // Resolve the real client IP ONCE here: rate limiting, logging and future
192    // Geo/IP-reputation all read it back from `ctx.client_ip` (single source of
193    // truth). A fallback behind a trusted proxy means a spoofing attempt or a
194    // misconfigured upstream — log it.
195    let request_id = next_request_id();
196    let resolved = ip_resolver.resolve(client_addr.ip(), &headers);
197    match resolved.source {
198        IpSource::FallbackMissingHeader | IpSource::FallbackMalformed => warn!(
199            request_id = %request_id,
200            peer = %client_addr.ip(),
201            source = ?resolved.source,
202            "client-IP resolution fell back to peer address"
203        ),
204        IpSource::DirectPeer | IpSource::TrustedHeader => {}
205    }
206
207    RequestContext {
208        client_ip: resolved.ip,
209        request_id,
210        timestamp: SystemTime::now(),
211        method,
212        path: path.clone(),
213        raw_path: path,
214        query,
215        http_version,
216        headers,
217        cookies,
218        body: body.clone(),
219        normalized,
220        score: 0,
221        score_contributions: vec![],
222    }
223}
224
225/// Config-derived state, rebuilt as a unit on every hot reload and swapped
226/// atomically. A request loads either the entire old or the entire new value —
227/// never a mix of recompiled rules and stale thresholds.
228struct Reloadable {
229    backend: String,
230    normalizer: Normalizer,
231    pipeline: Pipeline,
232    /// Fast-path skip prefilter (Fase 7 / Pillar 3). Built here, in the SAME unit as
233    /// `pipeline`, from the same rule sources and the same `paranoia_level` snapshot,
234    /// so a reload regenerates both together — they can never drift apart.
235    prefilter: ContentPrefilter,
236    ip_resolver: ClientIpResolver,
237    resilience: ResilienceConfig,
238}
239
240/// Process-lifetime state that survives reloads:
241/// - `client`: the hyper connection pool (kept warm);
242/// - `listen_addr`: the bound address (restart-required if it changes);
243/// - `rl_state`: the rate-limiter token buckets (NOT reset by a reload, so a
244///   reload cannot be used to clear an attacker's throttle);
245/// - `current`: the atomically-swappable `Reloadable`.
246struct StaticState {
247    client: Client<HttpConnector, HyperBoxBody>,
248    /// A SEPARATE h2c (HTTP/2 prior-knowledge) client used ONLY for gRPC targets. Kept
249    /// distinct from `client` on purpose: flipping the general client to `http2_only` would
250    /// break all existing h1 forwarding — gRPC needs end-to-end h2, the rest stays h1.
251    grpc_client: Client<HttpConnector, HyperBoxBody>,
252    listen_addr: SocketAddr,
253    rl_state: RateLimitState,
254    current: RwLock<Arc<Reloadable>>,
255    mode: HandlerMode,
256    /// Inbound TLS terminator (Phase 12). `Some` ⇒ the listener serves ONLY TLS (h1/h2
257    /// by ALPN); `None` ⇒ cleartext (h1 + h2c). Built once at bind; a required-but-broken
258    /// cert fails the bind, so there is no runtime path that downgrades to cleartext.
259    tls_acceptor: Option<TlsAcceptor>,
260    /// Process-lifetime metrics (B1). Survives reloads like the rate-limit store. Recorded
261    /// once per request in `handle`; served by the metrics task (`Proxy::metrics_listener`).
262    metrics: Arc<Metrics>,
263    /// Factory that rebuilds the injected (embedder) modules on every reload (core 0.3). Process
264    /// lifetime, so `Reloader::reload_from` can re-run it in place of the pre-0.3 `Vec::new()` —
265    /// this is what makes `.add_module`-style injected modules survive a SIGHUP. `None` ⇒ no
266    /// injected modules to carry across a reload (the default OPEN build).
267    module_factory: Option<Arc<ModuleFactory>>,
268}
269
270/// Which request handler the accept loop dispatches to. `Inspect` is the ONLY mode a
271/// configured WAF ever uses (every public `bind*` sets it). `Passthrough` is a
272/// `#[doc(hidden)]` bench seam set ONLY by `bind_passthrough` — no `config.toml` field
273/// reaches it (that is the line separating a bench seam from a production bypass flag).
274/// It exists so the Fase 9 (c) load-test can measure the WAF-overhead delta against the
275/// SAME `forward_to_backend` the inspecting path uses.
276#[derive(Clone, Copy)]
277enum HandlerMode {
278    Inspect,
279    Passthrough,
280}
281
282impl StaticState {
283    /// Load the current config snapshot: take the read lock just long enough to
284    /// clone the `Arc`, then release it (never held across `.await`). Poisoning is
285    /// recovered (`into_inner`) because the only writer holds the lock solely for a
286    /// pointer assignment that cannot panic — so the data is never left invalid.
287    fn current(&self) -> Arc<Reloadable> {
288        self.current
289            .read()
290            .unwrap_or_else(|poisoned| poisoned.into_inner())
291            .clone()
292    }
293}
294
295/// Handle that can hot-reload a running proxy's configuration. Obtained via
296/// `Proxy::reloader()`; cheap to clone (an `Arc`). Used by the SIGHUP task in the
297/// binary and directly by tests.
298#[derive(Clone)]
299pub struct Reloader(Arc<StaticState>);
300
301impl Reloader {
302    /// Re-read, validate (reusing Pillar-1 `config::load`) and atomically swap.
303    /// On any error the current configuration is KEPT and the error is logged —
304    /// a failed reload never degrades a working WAF.
305    pub fn reload_from(&self, path: &Path) -> Result<(), config::LoadError> {
306        let new_cfg = match config::load(path) {
307            Ok(c) => c,
308            Err(e) => {
309                error!(error = %e, "config reload failed; keeping current configuration");
310                return Err(e);
311            }
312        };
313
314        // Restart-required field: the socket is already bound.
315        if new_cfg.proxy.listen != self.0.listen_addr {
316            warn!(
317                current = %self.0.listen_addr,
318                requested = %new_cfg.proxy.listen,
319                "proxy.listen change requires a restart; keeping the current bind address"
320            );
321        }
322
323        // Rebuild the injected (embedder) modules via the factory (core 0.3). Pre-0.3 this
324        // passed `Vec::new()`, silently dropping every `.add_module` module on a reload. The
325        // factory is fallible as a UNIT: if it errors (e.g. an enterprise schema file became
326        // invalid on disk), the whole reload is ABORTED and the current `Reloadable` — which
327        // still holds the working modules — is kept, exactly like a rejected config. No
328        // partial rebuild, no unprotected window, and the modules are never dropped on error.
329        let extra = match &self.0.module_factory {
330            Some(factory) => match factory() {
331                Ok(modules) => modules,
332                Err(e) => {
333                    error!(error = %e, "module factory failed on reload; keeping current configuration");
334                    return Err(config::LoadError::ModuleFactory(e.to_string()));
335                }
336            },
337            None => Vec::new(),
338        };
339
340        // Rebuild ALL config-derived state (rules recompiled, CIDR re-parsed),
341        // reusing the shared rate-limit buckets so the throttle state survives.
342        let new_reloadable = build_reloadable(&new_cfg, self.0.rl_state.clone(), extra);
343
344        // Atomic swap. The write section is a single pointer assignment that
345        // cannot panic, so the lock is never poisoned by this path; recover
346        // defensively anyway so a foreign poison can't wedge reloads.
347        *self
348            .0
349            .current
350            .write()
351            .unwrap_or_else(|poisoned| poisoned.into_inner()) = Arc::new(new_reloadable);
352        info!("configuration reloaded");
353        Ok(())
354    }
355}
356
357/// Build an upstream-error response per `on_upstream_error`: 502 (fail_closed,
358/// definitive gateway failure) or 503 (fail_open, retryable). Note: "fail_open"
359/// here does NOT pass traffic through — there is no origin to reach — it only
360/// softens the status to a retryable one. Always logged (critical operational event).
361fn upstream_error_response(
362    ctx: &RequestContext,
363    resilience: &ResilienceConfig,
364    detail: &str,
365) -> Response<HyperBoxBody> {
366    let (status, body) = match resilience.on_upstream_error {
367        FailMode::FailClosed => (502, "Bad Gateway"),
368        FailMode::FailOpen => (503, "Service Unavailable"),
369    };
370    warn!(
371        request_id = %ctx.request_id,
372        client_ip = %ctx.client_ip,
373        status = status,
374        policy = ?resilience.on_upstream_error,
375        detail = detail,
376        "upstream error: applying on_upstream_error policy"
377    );
378    Response::builder().status(status).body(full_body(body)).unwrap()
379}
380
381/// Serialize the per-rule score breakdown to a compact JSON array for the decision-log
382/// (`score_contributions` field). This is the data the enterprise control-plane drill-down
383/// (§7) reconstructs a blocked verdict from — emitted only on the already-logged denied path,
384/// never per benign request. Serialization of this plain data cannot realistically fail; an
385/// empty array on the theoretical error keeps the log line well-formed.
386fn contributions_json(ctx: &RequestContext) -> String {
387    serde_json::to_string(&ctx.score_contributions).unwrap_or_else(|_| "[]".to_string())
388}
389
390/// Map a denying pipeline verdict to an HTTP response (403 for Block, the
391/// carried status — e.g. 429 + `Retry-After` — for Reject). `Allow` → `None`.
392fn deny_response(
393    ctx: &RequestContext,
394    verdict: PipelineVerdict,
395) -> Option<(Response<HyperBoxBody>, Outcome)> {
396    match verdict {
397        PipelineVerdict::Allow => None,
398        PipelineVerdict::Block { rule_id, reason } => {
399            warn!(
400                request_id = %ctx.request_id,
401                rule_id = %rule_id,
402                reason = %reason,
403                score = ctx.score,
404                score_contributions = %contributions_json(ctx),
405                "request blocked"
406            );
407            Some((
408                Response::builder()
409                    .status(403)
410                    .body(full_body("Forbidden"))
411                    .unwrap(),
412                Outcome::Blocked,
413            ))
414        }
415        PipelineVerdict::Reject { rule_id, reason, status, retry_after } => {
416            warn!(
417                request_id = %ctx.request_id,
418                rule_id = %rule_id,
419                reason = %reason,
420                status = status,
421                score_contributions = %contributions_json(ctx),
422                "request rejected"
423            );
424            // Reason phrase + metric outcome by status: 429 rate-limit, 400 illegal framing
425            // (request smuggling). Block (403 detection) is a separate arm above.
426            let (body, outcome) = match status {
427                429 => ("Too Many Requests", Outcome::RateLimited),
428                400 => ("Bad Request", Outcome::BadRequest),
429                _ => ("Rejected", Outcome::BadRequest),
430            };
431            let mut builder = Response::builder().status(status);
432            if let Some(secs) = retry_after {
433                builder = builder.header("retry-after", secs.to_string());
434            }
435            Some((builder.body(full_body(body)).unwrap(), outcome))
436        }
437    }
438}
439
440async fn try_forward(
441    req: Request<Incoming>,
442    state: &StaticState,
443    client_addr: SocketAddr,
444) -> Result<(Response<HyperBoxBody>, Outcome), Box<dyn std::error::Error + Send + Sync>> {
445    // Load the current config snapshot ONCE per request (atomic): the whole
446    // request runs against this `Reloadable`, immune to a concurrent reload.
447    let rel = state.current();
448
449    let (parts, body) = req.into_parts();
450    // Collect the body for inspection AND keep any trailers (gRPC carries `grpc-status` in
451    // HTTP/2 trailers); they are relayed to the backend, never inspected.
452    let (body_bytes, req_trailers) = collect_with_trailers(body).await?;
453
454    let mut ctx = build_context(&parts, &body_bytes, client_addr, &rel.ip_resolver);
455
456    // Connection-phase modules (rate limiting) run BEFORE normalization, so
457    // flood traffic is rejected without paying for Fase 2 parsing.
458    let connection_verdict = rel.pipeline.run_connection(&mut ctx);
459    if let Some(denied) = deny_response(&ctx, connection_verdict) {
460        return Ok(denied);
461    }
462
463    // Parser-limit policy (Fase 6 / Pillar 2): on a normalization failure
464    // (limits exceeded / malformed input) `fail_closed` → 400; `fail_open` →
465    // forward UNINSPECTED (logged loudly), trading inspection for availability.
466    let normalized_ok = match rel.normalizer.normalize(&mut ctx) {
467        Ok(()) => true,
468        Err(e) => match rel.resilience.on_parser_limit {
469            FailMode::FailClosed => {
470                warn!(
471                    request_id = %ctx.request_id,
472                    error = %e,
473                    policy = ?FailMode::FailClosed,
474                    "normalization failed: rejecting (on_parser_limit)"
475                );
476                return Ok((
477                    Response::builder()
478                        .status(400)
479                        .body(full_body("Bad Request"))
480                        .unwrap(),
481                    Outcome::BadRequest,
482                ));
483            }
484            FailMode::FailOpen => {
485                warn!(
486                    request_id = %ctx.request_id,
487                    error = %e,
488                    policy = ?FailMode::FailOpen,
489                    "normalization failed: forwarding UNINSPECTED (on_parser_limit)"
490                );
491                false
492            }
493        },
494    };
495
496    let path_and_query = parts
497        .uri
498        .path_and_query()
499        .map(|pq| pq.as_str())
500        .unwrap_or("/")
501        .to_string();
502
503    info!(
504        request_id = %ctx.request_id,
505        method = %ctx.method,
506        path = %path_and_query,
507        client_ip = %ctx.client_ip,
508        "→ request"
509    );
510
511    // Skip inspection when normalization failed under fail_open (no canonical
512    // data to inspect); the request is forwarded uninspected.
513    if normalized_ok {
514        // Fast-path (Fase 7 / Pillar 3): the prefilter decides whether any content
515        // rule *could* match the canonical surface. If not, `run_inspection_gated`
516        // skips inspection and returns Allow with an identical decision log. Sound
517        // by construction (the scope-aware union is the OR of every active rule);
518        // equivalence is proven on the corpus oracle through this same gate.
519        let inspect = rel.prefilter.is_candidate(&ctx);
520        let inspection_verdict = rel.pipeline.run_inspection_gated(&mut ctx, inspect);
521        if let Some(denied) = deny_response(&ctx, inspection_verdict) {
522            return Ok(denied);
523        }
524    }
525
526    forward_to_backend(state, &rel, &parts, &path_and_query, body_bytes, req_trailers, client_addr, &ctx).await
527}
528
529/// The SINGLE forwarding path. Both the inspecting handler (`try_forward`) and the
530/// `#[doc(hidden)]` passthrough seam (`try_passthrough`) call it, so the (c) load-test's
531/// no-WAF leg cannot drift from production forwarding — the §13 duplicate-path risk is
532/// removed at the root, not mitigated. Behaviour is unchanged vs the inlined version
533/// (proven by the `passthrough_*` integration tests, green before and after the extract).
534// Forwarding intrinsically threads many request facets (config snapshot, parts, payload +
535// trailers, peer, context); bundling them into a struct would only move the list, not
536// shorten the data this single forwarding path needs.
537#[allow(clippy::too_many_arguments)]
538async fn forward_to_backend(
539    state: &StaticState,
540    rel: &Reloadable,
541    parts: &hyper::http::request::Parts,
542    path_and_query: &str,
543    body_bytes: Bytes,
544    req_trailers: Option<HeaderMap>,
545    client_addr: SocketAddr,
546    ctx: &RequestContext,
547) -> Result<(Response<HyperBoxBody>, Outcome), Box<dyn std::error::Error + Send + Sync>> {
548    let backend_uri: Uri = format!("{}{}", rel.backend, path_and_query).parse()?;
549    let is_grpc = is_grpc_request(parts);
550
551    let mut builder = Request::builder()
552        .method(parts.method.clone())
553        .uri(backend_uri);
554
555    for (name, value) in &parts.headers {
556        if !HOP_BY_HOP.contains(&name.as_str()) {
557            builder = builder.header(name, value);
558        }
559    }
560    // XFF hop record: append the address THIS proxy actually saw (the peer), not
561    // the resolved client IP — that would corrupt the forwarded chain semantics.
562    builder = builder.header("x-forwarded-for", client_addr.ip().to_string());
563    builder = builder.header("x-request-id", ctx.request_id.as_str());
564    // gRPC requires `TE: trailers` on the request (stripped above as hop-by-hop) so the
565    // backend negotiates trailer delivery — re-add it for gRPC targets only.
566    if is_grpc {
567        builder = builder.header("te", "trailers");
568    }
569
570    // gRPC: relay the request trailers and forward over the dedicated h2c client. Non-gRPC:
571    // a plain `Full` body over the existing h1 client — byte-identical to before.
572    let (client, fwd_body) = if is_grpc {
573        (&state.grpc_client, body_with_trailers(body_bytes, req_trailers))
574    } else {
575        (&state.client, full_body(body_bytes))
576    };
577    let fwd_req = builder.body(fwd_body)?;
578
579    // Upstream round-trip under a hard timeout so a stalled origin cannot pin the
580    // worker. Connection/timeout failures apply on_upstream_error (502/503),
581    // returned here rather than bubbling to the generic 502 in `handle`.
582    let upstream = tokio::time::timeout(rel.resilience.upstream_timeout(), async {
583        let resp = client.request(fwd_req).await?;
584        let (resp_parts, resp_body) = resp.into_parts();
585        // Keep the response trailers (gRPC `grpc-status`/`grpc-message`); they are relayed,
586        // not inspected. A non-gRPC h1 response has none → `None` → a plain body downstream.
587        let (resp_bytes, resp_trailers) = collect_with_trailers(resp_body).await?;
588        Ok::<_, Box<dyn std::error::Error + Send + Sync>>((resp_parts, resp_bytes, resp_trailers))
589    })
590    .await;
591
592    let (resp_parts, resp_bytes, resp_trailers) = match upstream {
593        Ok(Ok(triple)) => triple,
594        Ok(Err(e)) => {
595            return Ok((
596                upstream_error_response(ctx, &rel.resilience, &e.to_string()),
597                Outcome::UpstreamError,
598            ))
599        }
600        Err(_elapsed) => {
601            return Ok((
602                upstream_error_response(ctx, &rel.resilience, "upstream timeout"),
603                Outcome::UpstreamError,
604            ))
605        }
606    };
607
608    info!(
609        request_id = %ctx.request_id,
610        status = %resp_parts.status,
611        score = ctx.score,
612        "← response"
613    );
614
615    Ok((
616        Response::from_parts(resp_parts, body_with_trailers(resp_bytes, resp_trailers)),
617        Outcome::Allowed,
618    ))
619}
620
621/// `#[doc(hidden)]` passthrough seam: build the context and forward, SKIPPING the
622/// connection phase, normalization and inspection. The WAF-overhead delta the (c)
623/// load-test publishes = (inspecting leg) − (this leg) = normalize + detect, measured
624/// against the identical `forward_to_backend`. `build_context` runs in BOTH legs (shared
625/// proxy machinery) so it cancels in the delta. Reached only via `bind_passthrough`; no
626/// `config.toml` field selects it.
627async fn try_passthrough(
628    req: Request<Incoming>,
629    state: &StaticState,
630    client_addr: SocketAddr,
631) -> Result<(Response<HyperBoxBody>, Outcome), Box<dyn std::error::Error + Send + Sync>> {
632    let rel = state.current();
633    let (parts, body) = req.into_parts();
634    let (body_bytes, req_trailers) = collect_with_trailers(body).await?;
635    let ctx = build_context(&parts, &body_bytes, client_addr, &rel.ip_resolver);
636    let path_and_query = parts
637        .uri
638        .path_and_query()
639        .map(|pq| pq.as_str())
640        .unwrap_or("/")
641        .to_string();
642    forward_to_backend(state, &rel, &parts, &path_and_query, body_bytes, req_trailers, client_addr, &ctx).await
643}
644
645async fn handle(
646    req: Request<Incoming>,
647    state: Arc<StaticState>,
648    client_addr: SocketAddr,
649) -> Result<Response<HyperBoxBody>, Infallible> {
650    // Dispatch on the (config-unreachable) handler mode. `Inspect` is production; the
651    // `try_forward` decision path is unchanged. `Passthrough` is the bench seam.
652    let start = Instant::now();
653    let result = match state.mode {
654        HandlerMode::Inspect => try_forward(req, &state, client_addr).await,
655        HandlerMode::Passthrough => try_passthrough(req, &state, client_addr).await,
656    };
657    // Single recording point (pure side effect): the inner path classifies the Outcome;
658    // an unexpected error here is the WAF's OWN failure → `internal_error`, distinct from the
659    // structured upstream 502/503 already classified inside `forward_to_backend`.
660    let (resp, outcome) = match result {
661        Ok((resp, outcome)) => (resp, outcome),
662        Err(e) => {
663            error!(error = %e, client_ip = %client_addr.ip(), "forwarding error");
664            let resp = Response::builder()
665                .status(502)
666                .body(full_body("Bad Gateway"))
667                .unwrap();
668            (resp, Outcome::InternalError)
669        }
670    };
671    state.metrics.record(outcome, start.elapsed());
672    Ok(resp)
673}
674
675pub struct Proxy {
676    listener: TcpListener,
677    state: Arc<StaticState>,
678    /// Dedicated `/metrics` listener (`Some` ⇒ `[metrics].enabled`). Bound at `bind` for
679    /// fail-fast; the server task is spawned by `run`. NEVER the data port (serving internal
680    /// posture there would be an info leak and would be inspected by the WAF itself).
681    metrics_listener: Option<TcpListener>,
682}
683
684/// Build the enabled built-in modules from config. The rate limiter is given the
685/// SHARED bucket store so its throttle state survives a reload.
686fn build_modules(config: &Config, rl_state: &RateLimitState) -> Vec<Box<dyn WafModule>> {
687    let mut modules: Vec<Box<dyn WafModule>> = vec![Box::new(NoopLogger)];
688    // Framing validation runs first among Connection-phase modules: illegal
689    // framing is refused before it is even counted against the rate limit.
690    if config.modules.request_smuggling.enabled {
691        modules.push(Box::new(RequestSmugglingModule::new()));
692    }
693    if config.rate_limit.enabled {
694        modules.push(Box::new(RateLimitModule::with_state(rl_state.clone())));
695    }
696    if config.modules.sqli.enabled {
697        modules.push(Box::new(SqliModule::new()));
698    }
699    if config.modules.xss.enabled {
700        modules.push(Box::new(XssModule::new()));
701    }
702    if config.modules.path_traversal.enabled {
703        modules.push(Box::new(PathTraversalModule::new()));
704    }
705    if config.modules.rce.enabled {
706        modules.push(Box::new(RceModule::new()));
707    }
708    if config.modules.lfi_rfi.enabled {
709        modules.push(Box::new(LfiRfiModule::new()));
710    }
711    if config.modules.ssrf.enabled {
712        modules.push(Box::new(SsrfModule::new()));
713    }
714    if config.modules.ldap.enabled {
715        modules.push(Box::new(LdapModule::new()));
716    }
717    if config.modules.nosql.enabled {
718        modules.push(Box::new(NosqlModule::new()));
719    }
720    if config.modules.mail.enabled {
721        modules.push(Box::new(MailModule::new()));
722    }
723    if config.modules.ssti.enabled {
724        modules.push(Box::new(SstiModule::new()));
725    }
726    if config.modules.scanner.enabled {
727        modules.push(Box::new(ScannerModule::new()));
728    }
729    if config.modules.ssi.enabled {
730        modules.push(Box::new(SsiModule::new()));
731    }
732    if config.modules.xxe.enabled {
733        modules.push(Box::new(XxeModule::new()));
734    }
735    if config.modules.header_injection.enabled {
736        modules.push(Box::new(HeaderInjectionModule::new()));
737    }
738    if config.modules.evasion.enabled {
739        modules.push(Box::new(EvasionModule::new()));
740    }
741    if config.modules.graphql.enabled {
742        modules.push(Box::new(GraphqlModule::new()));
743    }
744    if config.modules.grpc.enabled {
745        modules.push(Box::new(GrpcModule::new()));
746    }
747    if config.modules.crs.enabled {
748        modules.push(Box::new(load_crs_module(&config.modules.crs.files)));
749    }
750    if config.modules.wasm.enabled {
751        for plugin in &config.modules.wasm.plugins {
752            if let Some(m) = load_wasm_plugin(plugin, &config.modules.wasm) {
753                modules.push(Box::new(m));
754            }
755        }
756    }
757    modules
758}
759
760/// Load one Proxy-Wasm plugin. A plugin whose file is unreadable or whose `.wasm` cannot be
761/// compiled/instantiated is logged loudly and skipped (fail-open at LOAD, like CRS — the
762/// runtime posture is fail-closed per request). The import report is logged so the operator
763/// sees the coverage, and a plugin relying on stubbed (semantic) host calls is flagged
764/// **DEGRADED** but still loaded — the operator decides (policy D3=A, paletto #4).
765fn load_wasm_plugin(
766    plugin: &waf_core::WasmPluginConfig,
767    cfg: &waf_core::WasmConfig,
768) -> Option<WasmModule> {
769    let bytes = match std::fs::read(&plugin.path) {
770        Ok(b) => b,
771        Err(e) => {
772            error!(file = %plugin.path, error = %e, "WASM: cannot read plugin (skipped)");
773            return None;
774        }
775    };
776    let name = plugin_name(&plugin.path);
777    let opts = WasmOptions {
778        pool_size: cfg.pool_size,
779        fuel_per_request: cfg.fuel_per_request,
780        max_memory_bytes: cfg.max_memory_bytes,
781        checkout_timeout: std::time::Duration::from_millis(cfg.checkout_timeout_ms),
782    };
783    let config_bytes = plugin.config.as_deref().unwrap_or("").as_bytes();
784    match WasmModule::from_bytes(&name, &bytes, config_bytes, &opts) {
785        Ok((module, report)) => {
786            // Informational at boot; the loud "degraded" signal is emitted at runtime the
787            // first time the plugin actually invokes a stubbed semantic host call.
788            info!(plugin = %name, "{}", report.summary());
789            Some(module)
790        }
791        Err(e) => {
792            error!(file = %plugin.path, error = %e, "WASM: plugin failed to load (skipped)");
793            None
794        }
795    }
796}
797
798/// Derive a short plugin name from its path (file stem), for log correlation and `rule_id`.
799fn plugin_name(path: &str) -> String {
800    std::path::Path::new(path)
801        .file_stem()
802        .and_then(|s| s.to_str())
803        .unwrap_or("plugin")
804        .to_string()
805}
806
807/// Read the configured CRS `seclang` files (in order), concatenate them and build the
808/// [`CrsModule`]. An unreadable file is logged loudly and skipped — CRS is an additive
809/// detection layer (default off), so a missing import file fails open (consistent with
810/// `resilience.on_config_error` = fail-open) rather than taking down the proxy; the boot
811/// log makes the gap explicit. The loaded/skipped report and the skipped-rule reasons are
812/// logged so the operator sees exactly what coverage they got (policy D3=A).
813fn load_crs_module(files: &[String]) -> CrsModule {
814    let mut combined = String::new();
815    for path in files {
816        match std::fs::read_to_string(path) {
817            Ok(text) => {
818                combined.push_str(&text);
819                combined.push('\n');
820            }
821            Err(e) => error!(file = %path, error = %e, "CRS import: cannot read file (skipped)"),
822        }
823    }
824    let module = CrsModule::from_source(&combined);
825    info!(files = files.len(), "{}", module.report());
826    if !module.skipped().is_empty() {
827        warn!(
828            skipped = module.skipped().len(),
829            "CRS import: some rules fall outside the supported subset (see debug logs for reasons)"
830        );
831        for s in module.skipped() {
832            debug!(id = ?s.id, line = s.line_no, reason = %s.reason, "CRS import: rule skipped");
833        }
834    }
835    module
836}
837
838/// Build the full config-derived state as a unit (rules recompiled, CIDR
839/// re-parsed). Used at startup AND on every reload, so reload gets exactly the
840/// same construction path — no mixed state. `extra` modules are appended after the
841/// built-ins (test seam; they are NOT carried across a reload).
842fn build_reloadable(
843    config: &Config,
844    rl_state: RateLimitState,
845    extra: Vec<Box<dyn WafModule>>,
846) -> Reloadable {
847    let mut modules = build_modules(config, &rl_state);
848    modules.extend(extra);
849    let pipeline = Pipeline::new(config, modules);
850
851    // PL4 is "empty but legal": warn that a paranoia_level above the highest
852    // shipped rule activates no extra rules (forward-compatible).
853    if config.waf.paranoia_level > waf_detection::HIGHEST_RULE_PARANOIA {
854        warn!(
855            paranoia_level = config.waf.paranoia_level,
856            highest_rule_paranoia = waf_detection::HIGHEST_RULE_PARANOIA,
857            "paranoia_level exceeds the highest existing rule paranoia: no additional rules are activated"
858        );
859    }
860    let ip_resolver = ClientIpResolver::from_config(&config.network);
861    if ip_resolver.trusted_count() < config.network.trusted_proxies.len() {
862        warn!(
863            configured = config.network.trusted_proxies.len(),
864            valid = ip_resolver.trusted_count(),
865            "some trusted_proxies CIDR entries were invalid and skipped"
866        );
867    }
868
869    Reloadable {
870        backend: config.proxy.backend.trim_end_matches('/').to_string(),
871        normalizer: Normalizer::new(&config.limits),
872        pipeline,
873        // Same construction point + config snapshot as the pipeline above.
874        prefilter: ContentPrefilter::new(config.waf.paranoia_level),
875        ip_resolver,
876        resilience: config.resilience,
877    }
878}
879
880impl Proxy {
881    /// Bind a proxy from config with the default extension surface (built-in
882    /// modules, in-memory rate-limit store, file-based TLS cert). For embedding —
883    /// injecting a custom store or extra modules — use [`Proxy::builder`].
884    pub async fn bind(config: &Config) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
885        Self::builder(config).build().await
886    }
887
888    /// Start configuring a proxy with injectable extension points (the stable
889    /// embedding API): extra detection modules and the rate-limit [`StateStore`].
890    /// Every seam has a default, so `Proxy::builder(cfg).build()` equals
891    /// [`Proxy::bind`].
892    pub fn builder(config: &Config) -> ProxyBuilder<'_> {
893        ProxyBuilder {
894            config,
895            modules: Vec::new(),
896            state_store: None,
897            cert_source: None,
898            module_factory: None,
899            mode: HandlerMode::Inspect,
900        }
901    }
902
903    /// Bind with extra detection modules appended after the built-in set.
904    ///
905    /// Internal seam kept for integration tests (inject a panicking module to verify
906    /// Pillar-2 isolation). The stable public equivalent is
907    /// `Proxy::builder(cfg).modules(..).build()`.
908    #[doc(hidden)]
909    pub async fn bind_with_modules(
910        config: &Config,
911        extra: Vec<Box<dyn WafModule>>,
912    ) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
913        Self::bind_inner(config, extra, HandlerMode::Inspect, None, None, None).await
914    }
915
916    /// `#[doc(hidden)]` bench seam: bind a proxy that FORWARDS WITHOUT inspecting (no
917    /// connection phase, no normalization, no detection) — the no-WAF leg of the Fase 9
918    /// (c) load-test, sharing `forward_to_backend` with the real path. Not a production
919    /// surface: no `config.toml` field selects it, only this constructor does.
920    #[doc(hidden)]
921    pub async fn bind_passthrough(
922        config: &Config,
923    ) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
924        Self::bind_inner(config, Vec::new(), HandlerMode::Passthrough, None, None, None).await
925    }
926
927    async fn bind_inner(
928        config: &Config,
929        extra: Vec<Box<dyn WafModule>>,
930        mode: HandlerMode,
931        state_store: Option<RateLimitState>,
932        cert_source: Option<Arc<dyn TlsCertSource>>,
933        module_factory: Option<Arc<ModuleFactory>>,
934    ) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
935        let listener = TcpListener::bind(config.proxy.listen).await?;
936        let listen_addr = listener.local_addr()?;
937        let client: Client<HttpConnector, HyperBoxBody> =
938            Client::builder(TokioExecutor::new()).build(HttpConnector::new());
939        // Dedicated h2c client for gRPC backends (prior-knowledge HTTP/2 over cleartext).
940        let grpc_client: Client<HttpConnector, HyperBoxBody> =
941            Client::builder(TokioExecutor::new()).http2_only(true).build(HttpConnector::new());
942
943        // Build the TLS terminator BEFORE serving: a required cert that cannot be loaded
944        // is a fatal boot error (fail-closed), never a silent downgrade to cleartext. An
945        // injected cert source (e.g. enterprise ACME/mTLS) replaces the default file source.
946        let tls_acceptor = tls::acceptor_from_source(&config.tls, cert_source)
947            .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;
948        if tls_acceptor.is_some() {
949            info!(listen = %listen_addr, alpn = ?config.tls.alpn, "TLS termination enabled");
950        }
951
952        // The rate-limiter bucket store lives here (process lifetime), shared into
953        // every (re)built pipeline so reloads never reset the throttle. The
954        // tracked-key cap is fixed at boot (the store outlives reloads). An injected
955        // store (e.g. enterprise Redis) replaces the default in-memory one.
956        let rl_state = state_store
957            .unwrap_or_else(|| RateLimitState::in_memory(config.rate_limit.max_tracked_keys));
958
959        // Injected modules (core 0.3): the static `.add_module` extras first, then the factory's
960        // output. The factory is the SINGLE source of reload-surviving modules, so it also runs
961        // at boot here — a boot-time error is fatal (fail-closed), the same posture an embedder
962        // had when it built these modules inline before passing them in.
963        let mut extra_total = extra;
964        if let Some(factory) = &module_factory {
965            extra_total.extend(factory()?);
966        }
967        let reloadable = build_reloadable(config, rl_state.clone(), extra_total);
968
969        // Metrics (B1): a dedicated `/metrics` listener bound here for fail-fast (a busy
970        // port is a boot error, never a silent miss). Loopback by default; NEVER the data
971        // port. Counters live process-wide and survive reloads.
972        let metrics = Arc::new(Metrics::new());
973        let metrics_listener = if config.metrics.enabled {
974            let l = TcpListener::bind(config.metrics.listen).await?;
975            info!(listen = %l.local_addr()?, "metrics endpoint enabled (/metrics)");
976            Some(l)
977        } else {
978            None
979        };
980
981        Ok(Self {
982            listener,
983            state: Arc::new(StaticState {
984                client,
985                grpc_client,
986                listen_addr,
987                rl_state,
988                current: RwLock::new(Arc::new(reloadable)),
989                mode,
990                tls_acceptor,
991                metrics,
992                module_factory,
993            }),
994            metrics_listener,
995        })
996    }
997
998    /// A cheap, cloneable handle to hot-reload this proxy's configuration.
999    /// Obtain it before `run()` (which consumes `self`); the binary wires it to
1000    /// SIGHUP, tests call `reload_from` directly.
1001    pub fn reloader(&self) -> Reloader {
1002        Reloader(Arc::clone(&self.state))
1003    }
1004
1005    pub fn local_addr(&self) -> std::io::Result<SocketAddr> {
1006        self.listener.local_addr()
1007    }
1008
1009    /// Address of the metrics endpoint, when `[metrics].enabled` (tests/operability).
1010    pub fn metrics_addr(&self) -> Option<SocketAddr> {
1011        self.metrics_listener.as_ref().and_then(|l| l.local_addr().ok())
1012    }
1013
1014    pub async fn run(self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
1015        // Spawn the metrics server (B1) on its dedicated listener, if enabled. It shares the
1016        // process-wide `Metrics` with the datapath and is wholly separate from data serving.
1017        if let Some(metrics_listener) = self.metrics_listener {
1018            let metrics = Arc::clone(&self.state.metrics);
1019            tokio::spawn(serve_metrics(metrics_listener, metrics));
1020        }
1021        loop {
1022            let (stream, client_addr) = self.listener.accept().await?;
1023            let state = Arc::clone(&self.state);
1024
1025            tokio::spawn(async move {
1026                // When TLS is enabled, complete the handshake first; a handshake error is
1027                // logged and the connection dropped (non-fatal — the listener stays up).
1028                // Then serve h1/h2 (TLS by ALPN, cleartext by preface) via the auto Builder.
1029                // The acceptor is Arc-backed → cheap clone, frees `state` to move into serve.
1030                match state.tls_acceptor.clone() {
1031                    Some(acceptor) => match acceptor.accept(stream).await {
1032                        Ok(tls_stream) => {
1033                            serve_connection(TokioIo::new(tls_stream), state, client_addr).await;
1034                        }
1035                        Err(e) => {
1036                            warn!(error = %e, client_ip = %client_addr.ip(), "TLS handshake error");
1037                        }
1038                    },
1039                    None => {
1040                        serve_connection(TokioIo::new(stream), state, client_addr).await;
1041                    }
1042                }
1043            });
1044        }
1045    }
1046}
1047
1048/// Stable builder for embedding the proxy with custom extension points. Obtain it
1049/// via [`Proxy::builder`]. Every seam defaults to the built-in behaviour, so a
1050/// builder with no overrides is identical to [`Proxy::bind`]. The enterprise plugs
1051/// a distributed rate-limit store or premium modules here **without forking**
1052/// (BOUNDARY §4).
1053pub struct ProxyBuilder<'a> {
1054    config: &'a Config,
1055    modules: Vec<Box<dyn WafModule>>,
1056    state_store: Option<RateLimitState>,
1057    cert_source: Option<Arc<dyn TlsCertSource>>,
1058    module_factory: Option<Arc<ModuleFactory>>,
1059    mode: HandlerMode,
1060}
1061
1062impl<'a> ProxyBuilder<'a> {
1063    /// Replace the extra detection modules appended after the built-in set. These
1064    /// run after the built-ins and are NOT carried across a config reload.
1065    pub fn modules(mut self, modules: Vec<Box<dyn WafModule>>) -> Self {
1066        self.modules = modules;
1067        self
1068    }
1069
1070    /// Append a single extra detection module (additive over [`Self::modules`]).
1071    pub fn add_module(mut self, module: Box<dyn WafModule>) -> Self {
1072        self.modules.push(module);
1073        self
1074    }
1075
1076    /// Inject the rate-limit [`StateStore`] (e.g. a distributed Redis store). The
1077    /// store survives config reloads. Defaults to the in-memory token bucket sized
1078    /// from `[rate_limit].max_tracked_keys`.
1079    pub fn state_store(mut self, store: Arc<dyn StateStore>) -> Self {
1080        self.state_store = Some(RateLimitState::with_store(store));
1081        self
1082    }
1083
1084    /// Inject the [`TlsCertSource`] (e.g. enterprise ACME/managed-PKI/mTLS). `[tls].enabled`
1085    /// and `[tls].alpn` still come from config; the source only governs cert provenance, so
1086    /// the config `cert_path`/`key_path` are ignored when one is injected. Defaults to the
1087    /// OPEN `FileCertSource` reading those paths.
1088    pub fn cert_source(mut self, source: Arc<dyn TlsCertSource>) -> Self {
1089        self.cert_source = Some(source);
1090        self
1091    }
1092
1093    /// Inject a [`ModuleFactory`] that (re)builds the extra detection modules (core 0.3).
1094    /// Unlike [`Self::add_module`]/[`Self::modules`] (built once, dropped on a reload), the
1095    /// factory is re-run on every config reload, so injected modules SURVIVE a SIGHUP and are
1096    /// re-`init`'d. It runs at bind too (the single source of reload-surviving modules): a
1097    /// boot error is fatal, and a reload error aborts that reload and keeps the last-good
1098    /// modules. This is the seam an embedder uses to keep premium modules across reloads
1099    /// (BOUNDARY §4). Factory output is appended AFTER any static `.add_module` extras.
1100    pub fn module_factory<F>(mut self, factory: F) -> Self
1101    where
1102        F: Fn() -> Result<Vec<Box<dyn WafModule>>, Box<dyn std::error::Error + Send + Sync>>
1103            + Send
1104            + Sync
1105            + 'static,
1106    {
1107        self.module_factory = Some(Arc::new(factory));
1108        self
1109    }
1110
1111    /// Bind the listener and construct the proxy with the chosen seams.
1112    pub async fn build(self) -> Result<Proxy, Box<dyn std::error::Error + Send + Sync>> {
1113        Proxy::bind_inner(
1114            self.config,
1115            self.modules,
1116            self.mode,
1117            self.state_store,
1118            self.cert_source,
1119            self.module_factory,
1120        )
1121        .await
1122    }
1123}
1124
1125/// Serve one connection with the auto (h1/h2) builder. Generic over the transport so the
1126/// SAME service runs over a plain `TcpStream` or a `TlsStream` — the protocol negotiation
1127/// (h1 vs h2/h2c) is entirely inside `auto::Builder`, and `handle()` stays protocol-neutral.
1128async fn serve_connection<I>(io: I, state: Arc<StaticState>, client_addr: SocketAddr)
1129where
1130    I: hyper::rt::Read + hyper::rt::Write + Unpin + Send + 'static,
1131{
1132    let svc = service_fn(move |req| {
1133        let state = Arc::clone(&state);
1134        handle(req, state, client_addr)
1135    });
1136    if let Err(e) = auto::Builder::new(TokioExecutor::new())
1137        .serve_connection(io, svc)
1138        .await
1139    {
1140        warn!(error = %e, client_ip = %client_addr.ip(), "connection error");
1141    }
1142}
1143
1144/// Serve the `/metrics` endpoint on its dedicated listener (B1). Plain h1; a scraper opens a
1145/// short connection, GETs `/metrics`, reads the text. Anything that is not `GET /metrics`
1146/// gets a 404 — no path reflection, no other surface.
1147async fn serve_metrics(listener: TcpListener, metrics: Arc<Metrics>) {
1148    loop {
1149        let Ok((stream, _)) = listener.accept().await else { continue };
1150        let metrics = Arc::clone(&metrics);
1151        tokio::spawn(async move {
1152            let svc = service_fn(move |req: Request<Incoming>| {
1153                let metrics = Arc::clone(&metrics);
1154                async move { Ok::<_, Infallible>(metrics_response(&req, &metrics)) }
1155            });
1156            let _ = hyper::server::conn::http1::Builder::new()
1157                .serve_connection(TokioIo::new(stream), svc)
1158                .await;
1159        });
1160    }
1161}
1162
1163/// `GET /metrics` → Prometheus text exposition; anything else → 404.
1164fn metrics_response(req: &Request<Incoming>, metrics: &Metrics) -> Response<HyperBoxBody> {
1165    if req.method() == hyper::Method::GET && req.uri().path() == "/metrics" {
1166        Response::builder()
1167            .status(200)
1168            .header("content-type", "text/plain; version=0.0.4; charset=utf-8")
1169            .body(full_body(metrics.render()))
1170            .unwrap()
1171    } else {
1172        Response::builder().status(404).body(full_body("Not Found")).unwrap()
1173    }
1174}
1175
1176#[cfg(test)]
1177mod tests {
1178    use super::*;
1179    use waf_core::WafMode;
1180
1181    #[test]
1182    fn hop_by_hop_includes_connection_and_host() {
1183        assert!(HOP_BY_HOP.contains(&"connection"));
1184        assert!(HOP_BY_HOP.contains(&"host"));
1185        assert!(HOP_BY_HOP.contains(&"transfer-encoding"));
1186    }
1187
1188    #[test]
1189    fn hop_by_hop_excludes_regular_headers() {
1190        assert!(!HOP_BY_HOP.contains(&"content-type"));
1191        assert!(!HOP_BY_HOP.contains(&"authorization"));
1192        assert!(!HOP_BY_HOP.contains(&"x-custom-header"));
1193    }
1194
1195    #[test]
1196    fn config_parses_from_toml() {
1197        let raw = r#"
1198[proxy]
1199listen = "127.0.0.1:8080"
1200backend = "http://localhost:3000"
1201
1202[waf]
1203mode = "detection-only"
1204block_threshold = 10
1205"#;
1206        let config: Config = toml::from_str(raw).unwrap();
1207        assert_eq!(config.proxy.backend, "http://localhost:3000");
1208        assert_eq!(config.waf.mode, WafMode::DetectionOnly);
1209        assert_eq!(config.waf.block_threshold, 10);
1210    }
1211
1212    #[test]
1213    fn config_uses_default_block_threshold_when_omitted() {
1214        let raw = r#"
1215[proxy]
1216listen = "127.0.0.1:8080"
1217backend = "http://localhost:3000"
1218
1219[waf]
1220mode = "detection-only"
1221"#;
1222        let config: Config = toml::from_str(raw).unwrap();
1223        assert_eq!(config.waf.block_threshold, 5);
1224    }
1225
1226    #[test]
1227    fn config_parses_network_section() {
1228        let raw = r#"
1229[proxy]
1230listen = "127.0.0.1:8080"
1231backend = "http://localhost:3000"
1232
1233[waf]
1234mode = "blocking"
1235
1236[network]
1237trusted_proxies = ["10.0.0.0/8", "::1"]
1238client_ip_header = "X-Forwarded-For"
1239trusted_hops = 2
1240"#;
1241        let config: Config = toml::from_str(raw).unwrap();
1242        assert_eq!(config.network.trusted_proxies, vec!["10.0.0.0/8", "::1"]);
1243        assert_eq!(config.network.client_ip_header, "X-Forwarded-For");
1244        assert_eq!(config.network.trusted_hops, 2);
1245    }
1246
1247    #[test]
1248    fn config_network_defaults_to_failsafe_when_absent() {
1249        let raw = r#"
1250[proxy]
1251listen = "127.0.0.1:8080"
1252backend = "http://localhost:3000"
1253
1254[waf]
1255mode = "detection-only"
1256"#;
1257        let config: Config = toml::from_str(raw).unwrap();
1258        assert!(config.network.trusted_proxies.is_empty());
1259        assert_eq!(config.network.trusted_hops, 1);
1260        assert_eq!(config.network.client_ip_header, "x-forwarded-for".to_string());
1261    }
1262
1263    #[test]
1264    fn config_rejects_unknown_mode() {
1265        let raw = r#"
1266[proxy]
1267listen = "127.0.0.1:8080"
1268backend = "http://localhost:3000"
1269
1270[waf]
1271mode = "unknown-mode"
1272"#;
1273        assert!(toml::from_str::<Config>(raw).is_err());
1274    }
1275
1276    #[test]
1277    fn parse_cookies_splits_on_semicolon() {
1278        let headers = vec![("cookie".to_string(), "session=abc; user=123".to_string())];
1279        let cookies = parse_cookies(&headers);
1280        assert_eq!(cookies.len(), 2);
1281        assert!(cookies.contains(&("session".to_string(), "abc".to_string())));
1282        assert!(cookies.contains(&("user".to_string(), "123".to_string())));
1283    }
1284
1285    #[test]
1286    fn parse_cookies_handles_missing_value() {
1287        let headers = vec![("cookie".to_string(), "flag=; token=xyz".to_string())];
1288        let cookies = parse_cookies(&headers);
1289        assert!(cookies.contains(&("flag".to_string(), "".to_string())));
1290        assert!(cookies.contains(&("token".to_string(), "xyz".to_string())));
1291    }
1292
1293    #[test]
1294    fn parse_cookies_handles_empty_header_list() {
1295        assert!(parse_cookies(&[]).is_empty());
1296    }
1297
1298    #[test]
1299    fn request_id_is_unique_per_call() {
1300        let id1 = next_request_id();
1301        let id2 = next_request_id();
1302        assert_ne!(id1, id2);
1303        assert!(id1.starts_with("req-"));
1304    }
1305}