fix(xhr): resolve open URLs with credentials

This commit is contained in:
ldm0
2026-11-19 00:25:52 +08:00
parent c93b326256
commit ca023374bf
@@ -83,6 +83,60 @@ async fn same_origin_window_fetch_and_xhr_post_send_origin_on_wire() {
}
}
#[tokio::test(flavor = "current_thread")]
async fn window_xhr_open_freezes_base_url_and_applies_url_credentials() {
let server = StaticHttpServer::spawn(1).await;
let base_url = server.base_url();
let loader = static_http_loader(std::iter::empty::<String>());
let mut vm = new_page_task_executor_test_vm_with_loader(
base_url
.join("page.html")
.expect("page fixture URL")
.as_str(),
&loader,
);
let first_base = base_url.join("first/").expect("first base URL");
let second_base = base_url.join("second/").expect("second base URL");
vm.eval(&format!(
r#"
(() => {{
globalThis.__xhrOpenUrlProbe = "pending";
const base = document.createElement("base");
base.href = {};
document.head.append(base);
const xhr = new XMLHttpRequest();
xhr.open("GET", "resource", true, "alice", "secret");
base.href = {};
xhr.onload = () => {{ globalThis.__xhrOpenUrlProbe = "done"; }};
xhr.onerror = () => {{ globalThis.__xhrOpenUrlProbe = "error"; }};
xhr.send();
return "started";
}})()
"#,
serde_json::to_string(first_base.as_str()).expect("serialize first base URL"),
serde_json::to_string(second_base.as_str()).expect("serialize second base URL"),
))
.expect("XHR open URL probe should evaluate");
advance_page_task_executor_until_eval_equals(
&mut vm,
&loader,
"String(globalThis.__xhrOpenUrlProbe)",
"done",
"XHR open URL probe",
)
.await;
let requests = server.finish().await;
assert_eq!(requests.len(), 1);
assert_eq!(requests[0].target, "/first/resource");
assert_eq!(
requests[0].header_value("authorization"),
Some("Basic YWxpY2U6c2VjcmV0")
);
}
#[tokio::test(flavor = "current_thread")]
async fn asynchronous_window_xhr_records_resource_timing_with_xmlhttprequest_initiator() {
let server = StaticHttpServer::spawn(1).await;
@@ -154,7 +208,7 @@ fn pending_fetch_continuation<'s>(
host: &crate::native_bridge::PageScriptHost,
) -> crate::types::PendingSubresourceContinuation {
let dispatch_scope = crate::native_bridge::OwnerDispatchScope::Top;
crate::types::PendingSubresourceContinuation::Fetch(
crate::types::PendingSubresourceContinuation::Fetch(Box::new(
crate::types::PendingWindowFetchContinuation::new(
v8::Global::new(scope, resolver),
crate::context_bootstrap::FetchResourceTiming::capture(
@@ -173,7 +227,7 @@ fn pending_fetch_continuation<'s>(
)
.expect("test Fetch should capture its CSP report context"),
),
)
))
}
#[test]
@@ -2454,6 +2508,7 @@ __xhrGate.send();
from_cache: false,
cache_state: Default::default(),
negotiated_http_version: None,
preload_state: Default::default(),
},
network_request_headers: None,
})
@@ -4098,7 +4153,7 @@ fn xhr_response_documents_keep_distinct_source_modification_times() {
redirected: false,
redirect_chain: Vec::new(),
from_cache: false,
cache_state: Default::default(),
negotiated_http_version: None,
};
if streaming {