use runtime_foxdriver::{CapturedHeader, CapturedRequest, NetworkEntry};
fn request_with(
method: &str,
url: &str,
header: Option<(&str, &str)>,
body: Option<&str>,
) -> NetworkEntry {
NetworkEntry {
request: CapturedRequest {
id: "1".into(),
context: None,
method: method.into(),
url: url.into(),
headers: header
.map(|(name, value)| {
vec![CapturedHeader {
name: name.into(),
value: value.into(),
}]
})
.unwrap_or_default(),
post_data: body.map(Into::into),
timestamp: 0,
destination: "document".into(),
initiator_type: None,
timing: Default::default(),
cookies: vec![],
},
response: None,
error: None,
}
}
#[test]
fn to_curl_command_substitution_stays_inert() {
let entry = request_with(
"GET",
"https://evil.example/$(touch /tmp/pwned)",
Some(("X-Evil", "`id`")),
Some("$(reboot)"),
);
let curl = entry.to_curl();
assert!(
curl.contains("'https://evil.example/$(touch /tmp/pwned)'"),
"hostile URL must be single-quoted, got: {curl}"
);
assert!(
curl.contains("-H 'X-Evil: `id`'"),
"hostile header must be single-quoted, got: {curl}"
);
assert!(
curl.contains("-d '$(reboot)'"),
"hostile body must be single-quoted, got: {curl}"
);
}
#[test]
fn to_curl_single_quote_breakout_is_escaped() {
let entry = request_with("GET", "https://a.example/'; rm -rf /; '", None, None);
let curl = entry.to_curl();
assert!(
curl.contains("'https://a.example/'\\''; rm -rf /; '\\'''"),
"embedded quote must be escaped as '\\'', got: {curl}"
);
}
#[test]
fn to_curl_hostile_method_cannot_inject_arguments() {
let entry = request_with("POST http://evil.example/x", "https://a.example/", None, None);
let curl = entry.to_curl();
assert!(
curl.starts_with("curl -X 'POST http://evil.example/x' 'https://a.example/'"),
"method must be a single quoted token, got: {curl}"
);
}
#[test]
fn json_body_malformed_json_returns_none_not_panic() {
for body in [
"{not json",
"{\"a\":}",
" ",
"\u{0}\u{1}\u{2}",
"{\"a\": 1,}",
] {
let entry = request_with("POST", "https://a.example/", None, Some(body));
assert!(
entry.request.json_body().is_none(),
"malformed body {body:?} must parse to None"
);
}
let ok = request_with("POST", "https://a.example/", None, Some(r#"{"a":1}"#));
assert_eq!(
ok.request.json_body().unwrap()["a"],
serde_json::json!(1),
"valid JSON body must parse"
);
}
#[test]
fn query_params_malformed_url_errors_not_empty() {
let bad = request_with("GET", "http://[::1", None, None);
assert!(
bad.request.query_params().is_err(),
"malformed URL must surface Err"
);
let good = request_with("GET", "https://a.example/?x=1&y=2", None, None);
let params = good.request.query_params().unwrap();
assert_eq!(
params,
vec![("x".to_string(), "1".to_string()), ("y".to_string(), "2".to_string())],
"well-formed query string must decode both params"
);
}