httpjail 0.6.2

Monitor and restrict HTTP/HTTPS requests from processes
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
484
485
mod common;

use common::{HttpjailCommand, test_https_allow, test_https_blocking};
use serial_test::serial;
use std::process::{Command, Stdio};
use std::str::FromStr;
use std::thread;
use std::time::Duration;

#[test]
fn test_weak_mode_blocks_https_correctly() {
    // Use the common test function with weak mode (no sudo)
    test_https_blocking(false);
}

#[test]
fn test_weak_mode_allows_https_with_allow_rule() {
    // Use the common test function with weak mode (no sudo)
    test_https_allow(false);
}

#[test]
fn test_weak_mode_blocks_http_correctly() {
    // Test that HTTP to ifconfig.me is blocked in weak mode
    let result = HttpjailCommand::new()
        .weak()
        .js("false")
        .verbose(2)
        .command(vec!["curl", "--max-time", "3", "http://ifconfig.me"])
        .execute();

    match result {
        Ok((exit_code, stdout, stderr)) => {
            println!("Exit code: {}", exit_code);
            println!("Stdout: {}", stdout);
            println!("Stderr: {}", stderr);

            // HTTP blocking returns a 403 response, so curl succeeds but with forbidden message
            // Should contain the blocked message
            assert!(
                stdout.contains("Request blocked by httpjail") || exit_code != 0,
                "Expected request to be blocked, but got normal response"
            );

            // Should not contain actual response (IP address)
            assert!(std::net::Ipv4Addr::from_str(stdout.trim()).is_err());
        }
        Err(e) => {
            panic!("Failed to execute httpjail: {}", e);
        }
    }
}

// MacOS does not have `timeout` by default. Other options here could be `gtimeout`
// if coreutils is install or a simple perl script which mac has by default (these suggestions
// are courtesy of claude).
#[cfg(not(target_os = "macos"))]
#[test]
fn test_weak_mode_timeout_works() {
    // Test that the timeout mechanism works correctly (should complete in ~2 seconds)
    // This test uses a command that would normally hang
    let result = HttpjailCommand::new()
        .weak()
        .js("true")
        .verbose(2)
        .timeout(2) // Set 2-second timeout for fast test completion
        .command(vec!["bash", "-c", "sleep 3"]) // Sleep longer than timeout
        .execute();

    match result {
        Ok((exit_code, _stdout, _stderr)) => {
            // Should timeout with exit code 124
            assert_eq!(
                exit_code, 124,
                "Expected timeout exit code 124, got: {}",
                exit_code
            );
        }
        Err(e) => {
            panic!("Failed to execute httpjail: {}", e);
        }
    }
}

#[test]
fn test_weak_mode_allows_localhost() {
    // Test that localhost connections work (for the proxy itself)
    let result = HttpjailCommand::new()
        .weak()
        .js("r.host === 'localhost' || r.host === '127.0.0.1'")
        .verbose(1)
        .command(vec!["curl", "--max-time", "3", "http://localhost:80"])
        // may fail but should be allowed by rules
        .execute();

    match result {
        Ok((exit_code, _stdout, stderr)) => {
            println!("Exit code: {}", exit_code);
            println!("Stderr: {}", stderr);

            // This should fail with connection refused (no server on port 80)
            // but NOT be blocked by the proxy
            // Exit code 7 = Failed to connect (expected - no server)
            // Exit code 28 = Timeout (connection attempt timed out - also valid)
            // Exit code 52 = Empty reply from server (proxy allowed but no backend)
            assert!(
                exit_code == 7 || exit_code == 28 || exit_code == 52,
                "Expected connection refused (7), timeout (28), or empty reply (52), got: {}",
                exit_code
            );

            // The error should be about connection, not forbidden
            assert!(
                !stderr.contains("403") && !stderr.contains("Forbidden"),
                "Request should not be forbidden by proxy"
            );
        }
        Err(e) => {
            panic!("Failed to execute httpjail: {}", e);
        }
    }
}

#[test]
fn test_weak_mode_appends_no_proxy() {
    // Ensure existing NO_PROXY values are preserved and localhost entries appended
    let result = HttpjailCommand::new()
        .weak()
        .js("true")
        .env("NO_PROXY", "example.com")
        .verbose(2)
        .command(vec!["env"])
        .execute();

    match result {
        Ok((exit_code, stdout, _stderr)) => {
            assert_eq!(exit_code, 0, "env command should succeed");

            let mut found_upper = false;
            let mut found_lower = false;

            for line in stdout.lines() {
                if let Some((key, value)) = line.split_once('=') {
                    if key == "NO_PROXY" {
                        found_upper = true;
                        assert!(
                            value.contains("example.com")
                                && value.contains("localhost")
                                && value.contains("127.0.0.1")
                                && value.contains("::1"),
                            "NO_PROXY missing expected entries: {}",
                            value
                        );
                    } else if key == "no_proxy" {
                        found_lower = true;
                        assert!(
                            value.contains("example.com")
                                && value.contains("localhost")
                                && value.contains("127.0.0.1")
                                && value.contains("::1"),
                            "no_proxy missing expected entries: {}",
                            value
                        );
                    }
                }
            }

            assert!(found_upper, "NO_PROXY variable not found");
            assert!(found_lower, "no_proxy variable not found");
        }
        Err(e) => panic!("Failed to execute httpjail: {}", e),
    }
}

// Simple server start function - we know the ports we're setting
async fn start_server(http_port: u16, https_port: u16) -> Result<std::process::Child, String> {
    let httpjail_path: &str = env!("CARGO_BIN_EXE_httpjail");

    let mut cmd = Command::new(httpjail_path);
    cmd.arg("--server")
        .arg("--js")
        .arg("true")
        .arg("-vv")
        .env("HTTPJAIL_HTTP_BIND", format!("127.0.0.1:{}", http_port))
        .env("HTTPJAIL_HTTPS_BIND", format!("127.0.0.1:{}", https_port))
        .env("HTTPJAIL_SKIP_KEYCHAIN_INSTALL", "1") // Skip automatic keychain installation during tests
        .stdout(Stdio::null())
        .stderr(Stdio::null());

    let child = cmd
        .spawn()
        .map_err(|e| format!("Failed to start server: {}", e))?;

    // Wait for the server to start listening
    if !common::wait_for_server(http_port, Duration::from_secs(5)).await {
        return Err(format!("Server failed to start on port {}", http_port));
    }

    Ok(child)
}

fn test_curl_through_proxy(http_port: u16, _https_port: u16) -> Result<String, String> {
    // First, verify the proxy port is actually listening
    if !verify_bind_address(http_port, "127.0.0.1") {
        return Err(format!("Proxy port {} is not listening", http_port));
    }

    // Use a simple HTTP endpoint that should work in CI
    // Try with verbose output for debugging
    let output = Command::new("curl")
        .arg("-x")
        .arg(format!("http://127.0.0.1:{}", http_port))
        .arg("--max-time")
        .arg("10") // Increase timeout for CI
        .arg("-s")
        .arg("-S") // Show errors
        .arg("-w")
        .arg("\nHTTP_CODE:%{http_code}")
        .arg("http://example.com/")
        .output()
        .map_err(|e| format!("Failed to run curl: {}", e))?;

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    // Check if curl succeeded (exit code 0)
    if !output.status.success() {
        // For debugging in CI
        eprintln!("Curl failed - stdout: {}", stdout);
        eprintln!("Curl failed - stderr: {}", stderr);
        return Err(format!(
            "Curl failed with status: {}, stderr: {}",
            output.status, stderr
        ));
    }

    // Check if we got a valid HTTP response
    if stdout.contains("HTTP_CODE:200") || stdout.contains("Example Domain") {
        Ok(stdout.to_string())
    } else if stdout.contains("HTTP_CODE:403") {
        // Request was blocked by proxy (which is also fine for testing)
        Ok("Blocked by proxy".to_string())
    } else {
        Err(format!("Unexpected response: {}", stdout))
    }
}

fn verify_bind_address(port: u16, expected_ip: &str) -> bool {
    // Try to connect to the expected IP
    std::net::TcpStream::connect(format!("{}:{}", expected_ip, port)).is_ok()
}

#[tokio::test]
async fn test_server_mode() {
    // Test server mode with specific ports
    let http_port = 19876;
    let https_port = 19877;

    let mut server = start_server(http_port, https_port)
        .await
        .expect("Failed to start server");

    // Test HTTP proxy works
    match test_curl_through_proxy(http_port, https_port) {
        Ok(_response) => {
            // Success - proxy is working
        }
        Err(e) => panic!("Curl test failed: {}", e),
    }

    // Verify binds to localhost only
    assert!(
        verify_bind_address(http_port, "127.0.0.1"),
        "Server should bind to localhost"
    );

    // Cleanup
    let _ = server.kill();
    let _ = server.wait();
}

// Helper to start server with custom bind config
async fn start_server_with_bind(http_bind: &str, https_bind: &str) -> (std::process::Child, u16) {
    let httpjail_path: &str = env!("CARGO_BIN_EXE_httpjail");

    let mut child = Command::new(httpjail_path)
        .arg("--server")
        .arg("--js")
        .arg("true")
        .env("HTTPJAIL_HTTP_BIND", http_bind)
        .env("HTTPJAIL_HTTPS_BIND", https_bind)
        .env("HTTPJAIL_SKIP_KEYCHAIN_INSTALL", "1")
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .expect("Failed to spawn server");

    // Parse expected port from bind config (server mode uses defaults for unspecified)
    let expected_port = if let Ok(port) = http_bind.parse::<u16>() {
        port
    } else if let Some(port_str) = http_bind.strip_prefix(':') {
        // Handle :port format
        port_str.parse::<u16>().unwrap_or(8080)
    } else if let Ok(addr) = http_bind.parse::<std::net::SocketAddr>() {
        addr.port()
    } else {
        8080 // Default port for server mode
    };

    // Wait for server to bind
    if !common::wait_for_server(expected_port, Duration::from_secs(3)).await {
        child.kill().ok();
        panic!("Server failed to bind to port {}", expected_port);
    }

    (child, expected_port)
}

#[tokio::test]
#[serial]
async fn test_server_bind_defaults() {
    let (mut server, port) = start_server_with_bind("", "").await;
    assert_eq!(port, 8080, "Server should default to port 8080");
    server.kill().ok();
}

#[tokio::test]
#[serial]
async fn test_server_bind_port_only() {
    // Port-only should bind to all interfaces (0.0.0.0)
    let (mut server, port) = start_server_with_bind("19882", "19883").await;
    assert_eq!(
        port, 19882,
        "Server should bind to specified port on all interfaces"
    );
    server.kill().ok();
}

#[tokio::test]
#[serial]
async fn test_server_bind_colon_prefix_port() {
    // :port (Go-style) should bind to all interfaces (0.0.0.0)
    let (mut server, port) = start_server_with_bind(":19892", ":19893").await;
    assert_eq!(
        port, 19892,
        "Server should bind to specified port on all interfaces with :port format"
    );
    server.kill().ok();
}

#[tokio::test]
#[serial]
async fn test_server_bind_all_interfaces() {
    let (mut server, port) = start_server_with_bind("0.0.0.0:19884", "0.0.0.0:19885").await;
    assert_eq!(
        port, 19884,
        "Server should bind to specified port on 0.0.0.0"
    );
    server.kill().ok();
}

#[tokio::test]
#[serial]
async fn test_server_bind_ip_without_port() {
    let (mut server, port) = start_server_with_bind("127.0.0.1", "127.0.0.1").await;
    assert_eq!(
        port, 8080,
        "Server should use default port 8080 when only IP specified"
    );
    server.kill().ok();
}

#[test]
#[serial]
fn test_server_bind_explicit_port_zero() {
    // Explicit port 0 should be respected (OS auto-select), not overridden to 8080
    let httpjail_path: &str = env!("CARGO_BIN_EXE_httpjail");

    let mut child = Command::new(httpjail_path)
        .arg("--server")
        .arg("--js")
        .arg("true")
        .env("HTTPJAIL_HTTP_BIND", "0") // Explicit port 0
        .env("HTTPJAIL_HTTPS_BIND", "0")
        .env("HTTPJAIL_SKIP_KEYCHAIN_INSTALL", "1")
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .expect("Failed to spawn server");

    // Give server a moment to bind to an OS-selected port
    thread::sleep(Duration::from_millis(500));

    // Verify it's NOT bound to the default port 8080 (it should be random)
    let not_on_default = std::net::TcpStream::connect("127.0.0.1:8080").is_err();
    assert!(
        not_on_default,
        "Server should not bind to default port 8080 when explicit :0 provided"
    );

    // Server should still be running successfully
    assert!(
        child.try_wait().unwrap().is_none(),
        "Server should be running"
    );

    child.kill().ok();
}

/// Test for Host header security (Issue #57)
/// Verifies that httpjail corrects mismatched Host headers to prevent
/// CDN routing bypasses and other Host header attacks.
///
/// This test uses httpbingo.org/headers which echoes back the received headers,
/// allowing us to verify that httpjail corrects the Host header to match the
/// actual destination URL rather than relying on external service blocking behavior.
#[test]
fn test_host_header_security() {
    use std::process::Command;

    // Test 1: Direct curl with mismatched Host header
    // httpbingo.org/headers echoes back all headers it receives
    let direct_result = Command::new("curl")
        .args([
            "-s",
            "-H",
            "Host: evil.com",
            "--max-time",
            "5",
            "https://httpbingo.org/headers",
        ])
        .output()
        .expect("Failed to execute curl directly");

    let direct_stdout = String::from_utf8_lossy(&direct_result.stdout);

    // Verify curl sends the mismatched Host header as-is
    assert!(
        direct_stdout.contains("\"Host\"") && direct_stdout.contains("evil.com"),
        "Direct curl should send mismatched Host header (got: {})",
        direct_stdout
    );

    // Test 2: Same curl command through httpjail - should correct the Host header
    let httpjail_result = HttpjailCommand::new()
        .weak()
        .js("true") // Allow all requests
        .command(vec![
            "curl",
            "-s",
            "-H",
            "Host: evil.com",
            "--max-time",
            "5",
            "https://httpbingo.org/headers",
        ])
        .execute();

    assert!(httpjail_result.is_ok(), "Httpjail request should complete");
    let (exit_code, stdout, _) = httpjail_result.unwrap();
    assert_eq!(exit_code, 0, "Httpjail request should succeed");

    // Verify httpjail corrected the Host header to match the actual destination
    assert!(
        stdout.contains("\"Host\"") && stdout.contains("httpbingo.org"),
        "Httpjail should correct Host header to httpbingo.org (got: {})",
        stdout
    );

    // Verify the mismatched header was NOT forwarded
    assert!(
        !stdout.contains("evil.com"),
        "Httpjail should not forward mismatched Host header evil.com (got: {})",
        stdout
    );

    // This demonstrates that httpjail prevents Host header bypass attacks
    // by correcting the Host header to match the actual destination URL
}

// The proc/JS parity tests have been moved to tests/json_parity.rs
// which directly tests the rule engines without the fragility of
// going through the full httpjail binary and external processes

// The max_tx_bytes test has been moved to tests/weak_integration_max_tx_bytes.rs