safebrowsing 0.1.0

Rust implementation of Google Safe Browsing Update API (v4)
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
//! Caching for Safe Browsing API responses
//!
//! This module provides TTL-based caching for API responses to reduce network calls
//! and improve performance.

use safebrowsing_api::{ThreatDescriptor, URLThreat};
use safebrowsing_hash::HashPrefix;
use std::collections::HashMap;
use std::time::{Duration, Instant};

/// Cache result types
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CacheResult {
    /// Found a positive match in cache
    PositiveHit,
    /// Found a negative cache entry (known safe)
    NegativeHit,
    /// No cache entry found
    Miss,
}

/// Cache entry for threat matches
#[derive(Debug, Clone)]
struct ThreatCacheEntry {
    threats: HashMap<ThreatDescriptor, Instant>,
}

impl ThreatCacheEntry {
    fn new() -> Self {
        Self {
            threats: HashMap::new(),
        }
    }

    fn insert(&mut self, threat: ThreatDescriptor, expiry: Instant) {
        self.threats.insert(threat, expiry);
    }

    fn get_valid_threats(&self, now: Instant) -> Vec<ThreatDescriptor> {
        self.threats
            .iter()
            .filter(|(_, &expiry)| expiry > now)
            .map(|(threat, _)| threat.clone())
            .collect()
    }

    fn cleanup_expired(&mut self, now: Instant) {
        self.threats.retain(|_, &mut expiry| expiry > now);
    }

    fn is_empty(&self) -> bool {
        self.threats.is_empty()
    }
}

/// TTL-based cache for Safe Browsing responses
pub struct Cache {
    // Positive cache: full hash -> threat descriptors with TTL
    positive_cache: HashMap<HashPrefix, ThreatCacheEntry>,

    // Negative cache: hash prefix -> expiry time
    negative_cache: HashMap<HashPrefix, Instant>,

    // Statistics
    hits: u64,
    misses: u64,

    // Last cleanup time
    last_cleanup: Instant,

    // Cleanup interval
    cleanup_interval: Duration,
}

impl Cache {
    /// Create a new cache
    pub fn new() -> Self {
        Self {
            positive_cache: HashMap::new(),
            negative_cache: HashMap::new(),
            hits: 0,
            misses: 0,
            last_cleanup: Instant::now(),
            cleanup_interval: Duration::from_secs(300), // 5 minutes
        }
    }

    /// Create a cache with custom cleanup interval
    pub fn with_cleanup_interval(cleanup_interval: Duration) -> Self {
        Self {
            positive_cache: HashMap::new(),
            negative_cache: HashMap::new(),
            hits: 0,
            misses: 0,
            last_cleanup: Instant::now(),
            cleanup_interval,
        }
    }

    /// Look up a hash in the cache
    pub fn lookup(&mut self, hash: &HashPrefix) -> Option<Vec<URLThreat>> {
        if !hash.is_full_hash() {
            return None;
        }

        let now = Instant::now();
        self.maybe_cleanup(now);

        // Check positive cache first
        if let Some(entry) = self.positive_cache.get_mut(hash) {
            let valid_threats = entry.get_valid_threats(now);
            if !valid_threats.is_empty() {
                self.hits += 1;
                let url_threats = valid_threats
                    .into_iter()
                    .map(|threat| URLThreat {
                        pattern: String::new(),
                        threat_descriptor: threat,
                    })
                    .collect();
                return Some(url_threats);
            }

            // Clean up expired entries
            entry.cleanup_expired(now);
            if entry.is_empty() {
                self.positive_cache.remove(hash);
            }
        }

        // Check negative cache for all possible prefixes
        for prefix_len in 4..=hash.len() {
            if let Ok(prefix) = hash.truncate(prefix_len) {
                if let Some(&expiry) = self.negative_cache.get(&prefix) {
                    if expiry > now {
                        self.hits += 1;
                        return Some(Vec::new()); // Empty = safe
                    } else {
                        // Remove expired entry
                        self.negative_cache.remove(&prefix);
                    }
                }
            }
        }

        self.misses += 1;
        None
    }

    /// Insert positive cache entries (threats found)
    pub fn insert_positive(
        &mut self,
        hash: HashPrefix,
        threats: Vec<ThreatDescriptor>,
        ttl: Duration,
    ) {
        if !hash.is_full_hash() {
            return;
        }

        let expiry = Instant::now() + ttl;
        let entry = self
            .positive_cache
            .entry(hash)
            .or_insert_with(ThreatCacheEntry::new);

        for threat in threats {
            entry.insert(threat, expiry);
        }
    }

    /// Insert negative cache entry (no threats found)
    pub fn insert_negative(&mut self, hash_prefix: HashPrefix, ttl: Duration) {
        let expiry = Instant::now() + ttl;
        self.negative_cache.insert(hash_prefix, expiry);
    }

    /// Insert a generic cache entry with threats
    pub fn insert(&mut self, hash: HashPrefix, threats: Vec<URLThreat>) {
        let default_ttl = Duration::from_secs(300); // 5 minutes default

        if threats.is_empty() {
            // Negative cache entry
            self.insert_negative(hash, default_ttl);
        } else {
            // Positive cache entry
            let threat_descriptors: Vec<ThreatDescriptor> =
                threats.into_iter().map(|t| t.threat_descriptor).collect();
            self.insert_positive(hash, threat_descriptors, default_ttl);
        }
    }

    /// Update cache with API response
    pub fn update_with_response(
        &mut self,
        request_hashes: &[HashPrefix],
        response: &safebrowsing_proto::FindFullHashesResponse,
    ) {
        let _now = Instant::now();

        // Insert positive cache entries for matches
        for threat_match in &response.matches {
            if let Some(threat_entry) = &threat_match.threat {
                let full_hash = HashPrefix::new(threat_entry.hash.clone()).unwrap();

                let ttl = if let Some(cache_duration) = &threat_match.cache_duration {
                    Duration::from_secs(cache_duration.seconds as u64)
                        + Duration::from_nanos(cache_duration.nanos as u64)
                } else {
                    Duration::from_secs(300) // Default 5 minutes
                };

                let threat_descriptor = ThreatDescriptor {
                    threat_type: threat_match.threat_type.into(),
                    platform_type: threat_match.platform_type.into(),
                    threat_entry_type: threat_match.threat_entry_type.into(),
                };

                self.insert_positive(full_hash, vec![threat_descriptor], ttl);
            }
        }

        // Insert negative cache entries for prefixes that didn't match
        if let Some(negative_duration) = &response.negative_cache_duration {
            let negative_ttl = Duration::from_secs(negative_duration.seconds as u64)
                + Duration::from_nanos(negative_duration.nanos as u64);

            for hash_prefix in request_hashes {
                // Only add negative entry if no positive match exists
                let has_positive_match = response.matches.iter().any(|m| {
                    if let Some(threat_entry) = &m.threat {
                        let match_hash = HashPrefix::new(threat_entry.hash.clone()).unwrap();
                        hash_prefix.is_prefix_of(&match_hash)
                    } else {
                        false
                    }
                });

                if !has_positive_match {
                    self.insert_negative(hash_prefix.clone(), negative_ttl);
                }
            }
        }
    }

    /// Purge expired entries from the cache
    pub fn purge(&mut self) {
        let now = Instant::now();

        // Clean positive cache
        self.positive_cache.retain(|_, entry| {
            entry.cleanup_expired(now);
            !entry.is_empty()
        });

        // Clean negative cache
        self.negative_cache.retain(|_, &mut expiry| expiry > now);

        self.last_cleanup = now;
    }

    /// Get cache statistics
    pub fn stats(&self) -> CacheStats {
        CacheStats {
            hits: self.hits,
            misses: self.misses,
            positive_entries: self.positive_cache.len(),
            negative_entries: self.negative_cache.len(),
            hit_rate: if self.hits + self.misses > 0 {
                self.hits as f64 / (self.hits + self.misses) as f64
            } else {
                0.0
            },
        }
    }

    /// Clear all cache entries
    pub fn clear(&mut self) {
        self.positive_cache.clear();
        self.negative_cache.clear();
        self.hits = 0;
        self.misses = 0;
    }

    /// Get the total number of cache entries
    pub fn entry_count(&self) -> usize {
        self.positive_cache.len() + self.negative_cache.len()
    }

    /// Check if the cache is empty
    pub fn is_empty(&self) -> bool {
        self.positive_cache.is_empty() && self.negative_cache.is_empty()
    }

    /// Maybe perform cleanup if enough time has passed
    fn maybe_cleanup(&mut self, now: Instant) {
        if now.duration_since(self.last_cleanup) >= self.cleanup_interval {
            self.purge();
        }
    }
}

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

/// Cache statistics
#[derive(Debug, Clone)]
pub struct CacheStats {
    /// Number of cache hits
    pub hits: u64,
    /// Number of cache misses
    pub misses: u64,
    /// Number of positive cache entries
    pub positive_entries: usize,
    /// Number of negative cache entries
    pub negative_entries: usize,
    /// Cache hit rate (0.0 to 1.0)
    pub hit_rate: f64,
}

impl std::fmt::Display for CacheStats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Cache Stats: {} hits, {} misses, {:.1}% hit rate, {} positive, {} negative entries",
            self.hits,
            self.misses,
            self.hit_rate * 100.0,
            self.positive_entries,
            self.negative_entries
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use safebrowsing_api::{PlatformType, ThreatEntryType, ThreatType};

    fn create_test_hash() -> HashPrefix {
        HashPrefix::full_hash("test.example.com/path")
    }

    fn create_test_threat_descriptor() -> ThreatDescriptor {
        ThreatDescriptor {
            threat_type: ThreatType::Malware,
            platform_type: PlatformType::AnyPlatform,
            threat_entry_type: ThreatEntryType::Url,
        }
    }

    #[test]
    fn test_cache_miss() {
        let mut cache = Cache::new();
        let hash = create_test_hash();

        assert!(cache.lookup(&hash).is_none());
        assert_eq!(cache.stats().misses, 1);
        assert_eq!(cache.stats().hits, 0);
    }

    #[test]
    fn test_positive_cache() {
        let mut cache = Cache::new();
        let hash = create_test_hash();
        let threat = create_test_threat_descriptor();

        cache.insert_positive(hash.clone(), vec![threat.clone()], Duration::from_secs(60));

        let result = cache.lookup(&hash).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].threat_descriptor, threat);
        assert_eq!(cache.stats().hits, 1);
    }

    #[test]
    fn test_negative_cache() {
        let mut cache = Cache::new();
        let hash = create_test_hash();
        let prefix = hash.truncate(4).unwrap();

        cache.insert_negative(prefix, Duration::from_secs(60));

        let result = cache.lookup(&hash).unwrap();
        assert!(result.is_empty());
        assert_eq!(cache.stats().hits, 1);
    }

    #[test]
    fn test_cache_expiration() {
        let mut cache = Cache::new();
        let hash = create_test_hash();
        let threat = create_test_threat_descriptor();

        // Insert with very short TTL
        cache.insert_positive(hash.clone(), vec![threat], Duration::from_millis(1));

        // Wait for expiration
        std::thread::sleep(Duration::from_millis(10));

        // Should be cache miss now
        assert!(cache.lookup(&hash).is_none());
    }

    #[test]
    fn test_cache_purge() {
        let mut cache = Cache::new();
        let hash = create_test_hash();
        let threat = create_test_threat_descriptor();

        cache.insert_positive(hash.clone(), vec![threat], Duration::from_millis(1));
        cache.insert_negative(hash.truncate(4).unwrap(), Duration::from_millis(1));

        assert!(!cache.is_empty());

        // Wait for expiration
        std::thread::sleep(Duration::from_millis(10));

        cache.purge();
        assert!(cache.is_empty());
    }

    #[test]
    fn test_cache_stats() {
        let mut cache = Cache::new();
        let hash = create_test_hash();

        // Generate some hits and misses
        cache.lookup(&hash); // miss
        cache.insert_positive(
            hash.clone(),
            vec![create_test_threat_descriptor()],
            Duration::from_secs(60),
        );
        cache.lookup(&hash); // hit
        cache.lookup(&hash); // hit

        let stats = cache.stats();
        assert_eq!(stats.hits, 2);
        assert_eq!(stats.misses, 1);
        assert_eq!(stats.hit_rate, 2.0 / 3.0);
    }

    #[test]
    fn test_insert_generic() {
        let mut cache = Cache::new();
        let hash = create_test_hash();

        // Test empty threats (negative cache)
        cache.insert(hash.clone(), Vec::new());
        let result = cache.lookup(&hash).unwrap();
        assert!(result.is_empty());

        // Test with threats (positive cache)
        let threat = URLThreat {
            pattern: "test".to_string(),
            threat_descriptor: create_test_threat_descriptor(),
        };
        cache.insert(hash.clone(), vec![threat.clone()]);
        let result = cache.lookup(&hash).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].threat_descriptor, threat.threat_descriptor);
    }
}