Skip to main content

security_rust/data/
csv_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"^[=+\-@\t\r]").unwrap(),
10        Regex::new(r"(?i)^\s*DDE").unwrap(),
11        Regex::new(r"(?i)^\s*cmd\s*\|").unwrap(),
12        Regex::new(r"(?i)^\s*@SUM\s*\(").unwrap(),
13    ]
14});
15
16pub struct CsvInjectionDetector;
17
18impl Detector for CsvInjectionDetector {
19    fn name(&self) -> &'static str {
20        "csv_injection"
21    }
22
23    fn detect(&self, input: &str) -> Option<DetectionResult> {
24        for re in PATTERNS.iter() {
25            if let Some(m) = re.find(input) {
26                return Some(DetectionResult {
27                    attack_type: "csv_injection".into(),
28                    category: AttackCategory::Data,
29                    severity: Severity::Medium,
30                    matched_pattern: m.as_str().to_string(),
31                    offset: m.start(),
32                    message: "CSV formula injection detected".into(),
33                });
34            }
35        }
36        None
37    }
38}