rma-analyzer 0.11.0

Code analysis and security scanning for Rust Monorepo Analyzer
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
//! Go-specific security vulnerability DETECTION rules
//!
//! Optimized for speed with:
//! - Single-pass AST traversal where possible
//! - Pre-compiled patterns with LazyLock
//! - HashSet for O(1) lookups
//! - No unnecessary allocations
//!
//! Categorized into:
//! - **Sinks (High Confidence)**: Precise detection of dangerous patterns
//! - **Review Hints (Low Confidence)**: Patterns that need human review

use crate::rules::{Rule, create_finding_with_confidence};
use crate::security::generic::{is_generated_file, is_test_or_fixture_file};
use regex::Regex;
use rma_common::{Confidence, Finding, Language, Severity};
use rma_parser::ParsedFile;
use std::collections::HashSet;
use std::sync::LazyLock;
use tree_sitter::Node;

// =============================================================================
// PRE-COMPILED PATTERNS (initialized once, reused)
// =============================================================================

/// Hardcoded credential patterns
static CREDENTIAL_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r#"(?i)(password|passwd|secret|api_?key|auth_?token|access_?token)\s*[:=]\s*["'][^"']{8,}["']"#).unwrap()
});

/// AWS-style keys
static AWS_KEY_PATTERN: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r#"AKIA[0-9A-Z]{16}"#).unwrap());

/// Weak hash functions
static WEAK_HASH_IMPORTS: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
    ["crypto/md5", "crypto/sha1", "crypto/des", "crypto/rc4"]
        .into_iter()
        .collect()
});

/// Case-insensitive substring search without allocation
#[inline]
fn contains_ignore_case(haystack: &str, needle: &str) -> bool {
    if needle.len() > haystack.len() {
        return false;
    }
    haystack
        .as_bytes()
        .windows(needle.len())
        .any(|window| window.eq_ignore_ascii_case(needle.as_bytes()))
}

// =============================================================================
// MULTI-RULE SCANNER (Single AST pass for maximum speed)
// =============================================================================

/// Fast multi-rule scanner that checks all Go security rules in a single AST pass
pub struct GoSecurityScanner;

impl Rule for GoSecurityScanner {
    fn id(&self) -> &str {
        "go/security-scanner"
    }

    fn description(&self) -> &str {
        "Fast multi-rule Go security scanner (single AST pass)"
    }

    fn applies_to(&self, lang: Language) -> bool {
        lang == Language::Go
    }

    fn check(&self, parsed: &ParsedFile) -> Vec<Finding> {
        let mut findings = Vec::new();

        // Quick content checks to skip files that don't need detailed scanning
        let has_sql = parsed.content.contains("database/sql") || parsed.content.contains("\"sql\"");
        let has_exec = parsed.content.contains("os/exec");
        let has_http = parsed.content.contains("net/http");
        let has_unsafe = parsed.content.contains("\"unsafe\"");
        let has_crypto = parsed.content.contains("crypto/");
        let has_filepath = parsed.content.contains("filepath") || parsed.content.contains("path/");

        // Skip unsafe pointer checks for generated files (e.g., Kubernetes zz_generated_*)
        // These files use unsafe.Pointer intentionally for performance-critical conversions
        let skip_unsafe_check = is_generated_file(&parsed.path, &parsed.content);

        // Line-based checks (credentials, weak crypto imports)
        self.check_lines(parsed, &mut findings, has_crypto);

        // AST-based checks (single traversal)
        let mut cursor = parsed.tree.walk();
        self.traverse_ast(
            &mut cursor,
            parsed,
            &mut findings,
            has_sql,
            has_exec,
            has_http,
            has_unsafe && !skip_unsafe_check, // Skip unsafe check for generated files
            has_filepath,
        );

        findings
    }
}

impl GoSecurityScanner {
    /// Check lines for patterns (credentials, imports)
    fn check_lines(&self, parsed: &ParsedFile, findings: &mut Vec<Finding>, has_crypto: bool) {
        // Skip credential checks in test/fixture files - they commonly contain fake secrets
        if is_test_or_fixture_file(&parsed.path) {
            return;
        }

        for (line_num, line) in parsed.content.lines().enumerate() {
            // Hardcoded credentials
            if CREDENTIAL_PATTERN.is_match(line) {
                findings.push(create_line_based_finding(
                    "go/hardcoded-credential",
                    line_num + 1,
                    1,
                    &parsed.path,
                    line,
                    Severity::Critical,
                    "Hardcoded credential detected - use environment variables or secret management",
                    Language::Go,
                    Confidence::High,
                ));
            }

            // AWS keys
            if AWS_KEY_PATTERN.is_match(line) {
                findings.push(create_line_based_finding(
                    "go/aws-key-exposed",
                    line_num + 1,
                    1,
                    &parsed.path,
                    line,
                    Severity::Critical,
                    "AWS access key detected - rotate immediately and use IAM roles",
                    Language::Go,
                    Confidence::High,
                ));
            }

            // Weak crypto imports
            if has_crypto && line.contains("import") {
                for weak in WEAK_HASH_IMPORTS.iter() {
                    if line.contains(weak) {
                        findings.push(create_line_based_finding(
                            "go/weak-crypto",
                            line_num + 1,
                            1,
                            &parsed.path,
                            line,
                            Severity::Warning,
                            &format!(
                                "Weak crypto import: {} - use crypto/sha256 or stronger",
                                weak
                            ),
                            Language::Go,
                            Confidence::High,
                        ));
                    }
                }
            }
        }
    }

    /// Single-pass AST traversal checking multiple patterns
    #[allow(clippy::too_many_arguments)]
    fn traverse_ast(
        &self,
        cursor: &mut tree_sitter::TreeCursor,
        parsed: &ParsedFile,
        findings: &mut Vec<Finding>,
        has_sql: bool,
        has_exec: bool,
        has_http: bool,
        has_unsafe: bool,
        has_filepath: bool,
    ) {
        loop {
            let node = cursor.node();
            let kind = node.kind();

            match kind {
                "call_expression" => {
                    self.check_call_expression(
                        &node,
                        parsed,
                        findings,
                        has_sql,
                        has_exec,
                        has_http,
                        has_unsafe,
                        has_filepath,
                    );
                }
                "type_conversion_expression" if has_unsafe => {
                    self.check_type_conversion(&node, parsed, findings);
                }
                "short_var_declaration" => {
                    self.check_ignored_error(&node, parsed, findings);
                }
                _ => {}
            }

            // DFS traversal
            if cursor.goto_first_child() {
                continue;
            }
            loop {
                if cursor.goto_next_sibling() {
                    break;
                }
                if !cursor.goto_parent() {
                    return;
                }
            }
        }
    }

    /// Check call expressions for security issues
    #[allow(clippy::too_many_arguments)]
    fn check_call_expression(
        &self,
        node: &Node,
        parsed: &ParsedFile,
        findings: &mut Vec<Finding>,
        has_sql: bool,
        has_exec: bool,
        has_http: bool,
        has_unsafe: bool,
        has_filepath: bool,
    ) {
        let func = match node.child_by_field_name("function") {
            Some(f) => f,
            None => return,
        };
        let func_text = func.utf8_text(parsed.content.as_bytes()).unwrap_or("");

        // Command injection
        if has_exec && (func_text.ends_with("exec.Command") || func_text == "Command") {
            self.check_command_injection(node, parsed, findings);
        }

        // SQL injection
        if has_sql && contains_ignore_case(func_text, "sprintf") {
            self.check_sql_injection(node, parsed, findings);
        }

        // Unsafe pointer
        if has_unsafe && func_text.contains("unsafe.Pointer") {
            findings.push(create_finding_with_confidence(
                "go/unsafe-pointer",
                node,
                &parsed.path,
                &parsed.content,
                Severity::Warning,
                "unsafe.Pointer bypasses Go's type safety - ensure this is necessary",
                Language::Go,
                Confidence::High,
            ));
        }

        // Insecure HTTP server
        if has_http && func_text.ends_with("ListenAndServe") && !func_text.contains("TLS") {
            findings.push(create_finding_with_confidence(
                "go/insecure-http",
                node,
                &parsed.path,
                &parsed.content,
                Severity::Warning,
                "HTTP server without TLS - use ListenAndServeTLS for production",
                Language::Go,
                Confidence::High,
            ));
        }

        // SSRF check - http.Get/Post with variable
        if has_http
            && (func_text.ends_with("http.Get") || func_text.ends_with("http.Post"))
            && let Some(args) = node.child_by_field_name("arguments")
        {
            let args_text = args.utf8_text(parsed.content.as_bytes()).unwrap_or("");
            // Check if URL is a variable (not a string literal)
            if !args_text.starts_with("(\"") && !args_text.contains("\"http") {
                findings.push(create_finding_with_confidence(
                    "go/ssrf",
                    node,
                    &parsed.path,
                    &parsed.content,
                    Severity::Warning,
                    "HTTP request with variable URL - validate URL to prevent SSRF",
                    Language::Go,
                    Confidence::Medium,
                ));
            }
        }

        // Path traversal
        if has_filepath
            && (func_text.contains("filepath.Join")
                || func_text.contains("os.Open")
                || func_text.contains("ioutil.ReadFile"))
            && let Some(args) = node.child_by_field_name("arguments")
        {
            let args_text = args.utf8_text(parsed.content.as_bytes()).unwrap_or("");
            // Check for user input patterns
            if args_text.contains("request")
                || args_text.contains("param")
                || args_text.contains("input")
            {
                findings.push(create_finding_with_confidence(
                    "go/path-traversal",
                    node,
                    &parsed.path,
                    &parsed.content,
                    Severity::Warning,
                    "File operation with user input - validate path to prevent traversal",
                    Language::Go,
                    Confidence::Medium,
                ));
            }
        }

        // Weak crypto usage
        if func_text.contains("md5.") || func_text.contains("sha1.") {
            findings.push(create_finding_with_confidence(
                "go/weak-hash",
                node,
                &parsed.path,
                &parsed.content,
                Severity::Warning,
                "Weak hash function - use sha256 or stronger for security",
                Language::Go,
                Confidence::High,
            ));
        }
    }

    /// Check for command injection patterns
    fn check_command_injection(
        &self,
        node: &Node,
        parsed: &ParsedFile,
        findings: &mut Vec<Finding>,
    ) {
        let args = match node.child_by_field_name("arguments") {
            Some(a) => a,
            None => return,
        };
        let args_text = args.utf8_text(parsed.content.as_bytes()).unwrap_or("");

        let is_shell = args_text.contains("\"sh\"")
            || args_text.contains("\"bash\"")
            || args_text.contains("\"/bin/sh\"")
            || args_text.contains("\"/bin/bash\"");

        let has_shell_mode = args_text.contains("\"-c\"");

        if is_shell && has_shell_mode {
            // Check for dynamic arguments
            let context_start = node.start_byte().saturating_sub(500);
            let context_end = (node.end_byte() + 300).min(parsed.content.len());
            let context = &parsed.content[context_start..context_end];

            let has_dynamic = context.contains("fmt.Sprintf")
                || context.contains("+ \"")
                || context.contains("userInput")
                || context.contains("user_input")
                || context.contains("request.");

            if has_dynamic {
                findings.push(create_finding_with_confidence(
                    "go/command-injection",
                    node,
                    &parsed.path,
                    &parsed.content,
                    Severity::Critical,
                    "Command injection: shell -c with dynamic input - validate/escape input",
                    Language::Go,
                    Confidence::High,
                ));
            } else {
                findings.push(create_finding_with_confidence(
                    "go/command-injection",
                    node,
                    &parsed.path,
                    &parsed.content,
                    Severity::Warning,
                    "Shell command with -c mode - ensure arguments are trusted",
                    Language::Go,
                    Confidence::Medium,
                ));
            }
        }
    }

    /// Check for SQL injection patterns
    fn check_sql_injection(&self, node: &Node, parsed: &ParsedFile, findings: &mut Vec<Finding>) {
        let text = match node.utf8_text(parsed.content.as_bytes()) {
            Ok(t) => t,
            Err(_) => return,
        };

        if contains_ignore_case(text, "select ")
            || contains_ignore_case(text, "insert ")
            || contains_ignore_case(text, "update ")
            || contains_ignore_case(text, "delete ")
        {
            findings.push(create_finding_with_confidence(
                "go/sql-injection",
                node,
                &parsed.path,
                &parsed.content,
                Severity::Critical,
                "SQL query built with fmt.Sprintf - use parameterized queries",
                Language::Go,
                Confidence::High,
            ));
        }
    }

    /// Check for unsafe type conversions
    fn check_type_conversion(&self, node: &Node, parsed: &ParsedFile, findings: &mut Vec<Finding>) {
        if let Ok(text) = node.utf8_text(parsed.content.as_bytes())
            && text.contains("unsafe.Pointer")
        {
            findings.push(create_finding_with_confidence(
                "go/unsafe-pointer",
                node,
                &parsed.path,
                &parsed.content,
                Severity::Warning,
                "Conversion to unsafe.Pointer - requires careful review",
                Language::Go,
                Confidence::High,
            ));
        }
    }

    /// Check for ignored errors
    fn check_ignored_error(&self, node: &Node, parsed: &ParsedFile, findings: &mut Vec<Finding>) {
        if let Ok(text) = node.utf8_text(parsed.content.as_bytes())
            && text.contains(", _")
            && text.contains(":=")
            && !text.contains("err")
        {
            findings.push(create_finding_with_confidence(
                "go/ignored-error",
                node,
                &parsed.path,
                &parsed.content,
                Severity::Info,
                "Consider handling the error instead of discarding with _",
                Language::Go,
                Confidence::Low,
            ));
        }
    }
}

// =============================================================================
// INDIVIDUAL RULES (kept for backwards compatibility and granular control)
// =============================================================================

/// Detects command injection patterns
pub struct CommandInjectionRule;

impl Rule for CommandInjectionRule {
    fn id(&self) -> &str {
        "go/command-injection"
    }
    fn description(&self) -> &str {
        "Detects command injection patterns"
    }
    fn applies_to(&self, lang: Language) -> bool {
        lang == Language::Go
    }

    fn check(&self, parsed: &ParsedFile) -> Vec<Finding> {
        // Delegate to scanner for this specific check
        if !parsed.content.contains("os/exec") {
            return Vec::new();
        }
        let scanner = GoSecurityScanner;
        scanner
            .check(parsed)
            .into_iter()
            .filter(|f| f.rule_id == "go/command-injection")
            .collect()
    }
}

/// Detects SQL injection patterns
pub struct SqlInjectionRule;

impl Rule for SqlInjectionRule {
    fn id(&self) -> &str {
        "go/sql-injection"
    }
    fn description(&self) -> &str {
        "Detects SQL injection patterns"
    }
    fn applies_to(&self, lang: Language) -> bool {
        lang == Language::Go
    }

    fn check(&self, parsed: &ParsedFile) -> Vec<Finding> {
        if !parsed.content.contains("database/sql") && !parsed.content.contains("\"sql\"") {
            return Vec::new();
        }
        let scanner = GoSecurityScanner;
        scanner
            .check(parsed)
            .into_iter()
            .filter(|f| f.rule_id == "go/sql-injection")
            .collect()
    }
}

/// Detects unsafe pointer usage
pub struct UnsafePointerRule;

impl Rule for UnsafePointerRule {
    fn id(&self) -> &str {
        "go/unsafe-pointer"
    }
    fn description(&self) -> &str {
        "Detects unsafe.Pointer usage"
    }
    fn applies_to(&self, lang: Language) -> bool {
        lang == Language::Go
    }

    fn check(&self, parsed: &ParsedFile) -> Vec<Finding> {
        // Skip generated files - they use unsafe.Pointer intentionally
        if is_generated_file(&parsed.path, &parsed.content) {
            return Vec::new();
        }
        if !parsed.content.contains("\"unsafe\"") {
            return Vec::new();
        }
        let scanner = GoSecurityScanner;
        scanner
            .check(parsed)
            .into_iter()
            .filter(|f| f.rule_id == "go/unsafe-pointer")
            .collect()
    }
}

/// Detects insecure HTTP servers
pub struct InsecureHttpRule;

impl Rule for InsecureHttpRule {
    fn id(&self) -> &str {
        "go/insecure-http"
    }
    fn description(&self) -> &str {
        "Detects HTTP servers without TLS"
    }
    fn applies_to(&self, lang: Language) -> bool {
        lang == Language::Go
    }

    fn check(&self, parsed: &ParsedFile) -> Vec<Finding> {
        if !parsed.content.contains("net/http") {
            return Vec::new();
        }
        let scanner = GoSecurityScanner;
        scanner
            .check(parsed)
            .into_iter()
            .filter(|f| f.rule_id == "go/insecure-http")
            .collect()
    }
}

/// Detects ignored errors
pub struct IgnoredErrorHint;

impl Rule for IgnoredErrorHint {
    fn id(&self) -> &str {
        "go/ignored-error-hint"
    }
    fn description(&self) -> &str {
        "Detects ignored error values"
    }
    fn applies_to(&self, lang: Language) -> bool {
        lang == Language::Go
    }

    fn check(&self, parsed: &ParsedFile) -> Vec<Finding> {
        let scanner = GoSecurityScanner;
        scanner
            .check(parsed)
            .into_iter()
            .filter(|f| f.rule_id == "go/ignored-error")
            .collect()
    }
}

// =============================================================================
// HELPER - Line-based finding creation
// =============================================================================

/// Create a finding from line/column numbers (for line-based scanning)
#[allow(clippy::too_many_arguments)]
fn create_line_based_finding(
    rule_id: &str,
    line: usize,
    column: usize,
    path: &std::path::Path,
    snippet: &str,
    severity: Severity,
    message: &str,
    language: Language,
    confidence: Confidence,
) -> Finding {
    let mut finding = Finding {
        id: format!("{}:{}:{}", rule_id, path.display(), line),
        rule_id: rule_id.to_string(),
        message: message.to_string(),
        severity,
        location: rma_common::SourceLocation::new(
            path.to_path_buf(),
            line,
            column,
            line,
            snippet.len().min(100),
        ),
        language,
        snippet: Some(snippet.trim().chars().take(200).collect()),
        suggestion: None,
        confidence,
        category: rma_common::FindingCategory::Security,
        fingerprint: None,
        properties: None,
    };
    finding.compute_fingerprint();
    finding
}

#[cfg(test)]
mod tests {
    use super::*;
    use rma_common::RmaConfig;
    use rma_parser::ParserEngine;
    use std::path::Path;

    #[test]
    fn test_unsafe_pointer_skipped_in_generated_files() {
        let config = RmaConfig::default();
        let parser = ParserEngine::new(config);

        // Code with unsafe.Pointer usage (common in Kubernetes generated code)
        let content = r#"
// Code generated by controller-gen. DO NOT EDIT.

package v1

import (
    "unsafe"
)

func Convert(in, out interface{}) {
    out = (*string)(unsafe.Pointer(in.(*string)))
}
"#;

        // In a zz_generated file - should NOT produce findings
        let parsed_generated = parser
            .parse_file(
                Path::new("/project/pkg/apis/v1/zz_generated.conversion.go"),
                content,
            )
            .unwrap();
        let scanner = GoSecurityScanner;
        let findings_generated = scanner.check(&parsed_generated);

        let unsafe_findings: Vec<_> = findings_generated
            .iter()
            .filter(|f| f.rule_id == "go/unsafe-pointer")
            .collect();

        assert!(
            unsafe_findings.is_empty(),
            "Should skip unsafe.Pointer in generated files, but found: {:?}",
            unsafe_findings
        );

        // In a regular file - SHOULD produce findings
        let regular_content = r#"
package main

import (
    "unsafe"
)

func Convert(in, out interface{}) {
    out = (*string)(unsafe.Pointer(in.(*string)))
}
"#;
        let parsed_regular = parser
            .parse_file(Path::new("/project/pkg/convert.go"), regular_content)
            .unwrap();
        let findings_regular = scanner.check(&parsed_regular);

        let unsafe_findings_regular: Vec<_> = findings_regular
            .iter()
            .filter(|f| f.rule_id == "go/unsafe-pointer")
            .collect();

        assert!(
            !unsafe_findings_regular.is_empty(),
            "Should detect unsafe.Pointer in regular files"
        );
    }

    #[test]
    fn test_command_injection_detection() {
        let config = RmaConfig::default();
        let parser = ParserEngine::new(config);

        let content = r#"
package main

import (
    "os/exec"
    "fmt"
)

func runCommand(userInput string) {
    cmd := fmt.Sprintf("echo %s", userInput)
    exec.Command("sh", "-c", cmd).Run()
}
"#;

        let parsed = parser.parse_file(Path::new("main.go"), content).unwrap();
        let scanner = GoSecurityScanner;
        let findings = scanner.check(&parsed);

        let injection_findings: Vec<_> = findings
            .iter()
            .filter(|f| f.rule_id == "go/command-injection")
            .collect();

        assert!(
            !injection_findings.is_empty(),
            "Should detect injection pattern"
        );
    }

    #[test]
    fn test_hardcoded_credential() {
        let config = RmaConfig::default();
        let parser = ParserEngine::new(config);

        let content = r#"
package main

var password = "supersecret123"
var apiKey = "sk-1234567890abcdef"
"#;

        let parsed = parser.parse_file(Path::new("main.go"), content).unwrap();
        let scanner = GoSecurityScanner;
        let findings = scanner.check(&parsed);

        let cred_findings: Vec<_> = findings
            .iter()
            .filter(|f| f.rule_id == "go/hardcoded-credential")
            .collect();

        assert!(
            !cred_findings.is_empty(),
            "Should detect hardcoded credentials"
        );
    }

    #[test]
    fn test_weak_crypto() {
        let config = RmaConfig::default();
        let parser = ParserEngine::new(config);

        let content = r#"
package main

import "crypto/md5"

func hash(data []byte) []byte {
    h := md5.New()
    h.Write(data)
    return h.Sum(nil)
}
"#;

        let parsed = parser.parse_file(Path::new("main.go"), content).unwrap();
        let scanner = GoSecurityScanner;
        let findings = scanner.check(&parsed);

        let crypto_findings: Vec<_> = findings
            .iter()
            .filter(|f| f.rule_id.contains("crypto") || f.rule_id.contains("hash"))
            .collect();

        assert!(!crypto_findings.is_empty(), "Should detect weak crypto");
    }
}