Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(fetch): bound authentication retries per logical request
ldm06f0e9631ef0e
diff --git a/moli-fetch/src/auth_retry.rs b/moli-fetch/src/auth_retry.rsnew file mode 100644index 000000000..bff3a1450--- /dev/null+++ b/moli-fetch/src/auth_retry.rs@@ -0,0 +1,19 @@+/// Authentication restart policy for one logical request.+///+/// Retain this value across transport calls and authentication pauses. The+/// initial request is free; like Chromium's HttpNetworkTransaction, the 32nd+/// restart is rejected before sending another request.+#[derive(Clone, Debug, Default)]+pub struct AuthRetryBudget {+ restarts: u8,+}++impl AuthRetryBudget {+ pub const MAX_RESTARTS: u8 = 32;+ pub const ERROR_TEXT: &'static str = "net::ERR_TOO_MANY_RETRIES";++ pub fn try_restart(&mut self) -> bool {+ self.restarts = self.restarts.saturating_add(1);+ self.restarts < Self::MAX_RESTARTS+ }+}diff --git a/moli-fetch/src/lib.rs b/moli-fetch/src/lib.rsindex 18bc414c6..c52b51d23 100644--- a/moli-fetch/src/lib.rs+++ b/moli-fetch/src/lib.rs@@ -4,6 +4,7 @@//! execution, streaming HTML responses, and cookie-aware helper utilities used//! by higher-level browser code.+mod auth_retry;mod blocking;mod cancellation;mod client;@@ -27,6 +28,7 @@ mod streaming_response;mod tests;mod url_pattern;+pub use auth_retry::AuthRetryBudget;#[cfg(any(test, feature = "test-support"))]pub use blocking::{StreamingHtmlResponseStart, StreamingResponseCollector, outgoing_request_headers,diff --git a/moli-protocol/src/conn/dispatch_tests/bidi.rs b/moli-protocol/src/conn/dispatch_tests/bidi.rsindex a01c3bbb6..6c13af4b8 100644--- a/moli-protocol/src/conn/dispatch_tests/bidi.rs+++ b/moli-protocol/src/conn/dispatch_tests/bidi.rs@@ -16,6 +16,7 @@ async fn bidi_fetch_control_resolves_background_request_owner() {conn.register_pending_fetch_navigation_request_for_owner(&crate::conn::CommandOwnerScope::for_session("SID-background"),PendingFetchNavigation {+ auth_retry_budget: Default::default(),fetch_request_id: "FETCH-background".to_owned(),interception_session_id: Some("bidi-session-1".to_owned()),document_navigation_token: None,diff --git a/moli-protocol/src/conn/fetch_support.rs b/moli-protocol/src/conn/fetch_support.rsindex 7d915e2a9..164eedbb6 100644--- a/moli-protocol/src/conn/fetch_support.rs+++ b/moli-protocol/src/conn/fetch_support.rs@@ -223,6 +223,8 @@ impl PendingSubresourceFetchOwnerKind {#[derive(Debug, Clone)]pub struct PendingFetchNavigation {+ /// Authentication restart budget for this logical navigation, independent of pause IDs.+ pub(crate) auth_retry_budget: moli_fetch::AuthRetryBudget,pub fetch_request_id: String,pub interception_session_id: Option<String>,pub(crate) document_navigation_token: Option<DocumentNavigationToken>,@@ -235,6 +237,7 @@ pub struct PendingFetchNavigation {#[derive(Debug, Clone)]pub struct PendingFetchAuthNavigation {+ pub(crate) auth_retry_budget: moli_fetch::AuthRetryBudget,pub owner_session_id: Option<String>,// Auth can chain across CDP and BiDi sessions. Keep the current auth action// identity separate from the original Fetch response-stage identity.diff --git a/moli-protocol/src/conn/state/fetch.rs b/moli-protocol/src/conn/state/fetch.rsindex afb73cbf2..f1a5bb384 100644--- a/moli-protocol/src/conn/state/fetch.rs+++ b/moli-protocol/src/conn/state/fetch.rs@@ -2407,6 +2407,7 @@ mod tests {owner_session_id: Option<&str>,) -> PendingFetchAuthNavigation {PendingFetchAuthNavigation {+ auth_retry_budget: Default::default(),owner_session_id: owner_session_id.map(str::to_owned),action_session_id: owner_session_id.map(str::to_owned),interception_session_id: owner_session_id.map(str::to_owned),diff --git a/moli-protocol/src/conn/state/fetch/tests/supersession.rs b/moli-protocol/src/conn/state/fetch/tests/supersession.rsindex bca5cc823..e1ef06305 100644--- a/moli-protocol/src/conn/state/fetch/tests/supersession.rs+++ b/moli-protocol/src/conn/state/fetch/tests/supersession.rs@@ -11,6 +11,7 @@ fn navigation(token: &DocumentNavigationToken, id: &str) -> NavigationDispatchStfn request(token: &DocumentNavigationToken, id: &str) -> PendingFetchNavigation {PendingFetchNavigation {+ auth_retry_budget: Default::default(),fetch_request_id: id.to_owned(),interception_session_id: Some("SID-A".to_owned()),document_navigation_token: Some(token.clone()),diff --git a/moli-protocol/src/conn/state/tests.rs b/moli-protocol/src/conn/state/tests.rsindex 1cb722562..719daeda8 100644--- a/moli-protocol/src/conn/state/tests.rs+++ b/moli-protocol/src/conn/state/tests.rs@@ -807,6 +807,7 @@ fn target_fetch_owner_reports_matching_auth_required_network_intercepts() {fn pending_navigation_rejects_generic_request_action_without_consuming_id() {let mut state = TargetFetchState::default();state.register_pending_fetch_navigation_request(PendingFetchNavigation {+ auth_retry_budget: Default::default(),fetch_request_id: "FETCH-NAV".to_owned(),interception_session_id: Some("SID-fetch".to_owned()),document_navigation_token: None,@@ -848,6 +849,7 @@ fn pending_auth_navigation_rejects_generic_request_action_without_consuming_id()state.register_pending_fetch_auth_navigation("FETCH-AUTH".to_owned(),PendingFetchAuthNavigation {+ auth_retry_budget: Default::default(),owner_session_id: None,action_session_id: None,interception_session_id: None,@@ -898,6 +900,7 @@ fn unscoped_fetch_owned_auth_navigation_allows_routed_action_session() {state.register_pending_fetch_auth_navigation("FETCH-AUTH".to_owned(),PendingFetchAuthNavigation {+ auth_retry_budget: Default::default(),owner_session_id: None,action_session_id: None,interception_session_id: None,@@ -932,6 +935,7 @@ fn unscoped_fetch_owned_auth_navigation_allows_routed_action_session() {state.register_pending_fetch_auth_navigation("NETWORK-AUTH".to_owned(),PendingFetchAuthNavigation {+ auth_retry_budget: Default::default(),owner_session_id: None,action_session_id: None,interception_session_id: None,diff --git a/moli-protocol/src/domains/fetch/auth.rs b/moli-protocol/src/domains/fetch/auth.rsindex 166c960bd..900c6c7d7 100644--- a/moli-protocol/src/domains/fetch/auth.rs+++ b/moli-protocol/src/domains/fetch/auth.rs@@ -570,6 +570,7 @@ pub(super) async fn complete_continue_with_auth_command_async(conn,out,PendingFetchNavigation {+ auth_retry_budget: pending.auth_retry_budget,fetch_request_id: pending.response_stage_request_id,interception_session_id: pending.interception_session_id.clone(),document_navigation_token: pending.document_navigation_token,diff --git a/moli-protocol/src/domains/fetch/navigation.rs b/moli-protocol/src/domains/fetch/navigation.rsindex b2c1d8688..d7c18ce08 100644--- a/moli-protocol/src/domains/fetch/navigation.rs+++ b/moli-protocol/src/domains/fetch/navigation.rs@@ -62,6 +62,18 @@ pub(crate) async fn load_or_pause_navigation_for_auth_into_buffer_async(auth: Option<SubresourceAuthCredentials>,prior_network_observation_journal: Option<NetworkObservationJournal>,) {+ if auth.is_some() && !pending.auth_retry_budget.try_restart() {+ complete_pending_fetch_navigation_result_into_buffer_async(+ conn,+ out,+ pending,+ Ok(NavigationLoadOutcome::network_failure(+ moli_fetch::AuthRetryBudget::ERROR_TEXT.to_owned(),+ )),+ )+ .await;+ return;+ }let navigate_owner = pending.navigation.owner.clone();network::record_main_document_request_body(conn, &pending.navigation);let should_handle_auth = conn.target_fetch_matches_auth_required_for_owner(@@ -268,6 +280,7 @@ pub(super) async fn cancel_navigation_auth_as_background_events_async(Err(response) => response.as_ref().clone(),};let mut pending = PendingFetchNavigation {+ auth_retry_budget: pending_auth.auth_retry_budget,fetch_request_id: pending_auth.response_stage_request_id,interception_session_id: pending_auth.interception_session_id.clone(),document_navigation_token: pending_auth.document_navigation_token,@@ -829,6 +842,7 @@ fn register_navigation_auth_required_event(navigation.redirect_chain = head.redirect_chain.clone();let blocked_intercepts = navigation_auth_required_blocked_intercepts(conn, pending);let mut pending_auth = crate::conn::PendingFetchAuthNavigation {+ auth_retry_budget: pending.auth_retry_budget.clone(),owner_session_id: pending.navigation.owner.session_id().map(str::to_owned),action_session_id: pending.interception_session_id.clone(),interception_session_id: pending.interception_session_id.clone(),diff --git a/moli-protocol/src/domains/fetch/tests/basics.rs b/moli-protocol/src/domains/fetch/tests/basics.rsindex f2485a450..cd60be335 100644--- a/moli-protocol/src/domains/fetch/tests/basics.rs+++ b/moli-protocol/src/domains/fetch/tests/basics.rs@@ -1056,6 +1056,7 @@ async fn disable_clears_fetch_state() {bc.active_page_target_mut().fetch_owner.register_pending_fetch_navigation_request(PendingFetchNavigation {+ auth_retry_budget: Default::default(),fetch_request_id: "INT-1".to_owned(),interception_session_id: Some("SID-1".to_owned()),document_navigation_token: None,@@ -1098,6 +1099,7 @@ async fn disable_clears_fetch_state() {.register_pending_fetch_auth_navigation("INT-1".to_owned(),PendingFetchAuthNavigation {+ auth_retry_budget: Default::default(),owner_session_id: None,action_session_id: Some("SID-1".to_owned()),interception_session_id: Some("SID-1".to_owned()),@@ -1330,6 +1332,7 @@ async fn continue_with_auth_rejects_invalid_response_without_consuming_pending_a.register_pending_fetch_auth_navigation("INT-8".to_owned(),PendingFetchAuthNavigation {+ auth_retry_budget: Default::default(),owner_session_id: Some("SID-1".to_owned()),action_session_id: Some("SID-1".to_owned()),interception_session_id: Some("SID-1".to_owned()),@@ -1411,6 +1414,7 @@ async fn continue_with_auth_unsupported_challenge_preserves_pending_auth_navigat.register_pending_fetch_auth_navigation("INT-9".to_owned(),PendingFetchAuthNavigation {+ auth_retry_budget: Default::default(),owner_session_id: Some("SID-1".to_owned()),action_session_id: Some("SID-1".to_owned()),interception_session_id: Some("SID-1".to_owned()),@@ -1593,6 +1597,7 @@ fn extract_auth_challenge_preserves_quoted_commas_in_realm() {#[test]fn emit_auth_required_preserves_request_headers_and_post_data_shape() {let pending = PendingFetchNavigation {+ auth_retry_budget: Default::default(),fetch_request_id: "INT-11".to_owned(),interception_session_id: Some("SID-1".to_owned()),document_navigation_token: None,diff --git a/moli-protocol/src/domains/fetch/tests/mod.rs b/moli-protocol/src/domains/fetch/tests/mod.rsindex e48038cb3..935fdc669 100644--- a/moli-protocol/src/domains/fetch/tests/mod.rs+++ b/moli-protocol/src/domains/fetch/tests/mod.rs@@ -322,6 +322,7 @@ mod basics;mod command_correlation;mod header_bytes;mod navigation_auth;+mod navigation_auth_retry;mod navigation_control;mod navigation_response_stage;mod navigation_subresource;@@ -330,3 +331,4 @@ mod runtime_auth_response;mod runtime_fetch;mod runtime_websocket;mod runtime_xhr;+mod subresource_auth_retry;diff --git a/moli-protocol/src/domains/fetch/tests/navigation_auth_retry.rs b/moli-protocol/src/domains/fetch/tests/navigation_auth_retry.rsnew file mode 100644index 000000000..ac9e32a6b--- /dev/null+++ b/moli-protocol/src/domains/fetch/tests/navigation_auth_retry.rs@@ -0,0 +1,297 @@+const MAX_NAVIGATION_AUTH_RETRIES: u8 = moli_fetch::AuthRetryBudget::MAX_RESTARTS;+use super::*;++#[derive(Clone, Copy, Debug)]+enum AuthEndpoint {+ Server,+ HttpProxy,+ ConnectProxy,+}++async fn begin_navigation(ctx: &mut TestContext, url: &str, intercept_response: bool) -> String {+ ctx.sent.clear();+ ctx.process_async(json!({+ "id": 2000,+ "method": "Page.navigate",+ "sessionId": "SID-1",+ "params": { "url": url }+ }))+ .await;+ wait_until_message(ctx, "SID-1", "main-document request pause", |message| {+ message["method"] == json!("Fetch.requestPaused")+ && message["params"]["request"]["url"] == json!(url)+ && message["params"].get("responseStatusCode").is_none()+ })+ .await;+ let paused = ctx.take_first_matching("main-document request pause", |message| {+ message["method"] == json!("Fetch.requestPaused")+ && message["params"]["request"]["url"] == json!(url)+ && message["params"].get("responseStatusCode").is_none()+ });+ let network_id = paused["params"]["networkId"].as_str().unwrap().to_owned();+ ctx.process_async(json!({+ "id": 2001,+ "method": "Fetch.continueRequest",+ "sessionId": "SID-1",+ "params": {+ "requestId": paused["params"]["requestId"],+ "interceptResponse": intercept_response+ }+ }))+ .await;+ ctx.expect_result(2001, json!({}), Some("SID-1"));+ network_id+}++async fn answer_auth(ctx: &mut TestContext, chained: bool, password: Option<&str>) -> String {+ let mut auth = ctx.take_first_matching("navigation auth challenge", |message| {+ message["method"] == json!("Fetch.authRequired")+ });+ assert_eq!(auth["sessionId"], "SID-1");+ if chained {+ ctx.process_async(json!({+ "id": 2002,+ "method": "Fetch.continueWithAuth",+ "sessionId": "SID-1",+ "params": {+ "requestId": auth["params"]["requestId"],+ "authChallengeResponse": { "response": "Default" }+ }+ }))+ .await;+ ctx.expect_result(2002, json!({}), Some("SID-1"));+ let next = ctx.take_first_matching("chained navigation auth challenge", |message| {+ message["method"] == json!("Fetch.authRequired")+ });+ assert_eq!(next["sessionId"], "SID-auth");+ assert_ne!(next["params"]["requestId"], auth["params"]["requestId"]);+ auth = next;+ }+ let request_id = auth["params"]["requestId"].as_str().unwrap().to_owned();+ let session_id = auth["sessionId"].as_str().unwrap().to_owned();+ let response = match password {+ Some(password) => json!({+ "response": "ProvideCredentials", "username": "user", "password": password+ }),+ None => json!({ "response": "CancelAuth" }),+ };+ ctx.process_async(json!({+ "id": 2003,+ "method": "Fetch.continueWithAuth",+ "sessionId": session_id,+ "params": { "requestId": request_id, "authChallengeResponse": response }+ }))+ .await;+ ctx.expect_result(2003, json!({}), Some(&session_id));+ request_id+}++async fn verify_auth_retry_limit(endpoint: AuthEndpoint, intercept_response: bool, chained: bool) {+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();+ let addr = listener.local_addr().unwrap();+ let requests = Arc::new(AtomicUsize::new(0));+ let server_requests = requests.clone();+ let server = tokio::spawn(async move {+ loop {+ let (mut stream, _) = listener.accept().await.unwrap();+ let mut request = Vec::new();+ let mut buffer = [0; 4096];+ while !request.ends_with(b"\r\n\r\n") {+ let read = stream.read(&mut buffer).await.unwrap();+ assert_ne!(read, 0);+ request.extend_from_slice(&buffer[..read]);+ assert!(request.len() < 16384);+ }+ server_requests.fetch_add(1, Ordering::SeqCst);+ let request = String::from_utf8(request).unwrap();+ let (status, header, method) = match endpoint {+ AuthEndpoint::Server => ("401 Unauthorized", "WWW-Authenticate", "GET /"),+ AuthEndpoint::HttpProxy => (+ "407 Proxy Authentication Required",+ "Proxy-Authenticate",+ "GET http://",+ ),+ AuthEndpoint::ConnectProxy => (+ "407 Proxy Authentication Required",+ "Proxy-Authenticate",+ "CONNECT ",+ ),+ };+ assert!(request.starts_with(method), "{request}");+ let auth_header = match endpoint {+ AuthEndpoint::Server => "authorization",+ _ => "proxy-authorization",+ };+ let authorized = request.lines().any(|line| {+ line.split_once(':').is_some_and(|(name, value)| {+ name.eq_ignore_ascii_case(auth_header) && value.trim() == "Basic dXNlcjpwYXNz"+ })+ });+ let response = if authorized && !matches!(endpoint, AuthEndpoint::ConnectProxy) {+ let body = "<!doctype html><title>authorized</title>authorized";+ format!(+ "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",+ body.len()+ )+ } else {+ format!(+ "HTTP/1.1 {status}\r\n{header}: Basic realm=\"retry-limit\"\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"+ )+ };+ stream.write_all(response.as_bytes()).await.unwrap();+ stream.shutdown().await.unwrap();+ }+ });++ let mut ctx = TestContext::new();+ let mut bc = attached_browser_context();+ if chained {+ assert!(bc.assign_attached_session_to_target("TID-1", "SID-auth".to_owned()));+ }+ bc.active_page_target_mut()+ .runtime_slot+ .enable_primary_network_events();+ ctx.conn.install_browser_context_fixture_for_test(bc);+ ctx.enable_page_events_for_test(Some("SID-1"));+ let url = match endpoint {+ AuthEndpoint::Server => format!("http://{addr}/auth"),+ AuthEndpoint::HttpProxy => "http://example.test/auth".to_owned(),+ AuthEndpoint::ConnectProxy => "https://example.test/auth".to_owned(),+ };+ if !matches!(endpoint, AuthEndpoint::Server) {+ ctx.conn+ .set_http_proxy_override_async(Some(format!("http://{addr}")))+ .await;+ }+ ctx.process_async(json!({+ "id": 1998, "method": "Fetch.enable", "sessionId": "SID-1",+ "params": { "handleAuthRequests": true }+ }))+ .await;+ ctx.expect_result(1998, json!({}), Some("SID-1"));+ if chained {+ ctx.process_async(json!({+ "id": 1999, "method": "Fetch.enable", "sessionId": "SID-auth",+ "params": {+ "handleAuthRequests": true,+ "patterns": [{ "urlPattern": "*", "requestStage": "Response" }]+ }+ }))+ .await;+ ctx.expect_result(1999, json!({}), Some("SID-auth"));+ }++ let network_id = begin_navigation(&mut ctx, &url, intercept_response).await;+ let mut last_request_id = String::new();+ for _ in 0..MAX_NAVIGATION_AUTH_RETRIES {+ last_request_id = answer_auth(&mut ctx, chained, Some("wrong")).await;+ }+ let navigate = take_response_by_id(&mut ctx, 2000);+ assert_eq!(navigate["result"]["errorText"], "net::ERR_TOO_MANY_RETRIES");+ let failed = ctx+ .sent+ .iter()+ .filter(|message| {+ message["method"] == json!("Network.loadingFailed")+ && message["params"]["requestId"] == json!(network_id)+ })+ .collect::<Vec<_>>();+ assert_eq!(failed.len(), 1);+ assert_eq!(+ failed[0]["params"]["errorText"],+ "net::ERR_TOO_MANY_RETRIES"+ );+ assert_eq!(+ requests.load(Ordering::SeqCst),+ usize::from(MAX_NAVIGATION_AUTH_RETRIES)+ );+ assert!(!ctx.sent.iter().any(|message| {+ matches!(+ message["method"].as_str(),+ Some("Fetch.authRequired" | "Network.loadingFinished")+ )+ }));+ assert!(+ !ctx.conn+ .has_pending_document_navigation_for_session_owner(Some("SID-1"))+ );+ let fetch_owner = &ctx+ .conn+ .browser_context+ .as_ref()+ .unwrap()+ .active_page_target()+ .fetch_owner;+ assert!(!fetch_owner.has_pending_fetch_auth_navigation_for_test(&last_request_id));+ assert!(!fetch_owner.has_pending_fetch_auth_navigation_for_test("INT-1"));++ // A fresh navigation gets its own budget and can correct an earlier mistake.+ begin_navigation(&mut ctx, &url, intercept_response).await;+ let wrong_attempts = if matches!(endpoint, AuthEndpoint::ConnectProxy) {+ 1+ } else {+ MAX_NAVIGATION_AUTH_RETRIES - 2+ };+ for _ in 0..wrong_attempts {+ answer_auth(&mut ctx, chained, Some("wrong")).await;+ }+ if matches!(endpoint, AuthEndpoint::ConnectProxy) {+ answer_auth(&mut ctx, chained, None).await;+ let navigate = take_response_by_id(&mut ctx, 2000);+ assert_eq!(+ navigate["result"]["errorText"],+ "net::ERR_HTTP_RESPONSE_CODE_FAILURE"+ );+ } else {+ answer_auth(&mut ctx, chained, Some("pass")).await;+ if intercept_response {+ let paused = ctx.take_first_matching("authenticated response pause", |message| {+ message["method"] == json!("Fetch.requestPaused")+ && message["params"]["responseStatusCode"] == json!(200)+ });+ ctx.process_async(json!({+ "id": 2004, "method": "Fetch.continueResponse", "sessionId": paused["sessionId"],+ "params": { "requestId": paused["params"]["requestId"] }+ }))+ .await;+ ctx.expect_result(2004, json!({}), paused["sessionId"].as_str());+ }+ assert!(+ take_response_by_id(&mut ctx, 2000)["result"]+ .get("errorText")+ .is_none()+ );+ wait_until_frame_stopped_loading(&mut ctx, "TID-1").await;+ }+ assert!(+ !ctx.conn+ .has_pending_document_navigation_for_session_owner(Some("SID-1"))+ );+ server.abort();+}++#[tokio::test(flavor = "multi_thread")]+async fn basic_server_auth_navigation_retries_are_bounded() {+ for streaming in [false, true] {+ verify_auth_retry_limit(AuthEndpoint::Server, streaming, false).await;+ }+}++#[tokio::test(flavor = "multi_thread")]+async fn basic_http_proxy_auth_navigation_retries_are_bounded() {+ for streaming in [false, true] {+ verify_auth_retry_limit(AuthEndpoint::HttpProxy, streaming, false).await;+ }+}++#[tokio::test(flavor = "multi_thread")]+async fn basic_connect_proxy_auth_navigation_retries_are_bounded() {+ for streaming in [false, true] {+ verify_auth_retry_limit(AuthEndpoint::ConnectProxy, streaming, false).await;+ }+}++#[tokio::test(flavor = "multi_thread")]+async fn basic_auth_navigation_retry_budget_survives_chained_session_request_ids() {+ verify_auth_retry_limit(AuthEndpoint::Server, true, true).await;+}diff --git a/moli-protocol/src/domains/fetch/tests/subresource_auth_retry.rs b/moli-protocol/src/domains/fetch/tests/subresource_auth_retry.rsnew file mode 100644index 000000000..3788e9945--- /dev/null+++ b/moli-protocol/src/domains/fetch/tests/subresource_auth_retry.rs@@ -0,0 +1,248 @@+use super::*;++#[derive(Clone, Copy)]+enum RequestKind {+ Fetch,+ Xhr,+ WorkerFetch,+ WorkerXhr,+}++async fn begin_request(+ ctx: &mut TestContext,+ kind: RequestKind,+ url: &str,+ slot: u8,+) -> (String, String) {+ let url_literal = serde_json::to_string(url).unwrap();+ let fetch_body = format!(+ "fetch({url_literal}).then(r => r.text()).then(resolve).catch(() => resolve('failed'));"+ );+ let xhr_body = format!(+ "const xhr = new XMLHttpRequest(); xhr.open('GET', {url_literal}); xhr.onload = () => resolve(xhr.responseText); xhr.onerror = () => resolve('failed'); xhr.send();"+ );+ let body = match kind {+ RequestKind::Fetch => fetch_body,+ RequestKind::Xhr => xhr_body,+ RequestKind::WorkerFetch | RequestKind::WorkerXhr => {+ let body = if matches!(kind, RequestKind::WorkerFetch) {+ fetch_body+ } else {+ xhr_body+ };+ let worker_code = serde_json::to_string(&format!(+ "const resolve = value => postMessage(value); {body}"+ ))+ .unwrap();+ format!(+ "const worker = new Worker(URL.createObjectURL(new Blob([{worker_code}], {{type: 'text/javascript'}}))); worker.onmessage = event => {{ resolve(event.data); worker.terminate(); }}; worker.onerror = () => {{ resolve('failed'); worker.terminate(); }};"+ )+ }+ };+ ctx.process_async(json!({+ "id": 3000,+ "method": "Runtime.evaluate", "sessionId": "SID-1",+ "params": { "expression": format!("globalThis.__auth_promise_{slot} = new Promise(resolve => {{ {body} }}); 'scheduled'") }+ })).await;+ let _ = take_response_by_id(ctx, 3000);+ wait_until_message(ctx, "SID-1", "subresource request pause", |message| {+ message["method"] == json!("Fetch.requestPaused")+ && message["params"]["request"]["url"] == json!(url)+ })+ .await;+ let paused = ctx.take_first_matching("subresource request pause", |message| {+ message["method"] == json!("Fetch.requestPaused")+ && message["params"]["request"]["url"] == json!(url)+ });+ let request_id = paused["params"]["requestId"].as_str().unwrap().to_owned();+ let network_id = paused["params"]["networkId"].as_str().unwrap().to_owned();+ ctx.process_async(json!({+ "id": 3001, "method": "Fetch.continueRequest", "sessionId": "SID-1",+ "params": { "requestId": request_id }+ }))+ .await;+ ctx.expect_result(3001, json!({}), Some("SID-1"));+ (request_id, network_id)+}++async fn provide_credentials(ctx: &mut TestContext, request_id: &str, password: &str) {+ wait_until_message(ctx, "SID-1", "subresource auth challenge", |message| {+ message["method"] == json!("Fetch.authRequired")+ && message["params"]["requestId"] == json!(request_id)+ })+ .await;+ ctx.take_first_matching("subresource auth challenge", |message| {+ message["method"] == json!("Fetch.authRequired")+ && message["params"]["requestId"] == json!(request_id)+ });+ ctx.process_async(json!({+ "id": 3002, "method": "Fetch.continueWithAuth", "sessionId": "SID-1",+ "params": {+ "requestId": request_id,+ "authChallengeResponse": { "response": "ProvideCredentials", "username": "user", "password": password }+ }+ })).await;+ ctx.expect_result(3002, json!({}), Some("SID-1"));+}++async fn verify_request_budgets(kind: RequestKind) {+ let requests = Arc::new(AtomicUsize::new(0));+ let observed_requests = requests.clone();+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();+ let addr = listener.local_addr().unwrap();+ let server = tokio::spawn(async move {+ axum::serve(+ listener,+ Router::new()+ .route(+ "/page",+ get(|| async {+ (+ [(CONTENT_TYPE.as_str(), "text/html")],+ "<!doctype html><title>auth retry</title>",+ )+ }),+ )+ .route(+ "/protected",+ get(move |headers: HeaderMap| {+ let requests = observed_requests.clone();+ async move {+ requests.fetch_add(1, Ordering::SeqCst);+ if headers+ .get("authorization")+ .is_some_and(|value| value == "Basic dXNlcjpwYXNz")+ {+ (StatusCode::OK, "authorized").into_response()+ } else {+ (+ StatusCode::UNAUTHORIZED,+ [(WWW_AUTHENTICATE.as_str(), "Basic realm=\"retry-limit\"")],+ "auth required",+ )+ .into_response()+ }+ }+ }),+ ),+ )+ .await+ .unwrap();+ });+ let mut ctx = TestContext::new();+ with_loaded_http_document(&mut ctx, &format!("http://{addr}/page"), "SID-1", "TID-1").await;+ ctx.process_async(json!({ "id": 2997, "method": "Network.enable", "sessionId": "SID-1" }))+ .await;+ ctx.expect_result(2997, json!({}), Some("SID-1"));+ ctx.process_async(json!({+ "id": 2998, "method": "Fetch.enable", "sessionId": "SID-1",+ "params": { "handleAuthRequests": true, "patterns": [{ "urlPattern": "*/protected*" }] }+ }))+ .await;+ ctx.expect_result(2998, json!({}), Some("SID-1"));+ enable_runtime_async(&mut ctx, "SID-1", 2999).await;+ ctx.sent.clear();++ let (first_id, first_network_id) =+ begin_request(&mut ctx, kind, &format!("http://{addr}/protected?first"), 0).await;+ let (second_id, second_network_id) = begin_request(+ &mut ctx,+ kind,+ &format!("http://{addr}/protected?second"),+ 1,+ )+ .await;+ for _ in 0..moli_fetch::AuthRetryBudget::MAX_RESTARTS {+ provide_credentials(&mut ctx, &first_id, "wrong").await;+ }+ wait_until_message(&mut ctx, "SID-1", "auth retry exhaustion", |message| {+ message["method"] == json!("Network.loadingFailed")+ && message["params"]["requestId"] == json!(first_network_id)+ })+ .await;+ let failures = ctx+ .sent+ .iter()+ .filter(|message| {+ message["method"] == json!("Network.loadingFailed")+ && message["params"]["requestId"] == json!(first_network_id)+ })+ .collect::<Vec<_>>();+ assert_eq!(failures.len(), 1);+ assert_eq!(+ failures[0]["params"]["errorText"],+ moli_fetch::AuthRetryBudget::ERROR_TEXT+ );+ assert_eq!(+ requests.load(Ordering::SeqCst),+ usize::from(moli_fetch::AuthRetryBudget::MAX_RESTARTS) + 1+ );+ assert!(!ctx.sent.iter().any(|message| {+ (message["method"] == json!("Fetch.authRequired")+ && message["params"]["requestId"] == json!(first_id))+ || (message["method"] == json!("Network.loadingFinished")+ && message["params"]["requestId"] == json!(first_network_id))+ }));++ // This request was already paused before the first exhausted its budget.+ provide_credentials(&mut ctx, &second_id, "wrong").await;+ provide_credentials(&mut ctx, &second_id, "pass").await;+ wait_until_message(+ &mut ctx,+ "SID-1",+ "independent request succeeds",+ |message| {+ message["method"] == json!("Network.loadingFinished")+ && message["params"]["requestId"] == json!(second_network_id)+ },+ )+ .await;+ assert_eq!(+ requests.load(Ordering::SeqCst),+ usize::from(moli_fetch::AuthRetryBudget::MAX_RESTARTS) + 3+ );+ for (slot, expected) in [(0, "failed"), (1, "authorized")] {+ ctx.process_async(json!({+ "id": 3003, "method": "Runtime.evaluate", "sessionId": "SID-1",+ "params": { "expression": format!("globalThis.__auth_promise_{slot}"), "awaitPromise": true, "returnByValue": true }+ })).await;+ wait_until_message(&mut ctx, "SID-1", "auth promise settles", |message| {+ message["id"] == json!(3003)+ })+ .await;+ assert_eq!(+ take_response_by_id(&mut ctx, 3003)["result"]["result"]["value"],+ expected+ );+ }+ assert!(+ !ctx.conn+ .browser_context+ .as_ref()+ .unwrap()+ .active_page_target()+ .fetch_owner+ .has_pending_fetch_state_for_test()+ );+ server.abort();+}++#[tokio::test(flavor = "multi_thread")]+async fn fetch_auth_retry_budgets_are_bounded_and_request_local() {+ verify_request_budgets(RequestKind::Fetch).await;+}++#[tokio::test(flavor = "multi_thread")]+async fn xhr_auth_retry_budgets_are_bounded_and_request_local() {+ verify_request_budgets(RequestKind::Xhr).await;+}++#[tokio::test(flavor = "multi_thread")]+async fn worker_fetch_auth_retry_budgets_are_bounded_and_request_local() {+ verify_request_budgets(RequestKind::WorkerFetch).await;+}++#[tokio::test(flavor = "multi_thread")]+async fn worker_xhr_auth_retry_budgets_are_bounded_and_request_local() {+ verify_request_budgets(RequestKind::WorkerXhr).await;+}diff --git a/moli-protocol/src/domains/page/navigation.rs b/moli-protocol/src/domains/page/navigation.rsindex 1292af914..07fc41319 100644--- a/moli-protocol/src/domains/page/navigation.rs+++ b/moli-protocol/src/domains/page/navigation.rs@@ -2824,6 +2824,7 @@ fn start_navigate_to_url_command_with_background_policy_and_request());};pending_fetch_navigation = Some(PendingFetchNavigation {+ auth_retry_budget: Default::default(),fetch_request_id,interception_session_id: preflight.document_fetch_event_session_iddiff --git a/moli-renderer-v8/src/network/loads/registry.rs b/moli-renderer-v8/src/network/loads/registry.rsindex 61d2efd86..0f8c09a38 100644--- a/moli-renderer-v8/src/network/loads/registry.rs+++ b/moli-renderer-v8/src/network/loads/registry.rs@@ -210,6 +210,7 @@ impl ResourceLoadRegistry {browser_runtime,task_runner: self.inner.task_runner.clone(),lifecycle: Mutex::new(ResourceLoadLifecycle {+ auth_retry_budget: moli_fetch::AuthRetryBudget::default(),cancel_handle,consumer_cancel: None,detached_keepalive,@@ -357,6 +358,7 @@ impl ResourceLoadRegistry {}struct ResourceLoadLifecycle {+ auth_retry_budget: moli_fetch::AuthRetryBudget,cancel_handle: Option<FetchCancelHandle>,consumer_cancel: Option<Box<dyn FnOnce() + Send + 'static>>,detached_keepalive: bool,@@ -461,6 +463,15 @@ pub(crate) struct ResourceLoadLease {}impl ResourceLoadLease {+ /// Lease clones and authentication continuations belong to the same request.+ pub(crate) fn try_restart_with_auth(&self) -> bool {+ self.registration+ .lifecycle+ .lock()+ .auth_retry_budget+ .try_restart()+ }+pub(crate) fn id_for_diagnostics(&self) -> u64 {self.registration.id.as_u64()}@@ -637,6 +648,35 @@ mod tests {.expect("active registry should accept loads")}+ #[tokio::test(flavor = "current_thread")]+ async fn authentication_budget_follows_lease_clones_but_not_other_requests() {+ let registry = registry();+ let first = lease(+ ®istry,+ ResourceLoadDisposition::Ordinary,+ FetchCancelHandle::new(),+ );+ let same_request = first.clone();+ let other_request = lease(+ ®istry,+ ResourceLoadDisposition::Ordinary,+ FetchCancelHandle::new(),+ );+ for attempt in 0..31 {+ let current = if attempt % 2 == 0 {+ &first+ } else {+ &same_request+ };+ assert!(current.try_restart_with_auth());+ }+ assert!(!same_request.try_restart_with_auth());+ for _ in 0..256 {+ assert!(!first.try_restart_with_auth());+ }+ assert!(other_request.try_restart_with_auth());+ }+#[tokio::test(flavor = "current_thread")]async fn document_retirement_cancels_ordinary_loads() {let registry = registry();diff --git a/moli-renderer-v8/src/script_vm/subresource_fetch/pending_fetch.rs b/moli-renderer-v8/src/script_vm/subresource_fetch/pending_fetch.rsindex 7fa537aee..5dd2df733 100644--- a/moli-renderer-v8/src/script_vm/subresource_fetch/pending_fetch.rs+++ b/moli-renderer-v8/src/script_vm/subresource_fetch/pending_fetch.rs@@ -519,6 +519,31 @@ impl ScriptVm {.borrow_mut().take_pending_subresource_auth(internal_id).ok_or_else(|| anyhow!("unknown pending subresource auth `{internal_id}`"))?;+ if !pending.pending.load.try_restart_with_auth() {+ // Reuse the terminal auth path to settle Window and Worker consumers.+ self._context_host+ .borrow_mut()+ .record_pending_subresource_auth(pending);+ let execution = self.fail_pending_subresource_auth_body(+ internal_id,+ moli_fetch::AuthRetryBudget::ERROR_TEXT.to_owned(),+ )?;+ let ((), activity, post_checkpoint_event) = execution.into_parts();+ let mut completion = AsyncSubresourceCommandExecution::after_body(+ PendingSubresourceContinueOutcome::Started,+ activity,+ );+ if let Some(event) = post_checkpoint_event {+ completion = completion.with_post_checkpoint_event(event);+ } else {+ // Worker auth failure clears its network record, so its IPC+ // completion does not retire this parent protocol correlation.+ completion = completion.with_post_checkpoint_event(+ PendingSubresourceContinueEvent::Completed { internal_id },+ );+ }+ return Ok(completion);+ }let PendingSubresourceAuthState {pending: pending_fetch,request_url,