From 323ed738fc4067392c4bdea2b26712b23e8e1ace Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Thu, 5 Nov 2026 14:02:17 +0100 Subject: [PATCH] fix(tiles): use browser crypto for service login bindings --- crates/tiles/Cargo.toml | 4 ++ crates/tiles/src/logins.rs | 115 ++++++++++++++++++++++--------------- docs/architecture.md | 7 +++ docs/user-guide/data.md | 5 ++ 4 files changed, 86 insertions(+), 45 deletions(-) diff --git a/crates/tiles/Cargo.toml b/crates/tiles/Cargo.toml index 1572d02f06..67692bc92e 100644 --- a/crates/tiles/Cargo.toml +++ b/crates/tiles/Cargo.toml @@ -35,6 +35,10 @@ base64 = { workspace = true } ureq = { version = "3", default-features = false, features = ["rustls", "gzip", "platform-verifier"] } getrandom = "0.4" +# Browser and worker login bindings use Web Crypto, never a uniqueness-only fallback. +[target.'cfg(target_arch = "wasm32")'.dependencies] +getrandom = { version = "0.4", features = ["wasm_js"] } + [dev-dependencies] # Tests write Brotli-compressed PMTiles archives. brotli = { version = "~9.0.0", default-features = false, features = ["std"] } diff --git a/crates/tiles/src/logins.rs b/crates/tiles/src/logins.rs index 579c07b4d4..38a7099d4e 100644 --- a/crates/tiles/src/logins.rs +++ b/crates/tiles/src/logins.rs @@ -98,6 +98,17 @@ fn entries() -> MutexGuard<'static, BTreeMap> { /// Installs a fresh session/connection login generation. Only addresses bound to the returned /// token can use it. Callers revoke their previous generation explicitly. pub fn activate(origin: &str, owner: &str, method: Method, value: &str, insecure: bool) -> Result { + activate_with_random(origin, owner, method, value, insecure, fill_random) +} + +fn activate_with_random( + origin: &str, + owner: &str, + method: Method, + value: &str, + insecure: bool, + mut random: impl FnMut(&mut [u8; 16]) -> Result<(), &'static str>, +) -> Result { rustgis_data::login_address::set_validator(check); if self::origin(origin).as_deref() != Some(origin) { return Err("A login requires a canonical HTTP or HTTPS origin."); @@ -123,7 +134,7 @@ pub fn activate(origin: &str, owner: &str, method: Method, value: &str, insecure // A token is the only thing that scopes a bound address to its login, so it is random. A // collision (never expected at 128 bits) is retried rather than replacing a live login. for _ in 0..4 { - let binding = random_token()?; + let binding = random_token(&mut random)?; if !entries.contains_key(&binding) { entries.insert( binding.clone(), @@ -136,44 +147,18 @@ pub fn activate(origin: &str, owner: &str, method: Method, value: &str, insecure } /// 128 unpredictable bits as 32 lower-case hex digits. -fn random_token() -> Result { +fn random_token(random: &mut impl FnMut(&mut [u8; 16]) -> Result<(), &'static str>) -> Result { let mut bytes = [0u8; 16]; - fill_random(&mut bytes)?; + random(&mut bytes)?; Ok(format!("{:032x}", u128::from_be_bytes(bytes))) } -#[cfg(not(target_arch = "wasm32"))] +// The browser backend calls globalThis.crypto.getRandomValues (windows and workers), catching +// missing APIs and exceptions. No binding is installed if the secure source fails. fn fill_random(bytes: &mut [u8; 16]) -> Result<(), &'static str> { getrandom::fill(bytes).map_err(|_| "No random numbers are available for a login binding.") } -/// The browser build has no operating-system generator here (`getrandom` is a native-only -/// dependency of this crate). It mixes the per-process keys of `std`'s `RandomState`, a counter, -/// the clock and a stack address. On `wasm32-unknown-unknown` those keys may be fixed, so this is -/// unique but not unpredictable: the browser build has one session, with no other session to -/// guess a token against. -#[cfg(target_arch = "wasm32")] -fn fill_random(bytes: &mut [u8; 16]) -> Result<(), &'static str> { - use std::{ - hash::{BuildHasher, Hasher}, - sync::atomic::{AtomicU64, Ordering}, - }; - static COUNTER: AtomicU64 = AtomicU64::new(0); - let count = COUNTER.fetch_add(1, Ordering::Relaxed); - let nanos = web_time::SystemTime::now().duration_since(web_time::UNIX_EPOCH).map_or(0, |d| d.as_nanos()); - let marker = 0u8; - let state = std::collections::hash_map::RandomState::new(); - for (salt, half) in bytes.chunks_mut(8).enumerate() { - let mut hasher = state.build_hasher(); - hasher.write_u64(count); - hasher.write_u128(nanos); - hasher.write_usize(std::ptr::addr_of!(marker) as usize); - hasher.write_usize(salt); - half.copy_from_slice(&hasher.finish().to_be_bytes()); - } - Ok(()) -} - fn add_redactions(redactions: &mut Vec, value: &str) { redactions.push(Redaction { value: Secret::new(value), encoded: false }); let encoded = percent_encode(value); @@ -465,21 +450,61 @@ mod tests { } #[test] - fn bindings_are_long_distinct_and_not_a_sequence() { - let target = "https://token-login.example"; - let tokens: Vec = (0..8).map(|_| activate(target, "token", Method::Header("X-Key".into()), "k", false).unwrap()).collect(); - let distinct: std::collections::BTreeSet<_> = tokens.iter().collect(); - assert_eq!(distinct.len(), tokens.len()); - for t in &tokens { - assert_eq!(t.len(), 32); - assert!(t.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))); - assert!(bind(t, &format!("{target}/tile")).is_ok()); - } - let value = |t: &String| u128::from_str_radix(t, 16).unwrap(); - assert!(tokens.windows(2).all(|w| value(&w[1]).wrapping_sub(value(&w[0])) != 1)); - for t in &tokens { - assert!(deactivate(t)); + fn failed_random_source_installs_nothing_and_preserves_a_live_login() { + let target = "https://random-failure-login.example"; + let old = activate(target, "random-replacement", Method::Header("X-Key".into()), "old-credential", false).unwrap(); + let address = bind(&old, &format!("{target}/tile")).unwrap(); + for owner in ["random-first-login", "random-replacement"] { + let mut attempts = 0; + let result = activate_with_random(target, owner, Method::Header("X-Key".into()), "failed-credential", false, |bytes| { + attempts += 1; + // Even a source that writes before failing must never activate those bytes. + bytes.fill(0x21); + Err("No random numbers are available for a login binding.") + }); + assert!(result.is_err()); + assert_eq!(attempts, 1); + let expected = usize::from(owner == "random-replacement"); + assert_eq!(entries().values().filter(|entry| entry.owner == owner).count(), expected); + assert_eq!(apply(&address).unwrap().unwrap().header.unwrap().1, "old-credential"); } + assert!(deactivate(&old)); + } + + #[test] + fn collisions_retry_without_overwriting_and_exhaust_after_four_attempts() { + let target = "https://collision-login.example"; + let method = || Method::Header("X-Key".into()); + let old = activate_with_random(target, "collision-old", method(), "old-key", false, |bytes| { + bytes.fill(0x3c); + Ok(()) + }) + .unwrap(); + let old_address = bind(&old, &format!("{target}/tile")).unwrap(); + let mut attempts = 0; + let next = activate_with_random(target, "collision-new", method(), "new-key", false, |bytes| { + attempts += 1; + bytes.fill(if attempts == 1 { 0x3c } else { 0x7a }); + Ok(()) + }) + .unwrap(); + assert_eq!(attempts, 2); + let next_address = bind(&next, &format!("{target}/tile")).unwrap(); + assert_eq!(apply(&old_address).unwrap().unwrap().header.unwrap().1, "old-key"); + assert_eq!(apply(&next_address).unwrap().unwrap().header.unwrap().1, "new-key"); + + attempts = 0; + let result = activate_with_random(target, "collision-exhausted", method(), "failed-key", false, |bytes| { + attempts += 1; + bytes.fill(0x3c); + Ok(()) + }); + assert_eq!(result.unwrap_err(), "The service login binding could not be created."); + assert_eq!(attempts, 4); + assert!(!entries().values().any(|entry| entry.owner == "collision-exhausted")); + assert_eq!(apply(&old_address).unwrap().unwrap().header.unwrap().1, "old-key"); + assert!(deactivate(&old)); + assert!(deactivate(&next)); } #[test] diff --git a/docs/architecture.md b/docs/architecture.md index fff3e82b5c..8e5e41abbb 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -133,6 +133,13 @@ model listing. the requested bucket's explicit-grant status. Copy only that login and grant boolean, not the grant list; parse and validate addresses and prepare signatures outside the lock. Keychain calls and network I/O never run under the registry lock. +- **Web-service login bindings.** Session-only Basic and API-key-header logins use 128-bit + cryptographically random bearer capabilities, encoded as 32 lowercase hex digits. Native + builds use `getrandom`'s OS source; browser builds enable its `wasm_js` backend, which calls + `globalThis.crypto.getRandomValues` in both windows and workers. No initialization hook or + time/counter/hash fallback is used. A missing or throwing secure source returns a fixed, + non-secret activation error before installing an entry or replacing the session's prior + login. Collisions retry at most four times without overwriting a live entry. - **Earthdata Login token.** `rustgis_tiles::earthdata::set_secret_store` gets `Services::secrets` from `RustGisApp::with_services`, and `prefs::opt_in`'s store for the command line and Python. `earthdata.login` with `remember` keeps the token at `earthdata/token`, and without it (or when the store refuses diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index 625edfc869..3ab725cea3 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -819,6 +819,11 @@ connection's kind doesn't change: make a new one); renaming the connection or ch at the same origin does not. **Log Out** (`connection.logout`) removes the session login. Log in again when a new session needs it. +Creating a login requires secure random numbers: the operating system's generator on desktop, +or the browser's `crypto.getRandomValues` in the web app. If that source is missing or fails, +RustGIS refuses the login rather than using a predictable fallback. A failed replacement leaves +the previous active login usable. + A login is sent only to the connection's origin: the same scheme, host and port, for its capabilities, listings, tiles, feature pages and refreshes. On the desktop a redirect within that origin keeps the login, and a redirect to another origin is followed without it, never getting