go-brrr 0.1.0

Token-efficient code analysis for LLMs - Rust implementation
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
//! Taint source identification for multiple languages.
//!
//! This module provides two complementary approaches to identifying taint sources:
//!
//! 1. **Pattern-based matching** ([`registry`]): Fast string pattern matching for
//!    common source patterns. Used for quick lookups during analysis.
//!
//! 2. **AST-based detection** ([`python`], [`typescript`], [`go`], [`rust`]): Deep AST
//!    analysis using tree-sitter for precise source identification including decorator
//!    analysis, handler parameter detection, and language-specific patterns.
//!
//! # Pattern-Based Usage
//!
//! For quick pattern matching against code snippets:
//!
//! ```ignore
//! use go_brrr::security::taint::sources::{get_python_sources, SourceRegistry};
//!
//! let registry = get_python_sources();
//! let matches = registry.find_matches("request.args.get('id')");
//! assert!(matches.iter().any(|s| s.label == TaintLabel::UserInput));
//! ```
//!
//! # AST-Based Usage
//!
//! For comprehensive analysis of source files:
//!
//! ```ignore
//! use go_brrr::security::taint::sources::python::PythonSourceDetector;
//! use go_brrr::security::taint::sources::go::GoSourceDetector;
//! use go_brrr::security::taint::sources::rust::RustSourceDetector;
//!
//! // Python
//! let detector = PythonSourceDetector::new();
//! let result = detector.scan_file("app.py")?;
//!
//! // Go
//! let detector = GoSourceDetector::new();
//! let result = detector.scan_file("handler.go")?;
//!
//! // Rust
//! let detector = RustSourceDetector::new();
//! let result = detector.scan_file("handler.rs")?;
//!
//! for source in &result.sources {
//!     println!("{}: {} at line {}", source.kind, source.expression, source.location.line);
//! }
//! ```

// Pattern-based source registry (original implementation)
pub mod registry;

// AST-based Python source detector
pub mod python;

// AST-based TypeScript/JavaScript source detector
pub mod typescript;

// AST-based Go source detector
pub mod go;

// AST-based Rust source detector
pub mod rust;

// Re-export everything from registry for backward compatibility
pub use registry::{
    get_go_sources, get_python_sources, get_rust_sources, get_sources_for_language,
    get_typescript_sources, MatchStrategy, SourceRegistry, TaintSource,
};

// Re-export AST-based detectors
pub use go::GoSourceDetector;
pub use python::PythonSourceDetector;
pub use rust::RustSourceDetector;
pub use typescript::TypeScriptSourceDetector;

use crate::security::taint::types::{Location, TaintLabel};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

// =============================================================================
// AST-Based Source Detection Types
// =============================================================================

/// Category of taint source for AST-based detection.
///
/// This provides more granular classification than TaintLabel for
/// precise source identification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SourceKind {
    /// Web request parameters (GET/POST/query string)
    RequestParam,
    /// Request body (JSON, form data)
    RequestBody,
    /// HTTP headers
    HttpHeader,
    /// Cookie values
    Cookie,
    /// Uploaded files
    FileUpload,
    /// URL path segments
    UrlPath,
    /// Standard input (input(), stdin)
    Stdin,
    /// Command line arguments (sys.argv, process.argv)
    ProcessArgs,
    /// Environment variables
    Environment,
    /// File content from user-controlled path
    FileRead,
    /// HTTP response from external URL
    HttpResponse,
    /// Socket receive
    SocketRecv,
    /// Database query result
    DatabaseResult,
    /// Deserialized data (JSON, YAML, pickle)
    Deserialized,
    /// External API response
    ExternalApi,
    /// WebSocket message
    WebSocketMessage,
    /// Generic user input (unclassified)
    GenericUserInput,
}

impl SourceKind {
    /// Convert source kind to corresponding taint labels.
    pub fn to_taint_labels(&self) -> HashSet<TaintLabel> {
        let mut labels = HashSet::new();
        match self {
            SourceKind::RequestParam | SourceKind::RequestBody => {
                labels.insert(TaintLabel::UserInput);
            }
            SourceKind::HttpHeader => {
                labels.insert(TaintLabel::HttpHeader);
            }
            SourceKind::Cookie => {
                labels.insert(TaintLabel::Cookie);
            }
            SourceKind::FileUpload => {
                labels.insert(TaintLabel::UserInput);
                labels.insert(TaintLabel::FileContent);
            }
            SourceKind::UrlPath => {
                labels.insert(TaintLabel::UrlData);
            }
            SourceKind::Stdin => {
                labels.insert(TaintLabel::Stdin);
            }
            SourceKind::ProcessArgs => {
                labels.insert(TaintLabel::ProcessArgs);
            }
            SourceKind::Environment => {
                labels.insert(TaintLabel::Environment);
            }
            SourceKind::FileRead => {
                labels.insert(TaintLabel::FileContent);
            }
            SourceKind::HttpResponse | SourceKind::ExternalApi => {
                labels.insert(TaintLabel::NetworkData);
                labels.insert(TaintLabel::ExternalApi);
            }
            SourceKind::SocketRecv => {
                labels.insert(TaintLabel::NetworkData);
            }
            SourceKind::DatabaseResult => {
                labels.insert(TaintLabel::DatabaseQuery);
            }
            SourceKind::Deserialized => {
                labels.insert(TaintLabel::DeserializedData);
            }
            SourceKind::WebSocketMessage => {
                labels.insert(TaintLabel::NetworkData);
                labels.insert(TaintLabel::UserInput);
            }
            SourceKind::GenericUserInput => {
                labels.insert(TaintLabel::UserInput);
            }
        }
        labels
    }

    /// Get severity weight for prioritizing findings.
    pub fn severity_weight(&self) -> u8 {
        match self {
            SourceKind::RequestParam | SourceKind::RequestBody => 10,
            SourceKind::ProcessArgs | SourceKind::Stdin => 9,
            SourceKind::Cookie | SourceKind::UrlPath => 8,
            SourceKind::HttpHeader | SourceKind::WebSocketMessage => 7,
            SourceKind::HttpResponse | SourceKind::ExternalApi => 7,
            SourceKind::Deserialized => 8,
            SourceKind::FileUpload | SourceKind::FileRead => 6,
            SourceKind::SocketRecv => 6,
            SourceKind::DatabaseResult => 5,
            SourceKind::Environment => 4,
            SourceKind::GenericUserInput => 5,
        }
    }

    /// Get human-readable description.
    pub fn description(&self) -> &'static str {
        match self {
            SourceKind::RequestParam => "HTTP request parameter",
            SourceKind::RequestBody => "HTTP request body",
            SourceKind::HttpHeader => "HTTP header",
            SourceKind::Cookie => "HTTP cookie",
            SourceKind::FileUpload => "uploaded file",
            SourceKind::UrlPath => "URL path segment",
            SourceKind::Stdin => "standard input",
            SourceKind::ProcessArgs => "command line argument",
            SourceKind::Environment => "environment variable",
            SourceKind::FileRead => "file content",
            SourceKind::HttpResponse => "HTTP response",
            SourceKind::SocketRecv => "socket data",
            SourceKind::DatabaseResult => "database query result",
            SourceKind::Deserialized => "deserialized data",
            SourceKind::ExternalApi => "external API response",
            SourceKind::WebSocketMessage => "WebSocket message",
            SourceKind::GenericUserInput => "user input",
        }
    }
}

impl std::fmt::Display for SourceKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.description())
    }
}

// =============================================================================
// Detected Taint Source
// =============================================================================

/// A detected taint source in source code (AST-based).
///
/// Represents a location where untrusted data enters the program.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DetectedSource {
    /// Kind of taint source
    pub kind: SourceKind,
    /// Location in source code
    pub location: Location,
    /// The expression that introduces taint (e.g., "request.args.get('id')")
    pub expression: String,
    /// Variable name the tainted value is assigned to (if applicable)
    pub assigned_to: Option<String>,
    /// Confidence score (0.0 to 1.0)
    pub confidence: f64,
    /// Framework or library this source belongs to (e.g., "flask", "django")
    pub framework: Option<String>,
    /// Additional context (e.g., parameter name, header name)
    pub context: Option<String>,
    /// Whether this source is in a handler function (e.g., Flask route)
    pub in_handler: bool,
    /// Name of the enclosing function
    pub enclosing_function: Option<String>,
}

impl DetectedSource {
    /// Create a new detected source.
    pub fn new(kind: SourceKind, location: Location, expression: impl Into<String>) -> Self {
        Self {
            kind,
            location,
            expression: expression.into(),
            assigned_to: None,
            confidence: 1.0,
            framework: None,
            context: None,
            in_handler: false,
            enclosing_function: None,
        }
    }

    /// Set the variable this source is assigned to.
    pub fn with_assignment(mut self, var: impl Into<String>) -> Self {
        self.assigned_to = Some(var.into());
        self
    }

    /// Set confidence score.
    pub fn with_confidence(mut self, confidence: f64) -> Self {
        self.confidence = confidence.clamp(0.0, 1.0);
        self
    }

    /// Set the framework context.
    pub fn with_framework(mut self, framework: impl Into<String>) -> Self {
        self.framework = Some(framework.into());
        self
    }

    /// Set additional context.
    pub fn with_context(mut self, context: impl Into<String>) -> Self {
        self.context = Some(context.into());
        self
    }

    /// Mark as being in a handler function.
    pub fn in_handler_function(mut self, func_name: impl Into<String>) -> Self {
        self.in_handler = true;
        self.enclosing_function = Some(func_name.into());
        self
    }

    /// Get taint labels for this source.
    pub fn taint_labels(&self) -> HashSet<TaintLabel> {
        self.kind.to_taint_labels()
    }
}

// =============================================================================
// Source Pattern Definition (for AST detector)
// =============================================================================

/// A pattern for detecting taint sources in AST analysis.
#[derive(Debug, Clone)]
pub struct SourcePattern {
    /// Pattern name for identification
    pub name: &'static str,
    /// Kind of source this pattern detects
    pub kind: SourceKind,
    /// Object/module the method is called on (e.g., "request", "os")
    pub object: Option<&'static str>,
    /// Method/attribute name (e.g., "args", "environ")
    pub method: &'static str,
    /// Whether this is a property access (vs method call)
    pub is_property: bool,
    /// Confidence score for matches
    pub confidence: f64,
    /// Framework/library this belongs to
    pub framework: Option<&'static str>,
}

impl SourcePattern {
    /// Create a method call pattern.
    pub const fn method_call(
        name: &'static str,
        kind: SourceKind,
        object: &'static str,
        method: &'static str,
        framework: Option<&'static str>,
    ) -> Self {
        Self {
            name,
            kind,
            object: Some(object),
            method,
            is_property: false,
            confidence: 0.95,
            framework,
        }
    }

    /// Create a property access pattern.
    pub const fn property_access(
        name: &'static str,
        kind: SourceKind,
        object: &'static str,
        property: &'static str,
        framework: Option<&'static str>,
    ) -> Self {
        Self {
            name,
            kind,
            object: Some(object),
            method: property,
            is_property: true,
            confidence: 0.95,
            framework,
        }
    }

    /// Create a function call pattern (no object).
    pub const fn function_call(
        name: &'static str,
        kind: SourceKind,
        function: &'static str,
        confidence: f64,
    ) -> Self {
        Self {
            name,
            kind,
            object: None,
            method: function,
            is_property: false,
            confidence,
            framework: None,
        }
    }
}

// =============================================================================
// Handler Context (for web framework detection)
// =============================================================================

/// Information about a web handler function.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HandlerInfo {
    /// Function name
    pub name: String,
    /// Start line of the function
    pub start_line: usize,
    /// End line of the function
    pub end_line: usize,
    /// Route path (e.g., "/users/<id>")
    pub route: Option<String>,
    /// HTTP methods (GET, POST, etc.)
    pub methods: Vec<String>,
    /// Framework (flask, django, fastapi)
    pub framework: String,
    /// Parameters that are tainted (from URL path, query, body)
    pub tainted_params: Vec<TaintedParameter>,
}

/// A tainted parameter in a handler function.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaintedParameter {
    /// Parameter name
    pub name: String,
    /// Source kind
    pub kind: SourceKind,
    /// Parameter index in function signature
    pub index: usize,
    /// FastAPI annotation type (Body, Query, Path, Header, etc.)
    pub annotation: Option<String>,
}

// =============================================================================
// Scan Results
// =============================================================================

/// Results from scanning a file for taint sources (AST-based).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourceScanResult {
    /// Path to the scanned file
    pub file: String,
    /// Detected taint sources
    pub sources: Vec<DetectedSource>,
    /// Detected handler functions
    pub handlers: Vec<HandlerInfo>,
    /// Language of the file
    pub language: String,
    /// Errors encountered during scanning
    pub errors: Vec<String>,
}

impl SourceScanResult {
    /// Create an empty result for a file.
    pub fn new(file: impl Into<String>, language: impl Into<String>) -> Self {
        Self {
            file: file.into(),
            sources: Vec::new(),
            handlers: Vec::new(),
            language: language.into(),
            errors: Vec::new(),
        }
    }

    /// Add a detected source.
    pub fn add_source(&mut self, source: DetectedSource) {
        self.sources.push(source);
    }

    /// Add a handler.
    pub fn add_handler(&mut self, handler: HandlerInfo) {
        self.handlers.push(handler);
    }

    /// Add an error.
    pub fn add_error(&mut self, error: impl Into<String>) {
        self.errors.push(error.into());
    }

    /// Check if any sources were found.
    pub fn has_sources(&self) -> bool {
        !self.sources.is_empty()
    }

    /// Get sources by kind.
    pub fn sources_by_kind(&self, kind: SourceKind) -> Vec<&DetectedSource> {
        self.sources.iter().filter(|s| s.kind == kind).collect()
    }

    /// Get all sources in handler functions.
    pub fn handler_sources(&self) -> Vec<&DetectedSource> {
        self.sources.iter().filter(|s| s.in_handler).collect()
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_source_kind_to_labels() {
        let labels = SourceKind::RequestParam.to_taint_labels();
        assert!(labels.contains(&TaintLabel::UserInput));

        let labels = SourceKind::Cookie.to_taint_labels();
        assert!(labels.contains(&TaintLabel::Cookie));

        let labels = SourceKind::FileUpload.to_taint_labels();
        assert!(labels.contains(&TaintLabel::UserInput));
        assert!(labels.contains(&TaintLabel::FileContent));
    }

    #[test]
    fn test_source_kind_severity() {
        assert!(SourceKind::RequestParam.severity_weight() > SourceKind::Environment.severity_weight());
        assert!(SourceKind::ProcessArgs.severity_weight() > SourceKind::DatabaseResult.severity_weight());
    }

    #[test]
    fn test_detected_source_builder() {
        let loc = Location::new("test.py", 10, 5);
        let source = DetectedSource::new(SourceKind::RequestParam, loc, "request.args.get('id')")
            .with_assignment("user_id")
            .with_confidence(0.9)
            .with_framework("flask")
            .with_context("id parameter")
            .in_handler_function("get_user");

        assert_eq!(source.kind, SourceKind::RequestParam);
        assert_eq!(source.assigned_to, Some("user_id".to_string()));
        assert!((source.confidence - 0.9).abs() < f64::EPSILON);
        assert_eq!(source.framework, Some("flask".to_string()));
        assert!(source.in_handler);
        assert_eq!(source.enclosing_function, Some("get_user".to_string()));
    }

    #[test]
    fn test_source_pattern() {
        let pattern = SourcePattern::method_call(
            "flask_request_args",
            SourceKind::RequestParam,
            "request",
            "args",
            Some("flask"),
        );

        assert_eq!(pattern.object, Some("request"));
        assert_eq!(pattern.method, "args");
        assert!(!pattern.is_property);

        let prop_pattern = SourcePattern::property_access(
            "django_request_GET",
            SourceKind::RequestParam,
            "request",
            "GET",
            Some("django"),
        );

        assert!(prop_pattern.is_property);
    }

    #[test]
    fn test_scan_result() {
        let mut result = SourceScanResult::new("test.py", "python");

        let loc = Location::new("test.py", 10, 5);
        let source = DetectedSource::new(SourceKind::RequestParam, loc, "request.args");
        result.add_source(source);

        assert!(result.has_sources());
        assert_eq!(result.sources_by_kind(SourceKind::RequestParam).len(), 1);
        assert!(result.sources_by_kind(SourceKind::Cookie).is_empty());
    }

    #[test]
    fn test_handler_info() {
        let handler = HandlerInfo {
            name: "get_user".to_string(),
            start_line: 10,
            end_line: 20,
            route: Some("/users/<int:id>".to_string()),
            methods: vec!["GET".to_string()],
            framework: "flask".to_string(),
            tainted_params: vec![TaintedParameter {
                name: "id".to_string(),
                kind: SourceKind::UrlPath,
                index: 0,
                annotation: None,
            }],
        };

        assert_eq!(handler.name, "get_user");
        assert_eq!(handler.tainted_params.len(), 1);
        assert_eq!(handler.tainted_params[0].kind, SourceKind::UrlPath);
    }

    #[test]
    fn test_registry_reexport() {
        // Test that the pattern-based registry is properly re-exported
        let registry = get_python_sources();
        assert!(!registry.is_empty());

        let matches = registry.find_matches("request.args.get('id')");
        assert!(!matches.is_empty());
    }
}