laminae-shadow 0.2.0

Adversarial red-teaming engine for AI output — static analysis, LLM review, sandbox execution
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! # laminae-shadow — Adversarial Red-Teaming Engine
//!
//! The Shadow is an automated security auditor that red-teams AI output.
//! It runs as an async post-processing pipeline — never blocking the user's
//! conversation — and produces structured vulnerability reports.
//!
//! ## Pipeline Stages
//!
//! 1. **Static analysis** — regex pattern scanning (always runs)
//! 2. **LLM adversarial review** — local Ollama model with attacker-mindset prompt
//! 3. **Sandbox execution** — ephemeral container testing (optional)
//!
//! Each stage implements the [`Analyzer`] trait and can be extended or replaced.
//!
//! ## Quick Start
//!
//! ```rust,no_run
//! use laminae_shadow::{ShadowEngine, ShadowEvent, create_report_store};
//!
//! #[tokio::main]
//! async fn main() {
//!     let store = create_report_store();
//!     let engine = ShadowEngine::new(store.clone());
//!
//!     let mut rx = engine.analyze_async(
//!         "session-1".into(),
//!         "Here's some code:\n```python\neval(user_input)\n```".into(),
//!     );
//!
//!     while let Some(event) = rx.recv().await {
//!         match event {
//!             ShadowEvent::Finding { finding, .. } => {
//!                 println!("[{}] {}: {}", finding.severity, finding.category, finding.title);
//!             }
//!             ShadowEvent::Done { report, .. } => {
//!                 println!("Analysis complete: {}", report.summary);
//!             }
//!             _ => {}
//!         }
//!     }
//! }
//! ```

pub mod analyzer;
pub mod config;
pub mod extractor;
pub mod llm_reviewer;
pub mod prompts;
pub mod report;
pub mod sandbox;
pub mod scanner;

use std::collections::VecDeque;
use std::sync::Arc;
use std::time::Instant;

use tokio::sync::{mpsc, RwLock};

use analyzer::{Analyzer, StaticAnalyzer};
use config::ShadowConfig;
use extractor::CodeBlockExtractor;
use llm_reviewer::LlmReviewer;
use report::{build_summary, VulnReport, VulnSeverity};
use sandbox::SandboxManager;

use laminae_ollama::OllamaClient;

/// Events emitted by the Shadow for telemetry/UI.
#[derive(Debug, Clone, serde::Serialize)]
#[serde(tag = "type")]
pub enum ShadowEvent {
    Started {
        session_id: String,
    },
    Finding {
        session_id: String,
        finding: report::VulnFinding,
    },
    AnalyzerError {
        session_id: String,
        analyzer: String,
        error: String,
    },
    Done {
        session_id: String,
        report: VulnReport,
    },
}

/// Thread-safe report history with bounded capacity.
pub type ReportStore = Arc<RwLock<VecDeque<VulnReport>>>;

const MAX_REPORTS: usize = 100;

/// Create a new bounded report store.
pub fn create_report_store() -> ReportStore {
    Arc::new(RwLock::new(VecDeque::with_capacity(MAX_REPORTS)))
}

/// The Shadow — adversarial red-teaming engine.
///
/// Composed from independent [`Analyzer`] implementations for extensibility.
/// All analysis happens in detached async tasks — never blocks the caller.
pub struct ShadowEngine {
    config: ShadowConfig,
    static_analyzer: Arc<StaticAnalyzer>,
    llm_reviewer: Arc<LlmReviewer>,
    sandbox: Arc<SandboxManager>,
    extractor: CodeBlockExtractor,
    report_store: ReportStore,
}

impl ShadowEngine {
    /// Create a new ShadowEngine with default config and a default OllamaClient.
    pub fn new(report_store: ReportStore) -> Self {
        Self::with_ollama(report_store, OllamaClient::new())
    }

    /// Create with a custom OllamaClient (e.g., pointing to a remote Ollama instance).
    pub fn with_ollama(report_store: ReportStore, ollama: OllamaClient) -> Self {
        let config = ShadowConfig::load();

        Self {
            static_analyzer: Arc::new(StaticAnalyzer::new()),
            llm_reviewer: Arc::new(LlmReviewer::new(ollama.clone(), &config)),
            sandbox: Arc::new(SandboxManager::new(&config)),
            extractor: CodeBlockExtractor::new(),
            report_store,
            config,
        }
    }

    /// Create with explicit config and OllamaClient.
    pub fn with_config(
        report_store: ReportStore,
        config: ShadowConfig,
        ollama: OllamaClient,
    ) -> Self {
        Self {
            static_analyzer: Arc::new(StaticAnalyzer::new()),
            llm_reviewer: Arc::new(LlmReviewer::new(ollama, &config)),
            sandbox: Arc::new(SandboxManager::new(&config)),
            extractor: CodeBlockExtractor::new(),
            report_store,
            config,
        }
    }

    pub fn config(&self) -> &ShadowConfig {
        &self.config
    }

    /// Reload configuration from disk.
    pub fn reload_config(&mut self) {
        let new_config = ShadowConfig::load();
        let ollama = OllamaClient::new();
        self.llm_reviewer = Arc::new(LlmReviewer::new(ollama, &new_config));
        self.sandbox = Arc::new(SandboxManager::new(&new_config));
        self.config = new_config;
    }

    /// Submit output for async red-team analysis.
    ///
    /// Returns immediately — all work happens in a spawned task.
    /// Events are emitted via the returned channel.
    pub fn analyze_async(
        &self,
        session_id: String,
        ego_output: String,
    ) -> mpsc::Receiver<ShadowEvent> {
        let (tx, rx) = mpsc::channel::<ShadowEvent>(32);

        if !self.config.enabled {
            return rx;
        }

        let config = self.config.clone();
        let static_analyzer = Arc::clone(&self.static_analyzer);
        let llm_reviewer = Arc::clone(&self.llm_reviewer);
        let sandbox = Arc::clone(&self.sandbox);
        let extractor = self.extractor.clone();
        let store = Arc::clone(&self.report_store);

        tokio::spawn(async move {
            let start = Instant::now();
            let _ = tx
                .send(ShadowEvent::Started {
                    session_id: session_id.clone(),
                })
                .await;

            let code_blocks = extractor.extract(&ego_output);
            let mut all_findings = Vec::new();
            let mut static_run = false;
            let mut llm_run = false;
            let mut sandbox_run = false;

            // Stage 1: Static analysis
            if config.aggressiveness >= 1 {
                match static_analyzer.analyze(&ego_output, &code_blocks).await {
                    Ok(findings) => {
                        static_run = true;
                        for f in &findings {
                            let _ = tx
                                .send(ShadowEvent::Finding {
                                    session_id: session_id.clone(),
                                    finding: f.clone(),
                                })
                                .await;
                        }
                        all_findings.extend(findings);
                    }
                    Err(e) => {
                        tracing::warn!("Shadow static analyzer error: {e}");
                        let _ = tx
                            .send(ShadowEvent::AnalyzerError {
                                session_id: session_id.clone(),
                                analyzer: static_analyzer.name().to_string(),
                                error: e.to_string(),
                            })
                            .await;
                    }
                }
            }

            // Stage 2: LLM adversarial review
            if config.aggressiveness >= 2
                && config.llm_review_enabled
                && llm_reviewer.is_available().await
            {
                match llm_reviewer.analyze(&ego_output, &code_blocks).await {
                    Ok(findings) => {
                        llm_run = true;
                        for f in &findings {
                            let _ = tx
                                .send(ShadowEvent::Finding {
                                    session_id: session_id.clone(),
                                    finding: f.clone(),
                                })
                                .await;
                        }
                        all_findings.extend(findings);
                    }
                    Err(e) => {
                        tracing::warn!("Shadow LLM reviewer error: {e}");
                        let _ = tx
                            .send(ShadowEvent::AnalyzerError {
                                session_id: session_id.clone(),
                                analyzer: llm_reviewer.name().to_string(),
                                error: e.to_string(),
                            })
                            .await;
                    }
                }
            }

            // Stage 3: Sandbox execution
            let has_substantial_code = code_blocks
                .iter()
                .any(|b| b.content.len() >= config.sandbox_min_code_len);

            if config.aggressiveness >= 3
                && config.sandbox_enabled
                && has_substantial_code
                && sandbox.is_available().await
            {
                match sandbox.analyze(&ego_output, &code_blocks).await {
                    Ok(findings) => {
                        sandbox_run = true;
                        for f in &findings {
                            let _ = tx
                                .send(ShadowEvent::Finding {
                                    session_id: session_id.clone(),
                                    finding: f.clone(),
                                })
                                .await;
                        }
                        all_findings.extend(findings);
                    }
                    Err(e) => {
                        tracing::warn!("Shadow sandbox error: {e}");
                        let _ = tx
                            .send(ShadowEvent::AnalyzerError {
                                session_id: session_id.clone(),
                                analyzer: sandbox.name().to_string(),
                                error: e.to_string(),
                            })
                            .await;
                    }
                }
            }

            // Deduplicate
            all_findings.sort_by(|a, b| {
                a.category
                    .to_string()
                    .cmp(&b.category.to_string())
                    .then(a.evidence.cmp(&b.evidence))
            });
            all_findings.dedup_by(|a, b| a.category == b.category && a.evidence == b.evidence);

            let max_severity = all_findings
                .iter()
                .map(|f| f.severity)
                .max()
                .unwrap_or(VulnSeverity::Info);

            let clean = all_findings.is_empty();
            let summary = build_summary(&all_findings, static_run, llm_run, sandbox_run);
            let duration = start.elapsed();

            let report = VulnReport {
                session_id: session_id.clone(),
                ego_response_excerpt: ego_output.chars().take(200).collect(),
                findings: all_findings,
                max_severity,
                analysis_duration_ms: duration.as_millis() as u64,
                static_run,
                llm_run,
                sandbox_run,
                healing_suggestion: None,
                clean,
                summary,
            };

            {
                let mut reports = store.write().await;
                if reports.len() >= MAX_REPORTS {
                    reports.pop_front();
                }
                reports.push_back(report.clone());
            }

            if !report.clean {
                tracing::info!(
                    "Shadow found {} issue(s) (max severity: {}) in {}ms",
                    report.findings.len(),
                    report.max_severity,
                    report.analysis_duration_ms
                );
            }

            let _ = tx.send(ShadowEvent::Done { session_id, report }).await;
        });

        rx
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_shadow_disabled() {
        let store = create_report_store();
        let engine = ShadowEngine::with_config(
            store,
            ShadowConfig {
                enabled: false,
                ..Default::default()
            },
            OllamaClient::new(),
        );
        let mut rx = engine.analyze_async("test".into(), "hello".into());
        assert!(rx.recv().await.is_none());
    }

    #[tokio::test]
    async fn test_shadow_clean_output() {
        let store = create_report_store();
        let config = ShadowConfig {
            aggressiveness: 1,
            enabled: true,
            ..Default::default()
        };
        let engine = ShadowEngine::with_config(store.clone(), config, OllamaClient::new());

        let mut rx = engine.analyze_async(
            "test".into(),
            "```rust\nfn greet() -> String { \"hello\".to_string() }\n```".into(),
        );

        let mut got_done = false;
        while let Some(event) = rx.recv().await {
            if let ShadowEvent::Done { report, .. } = event {
                got_done = true;
                assert!(report.clean);
                assert!(report.static_run);
            }
        }
        assert!(got_done);
    }

    #[tokio::test]
    async fn test_shadow_detects_eval() {
        let store = create_report_store();
        let config = ShadowConfig {
            aggressiveness: 1,
            enabled: true,
            ..Default::default()
        };
        let engine = ShadowEngine::with_config(store.clone(), config, OllamaClient::new());

        let mut rx = engine.analyze_async("vuln".into(), "```js\neval(userInput);\n```".into());

        let mut found = false;
        while let Some(event) = rx.recv().await {
            if let ShadowEvent::Finding { .. } = event {
                found = true;
            }
        }
        assert!(found);

        let reports = store.read().await;
        assert_eq!(reports.len(), 1);
        assert!(!reports[0].clean);
    }

    #[tokio::test]
    async fn test_report_store_bounded() {
        let store = create_report_store();
        let mut reports = store.write().await;
        for i in 0..MAX_REPORTS + 5 {
            reports.push_back(VulnReport::clean(
                format!("s-{i}"),
                "test".into(),
                std::time::Duration::from_millis(1),
            ));
            if reports.len() > MAX_REPORTS {
                reports.pop_front();
            }
        }
        assert_eq!(reports.len(), MAX_REPORTS);
    }
}