Skip to main content

security_rust/protocol/
cors.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)Origin:\s*null").unwrap(),
11        Regex::new(r"(?i)Access-Control-Allow-Origin:\s*\*").unwrap(),
12        Regex::new(r"(?i)Access-Control-Allow-Credentials:\s*true").unwrap(),
13    ]
14});
15
16pub struct CorsDetector;
17
18impl Detector for CorsDetector {
19    fn name(&self) -> &'static str {
20        "cors"
21    }
22
23    fn detect(&self, input: &str) -> Option<DetectionResult> {
24        for re in PATTERNS.iter() {
25            if let Some(m) = re.find(input) {
26                return Some(DetectionResult {
27                    attack_type: "cors".into(),
28                    category: AttackCategory::Protocol,
29                    severity: Severity::Medium,
30                    matched_pattern: m.as_str().to_string(),
31                    offset: m.start(),
32                    message: "CORS bypass attempt detected".into(),
33                });
34            }
35        }
36        None
37    }
38}