Skip to main content

security_rust/injection/
xpath_injection.rs

1// Copyright (c) 2026 erik <erik@erik.xyz> — https://erik.xyz
2
3use crate::{AttackCategory, DetectionResult, Detector, Severity};
4use regex::Regex;
5use std::sync::LazyLock;
6
7static PATTERNS: LazyLock<Vec<Regex>> = LazyLock::new(|| {
8    vec![
9        Regex::new(r"'(?i)\s*or\s*'1'\s*=\s*'1").unwrap(),
10        Regex::new(r"'(?i)\s*and\s*'1'\s*=\s*'2").unwrap(),
11        Regex::new(r"'(?i)\s*or\s*1\s*=\s*1").unwrap(),
12        Regex::new(r#""(?i)\s*or\s*"1"\s*=\s*"1"#).unwrap(),
13        Regex::new(r"'\s*\]\s*\|\s*").unwrap(),
14        Regex::new(r"'(?i)\s*or\s*''='").unwrap(),
15        Regex::new(r"'(?i)\s*or\s*true\s*\(").unwrap(),
16    ]
17});
18
19pub struct XPathInjectionDetector;
20
21impl Detector for XPathInjectionDetector {
22    fn name(&self) -> &'static str {
23        "xpath_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: "xpath_injection".into(),
31                    category: AttackCategory::Injection,
32                    severity: Severity::High,
33                    matched_pattern: m.as_str().to_string(),
34                    offset: m.start(),
35                    message: "XPATH injection detected".into(),
36                });
37            }
38        }
39        None
40    }
41}