repotoire 0.7.0

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
//! Regex Compilation in Loop Detector
//!
//! Graph-enhanced detection of regex compilation inside loops.
//! Uses graph to:
//! - Find hidden patterns (loop calls function that compiles regex)
//! - Track regex functions that might be called in loops

use crate::detectors::base::{Detector, DetectorConfig};
use crate::graph::GraphQueryExt;
use crate::models::{deterministic_finding_id, Finding, Severity};
use anyhow::Result;
use regex::Regex;
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::LazyLock;
use tracing::info;

static LOOP: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?i)(for\s+\w+\s+in|\.forEach|for\s*\(|while\s*\()").expect("valid regex")
});
static REGEX_NEW: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?i)(Regex::new|re\.compile|new RegExp|Pattern\.compile)").expect("valid regex")
});

/// Check if a regex compilation is cached (OnceLock, lazy_static, etc.)
fn is_cached_regex(line: &str) -> bool {
    let trimmed = line.trim();
    trimmed.contains("get_or_init")
        || trimmed.contains("lazy_static")
        || trimmed.contains("LazyLock")
        || trimmed.contains("Lazy::new")
        || trimmed.contains("OnceLock")
        || trimmed.contains("OnceCell")
        // Skip lines that are just string constants containing regex pattern names
        || trimmed.starts_with('"')
        || trimmed.starts_with("r#\"")
        || trimmed.starts_with("r\"")
        // Skip lines inside Rust multi-line string literals (end with \n\)
        || trimmed.ends_with("\\n\\")
        || trimmed.ends_with(".to_string(),")
}

/// Check if a Regex::new call is inside a cached context by looking at surrounding lines
fn is_cached_regex_context(content: &str, line_idx: usize) -> bool {
    let lines: Vec<&str> = content.lines().collect();
    // Check ±5 lines for caching patterns
    let start = line_idx.saturating_sub(5);
    let end = (line_idx + 3).min(lines.len());
    for j in start..end {
        if let Some(l) = lines.get(j) {
            if is_cached_regex(l) {
                return true;
            }
        }
    }
    false
}

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

impl RegexInLoopDetector {
    crate::detectors::detector_new!(50);

    /// Find functions that compile regexes.
    /// Uses per-file line caching to avoid redundant content reads (71K functions → ~3.4K files).
    fn find_regex_functions(
        &self,
        graph: &dyn crate::graph::GraphQuery,
        det_ctx: &crate::detectors::DetectorContext,
    ) -> HashSet<String> {
        let i = graph.interner();
        let mut regex_funcs = HashSet::new();
        let mut file_lines: std::collections::HashMap<String, Vec<String>> =
            std::collections::HashMap::new();
        let ctx = Some(det_ctx);

        for func in graph.get_functions_shared().iter() {
            let lines = file_lines
                .entry(func.path(i).to_string())
                .or_insert_with(|| {
                    let path = std::path::Path::new(func.path(i));
                    // Try DetectorContext first, fall back to global_cache
                    if let Some(content) = ctx.and_then(|c| c.file_contents.get(path)).map(|s| &**s)
                    {
                        content.lines().map(String::from).collect()
                    } else {
                        crate::cache::global_cache()
                            .content(path)
                            .map(|c| c.lines().map(String::from).collect())
                            .unwrap_or_default()
                    }
                });

            let start = func.line_start.saturating_sub(1) as usize;
            let end = (func.line_end as usize).min(lines.len());

            // Check if the function itself uses caching patterns
            let func_is_cached = lines
                .get(start..end)
                .map(|slice| {
                    slice.iter().any(|line| {
                        line.contains("get_or_init")
                            || line.contains("OnceLock")
                            || line.contains("OnceCell")
                            || line.contains("lazy_static")
                            || line.contains("LazyLock")
                    })
                })
                .unwrap_or(false);

            if !func_is_cached {
                for line in lines.get(start..end).unwrap_or(&[]) {
                    if REGEX_NEW.is_match(line) && !is_cached_regex(line) {
                        regex_funcs.insert(func.qn(i).to_string());
                        break;
                    }
                }
            }
        }

        regex_funcs
    }

    /// Check if function transitively compiles regex
    #[allow(clippy::only_used_in_recursion)]
    fn calls_regex_transitively(
        &self,
        graph: &dyn crate::graph::GraphQuery,
        det_ctx: &crate::detectors::DetectorContext,
        func_qn: &str,
        regex_funcs: &HashSet<String>,
        visited: &mut HashSet<String>,
        depth: usize,
    ) -> Option<String> {
        let i = graph.interner();
        if depth > 3 || visited.contains(func_qn) {
            return None;
        }
        visited.insert(func_qn.to_string());

        // Use pre-built callees map
        if let Some(callee_qn_list) = det_ctx.callees_by_qn.get(func_qn) {
            for callee_qn in callee_qn_list {
                let callee_name = graph
                    .get_node(callee_qn)
                    .map(|n| n.node_name(i).to_string())
                    .unwrap_or_else(|| callee_qn.clone());
                if regex_funcs.contains(callee_qn) {
                    return Some(callee_name);
                }
                if let Some(chain) = self.calls_regex_transitively(
                    graph,
                    det_ctx,
                    callee_qn,
                    regex_funcs,
                    visited,
                    depth + 1,
                ) {
                    return Some(format!("{} \u{2192} {}", callee_name, chain));
                }
            }
        } else {
            // Fallback: use graph.get_callees() (empty callees map)
            for callee in graph.get_callees(func_qn) {
                if regex_funcs.contains(callee.qn(i)) {
                    return Some(callee.node_name(i).to_string());
                }
                if let Some(chain) = self.calls_regex_transitively(
                    graph,
                    det_ctx,
                    callee.qn(i),
                    regex_funcs,
                    visited,
                    depth + 1,
                ) {
                    return Some(format!("{} \u{2192} {}", callee.node_name(i), chain));
                }
            }
        }
        None
    }
}

impl Detector for RegexInLoopDetector {
    fn name(&self) -> &'static str {
        "regex-in-loop"
    }
    fn description(&self) -> &'static str {
        "Detects regex compilation inside loops"
    }

    fn file_extensions(&self) -> &'static [&'static str] {
        &["py", "js", "ts", "jsx", "tsx", "rb", "java", "go"]
    }

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

        // Find all functions that compile regex
        let regex_funcs = self.find_regex_functions(graph, det_ctx);

        for path in files.files_with_extensions(&["py", "js", "ts", "java", "rs", "go"]) {
            if findings.len() >= self.max_findings {
                break;
            }

            if let Some(content) = files.content(path) {
                let _path_str = path.to_string_lossy().to_string();
                let is_python = path.extension().and_then(|e| e.to_str()) == Some("py");
                let is_rust = path.extension().and_then(|e| e.to_str()) == Some("rs");
                let mut in_loop = false;
                let mut loop_line = 0;
                let mut brace_depth = 0;
                let mut loop_indent: usize = 0;
                let mut loop_line_idx: usize = 0;
                let all_lines: Vec<&str> = content.lines().collect();

                for (i, line) in all_lines.iter().enumerate() {
                    // Skip Python comments
                    if is_python && line.trim().starts_with('#') {
                        continue;
                    }

                    // Skip list comprehensions (one-shot constructs)
                    if is_python
                        && line.contains('[')
                        && line.contains(" for ")
                        && line.contains(" in ")
                    {
                        continue;
                    }

                    if LOOP.is_match(line) {
                        in_loop = true;
                        loop_line = i + 1;
                        loop_line_idx = i;
                        if is_python {
                            loop_indent = line.len() - line.trim_start().len();
                        } else {
                            brace_depth = 0;
                        }
                    }

                    if in_loop {
                        if is_python {
                            // Python: exit loop scope when indentation returns to/below loop level
                            let trimmed = line.trim();
                            if !trimmed.is_empty() && i > loop_line_idx {
                                let current_indent = line.len() - line.trim_start().len();
                                if current_indent <= loop_indent {
                                    in_loop = false;
                                    continue;
                                }
                            }
                        } else {
                            // Brace-based languages
                            brace_depth += line.matches('{').count() as i32;
                            brace_depth -= line.matches('}').count() as i32;
                            if brace_depth < 0 {
                                in_loop = false;
                                continue;
                            }
                        }

                        // Direct regex compilation in loop
                        // Skip cached patterns: OnceLock, lazy_static, static, get_or_init
                        if REGEX_NEW.is_match(line)
                            && !is_cached_regex(line)
                            && !is_cached_regex_context(&content, i)
                        {
                            let prev_line = if i > 0 { Some(all_lines[i - 1]) } else { None };
                            if crate::detectors::is_line_suppressed(line, prev_line) {
                                continue;
                            }
                            // Skip test code in Rust files (string literals containing regex patterns)
                            if is_rust
                                && crate::detectors::rust_smells::is_test_context(line, &content, i)
                            {
                                continue;
                            }

                            findings.push(Finding {
                                id: String::new(),
                                detector: "RegexInLoopDetector".to_string(),
                                severity: Severity::Medium,
                                title: "Regex compiled inside loop".to_string(),
                                description: format!(
                                    "Regex compiled in loop (loop starts line {}).\n\n\
                                     **Performance impact:** Regex compilation is expensive. \
                                     In a loop of N iterations, this costs N compilations instead of 1.",
                                    loop_line
                                ),
                                affected_files: vec![path.to_path_buf()],
                                line_start: Some((i + 1) as u32),
                                line_end: Some((i + 1) as u32),
                                suggested_fix: Some(
                                    "Move regex compilation outside the loop:\n\
                                     ```rust\n\
                                     let re = Regex::new(pattern)?;  // Before loop\n\
                                     for item in items {\n\
                                         re.is_match(item);  // Reuse\n\
                                     }\n\
                                     ```".to_string()
                                ),
                                estimated_effort: Some("10 minutes".to_string()),
                                category: Some("performance".to_string()),
                                cwe_id: None,
                                why_it_matters: Some(
                                    "Regex compilation involves parsing and optimization. \
                                     Doing it once instead of N times can dramatically improve performance.".to_string()
                                ),
                                ..Default::default()
                            });
                            in_loop = false;
                        }
                    }
                }
            }
        }

        // Graph-based: find loops that call regex-compiling functions
        // Skip Rust files — OnceLock/lazy_static caching is pervasive and
        // the call graph can't distinguish cached from uncached compilation
        if !regex_funcs.is_empty() {
            let mut graph_file_lines: std::collections::HashMap<String, Vec<String>> =
                std::collections::HashMap::new();

            for func in graph.get_functions_shared().iter() {
                if findings.len() >= self.max_findings {
                    break;
                }

                if func.path(i).ends_with(".rs") {
                    continue;
                }

                // Check if function contains a loop (per-file line caching)
                let lines = graph_file_lines
                    .entry(func.path(i).to_string())
                    .or_insert_with(|| {
                        let path = std::path::Path::new(func.path(i));
                        // Try DetectorContext first, fall back to global_cache
                        if let Some(content) = det_ctx.file_contents.get(path).map(|s| &**s) {
                            content.lines().map(String::from).collect()
                        } else {
                            crate::cache::global_cache()
                                .content(path)
                                .map(|c| c.lines().map(String::from).collect())
                                .unwrap_or_default()
                        }
                    });

                let start = func.line_start.saturating_sub(1) as usize;
                let end = (func.line_end as usize).min(lines.len());

                let has_loop = lines
                    .get(start..end)
                    .map(|slice| slice.iter().any(|line| LOOP.is_match(line)))
                    .unwrap_or(false);

                if !has_loop {
                    continue;
                }

                // Check callees for regex compilation
                // Collect callee (qn, name) pairs from pre-built callees map or graph fallback
                let callee_pairs: Vec<(String, String)> = det_ctx
                    .callees_by_qn
                    .get(func.qn(i))
                    .map(|v| {
                        v.iter()
                            .map(|qn| {
                                let name = graph
                                    .get_node(qn)
                                    .map(|n| n.node_name(i).to_string())
                                    .unwrap_or_else(|| qn.clone());
                                (qn.clone(), name)
                            })
                            .collect()
                    })
                    .unwrap_or_else(|| {
                        // Fallback: use graph.get_callees() (empty callees map)
                        graph
                            .get_callees(func.qn(i))
                            .into_iter()
                            .map(|c| (c.qn(i).to_string(), c.node_name(i).to_string()))
                            .collect()
                    });

                for (callee_qn, callee_name) in &callee_pairs {
                    let mut visited = HashSet::new();
                    if let Some(chain) = self.calls_regex_transitively(
                        graph,
                        det_ctx,
                        callee_qn,
                        &regex_funcs,
                        &mut visited,
                        0,
                    ) {
                        findings.push(Finding {
                            id: String::new(),
                            detector: "RegexInLoopDetector".to_string(),
                            severity: Severity::Medium,
                            title: format!("Hidden regex in loop: {}", func.node_name(i)),
                            description: format!(
                                "Function '{}' contains a loop and calls '{}' which compiles a regex.\n\n\
                                 **Call chain:** {} \u{2192} {}\n\n\
                                 This may cause regex compilation on every iteration.",
                                func.node_name(i), callee_name, callee_name, chain
                            ),
                            affected_files: vec![PathBuf::from(func.path(i))],
                            line_start: Some(func.line_start),
                            line_end: Some(func.line_end),
                            suggested_fix: Some(
                                "Options:\n\
                                 1. Cache the compiled regex (lazy_static, OnceLock)\n\
                                 2. Pass pre-compiled regex as parameter\n\
                                 3. Restructure to compile once before loop".to_string()
                            ),
                            estimated_effort: Some("20 minutes".to_string()),
                            category: Some("performance".to_string()),
                            cwe_id: None,
                            why_it_matters: Some(
                                "Hidden regex compilation across function boundaries \
                                 is harder to spot but equally impactful.".to_string()
                            ),
                            ..Default::default()
                        });
                        break;
                    }
                }
            }
        }

        info!(
            "RegexInLoopDetector found {} findings (graph-aware)",
            findings.len()
        );
        Ok(findings)
    }
}

impl crate::detectors::RegisteredDetector for RegexInLoopDetector {
    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_re_compile_in_loop() {
        let store = GraphBuilder::new().freeze();
        let detector = RegexInLoopDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(&store, vec![
            ("parser.py", "import re\n\ndef process_lines(lines):\n    for line in lines:\n        pattern = re.compile(r'\\d+')\n        match = pattern.match(line)\n        if match:\n            print(match.group())\n"),
        ]);
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            !findings.is_empty(),
            "Should detect re.compile() inside a for loop"
        );
        assert!(
            findings
                .iter()
                .any(|f| f.title.contains("Regex compiled inside loop")),
            "Finding title should mention regex compiled inside loop"
        );
    }

    #[test]
    fn test_no_finding_when_regex_outside_loop() {
        let store = GraphBuilder::new().freeze();
        let detector = RegexInLoopDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(&store, vec![
            ("parser_good.py", "import re\n\ndef process_lines(lines):\n    pattern = re.compile(r'\\d+')\n    for line in lines:\n        match = pattern.match(line)\n        if match:\n            print(match.group())\n"),
        ]);
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.is_empty(),
            "Should not flag re.compile() outside a loop, got: {:?}",
            findings
        );
    }

    #[test]
    fn test_no_finding_for_python_regex_outside_loop() {
        let store = GraphBuilder::new().freeze();
        let detector = RegexInLoopDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(&store, vec![
            ("parser.py", "import re\n\nfor item in items:\n    process(item)\n\npattern = re.compile(r'\\w+')\n"),
        ]);
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.is_empty(),
            "re.compile after loop exits should not be flagged. Found: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_no_finding_for_list_comprehension() {
        let store = GraphBuilder::new().freeze();
        let detector = RegexInLoopDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(&store, vec![
            ("security.py", "import re\n\nREDIRECT_HOSTS = [re.compile(r) for r in settings.ALLOWED_REDIRECTS]\n"),
        ]);
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.is_empty(),
            "List comprehension re.compile should not be flagged. Found: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_no_finding_for_regex_in_comment() {
        let store = GraphBuilder::new().freeze();
        let detector = RegexInLoopDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(&store, vec![
            ("settings.py", "LANGUAGES = [\n    ('en', 'English'),\n    ('fr', 'French'),\n]\n# LANGUAGES_BIDI = re.compile(r'...')\n"),
        ]);
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            findings.is_empty(),
            "Commented-out re.compile should not be flagged. Found: {:?}",
            findings.iter().map(|f| &f.title).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_still_detects_regex_inside_python_loop() {
        let store = GraphBuilder::new().freeze();
        let detector = RegexInLoopDetector::new("/mock/repo");
        let ctx = crate::detectors::analysis_context::AnalysisContext::test_with_mock_files(&store, vec![
            ("slow.py", "import re\n\nfor pattern in patterns:\n    compiled = re.compile(pattern)\n    compiled.match(text)\n"),
        ]);
        let findings = detector.detect(&ctx).expect("detection should succeed");
        assert!(
            !findings.is_empty(),
            "Should still detect re.compile inside a for loop"
        );
    }
}