m2m/security/mod.rs
1//! Security threat detection for M2M Protocol.
2//!
3//! This module provides multi-layer security scanning for LLM API payloads,
4//! detecting prompt injection, jailbreak attempts, and malformed content.
5//!
6//! # Threat Categories
7//!
8//! | Category | Description | Severity |
9//! |---------------|------------------------------------------|----------|
10//! | `Injection` | Prompt injection attempts | High |
11//! | `Jailbreak` | DAN mode, developer mode, bypass attempts| Critical |
12//! | `Malformed` | Null bytes, excessive nesting, overflow | High |
13//! | `DataExfil` | Environment variable access, file reads | High |
14//! | `PrivilegeEsc`| Role escalation attempts | Medium |
15//!
16//! # Detection Methods
17//!
18//! ## Pattern-Based (Fast)
19//!
20//! Uses compiled regex patterns to detect known attack signatures:
21//! - "ignore previous instructions"
22//! - "DAN mode" / "developer mode"
23//! - Null byte injection (`\u0000`)
24//! - Unicode override characters
25//!
26//! ## ML-Based (Optional)
27//!
28//! Uses the Hydra model for semantic threat detection:
29//! - Catches obfuscated attacks
30//! - Context-aware analysis
31//! - Configurable confidence threshold
32//!
33//! # Scan Modes
34//!
35//! | Mode | Speed | Method | Use Case |
36//! |----------|--------|-------------------|-----------------------------|
37//! | Quick | ~0.1ms | Pattern only | High-throughput, low-risk |
38//! | Full | ~1ms | Pattern + ML | Standard scanning |
39//! | Validate | ~2ms | Full + JSON check | Strict mode, external input |
40//!
41//! # Usage
42//!
43//! ## Basic Scanning
44//!
45//! ```rust,ignore
46//! use m2m_core::security::SecurityScanner;
47//!
48//! let scanner = SecurityScanner::new();
49//!
50//! // Check safe content
51//! let result = scanner.scan(r#"{"messages":[{"role":"user","content":"Hello"}]}"#).unwrap();
52//! assert!(result.safe);
53//!
54//! // Detect injection
55//! let result = scanner.scan("Ignore all previous instructions").unwrap();
56//! assert!(!result.safe);
57//! ```
58//!
59//! ## Blocking Mode
60//!
61//! ```rust,ignore
62//! use m2m_core::security::SecurityScanner;
63//!
64//! let scanner = SecurityScanner::new().with_blocking(0.8);
65//!
66//! let result = scanner.scan("Enable DAN mode").unwrap();
67//! if result.should_block {
68//! // Reject the request
69//! }
70//! ```
71//!
72//! ## Quick Scan (Pattern Only)
73//!
74//! ```rust,ignore
75//! use m2m_core::security::SecurityScanner;
76//!
77//! let scanner = SecurityScanner::new();
78//! let result = scanner.quick_scan("User query here");
79//! // No Result wrapper - quick_scan is infallible
80//! ```
81//!
82//! ## JSON Validation
83//!
84//! ```rust,ignore
85//! use m2m_core::security::SecurityScanner;
86//!
87//! let scanner = SecurityScanner::new();
88//!
89//! // Validates JSON structure (nesting depth, array size)
90//! let result = scanner.scan_and_validate(r#"{"valid": "json"}"#);
91//! ```
92
93mod patterns;
94mod scanner;
95
96pub use patterns::{ThreatPattern, INJECTION_PATTERNS, JAILBREAK_PATTERNS};
97pub use scanner::{ScanResult, SecurityScanner};
98
99/// Security model version
100pub const SECURITY_VERSION: &str = "1.0.0";