synapse-waf 0.9.1

High-performance WAF and reverse proxy with embedded intelligence — built on Cloudflare Pingora
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
//! Thread-safe profile storage with LRU eviction.
//!
//! Provides concurrent access to endpoint profiles using DashMap.
//! Includes dynamic path segment detection for template normalization.
//!
//! ## Memory Budget
//! Default: 10,000 profiles * ~2KB = ~20MB

use std::collections::HashSet;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};

use dashmap::DashMap;
use serde::{Deserialize, Serialize};

use crate::profiler::endpoint_profile::EndpointProfile;

// ============================================================================
// Configuration
// ============================================================================

/// Configuration for profile storage.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfileStoreConfig {
    /// Maximum number of endpoint profiles to store.
    pub max_profiles: usize,
    /// Minimum samples before profile is used for anomaly detection.
    pub min_samples_for_detection: u32,
    /// Profile idle timeout (ms) - profiles not seen for this long are eviction candidates.
    pub idle_timeout_ms: u64,
    /// Enable dynamic path segment detection.
    pub enable_segment_detection: bool,
    /// Cardinality threshold for path segment to be considered dynamic.
    pub dynamic_segment_threshold: usize,
}

impl Default for ProfileStoreConfig {
    fn default() -> Self {
        Self {
            max_profiles: 10_000,
            min_samples_for_detection: 100,
            idle_timeout_ms: 24 * 60 * 60 * 1000, // 24 hours
            enable_segment_detection: true,
            dynamic_segment_threshold: 10,
        }
    }
}

// ============================================================================
// SegmentCardinality - Dynamic path segment detection
// ============================================================================

/// Tracks unique values for each path segment position to detect dynamic segments.
///
/// Example: If position 2 in "/api/users/{id}" sees 100+ unique values,
/// that segment is marked as dynamic (variable).
#[derive(Debug, Default)]
pub struct SegmentCardinality {
    /// Position -> Set of unique values seen
    /// Uses HashSet with capacity limit for memory protection
    segments: DashMap<usize, HashSet<String>>,
    /// Maximum unique values to track per position
    max_values: usize,
}

impl SegmentCardinality {
    /// Create a new cardinality tracker.
    pub fn new(max_values: usize) -> Self {
        Self {
            segments: DashMap::new(),
            max_values,
        }
    }

    /// Record a path segment value at a position.
    /// Returns true if the segment appears to be dynamic (high cardinality).
    pub fn record(&self, position: usize, value: &str, threshold: usize) -> bool {
        let mut entry = self.segments.entry(position).or_insert_with(HashSet::new);
        let values = entry.value_mut();

        // Don't track beyond max_values (memory protection)
        if values.len() < self.max_values {
            values.insert(value.to_string());
        }

        values.len() >= threshold
    }

    /// Check if a position appears dynamic (has high cardinality).
    pub fn is_dynamic(&self, position: usize, threshold: usize) -> bool {
        self.segments
            .get(&position)
            .map(|v| v.len() >= threshold)
            .unwrap_or(false)
    }

    /// Get cardinality at a position.
    pub fn cardinality(&self, position: usize) -> usize {
        self.segments.get(&position).map(|v| v.len()).unwrap_or(0)
    }

    /// Clear all tracked data.
    pub fn clear(&self) {
        self.segments.clear();
    }
}

// ============================================================================
// ProfileStore - Thread-safe storage
// ============================================================================

/// Thread-safe storage for endpoint profiles.
///
/// Uses DashMap for lock-free concurrent access.
pub struct ProfileStore {
    /// Profiles by template path.
    profiles: DashMap<String, EndpointProfile>,
    /// Configuration.
    config: ProfileStoreConfig,
    /// Segment cardinality tracker for dynamic path detection.
    segment_cardinality: SegmentCardinality,
    /// Total profiles created (lifetime).
    total_created: AtomicU64,
    /// Total profiles evicted (lifetime).
    total_evicted: AtomicU64,
    /// Last eviction timestamp (ms).
    last_eviction_ms: AtomicU64,
}

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

impl ProfileStore {
    /// Create a new profile store with configuration.
    pub fn new(config: ProfileStoreConfig) -> Self {
        let max_segment_values = config.dynamic_segment_threshold * 2;
        Self {
            profiles: DashMap::with_capacity(config.max_profiles / 2),
            config,
            segment_cardinality: SegmentCardinality::new(max_segment_values),
            total_created: AtomicU64::new(0),
            total_evicted: AtomicU64::new(0),
            last_eviction_ms: AtomicU64::new(0),
        }
    }

    /// Get configuration.
    pub fn config(&self) -> &ProfileStoreConfig {
        &self.config
    }

    /// Get or create a profile for a path.
    ///
    /// Normalizes the path to a template if dynamic segment detection is enabled.
    pub fn get_or_create(
        &self,
        path: &str,
    ) -> dashmap::mapref::one::RefMut<'_, String, EndpointProfile> {
        let template = if self.config.enable_segment_detection {
            self.normalize_path(path)
        } else {
            path.to_string()
        };

        let now_ms = now_ms();

        // Check capacity and evict if needed
        self.maybe_evict(now_ms);

        self.profiles.entry(template.clone()).or_insert_with(|| {
            self.total_created.fetch_add(1, Ordering::Relaxed);
            EndpointProfile::new(template, now_ms)
        })
    }

    /// Get an existing profile (read-only).
    pub fn get(
        &self,
        template: &str,
    ) -> Option<dashmap::mapref::one::Ref<'_, String, EndpointProfile>> {
        self.profiles.get(template)
    }

    /// Check if a profile exists.
    pub fn contains(&self, template: &str) -> bool {
        self.profiles.contains_key(template)
    }

    /// Get number of stored profiles.
    pub fn len(&self) -> usize {
        self.profiles.len()
    }

    /// Check if store is empty.
    pub fn is_empty(&self) -> bool {
        self.profiles.is_empty()
    }

    /// Normalize a path to a template by replacing dynamic segments.
    ///
    /// Example: "/api/users/12345/orders" -> "/api/users/{id}/orders"
    fn normalize_path(&self, path: &str) -> String {
        let segments: Vec<&str> = path.split('/').collect();
        let threshold = self.config.dynamic_segment_threshold;

        let normalized: Vec<String> = segments
            .iter()
            .enumerate()
            .map(|(pos, segment)| {
                if segment.is_empty() {
                    return String::new();
                }

                // Check if this looks like an ID (numeric, UUID-like, etc.)
                let looks_dynamic = Self::looks_like_id(segment);

                // Record cardinality
                let is_high_cardinality = self.segment_cardinality.record(pos, segment, threshold);

                if looks_dynamic || is_high_cardinality {
                    "{id}".to_string()
                } else {
                    segment.to_string()
                }
            })
            .collect();

        normalized.join("/")
    }

    /// Check if a segment looks like an ID (numeric, UUID, hex hash, etc.).
    fn looks_like_id(segment: &str) -> bool {
        // Empty segments are not IDs
        if segment.is_empty() {
            return false;
        }

        // Pure numeric (user IDs, etc.)
        if segment.chars().all(|c| c.is_ascii_digit()) {
            return !segment.is_empty() && segment.len() <= 20; // Reasonable ID length
        }

        // UUID format: 8-4-4-4-12 hex
        if segment.len() == 36 && segment.chars().all(|c| c.is_ascii_hexdigit() || c == '-') {
            return true;
        }

        // Hex string (16+ chars, common for hashes/tokens)
        if segment.len() >= 16 && segment.chars().all(|c| c.is_ascii_hexdigit()) {
            return true;
        }

        // MongoDB ObjectId (24 hex chars)
        if segment.len() == 24 && segment.chars().all(|c| c.is_ascii_hexdigit()) {
            return true;
        }

        false
    }

    /// Evict profiles if at capacity.
    fn maybe_evict(&self, now_ms: u64) {
        // Only check eviction periodically (every 1000ms)
        let last = self.last_eviction_ms.load(Ordering::Relaxed);
        if now_ms.saturating_sub(last) < 1000 {
            return;
        }

        if self.profiles.len() < self.config.max_profiles {
            return;
        }

        self.last_eviction_ms.store(now_ms, Ordering::Relaxed);
        self.evict_stale(now_ms);
    }

    /// Evict stale profiles (not seen within idle timeout).
    fn evict_stale(&self, now_ms: u64) {
        let idle_timeout = self.config.idle_timeout_ms;
        let cutoff = now_ms.saturating_sub(idle_timeout);

        // Collect keys to remove (to avoid holding refs during iteration)
        let stale_keys: Vec<String> = self
            .profiles
            .iter()
            .filter(|entry| entry.value().last_updated_ms < cutoff)
            .map(|entry| entry.key().clone())
            .take(100) // Batch size
            .collect();

        for key in stale_keys {
            if self.profiles.remove(&key).is_some() {
                self.total_evicted.fetch_add(1, Ordering::Relaxed);
            }
        }
    }

    /// Clear all profiles.
    pub fn clear(&self) {
        self.profiles.clear();
        self.segment_cardinality.clear();
    }

    /// Get store metrics.
    pub fn metrics(&self) -> ProfileStoreMetrics {
        ProfileStoreMetrics {
            current_profiles: self.profiles.len(),
            max_profiles: self.config.max_profiles,
            total_created: self.total_created.load(Ordering::Relaxed),
            total_evicted: self.total_evicted.load(Ordering::Relaxed),
        }
    }

    /// List all profile templates.
    pub fn list_templates(&self) -> Vec<String> {
        self.profiles.iter().map(|e| e.key().clone()).collect()
    }

    /// Get all profiles as a snapshot.
    ///
    /// Returns a clone of all profiles currently in storage.
    /// Useful for persistence and state export.
    pub fn get_profiles(&self) -> Vec<EndpointProfile> {
        self.profiles.iter().map(|e| e.value().clone()).collect()
    }

    /// Get mature profiles (those with enough samples for detection).
    pub fn mature_profiles(&self) -> Vec<String> {
        let min = self.config.min_samples_for_detection;
        self.profiles
            .iter()
            .filter(|e| e.value().is_mature(min))
            .map(|e| e.key().clone())
            .collect()
    }
}

/// Profile store metrics.
#[derive(Debug, Clone, Serialize)]
pub struct ProfileStoreMetrics {
    pub current_profiles: usize,
    pub max_profiles: usize,
    pub total_created: u64,
    pub total_evicted: u64,
}

/// Get current time in milliseconds.
#[inline]
fn now_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0)
}

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

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

    #[test]
    fn test_segment_cardinality_basic() {
        let sc = SegmentCardinality::new(100);

        // Add values to position 0
        for i in 0..5 {
            sc.record(0, &format!("value_{}", i), 10);
        }

        assert_eq!(sc.cardinality(0), 5);
        assert!(!sc.is_dynamic(0, 10));
    }

    #[test]
    fn test_segment_cardinality_threshold() {
        let sc = SegmentCardinality::new(100);

        // Add 10 values
        for i in 0..10 {
            let is_dynamic = sc.record(0, &format!("value_{}", i), 10);
            if i < 9 {
                assert!(!is_dynamic);
            } else {
                assert!(is_dynamic);
            }
        }

        assert!(sc.is_dynamic(0, 10));
    }

    #[test]
    fn test_profile_store_basic() {
        let store = ProfileStore::default();

        {
            let mut profile = store.get_or_create("/api/users");
            profile.update(100, &[("name", "John")], Some("application/json"), now_ms());
        }

        assert_eq!(store.len(), 1);
        assert!(store.contains("/api/users"));
    }

    #[test]
    fn test_profile_store_path_normalization() {
        let config = ProfileStoreConfig {
            enable_segment_detection: true,
            dynamic_segment_threshold: 2,
            ..Default::default()
        };
        let store = ProfileStore::new(config);

        // Access paths with numeric IDs
        store.get_or_create("/api/users/123/orders");
        store.get_or_create("/api/users/456/orders");

        // Both should normalize to the same template
        assert_eq!(store.len(), 1);

        let templates = store.list_templates();
        assert!(templates[0].contains("{id}"));
    }

    #[test]
    fn test_looks_like_id() {
        // Numeric IDs
        assert!(ProfileStore::looks_like_id("123"));
        assert!(ProfileStore::looks_like_id("12345678901234567890"));
        assert!(!ProfileStore::looks_like_id("123456789012345678901")); // Too long

        // UUIDs
        assert!(ProfileStore::looks_like_id(
            "550e8400-e29b-41d4-a716-446655440000"
        ));

        // Hex hashes
        assert!(ProfileStore::looks_like_id("abcdef1234567890"));
        assert!(!ProfileStore::looks_like_id("abcdef12345")); // Too short

        // MongoDB ObjectId
        assert!(ProfileStore::looks_like_id("507f1f77bcf86cd799439011"));

        // Non-IDs
        assert!(!ProfileStore::looks_like_id("users"));
        assert!(!ProfileStore::looks_like_id("api"));
        assert!(!ProfileStore::looks_like_id(""));
    }

    #[test]
    fn test_profile_store_without_normalization() {
        let config = ProfileStoreConfig {
            enable_segment_detection: false,
            ..Default::default()
        };
        let store = ProfileStore::new(config);

        store.get_or_create("/api/users/123");
        store.get_or_create("/api/users/456");

        // Without normalization, these should be separate
        assert_eq!(store.len(), 2);
    }

    #[test]
    fn test_profile_store_metrics() {
        let store = ProfileStore::default();

        for i in 0..5 {
            store.get_or_create(&format!("/api/endpoint_{}", i));
        }

        let metrics = store.metrics();
        assert_eq!(metrics.current_profiles, 5);
        assert_eq!(metrics.total_created, 5);
        assert_eq!(metrics.total_evicted, 0);
    }

    #[test]
    fn test_profile_store_clear() {
        let store = ProfileStore::default();

        for i in 0..5 {
            store.get_or_create(&format!("/api/endpoint_{}", i));
        }
        assert_eq!(store.len(), 5);

        store.clear();
        assert!(store.is_empty());
    }

    #[test]
    fn test_profile_store_mature_profiles() {
        let config = ProfileStoreConfig {
            min_samples_for_detection: 10,
            enable_segment_detection: false,
            ..Default::default()
        };
        let store = ProfileStore::new(config);

        // Create one mature and one immature profile
        {
            let mut p1 = store.get_or_create("/api/mature");
            for _ in 0..15 {
                p1.update(100, &[], None, now_ms());
            }
        }
        {
            let mut p2 = store.get_or_create("/api/immature");
            for _ in 0..5 {
                p2.update(100, &[], None, now_ms());
            }
        }

        let mature = store.mature_profiles();
        assert_eq!(mature.len(), 1);
        assert!(mature.contains(&"/api/mature".to_string()));
    }

    #[test]
    fn test_segment_cardinality_clear() {
        let sc = SegmentCardinality::new(100);

        for i in 0..10 {
            sc.record(0, &format!("value_{}", i), 20);
        }
        assert_eq!(sc.cardinality(0), 10);

        sc.clear();
        assert_eq!(sc.cardinality(0), 0);
    }

    #[test]
    fn test_segment_cardinality_max_values() {
        let sc = SegmentCardinality::new(5); // Max 5 values

        // Try to add 10 values
        for i in 0..10 {
            sc.record(0, &format!("value_{}", i), 100);
        }

        // Should cap at 5
        assert_eq!(sc.cardinality(0), 5);
    }
}