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
//! Cookie scanner for web applications
//!
//! This module implements a complete HTTP cookie scanner using reqwest.
//! It can scan single URLs or crawl entire websites to find cookies.

use crate::types::config::ScanConfig;
use crate::types::report::{ScanResult, ScanStatus};
use crate::types::{Cookie, Error, Result};
use reqwest::{header, Client, Response};
use std::collections::{HashMap, HashSet};
use std::time::Duration;
use tokio::time::timeout;
use tracing::{debug, info, warn};
use url::Url;

/// HTTP cookie scanner
pub struct Scanner {
    config: ScanConfig,
    client: Client,
}

impl Scanner {
    /// Create a new scanner with configuration
    pub fn new(config: ScanConfig) -> Result<Self> {
        let client = build_http_client(&config)?;

        Ok(Self { config, client })
    }

    /// Scan a single URL for cookies
    pub async fn scan(&self, url: &str) -> Result<ScanResult> {
        info!("Starting cookie scan for: {}", url);

        let mut result = ScanResult::new(url);
        result.status = ScanStatus::InProgress;
        result.config = serde_json::to_value(&self.config).unwrap_or(serde_json::Value::Null);

        // Validate URL
        let parsed_url =
            Url::parse(url).map_err(|e| Error::scanner(format!("Invalid URL: {e}")))?;

        // Scan the page
        match self.scan_url(&parsed_url).await {
            Ok(cookies) => {
                result.cookies.extend(cookies);
                result.pages_scanned = 1;
                result.requests_made = 1;
            }
            Err(e) => {
                result.errors.push(format!("Failed to scan URL: {e}"));
                result.status = ScanStatus::Failed;
                return Ok(result);
            }
        }

        result.complete();
        info!("Scan completed. Found {} cookies", result.cookies.len());

        Ok(result)
    }

    /// Crawl a website and scan all pages for cookies
    pub async fn crawl(&self, start_url: &str) -> Result<ScanResult> {
        info!("Starting website crawl from: {}", start_url);

        let mut result = ScanResult::new(start_url);
        result.status = ScanStatus::InProgress;
        result.config = serde_json::to_value(&self.config).unwrap_or(serde_json::Value::Null);

        let start =
            Url::parse(start_url).map_err(|e| Error::scanner(format!("Invalid start URL: {e}")))?;

        let mut visited = HashSet::new();
        let mut to_visit = vec![start.clone()];
        let mut all_cookies = HashMap::new();

        while !to_visit.is_empty() && visited.len() < self.config.max_pages {
            let url = to_visit.pop().unwrap();

            if visited.contains(&url) {
                continue;
            }

            // Check domain restrictions
            if !self.should_scan_url(&url, &start) {
                continue;
            }

            info!("Scanning page: {}", url);
            visited.insert(url.clone());
            result.pages_scanned += 1;

            // Scan the URL
            match self.scan_url(&url).await {
                Ok(cookies) => {
                    result.requests_made += 1;

                    // Store unique cookies
                    for cookie in cookies {
                        let key =
                            format!("{}:{}", cookie.name, cookie.domain.as_deref().unwrap_or(""));
                        all_cookies.insert(key, cookie);
                    }

                    // Extract links for crawling
                    if visited.len() < self.config.max_pages {
                        match self.extract_links(&url).await {
                            Ok(links) => {
                                for link in links {
                                    if !visited.contains(&link) && to_visit.len() < 1000 {
                                        to_visit.push(link);
                                    }
                                }
                            }
                            Err(e) => {
                                warn!("Failed to extract links from {}: {}", url, e);
                            }
                        }
                    }
                }
                Err(e) => {
                    result.errors.push(format!("Failed to scan {url}: {e}"));
                }
            }

            // Rate limiting
            if let Some(rate_limit) = self.config.rate_limit {
                let delay = Duration::from_millis(1000 / u64::from(rate_limit));
                tokio::time::sleep(delay).await;
            }
        }

        // Convert HashMap to Vec
        result.cookies = all_cookies.into_values().collect();

        result.complete();
        info!(
            "Crawl completed. Scanned {} pages, found {} cookies",
            result.pages_scanned,
            result.cookies.len()
        );

        Ok(result)
    }

    /// Scan a single URL and extract cookies
    async fn scan_url(&self, url: &Url) -> Result<Vec<Cookie>> {
        debug!("Fetching URL: {}", url);

        // Make request with timeout
        let response = timeout(self.config.timeout, self.client.get(url.as_str()).send())
            .await
            .map_err(|_| Error::timeout(format!("Request timeout for {url}")))?
            .map_err(Error::Http)?;

        // Extract cookies from response
        Ok(Self::extract_cookies(url, &response))
    }

    /// Extract cookies from HTTP response
    fn extract_cookies(url: &Url, response: &Response) -> Vec<Cookie> {
        let mut cookies = Vec::new();

        // Extract Set-Cookie headers
        for value in response.headers().get_all(header::SET_COOKIE) {
            if let Ok(header_str) = value.to_str() {
                // Parse with strict=false for maximum compatibility
                match crate::parser::parse_set_cookie(header_str, false) {
                    Ok(mut cookie) => {
                        // Set source URL
                        cookie.source_url = Some(url.to_string());

                        // Determine if third-party
                        cookie.is_third_party = is_third_party_cookie(url, &cookie);

                        cookies.push(cookie);
                    }
                    Err(e) => {
                        warn!("Failed to parse cookie: {}", e);
                    }
                }
            }
        }

        debug!("Extracted {} cookies from {}", cookies.len(), url);
        cookies
    }

    /// Extract links from a page for crawling
    async fn extract_links(&self, url: &Url) -> Result<Vec<Url>> {
        use scraper::{Html, Selector};

        let client = &self.client;
        let timeout = self.config.timeout;

        let response = tokio::time::timeout(timeout, client.get(url.clone()).send())
            .await
            .map_err(|_| Error::timeout("Link extraction timeout"))?
            .map_err(Error::Http)?;

        let body = response.text().await.map_err(Error::Http)?;

        // Simple link extraction using scraper
        let document = Html::parse_document(&body);
        let selector = Selector::parse("a[href]").unwrap();

        let mut links = Vec::new();

        for element in document.select(&selector) {
            if let Some(href) = element.value().attr("href") {
                if let Ok(absolute_url) = url.join(href) {
                    links.push(absolute_url);
                }
            }
        }

        debug!("Extracted {} links from {}", links.len(), url);
        Ok(links)
    }

    /// Check if URL should be scanned based on domain restrictions
    fn should_scan_url(&self, url: &Url, start: &Url) -> bool {
        // Check include domains
        if !self.config.include_domains.is_empty() {
            let domain = url.domain().unwrap_or("");
            if !self
                .config
                .include_domains
                .iter()
                .any(|d| domain.contains(d))
            {
                return false;
            }
        }

        // Check exclude domains
        if !self.config.exclude_domains.is_empty() {
            let domain = url.domain().unwrap_or("");
            if self
                .config
                .exclude_domains
                .iter()
                .any(|d| domain.contains(d))
            {
                return false;
            }
        }

        // Stay on same domain by default
        if url.domain() != start.domain() {
            return false;
        }

        true
    }
}

/// Build HTTP client with configuration
fn build_http_client(config: &ScanConfig) -> Result<Client> {
    let mut builder = Client::builder()
        .timeout(config.timeout)
        .user_agent(&config.user_agent)
        .cookie_store(true); // Enable cookie jar

    // Configure redirects
    if config.follow_redirects {
        builder = builder.redirect(reqwest::redirect::Policy::limited(config.max_redirects));
    } else {
        builder = builder.redirect(reqwest::redirect::Policy::none());
    }

    // Configure SSL verification
    if !config.verify_ssl {
        builder = builder.danger_accept_invalid_certs(true);
    }

    // Configure proxy
    if let Some(ref proxy_url) = config.proxy {
        let proxy = reqwest::Proxy::all(proxy_url)
            .map_err(|e| Error::config(format!("Invalid proxy URL: {e}")))?;
        builder = builder.proxy(proxy);
    }

    // Add custom headers
    let mut headers = header::HeaderMap::new();
    for (name, value) in &config.headers {
        if let (Ok(header_name), Ok(header_value)) = (
            header::HeaderName::from_bytes(name.as_bytes()),
            header::HeaderValue::from_str(value),
        ) {
            headers.insert(header_name, header_value);
        }
    }
    builder = builder.default_headers(headers);

    builder
        .build()
        .map_err(|e| Error::scanner(format!("Failed to build HTTP client: {e}")))
}

/// Check if cookie is third-party relative to the URL
fn is_third_party_cookie(url: &Url, cookie: &Cookie) -> bool {
    let page_domain = url.domain().unwrap_or("");

    if let Some(ref cookie_domain) = cookie.domain {
        // Remove leading dot from cookie domain
        let cookie_domain = cookie_domain.trim_start_matches('.');

        // Check if domains match
        !page_domain.ends_with(cookie_domain) && !cookie_domain.ends_with(page_domain)
    } else {
        false
    }
}

/// Scanner builder for convenient configuration
pub struct ScannerBuilder {
    config: ScanConfig,
}

impl ScannerBuilder {
    /// Create a new scanner builder
    #[must_use]
    pub fn new() -> Self {
        Self {
            config: ScanConfig::default(),
        }
    }

    /// Set maximum pages to scan
    #[must_use]
    pub fn max_pages(mut self, max: usize) -> Self {
        self.config.max_pages = max;
        self
    }

    /// Set request timeout
    #[must_use]
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.config.timeout = timeout;
        self
    }

    /// Set user agent
    #[must_use]
    pub fn user_agent(mut self, ua: impl Into<String>) -> Self {
        self.config.user_agent = ua.into();
        self
    }

    /// Enable/disable SSL verification
    #[must_use]
    pub fn verify_ssl(mut self, verify: bool) -> Self {
        self.config.verify_ssl = verify;
        self
    }

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

    /// Set rate limit (requests per second)
    #[must_use]
    pub fn rate_limit(mut self, rps: u32) -> Self {
        self.config.rate_limit = Some(rps);
        self
    }

    /// Build the scanner
    pub fn build(self) -> Result<Scanner> {
        Scanner::new(self.config)
    }
}

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

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

    #[test]
    fn test_scanner_builder() {
        let scanner = ScannerBuilder::new()
            .max_pages(50)
            .timeout(Duration::from_secs(10))
            .user_agent("TestBot/1.0")
            .verify_ssl(false)
            .build();

        assert!(scanner.is_ok());
    }

    #[test]
    fn test_is_third_party() {
        let url = Url::parse("https://example.com/page").unwrap();
        let mut cookie = Cookie::new("test".to_string(), "value".to_string());

        cookie.domain = Some("example.com".to_string());
        assert!(!is_third_party_cookie(&url, &cookie));

        cookie.domain = Some("thirdparty.com".to_string());
        assert!(is_third_party_cookie(&url, &cookie));
    }

    #[tokio::test]
    async fn test_scanner_creation() {
        let scanner = Scanner::new(ScanConfig::default());
        assert!(scanner.is_ok());
    }
}