cipherrun 0.3.0

A fast, modular, and scalable TLS/SSL security scanner written in Rust
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
// HTTP Security Headers Checker - Validate security headers

use super::hsts_preload::PreloadStatus;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Security header issue severity
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum IssueSeverity {
    Critical,
    High,
    Medium,
    Low,
    Info,
}

impl IssueSeverity {
    /// Returns a colored string representation for terminal display
    pub fn colored_display(&self) -> colored::ColoredString {
        use colored::Colorize;
        match self {
            Self::Critical => "CRITICAL".red().bold(),
            Self::High => "HIGH".red(),
            Self::Medium => "MEDIUM".yellow(),
            Self::Low => "LOW".normal(),
            Self::Info => "INFO".cyan(),
        }
    }
}

/// Security header issue
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HeaderIssue {
    pub header_name: String,
    pub severity: IssueSeverity,
    pub issue_type: IssueType,
    pub description: String,
    pub recommendation: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub preload_status: Option<PreloadStatus>,
}

/// Type of header issue
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum IssueType {
    Missing,
    Insecure,
    Weak,
    Deprecated,
    Invalid,
}

/// HTTP security header checker
pub struct SecurityHeaderChecker;

impl SecurityHeaderChecker {
    /// Check all security headers (synchronous version without preload check)
    pub fn check_all_headers(headers: &HashMap<String, String>) -> Vec<HeaderIssue> {
        let mut issues = Vec::new();

        // Check critical security headers
        Self::check_hsts(headers, &mut issues, None);
        Self::check_csp(headers, &mut issues);
        Self::check_x_frame_options(headers, &mut issues);
        Self::check_x_content_type_options(headers, &mut issues);
        Self::check_x_xss_protection(headers, &mut issues);
        Self::check_referrer_policy(headers, &mut issues);
        Self::check_permissions_policy(headers, &mut issues);
        Self::check_expect_ct(headers, &mut issues);
        Self::check_expect_staple(headers, &mut issues);
        Self::check_cors(headers, &mut issues);

        issues
    }

    /// Check all security headers with HSTS preload verification (async version)
    pub async fn check_all_headers_with_preload(
        headers: &HashMap<String, String>,
        domain: &str,
    ) -> Vec<HeaderIssue> {
        let mut issues = Vec::new();

        // Check HSTS with preload verification
        use super::hsts_preload::HstsPreloadChecker;
        let checker = HstsPreloadChecker::new();

        // Check if HSTS has preload directive
        let has_preload = if let Some((_, value)) =
            Self::find_header_case_insensitive(headers, "strict-transport-security")
        {
            value.to_lowercase().contains("preload")
        } else {
            false
        };

        // Only check preload status if preload directive is present
        let preload_status = if has_preload {
            checker.check_preload_status(domain).await.ok()
        } else {
            None
        };

        Self::check_hsts(headers, &mut issues, preload_status);
        Self::check_csp(headers, &mut issues);
        Self::check_x_frame_options(headers, &mut issues);
        Self::check_x_content_type_options(headers, &mut issues);
        Self::check_x_xss_protection(headers, &mut issues);
        Self::check_referrer_policy(headers, &mut issues);
        Self::check_permissions_policy(headers, &mut issues);
        Self::check_expect_ct(headers, &mut issues);
        Self::check_expect_staple(headers, &mut issues);
        Self::check_cors(headers, &mut issues);

        issues
    }

    /// Check HTTP Strict Transport Security (HSTS)
    fn check_hsts(
        headers: &HashMap<String, String>,
        issues: &mut Vec<HeaderIssue>,
        preload_status: Option<PreloadStatus>,
    ) {
        let key = Self::find_header_case_insensitive(headers, "strict-transport-security");

        if let Some((_, value)) = key {
            // Parse HSTS value
            let max_age = Self::extract_directive(value, "max-age");
            let includes_subdomains = value.to_lowercase().contains("includesubdomains");
            let preload = value.to_lowercase().contains("preload");

            // Check max-age value
            if let Some(age_str) = max_age {
                if let Ok(age) = age_str.parse::<u64>() {
                    if age < 31536000 {
                        // Less than 1 year
                        issues.push(HeaderIssue {
                            header_name: "Strict-Transport-Security".to_string(),
                            severity: IssueSeverity::Medium,
                            issue_type: IssueType::Weak,
                            description: format!(
                                "HSTS max-age is {} seconds (less than 1 year)",
                                age
                            ),
                            recommendation: "Set max-age to at least 31536000 (1 year)".to_string(),
                            preload_status: None,
                        });
                    }
                } else {
                    issues.push(HeaderIssue {
                        header_name: "Strict-Transport-Security".to_string(),
                        severity: IssueSeverity::High,
                        issue_type: IssueType::Invalid,
                        description: "HSTS max-age value is invalid".to_string(),
                        recommendation: "Set a valid max-age directive".to_string(),
                        preload_status: None,
                    });
                }
            } else {
                issues.push(HeaderIssue {
                    header_name: "Strict-Transport-Security".to_string(),
                    severity: IssueSeverity::High,
                    issue_type: IssueType::Invalid,
                    description: "HSTS header missing max-age directive".to_string(),
                    recommendation: "Add max-age directive".to_string(),
                    preload_status: None,
                });
            }

            if !includes_subdomains {
                issues.push(HeaderIssue {
                    header_name: "Strict-Transport-Security".to_string(),
                    severity: IssueSeverity::Low,
                    issue_type: IssueType::Weak,
                    description: "HSTS does not include subdomains".to_string(),
                    recommendation: "Consider adding 'includeSubDomains' directive".to_string(),
                    preload_status: None,
                });
            }

            if !preload {
                issues.push(HeaderIssue {
                    header_name: "Strict-Transport-Security".to_string(),
                    severity: IssueSeverity::Info,
                    issue_type: IssueType::Weak,
                    description: "HSTS preload not enabled".to_string(),
                    recommendation: "Consider adding 'preload' directive for browser preload lists"
                        .to_string(),
                    preload_status: None,
                });
            } else if let Some(status) = &preload_status {
                // Preload directive is present, check if actually preloaded
                use super::hsts_preload::PreloadSource;

                let not_in_browsers = !status.in_chrome || !status.in_firefox || !status.in_edge;

                if not_in_browsers && !matches!(status.source, PreloadSource::Error(_)) {
                    let mut browsers_missing = Vec::new();
                    if !status.in_chrome {
                        browsers_missing.push("Chrome");
                    }
                    if !status.in_firefox {
                        browsers_missing.push("Firefox");
                    }
                    if !status.in_edge {
                        browsers_missing.push("Edge");
                    }
                    if !status.in_safari {
                        browsers_missing.push("Safari");
                    }

                    let severity = if browsers_missing.len() >= 3 {
                        IssueSeverity::Medium
                    } else {
                        IssueSeverity::Low
                    };

                    issues.push(HeaderIssue {
                        header_name: "Strict-Transport-Security".to_string(),
                        severity,
                        issue_type: IssueType::Weak,
                        description: format!(
                            "HSTS preload directive present but not in browser preload lists: {}",
                            browsers_missing.join(", ")
                        ),
                        recommendation: format!(
                            "Submit domain to https://hstspreload.org/ (Status: {})",
                            status.chromium_status.as_deref().unwrap_or("unknown")
                        ),
                        preload_status: Some(status.clone()),
                    });
                }
            }
        } else {
            issues.push(HeaderIssue {
                header_name: "Strict-Transport-Security".to_string(),
                severity: IssueSeverity::High,
                issue_type: IssueType::Missing,
                description: "HSTS header is missing".to_string(),
                recommendation:
                    "Add 'Strict-Transport-Security: max-age=31536000; includeSubDomains; preload'"
                        .to_string(),
                preload_status: None,
            });
        }
    }

    /// Check Content Security Policy (CSP)
    fn check_csp(headers: &HashMap<String, String>, issues: &mut Vec<HeaderIssue>) {
        let key = Self::find_header_case_insensitive(headers, "content-security-policy");

        if let Some((_, value)) = key {
            // Check for unsafe directives
            if value.contains("'unsafe-inline'") {
                issues.push(HeaderIssue {
                    header_name: "Content-Security-Policy".to_string(),
                    severity: IssueSeverity::Medium,
                    issue_type: IssueType::Insecure,
                    description: "CSP allows 'unsafe-inline' which reduces XSS protection"
                        .to_string(),
                    recommendation: "Remove 'unsafe-inline' and use nonces or hashes".to_string(),
                    preload_status: None,
                });
            }

            if value.contains("'unsafe-eval'") {
                issues.push(HeaderIssue {
                    header_name: "Content-Security-Policy".to_string(),
                    severity: IssueSeverity::Medium,
                    issue_type: IssueType::Insecure,
                    description: "CSP allows 'unsafe-eval' which can enable code injection"
                        .to_string(),
                    recommendation: "Remove 'unsafe-eval' directive".to_string(),
                    preload_status: None,
                });
            }

            if !value.contains("default-src") && !value.contains("script-src") {
                issues.push(HeaderIssue {
                    header_name: "Content-Security-Policy".to_string(),
                    severity: IssueSeverity::Medium,
                    issue_type: IssueType::Weak,
                    description: "CSP missing default-src or script-src directive".to_string(),
                    recommendation: "Add default-src or script-src directive".to_string(),
                    preload_status: None,
                });
            }
        } else {
            issues.push(HeaderIssue {
                header_name: "Content-Security-Policy".to_string(),
                severity: IssueSeverity::Medium,
                issue_type: IssueType::Missing,
                description: "CSP header is missing".to_string(),
                recommendation: "Add Content-Security-Policy header to prevent XSS attacks"
                    .to_string(),
                preload_status: None,
            });
        }
    }

    /// Check X-Frame-Options
    fn check_x_frame_options(headers: &HashMap<String, String>, issues: &mut Vec<HeaderIssue>) {
        let key = Self::find_header_case_insensitive(headers, "x-frame-options");

        if let Some((_, value)) = key {
            let value_lower = value.to_lowercase();
            if value_lower != "deny"
                && value_lower != "sameorigin"
                && !value_lower.starts_with("allow-from")
            {
                issues.push(HeaderIssue {
                    header_name: "X-Frame-Options".to_string(),
                    severity: IssueSeverity::Medium,
                    issue_type: IssueType::Invalid,
                    description: format!("Invalid X-Frame-Options value: {}", value),
                    recommendation: "Use 'DENY' or 'SAMEORIGIN'".to_string(),
                    preload_status: None,
                });
            }
        } else {
            issues.push(HeaderIssue {
                header_name: "X-Frame-Options".to_string(),
                severity: IssueSeverity::Medium,
                issue_type: IssueType::Missing,
                description: "X-Frame-Options header is missing".to_string(),
                recommendation:
                    "Add 'X-Frame-Options: DENY' or 'SAMEORIGIN' to prevent clickjacking"
                        .to_string(),
                preload_status: None,
            });
        }
    }

    /// Check X-Content-Type-Options
    fn check_x_content_type_options(
        headers: &HashMap<String, String>,
        issues: &mut Vec<HeaderIssue>,
    ) {
        let key = Self::find_header_case_insensitive(headers, "x-content-type-options");

        if let Some((_, value)) = key {
            if value.to_lowercase() != "nosniff" {
                issues.push(HeaderIssue {
                    header_name: "X-Content-Type-Options".to_string(),
                    severity: IssueSeverity::Low,
                    issue_type: IssueType::Invalid,
                    description: format!("Invalid X-Content-Type-Options value: {}", value),
                    recommendation: "Set to 'nosniff'".to_string(),
                    preload_status: None,
                });
            }
        } else {
            issues.push(HeaderIssue {
                header_name: "X-Content-Type-Options".to_string(),
                severity: IssueSeverity::Low,
                issue_type: IssueType::Missing,
                description: "X-Content-Type-Options header is missing".to_string(),
                recommendation: "Add 'X-Content-Type-Options: nosniff' to prevent MIME-sniffing"
                    .to_string(),
                preload_status: None,
            });
        }
    }

    /// Check X-XSS-Protection (deprecated but still checked)
    fn check_x_xss_protection(headers: &HashMap<String, String>, issues: &mut Vec<HeaderIssue>) {
        let key = Self::find_header_case_insensitive(headers, "x-xss-protection");

        if let Some((_, value)) = key {
            let value_lower = value.to_lowercase();
            if value_lower.starts_with('0') {
                issues.push(HeaderIssue {
                    header_name: "X-XSS-Protection".to_string(),
                    severity: IssueSeverity::Low,
                    issue_type: IssueType::Insecure,
                    description: "X-XSS-Protection is disabled".to_string(),
                    recommendation:
                        "Enable with '1; mode=block' or remove (deprecated, use CSP instead)"
                            .to_string(),
                    preload_status: None,
                });
            }
        } else {
            issues.push(HeaderIssue {
                header_name: "X-XSS-Protection".to_string(),
                severity: IssueSeverity::Info,
                issue_type: IssueType::Missing,
                description: "X-XSS-Protection header is missing (deprecated)".to_string(),
                recommendation: "Header is deprecated. Use Content-Security-Policy instead"
                    .to_string(),
                preload_status: None,
            });
        }
    }

    /// Check Referrer-Policy
    fn check_referrer_policy(headers: &HashMap<String, String>, issues: &mut Vec<HeaderIssue>) {
        let key = Self::find_header_case_insensitive(headers, "referrer-policy");

        if let Some((_, value)) = key {
            let value_lower = value.to_lowercase();
            if value_lower.contains("unsafe-url") || value_lower == "no-referrer-when-downgrade" {
                issues.push(HeaderIssue {
                    header_name: "Referrer-Policy".to_string(),
                    severity: IssueSeverity::Low,
                    issue_type: IssueType::Weak,
                    description: "Referrer-Policy uses a weak setting".to_string(),
                    recommendation:
                        "Use 'no-referrer', 'strict-origin', or 'strict-origin-when-cross-origin'"
                            .to_string(),
                    preload_status: None,
                });
            }
        } else {
            issues.push(HeaderIssue {
                header_name: "Referrer-Policy".to_string(),
                severity: IssueSeverity::Low,
                issue_type: IssueType::Missing,
                description: "Referrer-Policy header is missing".to_string(),
                recommendation: "Add 'Referrer-Policy: strict-origin-when-cross-origin'"
                    .to_string(),
                preload_status: None,
            });
        }
    }

    /// Check Permissions-Policy (formerly Feature-Policy)
    fn check_permissions_policy(headers: &HashMap<String, String>, issues: &mut Vec<HeaderIssue>) {
        let has_permissions =
            Self::find_header_case_insensitive(headers, "permissions-policy").is_some();
        let has_feature = Self::find_header_case_insensitive(headers, "feature-policy").is_some();

        if !has_permissions && !has_feature {
            issues.push(HeaderIssue {
                header_name: "Permissions-Policy".to_string(),
                severity: IssueSeverity::Info,
                issue_type: IssueType::Missing,
                description: "Permissions-Policy header is missing".to_string(),
                recommendation: "Consider adding Permissions-Policy to control browser features"
                    .to_string(),
                preload_status: None,
            });
        }
    }

    /// Check Expect-CT (deprecated)
    fn check_expect_ct(headers: &HashMap<String, String>, issues: &mut Vec<HeaderIssue>) {
        let key = Self::find_header_case_insensitive(headers, "expect-ct");

        if key.is_some() {
            issues.push(HeaderIssue {
                header_name: "Expect-CT".to_string(),
                severity: IssueSeverity::Info,
                issue_type: IssueType::Deprecated,
                description: "Expect-CT header is deprecated".to_string(),
                recommendation: "Header is no longer needed as CT is now mandatory".to_string(),
                preload_status: None,
            });
        }
    }

    /// Check Expect-Staple (never standardized)
    fn check_expect_staple(headers: &HashMap<String, String>, issues: &mut Vec<HeaderIssue>) {
        let key = Self::find_header_case_insensitive(headers, "expect-staple");

        if key.is_some() {
            issues.push(HeaderIssue {
                header_name: "Expect-Staple".to_string(),
                severity: IssueSeverity::Info,
                issue_type: IssueType::Deprecated,
                description: "Expect-Staple header was never standardized".to_string(),
                recommendation: "Remove this header".to_string(),
                preload_status: None,
            });
        }
    }

    /// Check CORS headers
    fn check_cors(headers: &HashMap<String, String>, issues: &mut Vec<HeaderIssue>) {
        let key = Self::find_header_case_insensitive(headers, "access-control-allow-origin");

        if let Some((_, value)) = key
            && value == "*"
        {
            // Check if credentials are allowed
            if let Some((_, creds)) =
                Self::find_header_case_insensitive(headers, "access-control-allow-credentials")
                && creds.to_lowercase() == "true"
            {
                issues.push(HeaderIssue {
                    header_name: "Access-Control-Allow-Origin".to_string(),
                    severity: IssueSeverity::High,
                    issue_type: IssueType::Insecure,
                    description: "CORS allows credentials with wildcard origin".to_string(),
                    recommendation: "Do not use '*' with Access-Control-Allow-Credentials: true"
                        .to_string(),
                    preload_status: None,
                });
            }

            issues.push(HeaderIssue {
                header_name: "Access-Control-Allow-Origin".to_string(),
                severity: IssueSeverity::Medium,
                issue_type: IssueType::Weak,
                description: "CORS allows all origins with '*'".to_string(),
                recommendation: "Specify allowed origins explicitly".to_string(),
                preload_status: None,
            });
        }
    }

    /// Helper: Find header case-insensitively
    fn find_header_case_insensitive<'a>(
        headers: &'a HashMap<String, String>,
        name: &str,
    ) -> Option<(&'a String, &'a String)> {
        let name_lower = name.to_lowercase();
        headers.iter().find(|(k, _)| k.to_lowercase() == name_lower)
    }

    /// Helper: Extract directive value from header
    fn extract_directive<'a>(value: &'a str, directive: &str) -> Option<&'a str> {
        let directive_lower = directive.to_lowercase();
        for part in value.split(';') {
            let part = part.trim();
            if let Some((key, val)) = part.split_once('=')
                && key.trim().to_lowercase() == directive_lower
            {
                return Some(val.trim());
            }
        }
        None
    }
}

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

    #[test]
    fn test_missing_hsts() {
        let headers = HashMap::new();
        let issues = SecurityHeaderChecker::check_all_headers(&headers);

        assert!(
            issues
                .iter()
                .any(|i| i.header_name == "Strict-Transport-Security")
        );
    }

    #[test]
    fn test_weak_hsts() {
        let mut headers = HashMap::new();
        headers.insert(
            "Strict-Transport-Security".to_string(),
            "max-age=3600".to_string(),
        );

        let issues = SecurityHeaderChecker::check_all_headers(&headers);
        let hsts_issues: Vec<_> = issues
            .iter()
            .filter(|i| i.header_name == "Strict-Transport-Security")
            .collect();

        assert!(
            hsts_issues
                .iter()
                .any(|i| matches!(i.issue_type, IssueType::Weak))
        );
    }

    #[test]
    fn test_unsafe_csp() {
        let mut headers = HashMap::new();
        headers.insert(
            "Content-Security-Policy".to_string(),
            "default-src 'self' 'unsafe-inline'".to_string(),
        );

        let issues = SecurityHeaderChecker::check_all_headers(&headers);
        let csp_issues: Vec<_> = issues
            .iter()
            .filter(|i| i.header_name == "Content-Security-Policy")
            .collect();

        assert!(
            csp_issues
                .iter()
                .any(|i| matches!(i.issue_type, IssueType::Insecure))
        );
    }
}