Skip to main content

security_rust/protocol/
header_injection.rs

1// Copyright (c) 2026 erik <erik@erik.xyz> — https://erik.xyz
2
3use regex::Regex;
4use std::sync::LazyLock;
5
6use crate::{AttackCategory, DetectionResult, Detector, Severity};
7
8static PATTERNS: LazyLock<Vec<Regex>> = LazyLock::new(|| {
9    vec![
10        Regex::new(r"(?i)%0[dD]%0[aA]").unwrap(),
11        Regex::new(
12            r"(?i)\r\n\s*(?:Set-Cookie|Location|Content-Length|Content-Type|Transfer-Encoding):",
13        )
14        .unwrap(),
15        Regex::new(r"(?i)%0[dD].*%0[aA]").unwrap(),
16    ]
17});
18
19pub struct HeaderInjectionDetector;
20
21impl Detector for HeaderInjectionDetector {
22    fn name(&self) -> &'static str {
23        "header_injection"
24    }
25
26    fn detect(&self, input: &str) -> Option<DetectionResult> {
27        for re in PATTERNS.iter() {
28            if let Some(m) = re.find(input) {
29                return Some(DetectionResult {
30                    attack_type: "header_injection".into(),
31                    category: AttackCategory::Protocol,
32                    severity: Severity::High,
33                    matched_pattern: m.as_str().to_string(),
34                    offset: m.start(),
35                    message: "HTTP header injection (CRLF) detected".into(),
36                });
37            }
38        }
39        None
40    }
41}