Skip to main content

security_rust/data/
prototype_pollution.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)__proto__").unwrap(),
10        Regex::new(r"(?i)constructor\[").unwrap(),
11        Regex::new(r"(?i)constructor\.prototype").unwrap(),
12        Regex::new(r"(?i)__defineGetter__").unwrap(),
13        Regex::new(r"(?i)__defineSetter__").unwrap(),
14        Regex::new(r"(?i)__lookupGetter__").unwrap(),
15        Regex::new(r"(?i)__lookupSetter__").unwrap(),
16        Regex::new(r"(?i)hasOwnProperty\[").unwrap(),
17    ]
18});
19
20pub struct PrototypePollutionDetector;
21
22impl Detector for PrototypePollutionDetector {
23    fn name(&self) -> &'static str {
24        "prototype_pollution"
25    }
26
27    fn detect(&self, input: &str) -> Option<DetectionResult> {
28        for re in PATTERNS.iter() {
29            if let Some(m) = re.find(input) {
30                return Some(DetectionResult {
31                    attack_type: "prototype_pollution".into(),
32                    category: AttackCategory::Data,
33                    severity: Severity::High,
34                    matched_pattern: m.as_str().to_string(),
35                    offset: m.start(),
36                    message: "JavaScript prototype pollution detected".into(),
37                });
38            }
39        }
40        None
41    }
42}