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
//! Configuration types for `ICOokForms`

use serde::{Deserialize, Serialize};
use std::time::Duration;

/// Main configuration for `ICOokForms`
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Config {
    /// Scan configuration
    pub scan: ScanConfig,
    /// Analysis configuration  
    pub analysis: AnalysisConfig,
    /// Report configuration
    pub report: ReportConfig,
    /// Storage configuration
    pub storage: StorageConfig,
}

/// Configuration for cookie scanning
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(clippy::struct_excessive_bools)]
pub struct ScanConfig {
    /// Maximum number of pages to crawl
    pub max_pages: usize,

    /// Maximum depth for crawling
    pub max_depth: usize,

    /// Request timeout
    pub timeout: Duration,

    /// User agent string
    pub user_agent: String,

    /// Follow redirects
    pub follow_redirects: bool,

    /// Maximum redirects to follow
    pub max_redirects: usize,

    /// Verify SSL certificates
    pub verify_ssl: bool,

    /// HTTP proxy URL
    pub proxy: Option<String>,

    /// Custom headers
    pub headers: Vec<(String, String)>,

    /// Analyze JavaScript-set cookies
    pub analyze_js_cookies: bool,

    /// Execute JavaScript
    pub execute_javascript: bool,

    /// Cookie domains to include (empty = all)
    pub include_domains: Vec<String>,

    /// Cookie domains to exclude
    pub exclude_domains: Vec<String>,

    /// Rate limiting (requests per second)
    pub rate_limit: Option<u32>,

    /// Concurrent requests
    pub concurrency: usize,
}

impl Default for ScanConfig {
    fn default() -> Self {
        Self {
            max_pages: 100,
            max_depth: 3,
            timeout: Duration::from_secs(30),
            user_agent: "ICOokForms/1.0".to_string(),
            follow_redirects: true,
            max_redirects: 5,
            verify_ssl: true,
            proxy: None,
            headers: Vec::new(),
            analyze_js_cookies: true,
            execute_javascript: false,
            include_domains: Vec::new(),
            exclude_domains: Vec::new(),
            rate_limit: None,
            concurrency: 4,
        }
    }
}

/// Configuration for cookie analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(clippy::struct_excessive_bools)]
pub struct AnalysisConfig {
    /// Enable security analysis
    pub security_enabled: bool,

    /// Enable compliance checking
    pub compliance_enabled: bool,

    /// Enable tracking detection
    pub tracking_enabled: bool,

    /// Enable fingerprinting detection
    pub fingerprinting_enabled: bool,

    /// Enable supply chain analysis
    pub supply_chain_enabled: bool,

    /// Minimum severity to report
    pub min_severity: crate::types::Severity,

    /// Categories to analyze
    pub categories: Vec<AnalysisCategory>,

    /// Regulations to check
    pub regulations: Vec<crate::types::Regulation>,

    /// Maximum session lifetime (seconds) before flagging
    pub max_session_lifetime: i64,

    /// Warn on cookies without Secure flag
    pub warn_no_secure: bool,

    /// Warn on cookies without `HttpOnly` flag
    pub warn_no_httponly: bool,

    /// Warn on cookies without `SameSite`
    pub warn_no_samesite: bool,

    /// Enable strict mode (all warnings become errors)
    pub strict_mode: bool,
}

/// Analysis category
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum AnalysisCategory {
    /// Security analysis
    Security,
    /// Compliance checking
    Compliance,
    /// Privacy analysis
    Privacy,
    /// Performance analysis
    Performance,
    /// Best practices
    BestPractices,
}

impl Default for AnalysisConfig {
    fn default() -> Self {
        Self {
            security_enabled: true,
            compliance_enabled: true,
            tracking_enabled: true,
            fingerprinting_enabled: true,
            supply_chain_enabled: true,
            min_severity: crate::types::Severity::Low,
            categories: vec![
                AnalysisCategory::Security,
                AnalysisCategory::Compliance,
                AnalysisCategory::Privacy,
                AnalysisCategory::BestPractices,
            ],
            regulations: vec![crate::types::Regulation::GDPR],
            max_session_lifetime: 3600, // 1 hour
            warn_no_secure: true,
            warn_no_httponly: true,
            warn_no_samesite: true,
            strict_mode: false,
        }
    }
}

/// Configuration for report generation
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(clippy::struct_excessive_bools)]
pub struct ReportConfig {
    /// Output format
    pub format: ReportFormat,

    /// Output file path
    pub output_path: Option<String>,

    /// Include summary
    pub include_summary: bool,

    /// Include detailed analysis
    pub include_details: bool,

    /// Include recommendations
    pub include_recommendations: bool,

    /// Include raw cookie data
    pub include_raw_data: bool,

    /// Group by domain
    pub group_by_domain: bool,

    /// Group by category
    pub group_by_category: bool,

    /// Sort by severity
    pub sort_by_severity: bool,

    /// Maximum issues to include (0 = all)
    pub max_issues: usize,

    /// Template file for custom reports
    pub template_path: Option<String>,

    /// Report title
    pub title: String,

    /// Report author
    pub author: Option<String>,

    /// Include timestamp
    pub include_timestamp: bool,

    /// Include version info
    pub include_version: bool,
}

/// Report format
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum ReportFormat {
    /// JSON format
    Json,
    /// YAML format
    Yaml,
    /// CSV format
    Csv,
    /// HTML format
    Html,
    /// PDF format
    Pdf,
    /// Markdown format
    Markdown,
    /// Plain text
    Text,
}

impl Default for ReportConfig {
    fn default() -> Self {
        Self {
            format: ReportFormat::Json,
            output_path: None,
            include_summary: true,
            include_details: true,
            include_recommendations: true,
            include_raw_data: false,
            group_by_domain: true,
            group_by_category: true,
            sort_by_severity: true,
            max_issues: 0,
            template_path: None,
            title: "ICOokForms Cookie Analysis Report".to_string(),
            author: None,
            include_timestamp: true,
            include_version: true,
        }
    }
}

/// Configuration for storage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageConfig {
    /// Database path
    pub database_path: String,

    /// Enable caching
    pub cache_enabled: bool,

    /// Cache TTL in seconds
    pub cache_ttl: u64,

    /// Maximum cache size in MB
    pub max_cache_size: usize,

    /// Store scan history
    pub store_history: bool,

    /// Maximum history entries
    pub max_history: usize,

    /// Auto-cleanup old entries
    pub auto_cleanup: bool,

    /// Cleanup interval in days
    pub cleanup_days: u32,
}

impl Default for StorageConfig {
    fn default() -> Self {
        Self {
            database_path: "./icookforms.db".to_string(),
            cache_enabled: true,
            cache_ttl: 3600,
            max_cache_size: 100,
            store_history: true,
            max_history: 1000,
            auto_cleanup: true,
            cleanup_days: 30,
        }
    }
}

impl Config {
    /// Create a new configuration with default values
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Load configuration from file
    #[must_use = "Configuration loading failure must be handled"]
    pub fn from_file(path: &str) -> crate::types::Result<Self> {
        let content = std::fs::read_to_string(path)?;

        if std::path::Path::new(path)
            .extension()
            .is_some_and(|ext| ext.eq_ignore_ascii_case("json"))
        {
            Ok(serde_json::from_str(&content)?)
        } else if std::path::Path::new(path)
            .extension()
            .is_some_and(|ext| ext.eq_ignore_ascii_case("yaml"))
            || std::path::Path::new(path)
                .extension()
                .is_some_and(|ext| ext.eq_ignore_ascii_case("yml"))
        {
            Ok(serde_yaml::from_str(&content)?)
        } else if std::path::Path::new(path)
            .extension()
            .is_some_and(|ext| ext.eq_ignore_ascii_case("toml"))
        {
            Ok(toml::from_str(&content)
                .map_err(|e| crate::types::Error::config(format!("TOML parse error: {e}")))?)
        } else {
            Err(crate::types::Error::config(
                "Unsupported config file format",
            ))
        }
    }

    /// Save configuration to file
    pub fn save_to_file(&self, path: &str) -> crate::types::Result<()> {
        let content = if std::path::Path::new(path)
            .extension()
            .is_some_and(|ext| ext.eq_ignore_ascii_case("json"))
        {
            serde_json::to_string_pretty(self)?
        } else if std::path::Path::new(path)
            .extension()
            .is_some_and(|ext| ext.eq_ignore_ascii_case("yaml"))
            || std::path::Path::new(path)
                .extension()
                .is_some_and(|ext| ext.eq_ignore_ascii_case("yml"))
        {
            serde_yaml::to_string(self)?
        } else if std::path::Path::new(path)
            .extension()
            .is_some_and(|ext| ext.eq_ignore_ascii_case("toml"))
        {
            toml::to_string_pretty(self)
                .map_err(|e| crate::types::Error::config(format!("TOML serialize error: {e}")))?
        } else {
            return Err(crate::types::Error::config(
                "Unsupported config file format",
            ));
        };

        std::fs::write(path, content)?;
        Ok(())
    }

    /// Validate configuration
    #[must_use = "Configuration validation result must be checked"]
    pub fn validate(&self) -> crate::types::Result<()> {
        // Validate scan config
        if self.scan.max_pages == 0 {
            return Err(crate::types::Error::config("max_pages must be > 0"));
        }

        if self.scan.max_depth == 0 {
            return Err(crate::types::Error::config("max_depth must be > 0"));
        }

        if self.scan.concurrency == 0 {
            return Err(crate::types::Error::config("concurrency must be > 0"));
        }

        // Validate analysis config
        if self.analysis.categories.is_empty() {
            return Err(crate::types::Error::config(
                "At least one analysis category must be enabled",
            ));
        }

        if self.analysis.max_session_lifetime == 0 {
            return Err(crate::types::Error::config(
                "max_session_lifetime must be > 0",
            ));
        }

        // Validate storage config
        if self.storage.cache_ttl == 0 {
            return Err(crate::types::Error::config("cache_ttl must be > 0"));
        }

        Ok(())
    }
}

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

    #[test]
    fn test_default_config() {
        let config = Config::default();
        assert!(config.scan.max_pages > 0);
        assert!(config.analysis.security_enabled);
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_config_validation() {
        let mut config = Config::default();
        config.scan.max_pages = 0;
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_scan_config_defaults() {
        let config = ScanConfig::default();
        assert_eq!(config.max_pages, 100);
        assert_eq!(config.max_depth, 3);
        assert!(config.verify_ssl);
    }

    #[test]
    fn test_analysis_config_defaults() {
        let config = AnalysisConfig::default();
        assert!(config.security_enabled);
        assert!(config.compliance_enabled);
        assert!(!config.categories.is_empty());
    }

    #[test]
    fn test_report_format() {
        let format = ReportFormat::Json;
        assert_eq!(format, ReportFormat::Json);
    }
}