From d8fe34fe93eeb7bc93310994898cb4ea77e99367 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Sun, 15 Nov 2026 03:43:03 +0800 Subject: [PATCH] fix(xhr): share intercepted request header handling --- moli-cdp-smoke/README.md | 6 + moli-cdp-smoke/moli_cdp_smoke/fixture.py | 21 +- .../moli_cdp_smoke/groups/fetch_headers.py | 163 +++++++ moli-cdp-smoke/moli_cdp_smoke/runner.py | 7 + moli-cdp-smoke/tests/test_group_selection.py | 1 + .../src/domains/fetch/tests/runtime_fetch.rs | 1 + .../runtime_fetch/intercepted_headers.rs | 405 ++++++++++++++++++ .../src/network_host/async_fetch.rs | 15 +- .../src/network_host/controlled_xhr.rs | 29 +- .../network_host/controlled_xhr/context.rs | 3 +- 10 files changed, 625 insertions(+), 26 deletions(-) create mode 100644 moli-cdp-smoke/moli_cdp_smoke/groups/fetch_headers.py create mode 100644 moli-protocol/src/domains/fetch/tests/runtime_fetch/intercepted_headers.rs diff --git a/moli-cdp-smoke/README.md b/moli-cdp-smoke/README.md index dae90140995..f6c96bd2db6 100644 --- a/moli-cdp-smoke/README.md +++ b/moli-cdp-smoke/README.md @@ -50,6 +50,12 @@ XHR retains its original raw-byte capture for Network consumers while the Window/Worker JS boundary materializes a response. Memory-allocation identity, disk extent lifetime, non-UTF-8 bytes and release are checked by Rust regressions. +The default `fetch-headers` group compares fetch, asynchronous XHR and synchronous +XHR using real redirect and echo endpoints. It checks replayed upload headers, +no-op method/body edits, fresh Content-Type values, generated Origin visibility, +and Authorization scoped to one redirect origin. Each request must retain its +Network identity and emit exactly one terminal event. + The default `fetch-media` group drives real audio, video and text-track element loads. Eleven scenarios cover single Media filters, mixed Image/Media/Font patterns, continue/fulfill/fail/disable and response-stage body reading. Valid diff --git a/moli-cdp-smoke/moli_cdp_smoke/fixture.py b/moli-cdp-smoke/moli_cdp_smoke/fixture.py index 3fba4482105..7b7cf90473b 100644 --- a/moli-cdp-smoke/moli_cdp_smoke/fixture.py +++ b/moli-cdp-smoke/moli_cdp_smoke/fixture.py @@ -585,7 +585,13 @@ class FixtureServer: self._dispatch() def do_OPTIONS(self) -> None: - if urllib.parse.urlparse(self.path).path.startswith("/fetch-dispatch/network/"): + if urllib.parse.urlparse(self.path).path.startswith("/fetch-headers/"): + self._send_common(HTTPStatus.NO_CONTENT, "text/plain", b"", { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET, POST", + "Access-Control-Allow-Headers": "authorization, content-type, x-author, user-agent", + }) + elif urllib.parse.urlparse(self.path).path.startswith("/fetch-dispatch/network/"): self._send_common(HTTPStatus.NO_CONTENT, "text/plain", b"", { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET, POST", @@ -600,6 +606,19 @@ class FixtureServer: query = urllib.parse.parse_qs(parsed.query) _body = self._read_body() + if route.startswith("/fetch-headers/"): + outer._record_resource_request(self.path, self.headers) + if route == "/fetch-headers/redirect": + self._send_common(HTTPStatus(int(query.get("status", ["302"])[0])), + "text/plain", b"", {"Location": query["to"][0], + "Access-Control-Allow-Origin": "*"}) + else: + self._send_json({"method": self.command, + "headers": {name.lower(): value for name, value in self.headers.items()}, + "body": _body.decode("utf-8")}, + headers={"Access-Control-Allow-Origin": "*"}) + return + if route == "/ws-echo" and self.headers.get("Upgrade", "").lower() == "websocket": self._handle_websocket() return diff --git a/moli-cdp-smoke/moli_cdp_smoke/groups/fetch_headers.py b/moli-cdp-smoke/moli_cdp_smoke/groups/fetch_headers.py new file mode 100644 index 00000000000..1b5d42292ab --- /dev/null +++ b/moli-cdp-smoke/moli_cdp_smoke/groups/fetch_headers.py @@ -0,0 +1,163 @@ +from __future__ import annotations + +import asyncio +import json +from typing import Any +from urllib.parse import parse_qs, urlencode, urlsplit + +from ..assertions import assert_equal, record_contract, wait_until +from ..diagnostics import new_cdp_session +from ..fixture import FixtureServer +from ..helpers import attach_cdp_event_collector + + +def _url(origin: str, route: str, case: str, *, hop: str = "final", target: str = "", status: int = 302) -> str: + return origin + "/fetch-headers/" + route + "?" + urlencode({"case": case, "hop": hop, "to": target, "status": status}) + + +async def run_fetch_headers_group(browser: Any, fixture: str, results: list[dict[str, Any]]) -> None: + peer, escape = FixtureServer(), FixtureServer() + peer.start() + escape.start() + try: + cases = [(f"replay-{status}", status) for status in (301, 302, 303, 307, 308)] + cases += [(case, 302) for case in ("noop-method", "empty-postdata", "new-content-type", "chain-replay", + "drop-then-replay", "fresh-then-replay", "origin-echo", "authorization")] + for api in ("fetch", "xhr", "sync-xhr"): + for case, status in cases: + await _case(browser, fixture, peer, escape.url, api, case, status, results) + finally: + peer.stop() + escape.stop() + + +async def _case(browser: Any, source: str, peer: FixtureServer, escape: str, + api: str, case: str, status: int, results: list[dict[str, Any]]) -> None: + label = f"{api}-{case}" + context = await browser.new_context() + operation = None + pending_pause = None + try: + page = await context.new_page() + await page.goto(source + "/plain", wait_until="load", timeout=10_000) + cdp = await new_cdp_session(context, page) + await cdp.send("Network.enable") + await cdp.send("Fetch.enable", {"patterns": [ + {"urlPattern": "*fetch-headers/*", "resourceType": resource} + for resource in ("Fetch", "XHR") + ]}) + queue: asyncio.Queue[dict[str, Any]] = asyncio.Queue() + cdp.on("Fetch.requestPaused", queue.put_nowait) + pauses = [] + preflights = [] + original = None + target = _url(source, "echo", label) + next_url = target + if case in ("chain-replay", "drop-then-replay", "fresh-then-replay"): + next_url = _url(source, "redirect", label, hop="middle", target=target) + peer_url = _url(peer.url, "redirect", label, hop="authorized", target=_url(escape, "echo", label)) + url = _url(source, "redirect", label, hop="source", target=next_url, status=status) + if case == "origin-echo": + url = _url(peer.url, "echo", label) + elif case == "authorization": + url = _url(source, "redirect", label, hop="source", target=peer_url) + method = "GET" if case == "authorization" else "POST" + author_headers = {"Authorization": "author-secret"} if case == "authorization" else {"Content-Type": "text/plain", "X-Author": "kept"} + body = "undefined" if method == "GET" else json.dumps("abc") + if api == "fetch": + script = f"fetch({json.dumps(url)},{{method:{json.dumps(method)},headers:{json.dumps(author_headers)},body:{body}}}).then(r=>r.text())" + else: + script = f"""new Promise((resolve,reject)=>{{const x=new XMLHttpRequest(); + x.open({json.dumps(method)},{json.dumps(url)},{str(api != 'sync-xhr').lower()}); + for(const [k,v] of Object.entries({json.dumps(author_headers)}))x.setRequestHeader(k,v); + x.onload=()=>resolve(x.responseText);x.onerror=()=>reject(new Error('XHR failed'));x.send({body});}})""" + with attach_cdp_event_collector(cdp, ["Network.requestWillBeSent", "Network.responseReceived", + "Network.loadingFinished", "Network.loadingFailed"]) as events: + operation = asyncio.create_task(page.evaluate(script)) + async with asyncio.timeout(10): + while True: + pending_pause = asyncio.create_task(queue.get()) + done, _ = await asyncio.wait((operation, pending_pause), return_when=asyncio.FIRST_COMPLETED) + if operation in done: + pending_pause.cancel() + await asyncio.gather(pending_pause, return_exceptions=True) + break + pause = pending_pause.result() + pending_pause = None + if pause["request"]["method"] == "OPTIONS": + # Chromium also labels intercepted preflights as XHR. + # They have independent Network IDs, outside this chain. + preflights.append(pause) + await cdp.send("Fetch.continueRequest", {"requestId": pause["requestId"]}) + continue + pauses.append(pause) + query = parse_qs(urlsplit(pause["request"]["url"]).query) + hop = query["hop"][0] + headers = None + params = {"requestId": pause["requestId"]} + if hop == "source" and case != "authorization": + original = dict(pause["request"]["headers"]) + elif case == "origin-echo": + headers = dict(pause["request"]["headers"]) + origin = next((value for name, value in headers.items() if name.lower() == "origin"), None) + assert_equal(origin, source, label + " visible generated Origin") + elif case == "authorization": + if hop == "authorized": + headers = {"Authorization": "interceptor-secret"} + else: + headers = dict(original) + if case == "noop-method": + params["method"] = pause["request"]["method"] + if case == "empty-postdata": + params["postData"] = "" + if case == "new-content-type" or (case == "fresh-then-replay" and hop == "middle"): + headers = {name: value for name, value in headers.items() if name.lower() != "content-type"} + headers["Content-Type"] = "application/x-new" + if case == "drop-then-replay" and hop == "middle": + headers = None + if headers is not None: + params["headers"] = [{"name": name, "value": value} for name, value in headers.items()] + await cdp.send("Fetch.continueRequest", params) + value = json.loads(await operation) + wire = value["headers"] + keep_body = status in (307, 308) or case == "origin-echo" + expected_type = "text/plain" if keep_body or case in ("drop-then-replay", "fresh-then-replay") else None + if case == "new-content-type": + expected_type = "application/x-new" + assert_equal(value["method"], "POST" if keep_body else "GET", label + " wire method") + assert_equal(value["body"], "abc" if keep_body else "", label + " wire body") + assert_equal(wire.get("content-type"), expected_type, label + " wire Content-Type") + assert_equal(wire.get("content-length"), "3" if keep_body else None, label + " wire Content-Length") + if case == "authorization": + peer_path = urlsplit(peer_url).path + "?" + urlsplit(peer_url).query + assert_equal(peer.resource_request_headers(peer_path).get("authorization"), "interceptor-secret", + label + " explicit second-hop Authorization") + assert_equal(wire.get("authorization"), None, label + " third origin has no Authorization") + else: + assert_equal(wire.get("x-author"), "kept", label + " retained custom header") + if not keep_body: + assert_equal(wire.get("origin"), None, label + " no replayed POST Origin") + ids = {pause.get("networkId") for pause in pauses} + assert_equal(len(ids), 1, label + " stable Network identity") + request_id = next(iter(ids)) + if not request_id: + raise AssertionError(label + " missing Network identity") + await wait_until(lambda: any(event["method"] in ("Network.loadingFinished", "Network.loadingFailed") + and event["params"].get("requestId") == request_id for event in events), + label + " Network terminal") + terminals = [event["method"] for event in events + if event["method"] in ("Network.loadingFinished", "Network.loadingFailed") + and event["params"].get("requestId") == request_id] + assert_equal(terminals, ["Network.loadingFinished"], label + " one terminal") + record_contract(results, "intercepted_headers_" + label, + contract="A redirect removes replayed upload headers, preserves fresh overrides and scopes explicit Authorization.", + source="Executable Chromium CDP oracle", + commands=["Fetch.enable", "Fetch.continueRequest"], + observed={"api": api, "case": case, "method": value["method"], "contentType": expected_type, + "pauses": len(pauses), "preflightPauses": len(preflights), "terminals": terminals}) + finally: + for task in (pending_pause, operation): + if task is not None and not task.done(): + task.cancel() + await asyncio.gather(task, return_exceptions=True) + await context.close() diff --git a/moli-cdp-smoke/moli_cdp_smoke/runner.py b/moli-cdp-smoke/moli_cdp_smoke/runner.py index 138fb731068..ef807138e66 100644 --- a/moli-cdp-smoke/moli_cdp_smoke/runner.py +++ b/moli-cdp-smoke/moli_cdp_smoke/runner.py @@ -43,6 +43,7 @@ from .groups.emulation_storage import run_emulation_storage_group from .groups.error_document import run_error_document_group from .groups.fetch_runtime_teardown import run_fetch_runtime_teardown_group from .groups.fetch_font import run_fetch_font_group +from .groups.fetch_headers import run_fetch_headers_group from .groups.fetch_media import run_fetch_media_group from .groups.fetch_resource_dispatch import run_fetch_resource_dispatch_group from .groups.history_worlds import run_history_worlds_group @@ -405,6 +406,12 @@ PAGE_GROUPS: tuple[SmokeGroup, ...] = ( ) BROWSER_GROUPS: tuple[SmokeGroup, ...] = ( + SmokeGroup( + "fetch-headers", + "Fetch and native XHR header overrides, redirect uploads, CORS and scoped Authorization.", + "browser", + run_fetch_headers_group, + ), SmokeGroup( "text-input", "Native legacy TextEvent order, cancellation, payload, focus and target lifetime.", diff --git a/moli-cdp-smoke/tests/test_group_selection.py b/moli-cdp-smoke/tests/test_group_selection.py index fc2e78b58af..0d9c2b3d66c 100644 --- a/moli-cdp-smoke/tests/test_group_selection.py +++ b/moli-cdp-smoke/tests/test_group_selection.py @@ -36,6 +36,7 @@ class GroupSelectionTests(unittest.TestCase): self.assertIn("navigation-outcomes", DEFAULT_GROUP_NAMES) self.assertIn("font-face", DEFAULT_GROUP_NAMES) self.assertIn("fetch-font", DEFAULT_GROUP_NAMES) + self.assertIn("fetch-headers", DEFAULT_GROUP_NAMES) self.assertIn("fetch-media", DEFAULT_GROUP_NAMES) self.assertIn("fetch-resource-dispatch", DEFAULT_GROUP_NAMES) self.assertIn("media-error", DEFAULT_GROUP_NAMES) diff --git a/moli-protocol/src/domains/fetch/tests/runtime_fetch.rs b/moli-protocol/src/domains/fetch/tests/runtime_fetch.rs index acb34f15b79..080659362c5 100644 --- a/moli-protocol/src/domains/fetch/tests/runtime_fetch.rs +++ b/moli-protocol/src/domains/fetch/tests/runtime_fetch.rs @@ -1,4 +1,5 @@ use super::*; +mod intercepted_headers; mod native_xhr; mod navigation_disable; use crate::automation::{ diff --git a/moli-protocol/src/domains/fetch/tests/runtime_fetch/intercepted_headers.rs b/moli-protocol/src/domains/fetch/tests/runtime_fetch/intercepted_headers.rs new file mode 100644 index 00000000000..6787302ad42 --- /dev/null +++ b/moli-protocol/src/domains/fetch/tests/runtime_fetch/intercepted_headers.rs @@ -0,0 +1,405 @@ +use super::native_xhr::run_cdp_command_with_network_decisions; +use super::*; +use axum::{body::Bytes, http::Uri}; +use parking_lot::Mutex; + +#[derive(Clone, Copy, Debug)] +enum Api { + Fetch, + AsyncXhr, + SyncXhr, +} + +impl Api { + fn resource_type(self) -> &'static str { + if matches!(self, Self::Fetch) { + "Fetch" + } else { + "XHR" + } + } + + fn expression(self, url: &str, method: &str, headers: Value) -> String { + let body = if method == "POST" { + "'abc'" + } else { + "undefined" + }; + match self { + Self::Fetch => format!( + "fetch({url:?},{{method:{method:?},headers:{headers},body:{body}}}).then(r=>r.text()).catch(e=>'ERROR:'+e.name)" + ), + Self::AsyncXhr | Self::SyncXhr => format!( + "new Promise(resolve=>{{const x=new XMLHttpRequest();x.open({method:?},{url:?},{asynchronous});for(const [k,v] of Object.entries({headers}))x.setRequestHeader(k,v);x.onload=()=>resolve(x.responseText);x.onerror=()=>resolve('ERROR:NetworkError');try{{x.send({body});}}catch(e){{resolve('ERROR:'+e.name);}}}})", + asynchronous = matches!(self, Self::AsyncXhr) + ), + } + } +} + +const APIS: [Api; 3] = [Api::Fetch, Api::AsyncXhr, Api::SyncXhr]; + +struct Fixture { + origin: String, + peer: String, + requests: Arc>>, + servers: Vec>, +} + +impl Drop for Fixture { + fn drop(&mut self) { + for server in &self.servers { + server.abort(); + } + } +} + +impl Fixture { + async fn new() -> Self { + let mut listeners = Vec::new(); + for _ in 0..3 { + listeners.push(TcpListener::bind("127.0.0.1:0").await.unwrap()); + } + let origins = listeners + .iter() + .map(|listener| format!("http://{}", listener.local_addr().unwrap())) + .collect::>(); + let requests = Arc::new(Mutex::new(Vec::new())); + let recorded = requests.clone(); + let peer = origins[1].clone(); + let last = origins[2].clone(); + let app = Router::new().fallback(any(move |method: Method, uri: Uri, headers: HeaderMap, body: Bytes| { + let recorded = recorded.clone(); + let peer = peer.clone(); + let last = last.clone(); + async move { + let header = |name| headers.get(name).and_then(|value| value.to_str().ok()); + let record = json!({"path":uri.path(),"method":method.as_str(),"contentType":header("content-type"),"contentLength":header("content-length"),"origin":header("origin"),"authorization":header("authorization"),"keep":header("x-keep"),"preflight":header("access-control-request-headers"),"body":String::from_utf8_lossy(&body)}); + recorded.lock().push(record.clone()); + let mut response = if method == Method::OPTIONS { + StatusCode::NO_CONTENT.into_response() + } else if uri.path() == "/page" { + axum::response::Html("ready").into_response() + } else if let Some(status) = uri.path().strip_prefix("/start/") { + (StatusCode::from_u16(status.parse().unwrap()).unwrap(), [("location", "/target")], "").into_response() + } else if uri.path() == "/auth-start" { + (StatusCode::FOUND, [("location", format!("{peer}/auth-next"))], "").into_response() + } else if uri.path() == "/auth-next" { + (StatusCode::FOUND, [("location", format!("{last}/target"))], "").into_response() + } else { + axum::Json(record).into_response() + }; + for (name,value) in [("access-control-allow-origin","*"),("access-control-allow-methods","GET, POST"),("access-control-allow-headers","authorization, content-type, x-keep")] { + response.headers_mut().insert(name,value.parse().unwrap()); + } + response + } + })); + let servers = listeners + .into_iter() + .map(|listener| { + let app = app.clone(); + tokio::spawn(async move { + axum::serve(listener, app).await.unwrap(); + }) + }) + .collect(); + Self { + origin: origins[0].clone(), + peer: origins[1].clone(), + requests, + servers, + } + } + + async fn context(&self, api: Api) -> TestContext { + let mut ctx = TestContext::new(); + with_loaded_http_document(&mut ctx, &format!("{}/page", self.origin), "SID-1", "TID-1") + .await; + enable_runtime_async(&mut ctx, "SID-1", 47000).await; + ctx.process_async(json!({"id":47001,"sessionId":"SID-1","method":"Fetch.enable","params":{"patterns":[{"urlPattern":"*","resourceType":api.resource_type()}]}})).await; + ctx.expect_result(47001, json!({}), Some("SID-1")); + self.requests.lock().clear(); + ctx + } +} + +fn continue_request(pause: &Value, headers: Option>, method: Option<&str>) -> Value { + let mut params = json!({"requestId":pause["params"]["requestId"]}); + if let Some(headers) = headers { + params["headers"] = json!(headers); + } + if let Some(method) = method { + params["method"] = json!(method); + } + json!({"id":47003,"sessionId":"SID-1","method":"Fetch.continueRequest","params":params}) +} + +fn echo_headers(pause: &Value) -> Vec { + pause["params"]["request"]["headers"] + .as_object() + .unwrap() + .iter() + .map(|(name, value)| json!({"name":name,"value":value})) + .collect() +} + +async fn request( + ctx: &mut TestContext, + expression: String, + decide: impl FnMut(&Value) -> Value, +) -> (Value, Vec) { + let pauses=run_cdp_command_with_network_decisions(ctx,json!({"id":47002,"sessionId":"SID-1","method":"Runtime.evaluate","params":{"expression":expression,"awaitPromise":true,"returnByValue":true}}),decide).await; + let result = take_response_by_id(ctx, 47002); + assert!( + result.get("error").is_none() && result["result"].get("exceptionDetails").is_none(), + "{result}" + ); + (result["result"]["result"]["value"].clone(), pauses) +} + +async fn assert_terminal(ctx: &mut TestContext, pauses: &[Value], terminal: &str) { + let id = pauses[0]["params"]["networkId"].clone(); + assert!(id.as_str().is_some_and(|id| !id.is_empty())); + for pause in pauses { + assert_eq!(pause["params"]["networkId"], id); + } + wait_until_scheduler_message(ctx, "intercepted request terminal", |message| { + matches!( + message["method"].as_str(), + Some("Network.loadingFailed" | "Network.loadingFinished") + ) && message["params"]["requestId"] == id + }) + .await; + let terminals = ctx + .sent + .iter() + .filter(|message| { + matches!( + message["method"].as_str(), + Some("Network.loadingFailed" | "Network.loadingFinished") + ) && message["params"]["requestId"] == id + }) + .collect::>(); + assert_eq!(terminals.len(), 1, "{terminals:?}"); + assert_eq!(terminals[0]["method"], terminal); +} + +#[tokio::test(flavor = "multi_thread")] +async fn replayed_upload_headers_follow_the_same_rules_for_fetch_and_native_xhr() { + let fixture = Fixture::new().await; + for api in APIS { + for status in [301, 302, 303, 307, 308] { + for repeat_method in [false, true] { + let mut ctx = fixture.context(api).await; + let mut original = None; + let (result, pauses) = request( + &mut ctx, + api.expression( + &format!("{}/start/{status}", fixture.origin), + "POST", + json!({"Content-Type":"text/plain","X-Keep":"yes"}), + ), + |pause| { + if pause["params"]["request"]["url"] + .as_str() + .unwrap() + .contains("/start/") + { + original = Some(echo_headers(pause)); + continue_request(pause, None, None) + } else { + continue_request( + pause, + original.clone(), + repeat_method.then(|| { + pause["params"]["request"]["method"].as_str().unwrap() + }), + ) + } + }, + ) + .await; + assert_eq!(pauses.len(), 2, "{api:?} {status}"); + let result: Value = serde_json::from_str(result.as_str().unwrap()).unwrap(); + let keep_body = matches!(status, 307 | 308); + assert_eq!( + result["method"], + if keep_body { "POST" } else { "GET" }, + "{api:?} {status}" + ); + assert_eq!( + result["contentType"], + if keep_body { + json!("text/plain") + } else { + Value::Null + }, + "{api:?} {status}" + ); + assert_eq!( + result["contentLength"], + if keep_body { json!("3") } else { Value::Null }, + "{api:?} {status}" + ); + assert_eq!(result["body"], if keep_body { "abc" } else { "" }); + assert_eq!(result["keep"], "yes"); + assert_eq!( + result["origin"], + if keep_body { + json!(fixture.origin) + } else { + Value::Null + } + ); + assert_terminal(&mut ctx, &pauses, "Network.loadingFinished").await; + } + } + } +} + +#[tokio::test(flavor = "multi_thread")] +async fn redirected_fetch_and_xhr_accept_explicit_new_content_type() { + let fixture = Fixture::new().await; + for api in APIS { + let mut ctx = fixture.context(api).await; + let (result, pauses) = request( + &mut ctx, + api.expression( + &format!("{}/start/302", fixture.origin), + "POST", + json!({"Content-Type":"text/plain"}), + ), + |pause| { + let redirected = pause["params"]["request"]["method"] == "GET"; + continue_request( + pause, + redirected + .then(|| vec![json!({"name":"Content-Type","value":"application/x-new"})]), + None, + ) + }, + ) + .await; + let result: Value = serde_json::from_str(result.as_str().unwrap()).unwrap(); + assert_eq!(result["method"], "GET", "{api:?}"); + assert_eq!(result["contentType"], "application/x-new", "{api:?}"); + assert_eq!(result["body"], ""); + assert_terminal(&mut ctx, &pauses, "Network.loadingFinished").await; + } +} + +#[tokio::test(flavor = "multi_thread")] +async fn fetch_and_native_xhr_project_origin_without_granting_forbidden_preflight_headers() { + let fixture = Fixture::new().await; + for api in APIS { + for denied in [false, true] { + let mut ctx = fixture.context(api).await; + let mut headers = json!({"Content-Type":"text/plain","X-Keep":"yes"}); + if denied { + headers["X-Denied"] = json!("no"); + } + let (result, pauses) = request( + &mut ctx, + api.expression(&format!("{}/target", fixture.peer), "POST", headers), + |pause| { + assert!( + pause["params"]["request"]["headers"] + .as_object() + .unwrap() + .iter() + .any(|(name, value)| name.eq_ignore_ascii_case("origin") + && value == &fixture.origin), + "{api:?}: {pause}" + ); + continue_request(pause, Some(echo_headers(pause)), None) + }, + ) + .await; + assert_eq!(pauses.len(), 1); + let records = fixture.requests.lock().clone(); + let preflight = records + .iter() + .find(|r| r["method"] == "OPTIONS") + .expect("author X-Keep requires preflight"); + let names = preflight["preflight"] + .as_str() + .unwrap() + .split(',') + .map(str::trim) + .collect::>(); + assert!(names.contains(&"x-keep")); + assert!(!names.contains(&"origin"), "{preflight}"); + if denied { + assert!( + result.as_str().unwrap().starts_with("ERROR:"), + "{api:?}: {result}" + ); + assert!(!records.iter().any(|r| r["method"] == "POST")); + } else { + let result: Value = serde_json::from_str(result.as_str().unwrap()).unwrap(); + assert_eq!(result["origin"], fixture.origin, "{api:?}"); + assert_eq!(result["body"], "abc"); + } + assert_terminal( + &mut ctx, + &pauses, + if denied { + "Network.loadingFailed" + } else { + "Network.loadingFinished" + }, + ) + .await; + } + } +} + +#[tokio::test(flavor = "multi_thread")] +async fn fetch_and_native_xhr_authorization_override_stays_with_its_redirect_hop() { + let fixture = Fixture::new().await; + for api in APIS { + let mut ctx = fixture.context(api).await; + let (result, pauses) = request( + &mut ctx, + api.expression( + &format!("{}/auth-start", fixture.origin), + "GET", + json!({"Authorization":"author-secret"}), + ), + |pause| { + let redirected = + pause["params"]["request"]["url"] == format!("{}/auth-next", fixture.peer); + continue_request( + pause, + redirected.then(|| { + vec![json!({"name":"Authorization","value":"interceptor-secret"})] + }), + None, + ) + }, + ) + .await; + assert_eq!(pauses.len(), 3); + let result: Value = serde_json::from_str(result.as_str().unwrap()).unwrap(); + assert_eq!(result["authorization"], Value::Null, "{api:?}"); + let actual = { + let records = fixture.requests.lock(); + records + .iter() + .filter(|r| r["method"] != "OPTIONS") + .map(|r| r["authorization"].clone()) + .collect::>() + }; + assert_eq!( + actual, + [ + json!("author-secret"), + json!("interceptor-secret"), + Value::Null + ], + "{api:?}" + ); + assert_terminal(&mut ctx, &pauses, "Network.loadingFinished").await; + } +} diff --git a/moli-renderer-v8/src/network_host/async_fetch.rs b/moli-renderer-v8/src/network_host/async_fetch.rs index 255725486a4..cbdee08064f 100644 --- a/moli-renderer-v8/src/network_host/async_fetch.rs +++ b/moli-renderer-v8/src/network_host/async_fetch.rs @@ -29,14 +29,7 @@ pub(crate) async fn fetch_intercepted_window_hop( cancel_handle: Option, ) -> Result, String> { let mode = request.redirect_mode(); - // DevTools can echo generated fields (Origin, Host, Content-Length, - // Sec-*). Those are not author CORS headers requiring preflight permission. - let preflight_headers = request - .request_headers - .to_byte_strings() - .into_iter() - .filter(|(name, _)| !moli_fetch::is_forbidden_request_header_name(name)) - .collect(); + let preflight_headers = request.request_headers.to_byte_strings(); let cancel = cancel_handle.unwrap_or_default(); let response_cancel = cancel.child_for_subrequest(); let observed = fetch_subresource_hop( @@ -157,6 +150,12 @@ pub(super) async fn fetch_subresource_hop( preflight_headers: Vec<(String, String)>, observer: Option<&CorsPreflightNetworkObserver>, ) -> Result, String> { + // Both intercepted fetch and native XHR can echo browser-generated fields. + // Origin, Host, Content-Length and Sec-* are not author CORS permissions. + let preflight_headers: Vec<_> = preflight_headers + .into_iter() + .filter(|(name, _)| !moli_fetch::is_forbidden_request_header_name(name)) + .collect(); run_cors_preflight_if_needed( loader, &request, diff --git a/moli-renderer-v8/src/network_host/controlled_xhr.rs b/moli-renderer-v8/src/network_host/controlled_xhr.rs index cffc4fc3d97..8b4fb5801b3 100644 --- a/moli-renderer-v8/src/network_host/controlled_xhr.rs +++ b/moli-renderer-v8/src/network_host/controlled_xhr.rs @@ -216,7 +216,7 @@ impl ControlledResourceLoad { fn sync_info(&mut self) { self.info.url = self.request.url.clone(); self.info.method = self.request.method.clone(); - self.info.request_headers = self.request.request_headers.clone(); + self.info.request_headers = self.request.interception_request_headers(); self.info.request_body_bytes = self.request.body.clone(); self.info.request_body = self .request @@ -225,6 +225,15 @@ impl ControlledResourceLoad { .map(|body| String::from_utf8_lossy(body).into_owned()); } + fn apply_request_overrides(&mut self, overrides: moli_fetch::RequestOverrides) { + let headers_changed = overrides.headers.is_some(); + overrides.apply(&mut self.request); + if headers_changed { + self.preflight_headers = self.request.request_headers.to_byte_strings(); + } + self.sync_info(); + } + fn check_request(&self) -> Result<(), String> { let redirect_status = if self.request.redirect_count() == 0 { crate::content_security_policy::ContentSecurityPolicyRedirectStatus::NoRedirect @@ -327,20 +336,14 @@ impl ControlledResourceLoad { intercept_response: response, handle_auth_requests: auth, } => { - let headers_changed = headers.is_some(); - moli_fetch::RequestOverrides { + self.apply_request_overrides(moli_fetch::RequestOverrides { url, method, body, headers, - } - .apply(&mut self.request); - if headers_changed { - self.preflight_headers = self.request.request_headers.to_byte_strings(); - } + }); intercept_response = response; handle_auth_requests = auth; - self.sync_info(); } D::Fail(error) => return Err(error), D::Fulfill { @@ -350,13 +353,7 @@ impl ControlledResourceLoad { body, } => { if let Some(overrides) = request_overrides { - let headers_changed = overrides.headers.is_some(); - overrides.apply(&mut self.request); - if headers_changed { - self.preflight_headers = - self.request.request_headers.to_byte_strings(); - } - self.sync_info(); + self.apply_request_overrides(*overrides); } synthetic = Some(self.synthetic_response(status, headers, body)); intercept_response = false; diff --git a/moli-renderer-v8/src/network_host/controlled_xhr/context.rs b/moli-renderer-v8/src/network_host/controlled_xhr/context.rs index 94f71588010..5cbf576a9ab 100644 --- a/moli-renderer-v8/src/network_host/controlled_xhr/context.rs +++ b/moli-renderer-v8/src/network_host/controlled_xhr/context.rs @@ -34,11 +34,12 @@ impl ControlledXhrContext { pub(crate) fn load( self, request: moli_fetch::Request, - info: PendingSubresourceFetchInfo, + mut info: PendingSubresourceFetchInfo, load: ResourceLoadLease, cancel: moli_fetch::FetchCancelHandle, preflight_headers: Vec<(String, String)>, ) -> super::ControlledResourceLoad { + info.request_headers = request.interception_request_headers(); super::ControlledResourceLoad { request, info,