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
//! Core types for the Safe Browsing library

use serde::{Deserialize, Serialize};
use std::fmt;

// Re-export protobuf types for convenience
pub use safebrowsing_proto;

/// Types of threats that can be detected
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ThreatType {
    /// Malware threat type
    Malware = 1,
    /// Social engineering threat type
    SocialEngineering = 2,
    /// Unwanted software threat type
    UnwantedSoftware = 3,
    /// Potentially harmful application threat type
    PotentiallyHarmfulApplication = 4,
}

impl fmt::Display for ThreatType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ThreatType::Malware => write!(f, "MALWARE"),
            ThreatType::SocialEngineering => write!(f, "SOCIAL_ENGINEERING"),
            ThreatType::UnwantedSoftware => write!(f, "UNWANTED_SOFTWARE"),
            ThreatType::PotentiallyHarmfulApplication => {
                write!(f, "POTENTIALLY_HARMFUL_APPLICATION")
            }
        }
    }
}

impl From<i32> for ThreatType {
    fn from(value: i32) -> Self {
        match value {
            1 => ThreatType::Malware,
            2 => ThreatType::SocialEngineering,
            3 => ThreatType::UnwantedSoftware,
            4 => ThreatType::PotentiallyHarmfulApplication,
            _ => ThreatType::Malware, // Default fallback
        }
    }
}

impl From<ThreatType> for i32 {
    fn from(threat_type: ThreatType) -> Self {
        threat_type as i32
    }
}

impl From<safebrowsing_proto::ThreatType> for ThreatType {
    fn from(proto_type: safebrowsing_proto::ThreatType) -> Self {
        match proto_type {
            safebrowsing_proto::ThreatType::Malware => ThreatType::Malware,
            safebrowsing_proto::ThreatType::SocialEngineering => ThreatType::SocialEngineering,
            safebrowsing_proto::ThreatType::UnwantedSoftware => ThreatType::UnwantedSoftware,
            safebrowsing_proto::ThreatType::PotentiallyHarmfulApplication => {
                ThreatType::PotentiallyHarmfulApplication
            }
            _ => ThreatType::Malware,
        }
    }
}

impl From<ThreatType> for safebrowsing_proto::ThreatType {
    fn from(threat_type: ThreatType) -> Self {
        match threat_type {
            ThreatType::Malware => safebrowsing_proto::ThreatType::Malware,
            ThreatType::SocialEngineering => safebrowsing_proto::ThreatType::SocialEngineering,
            ThreatType::UnwantedSoftware => safebrowsing_proto::ThreatType::UnwantedSoftware,
            ThreatType::PotentiallyHarmfulApplication => {
                safebrowsing_proto::ThreatType::PotentiallyHarmfulApplication
            }
        }
    }
}

/// Types of platforms that can be affected by threats
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PlatformType {
    /// Any platform
    AnyPlatform = 6,
    /// All platforms
    AllPlatforms = 7,
    /// Windows platform
    Windows = 1,
    /// Linux platform
    Linux = 2,
    /// Android platform
    Android = 3,
    /// OSX platform
    Osx = 4,
    /// iOS platform
    Ios = 5,
    /// Chrome browser
    Chrome = 8,
}

impl fmt::Display for PlatformType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PlatformType::AnyPlatform => write!(f, "ANY_PLATFORM"),
            PlatformType::AllPlatforms => write!(f, "ALL_PLATFORMS"),
            PlatformType::Windows => write!(f, "WINDOWS"),
            PlatformType::Linux => write!(f, "LINUX"),
            PlatformType::Android => write!(f, "ANDROID"),
            PlatformType::Osx => write!(f, "OSX"),
            PlatformType::Ios => write!(f, "IOS"),
            PlatformType::Chrome => write!(f, "CHROME"),
        }
    }
}

impl From<i32> for PlatformType {
    fn from(value: i32) -> Self {
        match value {
            1 => PlatformType::Windows,
            2 => PlatformType::Linux,
            3 => PlatformType::Android,
            4 => PlatformType::Osx,
            5 => PlatformType::Ios,
            6 => PlatformType::AnyPlatform,
            7 => PlatformType::AllPlatforms,
            8 => PlatformType::Chrome,
            _ => PlatformType::AnyPlatform, // Default fallback
        }
    }
}

impl From<PlatformType> for i32 {
    fn from(platform_type: PlatformType) -> Self {
        platform_type as i32
    }
}

impl From<safebrowsing_proto::PlatformType> for PlatformType {
    fn from(proto_type: safebrowsing_proto::PlatformType) -> Self {
        match proto_type {
            safebrowsing_proto::PlatformType::Windows => PlatformType::Windows,
            safebrowsing_proto::PlatformType::Linux => PlatformType::Linux,
            safebrowsing_proto::PlatformType::Android => PlatformType::Android,
            safebrowsing_proto::PlatformType::Osx => PlatformType::Osx,
            safebrowsing_proto::PlatformType::Ios => PlatformType::Ios,
            safebrowsing_proto::PlatformType::AnyPlatform => PlatformType::AnyPlatform,
            safebrowsing_proto::PlatformType::AllPlatforms => PlatformType::AllPlatforms,
            safebrowsing_proto::PlatformType::Chrome => PlatformType::Chrome,
            _ => PlatformType::AnyPlatform,
        }
    }
}

impl From<PlatformType> for safebrowsing_proto::PlatformType {
    fn from(platform_type: PlatformType) -> Self {
        match platform_type {
            PlatformType::Windows => safebrowsing_proto::PlatformType::Windows,
            PlatformType::Linux => safebrowsing_proto::PlatformType::Linux,
            PlatformType::Android => safebrowsing_proto::PlatformType::Android,
            PlatformType::Osx => safebrowsing_proto::PlatformType::Osx,
            PlatformType::Ios => safebrowsing_proto::PlatformType::Ios,
            PlatformType::AnyPlatform => safebrowsing_proto::PlatformType::AnyPlatform,
            PlatformType::AllPlatforms => safebrowsing_proto::PlatformType::AllPlatforms,
            PlatformType::Chrome => safebrowsing_proto::PlatformType::Chrome,
        }
    }
}

/// Types of threat entries
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ThreatEntryType {
    /// URL entry type
    Url = 1,
    /// Executable entry type
    Executable = 2,
    /// IP range entry type
    IpRange = 3,
}

impl fmt::Display for ThreatEntryType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ThreatEntryType::Url => write!(f, "URL"),
            ThreatEntryType::Executable => write!(f, "EXECUTABLE"),
            ThreatEntryType::IpRange => write!(f, "IP_RANGE"),
        }
    }
}

impl From<i32> for ThreatEntryType {
    fn from(value: i32) -> Self {
        match value {
            1 => ThreatEntryType::Url,
            2 => ThreatEntryType::Executable,
            3 => ThreatEntryType::IpRange,
            _ => ThreatEntryType::Url, // Default fallback
        }
    }
}

impl From<ThreatEntryType> for i32 {
    fn from(entry_type: ThreatEntryType) -> Self {
        entry_type as i32
    }
}

impl From<safebrowsing_proto::ThreatEntryType> for ThreatEntryType {
    fn from(proto_type: safebrowsing_proto::ThreatEntryType) -> Self {
        match proto_type {
            safebrowsing_proto::ThreatEntryType::Url => ThreatEntryType::Url,
            safebrowsing_proto::ThreatEntryType::Executable => ThreatEntryType::Executable,
            safebrowsing_proto::ThreatEntryType::IpRange => ThreatEntryType::IpRange,
            _ => ThreatEntryType::Url,
        }
    }
}

impl From<ThreatEntryType> for safebrowsing_proto::ThreatEntryType {
    fn from(entry_type: ThreatEntryType) -> Self {
        match entry_type {
            ThreatEntryType::Url => safebrowsing_proto::ThreatEntryType::Url,
            ThreatEntryType::Executable => safebrowsing_proto::ThreatEntryType::Executable,
            ThreatEntryType::IpRange => safebrowsing_proto::ThreatEntryType::IpRange,
        }
    }
}

/// Describes a specific threat list by combining threat type, platform type, and entry type
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ThreatDescriptor {
    /// The type of threat
    pub threat_type: ThreatType,
    /// The platform type affected
    pub platform_type: PlatformType,
    /// The type of threat entry
    pub threat_entry_type: ThreatEntryType,
}

impl fmt::Display for ThreatDescriptor {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}/{}/{}",
            self.threat_type, self.platform_type, self.threat_entry_type
        )
    }
}

impl ThreatDescriptor {
    /// Create a new threat descriptor
    pub fn new(
        threat_type: ThreatType,
        platform_type: PlatformType,
        threat_entry_type: ThreatEntryType,
    ) -> Self {
        Self {
            threat_type,
            platform_type,
            threat_entry_type,
        }
    }
}

/// Represents a threat found for a specific URL pattern
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct URLThreat {
    /// The URL pattern that matched
    pub pattern: String,
    /// The threat descriptor that matched
    pub threat_descriptor: ThreatDescriptor,
}

impl URLThreat {
    /// Create a new URL threat
    pub fn new(pattern: String, threat_descriptor: ThreatDescriptor) -> Self {
        Self {
            pattern,
            threat_descriptor,
        }
    }
}

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

/// Compression types supported by the Safe Browsing API
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CompressionType {
    /// Raw, uncompressed data
    Raw = 1,
    /// Rice-Golomb encoded data
    Rice = 2,
}

impl From<i32> for CompressionType {
    fn from(value: i32) -> Self {
        match value {
            1 => CompressionType::Raw,
            2 => CompressionType::Rice,
            _ => CompressionType::Raw,
        }
    }
}

impl From<CompressionType> for i32 {
    fn from(compression_type: CompressionType) -> Self {
        compression_type as i32
    }
}

impl From<safebrowsing_proto::CompressionType> for CompressionType {
    fn from(proto_type: safebrowsing_proto::CompressionType) -> Self {
        match proto_type {
            safebrowsing_proto::CompressionType::Raw => CompressionType::Raw,
            safebrowsing_proto::CompressionType::Rice => CompressionType::Rice,
            _ => CompressionType::Raw,
        }
    }
}

impl From<CompressionType> for safebrowsing_proto::CompressionType {
    fn from(compression_type: CompressionType) -> Self {
        match compression_type {
            CompressionType::Raw => safebrowsing_proto::CompressionType::Raw,
            CompressionType::Rice => safebrowsing_proto::CompressionType::Rice,
        }
    }
}

/// Default threat lists that cover the most common threats
pub const DEFAULT_THREAT_LISTS: &[ThreatDescriptor] = &[
    ThreatDescriptor {
        threat_type: ThreatType::Malware,
        platform_type: PlatformType::AnyPlatform,
        threat_entry_type: ThreatEntryType::Url,
    },
    ThreatDescriptor {
        threat_type: ThreatType::SocialEngineering,
        platform_type: PlatformType::AnyPlatform,
        threat_entry_type: ThreatEntryType::Url,
    },
    ThreatDescriptor {
        threat_type: ThreatType::UnwantedSoftware,
        platform_type: PlatformType::AnyPlatform,
        threat_entry_type: ThreatEntryType::Url,
    },
];

/// Android-specific threat lists
pub const ANDROID_THREAT_LISTS: &[ThreatDescriptor] = &[ThreatDescriptor {
    threat_type: ThreatType::PotentiallyHarmfulApplication,
    platform_type: PlatformType::Android,
    threat_entry_type: ThreatEntryType::Url,
}];

/// All available threat lists
pub const ALL_THREAT_LISTS: &[ThreatDescriptor] = &[
    ThreatDescriptor {
        threat_type: ThreatType::Malware,
        platform_type: PlatformType::AnyPlatform,
        threat_entry_type: ThreatEntryType::Url,
    },
    ThreatDescriptor {
        threat_type: ThreatType::SocialEngineering,
        platform_type: PlatformType::AnyPlatform,
        threat_entry_type: ThreatEntryType::Url,
    },
    ThreatDescriptor {
        threat_type: ThreatType::UnwantedSoftware,
        platform_type: PlatformType::AnyPlatform,
        threat_entry_type: ThreatEntryType::Url,
    },
    ThreatDescriptor {
        threat_type: ThreatType::PotentiallyHarmfulApplication,
        platform_type: PlatformType::Android,
        threat_entry_type: ThreatEntryType::Url,
    },
];

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

    #[test]
    fn test_threat_type_conversions() {
        assert_eq!(ThreatType::Malware as i32, 1);
        assert_eq!(ThreatType::from(1), ThreatType::Malware);
        assert_eq!(ThreatType::from(999), ThreatType::Malware); // Default fallback
    }

    #[test]
    fn test_platform_type_conversions() {
        assert_eq!(PlatformType::Windows as i32, 1);
        assert_eq!(PlatformType::from(1), PlatformType::Windows);
        assert_eq!(PlatformType::from(999), PlatformType::AnyPlatform); // Default fallback
    }

    #[test]
    fn test_threat_entry_type_conversions() {
        assert_eq!(ThreatEntryType::Url as i32, 1);
        assert_eq!(ThreatEntryType::from(1), ThreatEntryType::Url);
        assert_eq!(ThreatEntryType::from(999), ThreatEntryType::Url); // Default fallback
    }

    #[test]
    fn test_threat_descriptor() {
        let desc = ThreatDescriptor::new(
            ThreatType::Malware,
            PlatformType::AnyPlatform,
            ThreatEntryType::Url,
        );

        assert_eq!(desc.threat_type, ThreatType::Malware);
        assert_eq!(desc.platform_type, PlatformType::AnyPlatform);
        assert_eq!(desc.threat_entry_type, ThreatEntryType::Url);
    }

    #[test]
    fn test_url_threat() {
        let desc = ThreatDescriptor::new(
            ThreatType::Malware,
            PlatformType::AnyPlatform,
            ThreatEntryType::Url,
        );
        let threat = URLThreat::new("example.com/malicious".to_string(), desc);

        assert_eq!(threat.pattern, "example.com/malicious");
        assert_eq!(threat.threat_descriptor.threat_type, ThreatType::Malware);
    }

    #[test]
    fn test_display_implementations() {
        assert_eq!(ThreatType::Malware.to_string(), "MALWARE");
        assert_eq!(PlatformType::AnyPlatform.to_string(), "ANY_PLATFORM");
        assert_eq!(ThreatEntryType::Url.to_string(), "URL");

        let desc = ThreatDescriptor::new(
            ThreatType::Malware,
            PlatformType::AnyPlatform,
            ThreatEntryType::Url,
        );
        assert_eq!(desc.to_string(), "MALWARE/ANY_PLATFORM/URL");
    }

    #[test]
    fn test_default_threat_lists() {
        assert!(!DEFAULT_THREAT_LISTS.is_empty());
        assert!(DEFAULT_THREAT_LISTS.contains(&ThreatDescriptor {
            threat_type: ThreatType::Malware,
            platform_type: PlatformType::AnyPlatform,
            threat_entry_type: ThreatEntryType::Url,
        }));
    }
}