scanclient 0.2.0

High-performance, observable HTTP client for Santh security scanners
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
//! Adversarial tests for scanclient - designed to BREAK the code
//!
//! Tests: config with 0 timeout, 100 custom headers, proxy with auth,
//! TOML with unknown fields, empty user agent

#![allow(
    clippy::manual_string_new,
    clippy::match_same_arms,
    clippy::needless_raw_string_hashes,
    clippy::uninlined_format_args
)]

use crate::{client::ScanClient, config::HttpConfig, Error};
use std::collections::HashMap;

/// Test config with 0 timeout
#[test]
fn adversarial_zero_timeout_config() {
    let config = HttpConfig {
        timeout_secs: 0,         // Zero timeout
        connect_timeout_secs: 0, // Zero connect timeout
        ..HttpConfig::default()
    };

    let result = ScanClient::from_config(config);
    match result {
        Err(Error::InvalidTimeout {
            field: "timeout_secs",
            value: 0,
            ..
        }) => {}
        other => panic!("expected InvalidTimeout for timeout_secs, got {other:?}"),
    }
}

/// Test config with very large timeout
#[test]
fn adversarial_huge_timeout_config() {
    let config = HttpConfig {
        timeout_secs: u64::MAX,         // Max timeout
        connect_timeout_secs: u64::MAX, // Max connect timeout
        ..HttpConfig::default()
    };

    let result = ScanClient::from_config(config);
    match result {
        Err(Error::InvalidTimeout {
            field: "timeout_secs",
            value,
            ..
        }) if value == u64::MAX => {}
        other => panic!("expected huge timeout to be rejected, got {other:?}"),
    }
}

/// Test with 100 custom headers
#[test]
fn adversarial_100_custom_headers() {
    let mut custom_headers = HashMap::new();

    for i in 0..100 {
        custom_headers.insert(format!("X-Custom-Header-{}", i), format!("value-{}", i));
    }

    let config = HttpConfig {
        custom_headers,
        ..HttpConfig::default()
    };

    let result = ScanClient::from_config(config);
    assert!(result.is_ok(), "Should build client with 100 headers");
}

/// Test with headers that have special characters
#[test]
fn adversarial_headers_special_chars() {
    let mut custom_headers = HashMap::new();

    // Headers with various special characters
    custom_headers.insert("X-Test".to_string(), "value with spaces".to_string());
    custom_headers.insert("X-Unicode".to_string(), "日本語".to_string());
    custom_headers.insert("X-Newline".to_string(), "line1\nline2".to_string());
    custom_headers.insert("X-Tab".to_string(), "col1\tcol2".to_string());
    custom_headers.insert("X-Null".to_string(), "val\x00ue".to_string());

    let config = HttpConfig {
        custom_headers,
        ..HttpConfig::default()
    };

    let result = ScanClient::from_config(config);
    match result {
        Err(Error::InvalidHeaderValue { name }) => assert_eq!(name, "X-Newline"),
        other => panic!("expected newline header value to be rejected, got {other:?}"),
    }
}

/// Test proxy with authentication
#[test]
fn adversarial_proxy_with_auth() {
    let config = HttpConfig {
        proxy: Some("http://user:password@proxy.example.com:8080".to_string()),
        ..HttpConfig::default()
    };

    let result = ScanClient::from_config(config);
    assert!(result.is_ok(), "proxy URLs with auth should parse");
}

/// Test various proxy formats
#[test]
fn adversarial_proxy_formats() {
    let proxies = vec![
        ("http://proxy.example.com:8080", true),
        ("https://proxy.example.com:8080", true),
        ("socks5://proxy.example.com:1080", true),
        ("http://user:pass@proxy:8080", true),
        ("http://user:@proxy:8080", true),
        ("http://:pass@proxy:8080", true),
        ("proxy.example.com:8080", false),
        ("http://proxy", true),
        ("http://[::1]:8080", true),
        ("", false),
        ("not-a-valid-proxy-url", false),
    ];

    for (proxy, should_accept) in &proxies {
        let config = HttpConfig {
            proxy: Some((*proxy).to_string()),
            ..HttpConfig::default()
        };

        let result = ScanClient::from_config(config);
        assert_eq!(
            result.is_ok(),
            *should_accept,
            "unexpected proxy validation result for {proxy}"
        );
    }
}

/// Test TOML with unknown fields
#[test]
fn adversarial_toml_unknown_fields() {
    let toml = r#"
timeout_secs = 30
max_retries = 5
unknown_field = "this doesn't exist"
another_unknown = 123

[nested_section]
key = "value"

[[array_of_tables]]
name = "test"
"#;

    let result = HttpConfig::from_toml(toml);
    assert!(result.is_err(), "unknown TOML fields must be rejected");
}

/// Test empty user agent
#[test]
fn adversarial_empty_user_agent() {
    let config = HttpConfig {
        user_agent: "".to_string(),
        ..HttpConfig::default()
    };

    let result = ScanClient::from_config(config);
    match result {
        Err(Error::InvalidUserAgent) => {}
        other => panic!("expected empty user agent to be rejected, got {other:?}"),
    }
}

/// Test very long user agent
#[test]
fn adversarial_long_user_agent() {
    let config = HttpConfig {
        user_agent: "A".repeat(10_000),
        ..HttpConfig::default()
    };

    let result = ScanClient::from_config(config);
    let _ = result;
}

/// Test TOML with invalid types
#[test]
fn adversarial_toml_invalid_types() {
    let cases = vec![
        (
            "timeout_secs = \"not a number\"",
            "string instead of number",
        ),
        ("max_retries = true", "bool instead of number"),
        ("tls_verify = \"yes\"", "string instead of bool"),
        (
            "custom_headers = \"not an object\"",
            "string instead of table",
        ),
    ];

    for (toml, desc) in &cases {
        let result = HttpConfig::from_toml(toml);
        assert!(result.is_err(), "invalid TOML types should fail for {desc}");
    }
}

/// Test config with max redirects = 0
#[test]
fn adversarial_zero_max_redirects() {
    let config = HttpConfig {
        max_redirects: 0,
        ..HttpConfig::default()
    };

    let result = ScanClient::from_config(config);
    assert!(result.is_ok());
}

/// Test config with negative-like values (u64 overflow edge cases)
#[test]
fn adversarial_config_overflow_values() {
    // These are edge cases that might cause issues
    let config = HttpConfig {
        timeout_secs: 1, // Very short
        connect_timeout_secs: 1,
        retry_delay_ms: 0,           // No delay
        rate_limit_per_sec: Some(0), // Zero rate limit
        max_redirects: usize::MAX,   // Huge redirect limit
        max_retries: u32::MAX,       // Huge retry count
        ..HttpConfig::default()
    };

    let result = ScanClient::from_config(config);
    match result {
        Err(Error::InvalidRetryDelay { value: 0, .. }) => {}
        other => panic!("expected zero retry delay to be rejected, got {other:?}"),
    }
}

/// Test TOML with duplicate keys
#[test]
fn adversarial_toml_duplicate_keys() {
    let toml = r#"
timeout_secs = 10
timeout_secs = 20
timeout_secs = 30
"#;

    let result = HttpConfig::from_toml(toml);
    // May use first, last, or error
    if let Ok(config) = result {
        // Should have one of the values
        assert!(
            config.timeout_secs == 10 || config.timeout_secs == 20 || config.timeout_secs == 30
        );
    }
}

/// Test TOML with malformed structure
#[test]
fn adversarial_toml_malformed() {
    let cases = vec![
        "{{invalid toml",
        "[unclosed[section",
        "timeout_secs =",
        "= 10",
        "timeout_secs: 10",                   // YAML syntax in TOML
        "timeout_secs = 10; max_retries = 5", // Semicolons
    ];

    for toml in &cases {
        let result = HttpConfig::from_toml(toml);
        assert!(result.is_err(), "malformed TOML must fail: {toml}");
    }
}

/// Test config with unicode in headers
#[test]
fn adversarial_unicode_headers() {
    let mut custom_headers = HashMap::new();

    // Header values with unicode
    custom_headers.insert("X-Japanese".to_string(), "日本語".to_string());
    custom_headers.insert("X-Russian".to_string(), "Русский".to_string());
    custom_headers.insert("X-Arabic".to_string(), "العربية".to_string());
    custom_headers.insert("X-Emoji".to_string(), "🎉🎊🎁".to_string());
    custom_headers.insert("X-Mixed".to_string(), "Hello世界".to_string());

    let config = HttpConfig {
        custom_headers,
        ..HttpConfig::default()
    };

    let result = ScanClient::from_config(config);
    assert!(
        result.is_ok(),
        "unicode header values should be accepted by reqwest"
    );
}

/// Test rate limit edge cases
#[test]
fn adversarial_rate_limit_edge_cases() {
    let cases = vec![
        (Some(0u32), false),
        (Some(1), false),
        (Some(1000), false),
        (Some(u32::MAX), false),
        (None, true),
    ];

    for (rate_limit, should_build) in cases {
        let config = HttpConfig {
            rate_limit_per_sec: rate_limit,
            ..HttpConfig::default()
        };

        let result = ScanClient::from_config(config);
        assert_eq!(result.is_ok(), should_build);
    }
}

/// Test config save and load roundtrip
#[test]
fn adversarial_config_roundtrip() {
    let original = HttpConfig {
        timeout_secs: 42,
        max_retries: 7,
        proxy: Some("http://proxy:8080".to_string()),
        user_agent: "TestAgent/1.0".to_string(),
        tls_verify: false,
        ..HttpConfig::default()
    };

    // Serialize to TOML
    let toml = original.to_toml().unwrap();

    // Parse back
    let parsed = HttpConfig::from_toml(&toml).unwrap();

    assert_eq!(original.timeout_secs, parsed.timeout_secs);
    assert_eq!(original.max_retries, parsed.max_retries);
    assert_eq!(original.proxy, parsed.proxy);
    assert_eq!(original.user_agent, parsed.user_agent);
    assert_eq!(original.tls_verify, parsed.tls_verify);
}

/// Test TOML with deeply nested structures
#[test]
fn adversarial_toml_deeply_nested() {
    let toml = r#"
[custom_headers]
a = "1"

[custom_headers.nested]
this = "should not work"
"#;

    let result = HttpConfig::from_toml(toml);
    // Nested tables in custom_headers should fail or be ignored
    if let Ok(config) = result {
        // Should have basic headers
        assert!(config.custom_headers.contains_key("a"));
    }
}

/// Test header names with invalid characters
#[test]
fn adversarial_invalid_header_names() {
    let mut custom_headers = HashMap::new();

    // Invalid header names
    custom_headers.insert(" spaces ".to_string(), "value".to_string());
    custom_headers.insert("colons:here".to_string(), "value".to_string());
    custom_headers.insert("new\nline".to_string(), "value".to_string());
    custom_headers.insert("".to_string(), "value".to_string()); // empty name

    let config = HttpConfig {
        custom_headers,
        ..HttpConfig::default()
    };

    let result = ScanClient::from_config(config);
    match result {
        Ok(_) => {}
        Err(Error::InvalidHeaderName { .. }) => {}
        Err(_) => {}
    }
}

/// Test config load from non-existent file
#[test]
fn adversarial_load_missing_file() {
    let result = HttpConfig::load(std::path::Path::new("/nonexistent/path/config.toml"));
    assert!(result.is_err());
}

/// Test config load from directory instead of file
#[test]
fn adversarial_load_directory_as_file() {
    let dir = tempfile::tempdir().unwrap();
    let result = HttpConfig::load(dir.path());
    assert!(result.is_err());
}