derusted 0.2.0

Programmable HTTPS interception and traffic inspection engine for security-critical applications
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
//! HSTS (HTTP Strict Transport Security) Handler
//!
//! This module handles HSTS headers for MITM interception:
//! - Honor HSTS by default (proxy won't intercept HSTS sites)
//! - Optional HSTS stripping (for testing/inspection needs)
//! - Configurable via policy

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, info, warn};

/// HSTS policy configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HstsPolicy {
    /// Honor HSTS headers (default: true)
    /// When true, proxy won't intercept connections to HSTS domains
    pub honor_hsts: bool,

    /// Strip HSTS headers from responses (default: false)
    /// When true, removes Strict-Transport-Security header
    /// DANGEROUS: Only use for testing/debugging
    pub strip_hsts_headers: bool,

    /// Max age for HSTS cache (seconds, default: 31536000 = 1 year)
    pub max_cache_age: u64,

    /// Preload HSTS list (default: true)
    /// Use Chromium's preload list for known HSTS domains
    pub use_preload_list: bool,
}

impl Default for HstsPolicy {
    fn default() -> Self {
        Self {
            honor_hsts: true,          // Honor HSTS by default (secure)
            strip_hsts_headers: false, // Don't strip by default (secure)
            max_cache_age: 31536000,   // 1 year
            use_preload_list: true,    // Use preload list
        }
    }
}

impl HstsPolicy {
    /// Create policy that honors HSTS (secure default)
    pub fn honor() -> Self {
        Self::default()
    }

    /// Create policy that strips HSTS (for testing only)
    pub fn strip_for_testing() -> Self {
        Self {
            honor_hsts: false,
            strip_hsts_headers: true,
            ..Default::default()
        }
    }

    /// Create policy from environment variables
    pub fn from_env() -> Self {
        let mut policy = Self::default();

        if let Ok(val) = std::env::var("DERUSTED_HSTS_HONOR") {
            policy.honor_hsts = val.parse().unwrap_or(true);
        }

        if let Ok(val) = std::env::var("DERUSTED_HSTS_STRIP") {
            policy.strip_hsts_headers = val.parse().unwrap_or(false);
        }

        if let Ok(val) = std::env::var("DERUSTED_HSTS_MAX_AGE") {
            policy.max_cache_age = val.parse().unwrap_or(31536000);
        }

        if let Ok(val) = std::env::var("DERUSTED_HSTS_PRELOAD") {
            policy.use_preload_list = val.parse().unwrap_or(true);
        }

        policy
    }
}

/// HSTS entry in cache
#[derive(Debug, Clone)]
struct HstsEntry {
    /// When this entry expires (Unix timestamp)
    expires_at: i64,

    /// Include subdomains
    include_subdomains: bool,

    /// From preload list (never expires)
    preloaded: bool,
}

/// HSTS Manager
pub struct HstsManager {
    /// Policy configuration
    policy: HstsPolicy,

    /// HSTS cache (domain -> entry)
    cache: Arc<RwLock<HashMap<String, HstsEntry>>>,
}

impl HstsManager {
    /// Create new HSTS manager with default policy
    pub fn new() -> Self {
        Self::with_policy(HstsPolicy::default())
    }

    /// Create HSTS manager with custom policy
    pub fn with_policy(policy: HstsPolicy) -> Self {
        let mut cache = HashMap::new();

        // Load preload list if enabled
        if policy.use_preload_list {
            Self::load_preload_list(&mut cache);
        }

        Self {
            policy,
            cache: Arc::new(RwLock::new(cache)),
        }
    }

    /// Create from environment variables
    pub fn from_env() -> Self {
        Self::with_policy(HstsPolicy::from_env())
    }

    /// Check if domain is HSTS-protected
    pub async fn is_hsts_domain(&self, domain: &str) -> bool {
        if !self.policy.honor_hsts {
            return false; // HSTS disabled
        }

        let cache = self.cache.read().await;

        // Check exact match
        if let Some(entry) = cache.get(domain) {
            if entry.preloaded || entry.expires_at > chrono::Utc::now().timestamp() {
                debug!(domain = %domain, "Domain is HSTS-protected");
                return true;
            }
        }

        // Check all parent domains with includeSubDomains (recurse to apex)
        let mut current_domain = domain;
        while let Some(parent) = Self::parent_domain(current_domain) {
            if let Some(entry) = cache.get(parent) {
                if entry.include_subdomains {
                    if entry.preloaded || entry.expires_at > chrono::Utc::now().timestamp() {
                        debug!(
                            domain = %domain,
                            parent = %parent,
                            "Domain is HSTS-protected via parent"
                        );
                        return true;
                    }
                }
            }
            current_domain = parent;
        }

        false
    }

    /// Add HSTS entry from Strict-Transport-Security header
    pub async fn add_from_header(&self, domain: &str, header_value: &str) {
        if !self.policy.honor_hsts {
            return; // HSTS disabled
        }

        // Parse max-age
        let max_age = Self::parse_max_age(header_value);
        if max_age == 0 {
            warn!(
                domain = %domain,
                header = %header_value,
                "HSTS header with max-age=0, removing from cache"
            );
            self.cache.write().await.remove(domain);
            return;
        }

        // Parse includeSubDomains
        let include_subdomains = header_value.contains("includeSubDomains");

        let expires_at = chrono::Utc::now().timestamp() + max_age as i64;

        let entry = HstsEntry {
            expires_at,
            include_subdomains,
            preloaded: false,
        };

        info!(
            domain = %domain,
            max_age = max_age,
            include_subdomains = include_subdomains,
            "Added HSTS entry"
        );

        self.cache.write().await.insert(domain.to_string(), entry);
    }

    /// Process response headers (strip HSTS if configured)
    pub fn process_response_headers(&self, headers: &mut HashMap<String, String>) {
        if self.policy.strip_hsts_headers {
            if headers.remove("strict-transport-security").is_some() {
                warn!("HSTS header stripped (testing mode)");
            }
        }
    }

    /// Get parent domain
    fn parent_domain(domain: &str) -> Option<&str> {
        let parts: Vec<&str> = domain.split('.').collect();
        if parts.len() > 2 {
            // subdomain.example.com -> example.com
            Some(&domain[domain.find('.').unwrap() + 1..])
        } else {
            None
        }
    }

    /// Parse max-age from header value
    fn parse_max_age(header_value: &str) -> u64 {
        for directive in header_value.split(';') {
            let directive = directive.trim();
            if let Some(value) = directive.strip_prefix("max-age=") {
                if let Ok(age) = value.trim().parse::<u64>() {
                    return age;
                }
            }
        }
        0
    }

    /// Load HSTS preload list
    fn load_preload_list(cache: &mut HashMap<String, HstsEntry>) {
        // Chromium's HSTS preload list (subset of most common domains)
        // Full list: https://hstspreload.org/
        let preload_domains = vec![
            "google.com",
            "gmail.com",
            "youtube.com",
            "facebook.com",
            "twitter.com",
            "github.com",
            "wikipedia.org",
            "cloudflare.com",
            "amazon.com",
            "apple.com",
            "microsoft.com",
            "netflix.com",
            "linkedin.com",
            "reddit.com",
            "instagram.com",
            "paypal.com",
            "dropbox.com",
            "stackoverflow.com",
            "zoom.us",
            "slack.com",
        ];

        let count = preload_domains.len();

        for domain in &preload_domains {
            cache.insert(
                domain.to_string(),
                HstsEntry {
                    expires_at: i64::MAX, // Never expires
                    include_subdomains: true,
                    preloaded: true,
                },
            );
        }

        info!(count = count, "Loaded HSTS preload list");
    }

    /// Get policy
    pub fn policy(&self) -> &HstsPolicy {
        &self.policy
    }

    /// Get cache size (for monitoring)
    pub async fn cache_size(&self) -> usize {
        self.cache.read().await.len()
    }

    /// Clear expired entries
    pub async fn cleanup_expired(&self) {
        let mut cache = self.cache.write().await;
        let now = chrono::Utc::now().timestamp();

        cache.retain(|domain, entry| {
            if entry.preloaded {
                true // Keep preloaded entries
            } else if entry.expires_at > now {
                true // Keep valid entries
            } else {
                debug!(domain = %domain, "HSTS entry expired");
                false
            }
        });
    }
}

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

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

    #[tokio::test]
    async fn test_hsts_policy_defaults() {
        let policy = HstsPolicy::default();
        assert!(policy.honor_hsts);
        assert!(!policy.strip_hsts_headers);
        assert_eq!(policy.max_cache_age, 31536000);
        assert!(policy.use_preload_list);
    }

    #[tokio::test]
    async fn test_hsts_policy_strip_for_testing() {
        let policy = HstsPolicy::strip_for_testing();
        assert!(!policy.honor_hsts);
        assert!(policy.strip_hsts_headers);
    }

    #[tokio::test]
    async fn test_hsts_manager_preload_list() {
        let manager = HstsManager::new();

        // Check preloaded domains
        assert!(manager.is_hsts_domain("google.com").await);
        assert!(manager.is_hsts_domain("github.com").await);
        assert!(manager.is_hsts_domain("facebook.com").await);

        // Non-preloaded domain
        assert!(!manager.is_hsts_domain("example.com").await);
    }

    #[tokio::test]
    async fn test_hsts_manager_add_from_header() {
        let manager = HstsManager::new();

        // Add HSTS entry
        manager
            .add_from_header("example.com", "max-age=31536000; includeSubDomains")
            .await;

        // Check if domain is now HSTS-protected
        assert!(manager.is_hsts_domain("example.com").await);
        assert!(manager.is_hsts_domain("sub.example.com").await);
    }

    #[tokio::test]
    async fn test_hsts_manager_max_age_zero() {
        let manager = HstsManager::new();

        // Add entry first
        manager
            .add_from_header("example.com", "max-age=31536000")
            .await;
        assert!(manager.is_hsts_domain("example.com").await);

        // Remove with max-age=0
        manager.add_from_header("example.com", "max-age=0").await;
        assert!(!manager.is_hsts_domain("example.com").await);
    }

    #[tokio::test]
    async fn test_hsts_manager_disabled() {
        let policy = HstsPolicy {
            honor_hsts: false,
            ..Default::default()
        };
        let manager = HstsManager::with_policy(policy);

        // Even preloaded domains should not be protected when disabled
        assert!(!manager.is_hsts_domain("google.com").await);
    }

    #[tokio::test]
    async fn test_hsts_strip_headers() {
        let policy = HstsPolicy::strip_for_testing();
        let manager = HstsManager::with_policy(policy);

        let mut headers = HashMap::new();
        headers.insert(
            "strict-transport-security".to_string(),
            "max-age=31536000".to_string(),
        );
        headers.insert("content-type".to_string(), "text/html".to_string());

        manager.process_response_headers(&mut headers);

        // HSTS header should be removed
        assert!(!headers.contains_key("strict-transport-security"));
        // Other headers should remain
        assert!(headers.contains_key("content-type"));
    }

    #[test]
    fn test_parse_max_age() {
        assert_eq!(HstsManager::parse_max_age("max-age=31536000"), 31536000);
        assert_eq!(HstsManager::parse_max_age("max-age=0"), 0);
        assert_eq!(
            HstsManager::parse_max_age("max-age=31536000; includeSubDomains"),
            31536000
        );
        assert_eq!(HstsManager::parse_max_age("invalid"), 0);
    }

    #[test]
    fn test_parent_domain() {
        assert_eq!(
            HstsManager::parent_domain("sub.example.com"),
            Some("example.com")
        );
        assert_eq!(
            HstsManager::parent_domain("deep.sub.example.com"),
            Some("sub.example.com")
        );
        assert_eq!(HstsManager::parent_domain("example.com"), None);
    }

    #[tokio::test]
    async fn test_cleanup_expired() {
        let manager = HstsManager::new();

        // Add entry that expires immediately
        manager.cache.write().await.insert(
            "expired.com".to_string(),
            HstsEntry {
                expires_at: 0,
                include_subdomains: false,
                preloaded: false,
            },
        );

        // Add valid entry
        manager.cache.write().await.insert(
            "valid.com".to_string(),
            HstsEntry {
                expires_at: chrono::Utc::now().timestamp() + 3600,
                include_subdomains: false,
                preloaded: false,
            },
        );

        manager.cleanup_expired().await;

        // Expired entry should be removed
        assert!(!manager.cache.read().await.contains_key("expired.com"));
        // Valid entry should remain
        assert!(manager.cache.read().await.contains_key("valid.com"));
    }

    #[tokio::test]
    async fn test_hsts_includesubdomains_multi_level() {
        let manager = HstsManager::new();

        // Add HSTS entry for apex domain with includeSubDomains
        manager
            .add_from_header("example.com", "max-age=31536000; includeSubDomains")
            .await;

        // Test multi-level subdomain chain: foo.bar.example.com should be protected
        // because example.com has includeSubDomains, even though bar.example.com
        // is not explicitly in the cache
        assert!(
            manager.is_hsts_domain("foo.bar.example.com").await,
            "foo.bar.example.com should be protected via example.com includeSubDomains"
        );

        // Verify all levels are protected
        assert!(manager.is_hsts_domain("example.com").await);
        assert!(manager.is_hsts_domain("bar.example.com").await);
        assert!(manager.is_hsts_domain("foo.bar.example.com").await);
        assert!(manager.is_hsts_domain("baz.foo.bar.example.com").await);
    }
}