use crate::{AttackCategory, DetectionResult, Detector, Severity, regex_detect};
use regex::Regex;
use std::sync::LazyLock;
static PATTERNS: LazyLock<Vec<Regex>> = LazyLock::new(|| {
vec![
Regex::new(r"\{\{[^{}]{0,120}?\d+[ \t]*[-+*/%][ \t]*\d+[^{}]{0,120}?\}\}").unwrap(),
Regex::new(r"\$\{[^{}]{0,120}?\d+[ \t]*[-+*/%][ \t]*\d+[^{}]{0,120}?\}").unwrap(),
Regex::new(r"\{\{[ \t]*config\b").unwrap(),
Regex::new(r"\$\{[ \t]*(?:T[ \t]*\(|@[\w.]+@)").unwrap(),
Regex::new(r"\{%\s*.*?\s*%\}").unwrap(),
Regex::new(r"<%=").unwrap(),
Regex::new(r"<%@").unwrap(),
Regex::new(r"#set\s*\(").unwrap(),
Regex::new(r"__mro__").unwrap(),
Regex::new(r"__subclasses__").unwrap(),
Regex::new(r"__globals__").unwrap(),
Regex::new(r"__builtins__").unwrap(),
Regex::new(r"__class__").unwrap(),
Regex::new(r"__dict__").unwrap(),
]
});
pub struct SstiDetector;
impl Detector for SstiDetector {
fn name(&self) -> &'static str {
"ssti"
}
fn detect(&self, input: &str) -> Option<DetectionResult> {
regex_detect(
&PATTERNS,
self.name(),
AttackCategory::Injection,
Severity::Critical,
"Server-Side Template Injection detected",
input,
)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn det() -> SstiDetector {
SstiDetector
}
fn assert_hit(input: &str) {
crate::test_helpers::assert_detected(
&det(),
input,
AttackCategory::Injection,
Severity::Critical,
);
}
#[test]
fn name_is_ssti() {
assert_eq!(det().name(), "ssti");
}
#[test]
fn detects_common_payloads() {
for input in [
"{{7*7}}",
"{{ ''.__class__.__mro__[1].__subclasses__() }}",
"${7*7}",
"{% include '/etc/passwd' %}",
"<%= params[:x] %>",
r#"<%@ page import="java.util.*" %>"#,
"#set($x = 5)",
"{{config.__init__.__globals__}}",
] {
assert_hit(input);
}
}
#[test]
fn benign_inputs_not_detected() {
for input in [
"Hello, this is a normal text input. Nothing suspicious here.",
"The total is $5.00 plus tax",
"Please enter your name below",
"The class of 2026 graduates in May",
"100% of users agree with this",
] {
assert!(det().detect(input).is_none(), "false positive: {input}");
}
}
#[test]
fn plain_interpolation_not_detected() {
for input in [
"the price is ${amount}",
"${user}${pass}",
"@Value(\"${x.y.z}\")",
"${env:JAVA_HOME}",
"const x = `${name}`",
"${#strings.toUpperCase(name)}",
"{{ name }}",
"{{ app_config }}",
"${timeout:30s}",
"${PATH:-/usr/bin}",
] {
assert!(det().detect(input).is_none(), "false positive: {input}");
}
}
#[test]
fn evaluation_probes_and_runtime_access_detected() {
for input in [
"${7*7}",
"{{7*7}}",
"{{ 7 * 7 }}",
"${{7*7}}",
"<%= 7*7 %>",
"{{config}}",
"{{ config.items }}",
"${T(java.lang.Runtime)}",
"${@java.lang.Runtime@getRuntime()}",
"{{ ''.__class__.__mro__ }}",
"{{ self.__dict__ }}",
] {
assert_hit(input);
}
}
#[test]
fn edge_cases() {
assert!(det().detect("").is_none());
assert!(det().detect(" \t\n ").is_none());
assert!(det().detect("你好世界 こんにちは").is_none());
assert!(det().detect("{7*7}").is_none());
assert!(det().detect("{{7*7").is_none());
assert!(det().detect("__CLASS__").is_none());
assert!(det().detect("{$x=7}").is_none());
}
#[test]
fn obfuscated_variants_detected() {
for input in [
"{{ ''.__class__.__MRO__[1] }}",
"{{ self.__dict__ }}",
"{{request.application.__globals__}}",
] {
assert_hit(input);
}
}
}