icookforms 0.1.0

The World's Reference Cookie Audit Software - Complete Security & Compliance Analysis
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
//! Issue types for security and compliance problems

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

/// Severity level of an issue
/// Order is from most severe to least severe for correct `Ord` implementation
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Severity {
    /// Info - Informational only
    Info,
    /// Low - Nice to fix
    Low,
    /// Medium - Should be addressed
    Medium,
    /// High - Should be fixed as soon as possible
    High,
    /// Critical - Must be fixed immediately
    Critical,
}

impl fmt::Display for Severity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Critical => write!(f, "CRITICAL"),
            Self::High => write!(f, "HIGH"),
            Self::Medium => write!(f, "MEDIUM"),
            Self::Low => write!(f, "LOW"),
            Self::Info => write!(f, "INFO"),
        }
    }
}

impl Severity {
    /// Get numeric score for severity (higher = more severe)
    #[must_use]
    pub fn score(&self) -> u8 {
        match self {
            Self::Critical => 100,
            Self::High => 75,
            Self::Medium => 50,
            Self::Low => 25,
            Self::Info => 0,
        }
    }

    /// Get emoji representation
    #[must_use]
    pub fn emoji(&self) -> &'static str {
        match self {
            Self::Critical => "🔴",
            Self::High => "🟠",
            Self::Medium => "🟡",
            Self::Low => "🔵",
            Self::Info => "⚪",
        }
    }

    /// Get color code (for terminal output)
    #[must_use]
    pub fn color(&self) -> &'static str {
        match self {
            Self::Critical => "red",
            Self::High => "orange",
            Self::Medium => "yellow",
            Self::Low => "blue",
            Self::Info => "white",
        }
    }
}

/// Category of an issue
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum IssueCategory {
    /// Security vulnerability
    Security,
    /// Compliance violation
    Compliance,
    /// Privacy concern
    Privacy,
    /// Performance issue
    Performance,
    /// Best practice violation
    BestPractice,
    /// Configuration problem
    Configuration,
}

impl fmt::Display for IssueCategory {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Security => write!(f, "Security"),
            Self::Compliance => write!(f, "Compliance"),
            Self::Privacy => write!(f, "Privacy"),
            Self::Performance => write!(f, "Performance"),
            Self::BestPractice => write!(f, "Best Practice"),
            Self::Configuration => write!(f, "Configuration"),
        }
    }
}

/// Generic issue structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Issue {
    /// Unique identifier
    pub id: String,

    /// Issue severity
    pub severity: Severity,

    /// Issue category
    pub category: IssueCategory,

    /// Short title
    pub title: String,

    /// Detailed description
    pub description: String,

    /// Cookie name(s) affected
    pub affected_cookies: Vec<String>,

    /// URL(s) where issue was found
    pub affected_urls: Vec<String>,

    /// Recommendations for fixing
    pub recommendations: Vec<String>,

    /// Reference links (RFC, OWASP, etc.)
    pub references: Vec<String>,

    /// CWE ID (Common Weakness Enumeration)
    pub cwe_id: Option<String>,

    /// CVE ID (Common Vulnerabilities and Exposures)
    pub cve_id: Option<String>,

    /// OWASP category
    pub owasp_category: Option<String>,

    /// Confidence level (0-100)
    pub confidence: u8,

    /// False positive likelihood (0-100)
    pub false_positive_likelihood: u8,
}

impl Issue {
    /// Create a new issue
    pub fn new(
        severity: Severity,
        category: IssueCategory,
        title: impl Into<String>,
        description: impl Into<String>,
    ) -> Self {
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            severity,
            category,
            title: title.into(),
            description: description.into(),
            affected_cookies: Vec::new(),
            affected_urls: Vec::new(),
            recommendations: Vec::new(),
            references: Vec::new(),
            cwe_id: None,
            cve_id: None,
            owasp_category: None,
            confidence: 100,
            false_positive_likelihood: 0,
        }
    }

    /// Add affected cookie
    #[must_use]
    pub fn add_cookie(mut self, cookie: impl Into<String>) -> Self {
        self.affected_cookies.push(cookie.into());
        self
    }

    /// Add affected URL
    #[must_use]
    pub fn add_url(mut self, url: impl Into<String>) -> Self {
        self.affected_urls.push(url.into());
        self
    }

    /// Add recommendation
    #[must_use]
    pub fn add_recommendation(mut self, recommendation: impl Into<String>) -> Self {
        self.recommendations.push(recommendation.into());
        self
    }

    /// Add reference
    #[must_use]
    pub fn add_reference(mut self, reference: impl Into<String>) -> Self {
        self.references.push(reference.into());
        self
    }

    /// Set CWE ID
    #[must_use]
    pub fn with_cwe(mut self, cwe_id: impl Into<String>) -> Self {
        self.cwe_id = Some(cwe_id.into());
        self
    }

    /// Set confidence level
    #[must_use]
    pub fn with_confidence(mut self, confidence: u8) -> Self {
        self.confidence = confidence.min(100);
        self
    }
}

/// Security-specific issue
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityIssue {
    /// Base issue
    pub issue: Issue,

    /// Vulnerability type
    pub vulnerability_type: VulnerabilityType,

    /// Attack vector
    pub attack_vector: Option<AttackVector>,

    /// Exploitability score (0-10, CVSS)
    pub exploitability: f32,

    /// Impact score (0-10, CVSS)
    pub impact: f32,

    /// CVSS score (0-10)
    pub cvss_score: Option<f32>,
}

/// Types of vulnerabilities
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum VulnerabilityType {
    /// Cross-Site Scripting
    XSS,
    /// Cross-Site Request Forgery
    CSRF,
    /// Session Hijacking
    SessionHijacking,
    /// Cookie Injection
    CookieInjection,
    /// Cookie Tampering
    CookieTampering,
    /// Man-in-the-Middle
    MITM,
    /// Session Fixation
    SessionFixation,
    /// Insecure Transmission
    InsecureTransmission,
    /// Weak Cryptography
    WeakCryptography,
    /// Missing Security Headers
    MissingSecurityHeaders,
    /// Supply Chain Attack
    SupplyChainAttack,
}

impl fmt::Display for VulnerabilityType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::XSS => write!(f, "Cross-Site Scripting (XSS)"),
            Self::CSRF => write!(f, "Cross-Site Request Forgery (CSRF)"),
            Self::SessionHijacking => write!(f, "Session Hijacking"),
            Self::CookieInjection => write!(f, "Cookie Injection"),
            Self::CookieTampering => write!(f, "Cookie Tampering"),
            Self::MITM => write!(f, "Man-in-the-Middle (MITM)"),
            Self::SessionFixation => write!(f, "Session Fixation"),
            Self::InsecureTransmission => write!(f, "Insecure Transmission"),
            Self::WeakCryptography => write!(f, "Weak Cryptography"),
            Self::MissingSecurityHeaders => write!(f, "Missing Security Headers"),
            Self::SupplyChainAttack => write!(f, "Supply Chain Attack"),
        }
    }
}

/// Attack vector classification
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum AttackVector {
    /// Network-based attack
    Network,
    /// Adjacent network
    Adjacent,
    /// Local access required
    Local,
    /// Physical access required
    Physical,
}

/// Compliance-specific issue
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComplianceIssue {
    /// Base issue
    pub issue: Issue,

    /// Regulation violated
    pub regulation: crate::types::Regulation,

    /// Specific article/section violated
    pub article: Option<String>,

    /// Legal requirement text
    pub requirement: String,

    /// Potential fine/penalty
    pub penalty: Option<String>,

    /// Required by law
    pub mandatory: bool,

    /// Grace period for compliance
    pub grace_period: Option<String>,
}

impl SecurityIssue {
    /// Create a new security issue
    pub fn new(
        severity: Severity,
        title: impl Into<String>,
        description: impl Into<String>,
        vulnerability_type: VulnerabilityType,
    ) -> Self {
        Self {
            issue: Issue::new(severity, IssueCategory::Security, title, description),
            vulnerability_type,
            attack_vector: None,
            exploitability: 0.0,
            impact: 0.0,
            cvss_score: None,
        }
    }

    /// Calculate CVSS score
    pub fn calculate_cvss(&mut self) {
        // Simplified CVSS v3.1 calculation
        let base_score = (self.exploitability + self.impact) / 2.0;
        self.cvss_score = Some(base_score.clamp(0.0, 10.0));
    }
}

impl ComplianceIssue {
    /// Create a new compliance issue
    pub fn new(
        severity: Severity,
        regulation: crate::types::Regulation,
        title: impl Into<String>,
        description: impl Into<String>,
        requirement: impl Into<String>,
    ) -> Self {
        Self {
            issue: Issue::new(severity, IssueCategory::Compliance, title, description),
            regulation,
            article: None,
            requirement: requirement.into(),
            penalty: None,
            mandatory: true,
            grace_period: None,
        }
    }

    /// Set article/section
    #[must_use]
    pub fn with_article(mut self, article: impl Into<String>) -> Self {
        self.article = Some(article.into());
        self
    }

    /// Set penalty information
    #[must_use]
    pub fn with_penalty(mut self, penalty: impl Into<String>) -> Self {
        self.penalty = Some(penalty.into());
        self
    }
}

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

    #[test]
    fn test_severity_ordering() {
        assert!(Severity::Critical > Severity::High);
        assert!(Severity::High > Severity::Medium);
        assert!(Severity::Medium > Severity::Low);
        assert!(Severity::Low > Severity::Info);
    }

    #[test]
    fn test_severity_score() {
        assert_eq!(Severity::Critical.score(), 100);
        assert_eq!(Severity::High.score(), 75);
        assert_eq!(Severity::Medium.score(), 50);
        assert_eq!(Severity::Low.score(), 25);
        assert_eq!(Severity::Info.score(), 0);
    }

    #[test]
    fn test_issue_creation() {
        let issue = Issue::new(
            Severity::High,
            IssueCategory::Security,
            "Test Issue",
            "Test Description",
        );

        assert_eq!(issue.severity, Severity::High);
        assert_eq!(issue.category, IssueCategory::Security);
        assert_eq!(issue.title, "Test Issue");
        assert!(!issue.id.is_empty());
    }

    #[test]
    fn test_issue_builder() {
        let issue = Issue::new(
            Severity::Critical,
            IssueCategory::Security,
            "XSS Vulnerability",
            "Cookie accessible via JavaScript",
        )
        .add_cookie("session_id")
        .add_url("https://example.com")
        .add_recommendation("Add HttpOnly flag")
        .with_cwe("CWE-79");

        assert_eq!(issue.affected_cookies.len(), 1);
        assert_eq!(issue.affected_urls.len(), 1);
        assert_eq!(issue.recommendations.len(), 1);
        assert_eq!(issue.cwe_id, Some("CWE-79".to_string()));
    }

    #[test]
    fn test_security_issue() {
        let mut issue = SecurityIssue::new(
            Severity::High,
            "XSS via Cookie",
            "Session cookie accessible to JavaScript",
            VulnerabilityType::XSS,
        );

        issue.exploitability = 8.0;
        issue.impact = 7.0;
        issue.calculate_cvss();

        assert!(issue.cvss_score.is_some());
        assert!(issue.cvss_score.unwrap() > 0.0);
    }

    #[test]
    fn test_compliance_issue() {
        let issue = ComplianceIssue::new(
            Severity::High,
            crate::types::Regulation::GDPR,
            "Missing Consent",
            "Analytics cookies set without user consent",
            "Article 6(1)(a) - Consent required for processing",
        )
        .with_article("Art. 6(1)(a)")
        .with_penalty("Up to €20M or 4% of annual revenue");

        assert_eq!(issue.regulation, crate::types::Regulation::GDPR);
        assert!(issue.article.is_some());
        assert!(issue.penalty.is_some());
    }

    #[test]
    fn test_vulnerability_type_display() {
        assert_eq!(
            VulnerabilityType::XSS.to_string(),
            "Cross-Site Scripting (XSS)"
        );
        assert_eq!(
            VulnerabilityType::CSRF.to_string(),
            "Cross-Site Request Forgery (CSRF)"
        );
    }
}