repotoire 0.3.47

Graph-powered code analysis CLI. 81 detectors for security, architecture, and code quality.
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
//! Taint Analysis for Security Vulnerability Detection
//!
//! This module provides graph-based data flow analysis to trace potentially malicious
//! data from untrusted sources (user input) to dangerous sinks (SQL queries, shell commands, etc.).
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │                     TaintAnalyzer                           │
//! │  - Defines sources (user input entry points)                │
//! │  - Defines sinks (dangerous operations)                     │
//! │  - Defines sanitizers (functions that neutralize taint)     │
//! └─────────────────────────────────────────────────────────────┘
//!//!//! ┌─────────────────────────────────────────────────────────────┐
//! │                    trace_taint()                            │
//! │  - BFS through call graph from source functions             │
//! │  - Track path through function calls                        │
//! │  - Identify when tainted data reaches a sink                │
//! └─────────────────────────────────────────────────────────────┘
//!//!//! ┌─────────────────────────────────────────────────────────────┐
//! │                     TaintPath                               │
//! │  - Source function (where taint originates)                 │
//! │  - Sink function (dangerous operation)                      │
//! │  - Call chain (functions between source and sink)           │
//! │  - Sanitized flag (whether sanitizer was in path)           │
//! └─────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Usage
//!
//! ```ignore
//! use repotoire_cli::detectors::taint::{TaintAnalyzer, TaintCategory};
//!
//! let analyzer = TaintAnalyzer::new();
//! let paths = analyzer.trace_taint(&graph, TaintCategory::SqlInjection);
//!
//! for path in paths {
//!     if !path.is_sanitized {
//!         // Critical: unsanitized taint flow to SQL sink
//!     }
//! }
//! ```

use crate::graph::{GraphStore, NodeKind};
use std::collections::{HashMap, HashSet, VecDeque};

/// Categories of taint analysis
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TaintCategory {
    /// SQL injection (CWE-89)
    SqlInjection,
    /// Command injection (CWE-78)
    CommandInjection,
    /// Cross-site scripting (CWE-79)
    Xss,
    /// Server-side request forgery (CWE-918)
    Ssrf,
    /// Path traversal (CWE-22)
    PathTraversal,
    /// Code injection via eval/exec (CWE-94)
    CodeInjection,
    /// Log injection (CWE-117)
    LogInjection,
}

impl TaintCategory {
    /// Get the CWE ID for this category
    pub fn cwe_id(&self) -> &'static str {
        match self {
            TaintCategory::SqlInjection => "CWE-89",
            TaintCategory::CommandInjection => "CWE-78",
            TaintCategory::Xss => "CWE-79",
            TaintCategory::Ssrf => "CWE-918",
            TaintCategory::PathTraversal => "CWE-22",
            TaintCategory::CodeInjection => "CWE-94",
            TaintCategory::LogInjection => "CWE-117",
        }
    }

    /// Get a human-readable name
    pub fn name(&self) -> &'static str {
        match self {
            TaintCategory::SqlInjection => "SQL Injection",
            TaintCategory::CommandInjection => "Command Injection",
            TaintCategory::Xss => "Cross-Site Scripting (XSS)",
            TaintCategory::Ssrf => "Server-Side Request Forgery (SSRF)",
            TaintCategory::PathTraversal => "Path Traversal",
            TaintCategory::CodeInjection => "Code Injection",
            TaintCategory::LogInjection => "Log Injection",
        }
    }
}

/// A path from a taint source to a sink through the call graph
#[derive(Debug, Clone)]
pub struct TaintPath {
    /// The source function where taint originates (e.g., route handler)
    pub source_function: String,
    /// The file containing the source
    pub source_file: String,
    /// Line number of the source
    pub source_line: u32,
    /// The sink function where tainted data is used dangerously
    pub sink_function: String,
    /// The file containing the sink
    pub sink_file: String,
    /// Line number of the sink
    pub sink_line: u32,
    /// The category of vulnerability
    pub category: TaintCategory,
    /// Functions in the call chain from source to sink
    pub call_chain: Vec<String>,
    /// Whether a sanitizer function was found in the path
    pub is_sanitized: bool,
    /// The sanitizer function that was found (if any)
    pub sanitizer: Option<String>,
    /// Confidence level (0.0 - 1.0)
    pub confidence: f64,
}

impl TaintPath {
    /// Check if this path represents a likely vulnerability
    pub fn is_vulnerable(&self) -> bool {
        !self.is_sanitized && self.confidence >= 0.5
    }

    /// Get the full path as a string for display
    pub fn path_string(&self) -> String {
        if self.call_chain.is_empty() {
            format!("{}{}", self.source_function, self.sink_function)
        } else {
            format!(
                "{}{}{}",
                self.source_function,
                self.call_chain.join(""),
                self.sink_function
            )
        }
    }
}

/// Taint analyzer that uses the code graph for data flow analysis
pub struct TaintAnalyzer {
    /// Source patterns by category - functions that introduce tainted data
    sources: HashMap<TaintCategory, HashSet<String>>,
    /// Sink patterns by category - dangerous functions
    sinks: HashMap<TaintCategory, HashSet<String>>,
    /// Sanitizer patterns by category - functions that neutralize taint
    sanitizers: HashMap<TaintCategory, HashSet<String>>,
    /// Generic sanitizers that apply to all categories
    generic_sanitizers: HashSet<String>,
    /// Maximum depth for BFS traversal
    max_depth: usize,
}

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

impl TaintAnalyzer {
    /// Create a new TaintAnalyzer with default patterns
    pub fn new() -> Self {
        let mut analyzer = Self {
            sources: HashMap::new(),
            sinks: HashMap::new(),
            sanitizers: HashMap::new(),
            generic_sanitizers: HashSet::new(),
            max_depth: 10,
        };
        analyzer.init_default_patterns();
        analyzer
    }

    /// Create with custom max depth
    pub fn with_max_depth(mut self, depth: usize) -> Self {
        self.max_depth = depth;
        self
    }

    /// Initialize default taint source/sink/sanitizer patterns
    fn init_default_patterns(&mut self) {
        // ========== TAINT SOURCES (User Input Entry Points) ==========

        // These are functions/patterns that introduce untrusted data into the system
        let mut all_sources = HashSet::new();

        // Express.js (Node.js)
        for pattern in &[
            "req.body",
            "req.query",
            "req.params",
            "req.headers",
            "req.cookies",
            "request.body",
            "request.query",
            "request.params",
        ] {
            all_sources.insert(pattern.to_string());
        }

        // Flask (Python)
        for pattern in &[
            "request.form",
            "request.args",
            "request.data",
            "request.json",
            "request.files",
            "request.cookies",
            "request.headers",
        ] {
            all_sources.insert(pattern.to_string());
        }

        // Django (Python)
        for pattern in &[
            "request.GET",
            "request.POST",
            "request.body",
            "request.FILES",
        ] {
            all_sources.insert(pattern.to_string());
        }

        // Gin (Go)
        for pattern in &[
            "c.Param",
            "c.Query",
            "c.PostForm",
            "c.FormValue",
            "c.GetHeader",
            "c.BindJSON",
            "c.ShouldBindJSON",
        ] {
            all_sources.insert(pattern.to_string());
        }

        // FastAPI (Python)
        for pattern in &[
            "request.query_params",
            "request.path_params",
            "request.form",
            "request.body",
        ] {
            all_sources.insert(pattern.to_string());
        }

        // Generic input
        for pattern in &[
            "input(",
            "raw_input(",
            "sys.stdin",
            "os.environ",
            "getenv(",
            "process.env",
        ] {
            all_sources.insert(pattern.to_string());
        }

        // Apply sources to all categories (they're the same entry points)
        for category in &[
            TaintCategory::SqlInjection,
            TaintCategory::CommandInjection,
            TaintCategory::Xss,
            TaintCategory::Ssrf,
            TaintCategory::PathTraversal,
            TaintCategory::CodeInjection,
            TaintCategory::LogInjection,
        ] {
            self.sources.insert(*category, all_sources.clone());
        }

        // ========== TAINT SINKS (Dangerous Operations) ==========

        // SQL Injection Sinks
        let mut sql_sinks = HashSet::new();
        for pattern in &[
            // Python
            "cursor.execute",
            "cursor.executemany",
            "cursor.executescript",
            "connection.execute",
            "engine.execute",
            "session.execute",
            "db.execute",
            // SQLAlchemy
            "text(",
            ".raw(",
            "from_statement",
            // Django
            "objects.raw",
            "objects.extra",
            "RawSQL",
            // Node.js
            "pool.query",
            "client.query",
            "connection.query",
            "db.query",
            "knex.raw",
            "sequelize.query",
            // Go
            "db.Query",
            "db.QueryRow",
            "db.Exec",
            "db.Prepare",
            "tx.Query",
            "tx.Exec",
        ] {
            sql_sinks.insert(pattern.to_string());
        }
        self.sinks.insert(TaintCategory::SqlInjection, sql_sinks);

        // Command Injection Sinks
        let mut cmd_sinks = HashSet::new();
        for pattern in &[
            // Python
            "os.system",
            "os.popen",
            "subprocess.call",
            "subprocess.run",
            "subprocess.Popen",
            "commands.getoutput",
            // Node.js
            "child_process.exec",
            "child_process.execSync",
            "child_process.spawn",
            "child_process.spawnSync",
            "execSync",
            "exec(",
            "spawn(",
            // Go
            "exec.Command",
            "exec.CommandContext",
            // PHP
            "shell_exec",
            "system(",
            "passthru",
            "proc_open",
        ] {
            cmd_sinks.insert(pattern.to_string());
        }
        self.sinks.insert(TaintCategory::CommandInjection, cmd_sinks);

        // XSS Sinks
        let mut xss_sinks = HashSet::new();
        for pattern in &[
            // JavaScript
            "innerHTML",
            "outerHTML",
            "document.write",
            "document.writeln",
            // React
            "dangerouslySetInnerHTML",
            // Vue
            "v-html",
            // Angular
            "ng-bind-html",
            "[innerHTML]",
            // Python templates
            "render_template_string",
            "Markup(",
            "|safe",
        ] {
            xss_sinks.insert(pattern.to_string());
        }
        self.sinks.insert(TaintCategory::Xss, xss_sinks);

        // SSRF Sinks
        let mut ssrf_sinks = HashSet::new();
        for pattern in &[
            // Python
            "requests.get",
            "requests.post",
            "requests.put",
            "requests.delete",
            "requests.head",
            "urllib.urlopen",
            "urllib.request.urlopen",
            "httpx.get",
            "httpx.post",
            "aiohttp.get",
            // Node.js
            "fetch(",
            "axios.get",
            "axios.post",
            "http.get",
            "https.get",
            "got(",
            "request(",
            // Go
            "http.Get",
            "http.Post",
            "http.NewRequest",
            "client.Get",
            "client.Do",
        ] {
            ssrf_sinks.insert(pattern.to_string());
        }
        self.sinks.insert(TaintCategory::Ssrf, ssrf_sinks);

        // Path Traversal Sinks
        let mut path_sinks = HashSet::new();
        for pattern in &[
            // Python
            "open(",
            "os.path.join",
            "pathlib.Path",
            "shutil.copy",
            "shutil.move",
            "send_file",
            "send_from_directory",
            // Node.js
            "fs.readFile",
            "fs.writeFile",
            "fs.readFileSync",
            "fs.writeFileSync",
            "path.join",
            "path.resolve",
            // Go
            "os.Open",
            "os.Create",
            "ioutil.ReadFile",
            "ioutil.WriteFile",
            "filepath.Join",
        ] {
            path_sinks.insert(pattern.to_string());
        }
        self.sinks.insert(TaintCategory::PathTraversal, path_sinks);

        // Code Injection Sinks
        let mut code_sinks = HashSet::new();
        for pattern in &[
            // Python
            "eval(",
            "exec(",
            "compile(",
            "__import__",
            // JavaScript
            "eval(",
            "Function(",
            "setTimeout(", // with string arg
            "setInterval(", // with string arg
            // Go
            // (Go doesn't have direct eval, but template injection is similar)
            "template.HTML",
        ] {
            code_sinks.insert(pattern.to_string());
        }
        self.sinks.insert(TaintCategory::CodeInjection, code_sinks);

        // Log Injection Sinks
        let mut log_sinks = HashSet::new();
        for pattern in &[
            // Python
            "logging.info",
            "logging.debug",
            "logging.warning",
            "logging.error",
            "logging.critical",
            "logger.info",
            "logger.debug",
            "logger.warning",
            "logger.error",
            "log.info",
            "log.debug",
            "log.warn",
            "log.error",
            // JavaScript
            "console.log",
            "console.error",
            "console.warn",
            "console.info",
            // Go
            "log.Print",
            "log.Printf",
            "log.Println",
            "log.Fatal",
        ] {
            log_sinks.insert(pattern.to_string());
        }
        self.sinks.insert(TaintCategory::LogInjection, log_sinks);

        // ========== SANITIZERS (Functions that neutralize taint) ==========

        // SQL Injection Sanitizers
        let mut sql_sanitizers = HashSet::new();
        for pattern in &[
            // Parameterized queries (these patterns in the call chain suggest safe usage)
            "parameterize",
            "prepare",
            "bind",
            // ORMs (proper ORM usage is safe)
            "filter(",
            "where(",
            "findOne",
            "findById",
            "findByPk",
            // Escaping
            "escape(",
            "quote(",
            "mogrify",
        ] {
            sql_sanitizers.insert(pattern.to_string());
        }
        self.sanitizers
            .insert(TaintCategory::SqlInjection, sql_sanitizers);

        // Command Injection Sanitizers
        let mut cmd_sanitizers = HashSet::new();
        for pattern in &[
            "shlex.quote",
            "shlex.split",
            "pipes.quote",
            "shell=False", // subprocess flag
            "escapeshellarg",
            "escapeshellcmd",
        ] {
            cmd_sanitizers.insert(pattern.to_string());
        }
        self.sanitizers
            .insert(TaintCategory::CommandInjection, cmd_sanitizers);

        // XSS Sanitizers
        let mut xss_sanitizers = HashSet::new();
        for pattern in &[
            "escapeHtml",
            "escape(",
            "encode(",
            "htmlspecialchars",
            "sanitize",
            "DOMPurify",
            "xss(",
            "textContent", // safe alternative to innerHTML
            "innerText",
            "createTextNode",
        ] {
            xss_sanitizers.insert(pattern.to_string());
        }
        self.sanitizers.insert(TaintCategory::Xss, xss_sanitizers);

        // SSRF Sanitizers
        let mut ssrf_sanitizers = HashSet::new();
        for pattern in &[
            "validate_url",
            "is_safe_url",
            "url_validator",
            "allowlist",
            "whitelist",
            "check_host",
        ] {
            ssrf_sanitizers.insert(pattern.to_string());
        }
        self.sanitizers.insert(TaintCategory::Ssrf, ssrf_sanitizers);

        // Path Traversal Sanitizers
        let mut path_sanitizers = HashSet::new();
        for pattern in &[
            "basename",
            "os.path.basename",
            "path.basename",
            "filepath.Base",
            "realpath",
            "abspath",
            "secure_filename",
            "sanitize_path",
        ] {
            path_sanitizers.insert(pattern.to_string());
        }
        self.sanitizers
            .insert(TaintCategory::PathTraversal, path_sanitizers);

        // Code Injection Sanitizers
        let mut code_sanitizers = HashSet::new();
        for pattern in &[
            "ast.literal_eval",
            "json.loads",
            "JSON.parse",
        ] {
            code_sanitizers.insert(pattern.to_string());
        }
        self.sanitizers
            .insert(TaintCategory::CodeInjection, code_sanitizers);

        // Log Injection Sanitizers
        let mut log_sanitizers = HashSet::new();
        for pattern in &[
            "strip(",
            "replace(",
            "sanitize_log",
        ] {
            log_sanitizers.insert(pattern.to_string());
        }
        self.sanitizers
            .insert(TaintCategory::LogInjection, log_sanitizers);

        // Generic sanitizers that apply to all categories
        for pattern in &[
            "validate",
            "sanitize",
            "clean",
            "safe_",
            "_safe",
        ] {
            self.generic_sanitizers.insert(pattern.to_string());
        }
    }

    /// Check if a function name matches any source pattern for the category
    pub fn is_source(&self, func_name: &str, category: TaintCategory) -> bool {
        if let Some(sources) = self.sources.get(&category) {
            let name_lower = func_name.to_lowercase();
            sources.iter().any(|s| name_lower.contains(&s.to_lowercase()))
        } else {
            false
        }
    }

    /// Check if a function name matches any sink pattern for the category
    pub fn is_sink(&self, func_name: &str, category: TaintCategory) -> bool {
        if let Some(sinks) = self.sinks.get(&category) {
            let name_lower = func_name.to_lowercase();
            sinks.iter().any(|s| name_lower.contains(&s.to_lowercase()))
        } else {
            false
        }
    }

    /// Check if a function name matches any sanitizer pattern for the category
    pub fn is_sanitizer(&self, func_name: &str, category: TaintCategory) -> bool {
        let name_lower = func_name.to_lowercase();

        // Check category-specific sanitizers
        if let Some(sanitizers) = self.sanitizers.get(&category) {
            if sanitizers
                .iter()
                .any(|s| name_lower.contains(&s.to_lowercase()))
            {
                return true;
            }
        }

        // Check generic sanitizers
        self.generic_sanitizers
            .iter()
            .any(|s| name_lower.contains(&s.to_lowercase()))
    }

    /// Trace taint paths through the call graph for a specific category
    ///
    /// This uses BFS to find all paths from source functions to sink functions,
    /// tracking whether sanitizers are encountered along the way.
    pub fn trace_taint(&self, graph: &GraphStore, category: TaintCategory) -> Vec<TaintPath> {
        let mut paths = Vec::new();
        let functions = graph.get_functions();

        // Find all potential source functions (route handlers, input processors)
        let source_funcs: Vec<_> = functions
            .iter()
            .filter(|f| self.is_potential_source_function(f, category))
            .collect();

        // Find all sink functions
        let sink_funcs: Vec<_> = functions
            .iter()
            .filter(|f| self.is_sink(&f.name, category) || self.is_sink(&f.qualified_name, category))
            .collect();

        // For each source, BFS to find paths to sinks
        for source in &source_funcs {
            let source_paths = self.bfs_to_sinks(
                graph,
                &source.qualified_name,
                &sink_funcs.iter().map(|f| f.qualified_name.as_str()).collect::<HashSet<_>>(),
                category,
            );

            for (sink_qn, call_chain, sanitizer) in source_paths {
                if let Some(sink) = sink_funcs.iter().find(|f| f.qualified_name == sink_qn) {
                    paths.push(TaintPath {
                        source_function: source.name.clone(),
                        source_file: source.file_path.clone(),
                        source_line: source.line_start,
                        sink_function: sink.name.clone(),
                        sink_file: sink.file_path.clone(),
                        sink_line: sink.line_start,
                        category,
                        call_chain,
                        is_sanitized: sanitizer.is_some(),
                        sanitizer,
                        confidence: 0.7, // Base confidence for graph-traced paths
                    });
                }
            }
        }

        paths
    }

    /// Check if a function is a potential taint source (e.g., route handler)
    fn is_potential_source_function(
        &self,
        func: &crate::graph::CodeNode,
        _category: TaintCategory,
    ) -> bool {
        let name_lower = func.name.to_lowercase();
        let qn_lower = func.qualified_name.to_lowercase();

        // Route handlers are typically taint sources
        let is_route_handler = name_lower.contains("handler")
            || name_lower.contains("controller")
            || name_lower.contains("view")
            || name_lower.contains("endpoint")
            || name_lower.starts_with("get_")
            || name_lower.starts_with("post_")
            || name_lower.starts_with("put_")
            || name_lower.starts_with("delete_")
            || name_lower.starts_with("patch_")
            || name_lower.starts_with("handle_");

        // Check if function references any source patterns
        let references_source = if let Some(sources) = self.sources.get(&_category) {
            sources.iter().any(|s| {
                qn_lower.contains(&s.to_lowercase()) || name_lower.contains(&s.to_lowercase())
            })
        } else {
            false
        };

        // Check properties for route decorator indicators
        let has_route_decorator = func
            .get_str("decorators")
            .map(|d| {
                d.contains("@app.route")
                    || d.contains("@router")
                    || d.contains("@get")
                    || d.contains("@post")
                    || d.contains("@api")
            })
            .unwrap_or(false);

        is_route_handler || references_source || has_route_decorator
    }

    /// BFS from a source function to find paths to any sink
    fn bfs_to_sinks(
        &self,
        graph: &GraphStore,
        source_qn: &str,
        sink_qns: &HashSet<&str>,
        category: TaintCategory,
    ) -> Vec<(String, Vec<String>, Option<String>)> {
        let mut results = Vec::new();
        let mut visited = HashSet::new();
        let mut queue = VecDeque::new();

        // (current_qn, path, sanitizer_found)
        queue.push_back((source_qn.to_string(), Vec::new(), None::<String>));
        visited.insert(source_qn.to_string());

        while let Some((current_qn, path, sanitizer)) = queue.pop_front() {
            if path.len() > self.max_depth {
                continue;
            }

            // Check if we've reached a sink
            if sink_qns.contains(current_qn.as_str()) {
                results.push((current_qn.clone(), path.clone(), sanitizer.clone()));
                continue;
            }

            // Get callees (functions called by current)
            let callees = graph.get_callees(&current_qn);

            for callee in callees {
                if visited.contains(&callee.qualified_name) {
                    continue;
                }

                visited.insert(callee.qualified_name.clone());

                let mut new_path = path.clone();
                new_path.push(callee.name.clone());

                // Check if this callee is a sanitizer
                let new_sanitizer = if sanitizer.is_some() {
                    sanitizer.clone()
                } else if self.is_sanitizer(&callee.name, category)
                    || self.is_sanitizer(&callee.qualified_name, category)
                {
                    Some(callee.name.clone())
                } else {
                    None
                };

                queue.push_back((callee.qualified_name.clone(), new_path, new_sanitizer));
            }
        }

        results
    }

    /// Analyze a specific function for taint issues using both graph and local analysis
    ///
    /// This combines graph-based call chain analysis with local pattern matching
    /// for more comprehensive coverage.
    pub fn analyze_function(
        &self,
        graph: &GraphStore,
        func_qn: &str,
        category: TaintCategory,
    ) -> Vec<TaintPath> {
        let mut paths = Vec::new();

        // Get the function
        let func = match graph.get_node(func_qn) {
            Some(f) => f,
            None => return paths,
        };

        // Check direct callees for sinks
        let callees = graph.get_callees(func_qn);
        for callee in &callees {
            if self.is_sink(&callee.name, category) || self.is_sink(&callee.qualified_name, category)
            {
                // Direct call to sink from this function
                let is_sanitized = callees.iter().any(|c| {
                    self.is_sanitizer(&c.name, category)
                        || self.is_sanitizer(&c.qualified_name, category)
                });

                let sanitizer = if is_sanitized {
                    callees
                        .iter()
                        .find(|c| {
                            self.is_sanitizer(&c.name, category)
                                || self.is_sanitizer(&c.qualified_name, category)
                        })
                        .map(|c| c.name.clone())
                } else {
                    None
                };

                paths.push(TaintPath {
                    source_function: func.name.clone(),
                    source_file: func.file_path.clone(),
                    source_line: func.line_start,
                    sink_function: callee.name.clone(),
                    sink_file: callee.file_path.clone(),
                    sink_line: callee.line_start,
                    category,
                    call_chain: vec![],
                    is_sanitized,
                    sanitizer,
                    confidence: 0.8, // Higher confidence for direct calls
                });
            }
        }

        // Also trace through the call graph for indirect paths
        let indirect_paths = self.bfs_to_sinks(
            graph,
            func_qn,
            &callees
                .iter()
                .filter(|c| self.is_sink(&c.name, category))
                .map(|c| c.qualified_name.as_str())
                .collect(),
            category,
        );

        for (sink_qn, chain, sanitizer) in indirect_paths {
            if let Some(sink) = graph.get_node(&sink_qn) {
                paths.push(TaintPath {
                    source_function: func.name.clone(),
                    source_file: func.file_path.clone(),
                    source_line: func.line_start,
                    sink_function: sink.name.clone(),
                    sink_file: sink.file_path.clone(),
                    sink_line: sink.line_start,
                    category,
                    call_chain: chain,
                    is_sanitized: sanitizer.is_some(),
                    sanitizer,
                    confidence: 0.6, // Lower confidence for indirect paths
                });
            }
        }

        paths
    }

    /// Get all sink patterns for a category (useful for regex-based detection)
    pub fn get_sinks(&self, category: TaintCategory) -> Option<&HashSet<String>> {
        self.sinks.get(&category)
    }

    /// Get all source patterns for a category
    pub fn get_sources(&self, category: TaintCategory) -> Option<&HashSet<String>> {
        self.sources.get(&category)
    }

    /// Get all sanitizer patterns for a category
    pub fn get_sanitizers(&self, category: TaintCategory) -> Option<&HashSet<String>> {
        self.sanitizers.get(&category)
    }

    /// Add a custom source pattern
    pub fn add_source(&mut self, category: TaintCategory, pattern: String) {
        self.sources.entry(category).or_default().insert(pattern);
    }

    /// Add a custom sink pattern
    pub fn add_sink(&mut self, category: TaintCategory, pattern: String) {
        self.sinks.entry(category).or_default().insert(pattern);
    }

    /// Add a custom sanitizer pattern
    pub fn add_sanitizer(&mut self, category: TaintCategory, pattern: String) {
        self.sanitizers.entry(category).or_default().insert(pattern);
    }
}

/// Result of taint analysis for a file or function
#[derive(Debug, Clone)]
pub struct TaintAnalysisResult {
    /// All taint paths found
    pub paths: Vec<TaintPath>,
    /// Number of vulnerable paths (unsanitized)
    pub vulnerable_count: usize,
    /// Number of sanitized paths
    pub sanitized_count: usize,
}

impl TaintAnalysisResult {
    /// Create from a list of paths
    pub fn from_paths(paths: Vec<TaintPath>) -> Self {
        let vulnerable_count = paths.iter().filter(|p| p.is_vulnerable()).count();
        let sanitized_count = paths.iter().filter(|p| p.is_sanitized).count();

        Self {
            paths,
            vulnerable_count,
            sanitized_count,
        }
    }

    /// Get only vulnerable paths
    pub fn vulnerable_paths(&self) -> Vec<&TaintPath> {
        self.paths.iter().filter(|p| p.is_vulnerable()).collect()
    }

    /// Check if there are any vulnerabilities
    pub fn has_vulnerabilities(&self) -> bool {
        self.vulnerable_count > 0
    }
}

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

    #[test]
    fn test_taint_category_cwe() {
        assert_eq!(TaintCategory::SqlInjection.cwe_id(), "CWE-89");
        assert_eq!(TaintCategory::CommandInjection.cwe_id(), "CWE-78");
        assert_eq!(TaintCategory::Xss.cwe_id(), "CWE-79");
    }

    #[test]
    fn test_is_source() {
        let analyzer = TaintAnalyzer::new();

        assert!(analyzer.is_source("req.body", TaintCategory::SqlInjection));
        assert!(analyzer.is_source("request.form", TaintCategory::SqlInjection));
        assert!(analyzer.is_source("c.Param", TaintCategory::SqlInjection));
        assert!(!analyzer.is_source("random_function", TaintCategory::SqlInjection));
    }

    #[test]
    fn test_is_sink() {
        let analyzer = TaintAnalyzer::new();

        assert!(analyzer.is_sink("cursor.execute", TaintCategory::SqlInjection));
        assert!(analyzer.is_sink("db.query", TaintCategory::SqlInjection));
        assert!(analyzer.is_sink("os.system", TaintCategory::CommandInjection));
        assert!(analyzer.is_sink("innerHTML", TaintCategory::Xss));
        assert!(!analyzer.is_sink("print", TaintCategory::SqlInjection));
    }

    #[test]
    fn test_is_sanitizer() {
        let analyzer = TaintAnalyzer::new();

        assert!(analyzer.is_sanitizer("escapeHtml", TaintCategory::Xss));
        assert!(analyzer.is_sanitizer("shlex.quote", TaintCategory::CommandInjection));
        assert!(analyzer.is_sanitizer("validate_input", TaintCategory::SqlInjection)); // generic
        assert!(analyzer.is_sanitizer("sanitize_data", TaintCategory::Xss)); // generic
    }

    #[test]
    fn test_taint_path_is_vulnerable() {
        let vulnerable_path = TaintPath {
            source_function: "handler".to_string(),
            source_file: "app.py".to_string(),
            source_line: 10,
            sink_function: "execute".to_string(),
            sink_file: "db.py".to_string(),
            sink_line: 20,
            category: TaintCategory::SqlInjection,
            call_chain: vec![],
            is_sanitized: false,
            sanitizer: None,
            confidence: 0.8,
        };

        let safe_path = TaintPath {
            is_sanitized: true,
            sanitizer: Some("escape".to_string()),
            ..vulnerable_path.clone()
        };

        assert!(vulnerable_path.is_vulnerable());
        assert!(!safe_path.is_vulnerable());
    }

    #[test]
    fn test_taint_path_string() {
        let path = TaintPath {
            source_function: "handler".to_string(),
            source_file: "app.py".to_string(),
            source_line: 10,
            sink_function: "execute".to_string(),
            sink_file: "db.py".to_string(),
            sink_line: 20,
            category: TaintCategory::SqlInjection,
            call_chain: vec!["process".to_string(), "query".to_string()],
            is_sanitized: false,
            sanitizer: None,
            confidence: 0.8,
        };

        assert_eq!(path.path_string(), "handler → process → query → execute");
    }

    #[test]
    fn test_analysis_result() {
        let paths = vec![
            TaintPath {
                source_function: "a".to_string(),
                source_file: "a.py".to_string(),
                source_line: 1,
                sink_function: "b".to_string(),
                sink_file: "b.py".to_string(),
                sink_line: 2,
                category: TaintCategory::SqlInjection,
                call_chain: vec![],
                is_sanitized: false,
                sanitizer: None,
                confidence: 0.8,
            },
            TaintPath {
                source_function: "c".to_string(),
                source_file: "c.py".to_string(),
                source_line: 3,
                sink_function: "d".to_string(),
                sink_file: "d.py".to_string(),
                sink_line: 4,
                category: TaintCategory::SqlInjection,
                call_chain: vec![],
                is_sanitized: true,
                sanitizer: Some("escape".to_string()),
                confidence: 0.8,
            },
        ];

        let result = TaintAnalysisResult::from_paths(paths);

        assert_eq!(result.vulnerable_count, 1);
        assert_eq!(result.sanitized_count, 1);
        assert!(result.has_vulnerabilities());
        assert_eq!(result.vulnerable_paths().len(), 1);
    }
}