Skip to main content

security_rust/injection/
ssti.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"\{\{.*?\}\}").unwrap(),
10        Regex::new(r"\$\{.*?\}").unwrap(),
11        Regex::new(r"\{%\s*.*?\s*%\}").unwrap(),
12        Regex::new(r"<%=").unwrap(),
13        Regex::new(r"<%@").unwrap(),
14        Regex::new(r"#set\s*\(").unwrap(),
15        Regex::new(r"__mro__").unwrap(),
16        Regex::new(r"__subclasses__").unwrap(),
17        Regex::new(r"__globals__").unwrap(),
18        Regex::new(r"__builtins__").unwrap(),
19        Regex::new(r"__class__").unwrap(),
20    ]
21});
22
23pub struct SstiDetector;
24
25impl Detector for SstiDetector {
26    fn name(&self) -> &'static str {
27        "ssti"
28    }
29
30    fn detect(&self, input: &str) -> Option<DetectionResult> {
31        for re in PATTERNS.iter() {
32            if let Some(m) = re.find(input) {
33                return Some(DetectionResult {
34                    attack_type: "ssti".into(),
35                    category: AttackCategory::Injection,
36                    severity: Severity::Critical,
37                    matched_pattern: m.as_str().to_string(),
38                    offset: m.start(),
39                    message: "Server-Side Template Injection detected".into(),
40                });
41            }
42        }
43        None
44    }
45}