lonkero 3.6.2

Web scanner built for actual pentests. Fast, modular, Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
// Copyright (c) 2026 Bountyy Oy. All rights reserved.
// This software is proprietary and confidential.

use chrono::{DateTime, Utc};
use regex::Regex;
/**
 * High-Performance Rule Engine
 * Rust implementation for fast rule evaluation at scale
 *
 * Features:
 * - JSON-based rule definitions
 * - Rule validation
 * - Parallel evaluation
 * - Rule caching
 * - Performance optimization
 *
 * © 2026 Bountyy Oy
 */
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};

/// Rule operator types
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "UPPERCASE")]
pub enum LogicalOperator {
    And,
    Or,
    Not,
}

/// Comparison operator types
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ComparisonOperator {
    Equals,
    NotEquals,
    Contains,
    NotContains,
    StartsWith,
    EndsWith,
    Regex,
    Gt,
    Gte,
    Lt,
    Lte,
    In,
    NotIn,
    Exists,
    NotExists,
    Between,
    HasLabel,
    NotHasLabel,
}

/// Single condition in a rule
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Condition {
    Simple {
        field: String,
        operator: ComparisonOperator,
        value: Value,
        #[serde(default)]
        case_sensitive: bool,
    },
    Nested {
        operator: LogicalOperator,
        conditions: Vec<Condition>,
    },
}

/// Rule definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Rule {
    pub operator: LogicalOperator,
    pub conditions: Vec<Condition>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<RuleMetadata>,
}

/// Rule metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RuleMetadata {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
}

/// Asset representation for evaluation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Asset {
    pub id: i64,
    #[serde(rename = "asset_type")]
    pub type_: String,
    #[serde(rename = "asset_value")]
    pub value: String,
    pub status: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cloud_provider: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cloud_region: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub risk_score: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub discovered_at: Option<DateTime<Utc>>,
}

/// Rule evaluation result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvaluationResult {
    pub matches: bool,
    pub evaluated_at: DateTime<Utc>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<String>,
}

/// Cached evaluation result
struct CachedResult {
    result: bool,
    timestamp: DateTime<Utc>,
}

/// High-performance rule engine
pub struct RuleEngine {
    /// Cache for evaluation results
    cache: Arc<RwLock<HashMap<String, CachedResult>>>,
    /// Cache TTL in seconds
    cache_ttl: i64,
    /// Compiled regex cache
    regex_cache: Arc<RwLock<HashMap<String, Regex>>>,
    /// Performance metrics
    metrics: Arc<RwLock<EngineMetrics>>,
}

/// Engine performance metrics
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct EngineMetrics {
    pub total_evaluations: u64,
    pub cache_hits: u64,
    pub cache_misses: u64,
    pub avg_evaluation_time_ms: f64,
    pub rules_validated: u64,
    pub validation_errors: u64,
}

impl RuleEngine {
    /// Create a new rule engine
    pub fn new(cache_ttl_seconds: i64) -> Self {
        Self {
            cache: Arc::new(RwLock::new(HashMap::new())),
            cache_ttl: cache_ttl_seconds,
            regex_cache: Arc::new(RwLock::new(HashMap::new())),
            metrics: Arc::new(RwLock::new(EngineMetrics::default())),
        }
    }

    /// Validate a rule
    pub fn validate_rule(&self, rule: &Rule) -> Result<(), String> {
        let mut metrics = self
            .metrics
            .write()
            .map_err(|e| format!("Failed to acquire metrics lock: {}", e))?;
        metrics.rules_validated += 1;

        // Check if rule has at least one condition
        if rule.conditions.is_empty() {
            metrics.validation_errors += 1;
            return Err("Rule must have at least one condition".to_string());
        }

        // Validate each condition
        for condition in &rule.conditions {
            if let Err(e) = self.validate_condition(condition) {
                metrics.validation_errors += 1;
                return Err(e);
            }
        }

        Ok(())
    }

    /// Validate a condition
    fn validate_condition(&self, condition: &Condition) -> Result<(), String> {
        match condition {
            Condition::Simple {
                field,
                operator,
                value,
                ..
            } => {
                // Field must not be empty
                if field.is_empty() {
                    return Err("Field name cannot be empty".to_string());
                }

                // Validate value type based on operator
                match operator {
                    ComparisonOperator::In | ComparisonOperator::NotIn => {
                        if !value.is_array() {
                            return Err(format!("Operator {:?} requires array value", operator));
                        }
                    }
                    ComparisonOperator::Between => {
                        if !value.is_array() {
                            return Err("Between operator requires array value".to_string());
                        }
                        if let Some(arr) = value.as_array() {
                            if arr.len() != 2 {
                                return Err(
                                    "Between operator requires array with exactly 2 values"
                                        .to_string(),
                                );
                            }
                        }
                    }
                    ComparisonOperator::Regex => {
                        if let Some(pattern) = value.as_str() {
                            // Validate regex pattern
                            if Regex::new(pattern).is_err() {
                                return Err(format!("Invalid regex pattern: {}", pattern));
                            }
                        } else {
                            return Err("Regex operator requires string value".to_string());
                        }
                    }
                    _ => {}
                }

                Ok(())
            }
            Condition::Nested { conditions, .. } => {
                if conditions.is_empty() {
                    return Err("Nested condition must have at least one sub-condition".to_string());
                }

                for cond in conditions {
                    self.validate_condition(cond)?;
                }

                Ok(())
            }
        }
    }

    /// Evaluate rule against asset
    pub fn evaluate(&self, rule: &Rule, asset: &Asset) -> Result<EvaluationResult, String> {
        let start = std::time::Instant::now();

        // Update metrics
        {
            let mut metrics = self
                .metrics
                .write()
                .map_err(|e| format!("Failed to acquire metrics lock: {}", e))?;
            metrics.total_evaluations += 1;
        }

        // Check cache
        let cache_key = self.make_cache_key(rule, asset)?;
        if let Some(cached) = self.get_cached(&cache_key)? {
            let mut metrics = self
                .metrics
                .write()
                .map_err(|e| format!("Failed to acquire metrics lock: {}", e))?;
            metrics.cache_hits += 1;

            return Ok(EvaluationResult {
                matches: cached,
                evaluated_at: Utc::now(),
                details: Some("From cache".to_string()),
            });
        }

        // Update cache miss
        {
            let mut metrics = self
                .metrics
                .write()
                .map_err(|e| format!("Failed to acquire metrics lock: {}", e))?;
            metrics.cache_misses += 1;
        }

        // Evaluate
        let matches = self.evaluate_conditions(&rule.operator, &rule.conditions, asset)?;

        // Cache result
        self.cache_result(&cache_key, matches)?;

        // Update metrics
        let duration = start.elapsed();
        {
            let mut metrics = self
                .metrics
                .write()
                .map_err(|e| format!("Failed to acquire metrics lock: {}", e))?;
            let total = metrics.total_evaluations as f64;
            metrics.avg_evaluation_time_ms = (metrics.avg_evaluation_time_ms * (total - 1.0)
                + duration.as_millis() as f64)
                / total;
        }

        Ok(EvaluationResult {
            matches,
            evaluated_at: Utc::now(),
            details: None,
        })
    }

    /// Evaluate conditions with logical operator
    fn evaluate_conditions(
        &self,
        operator: &LogicalOperator,
        conditions: &[Condition],
        asset: &Asset,
    ) -> Result<bool, String> {
        match operator {
            LogicalOperator::And => {
                for condition in conditions {
                    if !self.evaluate_condition(condition, asset)? {
                        return Ok(false);
                    }
                }
                Ok(true)
            }
            LogicalOperator::Or => {
                for condition in conditions {
                    if self.evaluate_condition(condition, asset)? {
                        return Ok(true);
                    }
                }
                Ok(false)
            }
            LogicalOperator::Not => {
                for condition in conditions {
                    if self.evaluate_condition(condition, asset)? {
                        return Ok(false);
                    }
                }
                Ok(true)
            }
        }
    }

    /// Evaluate single condition
    fn evaluate_condition(&self, condition: &Condition, asset: &Asset) -> Result<bool, String> {
        match condition {
            Condition::Nested {
                operator,
                conditions,
            } => self.evaluate_conditions(operator, conditions, asset),
            Condition::Simple {
                field,
                operator,
                value,
                case_sensitive,
            } => {
                let field_value = self.get_field_value(asset, field);
                self.compare(&field_value, operator, value, *case_sensitive)
            }
        }
    }

    /// Get field value from asset (supports dot notation)
    fn get_field_value(&self, asset: &Asset, field: &str) -> Value {
        let parts: Vec<&str> = field.split('.').collect();

        // Handle direct fields
        if parts.len() == 1 {
            return match parts[0] {
                "id" => Value::Number(asset.id.into()),
                "type" | "asset_type" => Value::String(asset.type_.clone()),
                "value" | "asset_value" => Value::String(asset.value.clone()),
                "status" => Value::String(asset.status.clone()),
                "tags" => asset
                    .tags
                    .as_ref()
                    .and_then(|t| serde_json::to_value(t).ok())
                    .unwrap_or(Value::Null),
                "cloud_provider" => asset
                    .cloud_provider
                    .as_ref()
                    .map(|s| Value::String(s.clone()))
                    .unwrap_or(Value::Null),
                "cloud_region" => asset
                    .cloud_region
                    .as_ref()
                    .map(|s| Value::String(s.clone()))
                    .unwrap_or(Value::Null),
                "risk_score" => asset
                    .risk_score
                    .and_then(|r| serde_json::Number::from_f64(r))
                    .map(Value::Number)
                    .unwrap_or(Value::Null),
                "metadata" => asset.metadata.clone().unwrap_or(Value::Null),
                _ => Value::Null,
            };
        }

        // Handle nested fields (e.g., metadata.provider)
        if parts[0] == "metadata" {
            if let Some(metadata) = &asset.metadata {
                let mut current = metadata.clone();
                for part in &parts[1..] {
                    if let Some(obj) = current.as_object() {
                        current = obj.get(*part).cloned().unwrap_or(Value::Null);
                    } else {
                        return Value::Null;
                    }
                }
                return current;
            }
        }

        Value::Null
    }

    /// Compare values using operator
    fn compare(
        &self,
        field_value: &Value,
        operator: &ComparisonOperator,
        compare_value: &Value,
        case_sensitive: bool,
    ) -> Result<bool, String> {
        match operator {
            ComparisonOperator::Equals => {
                Ok(self.values_equal(field_value, compare_value, case_sensitive))
            }
            ComparisonOperator::NotEquals => {
                Ok(!self.values_equal(field_value, compare_value, case_sensitive))
            }
            ComparisonOperator::Contains => {
                if let (Some(haystack), Some(needle)) =
                    (field_value.as_str(), compare_value.as_str())
                {
                    if case_sensitive {
                        Ok(haystack.contains(needle))
                    } else {
                        Ok(haystack.to_lowercase().contains(&needle.to_lowercase()))
                    }
                } else {
                    Ok(false)
                }
            }
            ComparisonOperator::NotContains => {
                if let (Some(haystack), Some(needle)) =
                    (field_value.as_str(), compare_value.as_str())
                {
                    if case_sensitive {
                        Ok(!haystack.contains(needle))
                    } else {
                        Ok(!haystack.to_lowercase().contains(&needle.to_lowercase()))
                    }
                } else {
                    Ok(true)
                }
            }
            ComparisonOperator::StartsWith => {
                if let (Some(haystack), Some(prefix)) =
                    (field_value.as_str(), compare_value.as_str())
                {
                    if case_sensitive {
                        Ok(haystack.starts_with(prefix))
                    } else {
                        Ok(haystack.to_lowercase().starts_with(&prefix.to_lowercase()))
                    }
                } else {
                    Ok(false)
                }
            }
            ComparisonOperator::EndsWith => {
                if let (Some(haystack), Some(suffix)) =
                    (field_value.as_str(), compare_value.as_str())
                {
                    if case_sensitive {
                        Ok(haystack.ends_with(suffix))
                    } else {
                        Ok(haystack.to_lowercase().ends_with(&suffix.to_lowercase()))
                    }
                } else {
                    Ok(false)
                }
            }
            ComparisonOperator::Regex => {
                if let (Some(text), Some(pattern)) = (field_value.as_str(), compare_value.as_str())
                {
                    let regex = self.get_or_compile_regex(pattern, case_sensitive)?;
                    Ok(regex.is_match(text))
                } else {
                    Ok(false)
                }
            }
            ComparisonOperator::Gt => {
                Ok(self.numeric_compare(field_value, compare_value, |a, b| a > b))
            }
            ComparisonOperator::Gte => {
                Ok(self.numeric_compare(field_value, compare_value, |a, b| a >= b))
            }
            ComparisonOperator::Lt => {
                Ok(self.numeric_compare(field_value, compare_value, |a, b| a < b))
            }
            ComparisonOperator::Lte => {
                Ok(self.numeric_compare(field_value, compare_value, |a, b| a <= b))
            }
            ComparisonOperator::In => {
                if let Some(arr) = compare_value.as_array() {
                    Ok(arr
                        .iter()
                        .any(|v| self.values_equal(field_value, v, case_sensitive)))
                } else {
                    Ok(false)
                }
            }
            ComparisonOperator::NotIn => {
                if let Some(arr) = compare_value.as_array() {
                    Ok(!arr
                        .iter()
                        .any(|v| self.values_equal(field_value, v, case_sensitive)))
                } else {
                    Ok(true)
                }
            }
            ComparisonOperator::Exists => Ok(!field_value.is_null()),
            ComparisonOperator::NotExists => Ok(field_value.is_null()),
            ComparisonOperator::Between => {
                if let Some(arr) = compare_value.as_array() {
                    if arr.len() == 2 {
                        let gte = self.numeric_compare(field_value, &arr[0], |a, b| a >= b);
                        let lte = self.numeric_compare(field_value, &arr[1], |a, b| a <= b);
                        return Ok(gte && lte);
                    }
                }
                Ok(false)
            }
            ComparisonOperator::HasLabel => {
                if let (Some(tags), Some(label)) = (field_value.as_array(), compare_value.as_str())
                {
                    Ok(tags.iter().any(|t| t.as_str() == Some(label)))
                } else {
                    Ok(false)
                }
            }
            ComparisonOperator::NotHasLabel => {
                if let (Some(tags), Some(label)) = (field_value.as_array(), compare_value.as_str())
                {
                    Ok(!tags.iter().any(|t| t.as_str() == Some(label)))
                } else {
                    Ok(true)
                }
            }
        }
    }

    /// Check if two values are equal
    fn values_equal(&self, a: &Value, b: &Value, case_sensitive: bool) -> bool {
        if !case_sensitive {
            if let (Some(a_str), Some(b_str)) = (a.as_str(), b.as_str()) {
                return a_str.to_lowercase() == b_str.to_lowercase();
            }
        }
        a == b
    }

    /// Numeric comparison
    fn numeric_compare<F>(&self, a: &Value, b: &Value, op: F) -> bool
    where
        F: Fn(f64, f64) -> bool,
    {
        match (a.as_f64(), b.as_f64()) {
            (Some(a_num), Some(b_num)) => op(a_num, b_num),
            _ => false,
        }
    }

    /// Get or compile regex
    fn get_or_compile_regex(&self, pattern: &str, case_sensitive: bool) -> Result<Regex, String> {
        let cache_key = if case_sensitive {
            format!("cs:{}", pattern)
        } else {
            format!("ci:{}", pattern)
        };

        // Check cache
        {
            let cache = self
                .regex_cache
                .read()
                .map_err(|e| format!("Failed to acquire regex cache lock: {}", e))?;
            if let Some(regex) = cache.get(&cache_key) {
                return Ok(regex.clone());
            }
        }

        // Compile and cache
        let regex = if case_sensitive {
            Regex::new(pattern)
        } else {
            Regex::new(&format!("(?i){}", pattern))
        };

        match regex {
            Ok(r) => {
                let mut cache = self
                    .regex_cache
                    .write()
                    .map_err(|e| format!("Failed to acquire regex cache lock: {}", e))?;
                cache.insert(cache_key, r.clone());
                Ok(r)
            }
            Err(e) => Err(format!("Invalid regex: {}", e)),
        }
    }

    /// Make cache key
    fn make_cache_key(&self, rule: &Rule, asset: &Asset) -> Result<String, String> {
        let rule_str = serde_json::to_string(rule)
            .map_err(|e| format!("Failed to serialize rule for cache key: {}", e))?;
        Ok(format!("{}:{}", rule_str, asset.id))
    }

    /// Get cached result
    fn get_cached(&self, key: &str) -> Result<Option<bool>, String> {
        let cache = self
            .cache
            .read()
            .map_err(|e| format!("Failed to acquire cache lock: {}", e))?;
        if let Some(cached) = cache.get(key) {
            let age = Utc::now() - cached.timestamp;
            if age.num_seconds() < self.cache_ttl {
                return Ok(Some(cached.result));
            }
        }
        Ok(None)
    }

    /// Cache result
    fn cache_result(&self, key: &str, result: bool) -> Result<(), String> {
        let mut cache = self
            .cache
            .write()
            .map_err(|e| format!("Failed to acquire cache lock: {}", e))?;
        cache.insert(
            key.to_string(),
            CachedResult {
                result,
                timestamp: Utc::now(),
            },
        );
        Ok(())
    }

    /// Clear cache
    pub fn clear_cache(&self) -> Result<(), String> {
        let mut cache = self
            .cache
            .write()
            .map_err(|e| format!("Failed to acquire cache lock: {}", e))?;
        cache.clear();
        Ok(())
    }

    /// Get metrics
    pub fn get_metrics(&self) -> Result<EngineMetrics, String> {
        let metrics = self
            .metrics
            .read()
            .map_err(|e| format!("Failed to acquire metrics lock: {}", e))?;
        Ok(metrics.clone())
    }

    /// Reset metrics
    pub fn reset_metrics(&self) -> Result<(), String> {
        let mut metrics = self
            .metrics
            .write()
            .map_err(|e| format!("Failed to acquire metrics lock: {}", e))?;
        *metrics = EngineMetrics::default();
        Ok(())
    }
}

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

    fn create_test_asset() -> Asset {
        Asset {
            id: 1,
            type_: "subdomain".to_string(),
            value: "api.example.com".to_string(),
            status: "active".to_string(),
            tags: Some(vec!["production".to_string(), "critical".to_string()]),
            metadata: Some(serde_json::json!({
                "provider": "aws",
                "region": "us-east-1"
            })),
            cloud_provider: Some("aws".to_string()),
            cloud_region: Some("us-east-1".to_string()),
            risk_score: Some(8.5),
            discovered_at: Some(Utc::now()),
        }
    }

    #[test]
    fn test_simple_equals() {
        let engine = RuleEngine::new(300);
        let asset = create_test_asset();

        let rule = Rule {
            operator: LogicalOperator::And,
            conditions: vec![Condition::Simple {
                field: "type".to_string(),
                operator: ComparisonOperator::Equals,
                value: Value::String("subdomain".to_string()),
                case_sensitive: true,
            }],
            metadata: None,
        };

        let result = engine.evaluate(&rule, &asset).unwrap();
        assert!(result.matches);
    }

    #[test]
    fn test_contains() {
        let engine = RuleEngine::new(300);
        let asset = create_test_asset();

        let rule = Rule {
            operator: LogicalOperator::And,
            conditions: vec![Condition::Simple {
                field: "value".to_string(),
                operator: ComparisonOperator::Contains,
                value: Value::String("api".to_string()),
                case_sensitive: false,
            }],
            metadata: None,
        };

        let result = engine.evaluate(&rule, &asset).unwrap();
        assert!(result.matches);
    }

    #[test]
    fn test_nested_metadata() {
        let engine = RuleEngine::new(300);
        let asset = create_test_asset();

        let rule = Rule {
            operator: LogicalOperator::And,
            conditions: vec![Condition::Simple {
                field: "metadata.provider".to_string(),
                operator: ComparisonOperator::Equals,
                value: Value::String("aws".to_string()),
                case_sensitive: true,
            }],
            metadata: None,
        };

        let result = engine.evaluate(&rule, &asset).unwrap();
        assert!(result.matches);
    }

    #[test]
    fn test_and_operator() {
        let engine = RuleEngine::new(300);
        let asset = create_test_asset();

        let rule = Rule {
            operator: LogicalOperator::And,
            conditions: vec![
                Condition::Simple {
                    field: "status".to_string(),
                    operator: ComparisonOperator::Equals,
                    value: Value::String("active".to_string()),
                    case_sensitive: true,
                },
                Condition::Simple {
                    field: "risk_score".to_string(),
                    operator: ComparisonOperator::Gt,
                    value: Value::Number(serde_json::Number::from_f64(7.0).unwrap()),
                    case_sensitive: true,
                },
            ],
            metadata: None,
        };

        let result = engine.evaluate(&rule, &asset).unwrap();
        assert!(result.matches);
    }
}