From 54b07e5b007fbe91c8174ac90cdd69ca38af80e5 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Sat, 24 Oct 2026 14:51:38 +0800 Subject: [PATCH] fix(fetch): challenge and cache browser Basic authentication --- Cargo.lock | 2 +- moli-fetch/Cargo.toml | 2 +- moli-fetch/src/blocking/mod.rs | 11 +- moli-fetch/src/client.rs | 10 + moli-fetch/src/http_authentication.rs | 676 ++++++++++++++++++ moli-fetch/src/lib.rs | 1 + moli-fetch/src/request.rs | 70 +- moli-fetch/src/runtime.rs | 187 ++++- moli-fetch/src/tests/http_authentication.rs | 442 ++++++++++++ moli-fetch/src/tests/mod.rs | 1 + moli-fetch/src/tests/redirect_auth.rs | 5 +- .../src/script_vm/tests/dom_xhr/xhr.rs | 14 +- .../src/script_vm/tests/http_fixture.rs | 20 +- 13 files changed, 1365 insertions(+), 76 deletions(-) create mode 100644 moli-fetch/src/http_authentication.rs create mode 100644 moli-fetch/src/tests/http_authentication.rs diff --git a/Cargo.lock b/Cargo.lock index 00bf26e1f28..5b5619b73cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2987,7 +2987,7 @@ dependencies = [ "moli-web-bot-auth", "moli-web-mime", "parking_lot", - "percent-encoding 2.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "percent-encoding", "rcgen", "regex", "serde", diff --git a/moli-fetch/Cargo.toml b/moli-fetch/Cargo.toml index 1b5c50cdae2..057bcd3ec62 100644 --- a/moli-fetch/Cargo.toml +++ b/moli-fetch/Cargo.toml @@ -34,7 +34,7 @@ moli-url-policy = { path = "../moli-url-policy" } moli-web-bot-auth = { path = "../moli-web-bot-auth" } moli-web-mime = { path = "../moli-web-mime" } parking_lot = "0.12" -percent-encoding = "2" +percent-encoding = "2.3" regex = "1" serde = { version = "1.0.228", features = ["derive"] } sfv = "0.15.0" diff --git a/moli-fetch/src/blocking/mod.rs b/moli-fetch/src/blocking/mod.rs index e77cda485cd..df75283bc0b 100644 --- a/moli-fetch/src/blocking/mod.rs +++ b/moli-fetch/src/blocking/mod.rs @@ -669,7 +669,14 @@ pub(crate) fn configure_easy( .configure(easy, request.allows_credentials_for_url(request_url))?; easy.useragent(request.browser_identity(config).user_agent()) .context("failed to set curl user-agent")?; - easy.url(request_url.as_str()) + // Browser URL credentials are selected only after an eligible 401. Do + // not let libcurl independently turn userinfo into a preemptive header. + let mut transport_url = request_url.clone(); + if request.request_origin().is_some() { + let _ = transport_url.set_username(""); + let _ = transport_url.set_password(None); + } + easy.url(transport_url.as_str()) .with_context(|| anyhow!("failed to set curl request url to {}", request_url))?; match request.method.as_str() { @@ -766,7 +773,7 @@ pub(crate) fn configure_easy( .context("failed to suppress curl default content-type")?; } - if let Some(auth) = request.auth_for_url(request_url) + if let Some(auth) = request.transport_auth_for_url(request_url) && request.auth_requires_buffered_transport() { let mut methods = Auth::new(); diff --git a/moli-fetch/src/client.rs b/moli-fetch/src/client.rs index 436e945f5fe..e86990c68fd 100644 --- a/moli-fetch/src/client.rs +++ b/moli-fetch/src/client.rs @@ -21,6 +21,7 @@ pub struct FetchClientHandle { cookie_store: SharedBrowserCookieStore, runtime: FetchRuntimeHandle, dictionaries: crate::compression_dictionary::SharedDictionaryStore, + http_authentication: crate::http_authentication::SharedHttpAuthenticationCache, } /// Unique structured-concurrency owner for one fetch client. @@ -41,6 +42,12 @@ impl FetchClientHandle { pub fn prepare_request(&self, request: &mut Request) { request.prepare_accept_header(&self.config); let url = request.url.clone(); + crate::http_authentication::HttpAuthenticationCache::prepare( + &self.http_authentication, + &self.config, + request, + &url, + ); if let std::borrow::Cow::Owned(prepared) = crate::compression_dictionary::DictionaryStore::prepare( &self.dictionaries, @@ -315,17 +322,20 @@ impl FetchClient { let client_hint_preferences = Arc::new(parking_lot::Mutex::new(ClientHintPreferences::default())); let dictionaries = Arc::default(); + let http_authentication = Arc::default(); let runtime_owner = FetchRuntimeOwner::new_with_network_state( config, Arc::clone(&cookie_store), Arc::clone(&client_hint_preferences), Arc::clone(&dictionaries), + Arc::clone(&http_authentication), ); let handle = FetchClientHandle { config: config.clone(), runtime: runtime_owner.handle(), cookie_store, dictionaries, + http_authentication, }; Self { handle, diff --git a/moli-fetch/src/http_authentication.rs b/moli-fetch/src/http_authentication.rs new file mode 100644 index 00000000000..e232a60400d --- /dev/null +++ b/moli-fetch/src/http_authentication.rs @@ -0,0 +1,676 @@ +//! Browser Basic authentication belongs to the network context, not libcurl's +//! URL userinfo handling. URL credentials require a challenge; accepted +//! identities can subsequently be sent within their protection space. + +use std::{collections::VecDeque, fmt, sync::Arc}; + +use moli_header_field::{HeaderFieldTokenMode, HeaderFieldTokenizer, split_outside_quoted_strings}; +use moli_storage_key::{MoliStorageKey, deserialize_serialized_storage_key, site_for_url}; +use moli_url::WebOrigin; +use parking_lot::Mutex; +use url::Url; + +use crate::{FetchConfig, Request, RequestAuth, RequestAuthScheme, RequestAuthTarget, RequestMode}; + +const MAX_ENTRIES: usize = 256; +const MAX_PATHS: usize = 10; + +pub(crate) type SharedHttpAuthenticationCache = Arc>; + +#[derive(Default)] +pub(crate) struct HttpAuthenticationCache { + entries: VecDeque, +} + +#[derive(Clone, PartialEq, Eq)] +struct ProtectionSpace { + origin: url::Origin, + partition: String, +} + +impl ProtectionSpace { + fn new(request: &Request, url: &Url) -> Option { + let origin = request.request_origin()?; + let partition = if let Some(key) = request.network_partition_key() { + if origin.is_opaque() + && deserialize_serialized_storage_key(key)? + .opaque_nonce() + .is_none() + { + return None; + } + key.to_owned() + } else { + if origin.is_opaque() { + return None; + } + let frame = Url::parse(origin.ascii_serialization()).ok()?; + let context = &request.cookie_context.browser_context; + let top_frame = context + .top_frame_origin_url + .as_ref() + .or(context.site_for_cookies_url.as_ref()) + .unwrap_or(&frame); + MoliStorageKey::from_url_and_top_level_site(&frame, site_for_url(top_frame), None) + .serialized_storage_key() + }; + Some(Self { + origin: url.origin(), + partition, + }) + } +} + +struct Identity { + space: ProtectionSpace, + realm: String, + credentials: Arc, +} + +struct Entry { + identity: Arc, + paths: VecDeque, +} + +impl HttpAuthenticationCache { + fn lookup(&mut self, space: &ProtectionSpace, path: &str) -> Option> { + let (index, _) = self + .entries + .iter() + .enumerate() + .filter_map(|(index, entry)| { + if entry.identity.space != *space { + return None; + } + entry + .paths + .iter() + .filter(|prefix| path.starts_with(prefix.as_str())) + .map(String::len) + .max() + .map(|length| (index, length)) + }) + .max_by_key(|(_, length)| *length)?; + let entry = self.entries.remove(index)?; + let identity = Arc::clone(&entry.identity); + self.entries.push_front(entry); + Some(identity) + } + + fn store(&mut self, identity: Arc, path: &str) { + let mut entry = self + .entries + .iter() + .position(|entry| { + entry.identity.space == identity.space && entry.identity.realm == identity.realm + }) + .and_then(|index| self.entries.remove(index)) + .unwrap_or_else(|| Entry { + identity: Arc::clone(&identity), + paths: VecDeque::new(), + }); + entry.identity = identity; + let directory = &path[..path.rfind('/').map_or(0, |index| index + 1)]; + if !entry + .paths + .iter() + .any(|prefix| directory.starts_with(prefix.as_str())) + { + entry.paths.retain(|prefix| !prefix.starts_with(directory)); + entry.paths.push_front(directory.to_owned()); + entry.paths.truncate(MAX_PATHS); + } + self.entries.push_front(entry); + self.entries.truncate(MAX_ENTRIES); + } + + fn reject(&mut self, identity: &Arc) { + // An old in-flight rejection must not evict a newer accepted identity. + self.entries + .retain(|entry| !Arc::ptr_eq(&entry.identity, identity)); + } + + pub(crate) fn prepare_owned( + cache: &SharedHttpAuthenticationCache, + config: &FetchConfig, + mut request: Request, + url: &Url, + ) -> Request { + Self::prepare(cache, config, &mut request, url); + request + } + + pub(crate) fn prepare( + cache: &SharedHttpAuthenticationCache, + config: &FetchConfig, + request: &mut Request, + url: &Url, + ) { + if request.request_origin().is_none() + || request + .configured_auth() + .is_some_and(|auth| auth.target == RequestAuthTarget::Server) + { + request.http_authentication = None; + return; + } + if request + .http_authentication + .as_ref() + .is_some_and(|snapshot| snapshot.matches(cache, config, request, url)) + { + return; + } + let has_authorization = has_authorization(config, request); + let space = ProtectionSpace::new(request, url); + let action = if has_authorization + || !request.allows_credentials_for_url(url) + || !matches!(url.scheme(), "http" | "https") + { + Authentication::None + } else if !url.username().is_empty() || url.password().is_some() { + if matches!( + request.request_mode, + RequestMode::Cors | RequestMode::SameOrigin + ) && request.has_cross_origin_url(url) + { + Authentication::None + } else { + Authentication::Url(Arc::new(RequestAuth { + target: RequestAuthTarget::Server, + scheme: RequestAuthScheme::Basic, + username: percent_encoding::percent_decode_str(url.username()) + .decode_utf8_lossy() + .into_owned(), + password: percent_encoding::percent_decode_str(url.password().unwrap_or("")) + .decode_utf8_lossy() + .into_owned(), + })) + } + } else { + space + .as_ref() + .and_then(|space| cache.lock().lookup(space, url.path())) + .map_or(Authentication::None, Authentication::Cached) + }; + request.http_authentication = Some(Arc::new(PreparedHttpAuthentication { + cache: Arc::clone(cache), + url: url.clone(), + origin: request.request_origin().cloned(), + partition: request.network_partition_key().map(str::to_owned), + top_frame: request + .cookie_context + .browser_context + .top_frame_origin_url + .clone(), + site_for_cookies: request + .cookie_context + .browser_context + .site_for_cookies_url + .clone(), + credentials_mode: request.credentials_mode, + mode: request.request_mode, + cross_origin: request.has_cross_origin_url(url), + has_authorization, + space, + action, + })); + } +} + +#[derive(Clone)] +enum Authentication { + None, + Url(Arc), + Challenged { + realm: String, + credentials: Arc, + }, + Cached(Arc), +} + +#[derive(Clone)] +pub(crate) struct PreparedHttpAuthentication { + cache: SharedHttpAuthenticationCache, + url: Url, + origin: Option, + partition: Option, + top_frame: Option, + site_for_cookies: Option, + credentials_mode: crate::RequestCredentialsMode, + mode: RequestMode, + cross_origin: bool, + has_authorization: bool, + space: Option, + action: Authentication, +} + +impl fmt::Debug for PreparedHttpAuthentication { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("PreparedHttpAuthentication") + } +} + +fn has_authorization(config: &FetchConfig, request: &Request) -> bool { + request + .request_headers + .iter() + .any(|(name, _)| name.eq_ignore_ascii_case("authorization")) + || config + .default_request_headers() + .iter() + .any(|(name, _)| name.eq_ignore_ascii_case("authorization")) +} + +impl PreparedHttpAuthentication { + fn matches( + &self, + cache: &SharedHttpAuthenticationCache, + config: &FetchConfig, + request: &Request, + url: &Url, + ) -> bool { + Arc::ptr_eq(&self.cache, cache) + && self.url == *url + && self.origin.as_ref() == request.request_origin() + && self.partition.as_deref() == request.network_partition_key() + && self.top_frame == request.cookie_context.browser_context.top_frame_origin_url + && self.site_for_cookies == request.cookie_context.browser_context.site_for_cookies_url + && self.credentials_mode == request.credentials_mode + && self.mode == request.request_mode + && self.cross_origin == request.has_cross_origin_url(url) + && self.has_authorization == has_authorization(config, request) + } + + pub(crate) fn credentials(&self) -> Option<&RequestAuth> { + match &self.action { + Authentication::None => None, + Authentication::Url(credentials) | Authentication::Challenged { credentials, .. } => { + Some(credentials) + } + Authentication::Cached(identity) => Some(&identity.credentials), + } + } + + pub(crate) fn preemptive_credentials(&self, url: &Url) -> Option<&RequestAuth> { + if self.url != *url { + return None; + } + match &self.action { + Authentication::Challenged { credentials, .. } => Some(credentials), + Authentication::Cached(identity) => Some(&identity.credentials), + Authentication::None | Authentication::Url(_) => None, + } + } +} + +pub(crate) struct HttpAuthenticationRetry { + pub(crate) prepared: Arc, + pub(crate) headers: Vec<(String, Vec)>, +} + +pub(crate) enum AuthenticationHeaderAction { + Forward, + Defer, + Retry, + Replay(Vec>), +} + +pub(crate) struct HttpAuthenticationResponse { + prepared: Arc, + status: u16, + lines: Vec>, + headers: Vec<(String, Vec)>, + retry: Option, + finalized: bool, +} + +impl HttpAuthenticationResponse { + pub(crate) fn new(request: &Request) -> Option { + let prepared = request.http_authentication.as_ref()?; + prepared.credentials()?; + Some(Self { + prepared: Arc::clone(prepared), + status: 0, + lines: Vec::new(), + headers: Vec::new(), + retry: None, + finalized: false, + }) + } + + pub(crate) fn take_retry(&mut self) -> Option { + self.retry.take() + } + + pub(crate) fn header(&mut self, data: &[u8]) -> AuthenticationHeaderAction { + use AuthenticationHeaderAction::{Defer, Forward, Replay, Retry}; + if let Some(line) = crate::response::parse_http_response_status_line(data) { + self.status = line.status; + self.lines.clear(); + self.headers.clear(); + self.finalized = false; + } + if self.finalized { + return Forward; + } + let defer = self.status == 401 && matches!(self.prepared.action, Authentication::Url(_)); + if defer { + self.lines.push(data.to_vec()); + if let Some(header) = crate::headers::parse_http_response_header_line(data) { + self.headers.push(header); + } + } + if data != b"\r\n" && data != b"\n" { + return if defer { Defer } else { Forward }; + } + self.finalized = true; + match &self.prepared.action { + Authentication::Url(credentials) if defer => { + if let Some(realm) = basic_realm(&self.headers) { + let mut prepared = (*self.prepared).clone(); + prepared.action = Authentication::Challenged { + realm, + credentials: Arc::clone(credentials), + }; + self.retry = Some(HttpAuthenticationRetry { + prepared: Arc::new(prepared), + headers: std::mem::take(&mut self.headers), + }); + Retry + } else { + Replay(std::mem::take(&mut self.lines)) + } + } + Authentication::Challenged { realm, credentials } + if (200..400).contains(&self.status) => + { + if let Some(space) = &self.prepared.space { + self.prepared.cache.lock().store( + Arc::new(Identity { + space: space.clone(), + realm: realm.clone(), + credentials: Arc::clone(credentials), + }), + self.prepared.url.path(), + ); + } + Forward + } + Authentication::Cached(identity) if self.status == 401 => { + self.prepared.cache.lock().reject(identity); + Forward + } + _ => Forward, + } + } +} + +/// A comma can separate challenges or auth-params. Tokenize params using the +/// shared HTTP field parser, preserving quoted commas/escapes and rejecting +/// malformed Basic challenges. Chromium accepts a missing realm as empty. +fn basic_realm(headers: &[(String, Vec)]) -> Option { + for (_, value) in headers + .iter() + .filter(|(name, _)| name.eq_ignore_ascii_case("www-authenticate")) + { + let value: String = value.iter().copied().map(char::from).collect(); + let mut basic = false; + let mut valid = true; + let mut realm = String::new(); + for part in split_outside_quoted_strings(&value, ',') { + let mut parser = HeaderFieldTokenizer::new(part); + let Some(token) = parser.consume_token(HeaderFieldTokenMode::Normal) else { + valid = false; + continue; + }; + if !parser.consume('=') { + if basic && valid { + return Some(realm); + } + basic = token.eq_ignore_ascii_case("Basic"); + valid = true; + realm = String::new(); + if !basic || parser.is_consumed() { + continue; + } + let Some(name) = parser.consume_token(HeaderFieldTokenMode::Normal) else { + valid = false; + continue; + }; + if !parser.consume('=') { + valid = false; + continue; + } + valid &= auth_parameter(&mut parser, name, &mut realm); + } else if basic { + valid &= auth_parameter(&mut parser, token, &mut realm); + } + } + if basic && valid { + return Some(realm); + } + } + None +} + +fn auth_parameter(parser: &mut HeaderFieldTokenizer<'_>, name: &str, realm: &mut String) -> bool { + let Some(value) = parser.consume_token_or_quoted_string(HeaderFieldTokenMode::Normal) else { + return false; + }; + if !parser.is_consumed() + || name.bytes().any(|byte| byte.is_ascii_control()) + || value + .chars() + .any(|character| character.is_ascii_control() && character != '\t') + { + return false; + } + if name.eq_ignore_ascii_case("realm") { + *realm = value.into_owned(); + } + true +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn basic_challenges_use_http_quoted_parameter_grammar() { + for (field, expected) in [ + ("Basic", Some("")), + ("bAsIc realm=plain", Some("plain")), + ( + "Digest realm=other, nonce=abc, Basic realm=\"a,b\\\"c\", charset=UTF-8", + Some("a,b\"c"), + ), + ("Basic realm=old, REALM=new", Some("new")), + ("Basic realm=\"", None), + ("Basic realm=", None), + ("Basic realm=\"ok\"junk", None), + ("Basic realm=\"bad\u{7f}\"", None), + ("Bearer token", None), + ("NotBasic realm=other", None), + ] { + assert_eq!( + basic_realm(&[("www-authenticate".into(), field.as_bytes().to_vec())]).as_deref(), + expected, + "{field:?}" + ); + } + } + + fn identity(partition: &str, origin: &str, realm: &str) -> Arc { + Arc::new(Identity { + space: ProtectionSpace { + origin: Url::parse(origin).unwrap().origin(), + partition: partition.into(), + }, + realm: realm.into(), + credentials: Arc::new(RequestAuth { + target: RequestAuthTarget::Server, + scheme: RequestAuthScheme::Basic, + username: "user".into(), + password: "secret".into(), + }), + }) + } + + #[test] + fn cache_uses_longest_directory_match_without_crossing_origin_or_partition() { + let mut cache = HttpAuthenticationCache::default(); + let broad = identity("a", "https://one.test/", "broad"); + let narrow = identity("a", "https://one.test/", "narrow"); + cache.store(Arc::clone(&broad), "/shared/login"); + cache.store(Arc::clone(&narrow), "/shared/private/login"); + assert!(Arc::ptr_eq( + &cache.lookup(&broad.space, "/shared/private/data").unwrap(), + &narrow + )); + assert!(Arc::ptr_eq( + &cache.lookup(&broad.space, "/shared/public").unwrap(), + &broad + )); + for (partition, origin) in [ + ("b", "https://one.test/"), + ("a", "http://one.test/"), + ("a", "https://two.test/"), + ] { + assert!( + cache + .lookup( + &identity(partition, origin, "").space, + "/shared/private/data" + ) + .is_none() + ); + } + assert!(cache.lookup(&broad.space, "/sharedness/data").is_none()); + assert!(cache.lookup(&broad.space, "/shared").is_none()); + } + + #[test] + fn old_rejection_does_not_remove_new_credentials_and_cache_is_bounded() { + let mut cache = HttpAuthenticationCache::default(); + let old = identity("a", "https://one.test/", "realm"); + let new = identity("a", "https://one.test/", "realm"); + cache.store(Arc::clone(&old), "/login"); + cache.store(Arc::clone(&new), "/other/login"); + cache.reject(&old); + assert!(Arc::ptr_eq( + &cache.lookup(&new.space, "/data").unwrap(), + &new + )); + cache.reject(&new); + assert!(cache.lookup(&new.space, "/data").is_none()); + for index in 0..=MAX_ENTRIES { + cache.store( + identity(&index.to_string(), "https://one.test/", "realm"), + "/login", + ); + } + assert_eq!(cache.entries.len(), MAX_ENTRIES); + assert!( + cache + .lookup(&identity("0", "https://one.test/", "realm").space, "/data") + .is_none() + ); + let value = identity("last", "https://one.test/", "realm"); + for index in 0..MAX_PATHS + 3 { + cache.store(Arc::clone(&value), &format!("/{index}/login")); + } + assert_eq!(cache.entries.front().unwrap().paths.len(), MAX_PATHS); + assert!(cache.lookup(&value.space, "/0/data").is_none()); + cache.store(Arc::clone(&value), "/login"); + assert_eq!(cache.entries.front().unwrap().paths.len(), 1); + assert!(cache.lookup(&value.space, "/any/data").is_some()); + } + + #[test] + fn prepared_authentication_keeps_one_selection_but_revalidates_security_inputs_and_owner() { + let config = FetchConfig::default(); + let cache = SharedHttpAuthenticationCache::default(); + let url = Url::parse("https://one.test/data").unwrap(); + let request = Request::get_with_url(url.clone()).with_request_origin((&url).into()); + let selected = HttpAuthenticationCache::prepare_owned(&cache, &config, request, &url); + let accepted = identity( + ProtectionSpace::new(&selected, &url) + .unwrap() + .partition + .as_str(), + "https://one.test/", + "realm", + ); + cache.lock().store(accepted, "/login"); + let frozen = + HttpAuthenticationCache::prepare_owned(&cache, &config, selected.clone(), &url); + assert!( + frozen.auth().is_none(), + "cache selection must not change between renderer lookup and dispatch" + ); + let mut fresh = selected.clone(); + fresh.http_authentication = None; + let fresh = HttpAuthenticationCache::prepare_owned(&cache, &config, fresh, &url); + assert!(fresh.auth().is_some()); + let other_owner = SharedHttpAuthenticationCache::default(); + assert!( + HttpAuthenticationCache::prepare_owned(&other_owner, &config, fresh.clone(), &url) + .auth() + .is_none() + ); + let mut omitted = fresh.clone(); + omitted.credentials_mode = crate::RequestCredentialsMode::Omit; + assert!( + HttpAuthenticationCache::prepare_owned(&cache, &config, omitted, &url) + .auth() + .is_none() + ); + let mut explicit = fresh; + explicit + .request_headers + .push(("Authorization".into(), b"Bearer author".to_vec())); + assert!( + HttpAuthenticationCache::prepare_owned(&cache, &config, explicit, &url) + .auth() + .is_none() + ); + } + + #[test] + fn opaque_origins_require_a_storage_nonce_for_session_reuse() { + let url = Url::parse("https://one.test/data").unwrap(); + let request = Request::get_with_url(url.clone()).with_request_origin(WebOrigin::Opaque); + assert!(ProtectionSpace::new(&request, &url).is_none()); + for key in [ + "null", + "partition", + "storage-key:v1;origin=null;top-level-site=https://one.test", + ] { + assert!( + ProtectionSpace::new( + &request.clone().with_network_partition_key(Some(key.into())), + &url + ) + .is_none() + ); + } + let key = |nonce| { + MoliStorageKey::new( + "null".into(), + "https://one.test".into(), + Some(moli_storage_key::OpaqueOriginNonce::new(nonce)), + moli_storage_key::StoragePartitionRelation::FirstParty, + ) + .serialized_storage_key() + }; + let first = ProtectionSpace::new( + &request.clone().with_network_partition_key(Some(key(1))), + &url, + ) + .unwrap(); + let second = + ProtectionSpace::new(&request.with_network_partition_key(Some(key(2))), &url).unwrap(); + assert!(first != second); + } +} diff --git a/moli-fetch/src/lib.rs b/moli-fetch/src/lib.rs index 538afb18735..b6a7d850389 100644 --- a/moli-fetch/src/lib.rs +++ b/moli-fetch/src/lib.rs @@ -20,6 +20,7 @@ mod dns; mod error; mod fetch_url_list; mod headers; +mod http_authentication; mod network_fetch_result; mod proxy; mod proxy_connect; diff --git a/moli-fetch/src/request.rs b/moli-fetch/src/request.rs index 24cdd79e048..edf0fbe7cce 100644 --- a/moli-fetch/src/request.rs +++ b/moli-fetch/src/request.rs @@ -104,7 +104,9 @@ pub struct Request { pub credentials_mode: RequestCredentialsMode, pub(crate) redirect_chain: Vec, network_partition_key: Option, - auth: Option, + auth: Option>, + pub(crate) http_authentication: + Option>, pub cookie_context: NetworkCookieRequestContext, timeout_policy: RequestTimeoutPolicy, network_observation_recorder: Option, @@ -139,12 +141,6 @@ impl RequestRedirectPolicy { } } -#[derive(Debug, Clone)] -struct ScopedRequestAuth { - credentials: RequestAuth, - origin: WebOrigin, -} - /// The lifetime of request headers supplied by an interception command. #[derive(Debug, Clone, PartialEq, Eq)] pub enum RequestHeaderOverride { @@ -603,6 +599,14 @@ pub struct RequestAuth { pub password: String, } +// Credentials and their original server are immutable request configuration. +// Keep them together and out of the inline Request/async-future footprint. +#[derive(Debug)] +struct ConfiguredRequestAuth { + value: RequestAuth, + origin: url::Origin, +} + impl RequestAuth { fn can_use_header_transport(&self) -> bool { matches!( @@ -669,6 +673,7 @@ impl Request { redirect_chain: Vec::new(), network_partition_key: None, auth: None, + http_authentication: None, cookie_context: NetworkCookieRequestContext::top_level_navigation("GET"), timeout_policy: RequestTimeoutPolicy::default(), network_observation_recorder: None, @@ -714,6 +719,7 @@ impl Request { redirect_chain: Vec::new(), network_partition_key: None, auth: None, + http_authentication: None, cookie_context: NetworkCookieRequestContext::top_level_navigation("GET"), timeout_policy: RequestTimeoutPolicy::default(), network_observation_recorder: None, @@ -792,6 +798,7 @@ impl Request { redirect_chain: Vec::new(), network_partition_key: None, auth: None, + http_authentication: None, cookie_context: NetworkCookieRequestContext::subresource(method), timeout_policy: RequestTimeoutPolicy::default(), network_observation_recorder: None, @@ -1666,35 +1673,52 @@ impl Request { } pub fn auth(&self) -> Option<&RequestAuth> { - self.auth.as_ref().map(|auth| &auth.credentials) + self.configured_auth() + .or_else(|| self.http_authentication.as_ref()?.credentials()) } - pub(crate) fn auth_for_url(&self, request_url: &Url) -> Option<&RequestAuth> { - let auth = self.auth.as_ref()?; - (auth.credentials.target != RequestAuthTarget::Server - || auth.origin.same_origin(&WebOrigin::from_url(request_url))) - .then_some(&auth.credentials) + pub(crate) fn configured_auth(&self) -> Option<&RequestAuth> { + self.auth.as_ref().map(|auth| &auth.value) + } + + pub(crate) fn transport_auth_for_url(&self, url: &Url) -> Option<&RequestAuth> { + let configured = self.auth.as_ref()?; + let auth = &configured.value; + (auth.target != RequestAuthTarget::Server + || (self.allows_credentials_for_url(url) && configured.origin == url.origin())) + .then_some(auth) } pub fn auth_requires_buffered_transport(&self) -> bool { - self.auth() + self.configured_auth() .is_some_and(|auth| !auth.can_use_header_transport()) } pub fn preemptive_server_basic_auth_for_url(&self, request_url: &Url) -> Option<(&str, &str)> { - let auth = self.auth_for_url(request_url)?; - (auth.target == RequestAuthTarget::Server && auth.scheme == RequestAuthScheme::Basic) - .then_some((auth.username.as_str(), auth.password.as_str())) + let auth = self + .transport_auth_for_url(request_url) + .filter(|auth| { + auth.target == RequestAuthTarget::Server && auth.scheme == RequestAuthScheme::Basic + }) + .or_else(|| { + self.http_authentication + .as_ref()? + .preemptive_credentials(request_url) + })?; + (auth.target == RequestAuthTarget::Server + && auth.scheme == RequestAuthScheme::Basic + && self.allows_credentials_for_url(request_url)) + .then_some((auth.username.as_str(), auth.password.as_str())) } pub fn set_auth(&mut self, auth: Option) { - // Pin credentials to where they were supplied, rather than the mutable - // current URL. A later CDP auth response grants new credentials only to - // that challenged origin, without authorizing subsequent redirects. - self.auth = auth.map(|credentials| ScopedRequestAuth { - credentials, - origin: WebOrigin::from_url(&self.url), + self.auth = auth.map(|value| { + Arc::new(ConfiguredRequestAuth { + value, + origin: self.url.origin(), + }) }); + self.http_authentication = None; } pub fn with_auth(mut self, auth: RequestAuth) -> Self { diff --git a/moli-fetch/src/runtime.rs b/moli-fetch/src/runtime.rs index ca8375f6177..65eb516c67a 100644 --- a/moli-fetch/src/runtime.rs +++ b/moli-fetch/src/runtime.rs @@ -70,12 +70,16 @@ const CURLINFO_HTTP_VERSION: curl_sys::CURLINFO = curl_sys::CURLINFO_LONG + 46; static NEXT_FETCH_RUNTIME_ID: AtomicU64 = AtomicU64::new(0); static INSTALL_FETCH_RUNTIME_PANIC_HOOK: Once = Once::new(); -fn curl_runtime_deadline(request: &Request, config: &FetchConfig) -> Option { +fn curl_runtime_deadline( + request: &Request, + config: &FetchConfig, + started_at: Instant, +) -> Option { let timeout = request.effective_request_timeout(config); if timeout.is_zero() { None } else { - Instant::now().checked_add(timeout) + started_at.checked_add(timeout) } } @@ -377,6 +381,7 @@ impl FetchRuntimeOwner { crate::client_hints::ClientHintPreferences::default(), )), Arc::default(), + Arc::default(), ) } @@ -385,6 +390,7 @@ impl FetchRuntimeOwner { cookie_store: SharedBrowserCookieStore, client_hint_preferences: SharedClientHintPreferences, dictionaries: crate::compression_dictionary::SharedDictionaryStore, + http_authentication: crate::http_authentication::SharedHttpAuthenticationCache, ) -> Self { let runtime_id = NEXT_FETCH_RUNTIME_ID .fetch_add(1, Ordering::Relaxed) @@ -404,6 +410,7 @@ impl FetchRuntimeOwner { cookie_store, client_hint_preferences, dictionaries, + http_authentication, request_rx, curl_runtime, curl_http, @@ -715,6 +722,7 @@ struct RuntimeOwner { cookie_store: SharedBrowserCookieStore, client_hint_preferences: SharedClientHintPreferences, dictionaries: crate::compression_dictionary::SharedDictionaryStore, + http_authentication: crate::http_authentication::SharedHttpAuthenticationCache, request_rx: Receiver, curl_runtime: CurlMultiRuntime, curl_http: CurlHttpSender, @@ -738,6 +746,12 @@ impl RuntimeOwner { request, url, ); + prepared.request = crate::http_authentication::HttpAuthenticationCache::prepare_owned( + &self.http_authentication, + &self.config, + prepared.request, + url, + ); prepared.request = crate::compression_dictionary::DictionaryStore::prepare_owned( &self.dictionaries, &self.config, @@ -1047,7 +1061,11 @@ impl RuntimeOwner { let curl_job = CurlMultiJob { easy, origin: context.job.origin_key.clone(), - deadline: curl_runtime_deadline(&context.job.request, &self.config), + deadline: curl_runtime_deadline( + &context.job.request, + &self.config, + context.job.hop.started_at, + ), dns_resolution, priority: request_fetch_priority_rank(&context.job.request), label, @@ -1285,7 +1303,11 @@ impl RuntimeOwner { let curl_job = CurlMultiJob { easy, origin: context.job.origin_key.clone(), - deadline: curl_runtime_deadline(&context.job.request, &self.config), + deadline: curl_runtime_deadline( + &context.job.request, + &self.config, + context.job.hop.started_at, + ), dns_resolution, priority: request_fetch_priority_rank(&context.job.request), label, @@ -1537,7 +1559,11 @@ impl RuntimeOwner { let curl_job = CurlMultiJob { easy, origin: context.job.origin_key.clone(), - deadline: curl_runtime_deadline(&context.job.request, &self.config), + deadline: curl_runtime_deadline( + &context.job.request, + &self.config, + context.job.hop.started_at, + ), dns_resolution, priority: request_fetch_priority_rank(&context.job.request), label, @@ -1579,7 +1605,54 @@ impl RuntimeOwner { .as_ref() .and_then(moli_curl::CurlConnectionTiming::from_easy), ); - if let Some(easy) = completion.easy.as_mut() { + let authentication_retry = if !self.shutdown_requested.load(Ordering::SeqCst) + && !completion.context.is_cancelled() + { + completion + .easy + .as_mut() + .and_then(|easy| easy.get_mut().http_authentication.as_mut()?.take_retry()) + } else { + None + }; + if let Some(retry) = authentication_retry { + let (request, url, cookie_context) = completion.context.request_context(); + let cookies = if request.allows_credentials_for_url(url) { + store_response_cookies(&self.cookie_store, url, &retry.headers, cookie_context) + } else { + Ok(Vec::new()) + }; + match cookies { + Ok(_) => { + state.active_transfers = state.active_transfers.saturating_sub(1); + // No collector received this private 401. Keep the same + // logical request, body, cancellation and uncommitted stream + // channels; this is an authentication retry, not a redirect. + match completion.context { + ActiveTransferContext::Buffered(mut context) => { + context.job.request.http_authentication = Some(retry.prepared); + self.start_job_or_reply(state, context.job); + } + ActiveTransferContext::Streaming(mut context) => { + context.job.request.http_authentication = Some(retry.prepared); + context.job.easy = completion.easy; + self.start_streaming_job_or_reply(state, context.job); + } + ActiveTransferContext::StreamingRaw(mut context) => { + context.job.request.http_authentication = Some(retry.prepared); + context.job.easy = completion.easy; + self.start_raw_streaming_job_or_reply(state, context.job); + } + } + return; + } + Err(error) => completion.result = Err(error), + } + } + if completion.context.is_cancelled() { + completion.result = Err(anyhow::Error::new(crate::FetchCancelled) + .context("fetch runtime request cancelled")); + } else if let Some(easy) = completion.easy.as_mut() { if let Some(error) = easy.get_mut().content_decoding_error.take() { completion.result = Err(anyhow!(error)); } else if completion.result.is_ok() @@ -2940,6 +3013,26 @@ enum ActiveTransferContext { } impl ActiveTransferContext { + fn request_context(&self) -> (&Request, &Url, &NetworkCookieRequestContext) { + match self { + Self::Buffered(context) => ( + &context.job.request, + &context.job.current_url, + &context.job.current_cookie_context, + ), + Self::Streaming(context) => ( + &context.job.request, + &context.job.current_url, + &context.job.current_cookie_context, + ), + Self::StreamingRaw(context) => ( + &context.job.request, + &context.job.current_url, + &context.job.current_cookie_context, + ), + } + } + fn response_progress(&self) -> &ResponseProgress { match self { Self::Buffered(context) => &context.job.response_progress, @@ -3020,6 +3113,7 @@ pub(crate) struct FetchTransferHandler { upload_observer: Option, proxy_connect_response_recorder: ProxyConnectResponseRecorder, dictionary_response: Option, + http_authentication: Option, content_decoding: Option, content_decoding_error: Option, } @@ -3074,6 +3168,7 @@ impl FetchTransferHandler { upload_observer: None, proxy_connect_response_recorder: ProxyConnectResponseRecorder::default(), dictionary_response: None, + http_authentication: None, content_decoding: None, content_decoding_error: None, } @@ -3135,6 +3230,8 @@ impl FetchTransferHandler { } pub(crate) fn begin_dictionary_response(&mut self, request: &Request, url: &Url) { + self.http_authentication = + crate::http_authentication::HttpAuthenticationResponse::new(request); self.dictionary_response = request.compression_dictionary_store.as_ref().map(|store| { crate::compression_dictionary::DictionaryResponse::new( Arc::clone(store), @@ -3146,6 +3243,41 @@ impl FetchTransferHandler { self.content_decoding_error = None; } + fn forward_response_header(&mut self, data: &[u8]) -> bool { + if crate::response::parse_http_response_status_line(data) + .is_some_and(|line| line.status >= 200) + { + self.content_decoding_error = None; + } + if let Some(response) = self.dictionary_response.as_mut() { + response.header(data); + } + if let Some(decoding) = self.content_decoding.as_mut() { + if let Err(error) = decoding.header(data) { + self.content_decoding_error = Some(error.to_string()); + } else if decoding.needs_readable_dictionary_response() + && self + .dictionary_response + .as_ref() + .is_none_or(|response| !response.readable()) + { + self.content_decoding_error = + Some("dictionary response failed its CORS readability check".to_owned()); + } + } + if crate::response::parse_http_response_status_line(data) + .is_some_and(|line| line.status >= 200) + && let Some(timing) = self.response_progress() + { + timing.reset_response_body_sizes(); + } + match &mut self.response { + FetchResponseCollector::Buffered(collector) => collector.header(data), + FetchResponseCollector::Streaming(collector) => collector.header(data), + FetchResponseCollector::StreamingRaw(collector) => collector.header(data), + } + } + fn finish_dictionary_response(&mut self) -> Result<()> { if let Some(decoding) = self.content_decoding.as_mut() { decoding.finish()?; @@ -3273,40 +3405,21 @@ impl Handler for FetchTransferHandler { } fn header(&mut self, data: &[u8]) -> bool { - if crate::response::parse_http_response_status_line(data) - .is_some_and(|line| line.status >= 200) - { - self.content_decoding_error = None; - } - if let Some(response) = self.dictionary_response.as_mut() { - response.header(data); - } - if let Some(decoding) = self.content_decoding.as_mut() { - if let Err(error) = decoding.header(data) { - self.content_decoding_error = Some(error.to_string()); - } else if decoding.needs_readable_dictionary_response() - && self - .dictionary_response - .as_ref() - .is_none_or(|response| !response.readable()) - { - self.content_decoding_error = - Some("dictionary response failed its CORS readability check".to_owned()); - } - } - if crate::response::parse_http_response_status_line(data) - .is_some_and(|line| line.status >= 200) - && let Some(timing) = self.response_progress() - { - timing.reset_response_body_sizes(); - } if let Some(recorder) = self.network_observation_recorder.as_ref() { recorder.record_response_header_line(data); } - match &mut self.response { - FetchResponseCollector::Buffered(collector) => collector.header(data), - FetchResponseCollector::Streaming(collector) => collector.header(data), - FetchResponseCollector::StreamingRaw(collector) => collector.header(data), + use crate::http_authentication::AuthenticationHeaderAction; + match self + .http_authentication + .as_mut() + .map(|response| response.header(data)) + { + Some(AuthenticationHeaderAction::Defer) => true, + Some(AuthenticationHeaderAction::Retry) => false, + Some(AuthenticationHeaderAction::Replay(lines)) => { + lines.iter().all(|line| self.forward_response_header(line)) + } + Some(AuthenticationHeaderAction::Forward) | None => self.forward_response_header(data), } } diff --git a/moli-fetch/src/tests/http_authentication.rs b/moli-fetch/src/tests/http_authentication.rs new file mode 100644 index 00000000000..9364b7d22bc --- /dev/null +++ b/moli-fetch/src/tests/http_authentication.rs @@ -0,0 +1,442 @@ +use super::*; + +fn browser_request(url: &str) -> Result { + let origin: moli_url::WebOrigin = (&Url::parse(url)?).into(); + Ok(Request::get(url)?.with_request_origin(origin)) +} + +fn credentials(url: &str) -> String { + let mut url = Url::parse(url).unwrap(); + url.set_username("us%65r").unwrap(); + url.set_password(Some("p%2Bss%3Aword")).unwrap(); + url.into() +} + +fn header<'a>(request: &'a str, name: &str) -> Option<&'a str> { + request + .lines() + .filter_map(|line| line.split_once(':')) + .find(|(field, _)| field.eq_ignore_ascii_case(name)) + .map(|(_, value)| value.trim()) +} + +fn challenge() -> ScriptedResponse { + ScriptedResponse::status(401, "Unauthorized") + .with_header("WWW-Authenticate", "Basic realm=\"session\"") + .with_header("Cache-Control", "no-store") +} + +async fn body( + client: &FetchClientHandle, + request: Request, + transport: &str, +) -> Result<(u16, Vec)> { + let response = match transport { + "buffered" => { + let response = client.fetch(request.with_single_hop()).await?; + return Ok((response.status, response.body_text().as_bytes().to_vec())); + } + "html" => { + let response = client + .fetch_html_stream(request) + .await? + .into_materialized_text_response() + .await?; + return Ok((response.status, response.body_text().as_bytes().to_vec())); + } + "raw" => client.fetch_raw(request).await?, + _ => unreachable!(), + }; + Ok((response.status, response.body_bytes().to_vec())) +} + +#[tokio::test] +async fn url_basic_authentication_challenges_then_reuses_session_on_all_transports() -> Result<()> { + use base64::Engine; + let expected = format!( + "Basic {}", + base64::engine::general_purpose::STANDARD.encode("user:p+ss:word") + ); + for transport in ["buffered", "html", "raw"] { + // The private challenge body exceeds the final response limit; it is + // retired at headers without entering the response collector. + let server = ScriptedHttpServer::spawn(vec![ + challenge() + .with_body(&"x".repeat(1024)) + .with_header("Set-Cookie", "challenge=seen; Path=/"), + ScriptedResponse::ok("accepted"), + ScriptedResponse::ok("reused"), + ScriptedResponse::ok("explicit"), + ]); + let mut config = FetchConfig::default(); + config.set_connection_limits(None, None, Some(64)); + let client = FetchClient::new(&config, new_shared_browser_cookie_store()); + let first = browser_request(&credentials(&server.url()))?; + assert_eq!( + body(&client, first, transport).await?, + (200, b"accepted".to_vec()) + ); + assert_eq!( + body(&client, browser_request(&server.url())?, transport).await?, + (200, b"reused".to_vec()) + ); + let mut explicit = browser_request(&credentials(&server.url()))?; + explicit.credentials_mode = RequestCredentialsMode::Omit; + explicit + .request_headers + .push(("Authorization".into(), b"Bearer author".to_vec())); + assert_eq!( + body(&client, explicit, transport).await?, + (200, b"explicit".to_vec()) + ); + let requests = server.requests(); + assert_eq!(requests.len(), 4, "{transport}"); + assert_eq!(header(&requests[0], "Authorization"), None); + assert_eq!( + header(&requests[1], "Authorization"), + Some(expected.as_str()) + ); + assert_eq!(header(&requests[1], "Cookie"), Some("challenge=seen")); + assert_eq!( + header(&requests[2], "Authorization"), + Some(expected.as_str()) + ); + assert_eq!(header(&requests[3], "Authorization"), Some("Bearer author")); + assert_eq!(header(&requests[3], "Cookie"), None); + server.shutdown(); + } + Ok(()) +} + +#[tokio::test] +async fn browser_url_credentials_respect_cors_and_credentials_mode() -> Result<()> { + for (mode, credentials_mode, cross_origin) in [ + (RequestMode::Cors, RequestCredentialsMode::Include, true), + (RequestMode::Cors, RequestCredentialsMode::Omit, false), + ( + RequestMode::NoCors, + RequestCredentialsMode::SameOrigin, + true, + ), + ] { + let server = ScriptedHttpServer::spawn(vec![challenge().with_body("final 401")]); + let mut request = browser_request(&credentials(&server.url()))?.with_request_mode(mode); + request.credentials_mode = credentials_mode; + if cross_origin { + request = request.with_request_origin((&Url::parse("https://other.test/")?).into()); + } + let client = FetchClient::new(&FetchConfig::default(), new_shared_browser_cookie_store()); + let response = client.fetch_raw(request).await?; + assert_eq!(response.status, 401); + assert_eq!(response.body_bytes(), b"final 401"); + assert_eq!(server.hits(), 1); + assert_eq!(header(&server.requests()[0], "Authorization"), None); + server.shutdown(); + } + Ok(()) +} + +#[tokio::test] +async fn basic_authentication_cache_is_partitioned_and_owned_by_the_client() -> Result<()> { + let server = ScriptedHttpServer::spawn(vec![ + challenge(), + ScriptedResponse::ok("accepted"), + ScriptedResponse::ok("cached"), + challenge(), + challenge(), + challenge(), + ]); + let cookies = new_shared_browser_cookie_store(); + let client = FetchClient::new(&FetchConfig::default(), Arc::clone(&cookies)); + let request = |url: &str, partition: &str| -> Result { + Ok(browser_request(url)?.with_network_partition_key(Some(partition.into()))) + }; + assert_eq!( + client + .fetch_raw(request(&credentials(&server.url()), "partition-a")?) + .await? + .status, + 200 + ); + assert_eq!( + client + .fetch_raw(request(&server.url(), "partition-a")?) + .await? + .status, + 200 + ); + assert_eq!( + client + .fetch_raw(request(&server.url(), "partition-b")?) + .await? + .status, + 401 + ); + let mut omitted = request(&server.url(), "partition-a")?; + omitted.credentials_mode = RequestCredentialsMode::Omit; + assert_eq!(client.fetch_raw(omitted).await?.status, 401); + let other = FetchClient::new(&FetchConfig::default(), cookies); + assert_eq!( + other + .fetch_raw(request(&server.url(), "partition-a")?) + .await? + .status, + 401 + ); + let requests = server.requests(); + assert_eq!(requests.len(), 6); + for request in &requests[3..] { + assert_eq!(header(request, "Authorization"), None); + } + server.shutdown(); + Ok(()) +} + +#[tokio::test] +async fn rejected_basic_credentials_stop_after_one_retry_and_evict_cached_identity() -> Result<()> { + let server = ScriptedHttpServer::spawn(vec![ + challenge(), + challenge().with_body("rejected"), + challenge(), + challenge(), + ScriptedResponse::ok("accepted"), + challenge().with_body("expired"), + challenge(), + ]); + let client = FetchClient::new(&FetchConfig::default(), new_shared_browser_cookie_store()); + assert_eq!( + client + .fetch_raw(browser_request(&credentials(&server.url()))?) + .await? + .body_bytes(), + b"rejected" + ); + assert_eq!( + client + .fetch_raw(browser_request(&server.url())?) + .await? + .status, + 401 + ); + assert_eq!( + client + .fetch_raw(browser_request(&credentials(&server.url()))?) + .await? + .status, + 200 + ); + assert_eq!( + client + .fetch_raw(browser_request(&server.url())?) + .await? + .body_bytes(), + b"expired" + ); + assert_eq!( + client + .fetch_raw(browser_request(&server.url())?) + .await? + .status, + 401 + ); + let requests = server.requests(); + assert_eq!(requests.len(), 7); + for index in [0, 2, 3, 6] { + assert_eq!(header(&requests[index], "Authorization"), None); + } + for index in [1, 4, 5] { + assert!(header(&requests[index], "Authorization").is_some()); + } + server.shutdown(); + Ok(()) +} + +#[tokio::test] +async fn unsupported_or_malformed_authentication_challenges_preserve_final_response() -> Result<()> +{ + for challenge in [ + "Digest realm=\"other\", nonce=\"n\"", + "Basic realm=\"unterminated", + "Basic realm=", + ] { + let server = ScriptedHttpServer::spawn(vec![ + ScriptedResponse::status(401, "Unauthorized") + .with_header("WWW-Authenticate", challenge) + .with_header("X-Final", "retained") + .with_body("final body"), + ]); + let client = FetchClient::new(&FetchConfig::default(), new_shared_browser_cookie_store()); + let response = client + .fetch_raw(browser_request(&credentials(&server.url()))?) + .await?; + assert_eq!(response.status, 401); + assert_eq!(response.body_bytes(), b"final body"); + assert!( + response + .headers + .iter() + .any(|(name, value)| name == "x-final" && value == b"retained") + ); + assert_eq!(server.hits(), 1); + server.shutdown(); + } + Ok(()) +} + +#[tokio::test] +async fn cached_and_configured_basic_credentials_do_not_cross_redirect_origins() -> Result<()> { + for configured in [false, true] { + let target = ScriptedHttpServer::spawn(vec![challenge().with_body("target challenge")]); + let redirect = + ScriptedResponse::status(302, "Found").with_header("Location", &target.url()); + let responses = if configured { + vec![redirect] + } else { + vec![challenge(), ScriptedResponse::ok("accepted"), redirect] + }; + let source = ScriptedHttpServer::spawn(responses); + let client = FetchClient::new(&FetchConfig::default(), new_shared_browser_cookie_store()); + if !configured { + assert_eq!( + client + .fetch_raw(browser_request(&credentials(&source.url()))?) + .await? + .status, + 200 + ); + } + let mut request = browser_request(&source.url())?.with_request_mode(RequestMode::NoCors); + if configured { + request = request.with_auth(RequestAuth { + target: RequestAuthTarget::Server, + scheme: RequestAuthScheme::Basic, + username: "explicit".into(), + password: "secret".into(), + }); + } + let response = client.fetch_raw(request).await?; + assert_eq!(response.status, 401); + assert_eq!(response.body_bytes(), b"target challenge"); + assert!(response.redirected); + assert_eq!(response.redirect_chain.len(), 1); + assert_eq!(target.hits(), 1); + assert_eq!(header(&target.requests()[0], "Authorization"), None); + assert!(header(source.requests().last().unwrap(), "Authorization").is_some()); + source.shutdown(); + target.shutdown(); + } + Ok(()) +} + +#[tokio::test] +async fn basic_authentication_retry_retains_original_timeout_budget() -> Result<()> { + let server = ScriptedHttpServer::spawn(vec![ + challenge().with_delay_ms(150), + ScriptedResponse::ok("late").with_delay_ms(250), + ]); + let mut config = FetchConfig::default(); + config.set_request_timeout_ms(300); + let client = FetchClient::new(&config, new_shared_browser_cookie_store()); + let error = client + .fetch_raw(browser_request(&credentials(&server.url()))?) + .await + .unwrap_err(); + assert!( + error + .downcast_ref::() + .is_some_and(curl::Error::is_operation_timedout), + "{error:#}" + ); + assert_eq!(server.hits(), 2); + server.shutdown(); + Ok(()) +} + +async fn read_upload(stream: &mut tokio::net::TcpStream) -> Result { + let mut request = Vec::new(); + let mut byte = [0]; + while !request.ends_with(b"\r\n\r\n") { + anyhow::ensure!( + request.len() < 65536 && stream.read(&mut byte).await? == 1, + "incomplete request" + ); + request.push(byte[0]); + } + let length = header(std::str::from_utf8(&request)?, "Content-Length") + .unwrap_or("0") + .parse::()?; + let start = request.len(); + request.resize(start + length, 0); + stream.read_exact(&mut request[start..]).await?; + Ok(String::from_utf8(request)?) +} + +#[tokio::test] +async fn basic_authentication_replays_upload_and_keeps_final_body_streaming_and_cancellable() +-> Result<()> { + for cancel_final_body in [false, true] { + let listener = TcpListener::bind("127.0.0.1:0").await?; + let url = format!("http://{}/upload", listener.local_addr()?); + let (release_tx, release_rx) = oneshot::channel(); + let server = tokio::spawn(async move { + let (mut stream, _) = listener.accept().await?; + let first = read_upload(&mut stream).await?; + stream.write_all(b"HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate: Basic\r\nContent-Length: 1000000\r\nConnection: close\r\n\r\n").await?; + drop(stream); + let (mut stream, _) = listener.accept().await?; + let second = read_upload(&mut stream).await?; + stream + .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 6\r\nConnection: close\r\n\r\none") + .await?; + release_rx.await?; + let _ = stream.write_all(b"two").await; + Ok::<_, anyhow::Error>((first, second)) + }); + let client = FetchClient::new(&FetchConfig::default(), new_shared_browser_cookie_store()); + let mut request = browser_request(&credentials(&url))?; + request.method = "POST".into(); + request.body = Some(bytes::Bytes::from_static(b"payload")); + let cancel = FetchCancelHandle::new(); + let result = tokio::time::timeout( + Duration::from_secs(5), + client.fetch_raw_stream_with_cancel_and_network_metadata(request, cancel.clone()), + ) + .await??; + let (mut response, journal) = result.into_parts_with_observation_journal(); + assert_eq!(response.status, 200); + assert!(!response.redirected); + assert!(response.redirect_chain.is_empty()); + assert_eq!( + journal + .exchanges() + .iter() + .map(|exchange| exchange.response().unwrap().status()) + .collect::>(), + [401, 200] + ); + assert_eq!( + tokio::time::timeout(Duration::from_secs(5), response.next_chunk()).await?, + Some(b"one".to_vec()) + ); + assert!(!cancel.response_completion_is_committed()); + if cancel_final_body { + assert!(cancel.cancel_if_response_pending()); + } + release_tx.send(()).unwrap(); + if cancel_final_body { + let error = response.finish().await.unwrap_err(); + assert!(crate::is_fetch_cancelled(&error), "{error:#}"); + } else { + assert_eq!(response.next_chunk().await, Some(b"two".to_vec())); + response.finish().await?; + } + let (first, second) = tokio::time::timeout(Duration::from_secs(5), server).await???; + for request in [&first, &second] { + assert!(request.starts_with("POST /upload HTTP/1.1\r\n")); + assert!(request.ends_with("\r\n\r\npayload")); + } + assert_eq!(header(&first, "Authorization"), None); + assert!(header(&second, "Authorization").is_some()); + } + Ok(()) +} diff --git a/moli-fetch/src/tests/mod.rs b/moli-fetch/src/tests/mod.rs index c7d88a755cd..d7efccbcdfe 100644 --- a/moli-fetch/src/tests/mod.rs +++ b/moli-fetch/src/tests/mod.rs @@ -11,6 +11,7 @@ mod corp; mod fetch_metadata; mod header_bytes; mod header_eof; +mod http_authentication; mod mixed_transport; mod redirect_auth; mod redirect_policy; diff --git a/moli-fetch/src/tests/redirect_auth.rs b/moli-fetch/src/tests/redirect_auth.rs index 4589cf00685..2eac6004e94 100644 --- a/moli-fetch/src/tests/redirect_auth.rs +++ b/moli-fetch/src/tests/redirect_auth.rs @@ -264,7 +264,10 @@ fn redirect_authorization_proxy_credentials_keep_their_independent_scope() -> Re target, ..server_auth(RequestAuthScheme::Basic) }); - assert_eq!(request.auth_for_url(&foreign).unwrap().target, target); + assert_eq!( + request.transport_auth_for_url(&foreign).unwrap().target, + target + ); assert!( request .preemptive_server_basic_auth_for_url(&foreign) 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 c29c2dc55eb..3619d963679 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 @@ -85,7 +85,13 @@ async fn same_origin_window_fetch_and_xhr_post_send_origin_on_wire() { #[tokio::test(flavor = "current_thread")] async fn window_xhr_open_freezes_base_url_and_applies_url_credentials() { - let server = StaticHttpServer::spawn(1).await; + let server = StaticHttpServer::spawn_with_raw_responder(2, |index, _| { + if index == 0 { + "HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate: Basic realm=\"xhr\"\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".to_owned() + } else { + "HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".to_owned() + } + }).await; let base_url = server.base_url(); let loader = static_http_loader(std::iter::empty::()); let mut vm = new_page_task_executor_test_vm_with_loader( @@ -129,10 +135,12 @@ async fn window_xhr_open_freezes_base_url_and_applies_url_credentials() { .await; let requests = server.finish().await; - assert_eq!(requests.len(), 1); + assert_eq!(requests.len(), 2); assert_eq!(requests[0].target, "/first/resource"); + assert_eq!(requests[0].header_value("authorization"), None); + assert_eq!(requests[1].target, "/first/resource"); assert_eq!( - requests[0].header_value("authorization"), + requests[1].header_value("authorization"), Some("Basic YWxpY2U6c2VjcmV0") ); } diff --git a/moli-renderer-v8/src/script_vm/tests/http_fixture.rs b/moli-renderer-v8/src/script_vm/tests/http_fixture.rs index 46d676bf2d1..db914875545 100644 --- a/moli-renderer-v8/src/script_vm/tests/http_fixture.rs +++ b/moli-renderer-v8/src/script_vm/tests/http_fixture.rs @@ -63,6 +63,16 @@ impl StaticHttpServer { expected_requests: usize, response_headers: &'static str, response_body: impl Fn(usize, &CapturedHttpRequest) -> String + Send + 'static, + ) -> Self { + Self::spawn_with_raw_responder(expected_requests, move |index, request| { + let body = response_body(index, request); + format!("HTTP/1.1 200 OK\r\n{response_headers}Content-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()) + }).await + } + + pub(super) async fn spawn_with_raw_responder( + expected_requests: usize, + response: impl Fn(usize, &CapturedHttpRequest) -> String + Send + 'static, ) -> Self { let listener = tokio::net::TcpListener::bind("127.0.0.1:0") .await @@ -147,17 +157,11 @@ impl StaticHttpServer { headers, body: request[header_end..header_end + content_length].to_vec(), }; - let body = response_body(index, &captured); + let response = response(index, &captured); requests.push(captured); socket - .write_all( - format!( - "HTTP/1.1 200 OK\r\n{response_headers}Content-Length: {}\r\nConnection: close\r\n\r\n{body}", - body.len() - ) - .as_bytes(), - ) + .write_all(response.as_bytes()) .await .expect("write static HTTP test response"); }