security_rust/injection/
xpath_injection.rs1use crate::{AttackCategory, DetectionResult, Detector, Severity, regex_detect};
4use regex::Regex;
5use std::sync::LazyLock;
6
7static PATTERNS: LazyLock<Regex> = LazyLock::new(|| {
13 Regex::new(concat!(
14 r"'(?i:\s*or\s*'1'\s*=\s*'1)",
15 r"|'(?i:\s*and\s*'1'\s*=\s*'2)",
16 r"|'(?i:\s*or\s*1\s*=\s*1)",
17 r#"|"(?i:\s*or\s*"1"\s*=\s*"1)"#,
18 r"|'\s*\]\s*\|\s*[/@(*]",
23 r"|'(?i:\s*or\s*''=')",
24 r"|'(?i:\s*or\s*true\s*\()",
25 ))
26 .unwrap()
27});
28
29pub struct XPathInjectionDetector;
30
31impl Detector for XPathInjectionDetector {
32 fn name(&self) -> &'static str {
33 "xpath_injection"
34 }
35
36 fn detect(&self, input: &str) -> Option<DetectionResult> {
37 regex_detect(
38 std::slice::from_ref(&*PATTERNS),
39 self.name(),
40 AttackCategory::Injection,
41 Severity::High,
42 "XPATH injection detected",
43 input,
44 )
45 }
46}
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51
52 fn det() -> XPathInjectionDetector {
53 XPathInjectionDetector
54 }
55
56 fn assert_hit(input: &str) {
57 crate::test_helpers::assert_detected(
58 &det(),
59 input,
60 AttackCategory::Injection,
61 Severity::High,
62 );
63 }
64
65 #[test]
66 fn name_is_xpath_injection() {
67 assert_eq!(det().name(), "xpath_injection");
68 }
69
70 #[test]
71 fn detects_common_payloads() {
72 for input in [
73 "' or '1'='1",
74 "' or 1=1",
75 "' and '1'='2",
76 r#"" or "1"="1"#,
77 "' or ''='",
78 "' or true()",
79 "']|//admin",
80 ] {
81 assert_hit(input);
82 }
83 }
84
85 #[test]
86 fn benign_inputs_not_detected() {
87 for input in [
88 "Hello, this is a normal text input. Nothing suspicious here.",
89 "The first quarter results are in",
90 "or is a conjunction in English",
91 "I want 1 pizza and 1 drink",
92 ] {
93 assert!(det().detect(input).is_none(), "false positive: {input}");
94 }
95 }
96
97 #[test]
98 fn edge_cases() {
99 assert!(det().detect("").is_none());
100 assert!(det().detect(" \t\n ").is_none());
101 assert!(det().detect("你好世界 こんにちは").is_none());
102 assert!(det().detect("or 1=1").is_none());
104 assert!(det().detect("' or '2'='1").is_none());
105 assert!(det().detect("' or 2=2").is_none());
106 }
107
108 #[test]
109 fn obfuscated_variants_detected() {
110 for input in ["' OR '1'='1", "' And '1'='2", r#"" Or "1"="1"#] {
111 assert_hit(input);
112 }
113 }
114
115 #[test]
118 fn bracket_index_bitwise_or_is_not_detected() {
119 for input in [
120 "const flags = opts['flags'] | 0;\n",
121 "merged = d['a'] | d['b']\n",
122 "type Mode = flags['mode'] | Extra;\n",
123 ] {
124 crate::test_helpers::assert_clean(&det(), input);
125 }
126 }
127
128 #[test]
130 fn union_with_path_still_detected() {
131 for input in [
132 "']|//admin",
133 "'] | /user[position()=1]",
134 "']|@secret-attr",
135 "']|*",
136 ] {
137 assert_hit(input);
138 }
139 }
140}