Skip to content

Commit 574ca02

Browse files
authored
Fix test_host_header_security to use httpbin.org for reliable testing (#82)
Replace flaky Cloudflare-based test with httpbin.org/headers which reliably echoes back received headers. This allows us to directly verify that httpjail corrects mismatched Host headers without depending on external service blocking behavior (which was inconsistent across datacenters). The new approach: 1. Tests what we control: httpjail's Host header correction 2. Uses reliable service: httpbin.org simply echoes headers 3. Has clear assertions: directly checks Host header values 4. No flakiness: doesn't rely on varying security policies Fixes the test failure that occurred because Cloudflare stopped consistently blocking mismatched Host headers (error 1034).
1 parent 5afe807 commit 574ca02

1 file changed

Lines changed: 38 additions & 24 deletions

File tree

tests/weak_integration.rs

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -290,60 +290,74 @@ fn test_server_mode() {
290290

291291
/// Test for Host header security (Issue #57)
292292
/// Verifies that httpjail corrects mismatched Host headers to prevent
293-
/// CloudFlare and other CDN routing bypasses.
293+
/// CDN routing bypasses and other Host header attacks.
294+
///
295+
/// This test uses httpbin.org/headers which echoes back the received headers,
296+
/// allowing us to verify that httpjail corrects the Host header to match the
297+
/// actual destination URL rather than relying on external service blocking behavior.
294298
#[test]
295299
fn test_host_header_security() {
296300
use std::process::Command;
297301

298-
// Test with Cloudflare's trace endpoint that clearly shows the Host header
299-
let curl_args = vec![
300-
"-s",
301-
"-H",
302-
"Host: evil.com",
303-
"--max-time",
304-
"3",
305-
"http://www.cloudflare.com/cdn-cgi/trace",
306-
];
307-
308-
// Test 1: Direct curl execution (without httpjail) - Cloudflare blocks mismatched Host
302+
// Test 1: Direct curl with mismatched Host header
303+
// httpbin.org/headers echoes back all headers it receives
309304
let direct_result = Command::new("curl")
310-
.args(&curl_args)
305+
.args([
306+
"-s",
307+
"-H",
308+
"Host: evil.com",
309+
"--max-time",
310+
"5",
311+
"http://httpbin.org/headers",
312+
])
311313
.output()
312314
.expect("Failed to execute curl directly");
313315

314316
let direct_stdout = String::from_utf8_lossy(&direct_result.stdout);
315-
// Cloudflare returns an error code (1034) for mismatched Host headers
317+
318+
// Verify curl sends the mismatched Host header as-is
316319
assert!(
317-
direct_stdout.contains("error code: 1034"),
318-
"Direct curl with mismatched Host header should be blocked by Cloudflare with error 1034 (got: {})",
320+
direct_stdout.contains("\"Host\": \"evil.com\"")
321+
|| direct_stdout.contains("\"Host\":\"evil.com\""),
322+
"Direct curl should send mismatched Host header (got: {})",
319323
direct_stdout
320324
);
321325

322326
// Test 2: Same curl command through httpjail - should correct the Host header
323327
let httpjail_result = HttpjailCommand::new()
324328
.weak()
325329
.js("true") // Allow all requests
326-
.command(vec!["curl"].into_iter().chain(curl_args.clone()).collect())
330+
.command(vec![
331+
"curl",
332+
"-s",
333+
"-H",
334+
"Host: evil.com",
335+
"--max-time",
336+
"5",
337+
"http://httpbin.org/headers",
338+
])
327339
.execute();
328340

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

333-
// Httpjail should have corrected the Host header, allowing the request to succeed
345+
// Verify httpjail corrected the Host header to match the actual destination
334346
assert!(
335-
stdout.contains("h=www.cloudflare.com"),
336-
"Httpjail should correct the Host header to www.cloudflare.com, allowing the request (got: {})",
347+
stdout.contains("\"Host\": \"httpbin.org\"") || stdout.contains("\"Host\":\"httpbin.org\""),
348+
"Httpjail should correct Host header to httpbin.org (got: {})",
337349
stdout
338350
);
351+
352+
// Verify the mismatched header was NOT forwarded
339353
assert!(
340-
!stdout.contains("error code: 1034"),
341-
"Httpjail-corrected request should not be blocked by Cloudflare (got: {})",
354+
!stdout.contains("\"Host\": \"evil.com\"") && !stdout.contains("\"Host\":\"evil.com\""),
355+
"Httpjail should not forward mismatched Host header evil.com (got: {})",
342356
stdout
343357
);
344358

345-
// This demonstrates that httpjail prevents the Host header bypass attack
346-
// that would otherwise be possible with direct curl execution
359+
// This demonstrates that httpjail prevents Host header bypass attacks
360+
// by correcting the Host header to match the actual destination URL
347361
}
348362

349363
// The proc/JS parity tests have been moved to tests/json_parity.rs

0 commit comments

Comments
 (0)