Skip to main content

security_rust/injection/
graphql_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"(?i)__schema").unwrap(),
10        Regex::new(r"(?i)__type\s*\{").unwrap(),
11        Regex::new(r"(?i)__typename").unwrap(),
12        Regex::new(r"\{[^{}]*\{[^{}]*\{[^{}]*\{[^{}]*\{").unwrap(),
13    ]
14});
15
16pub struct GraphQlInjectionDetector;
17
18impl Detector for GraphQlInjectionDetector {
19    fn name(&self) -> &'static str {
20        "graphql_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: "graphql_injection".into(),
28                    category: AttackCategory::Injection,
29                    severity: Severity::Medium,
30                    matched_pattern: m.as_str().to_string(),
31                    offset: m.start(),
32                    message: "GraphQL injection/introspection detected".into(),
33                });
34            }
35        }
36        None
37    }
38}