rma-analyzer 0.19.0

Code analysis and security scanning for Rust Monorepo Analyzer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! Merged knowledge from multiple framework profiles
//!
//! Provides efficient lookup structures for sources, sinks, and sanitizers
//! across all detected frameworks. Optimized for large codebases with
//! lazy initialization and caching.

use super::types::*;
use rma_common::Language;
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};

/// Merged knowledge from all detected framework profiles
///
/// This struct combines knowledge from multiple frameworks into efficient
/// lookup structures. It's designed for production use with:
/// - O(1) pattern matching via hash sets
/// - Lazy compilation of regex patterns
/// - Memory-efficient static string references
#[derive(Debug)]
pub struct MergedKnowledge {
    /// Language this knowledge applies to
    pub language: Language,

    /// Names of active framework profiles
    pub active_frameworks: Vec<&'static str>,

    /// Fast lookup for source function calls
    source_functions: HashSet<&'static str>,

    /// Fast lookup for source member access patterns
    source_members: HashSet<&'static str>,

    /// Fast lookup for type extractors
    source_type_extractors: HashSet<&'static str>,

    /// Method on type patterns (type_pattern -> method)
    source_method_on_type: HashMap<&'static str, Vec<&'static str>>,

    /// Whether parameters are considered sources
    parameters_are_sources: bool,

    /// Full source definitions for detailed info
    all_sources: Vec<&'static SourceDef>,

    /// Fast lookup for sink function calls
    sink_functions: HashSet<&'static str>,

    /// Fast lookup for sink method calls
    sink_methods: HashSet<&'static str>,

    /// Fast lookup for sink macro invocations
    sink_macros: HashSet<&'static str>,

    /// Fast lookup for sink response body patterns
    sink_response_bodies: HashSet<&'static str>,

    /// Full sink definitions for detailed info
    all_sinks: Vec<&'static SinkDef>,

    /// Fast lookup for sanitizer functions
    sanitizer_functions: HashSet<&'static str>,

    /// Fast lookup for sanitizer method calls
    sanitizer_methods: HashSet<&'static str>,

    /// Fast lookup for sanitizer macros
    sanitizer_macros: HashSet<&'static str>,

    /// Sanitizer -> what it sanitizes (e.g., "html", "sql", "shell")
    sanitizer_targets: HashMap<&'static str, &'static str>,

    /// Full sanitizer definitions
    all_sanitizers: Vec<&'static SanitizerDef>,

    /// Safe pattern names for quick reference
    safe_patterns: HashSet<&'static str>,

    /// Full safe pattern definitions
    all_safe_patterns: Vec<&'static SafePattern>,

    /// Dangerous pattern definitions
    all_dangerous_patterns: Vec<&'static DangerousPattern>,

    /// Resource types that need lifecycle management
    all_resource_types: Vec<&'static ResourceType>,
}

impl MergedKnowledge {
    /// Create merged knowledge from a list of framework profiles
    ///
    /// This is the primary constructor used during flow analysis.
    pub fn from_profiles(language: Language, profiles: Vec<&'static FrameworkProfile>) -> Self {
        let mut merged = Self {
            language,
            active_frameworks: Vec::with_capacity(profiles.len()),
            source_functions: HashSet::new(),
            source_members: HashSet::new(),
            source_type_extractors: HashSet::new(),
            source_method_on_type: HashMap::new(),
            parameters_are_sources: false,
            all_sources: Vec::new(),
            sink_functions: HashSet::new(),
            sink_methods: HashSet::new(),
            sink_macros: HashSet::new(),
            sink_response_bodies: HashSet::new(),
            all_sinks: Vec::new(),
            sanitizer_functions: HashSet::new(),
            sanitizer_methods: HashSet::new(),
            sanitizer_macros: HashSet::new(),
            sanitizer_targets: HashMap::new(),
            all_sanitizers: Vec::new(),
            safe_patterns: HashSet::new(),
            all_safe_patterns: Vec::new(),
            all_dangerous_patterns: Vec::new(),
            all_resource_types: Vec::new(),
        };

        for profile in profiles {
            merged.merge_profile(profile);
        }

        merged
    }

    /// Create empty knowledge (for unknown languages)
    pub fn empty(language: Language) -> Self {
        Self {
            language,
            active_frameworks: Vec::new(),
            source_functions: HashSet::new(),
            source_members: HashSet::new(),
            source_type_extractors: HashSet::new(),
            source_method_on_type: HashMap::new(),
            parameters_are_sources: false,
            all_sources: Vec::new(),
            sink_functions: HashSet::new(),
            sink_methods: HashSet::new(),
            sink_macros: HashSet::new(),
            sink_response_bodies: HashSet::new(),
            all_sinks: Vec::new(),
            sanitizer_functions: HashSet::new(),
            sanitizer_methods: HashSet::new(),
            sanitizer_macros: HashSet::new(),
            sanitizer_targets: HashMap::new(),
            all_sanitizers: Vec::new(),
            safe_patterns: HashSet::new(),
            all_safe_patterns: Vec::new(),
            all_dangerous_patterns: Vec::new(),
            all_resource_types: Vec::new(),
        }
    }

    /// Merge a single framework profile into this knowledge base
    fn merge_profile(&mut self, profile: &'static FrameworkProfile) {
        self.active_frameworks.push(profile.name);

        // Merge sources
        for source in profile.sources {
            self.all_sources.push(source);

            match &source.pattern {
                SourceKind::FunctionCall(name) => {
                    self.source_functions.insert(name);
                }
                SourceKind::MemberAccess(path) => {
                    self.source_members.insert(path);
                }
                SourceKind::TypeExtractor(name) => {
                    self.source_type_extractors.insert(name);
                }
                SourceKind::MethodOnType {
                    type_pattern,
                    method,
                } => {
                    self.source_method_on_type
                        .entry(type_pattern)
                        .or_default()
                        .push(method);
                }
                SourceKind::Parameter => {
                    self.parameters_are_sources = true;
                }
            }
        }

        // Merge sinks
        for sink in profile.sinks {
            self.all_sinks.push(sink);

            match &sink.pattern {
                SinkKind::FunctionCall(name) => {
                    self.sink_functions.insert(name);
                }
                SinkKind::MethodCall(name) => {
                    self.sink_methods.insert(name);
                }
                SinkKind::MacroInvocation(name) => {
                    self.sink_macros.insert(name);
                }
                SinkKind::ResponseBody(name) => {
                    self.sink_response_bodies.insert(name);
                }
                SinkKind::PropertyAssignment(_) | SinkKind::TemplateInsertion => {
                    // These require different lookup strategies
                }
            }
        }

        // Merge sanitizers
        for sanitizer in profile.sanitizers {
            self.all_sanitizers.push(sanitizer);

            let key = match &sanitizer.pattern {
                SanitizerKind::Function(name) => {
                    self.sanitizer_functions.insert(name);
                    *name
                }
                SanitizerKind::MethodCall(name) => {
                    self.sanitizer_methods.insert(name);
                    *name
                }
                SanitizerKind::Macro(name) => {
                    self.sanitizer_macros.insert(name);
                    *name
                }
                SanitizerKind::TemplateEngine(name) => {
                    self.sanitizer_functions.insert(name);
                    *name
                }
            };

            self.sanitizer_targets.insert(key, sanitizer.sanitizes);
        }

        // Merge safe patterns
        for pattern in profile.safe_patterns {
            self.safe_patterns.insert(pattern.name);
            self.all_safe_patterns.push(pattern);
        }

        // Merge dangerous patterns
        self.all_dangerous_patterns
            .extend(profile.dangerous_patterns.iter());

        // Merge resource types
        self.all_resource_types
            .extend(profile.resource_types.iter());
    }

    // =========================================================================
    // Source queries (O(1) lookups)
    // =========================================================================

    /// Check if a function call is a taint source
    #[inline]
    pub fn is_source_function(&self, func_name: &str) -> bool {
        // Direct match
        if self.source_functions.contains(func_name) {
            return true;
        }

        // Check if it ends with any known source (e.g., "obj.method")
        for &source in &self.source_functions {
            if func_name.ends_with(source) {
                return true;
            }
        }

        false
    }

    /// Check if a member access is a taint source
    #[inline]
    pub fn is_source_member(&self, member_path: &str) -> bool {
        // Direct match
        if self.source_members.contains(member_path) {
            return true;
        }

        // Substring matching for nested paths
        for &source in &self.source_members {
            if member_path.contains(source) || member_path.ends_with(source) {
                return true;
            }
        }

        false
    }

    /// Check if a type extractor is a taint source
    #[inline]
    pub fn is_source_type_extractor(&self, type_name: &str) -> bool {
        self.source_type_extractors.contains(type_name)
            || self
                .source_type_extractors
                .iter()
                .any(|&t| type_name.contains(t))
    }

    /// Check if parameters should be treated as sources
    #[inline]
    pub fn parameters_are_sources(&self) -> bool {
        self.parameters_are_sources
    }

    /// Get source definition by pattern match
    pub fn get_source(&self, pattern: &str) -> Option<&'static SourceDef> {
        self.all_sources
            .iter()
            .find(|s| match &s.pattern {
                SourceKind::FunctionCall(p) => pattern.contains(*p),
                SourceKind::MemberAccess(p) => pattern.contains(*p),
                SourceKind::TypeExtractor(p) => pattern.contains(*p),
                SourceKind::MethodOnType { method, .. } => pattern.contains(*method),
                SourceKind::Parameter => false,
            })
            .copied()
    }

    // =========================================================================
    // Sink queries (O(1) lookups)
    // =========================================================================

    /// Check if a function call is a sink
    #[inline]
    pub fn is_sink_function(&self, func_name: &str) -> bool {
        if self.sink_functions.contains(func_name) {
            return true;
        }

        for &sink in &self.sink_functions {
            if func_name.ends_with(sink) || func_name.contains(sink) {
                return true;
            }
        }

        false
    }

    /// Check if a method call is a sink
    #[inline]
    pub fn is_sink_method(&self, method_name: &str) -> bool {
        if self.sink_methods.contains(method_name) {
            return true;
        }

        for &sink in &self.sink_methods {
            if method_name.ends_with(sink) {
                return true;
            }
        }

        false
    }

    /// Check if a macro is a sink
    #[inline]
    pub fn is_sink_macro(&self, macro_name: &str) -> bool {
        self.sink_macros.contains(macro_name)
    }

    /// Check if a property assignment is a sink
    pub fn is_sink_property(&self, prop_name: &str) -> bool {
        self.all_sinks.iter().any(|s| match &s.pattern {
            SinkKind::PropertyAssignment(p) => prop_name == *p,
            _ => false,
        })
    }

    /// Get sink definition by pattern match
    pub fn get_sink(&self, pattern: &str) -> Option<&'static SinkDef> {
        self.all_sinks
            .iter()
            .find(|s| match &s.pattern {
                SinkKind::FunctionCall(p) => pattern.contains(*p),
                SinkKind::MethodCall(p) => pattern.contains(*p),
                SinkKind::MacroInvocation(p) => pattern.contains(*p),
                SinkKind::PropertyAssignment(p) => pattern == *p,
                SinkKind::ResponseBody(p) => pattern.contains(*p),
                SinkKind::TemplateInsertion => false,
            })
            .copied()
    }

    // =========================================================================
    // Sanitizer queries (O(1) lookups)
    // =========================================================================

    /// Check if a function is a sanitizer
    #[inline]
    pub fn is_sanitizer(&self, func_name: &str) -> bool {
        // Direct match
        if self.sanitizer_functions.contains(func_name)
            || self.sanitizer_methods.contains(func_name)
            || self.sanitizer_macros.contains(func_name)
        {
            return true;
        }

        // Check with substring matching
        for &sanitizer in &self.sanitizer_functions {
            if func_name.contains(sanitizer) || func_name.ends_with(sanitizer) {
                return true;
            }
        }

        for &sanitizer in &self.sanitizer_methods {
            if func_name.ends_with(sanitizer) {
                return true;
            }
        }

        false
    }

    /// Check if function sanitizes a specific type of taint
    pub fn sanitizes_type(&self, func_name: &str, taint_type: &str) -> bool {
        // Find the sanitizer and check what it sanitizes
        for (&key, &target) in &self.sanitizer_targets {
            if func_name.contains(key) {
                return target == taint_type || target == "*";
            }
        }
        false
    }

    /// Get sanitizer definition
    pub fn get_sanitizer(&self, func_name: &str) -> Option<&'static SanitizerDef> {
        self.all_sanitizers
            .iter()
            .find(|s| match &s.pattern {
                SanitizerKind::Function(p) => func_name.contains(*p),
                SanitizerKind::MethodCall(p) => func_name.contains(*p),
                SanitizerKind::Macro(p) => func_name == *p,
                SanitizerKind::TemplateEngine(p) => func_name.contains(*p),
            })
            .copied()
    }

    // =========================================================================
    // Pattern queries
    // =========================================================================

    /// Check if a pattern is known to be safe
    #[inline]
    pub fn is_safe_pattern(&self, pattern_name: &str) -> bool {
        self.safe_patterns.contains(pattern_name)
    }

    /// Get all dangerous patterns to check
    pub fn dangerous_patterns(&self) -> &[&'static DangerousPattern] {
        &self.all_dangerous_patterns
    }

    /// Get all resource types
    pub fn resource_types(&self) -> &[&'static ResourceType] {
        &self.all_resource_types
    }

    // =========================================================================
    // Statistics and debugging
    // =========================================================================

    /// Get count of registered sources
    pub fn source_count(&self) -> usize {
        self.all_sources.len()
    }

    /// Get count of registered sinks
    pub fn sink_count(&self) -> usize {
        self.all_sinks.len()
    }

    /// Get count of registered sanitizers
    pub fn sanitizer_count(&self) -> usize {
        self.all_sanitizers.len()
    }

    /// Check if any frameworks are active
    pub fn has_frameworks(&self) -> bool {
        !self.active_frameworks.is_empty()
    }

    /// Get sanitizer definitions that match a given sanitize type (e.g., "html", "sql", "shell")
    pub fn sanitizers_for_type(&self, sanitize_type: &str) -> Vec<&'static SanitizerDef> {
        self.all_sanitizers
            .iter()
            .filter(|s| s.sanitizes == sanitize_type || s.sanitizes == "*")
            .copied()
            .collect()
    }

    /// Get all sanitizer definitions
    pub fn all_sanitizer_defs(&self) -> &[&'static SanitizerDef] {
        &self.all_sanitizers
    }

    /// Get all source patterns as strings (for debugging/testing)
    pub fn all_source_patterns(&self) -> Vec<Cow<'static, str>> {
        let mut patterns = Vec::new();

        for &p in &self.source_functions {
            patterns.push(Cow::Borrowed(p));
        }
        for &p in &self.source_members {
            patterns.push(Cow::Borrowed(p));
        }
        for &p in &self.source_type_extractors {
            patterns.push(Cow::Borrowed(p));
        }

        patterns
    }
}

/// Builder for creating MergedKnowledge from source content
pub struct KnowledgeBuilder {
    language: Language,
}

impl KnowledgeBuilder {
    /// Create a new builder for a language
    pub fn new(language: Language) -> Self {
        Self { language }
    }

    /// Build merged knowledge by detecting frameworks from source content
    pub fn from_content(&self, content: &str) -> MergedKnowledge {
        let profiles = super::detect_frameworks(self.language, content);
        MergedKnowledge::from_profiles(self.language, profiles)
    }

    /// Build merged knowledge from a list of import strings
    pub fn from_imports(&self, imports: &[&str]) -> MergedKnowledge {
        let profiles = super::detect_frameworks_from_imports(self.language, imports);
        MergedKnowledge::from_profiles(self.language, profiles)
    }

    /// Build merged knowledge using all known profiles for the language
    pub fn all_profiles(&self) -> MergedKnowledge {
        let profiles = super::profiles_for_language(self.language);
        MergedKnowledge::from_profiles(self.language, profiles)
    }
}

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

    #[test]
    fn test_merged_knowledge_javascript() {
        let builder = KnowledgeBuilder::new(Language::JavaScript);
        let knowledge = builder.all_profiles();

        assert!(knowledge.has_frameworks());
        assert!(knowledge.source_count() > 0);
        assert!(knowledge.sink_count() > 0);
        assert!(knowledge.sanitizer_count() > 0);

        // Test source detection
        assert!(knowledge.is_source_member("req.query"));
        assert!(knowledge.is_source_member("req.body"));
        assert!(knowledge.is_source_member("location.search"));

        // Test sink detection
        assert!(knowledge.is_sink_property("innerHTML"));
        assert!(knowledge.is_sink_function("eval"));

        // Test sanitizer detection
        assert!(knowledge.is_sanitizer("DOMPurify.sanitize"));
        assert!(knowledge.is_sanitizer("encodeURIComponent"));
    }

    #[test]
    fn test_merged_knowledge_rust() {
        let builder = KnowledgeBuilder::new(Language::Rust);
        let knowledge = builder.all_profiles();

        assert!(knowledge.has_frameworks());

        // Test source detection
        assert!(knowledge.is_source_function("env::var"));

        // Test sink detection
        assert!(knowledge.is_sink_function("Command::new"));

        // Test sanitizer detection
        assert!(knowledge.is_sanitizer("ammonia::clean"));
    }

    #[test]
    fn test_framework_detection() {
        let express_code = r#"
import express from 'express';
const app = express();
app.get('/user', (req, res) => {
    const query = req.query.name;
});
"#;

        let builder = KnowledgeBuilder::new(Language::JavaScript);
        let knowledge = builder.from_content(express_code);

        assert!(knowledge.active_frameworks.contains(&"express"));
    }

    #[test]
    fn test_empty_knowledge() {
        let knowledge = MergedKnowledge::empty(Language::Unknown);

        assert!(!knowledge.has_frameworks());
        assert_eq!(knowledge.source_count(), 0);
        assert_eq!(knowledge.sink_count(), 0);
        assert!(!knowledge.is_source_function("anything"));
        assert!(!knowledge.is_sink_function("anything"));
    }
}