domain-status 0.2.1

Concurrent URL status checker that captures comprehensive metadata in SQLite.
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
//! URL validation and normalization utilities.

use log::warn;

/// Maximum URL length (2048 characters) to prevent `DoS` attacks via extremely long URLs.
/// This matches common browser and server limits (e.g., IE, Apache, Nginx default limits).
const MAX_URL_LENGTH: usize = 2048;

/// Validates and normalizes a URL.
///
/// Adds https:// prefix if missing, then validates that the URL is syntactically
/// valid and uses http/https scheme. Rejects URLs longer than `MAX_URL_LENGTH` to prevent `DoS`.
/// Logs a warning and returns None if the URL is invalid, too long, or uses an unsupported scheme.
///
/// # Arguments
///
/// * `url` - The URL string to validate and normalize
///
/// # Returns
///
/// `Some(normalized_url)` if the URL is valid and should be processed, `None` otherwise.
pub fn validate_and_normalize_url(url: &str) -> Option<String> {
    // Check URL length before normalization to prevent DoS
    if url.len() > MAX_URL_LENGTH {
        warn!(
            "Skipping URL exceeding maximum length ({} > {}): {}...",
            url.len(),
            MAX_URL_LENGTH,
            &url[..50.min(url.len())]
        );
        return None;
    }

    // Normalize: add https:// prefix only when no scheme present (avoids https://ftp://... etc.)
    let normalized = if url.contains("://") {
        url.to_string()
    } else {
        format!("https://{url}")
    };

    // Check normalized URL length (after adding https:// prefix, it could exceed limit)
    if normalized.len() > MAX_URL_LENGTH {
        warn!(
            "Skipping normalized URL exceeding maximum length ({} > {}): {}...",
            normalized.len(),
            MAX_URL_LENGTH,
            &normalized[..50.min(normalized.len())]
        );
        return None;
    }

    // Validate: check syntax and scheme
    if let Ok(parsed) = url::Url::parse(&normalized) {
        match parsed.scheme() {
            "http" | "https" => Some(normalized),
            _ => {
                warn!("Skipping unsupported scheme for URL: {url}");
                None
            }
        }
    } else {
        warn!("Skipping invalid URL: {url}");
        None
    }
}

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

    #[test]
    fn test_validate_and_normalize_url_adds_https() {
        let result = validate_and_normalize_url("example.com");
        assert_eq!(result, Some("https://example.com".to_string()));
    }

    #[test]
    fn test_validate_and_normalize_url_preserves_https() {
        let result = validate_and_normalize_url("https://example.com");
        assert_eq!(result, Some("https://example.com".to_string()));
    }

    #[test]
    fn test_validate_and_normalize_url_preserves_http() {
        let result = validate_and_normalize_url("http://example.com");
        assert_eq!(result, Some("http://example.com".to_string()));
    }

    #[test]
    fn test_validate_and_normalize_url_rejects_unsupported_scheme() {
        // URLs with unsupported schemes: the function normalizes first (adds https:// if missing)
        // So "ftp://example.com" becomes "https://ftp://example.com" which parses
        // but has an invalid host. The URL parser may accept it, but it's not a valid URL.
        // The current implementation may accept it if the parser does, so we test the actual behavior.
        // What matters is that clearly invalid URLs are rejected
        let result = validate_and_normalize_url("not a url at all!!!");
        assert_eq!(result, None);
    }

    #[test]
    fn test_validate_and_normalize_url_rejects_invalid_url() {
        let result = validate_and_normalize_url("not a valid url!!!");
        assert_eq!(result, None);
    }

    #[test]
    fn test_validate_and_normalize_url_with_path() {
        let result = validate_and_normalize_url("example.com/path?query=value");
        assert_eq!(
            result,
            Some("https://example.com/path?query=value".to_string())
        );
    }

    #[test]
    fn test_validate_and_normalize_url_with_port() {
        let result = validate_and_normalize_url("example.com:8080");
        assert_eq!(result, Some("https://example.com:8080".to_string()));
    }

    #[test]
    fn test_validate_and_normalize_url_ipv6() {
        // IPv6 addresses
        let result = validate_and_normalize_url("http://[2001:db8::1]");
        assert_eq!(result, Some("http://[2001:db8::1]".to_string()));

        let result = validate_and_normalize_url("[2001:db8::1]");
        assert_eq!(result, Some("https://[2001:db8::1]".to_string()));

        let result = validate_and_normalize_url("https://[2001:db8::1]:8080");
        assert_eq!(result, Some("https://[2001:db8::1]:8080".to_string()));
    }

    #[test]
    fn test_validate_and_normalize_url_ipv6_with_path() {
        let result = validate_and_normalize_url("[2001:db8::1]/path/to/resource");
        assert_eq!(
            result,
            Some("https://[2001:db8::1]/path/to/resource".to_string())
        );
    }

    #[test]
    fn test_validate_and_normalize_url_internationalized_domain() {
        // IDN domains (punycode) - test with a valid punycode domain
        // "münchen.de" in punycode is "xn--mnchen-3ya.de"
        let result = validate_and_normalize_url("http://xn--mnchen-3ya.de");
        // Should work if punycode is valid
        if let Some(url_str) = &result {
            assert!(url_str.starts_with("http://"));
        }

        // IDN with Unicode - URL parser behavior may vary
        // The URL parser may or may not automatically convert Unicode to punycode
        // Test that it either succeeds or fails gracefully
        let result = validate_and_normalize_url("http://例え.テスト");
        // If it parses, it should be a valid URL; if not, it should be None
        if let Some(url_str) = &result {
            // If it succeeded, verify it's a valid URL
            assert!(url_str.starts_with("http://"));
        }
        // Either Some or None is acceptable depending on URL parser implementation
    }

    #[test]
    fn test_validate_and_normalize_url_complex_paths() {
        // Complex paths with special characters
        let result = validate_and_normalize_url("example.com/path/to/resource?key=value&other=123");
        assert_eq!(
            result,
            Some("https://example.com/path/to/resource?key=value&other=123".to_string())
        );

        let result = validate_and_normalize_url("example.com/path#fragment");
        assert_eq!(
            result,
            Some("https://example.com/path#fragment".to_string())
        );
    }

    #[test]
    fn test_validate_and_normalize_url_unsupported_schemes() {
        // URLs that already contain "://" are left as-is (no https:// prepended),
        // then parsed and rejected by scheme check — no malformed "https://ftp://..."
        assert_eq!(validate_and_normalize_url("ftp://example.com"), None);
        assert_eq!(validate_and_normalize_url("file:///path/to/file"), None);

        // mailto: has no "://" so it still gets https:// prepended; parser may accept or reject
        let mailto_result = validate_and_normalize_url("mailto:test@example.com");
        if let Some(ref u) = mailto_result {
            assert!(u.starts_with("http://") || u.starts_with("https://"));
        }

        // Test that http/https schemes are accepted
        assert_eq!(
            validate_and_normalize_url("http://example.com"),
            Some("http://example.com".to_string())
        );
        assert_eq!(
            validate_and_normalize_url("https://example.com"),
            Some("https://example.com".to_string())
        );
    }

    /// Kills: `validate_and_normalize_url` accepting a scheme-only `https://`
    /// (returning `Some`) instead of `None`.
    #[test]
    fn test_validate_and_normalize_url_edge_cases() {
        assert_eq!(validate_and_normalize_url(""), None);
        assert_eq!(validate_and_normalize_url("   "), None);
        assert_eq!(validate_and_normalize_url("https://"), None);
    }

    #[test]
    fn test_validate_and_normalize_url_subdomain() {
        let result = validate_and_normalize_url("subdomain.example.com");
        assert_eq!(result, Some("https://subdomain.example.com".to_string()));

        let result = validate_and_normalize_url("www.example.com");
        assert_eq!(result, Some("https://www.example.com".to_string()));
    }

    #[test]
    fn test_validate_and_normalize_url_with_userinfo() {
        // URLs with userinfo are accepted (though credentials are not used)
        let result = validate_and_normalize_url("https://user:pass@example.com");
        assert_eq!(result, Some("https://user:pass@example.com".to_string()));

        // Test with username only
        let result = validate_and_normalize_url("https://user@example.com");
        assert_eq!(result, Some("https://user@example.com".to_string()));
    }

    #[test]
    fn test_validate_and_normalize_url_http_preserved() {
        // HTTP should be preserved (not converted to HTTPS)
        let result = validate_and_normalize_url("http://example.com");
        assert_eq!(result, Some("http://example.com".to_string()));
        assert!(!result.unwrap().starts_with("https://"));
    }

    #[test]
    fn test_validate_and_normalize_url_https_preserved() {
        // HTTPS should be preserved
        let result = validate_and_normalize_url("https://example.com");
        assert_eq!(result, Some("https://example.com".to_string()));
    }

    /// Kills: `validate_and_normalize_url` accepting a scheme-only `http://`
    /// (returning `Some`) instead of `None`.
    #[test]
    fn test_validate_and_normalize_url_malformed() {
        assert_eq!(validate_and_normalize_url("://example.com"), None);
        assert_eq!(validate_and_normalize_url("http://"), None);
    }

    /// Kills: `validate_and_normalize_url` decoding `+` in the path (or
    /// dropping the path) instead of preserving `path+with+plus`.
    #[test]
    fn test_validate_and_normalize_url_special_characters() {
        assert_eq!(
            validate_and_normalize_url("example.com/path%20with%20spaces"),
            Some("https://example.com/path%20with%20spaces".to_string())
        );
        assert_eq!(
            validate_and_normalize_url("example.com/path+with+plus"),
            Some("https://example.com/path+with+plus".to_string())
        );
    }

    #[test]
    fn test_validate_and_normalize_url_rejects_too_long_url() {
        // Create a URL that exceeds MAX_URL_LENGTH (2048 chars)
        let long_path = "a".repeat(2100);
        let long_url = format!("https://example.com/{}", long_path);
        let result = validate_and_normalize_url(&long_url);
        assert_eq!(result, None, "Should reject URL exceeding maximum length");
    }

    #[test]
    fn test_validate_and_normalize_url_accepts_url_at_limit() {
        // Create a URL exactly at the limit (2048 chars)
        // "https://example.com/" is 20 chars, so path can be 2028 chars (20 + 2028 = 2048)
        let path = "a".repeat(2028);
        let url_at_limit = format!("https://example.com/{}", path);
        // Verify it's exactly at the limit
        assert_eq!(
            url_at_limit.len(),
            2048,
            "URL should be exactly 2048 characters"
        );
        let result = validate_and_normalize_url(&url_at_limit);
        assert!(
            result.is_some(),
            "Should accept URL at maximum length (2048 chars)"
        );
    }

    #[test]
    fn test_validate_and_normalize_url_rejects_too_long_url_after_normalization() {
        // URL that's under limit before normalization but exceeds it after adding https://
        let path = "a".repeat(2045); // 2045 + 8 (https://) = 2053 > 2048
        let url = format!("example.com/{}", path);
        let result = validate_and_normalize_url(&url);
        assert_eq!(
            result, None,
            "Should reject URL that exceeds limit after normalization"
        );
    }

    // Characterization: fuzz-shaped Some/no-panic guards, not exact-string contracts.
    use proptest::prelude::*;

    proptest! {
        #[test]
        fn test_url_normalization_idempotent(url in "[a-z]{3,20}\\.[a-z]{2,5}") {
            let normalized1 = validate_and_normalize_url(&url);
            if let Some(n1) = normalized1 {
                let normalized2 = validate_and_normalize_url(&n1);
                prop_assert_eq!(Some(n1.clone()), normalized2,
                    "Normalizing twice should produce same result");
            }
        }

        #[test]
        fn test_url_length_validation(
            domain in "[a-z]{3,20}\\.[a-z]{2,5}",
            path in prop::collection::vec("[a-z]{1,10}", 0..200)
        ) {
            let url = format!("https://{}/{}", domain, path.join("/"));
            let result = validate_and_normalize_url(&url);

            if url.len() <= 2048 {
                prop_assert!(result.is_some(),
                    "Valid URL under limit should normalize successfully");
            } else {
                prop_assert!(result.is_none(),
                    "URL over 2048 chars should be rejected");
            }
        }

        #[test]
        fn test_url_scheme_handling(domain in "[a-z]{3,20}\\.[a-z]{2,5}") {
            // URLs without scheme should get https:// prefix
            let no_scheme = validate_and_normalize_url(&domain);
            prop_assert!(no_scheme.is_some());
            prop_assert!(no_scheme.unwrap().starts_with("https://"));

            // HTTP URLs should preserve scheme
            let http_url = format!("http://{}", domain);
            let with_http = validate_and_normalize_url(&http_url);
            prop_assert!(with_http.is_some());
            prop_assert!(with_http.unwrap().starts_with("http://"));
        }

        #[test]
        fn test_url_special_chars_no_panic(
            domain in "[a-z]{3,20}\\.[a-z]{2,5}",
            path in "[^/]{0,100}"
        ) {
            let url = format!("https://{}/{}", domain, path);
            // Should not panic on any input
            let _result = validate_and_normalize_url(&url);
        }

        #[test]
        fn test_url_with_query_and_fragment(
            domain in "[a-z]{3,20}\\.[a-z]{2,5}",
            query in "[a-z]{0,50}",
            fragment in "[a-z]{0,50}"
        ) {
            let url = format!("{}?query={}&key=value#{}", domain, query, fragment);
            let result = validate_and_normalize_url(&url);

            if let Some(normalized) = result {
                prop_assert!(normalized.starts_with("https://"));
                prop_assert!(normalized.contains(&domain));
            }
        }

        #[test]
        fn test_url_port_validation(
            domain in "[a-z]{3,20}\\.[a-z]{2,5}",
            port in 1u16..=65535
        ) {
            let url = format!("{}:{}", domain, port);
            let result = validate_and_normalize_url(&url);

            // Should normalize successfully for valid ports
            prop_assert!(result.is_some());
            if let Some(normalized) = result {
                prop_assert!(normalized.contains(&port.to_string()));
            }
        }

        #[test]
        fn test_url_subdomain_normalization(
            subdomain in "[a-z]{2,10}",
            domain in "[a-z]{3,15}",
            tld in "(com|org|net)"
        ) {
            let url = format!("{}.{}.{}", subdomain, domain, tld);
            let result = validate_and_normalize_url(&url);

            prop_assert!(result.is_some());
            if let Some(normalized) = result {
                prop_assert!(normalized.starts_with("https://"));
                prop_assert!(normalized.contains(&subdomain));
                prop_assert!(normalized.contains(&domain));
                prop_assert!(normalized.contains(&tld));
            }
        }
    }
}