repotoire 0.7.1

Graph-powered code analysis CLI. 110 detectors for security, architecture, bus factor, and code quality.
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
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
//! Secret Detection
//!
//! Detects hardcoded secrets, API keys, passwords, and tokens in source code.
//! CWE-798: Use of Hard-coded Credentials
//!
//! # Architecture
//!
//! Two scan paths, picked by file language:
//!
//! 1. **AST path** (Python, JS/TS, Rust, Go, Java, C#, C, C++): walks the
//!    tree-sitter parse tree, extracts `(name, value)` pairs from binding
//!    nodes (`assignment`, `variable_declarator`, `keyword_argument`, `pair`,
//!    `const_item`, `init_declarator`, ...) plus naked string literals, and
//!    matches a typed `SecretPattern` table against the value text directly.
//!    No regex on lines, no `line.contains("=")` heuristics — the AST already
//!    knows what's a name and what's a value.
//!
//! 2. **Line path** (`.env`, `.yml`, `.json`, `.toml`, `.ini`, `.conf`, Ruby,
//!    PHP, Kotlin, Swift, ...): runs the legacy line-based regex scanner on
//!    raw content for languages without tree-sitter grammars. Config formats
//!    have no docstrings, so masking isn't needed.
//!
//! Patterns split into two kinds:
//!
//! - `SelfShaped` — match by the secret's own format alone (AKIA, ghp_,
//!   sk_live_, SG., xox..., postgres://user:pwd@host, BEGIN PRIVATE KEY).
//!   Applied to every string literal regardless of surrounding name.
//! - `NameGated` — require a sensitive LHS name (`password`, `secret`,
//!   `api_key`, etc.) AND a string value of sufficient length/content.
//!   Eliminates the previous regex's value-type guesswork (`if first char
//!   is "`/`'`/`b`...`) since the AST tells us if the RHS is a literal.
//!
//! This is the proper structural fix for QA audit finding #1: the previous
//! detector ran regex over `masked_content`, which had already replaced every
//! string literal's contents with spaces — the very place secrets live.

use crate::detectors::ast_fingerprint::parse_root_ext;
use crate::detectors::base::{is_test_file, Detector, DetectorConfig};
use crate::detectors::security::scan_inputs::{ScanAstInputs, ScanInputs};
use crate::graph::GraphQueryExt;
use crate::models::{Finding, Severity};
use crate::parsers::lightweight::Language;
use anyhow::Result;
use regex::Regex;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;
use tracing::debug;

/// Secret patterns split by matching strategy.
///
/// `SelfShaped` patterns identify a secret by its own format alone (e.g. AWS
/// `AKIA[0-9A-Z]{16}`, GitHub `ghp_[a-zA-Z0-9]{36}`); they apply to every
/// string-literal value regardless of the surrounding name.
///
/// `NameGated` patterns require BOTH a sensitive LHS name (matched by
/// `name_gate`) AND a value matching `value_regex`. The AST scanner already
/// knows what is a name and what is a value, so unlike the old `\s*[=:]\s*`-
/// shaped regex these never need to re-derive structure from text.
///
/// The line-based scanner (used for non-AST languages) reconstructs a
/// `name=value`-shaped synthesis from each pattern via `to_legacy_line_regex()`
/// so it can keep matching against whole lines for `.env`, `.yml`, `.toml`, etc.
enum SecretPattern {
    SelfShaped {
        name: &'static str,
        value_regex: Regex,
        severity: Severity,
    },
    NameGated {
        name: &'static str,
        /// Lowercased name regex applied to the LHS identifier text.
        /// Consumed by the AST scanner — the line-based scanner currently
        /// hard-codes the legacy combined form in `to_legacy_line_regex`.
        #[allow(dead_code)]
        name_gate: Regex,
        /// Regex applied to the value content (between the quotes).
        value_regex: Regex,
        severity: Severity,
    },
}

impl SecretPattern {
    fn finding_name(&self) -> &'static str {
        match self {
            SecretPattern::SelfShaped { name, .. } | SecretPattern::NameGated { name, .. } => name,
        }
    }

    fn severity(&self) -> Severity {
        match self {
            SecretPattern::SelfShaped { severity, .. }
            | SecretPattern::NameGated { severity, .. } => *severity,
        }
    }

    /// Reconstruct a legacy `name\s*[=:]\s*"value"` -shaped regex for the
    /// line-based scanner. Self-shaped patterns just return their value regex
    /// (they don't need a name on the line).
    fn to_legacy_line_regex(&self) -> Regex {
        match self {
            SecretPattern::SelfShaped { value_regex, .. } => value_regex.clone(),
            SecretPattern::NameGated {
                name_gate: _,
                value_regex,
                name,
                ..
            } => {
                // Build a permissive line regex for the legacy line path. We
                // can't easily compose two regexes, so for the few NameGated
                // patterns we hard-code the legacy form here (it matched the
                // original `r"(?i)(secret|password|...)\s*[=:]\s*[...]"`).
                match *name {
                    "Generic Secret" => {
                        Regex::new(r#"(?i)(secret|password|passwd|pwd)\s*[=:]\s*["'][^"']{8,}["']"#)
                            .expect("valid regex")
                    }
                    "Generic API Key" => {
                        Regex::new(r"(?i)api[_-]?key\s*[=:]\s*[a-zA-Z0-9_\-]{20,}")
                            .expect("valid regex")
                    }
                    "AWS Secret Access Key" => {
                        Regex::new(r"(?i)aws_secret_access_key\s*[=:]\s*[A-Za-z0-9/+=]{40}")
                            .expect("valid regex")
                    }
                    _ => value_regex.clone(),
                }
            }
        }
    }
}

/// Master pattern table.
///
/// Order matters: when multiple patterns match the same value (e.g. an AWS
/// secret access key value also matches `Generic API Key`'s relaxed pattern),
/// we report only the FIRST one matched per `(line, value)` pair. The most
/// specific patterns are listed first.
static SECRET_PATTERNS: LazyLock<Vec<SecretPattern>> = LazyLock::new(|| {
    vec![
        // ---------- Self-shaped (match by value alone) ----------
        SecretPattern::SelfShaped {
            name: "AWS Access Key ID",
            value_regex: Regex::new(r"AKIA[0-9A-Z]{16}").expect("valid regex"),
            severity: Severity::Critical,
        },
        SecretPattern::SelfShaped {
            name: "GitHub Token",
            value_regex: Regex::new(r"ghp_[a-zA-Z0-9]{36}").expect("valid regex"),
            severity: Severity::Critical,
        },
        SecretPattern::SelfShaped {
            name: "Private Key",
            value_regex: Regex::new(r"-----BEGIN (RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----")
                .expect("valid regex"),
            severity: Severity::Critical,
        },
        SecretPattern::SelfShaped {
            name: "Slack Token",
            value_regex: Regex::new(r"xox[baprs]-[0-9]{10,13}-[0-9]{10,13}[a-zA-Z0-9-]*")
                .expect("valid regex"),
            severity: Severity::Critical,
        },
        SecretPattern::SelfShaped {
            name: "Stripe API Key",
            value_regex: Regex::new(r"sk_live_[a-zA-Z0-9]{24,}").expect("valid regex"),
            severity: Severity::Critical,
        },
        SecretPattern::SelfShaped {
            name: "Database URL with Password",
            value_regex: Regex::new(r"(?i)(postgres|mysql|mongodb|redis)://[^:]+:[^@]+@")
                .expect("valid regex"),
            severity: Severity::Critical,
        },
        SecretPattern::SelfShaped {
            name: "SendGrid API Key",
            value_regex: Regex::new(r"SG\.[a-zA-Z0-9_-]{22}\.[a-zA-Z0-9_-]{43}")
                .expect("valid regex"),
            severity: Severity::High,
        },
        // ---------- Name-gated (require sensitive name + value) ----------
        SecretPattern::NameGated {
            name: "AWS Secret Access Key",
            name_gate: Regex::new(r"(?i)aws.?secret.?access.?key").expect("valid regex"),
            value_regex: Regex::new(r"^[A-Za-z0-9/+=]{40}$").expect("valid regex"),
            severity: Severity::Critical,
        },
        SecretPattern::NameGated {
            name: "Generic API Key",
            name_gate: Regex::new(r"(?i)^api[_-]?key$").expect("valid regex"),
            value_regex: Regex::new(r"^[a-zA-Z0-9_\-]{20,}$").expect("valid regex"),
            severity: Severity::High,
        },
        SecretPattern::NameGated {
            name: "Generic Secret",
            // Match any name that contains password/secret/passwd/pwd/token,
            // but not pure prefix/suffix words like "passwords" or hash names
            // (handled by FP filters on the value side).
            name_gate: Regex::new(r"(?i)(^|_)(secret|password|passwd|pwd|token)($|_)")
                .expect("valid regex"),
            value_regex: Regex::new(r"^[^\s]{8,}$").expect("valid regex"),
            severity: Severity::High,
        },
    ]
});

pub struct SecretDetector {
    #[allow(dead_code)] // Part of detector pattern
    config: DetectorConfig,
    #[allow(dead_code)] // Part of detector pattern, used for file scanning
    repository_path: PathBuf,
    max_findings: usize,
}

impl SecretDetector {
    pub fn new(repository_path: impl Into<PathBuf>) -> Self {
        Self {
            config: DetectorConfig::default(),
            repository_path: repository_path.into(),
            max_findings: 100,
        }
    }

    fn relative_path(&self, path: &Path) -> PathBuf {
        crate::detectors::detector_relative_path(&self.repository_path, path)
    }

    /// Check if a Python os.environ.get() or os.getenv() call has a fallback (second argument)
    /// Pattern: os.environ.get("KEY", "fallback") or os.getenv("KEY", "fallback")
    fn has_python_env_fallback(line: &str) -> bool {
        // Look for the pattern: os.environ.get( or os.getenv( followed by args with a comma
        // This indicates a default value is provided
        let line_lower = line.to_lowercase();

        for pattern in ["os.environ.get(", "os.getenv("] {
            if let Some(start) = line_lower.find(pattern) {
                let after_pattern = &line[start + pattern.len()..];
                // Count parentheses to find the matching close
                let mut depth = 1;
                let mut found_comma_at_depth_1 = false;

                for ch in after_pattern.chars() {
                    match ch {
                        '(' => depth += 1,
                        ')' => {
                            depth -= 1;
                            if depth == 0 {
                                break;
                            }
                        }
                        ',' if depth == 1 => {
                            found_comma_at_depth_1 = true;
                            break;
                        }
                        _ => {}
                    }
                }

                if found_comma_at_depth_1 {
                    return true;
                }
            }
        }

        false
    }

    /// Check if a Go os.Getenv() call has fallback handling on the same line
    /// Common patterns:
    /// - `if val := os.Getenv("X"); val == "" { ... }` (short variable declaration with check)
    /// - `val := os.Getenv("X"); if val == "" { val = "default" }`
    /// - Using with a helper: `getEnvOr(os.Getenv("X"), "default")`
    /// - Ternary-style: `func() string { if v := os.Getenv("X"); v != "" { return v }; return "default" }()`
    fn has_go_env_fallback(line: &str) -> bool {
        // Check for common fallback indicators on the same line
        let has_empty_check = line.contains(r#"== """#) || line.contains(r#"!= """#);
        let has_if_statement = line.contains("if ");
        let has_fallback_helper = line.to_lowercase().contains("getenvdefault")
            || line.to_lowercase().contains("getenvor")
            || line.to_lowercase().contains("envdefault");

        has_fallback_helper || (has_empty_check && has_if_statement)
    }

    fn scan_file(&self, path: &Path, content: &str) -> Vec<Finding> {
        let mut findings = vec![];

        // Skip test files - they often contain test certificates/keys
        if is_test_file(path) {
            return findings;
        }

        // Skip binary files
        if content.contains('\0') {
            return findings;
        }

        let lines: Vec<&str> = content.lines().collect();
        for (line_num, line) in lines.iter().enumerate() {
            let prev_line = if line_num > 0 {
                Some(lines[line_num - 1])
            } else {
                None
            };
            if crate::detectors::is_line_suppressed(line, prev_line) {
                continue;
            }

            // Skip comments that look like documentation
            let trimmed = line.trim();
            if trimmed.starts_with("//") && trimmed.contains("example") {
                continue;
            }

            findings.extend(self.try_match_line(path, line, line_num));
        }

        findings
    }

    /// Legacy line-based scanner for non-AST languages.
    ///
    /// Used ONLY by `scan_file()` for file types without a tree-sitter
    /// grammar in our dispatch list (`.env`, `.yml`, `.json`, `.toml`,
    /// `.ini`, `.conf`, Ruby, PHP, Kotlin, Swift, ...). For those formats
    /// we fall back to running each pattern's `to_legacy_line_regex()`
    /// against the raw line and applying contextual filters (placeholder
    /// text, env-var fallbacks, value-type checks, severity downgrades).
    ///
    /// **Not used by AST-eligible languages** (Python, JS/TS, Rust, Go,
    /// Java, C#, C, C++) — those go through `scan_file_ast()` →
    /// `match_pattern_against_binding()`, which operates on AST-extracted
    /// `(name, value)` pairs and does not regex-reverse-engineer line
    /// shape. The `line.find('=')` / `value_part.contains('(')` heuristics
    /// below are a deliberate concession to the line scanner; they are
    /// inappropriate for AST-handled sources.
    fn try_match_line(&self, path: &Path, line: &str, line_num: usize) -> Vec<Finding> {
        let mut findings = Vec::new();

        for pattern in SECRET_PATTERNS.iter() {
            // The line scanner uses each pattern's "legacy" line regex
            // (`name\s*[=:]\s*"value"`) so it can keep matching whole-line
            // shapes for non-AST languages like .env, .yml, Ruby, etc. The
            // AST scanner uses `name_gate` + `value_regex` separately.
            let line_regex = pattern.to_legacy_line_regex();
            if let Some(m) = line_regex.find(line) {
                let matched = m.as_str();

                // Skip obvious false positives
                if matched.len() < 10 {
                    continue;
                }
                if matched.contains("example") || matched.contains("EXAMPLE") {
                    continue;
                }
                if matched.contains("placeholder") || matched.contains("xxxx") {
                    continue;
                }

                // Skip placeholder patterns (setup templates, documentation)
                let matched_lower = matched.to_lowercase();
                if matched_lower.contains("your-")
                    || matched_lower.contains("-here")
                    || matched_lower.contains("changeme")
                    || matched_lower.contains("replace")
                    || matched_lower.contains("todo")
                    || matched_lower.contains("fixme")
                    || matched == "sk-your-openai-key"
                    || matched_lower.starts_with("xxx")
                    || matched_lower.ends_with("xxx")
                {
                    continue;
                }

                // Skip shell variable substitutions: ${VAR_NAME}
                // Docker Compose, shell scripts use ${SECRET} as variable reference, not hardcoded
                if line.contains(&format!("${{{}", &matched.split('=').next().unwrap_or(""))) {
                    continue;
                }

                // Skip when value is reading from environment variables or headers
                // (not hardcoding — the value is fetched at runtime, not embedded in source)
                // Pattern: const secret = process.env.SECRET
                if line.contains("= process.env.") || line.contains("=process.env.") {
                    continue;
                }
                // Node/Deno: process.env["KEY"] or process.env.KEY
                if line.contains("process.env") {
                    continue;
                }
                // Rust: std::env::var("KEY") or env::var("KEY")
                if line.contains("env::var(") || line.contains("std::env::var") {
                    continue;
                }
                // HTTP headers: req.headers.get(), headers.get(), request.headers
                if line.contains("headers.get(")
                    || line.contains("req.headers.")
                    || line.contains("request.headers.")
                    || line.contains("headers[")
                {
                    continue;
                }
                // Python: os.environ["KEY"] or os.environ.get()
                if line.contains("os.environ[")
                    || line.contains("os.environ.get(")
                    || line.contains("os.getenv(")
                {
                    continue;
                }
                // Go: os.Getenv("KEY")
                if line.contains("os.Getenv(") || line.contains("os.LookupEnv(") {
                    continue;
                }

                // Value-type filtering for Generic Secret pattern:
                // Skip when the value is clearly not a secret (function call or collection literal)
                if pattern.finding_name() == "Generic Secret" {
                    // Extract the value part after = or :
                    let value_part = if let Some(eq_pos) = line.find('=') {
                        line[eq_pos + 1..].trim()
                    } else if let Some(colon_pos) = line.find(':') {
                        line[colon_pos + 1..].trim()
                    } else {
                        ""
                    };

                    if !value_part.is_empty() {
                        // Skip function/class calls: CharField(...), Signal(), SecretManager.from_config()
                        if value_part.contains('(') {
                            continue;
                        }
                        // Skip collection literals: [...], {...}
                        let first_char = value_part.chars().next().unwrap_or(' ');
                        if matches!(first_char, '[' | '{') {
                            continue;
                        }
                        // Skip variable references — a hardcoded secret MUST be a string literal
                        // Variables, attribute accesses, settings reads are NOT hardcoded
                        if !matches!(first_char, '"' | '\'' | '`' | 'b') {
                            // Not a string literal (b"..." for bytes is also a literal)
                            continue;
                        }
                        // If starts with b, check it's b"..." not a variable like `base64...`
                        if first_char == 'b' {
                            let second_char = value_part.chars().nth(1).unwrap_or(' ');
                            if !matches!(second_char, '"' | '\'') {
                                continue;
                            }
                        }
                    }
                }

                // Determine effective severity based on context
                let line_lower = line.to_lowercase();
                let mut effective_severity = pattern.severity();

                // Dev fallback pattern: process.env.X || 'fallback' or process.env.X ?? 'fallback'
                // These are typically local dev defaults, not production credentials
                if (line_lower.contains("process.env")
                    && (line.contains("||") || line.contains("??")))
                    // Python fallback patterns: os.environ.get("KEY", "fallback") or os.getenv("KEY", "fallback")
                    // The second argument is the default value, indicating a fallback
                    || ((line_lower.contains("os.environ.get(")
                        || line_lower.contains("os.getenv("))
                        && Self::has_python_env_fallback(line))
                    // Go fallback patterns: os.Getenv with fallback handling
                    // os.LookupEnv returns (value, found) - implies fallback handling
                    // Also check for common inline fallback patterns
                    || line.contains("os.LookupEnv(")
                    || (line.contains("os.Getenv(") && Self::has_go_env_fallback(line))
                    // Localhost URLs are lower risk - typically dev/test environments
                    || matched.contains("localhost")
                    || matched.contains("127.0.0.1")
                {
                    effective_severity = Severity::Low;
                }
                // Check file path for seed/script/test/example patterns
                else if let Some(rel_path) = path.to_str() {
                    let rel_lower = rel_path.to_lowercase();
                    if rel_lower.contains("/seed")
                        || rel_lower.contains("/script")
                        || rel_lower.contains("/fixture")
                        || rel_lower.contains("/examples/")
                        || rel_lower.contains("/example/")
                        || rel_lower.contains("/demo/")
                        || rel_lower.contains("/samples/")
                        || rel_lower.contains("/sample/")
                        || rel_lower.contains(".seed.")
                        || rel_lower.contains(".script.")
                        || rel_lower.contains(".example.")
                        || rel_lower.contains(".sample.")
                    {
                        effective_severity = Severity::Low;
                    }
                }

                let line_start = line_num as u32 + 1;
                findings.push(Finding {
                    id: String::new(),
                    detector: "SecretDetector".to_string(),
                    severity: effective_severity,
                    title: format!("Hardcoded {}", pattern.finding_name()),
                    description: format!(
                        "Potential {} found in source code at line {}. \
                        Secrets should be stored in environment variables or secret management systems.",
                        pattern.finding_name(), line_start
                    ),
                    affected_files: vec![self.relative_path(path)],
                    line_start: Some(line_start),
                    line_end: Some(line_start),
                    suggested_fix: Some("Move this secret to an environment variable or secrets manager".to_string()),
                    estimated_effort: Some("15 minutes".to_string()),
                    category: Some("security".to_string()),
                    cwe_id: Some("CWE-798".to_string()),
                    why_it_matters: Some("Hardcoded secrets can be extracted from source code, leading to credential theft".to_string()),
                    ..Default::default()
                });
            }
        }

        findings
    }

    /// AST-first scanner.
    ///
    /// Walks the tree-sitter parse tree once and emits one `Binding` per
    /// "(name?, string-literal value)" pair, then matches the typed
    /// `SecretPattern` set against `(name, value)` directly. No regex over
    /// lines, no `line.find('=')` heuristics — the AST is the source of
    /// truth for what is a name and what is a value.
    ///
    /// This is the proper structural fix for QA audit finding #1: previously
    /// `masked_content` had stripped every string literal to spaces before
    /// the regex saw the line, making secrets in Python/JS/TS/Rust/Go/Java/
    /// C#/C/C++ structurally undetectable.
    fn scan_file_ast(&self, inputs: &ScanAstInputs<'_>) -> Vec<Finding> {
        let path = inputs.path();
        let content = inputs.content();
        let ext = inputs.ext();
        let lang = inputs.lang;
        let cached_tree = inputs.cached_tree;
        let mut findings = vec![];

        if is_test_file(path) {
            return findings;
        }
        if content.contains('\0') {
            return findings;
        }

        // Reuse the cached tree from the main parse phase when available.
        // Fall back to a fresh parse only on cache miss (test fixtures, files
        // added after graph build).
        let owned;
        let root = match cached_tree {
            Some(tree) => tree.root_node(),
            None => match parse_root_ext(content, lang, ext) {
                Some(t) => {
                    owned = t;
                    owned.root_node()
                }
                None => return findings,
            },
        };

        let bytes = content.as_bytes();
        let mut bindings: Vec<Binding> = Vec::new();
        collect_bindings(root, bytes, &mut bindings);

        let lines: Vec<&str> = content.lines().collect();

        for b in bindings {
            // Per-line suppression markers (// repotoire-ignore, etc.) still
            // apply on the AST path. We have to pull the line text to check.
            let line = match lines.get(b.line) {
                Some(l) => *l,
                None => continue,
            };
            let prev_line = if b.line > 0 {
                Some(lines[b.line - 1])
            } else {
                None
            };
            if crate::detectors::is_line_suppressed(line, prev_line) {
                continue;
            }

            for pat in SECRET_PATTERNS.iter() {
                if let Some(finding) = match_pattern_against_binding(self, path, pat, &b) {
                    findings.push(finding);
                    // First match wins per binding — patterns are ordered
                    // most-specific-first so we don't report a less specific
                    // pattern overlapping a more specific one.
                    break;
                }
            }
        }

        findings
    }
}

/// One AST-extracted (optional name, string value) pair.
///
/// Emitted by `collect_bindings` for every binding/literal node we care about.
/// `name` is `Some(lowercased_text)` for keyed bindings (Python `assignment`,
/// JS `variable_declarator`, etc.) and `None` for naked string literals
/// outside any binding (where only self-shaped patterns can apply).
struct Binding<'a> {
    /// LHS identifier text, lowercased, with leading `self.` / `this.` /
    /// scope prefixes stripped (so `self._password` becomes `_password`).
    /// `None` if this is a naked literal.
    name: Option<String>,
    /// Value content WITHOUT surrounding quotes, taken from raw source.
    value: &'a str,
    /// 0-indexed line of the value's first byte. Used for finding location
    /// and as the surrounding line for `is_line_suppressed`.
    line: usize,
}

/// Match one pattern against one binding. Applies all FP filters on the
/// VALUE (not the surrounding line). Returns `Some(Finding)` if it's a
/// real hit, `None` otherwise.
fn match_pattern_against_binding(
    detector: &SecretDetector,
    path: &Path,
    pattern: &SecretPattern,
    binding: &Binding<'_>,
) -> Option<Finding> {
    // Shared FP filters on the value. These are precise: they look at the
    // EXACT string-literal content the AST extracted.
    if !value_passes_fp_filters(binding.value) {
        return None;
    }

    // Pattern-specific match logic.
    let value_regex = match pattern {
        SecretPattern::SelfShaped { value_regex, .. } => value_regex,
        SecretPattern::NameGated {
            name_gate,
            value_regex,
            ..
        } => {
            // Name-gated patterns require an LHS name AND that name to
            // match the gate. Naked literals never match.
            let name = binding.name.as_deref()?;
            if !name_gate.is_match(name) {
                return None;
            }
            value_regex
        }
    };

    if !value_regex.is_match(binding.value) {
        return None;
    }

    // Path-based severity downgrade: seed/example/sample/demo files are
    // typically test fixtures or templates, not real production secrets.
    let mut severity = pattern.severity();
    if let Some(rel_path) = path.to_str() {
        let rel_lower = rel_path.to_lowercase();
        if rel_lower.contains("/seed")
            || rel_lower.contains("/script")
            || rel_lower.contains("/fixture")
            || rel_lower.contains("/examples/")
            || rel_lower.contains("/example/")
            || rel_lower.contains("/demo/")
            || rel_lower.contains("/samples/")
            || rel_lower.contains("/sample/")
            || rel_lower.contains(".seed.")
            || rel_lower.contains(".script.")
            || rel_lower.contains(".example.")
            || rel_lower.contains(".sample.")
        {
            severity = Severity::Low;
        }
    }
    // Localhost URLs: dev environment, low risk.
    if binding.value.contains("localhost") || binding.value.contains("127.0.0.1") {
        severity = Severity::Low;
    }

    let line_start = binding.line as u32 + 1;
    Some(Finding {
        id: String::new(),
        detector: "SecretDetector".to_string(),
        severity,
        title: format!("Hardcoded {}", pattern.finding_name()),
        description: format!(
            "Potential {} found in source code at line {}. \
            Secrets should be stored in environment variables or secret management systems.",
            pattern.finding_name(),
            line_start
        ),
        affected_files: vec![detector.relative_path(path)],
        line_start: Some(line_start),
        line_end: Some(line_start),
        suggested_fix: Some(
            "Move this secret to an environment variable or secrets manager".to_string(),
        ),
        estimated_effort: Some("15 minutes".to_string()),
        category: Some("security".to_string()),
        cwe_id: Some("CWE-798".to_string()),
        why_it_matters: Some(
            "Hardcoded secrets can be extracted from source code, leading to credential theft"
                .to_string(),
        ),
        ..Default::default()
    })
}

/// Filters that look only at the value text (between the quotes).
///
/// Replaces the previous regex match-on-line filters with precise checks
/// against what the AST extracted. Examples that should NOT be flagged:
///
/// - `password = "your-key-here"` (placeholder)
/// - `password = "CHANGEME"` (template)
/// - `password = "xxxxxxxx"` (filler)
/// - `password = "${SECRET}"` (shell variable substitution)
fn value_passes_fp_filters(value: &str) -> bool {
    if value.len() < 8 {
        return false;
    }

    let lower = value.to_lowercase();
    if lower.contains("example")
        || lower.contains("placeholder")
        || lower.contains("xxxx")
        || lower.contains("your-")
        || lower.contains("-here")
        || lower.contains("changeme")
        || lower.contains("replace")
        || lower.contains("todo")
        || lower.contains("fixme")
        || lower.starts_with("xxx")
        || lower.ends_with("xxx")
    {
        return false;
    }

    // Shell variable references (Docker Compose, .env templates).
    if value.starts_with("${") && value.ends_with('}') {
        return false;
    }

    true
}

// ---------------------------------------------------------------------------
// AST → Binding extraction
// ---------------------------------------------------------------------------

/// Walk the tree once and emit a `Binding` for every binding/literal node
/// whose value side is a string literal. The grammar-specific extraction
/// is centralized here so `scan_file_ast` is grammar-agnostic.
fn collect_bindings<'a>(node: tree_sitter::Node, source: &'a [u8], out: &mut Vec<Binding<'a>>) {
    let kind = node.kind();

    // 1) Keyed bindings: extract (name, value) pair when the value is a
    //    string literal directly.
    if let Some((name, value_node)) = extract_named_binding(node, source) {
        if is_string_node(value_node.kind()) && !is_likely_python_docstring(value_node) {
            if let Some(content) = string_node_content(value_node, source) {
                out.push(Binding {
                    name: Some(name),
                    value: content,
                    line: value_node.start_position().row,
                });
            }
        }
        // Don't return — children may contain nested bindings (e.g. a
        // `pair` whose value is another `assignment` in some grammars).
    }

    // 2) Naked string literals: collect every string-literal node not yet
    //    handled above (i.e. its parent isn't a binding we already paired).
    //    These can only match SelfShaped patterns.
    if is_string_node(kind) && !is_likely_python_docstring(node) {
        if !parent_is_handled_binding_value(node) {
            if let Some(content) = string_node_content(node, source) {
                out.push(Binding {
                    name: None,
                    value: content,
                    line: node.start_position().row,
                });
            }
        }
        // String content children hold no bindings — stop recursion.
        return;
    }

    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        collect_bindings(child, source, out);
    }
}

/// Extract `(name_text_lowercased, value_node)` when `node` is a binding
/// shape AND its value side is exactly a string literal (not an expression).
///
/// Returns `None` for non-binding nodes or bindings whose RHS is something
/// else (function call, member expression, identifier reference, etc.) —
/// the AST already filtered out the FP cases that the old regex had to
/// detect via `value_part.contains('(')` / first-char checks.
fn extract_named_binding<'a>(
    node: tree_sitter::Node<'a>,
    source: &'a [u8],
) -> Option<(String, tree_sitter::Node<'a>)> {
    match node.kind() {
        // Python: `password = "value"` (assignment), `func(api_key="v")`
        // (keyword_argument), `{"password": "v"}` (pair).
        // JS/TS: `x = "v"` (assignment_expression).
        "assignment" | "assignment_expression" | "assignment_statement" => {
            let name = extract_lhs_name(node.child_by_field_name("left")?, source)?;
            let value = node.child_by_field_name("right")?;
            Some((name, value))
        }
        "augmented_assignment" => {
            // `password += "v"` — treat the LHS as the binding name.
            let name = extract_lhs_name(node.child_by_field_name("left")?, source)?;
            let value = node.child_by_field_name("right")?;
            Some((name, value))
        }
        "keyword_argument" => {
            let name = node_text(node.child_by_field_name("name")?, source)?.to_lowercase();
            let value = node.child_by_field_name("value")?;
            Some((name, value))
        }
        "pair" => {
            // `{"password": "v"}` (Python dict / JS object).
            let key_node = node.child_by_field_name("key")?;
            let key_text = match key_node.kind() {
                "string" | "string_literal" => string_node_content(key_node, source)?,
                _ => node_text(key_node, source)?,
            };
            let value = node.child_by_field_name("value")?;
            Some((key_text.to_lowercase(), value))
        }
        // JS/TS: `const password = "v"`, `let api_key = "v"`, `var x = "v"`.
        // C#: same node kind for field declarations.
        // Java: inside `field_declaration` -> `variable_declarator`.
        "variable_declarator" => {
            let name_node = node.child_by_field_name("name")?;
            let name = node_text(name_node, source)?.to_lowercase();
            let value = node.child_by_field_name("value")?;
            Some((name, value))
        }
        // Go: `const Password = "v"`, `var ApiKey string = "v"`.
        "const_spec" | "var_spec" => {
            let name_node = node.child_by_field_name("name")?;
            let name = node_text(name_node, source)?.to_lowercase();
            let value = node.child_by_field_name("value")?;
            // Go's value field is an `expression_list` — drill in if it has
            // exactly one child that's a string literal.
            let value = if value.kind() == "expression_list" && value.named_child_count() == 1 {
                value.named_child(0)?
            } else {
                value
            };
            Some((name, value))
        }
        // Go: `x := "v"` (short_var_declaration).
        "short_var_declaration" => {
            let left = node.child_by_field_name("left")?;
            let right = node.child_by_field_name("right")?;
            let name = if left.kind() == "expression_list" && left.named_child_count() == 1 {
                node_text(left.named_child(0)?, source)?.to_lowercase()
            } else {
                node_text(left, source)?.to_lowercase()
            };
            let value = if right.kind() == "expression_list" && right.named_child_count() == 1 {
                right.named_child(0)?
            } else {
                right
            };
            Some((name, value))
        }
        // Go: `{key: "v"}` (composite literal element).
        "keyed_element" => {
            let key = node.named_child(0)?;
            let value = node.named_child(1)?;
            let key_text = match key.kind() {
                "string" | "string_literal" | "interpreted_string_literal" => {
                    string_node_content(key, source)?
                }
                _ => node_text(key, source)?,
            };
            Some((key_text.to_lowercase(), value))
        }
        // Rust: `let x = "v";` (let_declaration with .pattern + .value).
        "let_declaration" => {
            let pat = node.child_by_field_name("pattern")?;
            let name = extract_lhs_name(pat, source)?;
            let value = node.child_by_field_name("value")?;
            Some((name, value))
        }
        // Rust: `const NAME: TYPE = "v";` and `static NAME: TYPE = "v";`.
        // Both have .name (identifier) and .value (string_literal) fields.
        "const_item" | "static_item" => {
            let name_node = node.child_by_field_name("name")?;
            let name = node_text(name_node, source)?.to_lowercase();
            let value = node.child_by_field_name("value")?;
            Some((name, value))
        }
        // C / C++: `int x = "v";` -> `init_declarator { declarator, =, value }`.
        // The declarator may be a `pointer_declarator` containing the identifier.
        "init_declarator" => {
            let declarator = node.child_by_field_name("declarator")?;
            let name = extract_lhs_name(declarator, source)?;
            let value = node.child_by_field_name("value")?;
            Some((name, value))
        }
        // Python: `def f(password="default"):` — default_parameter has the
        // same .name/.value field shape as keyword_argument.
        "default_parameter" => {
            let name = node_text(node.child_by_field_name("name")?, source)?.to_lowercase();
            let value = node.child_by_field_name("value")?;
            Some((name, value))
        }
        // JS/TS class field declarations:
        //   class C { password = "v" }              -> field_definition
        //   class C { public password = "v" }       -> public_field_definition
        // Both expose .property (identifier) and .value.
        "field_definition" | "public_field_definition" => {
            let name_node = node.child_by_field_name("property")?;
            let name = node_text(name_node, source)?.to_lowercase();
            let value = node.child_by_field_name("value")?;
            Some((name, value))
        }
        // TypeScript: `enum E { Password = "v" }` -> enum_assignment.
        "enum_assignment" => {
            let name_node = node.child_by_field_name("name")?;
            let name = node_text(name_node, source)?.to_lowercase();
            let value = node.child_by_field_name("value")?;
            Some((name, value))
        }
        _ => None,
    }
}

/// Extract a usable "name" from an LHS node, walking through wrappers.
///
/// Examples:
/// - `password` -> `"password"`
/// - `self._password` (attribute) -> `"_password"` (last identifier)
/// - `*p` (pointer_declarator) -> `"p"`
/// - `(a, b)` (tuple_pattern) -> None (multi-bind, can't pick one name)
fn extract_lhs_name(node: tree_sitter::Node, source: &[u8]) -> Option<String> {
    match node.kind() {
        "identifier" | "property_identifier" | "field_identifier" | "type_identifier" => {
            Some(node_text(node, source)?.to_lowercase())
        }
        // Python `self._password` / JS `this.password` / Rust `self.password`.
        "attribute" | "member_expression" | "field_expression" => {
            // The last named child is the rightmost identifier.
            let count = node.named_child_count();
            let last_idx = count.checked_sub(1)?;
            extract_lhs_name(node.named_child(last_idx)?, source)
        }
        // C pointer: `*p`.
        "pointer_declarator" => {
            for i in 0..node.named_child_count() {
                if let Some(c) = node.named_child(i) {
                    if let Some(n) = extract_lhs_name(c, source) {
                        return Some(n);
                    }
                }
            }
            None
        }
        // Rust: `mut x` / `(a, b)` / etc. — drill into single-child wrappers.
        _ => {
            if node.named_child_count() == 1 {
                extract_lhs_name(node.named_child(0)?, source)
            } else {
                None
            }
        }
    }
}

/// Extract the raw text of a node as `&str`. Returns `None` if the bytes
/// aren't valid UTF-8 (shouldn't happen for tree-sitter on a UTF-8 input).
fn node_text<'a>(node: tree_sitter::Node, source: &'a [u8]) -> Option<&'a str> {
    let start = node.start_byte();
    let end = node.end_byte().min(source.len());
    std::str::from_utf8(&source[start..end]).ok()
}

/// Extract the content of a string-literal node (between the quotes).
///
/// Tree-sitter typically gives us a `string` node containing
/// `string_start` + `string_content` + `string_end` children, and the
/// fast path returns the `string_content` text directly. The fallback
/// is reachable only for grammars that omit `string_content` (e.g.
/// some older grammar versions, or empty string literals). It works
/// by:
///   1. skipping a single optional language prefix (`f`, `b`, `r`, `u`,
///      or any combination thereof — Python `rb"..."`, Rust `b"..."`,
///      etc.),
///   2. measuring the run of leading delimiter chars (a quote optionally
///      preceded by `#`s for Rust raw strings),
///   3. returning the slice between the matching opening and closing
///      delimiter runs.
///
/// We deliberately do NOT use `trim_start_matches([...])` for the prefix
/// because it would also strip multiple `r`s from a delimiter run like
/// `r##"…"##` and break the closing-delimiter computation.
fn string_node_content<'a>(node: tree_sitter::Node, source: &'a [u8]) -> Option<&'a str> {
    // Fast path: look for a child holding the raw content.
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if matches!(
            child.kind(),
            "string_content" | "string_fragment" | "string_literal_content"
        ) {
            return node_text(child, source);
        }
    }

    // Fallback (rare). Strip a single language prefix, then a single
    // delimiter run, from each side.
    let raw = node_text(node, source)?;
    let bytes = raw.as_bytes();

    // 1) Skip language prefix bytes. Stop as soon as the next byte is a
    //    quote or a `#` (which belongs to the delimiter run, not the
    //    prefix). Bound at 3 bytes — no real language uses a 4-letter
    //    string prefix.
    let mut i = 0;
    while i < bytes.len().min(3)
        && matches!(
            bytes[i],
            b'f' | b'F' | b'r' | b'R' | b'b' | b'B' | b'u' | b'U'
        )
        && i + 1 < bytes.len()
        && !matches!(bytes[i + 1], b'"' | b'\'' | b'`' | b'#')
    {
        i += 1;
    }
    // Consume the LAST prefix byte that immediately precedes the
    // delimiter run (the loop above stops one short to avoid eating
    // a `#`).
    if i < bytes.len()
        && matches!(
            bytes[i],
            b'f' | b'F' | b'r' | b'R' | b'b' | b'B' | b'u' | b'U'
        )
        && i + 1 < bytes.len()
        && matches!(bytes[i + 1], b'"' | b'\'' | b'`' | b'#')
    {
        i += 1;
    }

    // 2) Measure the opening delimiter run: optional `#`s followed by
    //    a single quote char.
    let hash_count = bytes[i..].iter().take_while(|&&b| b == b'#').count();
    let open_delim_end = i + hash_count;
    if open_delim_end >= bytes.len() {
        return Some(raw);
    }
    let quote = bytes[open_delim_end];
    if !matches!(quote, b'"' | b'\'' | b'`') {
        return Some(raw);
    }

    // Closing delimiter is `quote` followed by the same number of `#`s.
    let close_pat_len = 1 + hash_count;
    if bytes.len() < open_delim_end + 1 + close_pat_len {
        return Some(raw);
    }
    let close_start = bytes.len() - close_pat_len;
    if bytes[close_start] != quote || bytes[close_start + 1..].iter().any(|&b| b != b'#') {
        return Some(raw);
    }

    let content_start = open_delim_end + 1;
    let content_end = close_start;
    if content_start > content_end {
        return Some("");
    }
    std::str::from_utf8(&bytes[content_start..content_end]).ok()
}

/// True if `kind` represents a string-literal node across our grammars.
fn is_string_node(kind: &str) -> bool {
    matches!(
        kind,
        "string"
            | "string_literal"
            | "raw_string_literal"
            | "interpreted_string_literal"
            | "verbatim_string_literal"
            | "template_string"
    )
}

/// True if `node` is exactly the value side of a binding we already paired
/// in `extract_named_binding`. This prevents emitting the same string
/// literal as both a keyed binding and a naked literal.
///
/// We deliberately check the EXACT value-field position (not just "parent
/// is a binding kind") so that string KEYS in `pair`/`keyed_element` —
/// e.g. an `AKIA...` AWS key used as a Python dict key — still surface
/// through the naked-literal path and can match `SelfShaped` patterns.
/// (Audit finding #1: the old "any child of a binding parent" rule
/// silently dropped such keys.)
fn parent_is_handled_binding_value(node: tree_sitter::Node) -> bool {
    let parent = match node.parent() {
        Some(p) => p,
        None => return false,
    };

    let value_node = match parent.kind() {
        // Python / JS / Go assignments use field name "right".
        "assignment"
        | "assignment_expression"
        | "assignment_statement"
        | "augmented_assignment"
        | "short_var_declaration" => parent.child_by_field_name("right"),

        // Field name "value" is used by most binding shapes.
        "keyword_argument"
        | "pair"
        | "variable_declarator"
        | "const_spec"
        | "var_spec"
        | "let_declaration"
        | "const_item"
        | "static_item"
        | "init_declarator"
        | "default_parameter"
        | "field_definition"
        | "public_field_definition" => parent.child_by_field_name("value"),

        // Go composite literal: `{key: "v"}` — value is positional named_child(1).
        "keyed_element" => parent.named_child(1),

        _ => return false,
    };

    // For multi-element value-list nodes (Go `expression_list`), the actual
    // string literal is one level deeper. We treat the literal as suppressed
    // if it's the (sole) named child of an expression_list that is itself
    // the value field of the parent.
    if let Some(v) = value_node {
        if v.id() == node.id() {
            return true;
        }
        if v.kind() == "expression_list"
            && v.named_child_count() == 1
            && v.named_child(0).map(|c| c.id() == node.id()) == Some(true)
        {
            return true;
        }
    }
    false
}

/// Mirror of `cache::masking::is_python_docstring` for the AST scanner.
fn is_likely_python_docstring(node: tree_sitter::Node) -> bool {
    let parent = match node.parent() {
        Some(p) => p,
        None => return false,
    };
    if parent.kind() != "expression_statement" {
        return false;
    }
    let grandparent = match parent.parent() {
        Some(g) => g,
        None => return false,
    };
    if !matches!(grandparent.kind(), "block" | "module") {
        return false;
    }
    let mut cursor = grandparent.walk();
    for child in grandparent.children(&mut cursor) {
        if child.kind() == "expression_statement" {
            return child.id() == parent.id();
        }
    }
    false
}

impl Detector for SecretDetector {
    fn name(&self) -> &'static str {
        "secret-detection"
    }

    fn description(&self) -> &'static str {
        "Detects hardcoded secrets, API keys, and passwords"
    }

    fn bypass_postprocessor(&self) -> bool {
        true
    }

    fn file_extensions(&self) -> &'static [&'static str] {
        &[
            "py", "js", "ts", "jsx", "tsx", "rb", "java", "go", "rs", "env", "yml", "yaml", "json",
            "toml", "cfg", "ini", "conf",
        ]
    }

    fn content_requirements(&self) -> crate::detectors::detector_context::ContentFlags {
        crate::detectors::detector_context::ContentFlags::HAS_SECRET_PATTERN
    }

    fn detect(
        &self,
        ctx: &crate::detectors::analysis_context::AnalysisContext,
    ) -> Result<Vec<Finding>> {
        let graph = ctx.graph;
        let files = &ctx.as_file_provider();
        let i = graph.interner();
        let mut findings = vec![];

        for path in files.files_with_extensions(&[
            "py",
            "js",
            "ts",
            "jsx",
            "tsx",
            "rs",
            "go",
            "java",
            "rb",
            "php",
            "cs",
            "cpp",
            "c",
            "h",
            "hpp",
            "yaml",
            "yml",
            "json",
            "toml",
            "env",
            "conf",
            "config",
            "sh",
            "bash",
            "zsh",
            "properties",
            "xml",
        ]) {
            if findings.len() >= self.max_findings {
                break;
            }

            // Skip certain directories
            let path_str = path.to_string_lossy();
            if path_str.contains("node_modules")
                || path_str.contains(".git")
                || path_str.contains("vendor")
                || path_str.contains("target")
            {
                continue;
            }

            // Skip detector files (contain regex patterns that look like secrets)
            if path_str.contains("/detectors/") && path_str.ends_with(".rs") {
                continue;
            }

            // Cheap pre-filter: skip files without any secret-related patterns
            let raw = match files.content(path) {
                Some(c) => c,
                None => continue,
            };
            if !raw.contains("AKIA")          // AWS
                && !raw.contains("ghp_")      // GitHub
                && !raw.contains("sk_live_")  // Stripe
                && !raw.contains("SG.")       // SendGrid
                && !raw.contains("PRIVATE KEY") // Private keys
                && !raw.contains("api_key") && !raw.contains("api-key") && !raw.contains("apikey")
                && !raw.contains("API_KEY") && !raw.contains("API-KEY") && !raw.contains("APIKEY")
                && !raw.contains("password") && !raw.contains("PASSWORD")
                && !raw.contains("passwd") && !raw.contains("PASSWD")
                && !raw.contains("secret") && !raw.contains("SECRET")
                && !raw.contains("token") && !raw.contains("TOKEN")
                && !raw.contains("postgres://") && !raw.contains("mysql://")
                && !raw.contains("mongodb://") && !raw.contains("redis://")
                && !raw.contains("xoxb-") && !raw.contains("xoxp-")
                && !raw.contains("xoxa-") && !raw.contains("xoxr-")
            {
                continue;
            }

            debug!("Scanning for secrets: {}", path.display());
            // Use RAW content, not masked: the masking layer (`cache::masking`)
            // strips string-literal contents to spaces, which is exactly where
            // secrets live. For tree-sitter-supported languages we walk the AST
            // and only look at lines that contain real bindings/literals
            // (preventing comment/docstring FPs); for other formats (.env,
            // .yml, .json, .toml, .ini, .conf, Ruby, PHP, ...) we run the
            // line-based scanner directly on raw content. Config formats have
            // no docstrings, and the in-detector `is_line_suppressed` /
            // example-comment skip already covers their FPs.
            let raw_content = match files.content(path) {
                Some(c) => c,
                None => continue,
            };

            let lang = Language::from_path(path);
            let has_ast_grammar = matches!(
                lang,
                Language::Python
                    | Language::JavaScript
                    | Language::TypeScript
                    | Language::Rust
                    | Language::Go
                    | Language::Java
                    | Language::CSharp
                    | Language::C
                    | Language::Cpp
            );

            if has_ast_grammar {
                let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
                let cached = files.tree(path);
                let scan = ScanInputs::new(path, &raw_content, ext);
                let ast_inputs = ScanAstInputs::new(scan, lang, cached.as_deref());
                findings.extend(self.scan_file_ast(&ast_inputs));
            } else {
                findings.extend(self.scan_file(path, &raw_content));
            }
        }

        // Enrich findings with graph context
        for finding in &mut findings {
            if let (Some(file_path), Some(line)) =
                (finding.affected_files.first(), finding.line_start)
            {
                let path_str = file_path.to_string_lossy().to_string();

                if let Some(f) = graph.find_function_at(&path_str, line) {
                    let callers = graph.get_callers(f.qn(i)).len();
                    let name_lower = f.node_name(i).to_lowercase();
                    let is_config = name_lower.contains("config")
                        || name_lower.contains("init")
                        || name_lower.contains("setup")
                        || name_lower.contains("settings");
                    let func_name = f.node_name(i);

                    let mut notes = Vec::new();
                    notes.push(format!(
                        "📦 In function: `{}` ({} callers)",
                        func_name, callers
                    ));

                    if is_config {
                        notes.push("⚙️ In config/setup function".to_string());
                        // Config functions with secrets are more expected but still bad
                        if finding.severity == Severity::Critical {
                            finding.severity = Severity::High;
                        }
                    }

                    // Boost severity if function has many callers (widely used)
                    if callers > 10 && finding.severity == Severity::High {
                        finding.severity = Severity::Critical;
                    }

                    finding.description = format!(
                        "{}\n\n**Context:**\n{}",
                        finding.description,
                        notes.join("\n")
                    );
                }
            }
        }

        Ok(findings)
    }
}

impl crate::detectors::RegisteredDetector for SecretDetector {
    fn create(init: &crate::detectors::DetectorInit) -> std::sync::Arc<dyn Detector> {
        std::sync::Arc::new(Self::new(init.repo_path))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::builder::GraphBuilder;

    #[test]
    fn test_detects_hardcoded_aws_key() {
        let store = GraphBuilder::new().freeze();
        let _detector = SecretDetector::new("/mock/repo");
        // Use .rb extension: masking has no tree-sitter grammar for Ruby,
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(
            &store,
            vec![("config.rb", "\nAWS_ACCESS_KEY = \"AKIAIOSFODNN7ABCDEFG\"\n")],
        );
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            !findings.is_empty(),
            "Should detect hardcoded AWS access key"
        );
        assert!(findings.iter().any(|f| f.title.contains("AWS Access Key")));
    }

    #[test]
    fn test_no_finding_for_env_variable_usage() {
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(&store, vec![
            ("config.py", "\nimport os\nAWS_KEY = os.environ.get(\"AWS_ACCESS_KEY_ID\")\nSECRET = os.getenv(\"AWS_SECRET_ACCESS_KEY\")\n"),
        ]);
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.is_empty(),
            "Should not flag secrets read from environment variables, but got: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_no_finding_for_password_in_docstring() {
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(&store, vec![
            ("auth.py", "def authenticate(username, password):\n    \"\"\"\n    Authenticate user with password.\n    password = hashlib.sha256(raw).hexdigest()\n    \"\"\"\n    return check_password(username, password)\n"),
        ]);
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.is_empty(),
            "Should not flag 'password' references in docstrings. Found: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_no_finding_for_password_type_annotation() {
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(&store, vec![
            ("models.py", "from pydantic import BaseModel\n\nclass LoginRequest(BaseModel):\n    username: str\n    password: str\n"),
        ]);
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.is_empty(),
            "Should not flag password type annotations. Found: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_no_finding_for_password_field_definition() {
        let store = GraphBuilder::new().freeze();
        let _detector = SecretDetector::new("/mock/repo");
        // Use .rb -- no tree-sitter masking, content passes through
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(&store, vec![
            ("models.rb", "password = CharField(max_length=128)\nsecret = SecretManager.from_config(settings)\n"),
        ]);
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.is_empty(),
            "Should not flag function/class calls as secrets. Found: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_no_finding_for_password_list_assignment() {
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(&store, vec![
            ("config.rb", "password = [\"django.contrib.auth.hashers.PBKDF2PasswordHasher\"]\nsecret = {\"key\": \"value\", \"other\": \"data\"}\n"),
        ]);
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.is_empty(),
            "Should not flag list/dict literal assignments as secrets. Found: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_still_detects_real_hardcoded_password() {
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(
            &store,
            vec![("config.rb", "password = \"super_secret_password_123\"\n")],
        );
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            !findings.is_empty(),
            "Should still detect real hardcoded password"
        );
    }

    #[test]
    fn test_skips_uppercase_constant_reference() {
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(
            &store,
            vec![("config.rb", "password = HARDCODED_SECRET_VALUE\n")],
        );
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.is_empty(),
            "Should not flag variable/constant references as secrets. Found: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_no_finding_for_password_variable_reference() {
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(&store, vec![
            ("views.rb", "password=auth_password,\nsecret = settings.SECRET_KEY\nself._password = raw_password\n"),
        ]);
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.is_empty(),
            "Should not flag variable references as secrets. Found: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_no_finding_for_settings_read() {
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(&store, vec![
            ("config.rb", "self.password = settings.EMAIL_HOST_PASSWORD if password is None else password\npassword=self.settings_dict[\"PASSWORD\"],\n"),
        ]);
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.is_empty(),
            "Should not flag settings reads as secrets. Found: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_no_finding_for_request_data_read() {
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(&store, vec![
            ("views.rb", "csrf_secret = request.META[\"CSRF_COOKIE\"]\nold_password = self.cleaned_data[\"old_password\"]\n"),
        ]);
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.is_empty(),
            "Should not flag request/form data reads as secrets. Found: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    // ------------------------------------------------------------------
    // AST-path tests (QA audit finding #1)
    //
    // Before the AST migration, all positive tests used `.rb` extension
    // because Ruby has no tree-sitter grammar, so `mask_non_code()`
    // returned the original source unchanged. For supported languages
    // (`.py`, `.js`, `.ts`, `.rs`, ...) the masking layer replaced every
    // string literal's contents with spaces BEFORE this detector ran,
    // making it structurally impossible to detect a hardcoded secret in
    // those languages — the values were already gone.
    //
    // The tests below exercise the AST path against the languages that
    // were previously broken.
    // ------------------------------------------------------------------

    #[test]
    fn test_audit_repro_python_hardcoded_password() {
        // Direct reproducer of QA audit finding #1: a Python file with
        // an obvious hardcoded credential returned 0 findings even with
        // --all-detectors. This MUST find at least one secret.
        //
        // We build the fixture string at runtime so this test file
        // doesn't itself contain a contiguous `sk_live_...` literal —
        // GitHub's secret-scanner push protection would otherwise block
        // the commit (rightly: this is the very pattern the detector
        // is supposed to find).
        let stripe_token = String::from("sk") + "_live_" + "abcdefghijklmnopqrstuvwx";
        let source =
            format!("password = \"hardcoded_super_secret_p4ss\"\napi_key = \"{stripe_token}\"\n");
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(
            &store,
            vec![("creds.py", source.as_str())],
        );
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            !findings.is_empty(),
            "QA audit regression: SecretDetector returned 0 findings for an \
             obvious hardcoded password in Python (.py). The masking layer \
             was stripping the value. Findings: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
        // We expect at least the Generic Secret hit on line 1.
        assert!(
            findings.iter().any(|f| f.title.contains("Generic Secret")),
            "Expected a Generic Secret finding on the password line. Got: {:?}",
            findings
                .iter()
                .map(|f| (&f.title, f.line_start))
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_ast_detects_javascript_hardcoded_password() {
        // Build the GitHub PAT literal at runtime to avoid tripping GitHub
        // Push Protection on a contiguous `ghp_...` shape in this source.
        let github_pat = String::from("ghp") + "_" + &"a".repeat(36);
        let source =
            format!("const password = \"hardcoded_pass_j6k\";\nconst token = \"{github_pat}\";\n");
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(
            &store,
            vec![("app.js", source.as_str())],
        );
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.iter().any(|f| f.title.contains("Generic Secret")),
            "Expected Generic Secret on JS const password assignment. Got: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
        assert!(
            findings.iter().any(|f| f.title.contains("GitHub Token")),
            "Expected GitHub Token on JS string literal. Got: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_ast_detects_rust_hardcoded_stripe_key() {
        // Rust `const NAME: TYPE = "value"` defeats the Generic Secret regex
        // (the type colon is consumed greedily). But `sk_live_` is a self-
        // shaped pattern: it identifies the secret by its own format, so
        // the literal-walk path of the AST scanner catches it regardless.
        //
        // Same trick as `test_audit_repro_python_hardcoded_password`: build
        // the literal at runtime so this source file doesn't trip GitHub
        // Push Protection's secret scanner.
        let stripe_token = String::from("sk") + "_live_" + "zzzzzzzzzzzzzzzzzzzzzzzzz";
        let source = format!(
            "fn main() {{\n    let stripe_key = \"{stripe_token}\";\n    let _ = stripe_key;\n}}\n"
        );
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(
            &store,
            vec![("config.rs", source.as_str())],
        );
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.iter().any(|f| f.title.contains("Stripe API Key")),
            "Expected Stripe API Key on Rust string literal. Got: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_ast_skips_python_docstring_with_password_word() {
        // Verify the AST path does NOT regress on docstrings — the masking
        // layer used to handle this; we replicated `is_python_docstring`
        // in `is_likely_python_docstring` for the AST scanner.
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(
            &store,
            vec![(
                "auth.py",
                "def authenticate(user, raw):\n    \"\"\"Authenticate user. password = \\\"never_a_real_secret_xyz\\\"\"\"\"\n    return user\n",
            )],
        );
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.is_empty(),
            "Should not flag credentials inside a Python docstring. Got: {:?}",
            findings
                .iter()
                .map(|f| (&f.title, f.line_start))
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_ast_skips_python_env_fallback() {
        // The env-var fallback FP filter in `try_match_line` should still
        // apply on the AST path because we feed it the raw source line.
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(
            &store,
            vec![(
                "settings.py",
                "import os\nDB_PASSWORD = os.environ.get(\"DB_PASSWORD\", \"dev_default_pw\")\n",
            )],
        );
        let findings = detector.detect(&ctx).expect("detection should succeed");
        // Either no findings, or any finding is downgraded to Low severity.
        for f in &findings {
            assert!(
                matches!(f.severity, Severity::Low | Severity::Info),
                "env.get() with fallback should be Low severity, got {:?} for {:?}",
                f.severity,
                f.title
            );
        }
    }

    #[test]
    fn test_ast_python_function_call_value_not_flagged() {
        // The Generic Secret value-type filter in `try_match_line` skips
        // `password = SomeClass(...)`. AST path must keep this behavior.
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(
            &store,
            vec![(
                "models.py",
                "password = CharField(max_length=128)\nsecret = SecretManager.from_config(settings)\n",
            )],
        );
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.is_empty(),
            "Should not flag function/class calls as secrets. Got: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    // -----------------------------------------------------------------
    // Audit-fix regression tests (audit report on commit 4381002a)
    // -----------------------------------------------------------------

    /// Bug 1 (medium): An AKIA-shaped string used as a Python dict KEY
    /// should still be reported. Previously `parent_is_handled_binding_value`
    /// suppressed it because the parent kind was `pair`.
    #[test]
    fn test_ast_detects_aws_key_used_as_dict_key() {
        // Build the AKIA prefix at runtime so this source file doesn't
        // contain a literal AKIA-shaped token (GitHub Push Protection).
        let akia = format!("AKIA{}{}", "IOSFODNN", "7ABCDEFG");
        let py_src = format!("config = {{\"{key}\": \"placeholder\"}}\n", key = akia);
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(
            &store,
            vec![("audit.py", py_src.as_str())],
        );
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.iter().any(|f| f.title.contains("AWS Access Key")),
            "AKIA-shaped dict key must be reported. Got: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    /// Bug 2 (low): JS `class C { password = "v" }` field definitions
    /// should be picked up by the NameGated Generic Secret pattern.
    #[test]
    fn test_ast_detects_js_class_field_secret() {
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(
            &store,
            vec![(
                "auth.js",
                "class Auth {\n  password = \"hardcoded123!\";\n}\n",
            )],
        );
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings
                .iter()
                .any(|f| f.title.contains("Generic Secret") || f.title.contains("Password")),
            "JS class field with hardcoded password must be reported. Got: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    /// Coverage gap: Python `def f(password="hardcoded"):` default
    /// parameter values should be flagged the same as keyword arguments.
    #[test]
    fn test_ast_detects_python_default_parameter_secret() {
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(
            &store,
            vec![(
                "auth.py",
                "def authenticate(user, password=\"hardcoded123!\"):\n    return user\n",
            )],
        );
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            !findings.is_empty(),
            "Default-parameter hardcoded password must be reported. Got: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    /// `apiKeyHeader = "longvalue123..."` should NOT match the new
    /// anchored Generic API Key gate (`^api[_-]?key$`). Verifies the
    /// stricter gate doesn't regress on broad matching.
    #[test]
    fn test_ast_strict_api_key_gate_rejects_camelcase_substring() {
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(
            &store,
            vec![(
                "config.py",
                "apiKeyHeader = \"X-API-Key-Custom-Header-Name\"\n",
            )],
        );
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings
                .iter()
                .all(|f| !f.title.contains("Generic API Key")),
            "Generic API Key gate must be anchored — `apiKeyHeader` is not an api_key. Got: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    /// `name_gate` corner cases for Generic Secret: `passwords` (plural)
    /// and `mypassword` (no boundary) should NOT match; `auth_token_value`
    /// SHOULD match via the `_token_` boundary.
    #[test]
    fn test_ast_generic_secret_name_gate_boundaries() {
        let store = GraphBuilder::new().freeze();
        let detector = SecretDetector::new("/mock/repo");

        // `passwords` (plural) — not a credential variable.
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(
            &store,
            vec![("a.py", "passwords = \"this-is-a-list-name\"\n")],
        );
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.iter().all(|f| !f.title.contains("Generic Secret")),
            "Plural `passwords` must not match Generic Secret. Got: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );

        // `auth_token_value` SHOULD match (token surrounded by _).
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(
            &store,
            vec![(
                "b.py",
                "auth_token_value = \"hardcoded-bearer-abcd-1234\"\n",
            )],
        );
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.iter().any(|f| f.title.contains("Generic Secret")),
            "`auth_token_value` should match via `_token_` boundary. Got: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }
}