Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(fetch): retain redirect state across service worker fallback

ldm0b63197ff8a59
diff --git a/moli-core/tests/scripts/integrity.rs b/moli-core/tests/scripts/integrity.rs
index dd03a94f8..e65d08ab8 100644
--- a/moli-core/tests/scripts/integrity.rs
+++ b/moli-core/tests/scripts/integrity.rs
@@ -1,4 +1,6 @@
use super::*;
+use parking_lot::Mutex;
+use std::sync::Arc;
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::TcpListener,
@@ -11,9 +13,18 @@ const INTEGRITY: &str = "sha384-T7tuz8k7Hz0eBaWUPKiAEECRmaKHLJ1eRz7NF4VdK1fN++Ia
struct IntegrityServers {
origin: String,
cross_origin: String,
+ requests: Arc<Mutex<Vec<IntegrityRequest>>>,
tasks: Vec<JoinHandle<()>>,
}
+#[derive(Debug)]
+struct IntegrityRequest {
+ method: String,
+ path: String,
+ origin: Option<String>,
+ cookie: Option<String>,
+}
+
impl Drop for IntegrityServers {
fn drop(&mut self) {
for task in &self.tasks {
@@ -28,11 +39,13 @@ impl IntegrityServers {
let cross = TcpListener::bind("127.0.0.1:0").await?;
let origin = format!("http://{}", main.local_addr()?);
let cross_origin = format!("http://{}", cross.local_addr()?);
+ let requests = Arc::new(Mutex::new(Vec::new()));
let tasks = [main, cross]
.into_iter()
.map(|listener| {
let origin = origin.clone();
let cross_origin = cross_origin.clone();
+ let requests = Arc::clone(&requests);
tokio::spawn(async move {
let mut connections = JoinSet::new();
loop {
@@ -41,6 +54,7 @@ impl IntegrityServers {
let Ok((mut stream, _)) = accepted else { break };
let origin = origin.clone();
let cross_origin = cross_origin.clone();
+ let requests = Arc::clone(&requests);
connections.spawn(async move {
let mut request = Vec::new();
while !request.ends_with(b"\r\n\r\n") && request.len() < 16384 {
@@ -49,8 +63,15 @@ impl IntegrityServers {
}
let request = String::from_utf8_lossy(&request);
let path = request.split_whitespace().nth(1).unwrap_or("/");
- let path = path.split('?').next().unwrap_or(path);
- let (status, headers, body) = fixture_response(path, &origin, &cross_origin);
+ let header = |name: &str| request.lines().filter_map(|line| line.split_once(':'))
+ .find(|(key, _)| key.eq_ignore_ascii_case(name))
+ .map(|(_, value)| value.trim().to_owned());
+ let incoming = IntegrityRequest {
+ method: request.split_whitespace().next().unwrap_or("GET").to_owned(),
+ path: path.to_owned(), origin: header("Origin"), cookie: header("Cookie"),
+ };
+ let (status, headers, body) = fixture_response(&incoming, &origin, &cross_origin);
+ requests.lock().push(incoming);
let response = format!(
"HTTP/1.1 {status}\r\n{headers}Content-Length: {}\r\nConnection: close\r\n\r\n{body}",
body.len(),
@@ -67,6 +88,7 @@ impl IntegrityServers {
Ok(Self {
origin,
cross_origin,
+ requests,
tasks,
})
}
@@ -91,10 +113,40 @@ fn network_cases(origin: &str, cross: &str) -> serde_json::Value {
])
}
-fn fixture_response(path: &str, origin: &str, cross: &str) -> (&'static str, String, String) {
+fn fixture_response(
+ request: &IntegrityRequest,
+ origin: &str,
+ cross: &str,
+) -> (&'static str, String, String) {
+ let (path, query) = request.path.split_once('?').unwrap_or((&request.path, ""));
let javascript = "Content-Type: text/javascript\r\n";
match path {
+ "/echo-origin.js" | "/echo-origin-redirect.js" => {
+ // Echo the actual request Origin: a missing header must not silently
+ // receive ACAO: null and hide a broken redirect handoff.
+ let mut headers = format!(
+ "{javascript}Cache-Control: no-store\r\nVary: Origin\r\nX-Sri-Private: yes\r\n"
+ );
+ if let Some(origin) = &request.origin {
+ headers.push_str(&format!("Access-Control-Allow-Origin: {origin}\r\nAccess-Control-Allow-Credentials: true\r\n"));
+ }
+ if request.method == "OPTIONS" {
+ headers.push_str("Access-Control-Allow-Methods: GET, PUT\r\nAccess-Control-Allow-Headers: x-sri-test\r\n");
+ return ("204 No Content", headers, String::new());
+ }
+ if path == "/echo-origin-redirect.js" {
+ headers.push_str(&format!("Location: {cross}/echo-origin.js?{query}\r\n"));
+ ("302 Found", headers, String::new())
+ } else {
+ ("200 OK", headers, SCRIPT.to_owned())
+ }
+ }
"/script.js" => ("200 OK", javascript.to_owned(), SCRIPT.to_owned()),
+ "/cacheable.js" => (
+ "200 OK",
+ format!("{javascript}Cache-Control: max-age=60\r\n"),
+ SCRIPT.to_owned(),
+ ),
"/cors.js" | "/cors-null.js" | "/cors-origin.js" => (
"200 OK",
format!(
@@ -163,13 +215,32 @@ fn fixture_response(path: &str, origin: &str, cross: &str) -> (&'static str, Str
event.respondWith(Response.redirect('{origin}/script.js'));
else if (path === '/sw-cors-redirect-unapproved-hop.js')
event.respondWith(Response.redirect('{cross}/unapproved-redirect.js'));
+ else if (path === '/sw-request-state.js')
+ event.respondWith(Response.redirect('{origin}/echo-origin.js' + new URL(event.request.url).search));
+ else if (path === '/sw-request-state-redirect.js')
+ event.respondWith(Response.redirect('{origin}/echo-origin-redirect.js' + new URL(event.request.url).search));
+ else if (path === '/sw-cacheable.js')
+ event.respondWith(Response.redirect('{origin}/cacheable.js'));
+ else if (path.startsWith('/sw-return-'))
+ event.respondWith(Response.redirect('{origin}' + path.replace('/sw-return-', '/sw-final-')));
+ else if (path === '/sw-final-basic.js')
+ event.respondWith(fetch('{origin}/echo-origin.js?worker-basic'));
+ else if (path === '/sw-final-basic-buffered.js')
+ event.respondWith(fetch('{origin}/echo-origin.js?worker-basic-buffered').then(async response => {{
+ await response.clone().text();
+ return response;
+ }}));
+ else if (path === '/sw-final-cors.js')
+ event.respondWith(fetch('{cross}/echo-origin.js?worker-cors'));
+ else if (path === '/sw-final-default.js')
+ event.respondWith(new Response({script}, {{headers: {{'Content-Type': 'text/javascript', 'X-Sri-Private': 'yes'}}}}));
}});
"#,
script = serde_json::to_string(SCRIPT).unwrap()
);
("200 OK", javascript.to_owned(), body)
}
- "/page.html" | "/parser.html" => {
+ "/page.html" | "/parser.html" | "/cookie-page.html" => {
let mut html = "<!doctype html><body><script>globalThis.sriEvents = {}; globalThis.sriExecutions = 0;</script>".to_owned();
if path == "/parser.html" {
for case in network_cases(origin, cross).as_array().unwrap() {
@@ -183,7 +254,11 @@ fn fixture_response(path: &str, origin: &str, cross: &str) -> (&'static str, Str
html.push_str("></script>");
}
}
- ("200 OK", "Content-Type: text/html\r\n".to_owned(), html)
+ let mut headers = "Content-Type: text/html\r\n".to_owned();
+ if path == "/cookie-page.html" {
+ headers.push_str("Set-Cookie: sriSession=present; Path=/; SameSite=Lax\r\n");
+ }
+ ("200 OK", headers, html)
}
_ => (
"404 Not Found",
@@ -337,3 +412,259 @@ async fn service_worker_cross_origin_redirect_integrity_checks_network_responses
assert_integrity_results(result, &cases);
Ok(())
}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn service_worker_script_redirect_preserves_origin_and_credentials() -> Result<()> {
+ let servers = IntegrityServers::spawn().await?;
+ let browser = Browser::new(AppConfig::default())?;
+ let mut page = browser
+ .fetch(&format!("{}/cookie-page.html", servers.origin))
+ .await?;
+ let cross = &servers.cross_origin;
+ let cases = serde_json::json!([
+ {"name": "control", "src": "/echo-origin.js?control", "type": "module", "integrity": INTEGRITY, "expected": "load"},
+ {"name": "module", "src": format!("{cross}/sw-request-state.js?module"), "type": "module", "integrity": INTEGRITY, "expected": "load"},
+ {"name": "classic", "src": format!("{cross}/sw-request-state.js?classic"), "crossOrigin": "anonymous", "integrity": INTEGRITY, "expected": "load"},
+ {"name": "module-include", "src": format!("{cross}/sw-request-state.js?module-include"), "type": "module", "crossOrigin": "use-credentials", "integrity": INTEGRITY, "expected": "load"},
+ {"name": "classic-include", "src": format!("{cross}/sw-request-state.js?classic-include"), "crossOrigin": "use-credentials", "integrity": INTEGRITY, "expected": "load"},
+ {"name": "module-chain", "src": format!("{cross}/sw-request-state-redirect.js?module-chain"), "type": "module", "integrity": INTEGRITY, "expected": "load"},
+ {"name": "classic-chain", "src": format!("{cross}/sw-request-state-redirect.js?classic-chain"), "crossOrigin": "anonymous", "integrity": INTEGRITY, "expected": "load"}
+ ]);
+ let result = page
+ .evaluate_runtime_expression_with_await_async(&dynamic_probe(&cases, true), true)
+ .await?;
+ {
+ let requests = servers.requests.lock();
+ for case in cases.as_array().unwrap() {
+ let name = case["name"].as_str().unwrap();
+ let requests: Vec<_> = requests
+ .iter()
+ .filter(|request| request.path.ends_with(&format!("?{name}")))
+ .collect();
+ assert_eq!(
+ requests.len(),
+ if name.ends_with("-chain") { 2 } else { 1 },
+ "{name}: {requests:?}"
+ );
+ for request in requests {
+ assert_eq!(
+ request.origin.as_deref(),
+ if name == "control" {
+ None
+ } else {
+ Some("null")
+ },
+ "{name}: {request:?}"
+ );
+ assert_eq!(
+ request.cookie.as_deref(),
+ if name == "control" || name.ends_with("-include") {
+ Some("sriSession=present")
+ } else {
+ None
+ },
+ "{name}: {request:?}"
+ );
+ }
+ }
+ }
+ assert_integrity_results(result, &cases);
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn service_worker_fetch_redirect_preserves_origin_and_credentials() -> Result<()> {
+ let servers = IntegrityServers::spawn().await?;
+ let browser = Browser::new(AppConfig::default())?;
+ let mut page = browser
+ .fetch(&format!("{}/cookie-page.html", servers.origin))
+ .await?;
+ page.evaluate_runtime_expression_with_await_async(
+ &dynamic_probe(&serde_json::json!([]), true),
+ true,
+ )
+ .await?;
+ let cross = &servers.cross_origin;
+ let cases = serde_json::json!([
+ {"name": "control", "src": "/echo-origin.js?control", "credentials": "same-origin"},
+ {"name": "same-origin", "src": format!("{cross}/sw-request-state.js?same-origin"), "credentials": "same-origin"},
+ {"name": "include", "src": format!("{cross}/sw-request-state.js?include"), "credentials": "include"},
+ {"name": "omit", "src": format!("{cross}/sw-request-state.js?omit"), "credentials": "omit"},
+ {"name": "preflight", "src": format!("{cross}/sw-request-state.js?preflight"), "credentials": "same-origin", "method": "PUT", "headers": {"X-Sri-Test": "yes"}},
+ {"name": "chain", "src": format!("{cross}/sw-request-state-redirect.js?chain"), "credentials": "same-origin"},
+ {"name": "preflight-chain", "src": format!("{cross}/sw-request-state-redirect.js?preflight-chain"), "credentials": "same-origin", "method": "PUT", "headers": {"X-Sri-Test": "yes"}}
+ ]);
+ let result = page
+ .evaluate_runtime_expression_with_await_async(
+ &format!(
+ r#"(async () => {{
+ const results = {{}};
+ for (const test of {cases}) {{
+ try {{
+ const response = await fetch(test.src, test);
+ results[test.name] = {{body: await response.text(), type: response.type,
+ privateHeader: response.headers.get('X-Sri-Private')}};
+ }}
+ catch (error) {{ results[test.name] = String(error); }}
+ }}
+ return JSON.stringify(results);
+ }})()"#
+ ),
+ true,
+ )
+ .await?;
+ let results: serde_json::Value = serde_json::from_str(result["value"].as_str().unwrap())?;
+ let requests = servers.requests.lock();
+ for case in cases.as_array().unwrap() {
+ let name = case["name"].as_str().unwrap();
+ let requests: Vec<_> = requests
+ .iter()
+ .filter(|request| request.path.ends_with(&format!("?{name}")))
+ .collect();
+ assert_eq!(
+ requests.len(),
+ match name {
+ "preflight-chain" => 4,
+ "preflight" | "chain" => 2,
+ _ => 1,
+ },
+ "{name}: {requests:?}"
+ );
+ for request in requests {
+ assert_eq!(
+ request.origin.as_deref(),
+ if name == "control" {
+ None
+ } else {
+ Some("null")
+ },
+ "{name}: {request:?}"
+ );
+ assert_eq!(
+ request.cookie.as_deref(),
+ if name == "control" || name == "include" {
+ Some("sriSession=present")
+ } else {
+ None
+ },
+ "{name}: {request:?}"
+ );
+ }
+ assert_eq!(results[name]["body"], SCRIPT, "{name}: {results}");
+ assert_eq!(
+ results[name]["type"],
+ if name == "control" { "basic" } else { "cors" },
+ "{name}: {results}"
+ );
+ assert_eq!(
+ results[name]["privateHeader"],
+ if name == "control" {
+ serde_json::json!("yes")
+ } else {
+ serde_json::Value::Null
+ },
+ "{name}: {results}"
+ );
+ }
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn service_worker_script_redirect_does_not_reuse_another_response_url_list() -> Result<()> {
+ let servers = IntegrityServers::spawn().await?;
+ let browser = Browser::new(AppConfig::default())?;
+ let mut page = browser
+ .fetch(&format!("{}/page.html", servers.origin))
+ .await?;
+ // Prime the ordinary Fetch memory cache, and prove the entry is reusable
+ // before introducing a different URL list through the worker.
+ let result = page.evaluate_runtime_expression_with_await_async(
+ "(async () => { await (await fetch('/cacheable.js')).text(); return await (await fetch('/cacheable.js')).text(); })()",
+ true,
+ ).await?;
+ assert_eq!(result["value"], SCRIPT);
+ assert_eq!(
+ servers
+ .requests
+ .lock()
+ .iter()
+ .filter(|request| request.path == "/cacheable.js")
+ .count(),
+ 1
+ );
+ let cases = serde_json::json!([
+ {"name": "same-origin", "src": "/sw-cacheable.js", "crossOrigin": "anonymous", "integrity": INTEGRITY, "expected": "load"},
+ {"name": "cross-origin", "src": format!("{}/sw-cacheable.js", servers.cross_origin), "crossOrigin": "anonymous", "integrity": INTEGRITY, "expected": "error"}
+ ]);
+ // Both fetches end at the same cacheable URL. Its body can be reused, but
+ // the first response's same-origin URL list must not authorize the second.
+ let result = page
+ .evaluate_runtime_expression_with_await_async(&dynamic_probe(&cases, true), true)
+ .await?;
+ assert_integrity_results(result, &cases);
+ Ok(())
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn service_worker_redirect_preserves_worker_response_filter() -> Result<()> {
+ let servers = IntegrityServers::spawn().await?;
+ let browser = Browser::new(AppConfig::default())?;
+ let mut page = browser
+ .fetch(&format!("{}/page.html", servers.origin))
+ .await?;
+ page.evaluate_runtime_expression_with_await_async(
+ &dynamic_probe(&serde_json::json!([]), true),
+ true,
+ )
+ .await?;
+ let result = page.evaluate_runtime_expression_with_await_async(
+ &format!(r#"(async () => {{
+ const results = {{}};
+ for (const kind of ['basic', 'basic-buffered', 'cors', 'default']) {{
+ try {{
+ const response = await fetch('{cross}/sw-return-' + kind + '.js');
+ results[kind] = {{type: response.type, privateHeader: response.headers.get('X-Sri-Private'), body: await response.text()}};
+ }} catch (error) {{ results[kind] = String(error); }}
+ }}
+ return JSON.stringify(results);
+ }})()"#, cross = servers.cross_origin), true,
+ ).await?;
+ let results: serde_json::Value = serde_json::from_str(result["value"].as_str().unwrap())?;
+ for kind in ["basic", "basic-buffered", "cors", "default"] {
+ assert_eq!(results[kind]["body"], SCRIPT, "{kind}: {results}");
+ assert_eq!(
+ results[kind]["type"],
+ if kind.starts_with("basic") {
+ "basic"
+ } else {
+ "cors"
+ },
+ "{kind}: {results}"
+ );
+ assert_eq!(
+ results[kind]["privateHeader"],
+ if kind.starts_with("basic") {
+ serde_json::json!("yes")
+ } else {
+ serde_json::Value::Null
+ },
+ "{kind}: {results}"
+ );
+ }
+ let cases = serde_json::Value::Array(
+ ["basic", "basic-buffered", "cors", "default"]
+ .into_iter()
+ .map(|kind| {
+ serde_json::json!({
+ "name": kind, "src": format!("{}/sw-return-{kind}.js", servers.cross_origin),
+ "type": "module", "integrity": INTEGRITY, "expected": "load"
+ })
+ })
+ .collect(),
+ );
+ let result = page
+ .evaluate_runtime_expression_with_await_async(&dynamic_probe(&cases, false), true)
+ .await?;
+ assert_integrity_results(result, &cases);
+ Ok(())
+}
diff --git a/moli-fetch/src/blocking/cache.rs b/moli-fetch/src/blocking/cache.rs
index f1dd58db1..fef50220c 100644
--- a/moli-fetch/src/blocking/cache.rs
+++ b/moli-fetch/src/blocking/cache.rs
@@ -101,7 +101,7 @@ pub(super) fn request_cache_control_requires_validation(
request: &Request,
request_url: &Url,
) -> bool {
- outgoing_request_headers_for_url(config, request, request_url, &[], None)
+ outgoing_request_headers_for_url(config, request, request_url, None)
.iter()
.any(|(name, value)| request_header_requires_validation(name, value))
}
diff --git a/moli-fetch/src/blocking/cache/vary.rs b/moli-fetch/src/blocking/cache/vary.rs
index d168266f8..a3e261c51 100644
--- a/moli-fetch/src/blocking/cache/vary.rs
+++ b/moli-fetch/src/blocking/cache/vary.rs
@@ -77,7 +77,7 @@ fn vary_request_header_value(
normalized_name: &str,
) -> Option<String> {
let explicit_value = || {
- outgoing_request_headers_for_url(config, request, request_url, &[], None)
+ outgoing_request_headers_for_url(config, request, request_url, None)
.into_iter()
.rev()
.find(|(name, _)| name.eq_ignore_ascii_case(normalized_name))
diff --git a/moli-fetch/src/blocking/mod.rs b/moli-fetch/src/blocking/mod.rs
index e9bfc3850..3eab82420 100644
--- a/moli-fetch/src/blocking/mod.rs
+++ b/moli-fetch/src/blocking/mod.rs
@@ -14,9 +14,7 @@ use moli_cookie_jar::{
NetworkCookieRequestContext, SharedBrowserCookieStore, StoredCookieQueryReport,
StoredCookieSetReport, same_site_urls,
};
-use moli_url::{
- is_potentially_trustworthy_url, origin_ascii_serialization, same_origin, tuple_origin_url,
-};
+use moli_url::{is_potentially_trustworthy_url, same_origin, tuple_origin_url};
use moli_url_policy::ensure_http_network_transport_url;
use tracing::debug;
use url::Url;
@@ -154,14 +152,13 @@ pub fn outgoing_request_headers(
request: &Request,
cookie_header: Option<&str>,
) -> Vec<(String, String)> {
- outgoing_request_headers_for_url(config, request, &request.url, &[], cookie_header)
+ outgoing_request_headers_for_url(config, request, &request.url, cookie_header)
}
pub(crate) fn outgoing_request_headers_for_url(
config: &FetchConfig,
request: &Request,
request_url: &Url,
- redirect_chain: &[RedirectInfo],
cookie_header: Option<&str>,
) -> Vec<(String, String)> {
let mut outgoing = Vec::new();
@@ -191,8 +188,8 @@ pub(crate) fn outgoing_request_headers_for_url(
outgoing.push((name.clone(), value.clone()));
}
- append_browser_navigation_headers(&mut outgoing, config, request, request_url, redirect_chain);
- append_browser_subresource_headers(&mut outgoing, config, request, request_url, redirect_chain);
+ append_browser_navigation_headers(&mut outgoing, config, request, request_url);
+ append_browser_subresource_headers(&mut outgoing, config, request, request_url);
append_browser_storage_access_header(&mut outgoing, request, request_url);
if !header_present(&outgoing, "referer")
@@ -274,7 +271,6 @@ fn append_browser_navigation_headers(
config: &FetchConfig,
request: &Request,
request_url: &Url,
- redirect_chain: &[RedirectInfo],
) {
if !request.is_navigation_request() || !matches!(request_url.scheme(), "http" | "https") {
return;
@@ -316,7 +312,7 @@ fn append_browser_navigation_headers(
append_header_if_missing(outgoing, "Cache-Control", "max-age=0".to_owned());
}
- if let Some(origin) = request_origin_header_value(request, request_url, redirect_chain) {
+ if let Some(origin) = request_origin_header_value(request, request_url) {
append_header_if_missing(outgoing, "Origin", origin);
}
@@ -328,7 +324,6 @@ fn append_browser_subresource_headers(
config: &FetchConfig,
request: &Request,
request_url: &Url,
- redirect_chain: &[RedirectInfo],
) {
let Some(metadata) = request.browser_request_metadata() else {
return;
@@ -400,7 +395,7 @@ fn append_browser_subresource_headers(
{
None
} else {
- request_origin_header_value(request, request_url, redirect_chain)
+ request_origin_header_value(request, request_url)
};
if let Some(origin) = origin {
append_header_if_missing(outgoing, "Origin", origin);
@@ -410,78 +405,20 @@ fn append_browser_subresource_headers(
}
}
-fn request_origin_header_value(
- request: &Request,
- request_url: &Url,
- redirect_chain: &[RedirectInfo],
-) -> Option<String> {
- if !request_needs_origin_header(request, request_url, redirect_chain) {
- return None;
- }
- let Some(initiator_url) = request.cookie_context.initiator_url.as_ref() else {
- return Some("null".to_owned());
- };
- let Some(initiator_origin_url) = tuple_origin_url(initiator_url) else {
- return Some("null".to_owned());
- };
- Some(
- if request_has_redirect_tainted_origin(
- initiator_origin_url.as_ref(),
- &request.url,
- redirect_chain,
- ) {
- "null".to_owned()
- } else {
- origin_ascii_serialization(initiator_origin_url.as_ref())
- },
- )
-}
-
-fn request_needs_origin_header(
- request: &Request,
- request_url: &Url,
- redirect_chain: &[RedirectInfo],
-) -> bool {
+fn request_origin_header_value(request: &Request, request_url: &Url) -> Option<String> {
if !matches!(request_url.scheme(), "http" | "https") {
- return false;
- }
- if !request.method.eq_ignore_ascii_case("GET") && !request.method.eq_ignore_ascii_case("HEAD") {
- return true;
- }
- if !matches!(request.request_mode, crate::RequestMode::Cors) {
- return false;
+ return None;
}
-
- let Some(initiator_url) = request.cookie_context.initiator_url.as_ref() else {
- return true;
- };
- let Some(initiator_origin_url) = tuple_origin_url(initiator_url) else {
- return true;
- };
- !same_origin(initiator_origin_url.as_ref(), request_url)
- || request_has_redirect_tainted_origin(
- initiator_origin_url.as_ref(),
- &request.url,
- redirect_chain,
- )
-}
-
-fn request_has_redirect_tainted_origin(
- request_origin_url: &Url,
- original_request_url: &Url,
- redirect_chain: &[RedirectInfo],
-) -> bool {
- let mut last_url = original_request_url;
-
- for redirect in redirect_chain {
- let next_url = &redirect.to_url;
- if !same_origin(next_url, last_url) && !same_origin(request_origin_url, last_url) {
- return true;
- }
- last_url = next_url;
+ let safe_method =
+ request.method.eq_ignore_ascii_case("GET") || request.method.eq_ignore_ascii_case("HEAD");
+ if safe_method
+ && (request.request_mode != crate::RequestMode::Cors
+ || (request.cookie_context.initiator_url.is_some()
+ && !request.has_cross_origin_url(request_url)))
+ {
+ return None;
}
-
- false
+ Some(request.serialized_origin())
}
fn append_browser_client_hints(outgoing: &mut Vec<(String, String)>, config: &FetchConfig) {
@@ -586,7 +523,6 @@ pub(crate) fn configure_easy<H: Handler>(
config: &FetchConfig,
request: &Request,
request_url: &Url,
- redirect_chain: &[RedirectInfo],
cookie_header: Option<&str>,
http_version: RequestHttpVersion,
validation_headers: Option<Vec<(String, String)>>,
@@ -705,13 +641,8 @@ pub(crate) fn configure_easy<H: Handler>(
}
let mut headers = List::new();
- let mut outgoing_headers = outgoing_request_headers_for_url(
- config,
- request,
- request_url,
- redirect_chain,
- cookie_header,
- );
+ let mut outgoing_headers =
+ outgoing_request_headers_for_url(config, request, request_url, cookie_header);
if let Some(web_bot_auth) = config.web_bot_auth() {
web_bot_auth
.append_request_headers(&mut outgoing_headers, &request.method, request_url)
@@ -1322,13 +1253,7 @@ mod tests {
request = request.with_initiator_url(&url(initiator));
}
- let headers = outgoing_request_headers_for_url(
- &config,
- &request,
- &request_url,
- &Vec::new(),
- None,
- );
+ let headers = outgoing_request_headers_for_url(&config, &request, &request_url, None);
assert_eq!(
header_value(&headers, "origin").as_deref(),
case.expected,
@@ -1352,8 +1277,7 @@ mod tests {
.with_initiator_url(&url("https://app.test/page"))
.with_browser_request_metadata(BrowserRequestMetadata::Xhr);
- let headers =
- outgoing_request_headers_for_url(&config, &request, &request_url, &Vec::new(), None);
+ let headers = outgoing_request_headers_for_url(&config, &request, &request_url, None);
assert_eq!(
headers
.iter()
@@ -1372,8 +1296,7 @@ mod tests {
.unwrap()
.with_top_level_navigation_cookie_context();
- let headers =
- outgoing_request_headers_for_url(&config, &request, &request_url, &Vec::new(), None);
+ let headers = outgoing_request_headers_for_url(&config, &request, &request_url, None);
assert_eq!(header_value(&headers, "origin").as_deref(), Some("null"));
}
@@ -1388,14 +1311,9 @@ mod tests {
.with_initiator_url(&initiator_url)
.with_browser_request_metadata(BrowserRequestMetadata::Fetch);
let redirect_chain = vec![redirect(&original_url, &redirected_url)];
+ let request = request.with_redirect_chain(redirect_chain);
- let headers = outgoing_request_headers_for_url(
- &config,
- &request,
- &redirected_url,
- &redirect_chain,
- None,
- );
+ let headers = outgoing_request_headers_for_url(&config, &request, &redirected_url, None);
assert_eq!(
header_value(&headers, "origin"),
@@ -1422,8 +1340,7 @@ mod tests {
.with_initiator_url(&initiator_url)
.with_browser_request_metadata(BrowserRequestMetadata::Xhr);
- let headers =
- outgoing_request_headers_for_url(&config, &request, &request_url, &Vec::new(), None);
+ let headers = outgoing_request_headers_for_url(&config, &request, &request_url, None);
assert_eq!(
header_value(&headers, "origin"),
@@ -1446,14 +1363,9 @@ mod tests {
.with_initiator_url(&initiator_url)
.with_browser_request_metadata(BrowserRequestMetadata::Fetch);
let redirect_chain = vec![redirect(&original_url, &redirected_url)];
+ let request = request.with_redirect_chain(redirect_chain);
- let headers = outgoing_request_headers_for_url(
- &config,
- &request,
- &redirected_url,
- &redirect_chain,
- None,
- );
+ let headers = outgoing_request_headers_for_url(&config, &request, &redirected_url, None);
assert_eq!(header_value(&headers, "origin"), Some("null".to_owned()));
assert_eq!(
@@ -1477,9 +1389,9 @@ mod tests {
redirect(&original_url, &intermediate_url),
redirect(&intermediate_url, &final_url),
];
+ let request = request.with_redirect_chain(redirect_chain);
- let headers =
- outgoing_request_headers_for_url(&config, &request, &final_url, &redirect_chain, None);
+ let headers = outgoing_request_headers_for_url(&config, &request, &final_url, None);
assert_eq!(header_value(&headers, "origin"), Some("null".to_owned()));
assert_eq!(
@@ -1504,9 +1416,9 @@ mod tests {
redirect(&original_url, &cross_site_url),
redirect(&cross_site_url, &final_url),
];
+ let request = request.with_redirect_chain(redirect_chain);
- let headers =
- outgoing_request_headers_for_url(&config, &request, &final_url, &redirect_chain, None);
+ let headers = outgoing_request_headers_for_url(&config, &request, &final_url, None);
assert_eq!(header_value(&headers, "origin"), Some("null".to_owned()));
assert_eq!(
@@ -1531,9 +1443,9 @@ mod tests {
redirect(&original_url, &cross_site_redirect_url),
redirect(&cross_site_redirect_url, &final_url),
];
+ let request = request.with_redirect_chain(redirect_chain);
- let headers =
- outgoing_request_headers_for_url(&config, &request, &final_url, &redirect_chain, None);
+ let headers = outgoing_request_headers_for_url(&config, &request, &final_url, None);
assert_eq!(header_value(&headers, "origin"), Some("null".to_owned()));
assert_eq!(
@@ -1552,8 +1464,7 @@ mod tests {
.with_initiator_url(&url("https://app.test/page"))
.with_browser_request_metadata(BrowserRequestMetadata::Fetch);
- let headers =
- outgoing_request_headers_for_url(&config, &request, &request_url, &Vec::new(), None);
+ let headers = outgoing_request_headers_for_url(&config, &request, &request_url, None);
assert_eq!(
header_value(&headers, "sec-fetch-mode"),
@@ -1570,8 +1481,7 @@ mod tests {
.with_initiator_url(&url("https://page.test/"))
.with_subframe_navigation_cookie_context();
- let headers =
- outgoing_request_headers_for_url(&config, &request, &request_url, &Vec::new(), None);
+ let headers = outgoing_request_headers_for_url(&config, &request, &request_url, None);
assert_eq!(
header_value(&headers, "sec-fetch-mode").as_deref(),
@@ -1623,8 +1533,7 @@ mod tests {
.with_credentials_mode(crate::RequestCredentialsMode::Include)
.with_request_mode(crate::RequestMode::Cors);
- let headers =
- outgoing_request_headers_for_url(&config, &request, &request_url, &Vec::new(), None);
+ let headers = outgoing_request_headers_for_url(&config, &request, &request_url, None);
assert_eq!(
header_value(&headers, "sec-fetch-site").as_deref(),
@@ -1648,8 +1557,7 @@ mod tests {
.with_credentials_mode(crate::RequestCredentialsMode::SameOrigin)
.with_request_mode(crate::RequestMode::Cors);
- let headers =
- outgoing_request_headers_for_url(&config, &request, &request_url, &Vec::new(), None);
+ let headers = outgoing_request_headers_for_url(&config, &request, &request_url, None);
assert_eq!(
header_value(&headers, "sec-fetch-site").as_deref(),
@@ -1669,8 +1577,7 @@ mod tests {
.with_browser_request_metadata(BrowserRequestMetadata::Xhr)
.with_credentials_mode(crate::RequestCredentialsMode::Include);
- let headers =
- outgoing_request_headers_for_url(&config, &request, &request_url, &Vec::new(), None);
+ let headers = outgoing_request_headers_for_url(&config, &request, &request_url, None);
assert_eq!(header_value(&headers, "sec-fetch-storage-access"), None);
}
diff --git a/moli-fetch/src/request.rs b/moli-fetch/src/request.rs
index 998724a5e..547861194 100644
--- a/moli-fetch/src/request.rs
+++ b/moli-fetch/src/request.rs
@@ -9,10 +9,12 @@ use moli_cookie_jar::{
NetworkSiteContextMetadata, NetworkSiteContextTrackMetadata, redirect_types_for_request,
site_context_downgrade_type,
};
-use moli_url::same_origin;
+use moli_url::{origin_ascii_serialization, same_origin};
use url::Url;
-use crate::{FetchConfig, network_fetch_result::NetworkObservationRecorder};
+use crate::{
+ FetchConfig, RedirectInfo, RedirectSource, network_fetch_result::NetworkObservationRecorder,
+};
#[derive(Debug, Clone)]
pub struct Request {
@@ -33,6 +35,7 @@ pub struct Request {
pub request_mode: RequestMode,
pub redirect_mode: RequestRedirectMode,
pub credentials_mode: RequestCredentialsMode,
+ pub(crate) redirect_chain: Vec<RedirectInfo>,
network_partition_key: Option<String>,
auth: Option<RequestAuth>,
pub cookie_context: NetworkCookieRequestContext,
@@ -383,6 +386,7 @@ impl Request {
request_mode: RequestMode::Navigate,
redirect_mode: RequestRedirectMode::Follow,
credentials_mode: RequestCredentialsMode::Include,
+ redirect_chain: Vec::new(),
network_partition_key: None,
auth: None,
cookie_context: NetworkCookieRequestContext::top_level_navigation("GET"),
@@ -410,6 +414,7 @@ impl Request {
request_mode: RequestMode::Navigate,
redirect_mode: RequestRedirectMode::Follow,
credentials_mode: RequestCredentialsMode::Include,
+ redirect_chain: Vec::new(),
network_partition_key: None,
auth: None,
cookie_context: NetworkCookieRequestContext::top_level_navigation("GET"),
@@ -458,6 +463,7 @@ impl Request {
request_mode: RequestMode::Cors,
redirect_mode: RequestRedirectMode::Follow,
credentials_mode: RequestCredentialsMode::Include,
+ redirect_chain: Vec::new(),
network_partition_key: None,
auth: None,
cookie_context: NetworkCookieRequestContext::subresource(method),
@@ -634,6 +640,64 @@ impl Request {
self
}
+ /// Continues a fetch at `url` with redirects already followed by its caller.
+ /// Keep synthetic redirects too: their URLs affect Origin and credentials
+ /// even though their response headers are not subject to network CORS checks.
+ pub fn with_redirect_chain(mut self, redirect_chain: Vec<RedirectInfo>) -> Self {
+ self.redirect_chain = redirect_chain;
+ self
+ }
+
+ pub fn redirect_chain(&self) -> &[RedirectInfo] {
+ &self.redirect_chain
+ }
+
+ /// Records a followed redirect. The caller controls method and current URL
+ /// updates so this also supports transports that retain the original URL.
+ pub fn record_redirect(&mut self, redirect: RedirectInfo) {
+ self.redirect_chain.push(redirect);
+ }
+
+ pub fn redirect_count(&self) -> usize {
+ self.redirect_chain
+ .iter()
+ .filter(|redirect| redirect.source != RedirectSource::Internal)
+ .count()
+ }
+
+ /// Whether the URL list has left the initiating origin. For subresource
+ /// fetches, returning to that origin cannot restore basic response tainting.
+ pub fn has_cross_origin_url(&self, request_url: &Url) -> bool {
+ self.cookie_context
+ .initiator_url
+ .as_ref()
+ .is_some_and(|initiator| {
+ !same_origin(initiator, request_url)
+ || !same_origin(initiator, &self.url)
+ || self.redirect_chain.iter().any(|redirect| {
+ !same_origin(initiator, &redirect.from_url)
+ || !same_origin(initiator, &redirect.to_url)
+ })
+ })
+ }
+
+ /// Fetch's serialized request origin, including redirect taint. A first
+ /// same-origin-to-cross-origin hop retains the origin; crossing origins
+ /// again after leaving it serializes as null.
+ pub fn serialized_origin(&self) -> String {
+ let Some(initiator) = self.cookie_context.initiator_url.as_ref() else {
+ return "null".to_owned();
+ };
+ if self.redirect_chain.iter().any(|redirect| {
+ !same_origin(&redirect.from_url, &redirect.to_url)
+ && !same_origin(initiator, &redirect.from_url)
+ }) {
+ "null".to_owned()
+ } else {
+ origin_ascii_serialization(initiator)
+ }
+ }
+
pub fn with_network_partition_key(mut self, key: Option<String>) -> Self {
if let Some(serialized_key) = key.as_deref() {
self.cookie_context.browser_context = self
@@ -654,11 +718,7 @@ impl Request {
match self.credentials_mode {
RequestCredentialsMode::Include => true,
RequestCredentialsMode::Omit => false,
- RequestCredentialsMode::SameOrigin => self
- .cookie_context
- .initiator_url
- .as_ref()
- .is_none_or(|initiator_url| same_origin(initiator_url, request_url)),
+ RequestCredentialsMode::SameOrigin => !self.has_cross_origin_url(request_url),
}
}
diff --git a/moli-fetch/src/runtime.rs b/moli-fetch/src/runtime.rs
index c3e17459a..5bddcb0d1 100644
--- a/moli-fetch/src/runtime.rs
+++ b/moli-fetch/src/runtime.rs
@@ -903,7 +903,6 @@ impl RuntimeOwner {
&self.config,
&prepared_request.request,
&job.current_url,
- &job.redirect_chain,
cookie_header.as_deref(),
job.http_version,
// Buffered transfers are now the auth/compatibility fallback and
@@ -924,7 +923,7 @@ impl RuntimeOwner {
)
});
attach_next_request_extra_info(
- &mut job.redirect_chain,
+ &mut job.request.redirect_chain,
request_cookie_report.clone(),
request_extra_info.as_ref(),
);
@@ -1076,7 +1075,6 @@ impl RuntimeOwner {
&self.config,
&prepared_request.request,
&job.current_url,
- &job.redirect_chain,
cookie_header.as_deref(),
job.http_version,
None,
@@ -1094,7 +1092,7 @@ impl RuntimeOwner {
)
});
attach_next_request_extra_info(
- &mut job.redirect_chain,
+ &mut job.request.redirect_chain,
request_cookie_report.clone(),
request_extra_info.as_ref(),
);
@@ -1108,7 +1106,7 @@ impl RuntimeOwner {
job.current_cookie_context.clone(),
request_cookie_report.clone(),
credentials_allowed,
- job.redirect_chain.clone(),
+ job.request.redirect_chain.clone(),
request_extra_info.clone(),
cache_plan,
);
@@ -1271,7 +1269,6 @@ impl RuntimeOwner {
&self.config,
&prepared_request.request,
&job.current_url,
- &job.redirect_chain,
cookie_header.as_deref(),
job.http_version,
stale_cached_lookup
@@ -1291,7 +1288,7 @@ impl RuntimeOwner {
)
});
attach_next_request_extra_info(
- &mut job.redirect_chain,
+ &mut job.request.redirect_chain,
request_cookie_report.clone(),
request_extra_info.as_ref(),
);
@@ -1305,7 +1302,7 @@ impl RuntimeOwner {
job.current_cookie_context.clone(),
request_cookie_report.clone(),
credentials_allowed,
- job.redirect_chain.clone(),
+ job.request.redirect_chain.clone(),
request_extra_info.clone(),
cache_plan,
stale_cached_lookup.is_some(),
@@ -1402,7 +1399,7 @@ impl RuntimeOwner {
if let Some(response) = easy.as_mut().and_then(take_failed_proxy_connect_response) {
let response = proxy_connect_raw_response(
&job.current_url,
- &job.redirect_chain,
+ &job.request.redirect_chain,
request_cookie_report,
response,
);
@@ -1443,7 +1440,7 @@ impl RuntimeOwner {
&job.request.url,
&upgraded_url,
);
- job.redirect_chain.push(https_upgrade_redirect_info(
+ job.request.redirect_chain.push(https_upgrade_redirect_info(
job.current_url.clone(),
upgraded_url.clone(),
request_cookie_report,
@@ -1498,7 +1495,8 @@ impl RuntimeOwner {
url = %job.current_url,
"restarting navigation before response commit for missing Critical-CH headers"
);
- job.redirect_chain
+ job.request
+ .redirect_chain
.push(critical_client_hint_restart_redirect_info(
response.final_url.clone(),
network_response_extra_info(
@@ -1531,7 +1529,7 @@ impl RuntimeOwner {
&& job.request.follow_redirects
{
let redirect_has_extra_info = request_extra_info.is_some() && !response.from_cache;
- job.redirect_chain.push(RedirectInfo {
+ job.request.redirect_chain.push(RedirectInfo {
source: crate::RedirectSource::Network,
from_url: response.final_url.clone(),
to_url: next_url.clone(),
@@ -1566,8 +1564,8 @@ impl RuntimeOwner {
return Ok(JobOutcome::Retry(Box::new(job)));
}
- response.redirected = !job.redirect_chain.is_empty();
- response.redirect_chain = job.redirect_chain;
+ response.redirected = !job.request.redirect_chain.is_empty();
+ response.redirect_chain = job.request.redirect_chain;
Ok(JobOutcome::Complete(
job.response_tx,
Box::new(CompletedBufferedResponse::Raw(response)),
@@ -1637,7 +1635,8 @@ impl RuntimeOwner {
collector.take_cookie_set_reports(),
)
};
- job.redirect_chain
+ job.request
+ .redirect_chain
.push(critical_client_hint_restart_redirect_info(
job.current_url.clone(),
network_response_extra_info(
@@ -1729,7 +1728,7 @@ impl RuntimeOwner {
&job.request.url,
&upgraded_url,
);
- job.redirect_chain.push(https_upgrade_redirect_info(
+ job.request.redirect_chain.push(https_upgrade_redirect_info(
job.current_url.clone(),
upgraded_url.clone(),
request_cookie_report,
@@ -1819,7 +1818,7 @@ impl RuntimeOwner {
tracing::debug!(url = %job.current_url, "failed to store streaming redirect response in disk cache: {error}");
}
let redirect_has_extra_info = request_extra_info.is_some();
- job.redirect_chain.push(RedirectInfo {
+ job.request.redirect_chain.push(RedirectInfo {
source: crate::RedirectSource::Network,
from_url: final_url,
to_url: next_url.clone(),
@@ -1947,7 +1946,8 @@ impl RuntimeOwner {
collector.take_cookie_set_reports(),
)
};
- job.redirect_chain
+ job.request
+ .redirect_chain
.push(critical_client_hint_restart_redirect_info(
job.current_url.clone(),
network_response_extra_info(
@@ -2039,7 +2039,7 @@ impl RuntimeOwner {
&job.request.url,
&upgraded_url,
);
- job.redirect_chain.push(https_upgrade_redirect_info(
+ job.request.redirect_chain.push(https_upgrade_redirect_info(
job.current_url.clone(),
upgraded_url.clone(),
request_cookie_report,
@@ -2200,7 +2200,7 @@ impl RuntimeOwner {
tracing::debug!(url = %job.current_url, "failed to store raw streaming redirect response in disk cache: {error}");
}
let redirect_has_extra_info = request_extra_info.is_some();
- job.redirect_chain.push(RedirectInfo {
+ job.request.redirect_chain.push(RedirectInfo {
source: crate::RedirectSource::Network,
from_url: final_url,
to_url: next_url.clone(),
@@ -2273,8 +2273,8 @@ impl RuntimeOwner {
headers,
request_cookie_report,
cookie_set_reports,
- redirected: !job.redirect_chain.is_empty(),
- redirect_chain: job.redirect_chain,
+ redirected: !job.request.redirect_chain.is_empty(),
+ redirect_chain: job.request.redirect_chain,
from_cache: false,
negotiated_http_version,
network_request_extra_info: request_extra_info,
@@ -2341,7 +2341,7 @@ impl RuntimeOwner {
if let Some(next_url) = next_url
&& job.request.follow_redirects
{
- job.redirect_chain.push(RedirectInfo {
+ job.request.redirect_chain.push(RedirectInfo {
source: crate::RedirectSource::Network,
from_url: final_url,
to_url: next_url.clone(),
@@ -2403,7 +2403,7 @@ impl RuntimeOwner {
if let Some(next_url) = next_url
&& job.request.follow_redirects
{
- job.redirect_chain.push(RedirectInfo {
+ job.request.redirect_chain.push(RedirectInfo {
source: crate::RedirectSource::Network,
from_url: final_url,
to_url: next_url.clone(),
@@ -2448,7 +2448,6 @@ struct RuntimeJob {
request: Request,
current_url: Url,
current_cookie_context: NetworkCookieRequestContext,
- redirect_chain: Vec<RedirectInfo>,
redirect_count: usize,
origin_key: Option<CurlOriginKey>,
response_tx: RuntimeResponseTx,
@@ -2468,9 +2467,8 @@ impl RuntimeJob {
Self {
current_url: request.url.clone(),
current_cookie_context: request.cookie_context.clone(),
+ redirect_count: request.redirect_count(),
request,
- redirect_chain: Vec::new(),
- redirect_count: 0,
origin_key,
response_tx,
cancel_handle,
@@ -2485,7 +2483,6 @@ struct StreamingRuntimeJob {
request: Request,
current_url: Url,
current_cookie_context: NetworkCookieRequestContext,
- redirect_chain: Vec<RedirectInfo>,
redirect_count: usize,
origin_key: Option<CurlOriginKey>,
started_tx: Option<oneshot::Sender<Result<StreamingHtmlResponseStart>>>,
@@ -2510,9 +2507,8 @@ impl StreamingRuntimeJob {
Self {
current_url: request.url.clone(),
current_cookie_context: request.cookie_context.clone(),
+ redirect_count: request.redirect_count(),
request,
- redirect_chain: Vec::new(),
- redirect_count: 0,
origin_key,
started_tx: Some(started_tx),
body_tx: Some(body_tx),
@@ -2530,7 +2526,6 @@ struct StreamingRawRuntimeJob {
request: Request,
current_url: Url,
current_cookie_context: NetworkCookieRequestContext,
- redirect_chain: Vec<RedirectInfo>,
redirect_count: usize,
origin_key: Option<CurlOriginKey>,
started_tx: Option<oneshot::Sender<Result<StreamingHtmlResponseStart>>>,
@@ -2555,9 +2550,8 @@ impl StreamingRawRuntimeJob {
Self {
current_url: request.url.clone(),
current_cookie_context: request.cookie_context.clone(),
+ redirect_count: request.redirect_count(),
request,
- redirect_chain: Vec::new(),
- redirect_count: 0,
origin_key,
started_tx: Some(started_tx),
body_tx: Some(body_tx),
@@ -2857,8 +2851,8 @@ fn complete_cached_streaming_html_job(
cached: CachedStreamingResponseLookup,
request_cookie_report: Option<StoredCookieQueryReport>,
) {
- let redirected = !job.redirect_chain.is_empty();
- let redirect_chain = job.redirect_chain.clone();
+ let redirected = !job.request.redirect_chain.is_empty();
+ let redirect_chain = job.request.redirect_chain.clone();
let CachedStreamingResponseLookup {
final_url,
status,
@@ -2977,8 +2971,8 @@ fn complete_cached_streaming_raw_job(
request_cookie_report: Option<StoredCookieQueryReport>,
) {
job.cancel_handle.mark_response_terminal();
- let redirected = !job.redirect_chain.is_empty();
- let redirect_chain = job.redirect_chain.clone();
+ let redirected = !job.request.redirect_chain.is_empty();
+ let redirect_chain = job.request.redirect_chain.clone();
let CachedStreamingResponseLookup {
final_url,
status,
@@ -3327,7 +3321,9 @@ fn attach_next_request_extra_info(
request_cookie_report: Option<StoredCookieQueryReport>,
request_extra_info: Option<&NetworkRequestExtraInfo>,
) {
- if let Some(previous_redirect) = redirect_chain.last_mut() {
+ if let Some(previous_redirect) = redirect_chain.last_mut()
+ && previous_redirect.source != crate::RedirectSource::ServiceWorker
+ {
previous_redirect.request_cookie_report = request_cookie_report;
previous_redirect.request_extra_info = request_extra_info.cloned();
}
@@ -3475,7 +3471,7 @@ fn complete_streaming_proxy_connect_response(
) {
let start = proxy_connect_response_start(
&job.current_url,
- &job.redirect_chain,
+ &job.request.redirect_chain,
request_cookie_report,
response,
);
@@ -3500,7 +3496,7 @@ fn complete_raw_streaming_proxy_connect_response(
) {
let start = proxy_connect_response_start(
&job.current_url,
- &job.redirect_chain,
+ &job.request.redirect_chain,
request_cookie_report,
response,
);
@@ -3570,7 +3566,7 @@ fn fail_streaming_job_with_easy(
let error = network_fetch_failure_for_request(
&job.request,
&job.current_url,
- &job.redirect_chain,
+ &job.request.redirect_chain,
error,
);
if let Some(easy) = easy.as_mut()
@@ -3608,7 +3604,7 @@ fn fail_raw_streaming_job_with_easy(
let error = network_fetch_failure_for_request(
&job.request,
&job.current_url,
- &job.redirect_chain,
+ &job.request.redirect_chain,
error,
);
job.cancel_handle.mark_response_terminal();
diff --git a/moli-fetch/src/tests/tls_credentials.rs b/moli-fetch/src/tests/tls_credentials.rs
index 19a6ca7a5..87675b5af 100644
--- a/moli-fetch/src/tests/tls_credentials.rs
+++ b/moli-fetch/src/tests/tls_credentials.rs
@@ -24,7 +24,8 @@ use tokio_rustls::{
use url::Url;
use crate::{
- FetchCancelHandle, FetchClient, FetchClientHandle, FetchConfig, Request, RequestCredentialsMode,
+ FetchCancelHandle, FetchClient, FetchClientHandle, FetchConfig, RedirectInfo, RedirectSource,
+ Request, RequestCredentialsMode, RequestMode,
};
use super::support::unique_test_cache_dir;
@@ -310,14 +311,14 @@ async fn tls_client_certificate_isolated_on_reused_connections() -> Result<()> {
}
#[tokio::test]
-async fn tls_client_certificate_uses_current_redirect_origin() -> Result<()> {
+async fn tls_client_certificate_preserves_cross_origin_redirect_taint() -> Result<()> {
let credentials = TlsCredentials::new()?;
let server = TlsServer::spawn(&credentials).await?;
for transport in Transport::ALL {
for (mode, expected_authentication) in [
(RequestCredentialsMode::Include, [true, true, true]),
(RequestCredentialsMode::Omit, [false, false, false]),
- (RequestCredentialsMode::SameOrigin, [true, false, true]),
+ (RequestCredentialsMode::SameOrigin, [true, false, false]),
] {
let client = FetchClient::new(
&credentials.fetch_config(),
@@ -325,6 +326,7 @@ async fn tls_client_certificate_uses_current_redirect_origin() -> Result<()> {
);
let request = Request::get(server.url.join("/redirect-cross")?.as_str())?
.with_initiator_url(&server.url)
+ .with_request_mode(RequestMode::Cors)
.with_credentials_mode(mode);
transport.fetch(&client, request).await?;
let observed = std::mem::take(&mut *server.requests.lock());
@@ -351,3 +353,55 @@ async fn tls_client_certificate_uses_current_redirect_origin() -> Result<()> {
}
Ok(())
}
+
+#[tokio::test]
+async fn tls_client_certificate_preserves_service_worker_redirect_taint() -> Result<()> {
+ let credentials = TlsCredentials::new()?;
+ let server = TlsServer::spawn(&credentials).await?;
+ let mut cross_origin = server.url.clone();
+ cross_origin.set_host(Some("localhost"))?;
+ for transport in Transport::ALL {
+ let client = FetchClient::new(
+ &credentials.fetch_config(),
+ new_shared_browser_cookie_store(),
+ );
+ // Populate an authenticated keep-alive connection before handing a
+ // redirected same-origin-credentials request to this same endpoint.
+ transport
+ .fetch(&client, Request::get(server.url.as_str())?)
+ .await?;
+ let request = Request::get(server.url.as_str())?
+ .with_initiator_url(&server.url)
+ .with_request_mode(RequestMode::Cors)
+ .with_credentials_mode(RequestCredentialsMode::SameOrigin)
+ .with_redirect_chain(vec![RedirectInfo {
+ source: RedirectSource::ServiceWorker,
+ from_url: cross_origin.clone(),
+ to_url: server.url.clone(),
+ status: 302,
+ headers: vec![("Location".to_owned(), server.url.to_string())],
+ network_extra_info_available: false,
+ request_extra_info: None,
+ response_extra_info: None,
+ redirect_has_extra_info: false,
+ request_cookie_report: None,
+ cookie_set_reports: Vec::new(),
+ from_cache: false,
+ negotiated_http_version: None,
+ }]);
+ transport.fetch(&client, request).await?;
+ let observed = std::mem::take(&mut *server.requests.lock());
+ assert_eq!(observed.len(), 2, "{transport:?}");
+ assert_eq!(
+ observed[0].client_certificates.as_slice(),
+ std::slice::from_ref(&credentials.client_certificate)
+ );
+ assert!(
+ observed[1].client_certificates.is_empty(),
+ "{transport:?}: {observed:?}"
+ );
+ assert_ne!(observed[0].connection_id, observed[1].connection_id);
+ assert!(client.shutdown().is_clean());
+ }
+ Ok(())
+}
diff --git a/moli-renderer-v8/src/network/backend/memory_cache.rs b/moli-renderer-v8/src/network/backend/memory_cache.rs
index 060812f0c..a832b0bad 100644
--- a/moli-renderer-v8/src/network/backend/memory_cache.rs
+++ b/moli-renderer-v8/src/network/backend/memory_cache.rs
@@ -575,6 +575,9 @@ impl SharedMemoryResourceCache {
pub(in crate::network) fn script_text_request_is_memory_cacheable(request: &Request) -> bool {
request.cache_mode().allows_memory_cache_store()
+ // These entries retain a complete response including its redirect
+ // history. A resumed fetch must assemble its own history in transport.
+ && request.redirect_chain().is_empty()
&& request.subresource_request_metadata().is_some()
&& request.method.eq_ignore_ascii_case("GET")
&& request.body.is_none()
@@ -648,6 +651,7 @@ fn raw_subresource_cache_resource_type_key(resource_type: RequestResourceType) -
fn raw_subresource_request_is_memory_cacheable(request: &Request) -> bool {
request.cache_mode().allows_memory_cache_store()
+ && request.redirect_chain().is_empty()
&& raw_subresource_memory_cacheable_resource_type(request)
&& request.method.eq_ignore_ascii_case("GET")
&& request.body.is_none()
diff --git a/moli-renderer-v8/src/network_host.rs b/moli-renderer-v8/src/network_host.rs
index 500bf893b..fe4463ee5 100644
--- a/moli-renderer-v8/src/network_host.rs
+++ b/moli-renderer-v8/src/network_host.rs
@@ -33,7 +33,7 @@ pub(crate) use self::async_fetch::{
fetch_browser_subresource_with_preflight_and_network_metadata,
fetch_browser_subresource_with_preflight_headers,
fetch_browser_subresource_with_preflight_headers_and_network_metadata,
- spawn_async_subresource_fetch, spawn_async_subresource_fetch_with_redirect_chain,
+ spawn_async_subresource_fetch,
};
pub(crate) use self::beacon::{navigator_send_beacon_callback, send_link_audit_ping};
pub(super) use self::bindings::install_window_network_bindings;
@@ -145,6 +145,7 @@ pub(crate) use self::response::{
build_fetch_response_object_from_body_source_for_request_mode,
build_fetch_response_object_from_body_source_for_request_mode_with_filter,
build_fetch_response_object_from_stream_for_request_mode,
+ build_fetch_response_object_from_stream_for_request_mode_with_filter,
build_fetch_response_object_from_subresource_body_for_request_mode,
build_filtered_cached_response_object,
build_navigation_preload_response_object_from_stream_for_request_mode,
@@ -158,6 +159,7 @@ pub(crate) use self::response::{
validate_cross_origin_resource_policy, validate_fetch_response_security_policy,
validate_fetch_response_security_policy_with_body,
validate_fetch_response_security_policy_with_body_classified,
+ validate_opaque_response_blocking_with_body,
};
pub(crate) use self::stylesheet_subresource::{
StylesheetSubresourceFetchStart, start_stylesheet_subresource_fetch,
diff --git a/moli-renderer-v8/src/network_host/async_fetch.rs b/moli-renderer-v8/src/network_host/async_fetch.rs
index 9c84a48cc..24fd2b529 100644
--- a/moli-renderer-v8/src/network_host/async_fetch.rs
+++ b/moli-renderer-v8/src/network_host/async_fetch.rs
@@ -1,3 +1,4 @@
+use super::response::validate_cors_response_for_origin;
use super::*;
use moli_fetch::{
BrowserRequestMetadata, FetchCancelHandle, NetworkFetchResult, RedirectInfo,
@@ -124,7 +125,6 @@ enum ManualCorsRedirectTransition {
struct ManualCorsRedirectState {
request: Request,
preflight_request_headers: Vec<(String, String)>,
- redirect_chain: Vec<RedirectInfo>,
}
impl ManualCorsRedirectState {
@@ -132,7 +132,6 @@ impl ManualCorsRedirectState {
Self {
request,
preflight_request_headers,
- redirect_chain: Vec::new(),
}
}
@@ -184,7 +183,7 @@ impl ManualCorsRedirectState {
&head.final_url,
head.status,
&head.headers,
- self.redirect_chain.len(),
+ self.request.redirect_count(),
)?
else {
return Ok(ManualCorsRedirectTransition::FinalResponse);
@@ -203,7 +202,7 @@ impl ManualCorsRedirectState {
}
}
let redirect_status = head.status;
- self.redirect_chain.push(RedirectInfo {
+ self.request.record_redirect(RedirectInfo {
source: moli_fetch::RedirectSource::Network,
from_url: head.final_url,
to_url: next_url.clone(),
@@ -225,7 +224,7 @@ impl ManualCorsRedirectState {
}
fn into_redirect_chain(self) -> Vec<RedirectInfo> {
- self.redirect_chain
+ self.request.redirect_chain().to_vec()
}
}
@@ -317,24 +316,15 @@ fn validate_actual_cors_response_head(
request: &Request,
response: &ResponseHead,
) -> Result<(), String> {
- validate_actual_cors_response_parts(request, &response.final_url, &response.headers)
-}
-
-fn validate_actual_cors_response_parts(
- request: &Request,
- response_url: &url::Url,
- response_headers: &[(String, String)],
-) -> Result<(), String> {
- let Some(initiator_url) = request.cookie_context.initiator_url.as_ref() else {
- return Ok(());
- };
- if request.request_mode == RequestMode::NoCors {
+ if request.request_mode == RequestMode::NoCors
+ || !matches!(response.final_url.scheme(), "http" | "https")
+ || !request.has_cross_origin_url(&response.final_url)
+ {
return Ok(());
}
- validate_cors_response(
- initiator_url,
- response_url,
- response_headers,
+ validate_cors_response_for_origin(
+ &request.serialized_origin(),
+ &response.headers,
request.credentials_mode,
)
}
@@ -424,7 +414,7 @@ async fn run_cors_preflight_if_needed(
if let Some(initiator_url) = request.cookie_context.initiator_url.clone()
&& request.request_mode != RequestMode::NoCors
&& let Some(preflight_headers) = cors_preflight_request_headers(
- &initiator_url,
+ request.has_cross_origin_url(&request.url),
&request.url,
&request.method,
preflight_request_headers,
@@ -436,6 +426,7 @@ async fn run_cors_preflight_if_needed(
.map_err(|error| format!("cors preflight: failed to build request: {error}"))?
.with_initiator_url(&initiator_url)
.with_credentials_mode(RequestCredentialsMode::SameOrigin)
+ .with_redirect_chain(request.redirect_chain().to_vec())
.with_network_partition_key(request.network_partition_key().map(str::to_owned));
if let Some(metadata) = request.browser_request_metadata() {
preflight_request = preflight_request.with_browser_request_metadata(metadata);
@@ -470,15 +461,15 @@ async fn run_cors_preflight_if_needed(
&preflight_response,
);
}
- if preflight_response.redirected {
+ if preflight_response.redirect_chain.len() > request.redirect_chain().len() {
return Err(format!(
"CORS preflight failed: preflight request redirected to {}",
preflight_response.final_url
));
}
validate_cors_preflight_response(
- &initiator_url,
- &preflight_response.final_url,
+ &request.serialized_origin(),
+ request.credentials_mode,
&request.method,
preflight_request_headers,
preflight_response.status,
@@ -501,38 +492,6 @@ pub(crate) fn spawn_async_subresource_fetch(
request_method: String,
request_headers: Vec<(String, String)>,
request_body: Option<String>,
-) {
- spawn_async_subresource_fetch_with_redirect_chain(
- task_runner,
- completion_tx,
- loader,
- request,
- cancel_handle,
- preflight_request_headers,
- Vec::new(),
- internal_id,
- network_context,
- request_url,
- request_method,
- request_headers,
- request_body,
- );
-}
-
-pub(crate) fn spawn_async_subresource_fetch_with_redirect_chain(
- task_runner: crate::network::RendererResourceTaskRunner,
- completion_tx: RendererResourceCompletionSender,
- loader: ResourceRequestClient,
- request: Request,
- cancel_handle: Option<FetchCancelHandle>,
- preflight_request_headers: Vec<(String, String)>,
- initial_redirect_chain: Vec<RedirectInfo>,
- internal_id: u64,
- network_context: AsyncSubresourceNetworkContext,
- request_url: url::Url,
- request_method: String,
- request_headers: Vec<(String, String)>,
- request_body: Option<String>,
) {
task_runner.spawn(async move {
let preflight_observer =
@@ -577,13 +536,7 @@ pub(crate) fn spawn_async_subresource_fetch_with_redirect_chain(
)
.await
.map(|observed| {
- let (mut response, request_observation) = observed.into_parts();
- if !initial_redirect_chain.is_empty() {
- let mut redirect_chain = initial_redirect_chain;
- redirect_chain.append(&mut response.redirect_chain);
- response.redirect_chain = redirect_chain;
- response.redirected = true;
- }
+ let (response, request_observation) = observed.into_parts();
crate::protocol_types::NavigationResponse::from(response)
.with_network_request_headers(
request_observation.map(|observation| observation.into_headers()),
@@ -620,7 +573,6 @@ pub(crate) fn spawn_async_subresource_fetch_with_redirect_chain(
request_method_for_event,
request_headers_for_event,
request_body_for_event,
- initial_redirect_chain,
)
.await;
if let Err(error) = result {
@@ -652,7 +604,6 @@ async fn fetch_browser_subresource_streaming_with_preflight_headers(
request_method: String,
request_headers: Vec<(String, String)>,
request_body: Option<String>,
- initial_redirect_chain: Vec<RedirectInfo>,
) -> Result<(), String> {
let body_source_id = new_network_body_source_id();
let requires_manual_preflight_redirects =
@@ -677,15 +628,11 @@ async fn fetch_browser_subresource_streaming_with_preflight_headers(
.await?
};
let (mut response, request_observation) = observed.into_parts();
- let mut head = response.head();
- if !initial_redirect_chain.is_empty() {
- let mut redirect_chain = initial_redirect_chain;
- redirect_chain.append(&mut head.redirect_chain);
- head.redirect_chain = redirect_chain;
- head.redirected = true;
- }
+ let head = response.head();
let _ = completion_tx.send_async_subresource_event(
AsyncSubresourceFetchEvent::StreamingStarted(Box::new(AsyncSubresourceStreamingStarted {
+ skip_fetch_security_validation: false,
+ response_filter: None,
internal_id,
request_url,
request_method,
@@ -895,9 +842,9 @@ mod tests {
redirects.preflight_request_headers(),
&[("X-Challenge".to_owned(), "yes".to_owned())]
);
- assert_eq!(redirects.redirect_chain.len(), 1);
- assert_eq!(redirects.redirect_chain[0].status, 303);
- assert!(redirects.redirect_chain[0].network_extra_info_available);
+ assert_eq!(redirects.request().redirect_chain().len(), 1);
+ assert_eq!(redirects.request().redirect_chain()[0].status, 303);
+ assert!(redirects.request().redirect_chain()[0].network_extra_info_available);
Ok(())
}
@@ -1271,14 +1218,13 @@ mod tests {
negotiated_http_version: None,
}];
- spawn_async_subresource_fetch_with_redirect_chain(
+ spawn_async_subresource_fetch(
crate::network::RendererResourceTaskRunner::from_current_tokio()?,
queue.sender(),
loader,
- request,
+ request.with_redirect_chain(initial_redirect_chain),
Some(FetchCancelHandle::new()),
Vec::new(),
- initial_redirect_chain,
74,
AsyncSubresourceNetworkContext {
frame_id: None,
diff --git a/moli-renderer-v8/src/network_host/fetch/bindings/paths.rs b/moli-renderer-v8/src/network_host/fetch/bindings/paths.rs
index ebca320f8..b0de5f165 100644
--- a/moli-renderer-v8/src/network_host/fetch/bindings/paths.rs
+++ b/moli-renderer-v8/src/network_host/fetch/bindings/paths.rs
@@ -235,7 +235,7 @@ pub(super) fn spawn_network_fetch(
let cancel_handle = FetchCancelHandle::new();
let requires_preflight = prepared.request_mode != moli_fetch::RequestMode::NoCors
&& crate::network_host::cors_preflight_request_headers(
- &prepared.document_url,
+ !moli_url::same_origin(&prepared.document_url, &prepared.resolved_url),
&prepared.resolved_url,
&prepared.method,
&prepared.cors_preflight_request_headers,
diff --git a/moli-renderer-v8/src/network_host/fetch/bindings/service_worker.rs b/moli-renderer-v8/src/network_host/fetch/bindings/service_worker.rs
index 01e0ea95e..b2877026a 100644
--- a/moli-renderer-v8/src/network_host/fetch/bindings/service_worker.rs
+++ b/moli-renderer-v8/src/network_host/fetch/bindings/service_worker.rs
@@ -42,7 +42,7 @@ pub(super) fn dispatch_service_worker_fetch(
};
let requires_preflight = prepared.request_mode != moli_fetch::RequestMode::NoCors
&& crate::network_host::cors_preflight_request_headers(
- &prepared.document_url,
+ !moli_url::same_origin(&prepared.document_url, &prepared.resolved_url),
&prepared.resolved_url,
&prepared.method,
&prepared.cors_preflight_request_headers,
diff --git a/moli-renderer-v8/src/network_host/image.rs b/moli-renderer-v8/src/network_host/image.rs
index c1e0f11bb..90ae7b0a3 100644
--- a/moli-renderer-v8/src/network_host/image.rs
+++ b/moli-renderer-v8/src/network_host/image.rs
@@ -134,8 +134,7 @@ pub(crate) fn start_scanned_image_preload(
response.redirect_chain.is_empty()
&& validate_fetch_response_security_policy_with_body(
&document_url,
- &response.final_url,
- &response.headers,
+ &response.head(),
response.body_bytes(),
RequestMode::NoCors,
RequestCredentialsMode::Include,
diff --git a/moli-renderer-v8/src/network_host/response.rs b/moli-renderer-v8/src/network_host/response.rs
index d38089c00..a770b0a18 100644
--- a/moli-renderer-v8/src/network_host/response.rs
+++ b/moli-renderer-v8/src/network_host/response.rs
@@ -16,10 +16,12 @@ pub(crate) use self::cors::{
FetchResponseSecurityViolation, cors_preflight_request_headers,
filter_cors_exposed_response_headers, is_cors_policy_failure_message,
validate_cors_preflight_response, validate_cors_response, validate_cors_response_chain,
+ validate_cors_response_for_origin,
validate_cross_origin_embedder_and_document_isolation_policy,
validate_cross_origin_resource_policy, validate_fetch_response_security_policy,
validate_fetch_response_security_policy_with_body,
validate_fetch_response_security_policy_with_body_classified,
+ validate_opaque_response_blocking_with_body,
};
#[cfg(test)]
pub(crate) use self::materialize::materialize_response_object;
@@ -29,6 +31,7 @@ pub(crate) use self::materialize::{
build_fetch_response_object_from_body_source_for_request_mode,
build_fetch_response_object_from_body_source_for_request_mode_with_filter,
build_fetch_response_object_from_stream_for_request_mode,
+ build_fetch_response_object_from_stream_for_request_mode_with_filter,
build_fetch_response_object_from_subresource_body_for_request_mode,
build_filtered_cached_response_object,
build_navigation_preload_response_object_from_stream_for_request_mode,
diff --git a/moli-renderer-v8/src/network_host/response/cors.rs b/moli-renderer-v8/src/network_host/response/cors.rs
index 341211568..882663472 100644
--- a/moli-renderer-v8/src/network_host/response/cors.rs
+++ b/moli-renderer-v8/src/network_host/response/cors.rs
@@ -84,7 +84,7 @@ pub(crate) fn validate_cors_response_chain(
Ok(())
}
-fn validate_cors_response_for_origin(
+pub(crate) fn validate_cors_response_for_origin(
origin: &str,
response_headers: &[(String, String)],
credentials_mode: RequestCredentialsMode,
@@ -128,38 +128,31 @@ fn validate_cors_response_for_origin(
pub(crate) fn validate_fetch_response_security_policy(
document_url: &url::Url,
- response_url: &url::Url,
- response_headers: &[(String, String)],
+ head: &moli_fetch::ResponseHead,
request_mode: RequestMode,
credentials_mode: RequestCredentialsMode,
policy_context: crate::types::SubresourcePolicyContext,
) -> Result<(), String> {
if request_mode == RequestMode::NoCors {
- validate_cross_origin_resource_policy(document_url, response_url, response_headers)?;
+ validate_cross_origin_resource_policy(document_url, &head.final_url, &head.headers)?;
validate_cross_origin_embedder_and_document_isolation_policy(
document_url,
- response_url,
- response_headers,
+ &head.final_url,
+ &head.headers,
request_mode,
credentials_mode,
policy_context.cross_origin_embedder_policy,
policy_context.document_isolation_policy,
)?;
- validate_opaque_response_blocking(document_url, response_url, response_headers)
+ validate_opaque_response_blocking(document_url, &head.final_url, &head.headers)
} else {
- validate_cors_response(
- document_url,
- response_url,
- response_headers,
- credentials_mode,
- )
+ validate_cors_response_chain(document_url, head, credentials_mode)
}
}
pub(crate) fn validate_fetch_response_security_policy_with_body(
document_url: &url::Url,
- response_url: &url::Url,
- response_headers: &[(String, String)],
+ head: &moli_fetch::ResponseHead,
response_body: &[u8],
request_mode: RequestMode,
credentials_mode: RequestCredentialsMode,
@@ -167,8 +160,7 @@ pub(crate) fn validate_fetch_response_security_policy_with_body(
) -> Result<(), String> {
validate_fetch_response_security_policy_with_body_classified(
document_url,
- response_url,
- response_headers,
+ head,
response_body,
request_mode,
credentials_mode,
@@ -179,20 +171,19 @@ pub(crate) fn validate_fetch_response_security_policy_with_body(
pub(crate) fn validate_fetch_response_security_policy_with_body_classified(
document_url: &url::Url,
- response_url: &url::Url,
- response_headers: &[(String, String)],
+ head: &moli_fetch::ResponseHead,
response_body: &[u8],
request_mode: RequestMode,
credentials_mode: RequestCredentialsMode,
policy_context: crate::types::SubresourcePolicyContext,
) -> Result<(), FetchResponseSecurityViolation> {
if request_mode == RequestMode::NoCors {
- validate_cross_origin_resource_policy(document_url, response_url, response_headers)
+ validate_cross_origin_resource_policy(document_url, &head.final_url, &head.headers)
.map_err(FetchResponseSecurityViolation::Rejected)?;
validate_cross_origin_embedder_and_document_isolation_policy(
document_url,
- response_url,
- response_headers,
+ &head.final_url,
+ &head.headers,
request_mode,
credentials_mode,
policy_context.cross_origin_embedder_policy,
@@ -201,19 +192,14 @@ pub(crate) fn validate_fetch_response_security_policy_with_body_classified(
.map_err(FetchResponseSecurityViolation::Rejected)?;
validate_opaque_response_blocking_with_body(
document_url,
- response_url,
- response_headers,
+ &head.final_url,
+ &head.headers,
response_body,
)
.map_err(FetchResponseSecurityViolation::OpaqueResponseBlocked)
} else {
- validate_cors_response(
- document_url,
- response_url,
- response_headers,
- credentials_mode,
- )
- .map_err(FetchResponseSecurityViolation::Rejected)
+ validate_cors_response_chain(document_url, head, credentials_mode)
+ .map_err(FetchResponseSecurityViolation::Rejected)
}
}
@@ -400,12 +386,12 @@ pub(crate) fn validate_cross_origin_resource_policy(
}
pub(crate) fn cors_preflight_request_headers(
- document_url: &url::Url,
+ cors_tainted: bool,
request_url: &url::Url,
method: &str,
request_headers: &[(String, String)],
) -> Option<Vec<(String, String)>> {
- if same_origin(document_url, request_url) {
+ if !cors_tainted {
return None;
}
if !matches!(request_url.scheme(), "http" | "https") {
@@ -432,8 +418,8 @@ pub(crate) fn cors_preflight_request_headers(
}
pub(crate) fn validate_cors_preflight_response(
- document_url: &url::Url,
- response_url: &url::Url,
+ origin: &str,
+ credentials_mode: RequestCredentialsMode,
requested_method: &str,
request_headers: &[(String, String)],
response_status: u16,
@@ -444,12 +430,7 @@ pub(crate) fn validate_cors_preflight_response(
"CORS preflight failed: response status {response_status}"
));
}
- validate_cors_response(
- document_url,
- response_url,
- response_headers,
- RequestCredentialsMode::SameOrigin,
- )?;
+ validate_cors_response_for_origin(origin, response_headers, credentials_mode)?;
if !moli_fetch::is_cors_safelisted_method(requested_method) {
let Some(allow_methods) =
@@ -499,14 +480,14 @@ pub(crate) fn validate_cors_preflight_response(
pub(crate) fn filter_cors_exposed_response_headers(
document_url: &url::Url,
- response_url: &url::Url,
- response_headers: &[(String, String)],
+ head: &moli_fetch::ResponseHead,
credentials_mode: RequestCredentialsMode,
) -> Vec<(String, String)> {
- if same_origin(document_url, response_url) {
+ let response_headers = &head.headers;
+ if !super::materialize::response_has_cross_origin_url(document_url, head) {
return response_headers.to_vec();
}
- if !matches!(response_url.scheme(), "http" | "https") {
+ if !matches!(head.final_url.scheme(), "http" | "https") {
return response_headers.to_vec();
}
@@ -575,6 +556,23 @@ mod tests {
url::Url::parse(value).expect("valid URL")
}
+ fn header_response(
+ final_url: url::Url,
+ headers: Vec<(String, String)>,
+ ) -> moli_fetch::ResponseHead {
+ moli_fetch::ResponseHead {
+ final_url,
+ status: 200,
+ headers,
+ request_cookie_report: None,
+ cookie_set_reports: Vec::new(),
+ redirected: false,
+ redirect_chain: Vec::new(),
+ from_cache: false,
+ negotiated_http_version: None,
+ }
+ }
+
#[test]
fn cors_response_chain_checks_network_redirects_without_extra_info() {
for from_cache in [false, true] {
@@ -629,12 +627,8 @@ mod tests {
("X-Other".to_owned(), "ok".to_owned()),
];
- let preflight = cors_preflight_request_headers(
- &url("http://example.test/page"),
- &url("http://other.test/data"),
- "PUT",
- &headers,
- );
+ let preflight =
+ cors_preflight_request_headers(true, &url("http://other.test/data"), "PUT", &headers);
assert_eq!(
preflight,
@@ -660,12 +654,7 @@ mod tests {
];
assert_eq!(
- cors_preflight_request_headers(
- &url("http://example.test/page"),
- &url("http://other.test/data"),
- "POST",
- &headers,
- ),
+ cors_preflight_request_headers(true, &url("http://other.test/data"), "POST", &headers,),
None
);
}
@@ -693,8 +682,8 @@ mod tests {
assert_eq!(
validate_cors_preflight_response(
- &url("http://example.test/page"),
- &url("http://other.test/data"),
+ "http://example.test",
+ RequestCredentialsMode::SameOrigin,
"POST",
&request_headers,
204,
@@ -707,7 +696,6 @@ mod tests {
#[test]
fn validate_cors_preflight_response_allows_safelisted_methods_without_allow_methods() {
let document_url = url::Url::parse("https://origin.test/page").unwrap();
- let response_url = url::Url::parse("https://api.test/data").unwrap();
let response_headers = vec![
("Access-Control-Allow-Origin".to_owned(), "*".to_owned()),
(
@@ -718,8 +706,8 @@ mod tests {
for method in ["GET", "HEAD", "POST"] {
validate_cors_preflight_response(
- &document_url,
- &response_url,
+ &origin_ascii_serialization(&document_url),
+ RequestCredentialsMode::SameOrigin,
method,
&[("Content-Type".to_owned(), "custom/type".to_owned())],
200,
@@ -736,7 +724,6 @@ mod tests {
#[test]
fn validate_cors_preflight_response_rejects_unsafelisted_method_without_allow_methods() {
let document_url = url::Url::parse("https://origin.test/page").unwrap();
- let response_url = url::Url::parse("https://api.test/data").unwrap();
let response_headers = vec![
("Access-Control-Allow-Origin".to_owned(), "*".to_owned()),
(
@@ -746,8 +733,8 @@ mod tests {
];
let error = validate_cors_preflight_response(
- &document_url,
- &response_url,
+ &origin_ascii_serialization(&document_url),
+ RequestCredentialsMode::SameOrigin,
"PUT",
&[("Content-Type".to_owned(), "custom/type".to_owned())],
200,
@@ -773,8 +760,7 @@ mod tests {
let filtered = filter_cors_exposed_response_headers(
&url("http://example.test/page"),
- &url("http://other.test/data"),
- &headers,
+ &header_response(url("http://other.test/data"), headers.clone()),
RequestCredentialsMode::SameOrigin,
);
@@ -798,16 +784,14 @@ mod tests {
let non_credentialed = filter_cors_exposed_response_headers(
&url("http://example.test/page"),
- &url("http://other.test/data"),
- &headers,
+ &header_response(url("http://other.test/data"), headers.clone()),
RequestCredentialsMode::SameOrigin,
);
assert_eq!(non_credentialed, headers);
let credentialed = filter_cors_exposed_response_headers(
&url("http://example.test/page"),
- &url("http://other.test/data"),
- &headers,
+ &header_response(url("http://other.test/data"), headers.clone()),
RequestCredentialsMode::Include,
);
assert_eq!(
@@ -828,8 +812,7 @@ mod tests {
let filtered = filter_cors_exposed_response_headers(
&url("http://example.test/page"),
- &url("http://example.test/data"),
- &headers,
+ &header_response(url("http://example.test/data"), headers.clone()),
RequestCredentialsMode::Include,
);
diff --git a/moli-renderer-v8/src/network_host/response/materialize.rs b/moli-renderer-v8/src/network_host/response/materialize.rs
index b52482ed0..6d41c7209 100644
--- a/moli-renderer-v8/src/network_host/response/materialize.rs
+++ b/moli-renderer-v8/src/network_host/response/materialize.rs
@@ -16,13 +16,23 @@ fn is_redirect_status(status: u16) -> bool {
#[derive(Clone, Copy, PartialEq, Eq)]
enum FetchResponseFilter {
None,
+ Basic,
+ Cors,
Opaque,
OpaqueRedirect,
}
+impl FetchResponseFilter {
+ fn is_readable(self) -> bool {
+ matches!(self, Self::None | Self::Basic | Self::Cors)
+ }
+}
+
impl From<crate::types::AsyncSubresourceFetchResponseFilter> for FetchResponseFilter {
fn from(value: crate::types::AsyncSubresourceFetchResponseFilter) -> Self {
match value {
+ crate::types::AsyncSubresourceFetchResponseFilter::Basic => Self::Basic,
+ crate::types::AsyncSubresourceFetchResponseFilter::Cors => Self::Cors,
crate::types::AsyncSubresourceFetchResponseFilter::Opaque => Self::Opaque,
crate::types::AsyncSubresourceFetchResponseFilter::OpaqueRedirect => {
Self::OpaqueRedirect
@@ -86,7 +96,8 @@ pub(crate) fn network_response_filter(
if is_redirect_status(head.status) {
Some(AsyncSubresourceFetchResponseFilter::OpaqueRedirect)
- } else if request_mode == RequestMode::NoCors && no_cors_response_is_opaque(document_url, head)
+ } else if request_mode == RequestMode::NoCors
+ && response_has_cross_origin_url(document_url, head)
{
Some(AsyncSubresourceFetchResponseFilter::Opaque)
} else {
@@ -94,7 +105,10 @@ pub(crate) fn network_response_filter(
}
}
-fn no_cors_response_is_opaque(document_url: &url::Url, head: &moli_fetch::ResponseHead) -> bool {
+pub(super) fn response_has_cross_origin_url(
+ document_url: &url::Url,
+ head: &moli_fetch::ResponseHead,
+) -> bool {
!moli_url::same_origin(document_url, &head.final_url)
|| head.redirect_chain.iter().any(|redirect| {
!moli_url::same_origin(document_url, &redirect.from_url)
@@ -104,62 +118,50 @@ fn no_cors_response_is_opaque(document_url: &url::Url, head: &moli_fetch::Respon
fn compute_fetch_response_type(
document_url: &url::Url,
- response_url: &url::Url,
+ head: &moli_fetch::ResponseHead,
filter: FetchResponseFilter,
) -> &'static str {
- // The fixtures that drove this helper were checking `Response.type`, not just status/body.
- // Returning `"basic"` unconditionally looked harmless at first because our fetch stack does
- // not yet model the full Fetch standard response filtering pipeline (`opaque`,
- // `opaqueredirect`, etc.). In practice that shortcut breaks a useful compatibility signal:
- // browser-facing code distinguishes same-origin subresource fetches from cross-origin ones
- // through `Response.type`.
- //
- // We intentionally keep the rule narrow and deterministic here:
- // - same origin => `basic`
- // - different origin => `cors`
- //
- // This is not a complete Fetch implementation, but it preserves the observable behavior that
- // our current runtime can support without pretending every response is same-origin.
+ // Returning to the initiating origin does not undo response tainting.
match filter {
+ FetchResponseFilter::Basic => "basic",
+ FetchResponseFilter::Cors => "cors",
FetchResponseFilter::Opaque => "opaque",
FetchResponseFilter::OpaqueRedirect => "opaqueredirect",
- FetchResponseFilter::None if moli_url::same_origin(document_url, response_url) => "basic",
- FetchResponseFilter::None => "cors",
+ FetchResponseFilter::None if response_has_cross_origin_url(document_url, head) => "cors",
+ FetchResponseFilter::None => "basic",
}
}
fn filtered_response_status(head: &moli_fetch::ResponseHead, filter: FetchResponseFilter) -> u16 {
- if filter == FetchResponseFilter::None {
- head.status
- } else {
- 0
- }
+ if filter.is_readable() { head.status } else { 0 }
}
fn filtered_response_url(head: &moli_fetch::ResponseHead, filter: FetchResponseFilter) -> &str {
match filter {
FetchResponseFilter::Opaque | FetchResponseFilter::OpaqueRedirect => "",
- FetchResponseFilter::None => head.final_url.as_str(),
+ FetchResponseFilter::None | FetchResponseFilter::Basic | FetchResponseFilter::Cors => {
+ head.final_url.as_str()
+ }
}
}
fn filtered_response_exposes_body(filter: FetchResponseFilter) -> bool {
- filter == FetchResponseFilter::None
+ filter.is_readable()
}
fn filtered_response_exposes_redirected(filter: FetchResponseFilter) -> bool {
- filter == FetchResponseFilter::None
+ filter.is_readable()
}
fn filtered_response_exposes_headers(filter: FetchResponseFilter) -> bool {
- filter == FetchResponseFilter::None
+ filter.is_readable()
}
fn filtered_response_status_text(
head: &moli_fetch::ResponseHead,
filter: FetchResponseFilter,
) -> &'static str {
- if filter == FetchResponseFilter::None {
+ if filter.is_readable() {
http_status_text(head.status)
} else {
""
@@ -252,6 +254,24 @@ pub(crate) fn build_fetch_response_object_from_stream_for_request_mode<'s>(
request_mode: RequestMode,
head: moli_fetch::ResponseHead,
body_source_id: NetworkBodySourceId,
+) -> v8::Local<'s, v8::Object> {
+ build_fetch_response_object_from_stream_for_request_mode_with_filter(
+ scope,
+ document_url,
+ request_mode,
+ head,
+ body_source_id,
+ None,
+ )
+}
+
+pub(crate) fn build_fetch_response_object_from_stream_for_request_mode_with_filter<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ document_url: &url::Url,
+ request_mode: RequestMode,
+ head: moli_fetch::ResponseHead,
+ body_source_id: NetworkBodySourceId,
+ filter_override: Option<crate::types::AsyncSubresourceFetchResponseFilter>,
) -> v8::Local<'s, v8::Object> {
build_fetch_response_object_from_stream_for_request_mode_with_surface_url(
scope,
@@ -260,6 +280,7 @@ pub(crate) fn build_fetch_response_object_from_stream_for_request_mode<'s>(
head,
body_source_id,
None,
+ filter_override,
)
}
@@ -277,6 +298,7 @@ pub(crate) fn build_navigation_preload_response_object_from_stream_for_request_m
head,
body_source_id,
Some(request_url.as_str()),
+ None,
)
}
@@ -287,8 +309,11 @@ fn build_fetch_response_object_from_stream_for_request_mode_with_surface_url<'s>
head: moli_fetch::ResponseHead,
body_source_id: NetworkBodySourceId,
filtered_surface_url: Option<&str>,
+ filter_override: Option<crate::types::AsyncSubresourceFetchResponseFilter>,
) -> v8::Local<'s, v8::Object> {
- let filter = response_filter(document_url, &head, request_mode);
+ let filter = filter_override
+ .map(FetchResponseFilter::from)
+ .unwrap_or_else(|| response_filter(document_url, &head, request_mode));
let filtered_surface_url = (filter == FetchResponseFilter::OpaqueRedirect)
.then_some(filtered_surface_url)
.flatten();
@@ -310,10 +335,7 @@ fn build_fetch_response_object_head<'s>(
filtered_surface_url: Option<&str>,
) -> v8::Local<'s, v8::Object> {
let status = filtered_response_status(head, filter);
- // `Response.type` must be derived from the final resolved URL, not the request URL string or
- // the redirect start point. A redirect chain can cross origins, and the JS surface is meant
- // to describe the response object the page actually observes after redirects settle.
- let response_type = compute_fetch_response_type(document_url, &head.final_url, filter);
+ let response_type = compute_fetch_response_type(document_url, head, filter);
let obj = FetchResponseHeadDeclaration::new(
status as f64,
(200..300).contains(&status),
@@ -328,7 +350,7 @@ fn build_fetch_response_object_head<'s>(
FetchResponseInternalUrlDeclaration::new(head.final_url.to_string())
.initialize(scope, obj)
.expect("Fetch Response internal URL declaration should initialize");
- if filter != FetchResponseFilter::None {
+ if !filter.is_readable() {
set_response_slot_value(
scope,
obj,
diff --git a/moli-renderer-v8/src/network_host/xhr/send.rs b/moli-renderer-v8/src/network_host/xhr/send.rs
index ed5690182..9fbc73818 100644
--- a/moli-renderer-v8/src/network_host/xhr/send.rs
+++ b/moli-renderer-v8/src/network_host/xhr/send.rs
@@ -405,8 +405,7 @@ fn send_synchronous_network_xhr(
let result = result.and_then(|response| {
crate::network_host::validate_fetch_response_security_policy_with_body(
&prepared.document_url,
- &response.final_url,
- &response.headers,
+ &response.head(),
response.body_bytes(),
RequestMode::Cors,
prepared.credentials_mode,
@@ -419,8 +418,7 @@ fn send_synchronous_network_xhr(
Ok(response) => {
let observable_headers = crate::network_host::filter_cors_exposed_response_headers(
&prepared.document_url,
- &response.final_url,
- &response.headers,
+ &response.head(),
prepared.credentials_mode,
);
host.record_subresource_network(
diff --git a/moli-renderer-v8/src/planning.rs b/moli-renderer-v8/src/planning.rs
index d60ef3881..25966339e 100644
--- a/moli-renderer-v8/src/planning.rs
+++ b/moli-renderer-v8/src/planning.rs
@@ -393,7 +393,9 @@ pub(crate) async fn load_service_worker_aware_external_script_source_outcome(
*response.response,
document_character_set,
response.response_filter,
- response.response_filter.is_none(),
+ response
+ .response_filter
+ .is_none_or(|filter| filter.is_readable()),
),
Ok(None) => {
load_prepared_script_source_outcome_with_document_character_set(
@@ -506,7 +508,7 @@ pub(crate) fn external_script_source_load_outcome_from_response(
let head = response.head();
let response_filter =
crate::network_host::network_response_filter(&script.initiator_url, &head, request_mode);
- let response_is_eligible = response_filter.is_none()
+ let response_is_eligible = response_filter.is_none_or(|filter| filter.is_readable())
&& (request_mode == RequestMode::NoCors
|| crate::network_host::validate_cors_response_chain(
&script.initiator_url,
@@ -562,7 +564,7 @@ fn external_script_source_load_outcome_from_response_inner(
} else if !crate::subresource_integrity::response_matches_subresource_integrity_metadata(
&response_bytes,
script.fetch_metadata.integrity.as_deref(),
- response_is_eligible && response_filter.is_none(),
+ response_is_eligible && response_filter.is_none_or(|filter| filter.is_readable()),
) {
Err(format!(
"script request `{}` failed its integrity check",
diff --git a/moli-renderer-v8/src/runtime/page_vm/tests/async_subresource_completion.rs b/moli-renderer-v8/src/runtime/page_vm/tests/async_subresource_completion.rs
index 778ca8c82..53a0de689 100644
--- a/moli-renderer-v8/src/runtime/page_vm/tests/async_subresource_completion.rs
+++ b/moli-renderer-v8/src/runtime/page_vm/tests/async_subresource_completion.rs
@@ -226,6 +226,8 @@ fetch({:?})
root_document,
AsyncSubresourceFetchEvent::StreamingStarted(Box::new(
AsyncSubresourceStreamingStarted {
+ skip_fetch_security_validation: false,
+ response_filter: None,
internal_id,
request_url: request_url.clone(),
request_method: "GET".to_owned(),
@@ -438,6 +440,8 @@ async fn streaming_finish_requires_matching_request_and_body_source_identity() {
let started = AsyncSubresourceFetchEvent::StreamingStarted(Box::new(
AsyncSubresourceStreamingStarted {
+ skip_fetch_security_validation: false,
+ response_filter: None,
internal_id,
request_url: request_url.clone(),
request_method: "GET".to_owned(),
diff --git a/moli-renderer-v8/src/script_vm/subresource_fetch.rs b/moli-renderer-v8/src/script_vm/subresource_fetch.rs
index 44ed87035..2c4475d47 100644
--- a/moli-renderer-v8/src/script_vm/subresource_fetch.rs
+++ b/moli-renderer-v8/src/script_vm/subresource_fetch.rs
@@ -2589,10 +2589,9 @@ impl ScriptVm {
let result = if pending.pending.request_mode == moli_fetch::RequestMode::NoCors {
Ok(response)
} else {
- crate::network_host::validate_cors_response(
+ crate::network_host::validate_cors_response_chain(
&pending.pending.info.document_url,
- &response.final_url,
- &response.headers,
+ &response.head(),
pending.pending.credentials_mode,
)
.map(|()| response)
@@ -2650,10 +2649,9 @@ impl ScriptVm {
let result = if pending.pending.request_mode == moli_fetch::RequestMode::NoCors {
Ok(response)
} else {
- crate::network_host::validate_cors_response(
+ crate::network_host::validate_cors_response_chain(
&pending.pending.info.document_url,
- &response.final_url,
- &response.headers,
+ &response.head(),
pending.pending.credentials_mode,
)
.map(|()| response)
@@ -2878,10 +2876,9 @@ impl ScriptVm {
let result = if pending.pending.request_mode == moli_fetch::RequestMode::NoCors {
Ok(response)
} else {
- crate::network_host::validate_cors_response(
+ crate::network_host::validate_cors_response_chain(
&pending.pending.info.document_url,
- &response.final_url,
- &response.headers,
+ &response.head(),
pending.pending.credentials_mode,
)
.map(|()| response)
@@ -2941,10 +2938,9 @@ impl ScriptVm {
let result = if pending.pending.request_mode == moli_fetch::RequestMode::NoCors {
Ok(response)
} else {
- crate::network_host::validate_cors_response(
+ crate::network_host::validate_cors_response_chain(
&pending.pending.info.document_url,
- &response.final_url,
- &response.headers,
+ &response.head(),
pending.pending.credentials_mode,
)
.map(|()| response)
@@ -3136,8 +3132,7 @@ impl ScriptVm {
if !skip_fetch_security_validation {
crate::network_host::validate_fetch_response_security_policy_with_body(
&pending.info.document_url,
- &response.final_url,
- &response.headers,
+ &response.head(),
response.body_bytes(),
pending.request_mode,
pending.credentials_mode,
@@ -3309,8 +3304,7 @@ impl ScriptVm {
.then(|| {
crate::network_host::validate_fetch_response_security_policy_with_body(
&pending.info.document_url,
- &response.final_url,
- &response.headers,
+ &response.head(),
response.body_bytes(),
pending.request_mode,
pending.credentials_mode,
@@ -3593,8 +3587,7 @@ impl ScriptVm {
) {
let validation = crate::network_host::validate_fetch_response_security_policy_with_body_classified(
&pending.info.document_url,
- &response.final_url,
- &response.headers,
+ &response.head(),
response.body_bytes(),
pending.request_mode,
pending.credentials_mode,
@@ -3687,12 +3680,11 @@ impl ScriptVm {
if matches!(
pending.info.resource_type,
SubresourceResourceType::Fetch | SubresourceResourceType::Xhr
- ) {
+ ) && !response_filter.is_some_and(|filter| filter.is_readable()) {
observable_response.headers =
crate::network_host::filter_cors_exposed_response_headers(
&pending.info.document_url,
- &observable_response.final_url,
- &observable_response.headers,
+ &observable_response.head(),
pending.credentials_mode,
);
}
@@ -4378,10 +4370,12 @@ impl ScriptVm {
None
}
.or_else(|| {
+ if started.skip_fetch_security_validation {
+ return None;
+ }
crate::network_host::validate_fetch_response_security_policy(
&pending.info.document_url,
- &started.head.final_url,
- &started.head.headers,
+ &started.head,
pending.request_mode,
pending.credentials_mode,
pending.policy_context,
@@ -4429,6 +4423,7 @@ impl ScriptVm {
self._context_host
.borrow_mut()
.record_streaming_subresource_fetch(StreamingSubresourceFetchState {
+ response_filter: started.response_filter,
pending,
request_url: started.request_url,
request_method: started.request_method,
@@ -4572,6 +4567,9 @@ impl ScriptVm {
None
}
.or_else(|| {
+ if started.skip_fetch_security_validation {
+ return None;
+ }
matches!(
pending.info.resource_type,
SubresourceResourceType::EventSource
@@ -4581,8 +4579,7 @@ impl ScriptVm {
.then(|| {
crate::network_host::validate_fetch_response_security_policy(
&pending.info.document_url,
- &started.head.final_url,
- &started.head.headers,
+ &started.head,
pending.request_mode,
pending.credentials_mode,
pending.policy_context,
@@ -4770,11 +4767,10 @@ impl ScriptVm {
if matches!(
pending.info.resource_type,
SubresourceResourceType::Fetch | SubresourceResourceType::Xhr
- ) {
+ ) && !started.response_filter.is_some_and(|filter| filter.is_readable()) {
observable_head.headers = crate::network_host::filter_cors_exposed_response_headers(
&pending.info.document_url,
- &observable_head.final_url,
- &observable_head.headers,
+ &observable_head,
pending.credentials_mode,
);
}
@@ -4814,6 +4810,7 @@ impl ScriptVm {
self._context_host
.borrow_mut()
.record_streaming_subresource_fetch(StreamingSubresourceFetchState {
+ response_filter: started.response_filter,
pending,
request_url: started.request_url.clone(),
request_method: started.request_method.clone(),
@@ -4860,12 +4857,13 @@ impl ScriptVm {
.resolver()
.expect("detached keepalive stream is handled before V8 entry");
let resolver = v8::Local::new(scope, resolver);
- let response_obj = crate::network_host::build_fetch_response_object_from_stream_for_request_mode(
+ let response_obj = crate::network_host::build_fetch_response_object_from_stream_for_request_mode_with_filter(
scope,
&pending.info.document_url,
pending.request_mode,
observable_head,
started.body_source_id,
+ started.response_filter,
);
resolver.resolve(scope, response_obj.into());
}
@@ -4964,6 +4962,7 @@ impl ScriptVm {
let pending_owner = pending.execution_context.dispatch_scope();
self._context_host.borrow_mut().record_streaming_subresource_fetch(
StreamingSubresourceFetchState {
+ response_filter: started.response_filter,
pending,
request_url: started.request_url.clone(),
request_method: started.request_method.clone(),
@@ -5685,13 +5684,17 @@ impl ScriptVm {
moli_trace::cdp_runtime_trace_enabled().then(Instant::now);
let xhr = v8::Local::new(scope, &xhr);
let mut observable_head = streaming.head;
- observable_head.headers =
- crate::network_host::filter_cors_exposed_response_headers(
- &streaming.pending.info.document_url,
- &observable_head.final_url,
- &observable_head.headers,
- streaming.pending.credentials_mode,
- );
+ if !streaming
+ .response_filter
+ .is_some_and(|filter| filter.is_readable())
+ {
+ observable_head.headers =
+ crate::network_host::filter_cors_exposed_response_headers(
+ &streaming.pending.info.document_url,
+ &observable_head,
+ streaming.pending.credentials_mode,
+ );
+ }
// XHR still exposes a complete response at DONE. Keep the
// network transfer and CDP record streaming/spooled, and
// materialize only at this Web-visible completion boundary.
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/xhr.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/xhr.rs
index cc9fa7844..dca9d7370 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/xhr.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/xhr.rs
@@ -1846,6 +1846,8 @@ __streamingXhr.send();
};
vm.start_streaming_async_subresource_fetch(crate::types::AsyncSubresourceStreamingStarted {
+ skip_fetch_security_validation: false,
+ response_filter: None,
internal_id,
request_url: request_url.clone(),
request_method: "GET".to_owned(),
@@ -1945,6 +1947,7 @@ async fn streaming_subresource_finish_preserves_response_head_cache_state() {
context_host
.borrow_mut()
.record_streaming_subresource_fetch(super::StreamingSubresourceFetchState {
+ response_filter: None,
pending: super::PendingSubresourceFetchState {
info: crate::types::PendingSubresourceFetchInfo {
internal_id,
@@ -2226,6 +2229,7 @@ async fn streaming_fetch_body_error_records_response_started_then_body_failed()
context_host
.borrow_mut()
.record_streaming_subresource_fetch(super::StreamingSubresourceFetchState {
+ response_filter: None,
pending: super::PendingSubresourceFetchState {
info: crate::types::PendingSubresourceFetchInfo {
internal_id,
@@ -2381,6 +2385,7 @@ fn install_streaming_fetch_response_fixture(
context_host
.borrow_mut()
.record_streaming_subresource_fetch(super::StreamingSubresourceFetchState {
+ response_filter: None,
pending: super::PendingSubresourceFetchState {
info: crate::types::PendingSubresourceFetchInfo {
internal_id,
@@ -2481,6 +2486,7 @@ async fn streaming_fetch_body_cancel_aborts_streaming_subresource() {
context_host
.borrow_mut()
.record_streaming_subresource_fetch(super::StreamingSubresourceFetchState {
+ response_filter: None,
pending: super::PendingSubresourceFetchState {
info: crate::types::PendingSubresourceFetchInfo {
internal_id,
@@ -3104,6 +3110,7 @@ async fn streaming_xhr_materialization_failure_errors_body_source_before_close()
context_host
.borrow_mut()
.record_streaming_subresource_fetch(super::StreamingSubresourceFetchState {
+ response_filter: None,
pending: super::PendingSubresourceFetchState {
info: crate::types::PendingSubresourceFetchInfo {
internal_id,
diff --git a/moli-renderer-v8/src/script_vm/tests/mod.rs b/moli-renderer-v8/src/script_vm/tests/mod.rs
index 6db5c42a2..c34483685 100644
--- a/moli-renderer-v8/src/script_vm/tests/mod.rs
+++ b/moli-renderer-v8/src/script_vm/tests/mod.rs
@@ -349,6 +349,7 @@ fn register_pending_window_fetch_for_test(
PendingWindowFetchTestStage::Streaming => {
host.record_streaming_subresource_fetch(
crate::types::StreamingSubresourceFetchState {
+ response_filter: None,
pending,
request_url: url.clone(),
request_method: "GET".to_owned(),
@@ -3340,6 +3341,8 @@ async fn child_navigation_keeps_accepted_beacon_network_only_and_rejects_stale_s
let request_url = Url::parse("https://beacon-execution-context.test/accepted").unwrap();
let body_source_id = 60_000 + internal_id;
vm.start_streaming_async_subresource_fetch(crate::types::AsyncSubresourceStreamingStarted {
+ skip_fetch_security_validation: false,
+ response_filter: None,
internal_id,
request_url: request_url.clone(),
request_method: "POST".to_owned(),
@@ -3578,6 +3581,8 @@ async fn child_csp_report_keeps_exact_violation_document_without_v8_after_naviga
let body_source_id = 70_000 + internal_id;
vm.start_streaming_async_subresource_fetch(crate::types::AsyncSubresourceStreamingStarted {
+ skip_fetch_security_validation: false,
+ response_filter: None,
internal_id,
request_url: report_url.clone(),
request_method: "POST".to_owned(),
@@ -3780,6 +3785,8 @@ fn isolated_realm_destruction_aborts_fetch_and_detaches_keepalive() {
let request_url = Url::parse("https://fetch-execution-context.test/pending").unwrap();
let body_source_id = 50_000 + keepalive.0;
vm.start_streaming_async_subresource_fetch(crate::types::AsyncSubresourceStreamingStarted {
+ skip_fetch_security_validation: false,
+ response_filter: None,
internal_id: keepalive.0,
request_url: request_url.clone(),
request_method: "GET".to_owned(),
diff --git a/moli-renderer-v8/src/service_worker_runtime/service/fetch_settlement.rs b/moli-renderer-v8/src/service_worker_runtime/service/fetch_settlement.rs
index 4aa54c947..57dd79fd6 100644
--- a/moli-renderer-v8/src/service_worker_runtime/service/fetch_settlement.rs
+++ b/moli-renderer-v8/src/service_worker_runtime/service/fetch_settlement.rs
@@ -90,6 +90,7 @@ fn configure_service_worker_network_fallback_request(
.with_initiator_url(&job.network_context.document_url)
.with_request_mode(job.request_mode)
.with_credentials_mode(job.credentials_mode)
+ .with_redirect_chain(job.redirect_chain.clone())
.with_redirect_mode(if service_worker_fetch_is_navigation_request(job) {
moli_fetch::RequestRedirectMode::Follow
} else {
@@ -302,14 +303,13 @@ impl ServiceWorkerRuntimeService {
return;
}
};
- crate::network_host::spawn_async_subresource_fetch_with_redirect_chain(
+ crate::network_host::spawn_async_subresource_fetch(
job.resource_task_runner.clone(),
job.completion_tx,
request_client,
request,
Some(job.cancel_handle),
job.cors_preflight_request_headers,
- job.redirect_chain,
job.internal_id,
job.network_context,
job.request_url,
@@ -333,7 +333,6 @@ impl ServiceWorkerRuntimeService {
}
};
let cancel_handle = job.cancel_handle.clone();
- let redirect_chain = job.redirect_chain.clone();
let document_url = job.network_context.document_url.clone();
let request_mode = job.request_mode;
let credentials_mode = job.credentials_mode;
@@ -347,13 +346,7 @@ impl ServiceWorkerRuntimeService {
};
let result = match result {
Ok(response) => {
- let mut head = response.head();
- if !redirect_chain.is_empty() {
- let mut combined_redirect_chain = redirect_chain;
- combined_redirect_chain.extend(head.redirect_chain);
- head.redirect_chain = combined_redirect_chain;
- head.redirected = true;
- }
+ let head = response.head();
// This is a network response after a synthetic redirect,
// not a worker-produced readable Response. Authorize CORS
// before the direct consumer can trust an absent filter.
@@ -431,6 +424,10 @@ impl ServiceWorkerRuntimeService {
job.completion_tx.clone(),
AsyncSubresourceFetchEvent::StreamingStarted(Box::new(
AsyncSubresourceStreamingStarted {
+ skip_fetch_security_validation: true,
+ response_filter: service_worker_response_type_filter(
+ &started.response_head.response_type,
+ ),
internal_id: job.internal_id,
request_url: job.request_url.clone(),
request_method: job.request_method.clone(),
@@ -836,14 +833,12 @@ fn validate_service_worker_fetch_response_body_security_policy(
return Ok(());
}
- crate::network_host::validate_fetch_response_security_policy_with_body(
+ validate_service_worker_fetch_response_head_security_policy(job, final_url, &response.headers)?;
+ crate::network_host::validate_opaque_response_blocking_with_body(
&job.network_context.document_url,
final_url,
&response.headers,
&response.body,
- job.request_mode,
- job.credentials_mode,
- job.network_context.policy_context,
)
}
@@ -923,12 +918,23 @@ fn service_worker_fetch_response_rejection(
fn service_worker_fetch_response_filter(
response: &ServiceWorkerFetchResponse,
) -> Option<AsyncSubresourceFetchResponseFilter> {
- match response.response_type.as_str() {
+ if is_redirect_status(response.status)
+ && !matches!(response.response_type.as_str(), "opaque" | "opaqueredirect")
+ {
+ Some(AsyncSubresourceFetchResponseFilter::OpaqueRedirect)
+ } else {
+ service_worker_response_type_filter(&response.response_type)
+ }
+}
+
+fn service_worker_response_type_filter(
+ response_type: &str,
+) -> Option<AsyncSubresourceFetchResponseFilter> {
+ match response_type {
+ "basic" => Some(AsyncSubresourceFetchResponseFilter::Basic),
+ "cors" => Some(AsyncSubresourceFetchResponseFilter::Cors),
"opaque" => Some(AsyncSubresourceFetchResponseFilter::Opaque),
"opaqueredirect" => Some(AsyncSubresourceFetchResponseFilter::OpaqueRedirect),
- _ if is_redirect_status(response.status) => {
- Some(AsyncSubresourceFetchResponseFilter::OpaqueRedirect)
- }
_ => None,
}
}
diff --git a/moli-renderer-v8/src/stylesheet_blocking.rs b/moli-renderer-v8/src/stylesheet_blocking.rs
index e45ee1e7c..6e7cbe412 100644
--- a/moli-renderer-v8/src/stylesheet_blocking.rs
+++ b/moli-renderer-v8/src/stylesheet_blocking.rs
@@ -296,7 +296,7 @@ fn stylesheet_terminal_from_response(
} => Err(format!(
"failed to fetch stylesheet `{request_url}`: CORS response is opaque"
)),
- StylesheetResponseProvenance::ServiceWorker { filter: None } => Ok(()),
+ StylesheetResponseProvenance::ServiceWorker { .. } => Ok(()),
StylesheetResponseProvenance::Network => crate::network_host::validate_cors_response(
document_url,
&response.final_url,
diff --git a/moli-renderer-v8/src/stylesheet_runtime/connected.rs b/moli-renderer-v8/src/stylesheet_runtime/connected.rs
index f03337b4e..431ac9313 100644
--- a/moli-renderer-v8/src/stylesheet_runtime/connected.rs
+++ b/moli-renderer-v8/src/stylesheet_runtime/connected.rs
@@ -2196,11 +2196,7 @@ fn connected_link_readiness_request(
fn connected_link_origin_clean_from_service_worker_filter(
response_filter: Option<AsyncSubresourceFetchResponseFilter>,
) -> bool {
- match response_filter {
- Some(AsyncSubresourceFetchResponseFilter::Opaque)
- | Some(AsyncSubresourceFetchResponseFilter::OpaqueRedirect) => false,
- None => true,
- }
+ response_filter.is_none_or(|filter| filter.is_readable())
}
fn link_crossorigin_credentials_mode(cross_origin: Option<&str>) -> RequestCredentialsMode {
@@ -2218,11 +2214,8 @@ fn connected_link_load_event_successful(
response: &crate::protocol_types::NavigationResponse,
response_filter: Option<AsyncSubresourceFetchResponseFilter>,
) -> bool {
- match response_filter {
- Some(AsyncSubresourceFetchResponseFilter::Opaque)
- | Some(AsyncSubresourceFetchResponseFilter::OpaqueRedirect) => true,
- None => (200..=299).contains(&response.status),
- }
+ response_filter.is_some_and(|filter| !filter.is_readable())
+ || (200..=299).contains(&response.status)
}
async fn fetch_connected_link_readiness_with_request(
diff --git a/moli-renderer-v8/src/types.rs b/moli-renderer-v8/src/types.rs
index 4b9df985e..e4b0e7ce8 100644
--- a/moli-renderer-v8/src/types.rs
+++ b/moli-renderer-v8/src/types.rs
@@ -599,10 +599,18 @@ pub(super) struct AsyncSubresourceFetchCompletion {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum AsyncSubresourceFetchResponseFilter {
+ Basic,
+ Cors,
Opaque,
OpaqueRedirect,
}
+impl AsyncSubresourceFetchResponseFilter {
+ pub(super) fn is_readable(self) -> bool {
+ matches!(self, Self::Basic | Self::Cors)
+ }
+}
+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(crate) struct SubresourcePolicyContext {
pub(super) cross_origin_embedder_policy:
@@ -631,6 +639,8 @@ pub(super) struct AsyncSubresourceNetworkContext {
#[derive(Debug)]
pub(super) struct AsyncSubresourceStreamingStarted {
+ pub(super) skip_fetch_security_validation: bool,
+ pub(super) response_filter: Option<AsyncSubresourceFetchResponseFilter>,
pub(super) internal_id: u64,
pub(super) request_url: Url,
pub(super) request_method: String,
@@ -851,6 +861,7 @@ pub(super) struct ServiceWorkerControllerChangeCompletion {
}
pub(super) struct StreamingSubresourceFetchState {
+ pub(super) response_filter: Option<AsyncSubresourceFetchResponseFilter>,
pub(super) pending: PendingSubresourceFetchState,
pub(super) request_url: Url,
pub(super) request_method: String,
diff --git a/moli-renderer-v8/src/worker/global_scope/fetch.rs b/moli-renderer-v8/src/worker/global_scope/fetch.rs
index 446331733..91210d4b5 100644
--- a/moli-renderer-v8/src/worker/global_scope/fetch.rs
+++ b/moli-renderer-v8/src/worker/global_scope/fetch.rs
@@ -2478,8 +2478,7 @@ pub(in crate::worker) fn start_worker_streaming_fetch(
};
if let Err(message) = validate_fetch_response_security_policy(
&pending.document_url,
- &started.head.final_url,
- &started.head.headers,
+ &started.head,
pending.request_mode,
pending.credentials_mode,
pending.policy_context,
@@ -2491,8 +2490,7 @@ pub(in crate::worker) fn start_worker_streaming_fetch(
let mut observable_head = started.head.clone();
observable_head.headers = filter_cors_exposed_response_headers(
&pending.document_url,
- &observable_head.final_url,
- &observable_head.headers,
+ &observable_head,
pending.credentials_mode,
);
pending.streaming_body_source_id = Some(started.body_source_id);
@@ -2811,8 +2809,7 @@ pub(in crate::worker) fn drain_worker_fetch_completion_result(
WorkerFetchResponse::Materialized(response) => {
validate_fetch_response_security_policy_with_body_classified(
&pending.document_url,
- &response_head.final_url,
- &response_head.headers,
+ &response_head,
response.body_bytes(),
pending.request_mode,
pending.credentials_mode,
@@ -2829,8 +2826,7 @@ pub(in crate::worker) fn drain_worker_fetch_completion_result(
.and_then(|body_bytes| {
validate_fetch_response_security_policy_with_body_classified(
&pending.document_url,
- &response_head.final_url,
- &response_head.headers,
+ &response_head,
&body_bytes,
pending.request_mode,
pending.credentials_mode,
@@ -2882,8 +2878,7 @@ pub(in crate::worker) fn drain_worker_fetch_completion_result(
}
let filtered_headers = filter_cors_exposed_response_headers(
&pending.document_url,
- &response_head.final_url,
- &response_head.headers,
+ &response_head,
pending.credentials_mode,
);
let response_obj = match response.into_fetch_parts() {
diff --git a/moli-renderer-v8/src/worker/global_scope/mod.rs b/moli-renderer-v8/src/worker/global_scope/mod.rs
index 5c61bcbb2..350d6dbdf 100644
--- a/moli-renderer-v8/src/worker/global_scope/mod.rs
+++ b/moli-renderer-v8/src/worker/global_scope/mod.rs
@@ -86,8 +86,7 @@ use crate::network_host::{
is_cors_policy_failure_message, local_url_response, parse_fetch_init,
prepare_xhr_send_body_from_args, request_input_snapshot, request_object_credentials_mode,
reset_xhr_response_for_request_error, resolve_context_url, set_xhr_state_bool,
- set_xhr_state_number, throw_synchronous_xhr_failure, validate_cors_response,
- validate_fetch_response_security_policy,
+ set_xhr_state_number, throw_synchronous_xhr_failure, validate_fetch_response_security_policy,
validate_fetch_response_security_policy_with_body_classified, xhr_author_request_headers,
xhr_dispatch_progress_event, xhr_ensure_send_allowed, xhr_state_bool_property,
xhr_state_number_property, xhr_state_string_property,
diff --git a/moli-renderer-v8/src/worker/global_scope/xhr.rs b/moli-renderer-v8/src/worker/global_scope/xhr.rs
index 11a54fc5b..5ade730a1 100644
--- a/moli-renderer-v8/src/worker/global_scope/xhr.rs
+++ b/moli-renderer-v8/src/worker/global_scope/xhr.rs
@@ -696,10 +696,9 @@ fn send_synchronous_worker_xhr(
throw_synchronous_xhr_failure(scope, xhr, &request_url_text, "NetworkError");
return;
}
- if let Err(message) = validate_cors_response(
+ if let Err(message) = crate::network_host::validate_cors_response_chain(
&prepared.document_url,
- &response_head.final_url,
- &response_head.headers,
+ &response_head,
prepared.credentials_mode,
) {
record_worker_subresource_failure(
@@ -1069,10 +1068,9 @@ pub(in crate::worker) fn drain_worker_xhr_completion(
match completion.result {
Ok(response) => {
let response_head = response.head();
- match validate_cors_response(
+ match crate::network_host::validate_cors_response_chain(
&pending.document_url,
- &response_head.final_url,
- &response_head.headers,
+ &response_head,
pending.credentials_mode,
) {
Ok(()) => {
@@ -1106,8 +1104,7 @@ pub(in crate::worker) fn drain_worker_xhr_completion(
}
response_head.headers = filter_cors_exposed_response_headers(
&pending.document_url,
- &response_head.final_url,
- &response_head.headers,
+ &response_head,
pending.credentials_mode,
);
apply_xhr_response_body_source(scope, xhr, response_head, body);
diff --git a/moli-renderer-v8/src/worker/thread/mod.rs b/moli-renderer-v8/src/worker/thread/mod.rs
index f4745f053..b4b3e3ea6 100644
--- a/moli-renderer-v8/src/worker/thread/mod.rs
+++ b/moli-renderer-v8/src/worker/thread/mod.rs
@@ -727,8 +727,7 @@ fn start_worker_module_graph_fetch(
}
crate::network_host::validate_fetch_response_security_policy(
&request_initiator_url,
- &response.final_url,
- &response.headers,
+ &response.head(),
moli_fetch::RequestMode::Cors,
request_credentials_mode,
Default::default(),
diff --git a/moli-wpt-compat/fixtures/wpt/ported/fetch/fetch-xhr-cors-redirect-basic.html b/moli-wpt-compat/fixtures/wpt/ported/fetch/fetch-xhr-cors-redirect-basic.html
index 5c63ac435..c42b24a2f 100644
--- a/moli-wpt-compat/fixtures/wpt/ported/fetch/fetch-xhr-cors-redirect-basic.html
+++ b/moli-wpt-compat/fixtures/wpt/ported/fetch/fetch-xhr-cors-redirect-basic.html
@@ -79,7 +79,7 @@ promise_test(async function () {
promise_test(async function () {
const response = await fetch(alternate_url("/wpt/runtime/cors/redirect/to-same-origin-echo"));
- assert_equals(response.type, "basic");
+ assert_equals(response.type, "cors");
assert_true(response.redirected, "fetch response should expose redirect state");
assert_true(response.url.endsWith("/wpt/runtime/cors/redirect/same-origin-echo"));
const payload = JSON.parse(await response.text());
@@ -99,7 +99,7 @@ promise_test(async function () {
promise_test(async function () {
const response = await fetch("/wpt/runtime/cors/redirect/to-alt-to-same-origin-echo");
- assert_equals(response.type, "basic");
+ assert_equals(response.type, "cors");
assert_true(response.redirected, "fetch response should expose redirect state");
assert_true(response.url.endsWith("/wpt/runtime/cors/redirect/same-origin-echo"));
const payload = JSON.parse(await response.text());
@@ -119,7 +119,7 @@ promise_test(async function () {
promise_test(async function () {
const response = await fetch("/wpt/runtime/cors/redirect/to-cross-site-to-same-origin-echo");
- assert_equals(response.type, "basic");
+ assert_equals(response.type, "cors");
assert_true(response.redirected, "fetch response should expose redirect state");
assert_true(response.url.endsWith("/wpt/runtime/cors/redirect/same-origin-echo"));
const payload = JSON.parse(await response.text());
diff --git a/moli-wpt-compat/fixtures/wpt/ported/worker/worker-fetch-xhr-cors-redirect-basic.html b/moli-wpt-compat/fixtures/wpt/ported/worker/worker-fetch-xhr-cors-redirect-basic.html
index 4c894f73b..5f6986774 100644
--- a/moli-wpt-compat/fixtures/wpt/ported/worker/worker-fetch-xhr-cors-redirect-basic.html
+++ b/moli-wpt-compat/fixtures/wpt/ported/worker/worker-fetch-xhr-cors-redirect-basic.html
@@ -69,7 +69,7 @@ promise_test(async function () {
promise_test(async function () {
const result = await run_worker_case("fetch-cross-to-same");
assert_equals(result.error, undefined, "worker fetch should resolve");
- assert_equals(result.type, "basic", "fetch response type");
+ assert_equals(result.type, "cors", "fetch response type");
assert_true(result.redirected, "fetch response should expose redirect state");
assert_true(
result.url.endsWith("/wpt/runtime/cors/redirect/same-origin-echo"),
@@ -95,7 +95,7 @@ promise_test(async function () {
promise_test(async function () {
const result = await run_worker_case("fetch-cross-site-chain-to-same");
assert_equals(result.error, undefined, "worker fetch should resolve");
- assert_equals(result.type, "basic", "fetch response type");
+ assert_equals(result.type, "cors", "fetch response type");
assert_true(result.redirected, "fetch response should expose redirect state");
assert_true(
result.url.endsWith("/wpt/runtime/cors/redirect/same-origin-echo"),
diff --git a/moli-wpt-compat/src/server.rs b/moli-wpt-compat/src/server.rs
index ae71d4aa9..ed7462283 100644
--- a/moli-wpt-compat/src/server.rs
+++ b/moli-wpt-compat/src/server.rs
@@ -2460,7 +2460,11 @@ async fn wpt_cors_redirect_same_origin_echo(headers: HeaderMap) -> Response {
"origin": header_value(&headers, ORIGIN.as_str()),
"response": "cors-redirect-same-origin-echo",
});
- json_string_response(payload.to_string())
+ // A return to the initiating URL still needs CORS authorization after a
+ // cross-origin hop. Echo the actual Origin, including the literal null.
+ let mut response = json_string_response(payload.to_string());
+ add_cors_allow_origin_header(&mut response, &headers);
+ response
}
async fn wpt_cors_credentials_allow(headers: HeaderMap) -> Response {