From 43d589a9a33dfa0dd3e6a83ab294d87ce0bdb96f Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Mon, 2 Nov 2026 12:11:17 +0100 Subject: [PATCH 01/19] feat(net): send a web-service login only to its connection's origin A login is carried by an unguessable per-session, per-connection binding inside the request address and applied by the tile cache only at the connection's exact origin (scheme, host and port). A redirect to another origin drops it; authenticated responses are cached per binding. The web app fetches such requests with redirect: error, no referrer and no browser credentials. --- apps/rustgis-web/src/web.rs | 388 ++++++++++++++---------- crates/data/src/lib.rs | 1 + crates/data/src/login_address.rs | 171 +++++++++++ crates/data/src/ogc.rs | 98 +++++- crates/data/src/ogc_info.rs | 38 ++- crates/data/src/source/mod.rs | 13 +- crates/data/src/source/remote.rs | 62 +++- crates/data/src/source/tests.rs | 18 ++ crates/map/src/lib.rs | 14 +- crates/map/src/raster.rs | 31 +- crates/map/src/tiles.rs | 46 ++- crates/tiles/src/address.rs | 5 + crates/tiles/src/cache.rs | 328 ++++++++++++++++++-- crates/tiles/src/cog.rs | 14 + crates/tiles/src/failure.rs | 14 +- crates/tiles/src/lib.rs | 1 + crates/tiles/src/logins.rs | 506 +++++++++++++++++++++++++++++++ crates/tiles/src/pmtiles.rs | 14 +- crates/tiles/src/schedule.rs | 4 +- crates/tiles/src/source.rs | 14 +- crates/tiles/src/sprite.rs | 6 + crates/tiles/src/stac.rs | 78 +++-- crates/tiles/src/stac/catalog.rs | 5 +- crates/tiles/src/template.rs | 22 ++ 24 files changed, 1656 insertions(+), 235 deletions(-) create mode 100644 crates/data/src/login_address.rs create mode 100644 crates/tiles/src/logins.rs diff --git a/apps/rustgis-web/src/web.rs b/apps/rustgis-web/src/web.rs index 6418b46675..05e11c8234 100644 --- a/apps/rustgis-web/src/web.rs +++ b/apps/rustgis-web/src/web.rs @@ -572,9 +572,8 @@ async fn unless_stalled( /// Downloads `url`, or the byte range it asks for (a PMTiles archive's part: see /// `rustgis_tiles::cache::split_range`). async fn fetch_bytes(url: &str) -> Result, String> { - let err = |e: wasm_bindgen::JsValue| format!("{e:?}"); let window = web_sys::window().ok_or("no window")?; - use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::cache; + use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{cache, logins}; // A data download carries its caller's budget (at most MAX_DOWNLOAD); other fetches use 64 MB. let (url, limit) = cache::split_download_limit(url); let max = limit.map(|n| n as u64).unwrap_or(64 << 20); @@ -582,7 +581,9 @@ async fn fetch_bytes(url: &str) -> Result, String> { let (url, range) = cache::split_range(url); // An `s3://` address becomes its web address, and a request to an S3 connection's endpoint // is signed (the bucket's CORS rules must allow the Authorization and x-amz-* headers). - let prepared = rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::s3::prepare(url)?; + logins::check(url).map_err(str::to_string)?; + let bound = url; + let prepared = rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::s3::prepare(logins::inner(url))?; let url = prepared.url.as_str(); // An ArcGIS sign-in's token, for the portal and the servers it covers, renewed first when it // is about to expire. It goes as the request's `token` parameter, not a header: ArcGIS's @@ -590,187 +591,268 @@ async fn fetch_bytes(url: &str) -> Result, String> { if !prepared.signed { crate::arcgis::renew_if_due(url).await; } - let arcgis = if prepared.signed { None } else { rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::arcgis_auth::with_token(url) }; - // Only the request carries it: messages name the address without it. - let request = arcgis.as_deref().unwrap_or(url); - let asked = match (range, tail) { - (Some((a, b)), _) => Some(format!("bytes={a}-{b}")), - (None, Some(n)) => Some(format!("bytes=-{n}")), - (None, None) => None, + let target = logins::inherit(bound, url); + let login = if prepared.signed { None } else { logins::apply(&target).map_err(str::to_string)? }; + let login_url = login.as_ref().map_or(url, |applied| applied.url.as_str()); + // An explicit login overrides ArcGIS's equivalent built-in credential on this hop. + let overrides_arcgis = login + .as_ref() + .and_then(|applied| applied.header.as_ref()) + .is_some_and(|(name, _)| name.eq_ignore_ascii_case("Authorization") || name.eq_ignore_ascii_case("X-Esri-Authorization")); + let arcgis = if prepared.signed || overrides_arcgis { + None + } else { + rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::arcgis_auth::with_token(login_url) }; - let init = web_sys::RequestInit::new(); - // A tile or document that stalls is given up (see `STALL_MS`). - let abort = if limit.is_none() { web_sys::AbortController::new().ok() } else { None }; - if let Some(a) = &abort { - init.set_signal(Some(&a.signal())); - } - let stalled = || format!("nothing came in {} seconds", STALL_MS / 1000); - if asked.is_some() || !prepared.headers.is_empty() { - let headers = web_sys::Headers::new().map_err(err)?; - if let Some(r) = &asked { - headers.set("Range", r).map_err(err)?; + // Only the request carries credentials: messages and cache keys keep the original address. + let request = arcgis.as_deref().unwrap_or(login_url); + let result = async { + let err = |e: wasm_bindgen::JsValue| { + if login.is_some() { "the browser couldn't process the authenticated request".to_string() } else { format!("{e:?}") } + }; + let login_header = login.as_ref().and_then(|applied| applied.header.as_ref()); + let asked = match (range, tail) { + (Some((a, b)), _) => Some(format!("bytes={a}-{b}")), + (None, Some(n)) => Some(format!("bytes=-{n}")), + (None, None) => None, + }; + let init = web_sys::RequestInit::new(); + // A tile or document that stalls is given up (see `STALL_MS`). + let abort = if limit.is_none() { web_sys::AbortController::new().ok() } else { None }; + if let Some(a) = &abort { + init.set_signal(Some(&a.signal())); } - for (k, v) in &prepared.headers { - headers.set(k, v).map_err(err)?; + let stalled = || format!("nothing came in {} seconds", STALL_MS / 1000); + if asked.is_some() || !prepared.headers.is_empty() || login_header.is_some() { + let headers = web_sys::Headers::new().map_err(err)?; + if let Some(r) = &asked { + headers.set("Range", r).map_err(err)?; + } + for (k, v) in &prepared.headers { + if !login_header.is_some_and(|(name, _)| name.eq_ignore_ascii_case(k)) { + headers.set(k, v).map_err(err)?; + } + } + if let Some((name, value)) = login_header { + headers.set(name, value).map_err(err)?; + } + init.set_headers(&headers); } - init.set_headers(&headers); - } - // A signed or authorized request goes only where its credentials are for. - if prepared.signed || arcgis.is_some() { - init.set_redirect(web_sys::RequestRedirect::Error); - } - // The token's address goes nowhere else: no referrer from it, no cookies with it. - if arcgis.is_some() { - init.set_referrer_policy(web_sys::ReferrerPolicy::NoReferrer); - init.set_credentials(web_sys::RequestCredentials::Omit); - } - let promise = window.fetch_with_str_and_init(request, &init); - // A refused fetch is an opaque TypeError ("Failed to fetch") by design: the browser doesn't - // say why, so the likely reasons are named, with the browser's own message but not its stack. - let resp: web_sys::Response = unless_stalled(promise, abort.as_ref()) - .await - .ok_or_else(stalled)? - .map_err(|e| { - let why = e.dyn_ref::().map(|e| String::from(e.message())).unwrap_or_else(|| err(e)); - format!( - "the browser couldn't reach it (offline, blocked by a browser extension or the network, or the server doesn't accept requests from web pages): {why}" - ) - })? - .dyn_into() - .map_err(err)?; - if !resp.ok() { - return Err(format!("HTTP {}", resp.status())); - } - // The last bytes asked for and the whole file answered: fine when it is no longer. - let whole_tail = tail.filter(|_| resp.status() != 206); - let max = match whole_tail { - Some(n) => n.saturating_add(1).min(max), - None => max, - }; - // A range asked for and the whole file answered (200): fine for a range from the start that - // holds the whole file (a small archive), checked once it's read; else no ranges. - let whole = range.is_some() && resp.status() != 206; - if range.is_some_and(|(a, _)| whole && a > 0) { - return Err(format!("{url}: {}", cache::NO_RANGES)); - } - // Fonts are at most about 10 MB and plugins 64 MB; refuse anything larger than allowed, by - // header when there is one and by the downloaded size otherwise. - if let Ok(Some(len)) = resp.headers().get("content-length").map(|v| v.and_then(|s| s.parse::().ok())) - && len > max - { - // The last bytes asked for and a longer file answered: no ranges, so it is downloaded whole. - if whole_tail.is_some() { + // A signed or authorized request goes only where its credentials are for. + if prepared.signed || arcgis.is_some() || login.is_some() { + init.set_redirect(web_sys::RequestRedirect::Error); + } + // A credential-bearing address goes nowhere else: no referrer from it, no cookies with it. + if arcgis.is_some() || login.is_some() { + init.set_referrer_policy(web_sys::ReferrerPolicy::NoReferrer); + init.set_credentials(web_sys::RequestCredentials::Omit); + } + let promise = window.fetch_with_str_and_init(request, &init); + // A refused fetch is an opaque TypeError ("Failed to fetch") by design. Keep its message + // only on requests without a login: even a browser's error may repeat credentials. + let resp: web_sys::Response = unless_stalled(promise, abort.as_ref()) + .await + .ok_or_else(stalled)? + .map_err(|e| { + let why = if login.is_some() { + "the authenticated request failed".to_string() + } else { + e.dyn_ref::().map(|e| String::from(e.message())).unwrap_or_else(|| err(e)) + }; + format!( + "the browser couldn't reach it (offline, blocked by a browser extension or the network, or the server doesn't accept requests from web pages): {why}" + ) + })? + .dyn_into() + .map_err(err)?; + if !resp.ok() { + return Err(format!("HTTP {}", resp.status())); + } + // The last bytes asked for and the whole file answered: fine when it is no longer. + let whole_tail = tail.filter(|_| resp.status() != 206); + let max = match whole_tail { + Some(n) => n.saturating_add(1).min(max), + None => max, + }; + // A range asked for and the whole file answered (200): fine for a range from the start that + // holds the whole file (a small archive), checked once it's read; else no ranges. + let whole = range.is_some() && resp.status() != 206; + if range.is_some_and(|(a, _)| whole && a > 0) { return Err(format!("{url}: {}", cache::NO_RANGES)); } - return Err(format!("{len} bytes is too large")); - } - // Read a chunk at a time, so a body larger than allowed (whatever its header said) is cut - // off as soon as it is, never held whole. - // Sized by the header when there is one (checked above), so the chunks don't reallocate. - let len = resp.headers().get("content-length").ok().flatten().and_then(|s| s.parse::().ok()).unwrap_or(0); - let mut bytes: Vec = Vec::with_capacity(usize::try_from(len.min(max)).unwrap_or(0)); - if let Some(body) = resp.body() { - let reader: web_sys::ReadableStreamDefaultReader = - body.get_reader().dyn_into().map_err(|_| "the response has no readable body".to_string())?; - loop { - // Headers came and then the body stalled: given up as a fetch that doesn't answer. - // (Aborting the fetch has cancelled its body too.) - let Some(chunk) = unless_stalled(reader.read(), abort.as_ref()).await else { return Err(stalled()) }; - let chunk = chunk.map_err(err)?; - if js_sys::Reflect::get(&chunk, &"done".into()).ok().and_then(|d| d.as_bool()).unwrap_or(true) { - break; + // Fonts are at most about 10 MB and plugins 64 MB; refuse anything larger than allowed, by + // header when there is one and by the downloaded size otherwise. + if let Ok(Some(len)) = resp.headers().get("content-length").map(|v| v.and_then(|s| s.parse::().ok())) + && len > max + { + // The last bytes asked for and a longer file answered: no ranges, so it is downloaded whole. + if whole_tail.is_some() { + return Err(format!("{url}: {}", cache::NO_RANGES)); } - let value = js_sys::Uint8Array::new(&js_sys::Reflect::get(&chunk, &"value".into()).map_err(err)?); - let n = value.length() as usize; - if (bytes.len() + n) as u64 > max { - let _ = reader.cancel(); - if whole_tail.is_some() { - return Err(format!("{url}: {}", cache::NO_RANGES)); + return Err(format!("{len} bytes is too large")); + } + // Read a chunk at a time, so a body larger than allowed (whatever its header said) is cut + // off as soon as it is, never held whole. + // Sized by the header when there is one (checked above), so the chunks don't reallocate. + let len = resp.headers().get("content-length").ok().flatten().and_then(|s| s.parse::().ok()).unwrap_or(0); + let mut bytes: Vec = Vec::with_capacity(usize::try_from(len.min(max)).unwrap_or(0)); + if let Some(body) = resp.body() { + let reader: web_sys::ReadableStreamDefaultReader = + body.get_reader().dyn_into().map_err(|_| "the response has no readable body".to_string())?; + loop { + // Headers came and then the body stalled: given up as a fetch that doesn't answer. + // (Aborting the fetch has cancelled its body too.) + let Some(chunk) = unless_stalled(reader.read(), abort.as_ref()).await else { return Err(stalled()) }; + let chunk = chunk.map_err(err)?; + if js_sys::Reflect::get(&chunk, &"done".into()).ok().and_then(|d| d.as_bool()).unwrap_or(true) { + break; + } + let value = js_sys::Uint8Array::new(&js_sys::Reflect::get(&chunk, &"value".into()).map_err(err)?); + let n = value.length() as usize; + if (bytes.len() + n) as u64 > max { + let _ = reader.cancel(); + if whole_tail.is_some() { + return Err(format!("{url}: {}", cache::NO_RANGES)); + } + return Err(format!("larger than {max} bytes")); } - return Err(format!("larger than {max} bytes")); + let at = bytes.len(); + bytes.resize(at + n, 0); + value.copy_to(bytes.get_mut(at..).unwrap_or_default()); } - let at = bytes.len(); - bytes.resize(at + n, 0); - value.copy_to(bytes.get_mut(at..).unwrap_or_default()); } + if let Some((a, b)) = range.filter(|_| whole) + && bytes.len() as u64 > b - a + 1 + { + return Err(format!("{url}: {}", cache::NO_RANGES)); + } + if whole_tail.is_some_and(|n| bytes.len() as u64 > n) { + return Err(format!("{url}: {}", cache::NO_RANGES)); + } + Ok(bytes) } - if let Some((a, b)) = range.filter(|_| whole) - && bytes.len() as u64 > b - a + 1 - { - return Err(format!("{url}: {}", cache::NO_RANGES)); - } - if whole_tail.is_some_and(|n| bytes.len() as u64 > n) { - return Err(format!("{url}: {}", cache::NO_RANGES)); - } - Ok(bytes) + .await; + logins::check(bound).map_err(str::to_string)?; + result.map_err(|error| if login.is_some() { logins::scrub(&format!("{url}: {error}")) } else { error }) } /// POSTs a JSON document and reads the answer (at most the tile cache's `MAX_BYTES`), without /// following redirects; an error status is `HTTP `. async fn post_json(url: &str, body: &str) -> Result, String> { - use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::cache; - let err = |e: wasm_bindgen::JsValue| format!("{e:?}"); + use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{cache, logins, s3}; let window = web_sys::window().ok_or("no window")?; - let init = web_sys::RequestInit::new(); - init.set_method("POST"); - init.set_redirect(web_sys::RequestRedirect::Error); - let headers = web_sys::Headers::new().map_err(err)?; - headers.set("Content-Type", "application/json").map_err(err)?; - headers.set("Accept", "application/json").map_err(err)?; - init.set_headers(&headers); - init.set_body(&wasm_bindgen::JsValue::from_str(body)); - let resp: web_sys::Response = wasm_bindgen_futures::JsFuture::from(window.fetch_with_str_and_init(url, &init)) - .await - .map_err(|_| "the browser couldn't reach it (offline, or the server doesn't accept requests from web pages)".to_string())? - .dyn_into() - .map_err(err)?; - if !resp.ok() { - return Err(format!("HTTP {}", resp.status())); + logins::check(url).map_err(str::to_string)?; + // Preparation supplies the reached URL, never GET signing headers for a POST. + let prepared = s3::prepare(logins::inner(url))?; + if prepared.signed { + return Err("S3 signed JSON POST requests are not supported.".into()); } - let buf = wasm_bindgen_futures::JsFuture::from(resp.array_buffer().map_err(err)?).await.map_err(err)?; - let bytes = js_sys::Uint8Array::new(&buf); - if bytes.length() as usize > cache::MAX_BYTES { - return Err(format!("{} bytes is too large", bytes.length())); + let target = logins::inherit(url, &prepared.url); + let login = logins::apply(&target).map_err(str::to_string)?; + let request = prepared.url.as_str(); + let result = async { + let err = |e: wasm_bindgen::JsValue| { + if login.is_some() { "the browser couldn't process the authenticated request".to_string() } else { format!("{e:?}") } + }; + let init = web_sys::RequestInit::new(); + init.set_method("POST"); + init.set_redirect(web_sys::RequestRedirect::Error); + let headers = web_sys::Headers::new().map_err(err)?; + let login_header = login.as_ref().and_then(|applied| applied.header.as_ref()); + for name in ["Content-Type", "Accept"] { + if !login_header.is_some_and(|(login_name, _)| login_name.eq_ignore_ascii_case(name)) { + headers.set(name, "application/json").map_err(err)?; + } + } + if let Some((name, value)) = login_header { + headers.set(name, value).map_err(err)?; + } + init.set_headers(&headers); + init.set_body(&wasm_bindgen::JsValue::from_str(body)); + if login.is_some() { + init.set_referrer_policy(web_sys::ReferrerPolicy::NoReferrer); + init.set_credentials(web_sys::RequestCredentials::Omit); + } + let resp: web_sys::Response = wasm_bindgen_futures::JsFuture::from(window.fetch_with_str_and_init(request, &init)) + .await + .map_err(|_| "the browser couldn't reach it (offline, or the server doesn't accept requests from web pages)".to_string())? + .dyn_into() + .map_err(err)?; + if !resp.ok() { + return Err(format!("HTTP {}", resp.status())); + } + let buf = wasm_bindgen_futures::JsFuture::from(resp.array_buffer().map_err(err)?).await.map_err(err)?; + let bytes = js_sys::Uint8Array::new(&buf); + if bytes.length() as usize > cache::MAX_BYTES { + return Err(format!("{} bytes is too large", bytes.length())); + } + Ok(bytes.to_vec()) } - Ok(bytes.to_vec()) + .await; + logins::check(url).map_err(str::to_string)?; + result.map_err(|error| if login.is_some() { logins::scrub(&format!("{url}: {error}")) } else { error }) } /// Asks for an address's headers (`HEAD`): a TIFF header's announced length and the type. The /// browser shows a page `X-TIFF-Header-Length` only when the server exposes it /// (`Access-Control-Expose-Headers`); without it, the header is read as any other file's. async fn head(url: &str) -> Result { - use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{cache, s3}; - let err = |e: wasm_bindgen::JsValue| format!("{e:?}"); + use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{cache, logins, s3}; let window = web_sys::window().ok_or("no window")?; - let prepared = s3::prepare(url)?; + logins::check(url).map_err(str::to_string)?; + let bound = url; + let prepared = s3::prepare(logins::inner(url))?; // A request signed for S3 is signed for a GET. if prepared.signed { return Ok(cache::Head::default()); } - let init = web_sys::RequestInit::new(); - init.set_method("HEAD"); - if !prepared.headers.is_empty() { - let headers = web_sys::Headers::new().map_err(err)?; - for (k, v) in &prepared.headers { - headers.set(k, v).map_err(err)?; + let url = prepared.url.as_str(); + let target = logins::inherit(bound, url); + let login = logins::apply(&target).map_err(str::to_string)?; + let request = login.as_ref().map_or(url, |applied| applied.url.as_str()); + let result = async { + let err = |e: wasm_bindgen::JsValue| { + if login.is_some() { "the browser couldn't process the authenticated request".to_string() } else { format!("{e:?}") } + }; + let init = web_sys::RequestInit::new(); + init.set_method("HEAD"); + let login_header = login.as_ref().and_then(|applied| applied.header.as_ref()); + if !prepared.headers.is_empty() || login_header.is_some() { + let headers = web_sys::Headers::new().map_err(err)?; + for (k, v) in &prepared.headers { + if !login_header.is_some_and(|(name, _)| name.eq_ignore_ascii_case(k)) { + headers.set(k, v).map_err(err)?; + } + } + if let Some((name, value)) = login_header { + headers.set(name, value).map_err(err)?; + } + init.set_headers(&headers); } - init.set_headers(&headers); - } - let resp: web_sys::Response = wasm_bindgen_futures::JsFuture::from(window.fetch_with_str_and_init(&prepared.url, &init)) - .await - .map_err(|_| "the browser couldn't reach it (offline, or the server doesn't accept requests from web pages)".to_string())? - .dyn_into() - .map_err(err)?; - if !resp.ok() { - return Err(format!("HTTP {}", resp.status())); + if login.is_some() { + init.set_redirect(web_sys::RequestRedirect::Error); + init.set_referrer_policy(web_sys::ReferrerPolicy::NoReferrer); + init.set_credentials(web_sys::RequestCredentials::Omit); + } + let resp: web_sys::Response = wasm_bindgen_futures::JsFuture::from(window.fetch_with_str_and_init(request, &init)) + .await + .map_err(|_| "the browser couldn't reach it (offline, or the server doesn't accept requests from web pages)".to_string())? + .dyn_into() + .map_err(err)?; + if !resp.ok() { + return Err(format!("HTTP {}", resp.status())); + } + let header = |k: &str| resp.headers().get(k).ok().flatten(); + Ok(cache::Head { + tiff_header_length: header("x-tiff-header-length").as_deref().and_then(cache::tiff_header_length), + content_type: header("content-type"), + // Seen only where the server exposes it to pages. + accepts_ranges: header("accept-ranges").as_deref().is_some_and(cache::accepts_byte_ranges), + }) } - let header = |k: &str| resp.headers().get(k).ok().flatten(); - Ok(cache::Head { - tiff_header_length: header("x-tiff-header-length").as_deref().and_then(cache::tiff_header_length), - content_type: header("content-type"), - // Seen only where the server exposes it to pages. - accepts_ranges: header("accept-ranges").as_deref().is_some_and(cache::accepts_byte_ranges), - }) + .await; + logins::check(bound).map_err(str::to_string)?; + result.map_err(|error| if login.is_some() { logins::scrub(&format!("{url}: {error}")) } else { error }) } /// Makes an HTTP request (the AI assistant's calls to its model), returning the response of any diff --git a/crates/data/src/lib.rs b/crates/data/src/lib.rs index c07e2ed6df..30d755b6db 100644 --- a/crates/data/src/lib.rs +++ b/crates/data/src/lib.rs @@ -20,6 +20,7 @@ pub mod io; pub mod kml; pub mod landxml; pub mod lidar; +pub mod login_address; pub mod mapinfo; pub mod mbtiles; pub mod ogc; diff --git a/crates/data/src/login_address.rs b/crates/data/src/login_address.rs new file mode 100644 index 0000000000..ebeffc3771 --- /dev/null +++ b/crates/data/src/login_address.rs @@ -0,0 +1,171 @@ +//! Secret-free runtime addresses bound to one web-service connection login. +//! Saved runtime tokens must be discarded and rebound by the owning session on project open. + +use std::net::Ipv6Addr; + +/// Canonical HTTP(S) origin: lowercase scheme and host, without a trailing host dot or +/// the scheme's default port. Paths, queries and fragments do not form part of the origin. +/// User information, whitespace, controls, backslashes and malformed authorities are refused. +fn raw_origin(url: &str) -> Option { + if url.chars().any(|c| c.is_whitespace() || c.is_control() || c == '\\') { + return None; + } + let (scheme, rest) = url.split_once("://")?; + let scheme = if scheme.eq_ignore_ascii_case("https") { + "https" + } else if scheme.eq_ignore_ascii_case("http") { + "http" + } else { + return None; + }; + let authority = rest.split(['/', '?', '#']).next()?; + if authority.is_empty() || authority.contains('@') { + return None; + } + let (host, port) = if let Some(ip) = authority.strip_prefix('[') { + let (ip, tail) = ip.split_once(']')?; + let host = format!("[{}]", ip.parse::().ok()?); + let port = if tail.is_empty() { None } else { Some(tail.strip_prefix(':')?) }; + (host, port) + } else { + let (host, port) = authority.split_once(':').map_or((authority, None), |(h, p)| (h, Some(p))); + let host = host.strip_suffix('.').unwrap_or(host); + if host.is_empty() || host.contains(['[', ']', '%', '<', '>', '"', '^', '`', '{', '}', '|']) || host.split('.').any(str::is_empty) { + return None; + } + (host.to_lowercase(), port) + }; + let port = match port { + Some(port) => { + if port.is_empty() || !port.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + Some(port.parse::().ok()?) + } + None => None, + }; + match port.filter(|p| *p != if scheme == "https" { 443 } else { 80 }) { + Some(port) => Some(format!("{scheme}://{host}:{port}")), + None => Some(format!("{scheme}://{host}")), + } +} +pub const PREFIX: &str = "rustgis-login:"; +const BAD: &str = "not a bound service login address"; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct BoundLogin<'a> { + pub binding: &'a str, + pub origin: &'a str, + pub url: &'a str, +} + +pub fn is_wrapper(address: &str) -> bool { + address.starts_with(PREFIX) +} + +pub fn parse(address: &str) -> Result>, &'static str> { + let Some(rest) = address.strip_prefix(PREFIX) else { return Ok(None) }; + if address.len() > 16384 || address.chars().any(char::is_control) { + return Err(BAD); + } + let (binding, rest) = rest.split_once(':').ok_or(BAD)?; + if !(16..=64).contains(&binding.len()) || !binding.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) { + return Err(BAD); + } + let (len, rest) = rest.split_once(':').ok_or(BAD)?; + if len.len() > 4 || len.is_empty() || !len.bytes().all(|b| b.is_ascii_digit()) { + return Err(BAD); + } + let len: usize = len.parse().map_err(|_| BAD)?; + let origin = rest.get(..len).ok_or(BAD)?; + let url = rest.get(len..).and_then(|s| s.strip_prefix(':')).ok_or(BAD)?; + if raw_origin(origin).as_deref() != Some(origin) || raw_origin(url).as_deref() != Some(origin) { + return Err(BAD); + } + Ok(Some(BoundLogin { binding, origin, url })) +} + +pub fn bind(binding: &str, origin: &str, url: &str) -> Result { + let address = format!("{PREFIX}{binding}:{}:{origin}:{url}", origin.len()); + parse(&address)?; + Ok(address) +} + +pub fn inner(address: &str) -> &str { + parse(address).ok().flatten().map_or(address, |b| b.url) +} + +/// Canonical origin of a plain URL or a well-formed bound URL. +pub fn origin(address: &str) -> Option { + raw_origin(inner(address)) +} + +pub fn rebound(address: &str, binding: &str) -> Option { + let b = parse(address).ok().flatten()?; + bind(binding, b.origin, b.url).ok() +} + +pub fn reached(address: &str) -> &str { + if is_wrapper(address) { + inner(address) + } else { + address.split_once('@').filter(|(_, a)| is_wrapper(a)).map_or(address, |(_, a)| inner(a)) + } +} + +/// Retains a document's binding for same-origin derived addresses only. +pub fn inherit(base: &str, url: &str) -> String { + let url = inner(url); + let Ok(Some(b)) = parse(base) else { return url.to_string() }; + if origin(url).as_deref() == Some(b.origin) { + bind(b.binding, b.origin, url).unwrap_or_else(|_| url.to_string()) + } else { + url.to_string() + } +} + +type Validator = fn(&str) -> Result<(), &'static str>; +static VALIDATOR: std::sync::OnceLock = std::sync::OnceLock::new(); + +/// Installs the runtime authority check supplied by the networking crate. +pub fn set_validator(validator: Validator) { + let _ = VALIDATOR.set(validator); +} + +/// Bound cached data is unreadable unless its owning runtime still authorizes the binding. +pub fn check(address: &str) -> Result<(), &'static str> { + if !is_wrapper(address) { + return Ok(()); + } + parse(address)?; + VALIDATOR.get().ok_or("The service login is no longer active. Sign in to this connection again.")?(address) +} + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + + #[test] + fn binding_length_and_alphabet_are_checked() { + let wrapped = |b: &str| format!("{PREFIX}{b}:14:https://a.test:https://a.test/x"); + assert!(parse(&wrapped(&"ab12".repeat(8))).unwrap().is_some()); + assert!(parse(&wrapped(&"a".repeat(15))).is_err()); + assert!(parse(&wrapped(&"a".repeat(65))).is_err()); + assert!(parse(&wrapped(&format!("{}g", "a".repeat(31)))).is_err()); + assert!(parse(&wrapped(&"A".repeat(32))).is_err()); + } + + #[test] + fn round_trip_and_fixed_errors() { + let b = bind("0123456789abcdef", "https://a.test", "https://a.test/x?y=1").unwrap(); + let p = parse(&b).unwrap().unwrap(); + assert_eq!((p.binding, p.origin, p.url), ("0123456789abcdef", "https://a.test", "https://a.test/x?y=1")); + assert_eq!(inner(&b), p.url); + for bad in + ["rustgis-login:secret", "rustgis-login:0123456789abcdef:1:é:https://a.test", "rustgis-login:0123456789abcdef:0::/etc/passwd"] + { + assert_eq!(parse(bad).unwrap_err(), BAD); + } + } +} diff --git a/crates/data/src/ogc.rs b/crates/data/src/ogc.rs index 6ed3f478f2..326043e04e 100644 --- a/crates/data/src/ogc.rs +++ b/crates/data/src/ogc.rs @@ -454,7 +454,11 @@ pub const OGC_API_LIMIT: usize = 10_000; /// The address to fetch a service's capabilities from: OGC API landing pages and collection lists /// are JSON; WMS, WMTS and WFS addresses get a GetCapabilities request unless they have one. +/// A runtime service-login binding stays attached only to same-origin derived requests. pub fn capabilities_url(url: &str, kind: Option<&str>) -> String { + if let Ok(Some(bound)) = crate::login_address::parse(url) { + return crate::login_address::inherit(url, &capabilities_url(bound.url, kind)); + } // ArcGIS REST: the service's JSON description. if kind.is_some_and(|k| k.eq_ignore_ascii_case("arcgis")) || (kind.is_none() && is_arcgis(url)) { return if query_value(url, "f").is_some_and(|f| f.eq_ignore_ascii_case("json") || f.eq_ignore_ascii_case("pjson")) { @@ -491,6 +495,9 @@ pub fn capabilities_url(url: &str, kind: Option<&str>) -> String { /// Requests JSON once, retaining the address's other query parameters. pub fn json_url(address: &str) -> String { + if let Ok(Some(bound)) = crate::login_address::parse(address) { + return crate::login_address::inherit(address, &json_url(bound.url)); + } let Ok(mut url) = url::Url::parse(address) else { return with_query(address, "f=json") }; // Authentication and signed query values can depend on their original byte encoding. let mut format_seen = false; @@ -525,7 +532,7 @@ fn is_data_link(link: &serde_json::Value) -> bool { /// Chooses JSON before an untyped data link, without guessing an endpoint or copying credentials. fn collections_link(address: &str, links: &[serde_json::Value]) -> Option { - let base = url::Url::parse(address).ok()?; + let base = url::Url::parse(crate::login_address::inner(address)).ok()?; links .iter() .filter(|l| is_data_link(l)) @@ -545,8 +552,8 @@ fn collections_link(address: &str, links: &[serde_json::Value]) -> Option bool { /// The part of an address after its host: the path, query and fragment. fn after_host(url: &str) -> &str { + let url = crate::login_address::inner(url); let rest = url.split_once("://").map(|(_, r)| r).unwrap_or(url); rest.find(['/', '?', '#']).and_then(|i| rest.get(i..)).unwrap_or("") } @@ -577,6 +585,7 @@ fn origin(url: &str) -> &str { /// The value of a query parameter, by its whole name (any case). fn query_value<'a>(url: &'a str, key: &str) -> Option<&'a str> { + let url = crate::login_address::inner(url); let q = url.split('#').next()?.split_once('?')?.1; q.split('&').find_map(|kv| { let (k, v) = kv.split_once('=').unwrap_or((kv, "")); @@ -587,13 +596,22 @@ fn query_value<'a>(url: &'a str, key: &str) -> Option<&'a str> { /// Whether an address is a web address (`http://` or `https://`), not another scheme that only /// starts with those letters. fn is_web(url: &str) -> bool { + let url = crate::login_address::inner(url); let l = url.get(..8).unwrap_or(url).to_ascii_lowercase(); l.starts_with("http://") || l.starts_with("https://") } /// A link from a document at `from`, resolved: absolute, scheme-relative (`//host/…`), /// host-relative (`/…`) or relative to the document's folder. `None` for other schemes. +/// Only links at the bound document's canonical origin retain its runtime login binding. fn resolve(from: &str, href: &str) -> Option { + crate::login_address::parse(from).ok()?; + crate::login_address::parse(href.trim()).ok()?; + let url = resolve_inner(crate::login_address::inner(from), crate::login_address::inner(href.trim()))?; + Some(crate::login_address::inherit(from, &url)) +} + +fn resolve_inner(from: &str, href: &str) -> Option { let href = href.trim(); if is_web(href) { return Some(href.to_string()); @@ -640,6 +658,9 @@ fn items_3d(from: &str, link: &serde_json::Value) -> Option<&'static str> { } fn with_query(url: &str, q: &str) -> String { + if let Ok(Some(bound)) = crate::login_address::parse(url) { + return crate::login_address::inherit(url, &with_query(bound.url, q)); + } let (base, frag) = url.split_once('#').map(|(b, f)| (b, Some(f))).unwrap_or((url, None)); let sep = if base.contains('?') { if base.ends_with('?') || base.ends_with('&') { "" } else { "&" } } else { "?" }; match frag { @@ -651,6 +672,9 @@ fn with_query(url: &str, q: &str) -> String { /// A service's address for its requests when its document names none: the address without the /// OGC request parameters, keeping the others (MapServer's `map=`, a token). fn endpoint(url: &str) -> String { + if let Ok(Some(bound)) = crate::login_address::parse(url) { + return crate::login_address::inherit(url, &endpoint(bound.url)); + } let b = base(url); let kept: Vec<&str> = url .split('#') @@ -2748,6 +2772,9 @@ fn arcgis_paging(layer: &serde_json::Value) -> Option { /// The description of the layer an ArcGIS query reads (`…/FeatureServer/0?f=json`), with the /// query's token. fn arcgis_layer_url(query: &str) -> String { + if let Ok(Some(bound)) = crate::login_address::parse(query) { + return crate::login_address::inherit(query, &arcgis_layer_url(bound.url)); + } let b = base(query).trim_end_matches('/'); let layer = b.get(..b.len().saturating_sub("/query".len())).unwrap_or(b); match query_value(query, "token").filter(|t| !t.is_empty()) { @@ -3315,6 +3342,71 @@ mod fixtures; mod tests { use super::*; + #[test] + fn capabilities_and_collections_preserve_only_the_explicit_login() { + let binding = "0123456789abcdef"; + let origin = "https://maps.example.org"; + for (url, kind) in [ + ("https://MAPS.example.org:443/wms?map=x", None), + ("https://maps.example.org/ogc/features?f=html&key=a%2Fb", Some("ogcapi")), + ("https://maps.example.org/rest/services/P/FeatureServer", Some("arcgis")), + ("https://maps.example.org/capabilities.xml", None), + ] { + let address = crate::login_address::bind(binding, origin, url).unwrap(); + let requested = capabilities_url(&address, kind); + let bound = crate::login_address::parse(&requested).unwrap().unwrap(); + assert_eq!((bound.binding, bound.origin), (binding, origin)); + assert_eq!(bound.url, capabilities_url(url, kind)); + assert_eq!(capabilities_url(&requested, kind), requested); + assert!(crate::login_address::parse(&capabilities_url(url, kind)).unwrap().is_none()); + } + let address = crate::login_address::bind(binding, origin, "https://maps.example.org/api?key=a%2Fb").unwrap(); + for href in ["/collections", "https://MAPS.example.org:443/collections"] { + let links = serde_json::json!([{"rel":"data","type":"application/json","href":href}]); + let next = collections_link(&address, links.as_array().unwrap()).unwrap(); + assert_eq!(crate::login_address::parse(&next).unwrap().unwrap().binding, binding); + } + let links = serde_json::json!([{"rel":"data","type":"application/json","href":"https://other.example.org/collections"}]); + assert_eq!(collections_link(&address, links.as_array().unwrap()).unwrap(), "https://other.example.org/collections"); + } + + #[test] + fn linked_pages_drop_the_login_after_leaving_its_origin() { + let first_url = + crate::login_address::bind("0123456789abcdef", "https://maps.example.org", "https://maps.example.org/api/items").unwrap(); + let page = |id, href| { + serde_json::to_vec(&serde_json::json!({ + "type":"FeatureCollection", "features":[{"id":id}], + "links":[{"rel":"next","type":"application/geo+json","href":href}] + })) + .unwrap() + }; + let Step::Fetch(pages) = read_pages(&first_url, page(1, "https://MAPS.example.org:443/api/items?page=2"), 100) else { + panic!("the second page must be fetched"); + }; + assert_eq!(pages.referrer(), first_url); + let second_url = pages.next_url().to_string(); + let bound = crate::login_address::parse(&second_url).unwrap().unwrap(); + assert_eq!(bound.binding, "0123456789abcdef"); + assert_eq!(bound.url, "https://MAPS.example.org:443/api/items?page=2"); + let Step::Fetch(pages) = pages.land(Ok(page(2, "https://other.example.org/items?page=3"))) else { + panic!("the foreign page must be fetched anonymously"); + }; + assert_eq!(pages.referrer(), second_url); + assert_eq!(pages.next_url(), "https://other.example.org/items?page=3"); + let Step::Fetch(pages) = pages.land(Ok(page(3, "https://maps.example.org/api/items?page=4"))) else { + panic!("the returning page must remain anonymous"); + }; + assert_eq!(pages.next_url(), "https://maps.example.org/api/items?page=4"); + assert!(crate::login_address::parse(pages.next_url()).unwrap().is_none()); + assert_eq!( + resolve(&first_url, "/api/items?page=2").unwrap(), + crate::login_address::bind("0123456789abcdef", "https://maps.example.org", "https://maps.example.org/api/items?page=2") + .unwrap() + ); + assert_eq!(resolve("https://maps.example.org/api", &first_url).unwrap(), "https://maps.example.org/api/items"); + } + /// A layer's box names its system otherwise than its list (`EPSG:3035` for the URN): found by /// code, it still says the service writes LAEA Europe north first. #[test] diff --git a/crates/data/src/ogc_info.rs b/crates/data/src/ogc_info.rs index 4f5e5f560c..76a4727d5d 100644 --- a/crates/data/src/ogc_info.rs +++ b/crates/data/src/ogc_info.rs @@ -192,8 +192,12 @@ const GET_MAP_KEYS: [&str; 16] = [ const INFO_KEYS: [&str; 7] = ["query_layers", "info_format", "feature_count", "i", "j", "x", "y"]; /// An address without the WMS request parameters it carries (GetMap's and GetFeatureInfo's), -/// keeping the others (MapServer's `map=`, a token); as it is when it carries none. +/// keeping the others (MapServer's `map=`, a token); as it is when it carries none. Runtime +/// service-login bindings remain attached to the cleaned same-origin request address. pub(super) fn without_wms_keys(url: &str) -> String { + if let Ok(Some(bound)) = crate::login_address::parse(url) { + return crate::login_address::inherit(url, &without_wms_keys(bound.url)); + } let pairs: Vec<&str> = url .split('#') .next() @@ -593,6 +597,38 @@ mod tests { service.layers.into_iter().find(|l| l.name == layer).and_then(|l| l.feature_info).unwrap() } + #[test] + fn bound_feature_info_requests_and_service_discovery_preserve_authority() { + let address = crate::login_address::bind( + "0123456789abcdef", + "https://maps.example.org", + "https://maps.example.org/wms?map=x&SERVICE=WMS&REQUEST=GetMap&LAYERS=roads", + ) + .unwrap(); + let info = FeatureInfo { + url: without_wms_keys(&address), + version: "1.3.0".into(), + layer: "roads".into(), + formats: vec!["application/json".into()], + ..FeatureInfo::default() + }; + assert!(info.queryable()); + let request = info.request(0.0, 0.0, 10.0).unwrap(); + let bound = crate::login_address::parse(&request).unwrap().unwrap(); + assert_eq!(bound.binding, "0123456789abcdef"); + assert!(bound.url.contains("map=x&SERVICE=WMS") && bound.url.contains("REQUEST=GetFeatureInfo")); + let (caps, layer) = wms_layer_of(&address).unwrap(); + assert_eq!(layer, "roads"); + assert_eq!(crate::login_address::parse(&caps).unwrap().unwrap().binding, bound.binding); + let same_origin = CAPS_13.replace("https://info.example.org", "https://maps.example.org"); + let service = crate::ogc::parse(&address, same_origin.as_bytes()).unwrap(); + let roads = service.layers.iter().find(|l| l.name == "roads").unwrap().feature_info.as_ref().unwrap(); + assert_eq!(crate::login_address::parse(&roads.url).unwrap().unwrap().binding, bound.binding); + let service = crate::ogc::parse(&address, CAPS_13.as_bytes()).unwrap(); + let roads = service.layers.iter().find(|l| l.name == "roads").unwrap().feature_info.as_ref().unwrap(); + assert_eq!(roads.url, "https://info.example.org/wms?"); + } + #[test] fn capabilities_give_the_request_and_queryable_is_inherited() { let roads = info_of(CAPS_13, "roads"); diff --git a/crates/data/src/source/mod.rs b/crates/data/src/source/mod.rs index 1bf7ce9ff1..f8357131fc 100644 --- a/crates/data/src/source/mod.rs +++ b/crates/data/src/source/mod.rs @@ -385,11 +385,11 @@ pub fn open_shared(file_name: &str, bytes: std::sync::Arc>, layer: Optio } } -/// Whether a web address names a file read in place by range: a FlatGeobuf or a GeoParquet. An -/// address read through a saved S3 connection ([`crate::s3_address`]) is judged by the address it -/// wraps. +/// Whether a web address names a file read in place by range: a FlatGeobuf or a GeoParquet. +/// Saved S3 connection and runtime service-login addresses are judged by the URL they wrap; +/// the original address remains the range reader's identity. pub fn is_remote_name(url: &str) -> bool { - let lower = crate::s3_address::inner(url).to_ascii_lowercase(); + let lower = crate::login_address::inner(crate::s3_address::inner(url)).to_ascii_lowercase(); let path = lower.split(['?', '#']).next().unwrap_or(&lower); // `s3://` too: the app's fetchers check it and read it from its bucket's web address (signed // only for a connection that serves the bucket, see rustgis_tiles::s3). @@ -399,6 +399,7 @@ pub fn is_remote_name(url: &str) -> bool { /// A layer name from a web address: its file name without the extension. fn url_name(url: &str) -> String { + let url = crate::login_address::inner(crate::s3_address::inner(url)); let path = url.split(['?', '#']).next().unwrap_or(url); let file = path.rsplit('/').next().unwrap_or(path); let stem = file.rsplit_once('.').map(|(a, _)| a).unwrap_or(file); @@ -414,8 +415,8 @@ pub fn open_url_now(url: &str) -> Result> { /// [`open_url_now`] from `file`: a drive's reads note what they miss on the file it fetches for, /// even after other files pushed it out of those kept. fn open_file_now(file: std::sync::Arc) -> Result> { - let url = file.url().to_string(); - let name = url_name(&url); + let url = crate::login_address::inner(crate::s3_address::inner(file.url())); + let name = url_name(url); let lower = url.to_ascii_lowercase(); let path = lower.split(['?', '#']).next().unwrap_or(&lower); if path.ends_with(".fgb") { diff --git a/crates/data/src/source/remote.rs b/crates/data/src/source/remote.rs index 6cf7fd3f79..09afd5c420 100644 --- a/crates/data/src/source/remote.rs +++ b/crates/data/src/source/remote.rs @@ -86,6 +86,16 @@ pub fn can_fetch() -> bool { /// Fetches a whole file through the installed fetcher (a file that can't be read in place). pub fn fetch_whole(url: &str, done: Done) { + if let Err(error) = crate::login_address::check(url) { + done(Err(Failed::Other(error.into()))); + return; + } + let done: Done = if crate::login_address::is_wrapper(url) { + let address = url.to_string(); + Box::new(move |r| done(crate::login_address::check(&address).map_err(|e| Failed::Other(e.into())).and(r))) + } else { + done + }; let f = fetcher().lock().unwrap_or_else(|e| e.into_inner()).clone(); match f { Some(f) => f(url, Want::Whole, done), @@ -148,7 +158,7 @@ pub struct Remote { impl std::fmt::Debug for Remote { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Remote").field("url", &self.url).finish_non_exhaustive() + f.debug_struct("Remote").field("url", &crate::login_address::inner(&self.url)).finish_non_exhaustive() } } @@ -185,6 +195,7 @@ impl Remote { } fn get(&self, at: u64, len: u64, exact: bool) -> Option { + crate::login_address::check(&self.url).ok()?; let mut s = self.lock(); let len = match s.end { Some(end) if at >= end => return Some(bytes::Bytes::new()), @@ -216,6 +227,7 @@ impl Remote { /// Bytes from `at` to the end of the held piece that holds `at` (a column chunk read in /// pages), or `None` (noted as missing, `want` bytes from there). pub fn from(&self, at: u64, want: u64) -> Option { + crate::login_address::check(&self.url).ok()?; let mut s = self.lock(); s.tick += 1; let tick = s.tick; @@ -244,6 +256,7 @@ impl Remote { /// The file's last `n` bytes (all of it when it's shorter), and whether that is the whole /// file; `None` when they aren't held (noted as missing). pub fn tail(&self, n: u64) -> Option<(Arc>, bool)> { + crate::login_address::check(&self.url).ok()?; let mut s = self.lock(); match &s.tail { Some((t, whole)) if *whole || t.len() as u64 >= n => Some((t.clone(), *whole)), @@ -308,6 +321,9 @@ impl Remote { /// Keeps fetched bytes `at..`: merged with the parts they meet. A piece shorter than asked /// for (`asked`) ends the file. fn insert(&self, at: u64, asked: u64, bytes: Vec) { + if crate::login_address::check(&self.url).is_err() { + return; + } let mut s = self.lock(); if (bytes.len() as u64) < asked { let end = at.saturating_add(bytes.len() as u64); @@ -359,6 +375,9 @@ impl Remote { /// Keeps a file's last `asked` bytes: fewer is the whole file. fn insert_tail(&self, asked: u64, bytes: Vec) { + if crate::login_address::check(&self.url).is_err() { + return; + } let whole = (bytes.len() as u64) < asked; let bytes = Arc::new(bytes); { @@ -429,10 +448,18 @@ fn step( drive: u64, rounds: usize, ) { + if let Err(error) = crate::login_address::check(remote.url()) { + done(Err(DataError::Format(error.into()))); + return; + } let r = { let _running = Running::enter(drive); work() }; + if let Err(error) = crate::login_address::check(remote.url()) { + done(Err(DataError::Format(error.into()))); + return; + } let wants = remote.take_missing(drive); if wants.is_empty() { done(r); @@ -598,6 +625,17 @@ pub(crate) mod testing { use std::collections::HashMap; use std::sync::atomic::{AtomicUsize, Ordering}; + pub(crate) static LOGIN_ACTIVE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(true); + + pub(crate) fn validate_login(address: &str) -> std::result::Result<(), &'static str> { + let binding = crate::login_address::parse(address)?.map(|b| b.binding); + if binding == Some("ffffffffffffffff") && !LOGIN_ACTIVE.load(Ordering::Acquire) { + Err("The service login is no longer active.") + } else { + Ok(()) + } + } + fn served() -> &'static Mutex>, Arc)>> { static S: OnceLock>, Arc)>>> = OnceLock::new(); S.get_or_init(Mutex::default) @@ -635,6 +673,28 @@ pub(crate) mod testing { mod tests { use super::*; + #[test] + fn held_ranges_and_late_publications_are_revoked() { + crate::login_address::set_validator(testing::validate_login); + let address = crate::login_address::bind("ffffffffffffffff", "https://revoked.test", "https://revoked.test/file.fgb").unwrap(); + let remote = Remote::new(&address); + remote.insert(0, 4, b"body".to_vec()); + remote.insert_tail(4, b"tail".to_vec()); + assert_eq!(remote.bytes(0, 4).unwrap().as_ref(), b"body"); + assert!(remote.tail(4).is_some()); + testing::LOGIN_ACTIVE.store(false, Ordering::Release); + assert!(remote.bytes(0, 4).is_none()); + assert!(remote.from(0, 4).is_none()); + assert!(remote.tail(4).is_none()); + remote.insert(4, 4, b"late".to_vec()); + remote.insert_tail(4, b"late".to_vec()); + assert_eq!(remote.lock().held, 4); + assert_eq!(remote.lock().tail.as_ref().unwrap().0.as_slice(), b"tail"); + let replacement = crate::login_address::bind("eeeeeeeeeeeeeeee", "https://revoked.test", "https://revoked.test/file.fgb").unwrap(); + assert!(file(&replacement).bytes(0, 4).is_none()); + assert!(file("https://revoked.test/file.fgb").bytes(0, 4).is_none()); + } + #[test] fn holds_merges_and_notes_what_is_missing() { let r = Remote::new("https://x.org/a.fgb"); diff --git a/crates/data/src/source/tests.rs b/crates/data/src/source/tests.rs index d6b5e55cd6..b39aa6aba9 100644 --- a/crates/data/src/source/tests.rs +++ b/crates/data/src/source/tests.rs @@ -1029,6 +1029,24 @@ fn flatgeobuf_and_geoparquet_by_address() { assert!(is_remote_name("s3://bucket/a/b.parquet") && !is_remote_name("s3://bucket/a/b.tif") && !is_remote_name("ftp://x.org/a.fgb")); } +#[test] +fn login_bound_remote_files_keep_their_request_address() { + crate::login_address::set_validator(remote::testing::validate_login); + for (name, parquet) in [("bound-grid.fgb", false), ("bound-grid.geoparquet", true)] { + let inner = format!("https://test.example/{name}?signature=a%2Fb#view"); + let address = crate::login_address::bind("0123456789abcdef", "https://test.example", &inner).unwrap(); + assert!(is_remote_name(&address)); + assert_eq!(url_name(&address), "bound-grid"); + remote::testing::serve(&address, big(4, parquet)); + let file = remote::file(&address); + assert_eq!(file.url(), address); + let source = open_driven(&address); + assert_eq!(source.info().count, Some(16)); + } + let image = crate::login_address::bind("0123456789abcdef", "https://test.example", "https://test.example/a.tif").unwrap(); + assert!(!is_remote_name(&image)); +} + #[test] fn flatgeobuf_stand_in_is_a_feature() { // The stand-in a sample passes over must pass the flatbuffers verifier, as real features do. diff --git a/crates/map/src/lib.rs b/crates/map/src/lib.rs index 44454f706b..f5de5cd01e 100644 --- a/crates/map/src/lib.rs +++ b/crates/map/src/lib.rs @@ -211,6 +211,12 @@ pub struct Layer { /// The GeoPackage table this layer's edits save to. #[serde(default, skip_serializing_if = "Option::is_none")] pub gpkg: Option, + /// The saved connection it was added from (its id, in the app's preferences): a layer whose + /// source is read again logs in with that connection's login, and a project opened where + /// the connection or its login is missing asks for one. Empty for a layer that came from no + /// connection. Never a secret. + #[serde(default, skip_serializing_if = "String::is_empty")] + pub connection: String, /// Process-local identity, preserved by snapshots but never stored in a project. #[serde(skip, default = "next_layer_instance")] instance_id: u64, @@ -403,6 +409,7 @@ impl Layer { source: None, postgis: None, gpkg: None, + connection: String::new(), projected: Default::default(), display: None, display_rejected: Vec::new(), @@ -1354,8 +1361,8 @@ impl Map { } /// Every address the map keeps to read its data again (service groups and their layers' - /// sources, tile layers and their dated addresses, terrain tiles, layers' dataset, streamed and - /// raster sources, time-following Zarr stores). Local paths come too: callers judge each one. + /// sources, tile layers and their dated, feature-info and legend addresses, terrain tiles, + /// layers' dataset, streamed and raster sources, time-following Zarr stores). Local paths come too. pub fn addresses_mut(&mut self) -> Vec<&mut String> { let mut addresses = Vec::new(); for group in &mut self.service_groups { @@ -1372,6 +1379,9 @@ impl Map { for (_, url) in &mut tile.time_urls { addresses.push(url); } + if let Some(info) = &mut tile.feature_info { + addresses.push(&mut info.url); + } // (A legend's address is built from the service's, its key or password included.) for legend in &mut tile.legends { addresses.push(&mut legend.url); diff --git a/crates/map/src/raster.rs b/crates/map/src/raster.rs index ae357e7b19..5c31059b33 100644 --- a/crates/map/src/raster.rs +++ b/crates/map/src/raster.rs @@ -152,8 +152,9 @@ impl RasterLayer { } } - /// Reads the pixels again from the path or the embedded file (after a project opens). A - /// layer that follows the time slider is left to the engine, which reads the step shown. + /// Reads the pixels again from the path or the embedded file (after a project opens). An S3 + /// or login-wrapped address is downloaded whole, keeping its wrapper. A layer that follows the + /// time slider is left to the engine, which reads the step shown. pub fn reload(&mut self) -> Result<(), String> { if self.time_zarr.is_some() { return Ok(()); @@ -161,12 +162,17 @@ impl RasterLayer { self.opening = None; self.error = None; if self.embedded.is_none() - && let Some(path) = self - .path - .as_deref() - .filter(|p| rustgis_data::s3_address::is_wrapper(p) || p.get(..5).is_some_and(|s| s.eq_ignore_ascii_case("s3://"))) + && let Some(path) = self.path.as_deref().filter(|p| { + rustgis_data::login_address::is_wrapper(p) + || rustgis_data::s3_address::is_wrapper(p) + || p.get(..5).is_some_and(|s| s.eq_ignore_ascii_case("s3://")) + }) { - let name = rustgis_data::s3_address::inner(path).split(['?', '#']).next().unwrap_or("raster.tif").to_string(); + let name = rustgis_data::login_address::inner(rustgis_data::s3_address::inner(path)) + .split(['?', '#']) + .next() + .unwrap_or("raster.tif") + .to_string(); let placement = self.placement; let slot = Arc::new(Mutex::new(None)); let land = slot.clone(); @@ -1329,4 +1335,15 @@ mod tests { assert!(missing.reload().is_err()); assert!(missing.data.is_none()); } + + /// A plain web address is read as it was before logins: never downloaded whole on reload. + #[test] + fn reload_downloads_only_s3_and_login_wrapped_addresses() { + let gt = GeoTransform { origin_x: 0.0, origin_y: 1.0, pixel_width: 1.0, pixel_height: -1.0 }; + let r = Raster::new("web", 1, 1, vec![vec![1.0]], gt, Crs::LonLat, None, ColorHint::Gray, "UInt8").unwrap(); + let mut plain = RasterLayer::from_path(r, "https://anon757.invalid/x.tif"); + plain.data = None; + assert!(plain.reload().is_err()); + assert!(plain.opening.is_none(), "an anonymous web raster is not fetched whole"); + } } diff --git a/crates/map/src/tiles.rs b/crates/map/src/tiles.rs index 1c73affea8..3b7ca1e00c 100644 --- a/crates/map/src/tiles.rs +++ b/crates/map/src/tiles.rs @@ -73,6 +73,7 @@ impl TileKind { } /// The kind an address most likely is, from how it looks. pub fn guess(url: &str) -> TileKind { + let url = rustgis_data::login_address::inner(url); let path = url.split(['?', '#']).next().unwrap_or(url).to_ascii_lowercase(); if path.ends_with(".pmtiles") { TileKind::PmTiles @@ -178,6 +179,11 @@ pub struct TileLayer { /// Expanded in the Layers pane, its legend and source layers showing. #[serde(default, skip_serializing_if = "is_false")] pub expanded: bool, + /// The saved connection it was added from (its id, in the app's preferences), so a project + /// reopened later logs in with that connection's login and never needs a token in its + /// address. Empty for a layer that came from no connection. Never a secret. + #[serde(default, skip_serializing_if = "String::is_empty")] + pub connection: String, /// Picked in the Layers pane (`map.selectTiles`), for the session only: it holds while no /// map layer is active (the engine's `Session::selected_tile`). #[serde(skip)] @@ -200,7 +206,7 @@ pub fn checked_legends(legends: impl IntoIterator bool { + let url = rustgis_data::login_address::inner(url); let scheme = url.get(..8).unwrap_or(url).to_ascii_lowercase(); scheme.starts_with("https://") || scheme.starts_with("http://") } @@ -370,6 +377,7 @@ impl TileLayer { error: None, feature_info: None, legends: Vec::new(), + connection: String::new(), expanded: false, selected: false, single_image: false, @@ -423,11 +431,11 @@ impl TileLayer { pub fn repair(&mut self) -> Result<(), String> { self.url = self.url.trim().to_string(); // A dated address is a web address whose date is filled in after the scheme and host. - if self.time_url.as_ref().is_some_and(|u| !is_web(u) || u.len() > 4096) { + if self.time_url.as_ref().is_some_and(|u| !is_web(u) || rustgis_data::login_address::inner(u).len() > 4096) { self.time_url = None; } // A file per date: web addresses, finite dates, in order. - self.time_urls.retain(|(d, u)| d.is_finite() && is_web(u) && u.len() <= 4096); + self.time_urls.retain(|(d, u)| d.is_finite() && is_web(u) && rustgis_data::login_address::inner(u).len() <= 4096); // Sorted before the cap, so a list out of order keeps its first dates, not any. self.time_urls.sort_by(|a, b| a.0.total_cmp(&b.0)); self.time_urls.dedup_by(|a, b| a.0 == b.0); @@ -488,9 +496,9 @@ impl TileLayer { pub const UPLOAD: &str = "upload:"; /// Whether a PMTiles or MBTiles archive address is a file rather than a web address. An address -/// read through a saved S3 connection is never a file, well formed or not. +/// wrapped for an S3 connection or service login is never a file, well formed or not. pub fn is_local_archive(url: &str) -> bool { - if rustgis_data::s3_address::is_wrapper(url) { + if rustgis_data::s3_address::is_wrapper(url) || rustgis_data::login_address::is_wrapper(url) { return false; } let lower = url.to_ascii_lowercase(); @@ -517,10 +525,15 @@ pub fn check_path(path: &str, ext: &str) -> Result<(), String> { Ok(()) } -/// Whether `url` may be fetched: HTTP or HTTPS, with a host, and no longer than 4096 bytes. An -/// address read through a saved S3 connection is judged by the address it wraps (still bounded -/// by 4096 bytes), and itself by the wrapper's own bound; it needs no login to be kept. +/// Whether `url` may be fetched: HTTP, HTTPS or S3, with a host, and no longer than 4096 bytes. +/// Connection and service-login wrappers are judged by their inner address and their own bound; +/// no live login is needed to keep a saved address. pub fn check_url(url: &str) -> Result<(), String> { + let url = match rustgis_data::login_address::parse(url) { + Ok(Some(b)) => b.url, + Ok(None) => url, + Err(e) => return Err(e.to_string()), + }; let url = match rustgis_data::s3_address::parse(url) { Ok(Some(b)) => b.url, Ok(None) => url, @@ -542,9 +555,9 @@ pub fn check_url(url: &str) -> Result<(), String> { Ok(()) } -/// The host part of an HTTP(S) address (of the address an address read through a saved S3 -/// connection wraps). +/// The host part of an HTTP(S) address, inspecting the address inside a connection or login wrapper. pub fn host_of(url: &str) -> Option<&str> { + let url = rustgis_data::login_address::parse(url).ok()?.map_or(url, |b| b.url); let url = rustgis_data::s3_address::inner(url); let rest = url.split_once("://")?.1; let host = rest.split(['/', '?', '#']).next()?; @@ -556,6 +569,19 @@ pub fn host_of(url: &str) -> Option<&str> { mod tests { use super::*; + #[test] + fn runtime_bound_tile_and_legend_addresses_are_web_addresses() { + let bound = rustgis_data::login_address::bind("0123456789abcdef", "https://maps.test", "https://maps.test/style.json").unwrap(); + assert!(is_web(&bound)); + assert!(check_url(&bound).is_ok()); + assert_eq!(host_of(&bound), Some("maps.test")); + assert_eq!(TileKind::guess(&bound), TileKind::Style); + let legend = rustgis_data::ogc::Legend { url: bound.clone(), ..Default::default() }; + assert_eq!(checked_legends([legend]).first().unwrap().url, bound); + assert!(check_url("rustgis-login:broken").is_err()); + assert!(!is_web("rustgis-login:broken")); + } + #[test] fn style_layers_are_checked() { assert!(checked_style_layers(r#"[{"id": "a", "type": "fill"}]"#).is_ok()); diff --git a/crates/tiles/src/address.rs b/crates/tiles/src/address.rs index 1212a04d58..753dc6d1fb 100644 --- a/crates/tiles/src/address.rs +++ b/crates/tiles/src/address.rs @@ -113,6 +113,11 @@ fn inet_aton(h: &str) -> Option { /// Whether `url` may be fetched. `from` is where it was found (a document, or the address that /// redirected to it), or `None` when the user gave it. pub fn check(url: &str, from: Option<&str>) -> Result<(), String> { + crate::logins::check(url).map_err(str::to_string)?; + if let Some(bound) = rustgis_data::login_address::parse(url).map_err(str::to_string)? { + return check(bound.url, from.map(crate::logins::inner)); + } + let from = from.map(crate::logins::inner); // An address read through a saved S3 connection is judged by where it is fetched, which its // own endpoint decides (no login is needed to keep it). if let Some(destination) = crate::s3::bound_destination(url)? { diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index 84f4f66dca..51165e76ab 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -171,6 +171,9 @@ pub fn in_flight() -> usize { /// What the cache holds for `url`, fetching it when it holds nothing (or a failure old enough /// to try again). pub fn get(url: &str, want: Want) -> State { + if let Err(e) = crate::logins::check(resource_of(url)) { + return State::Failed(e.to_string()); + } // A map tile of a layer in another system: its service's picture as it comes, or made from // its pictures (#769). if let Some(base) = crate::warp::direct(url) { @@ -217,6 +220,7 @@ pub fn get(url: &str, want: Want) -> State { /// What the cache holds for `url`, without fetching it. pub fn peek(url: &str) -> Option> { + crate::logins::check(resource_of(url)).ok()?; if let Some(base) = crate::warp::direct(url) { return peek(base); } @@ -469,7 +473,7 @@ fn fetch_tile(url: String, host: String, want: Want) { Entry::Ready(Arc::new(p), size) } Err(e) => { - log::debug!("{}: {e}", answer.key); + log::debug!("{}", crate::logins::scrub(&format!("{}: {e}", answer.key))); Entry::Failed(e, Instant::now()) } }); @@ -615,7 +619,8 @@ pub fn download_detailed(url: &str, max: usize, done: DoneDetailed) { // The failure names the address asked for, not the marked one fetched. let asked = url.to_string(); let done: DoneDetailed = Box::new(move |r| { - done(r.map_err(|f| Failure { url: asked, ..f })); + let r = crate::logins::check(&asked).map_err(|e| Failure::new(&asked, e)).and(r); + done(r.map_err(|f| Failure { url: crate::logins::inner(&asked).to_string(), ..f })); finished(); }); let f = fetcher().lock().unwrap_or_else(|e| e.into_inner()).clone(); @@ -698,8 +703,9 @@ pub fn post_json(url: &str, body: String, done: Done) { return; } IN_FLIGHT.fetch_add(1, Ordering::Relaxed); + let key = url.to_string(); let done: Done = Box::new(move |r| { - done(r); + done(crate::logins::check(&key).map_err(str::to_string).and(r)); finished(); }); let p = poster().lock().unwrap_or_else(|e| e.into_inner()).clone(); @@ -763,8 +769,9 @@ pub fn head(url: &str, done: HeadDone) { return; } IN_FLIGHT.fetch_add(1, Ordering::Relaxed); + let key = url.to_string(); let done: HeadDone = Box::new(move |r| { - done(r); + done(crate::logins::check(&key).map_err(str::to_string).and(r)); finished(); }); let h = header().lock().unwrap_or_else(|e| e.into_inner()).clone(); @@ -776,6 +783,12 @@ pub fn head(url: &str, done: HeadDone) { /// Fetches an address's bytes, uncached (through the browser's fetcher, or the worker threads). pub(crate) fn fetch_bytes(url: String, done: Done) { + if let Err(e) = crate::logins::check(resource_of(&url)) { + done(Err(e.to_string())); + return; + } + let key = url.clone(); + let done: Done = Box::new(move |r| done(crate::logins::check(resource_of(&key)).map_err(str::to_string).and(r))); let f = fetcher().lock().unwrap_or_else(|e| e.into_inner()).clone(); match f { Some(f) => f(url, done), @@ -836,6 +849,13 @@ fn finished() { } fn store(url: &str, entry: Entry) { + if crate::logins::check(resource_of(url)).is_err() { + let mut c = lock(); + if let Some(Slot { entry: Entry::Ready(_, n), .. }) = c.slots.remove(url) { + c.bytes = c.bytes.saturating_sub(n); + } + return; + } let mut c = lock(); let added = if let Entry::Ready(_, n) = &entry { *n } else { 0 }; // Dropped while it was fetched (cleared, or evicted): kept anyway, it was asked for. @@ -1001,7 +1021,9 @@ mod native { if crate::mbtiles::is_tile(url) || url.starts_with(LOCAL) { return String::new(); } - let Some((_, rest)) = rustgis_data::s3_address::reached(url).split_once("://") else { return String::new() }; + let Some((_, rest)) = rustgis_data::login_address::reached(rustgis_data::s3_address::reached(url)).split_once("://") else { + return String::new(); + }; let end = rest.find(['/', '?', '#']).unwrap_or(rest.len()); let authority = rest.get(..end).unwrap_or(rest); // Credentials in the address aren't part of the server. @@ -1135,13 +1157,27 @@ mod native { /// Asks for `url`'s headers, following up to 5 redirects, each checked as [`get`] checks /// them. pub(super) fn head_with(agent: &ureq::Agent, url: &str) -> Result { - let prepared = crate::s3::prepare(url)?; + let mut carried_login = false; + head_answer(agent, url, &mut carried_login).map_err(|e| if carried_login { crate::logins::scrub(&e) } else { e }) + } + + fn head_answer(agent: &ureq::Agent, url: &str, carried_login: &mut bool) -> Result { + crate::logins::check(url).map_err(str::to_string)?; + let prepared = crate::s3::prepare(crate::logins::inner(url))?; if prepared.signed { return Ok(super::Head::default()); } let mut at = prepared.url.clone(); + let first_origin = crate::logins::origin(&at); + let mut same_origin = true; for redirects in 0..=5 { - let mut req = agent.head(&at); + let bound = crate::logins::inherit(url, &at); + let login = if same_origin { crate::logins::apply(&bound)? } else { None }; + *carried_login |= login.is_some(); + let mut req = agent.head(login.as_ref().map_or(at.as_str(), |a| a.url.as_str())); + if let Some((name, value)) = login.as_ref().and_then(|a| a.header.as_ref()) { + req = req.header(name.as_str(), value.as_str()); + } if redirects == 0 { for (k, v) in &prepared.headers { req = req.header(k.as_str(), v.as_str()); @@ -1155,12 +1191,14 @@ mod native { if at.starts_with("https://") && !next.starts_with("https://") { return Err(format!("{next}: a redirect from https to http")); } + same_origin &= crate::logins::origin(&next) == first_origin; at = next; continue; } if resp.status().as_u16() >= 400 { return Err(format!("http status: {}", resp.status().as_u16())); } + crate::logins::check(url).map_err(str::to_string)?; return Ok(super::Head { tiff_header_length: header("x-tiff-header-length").and_then(super::tiff_header_length), content_type: header("content-type").map(str::to_string), @@ -1179,16 +1217,25 @@ mod native { /// POSTs a JSON document and reads the answer (at most [`MAX_BYTES`]); an error status is /// `http status: `, and a redirect isn't followed. pub(super) fn post_with(agent: &ureq::Agent, url: &str, body: &str) -> Result, String> { + let mut carried_login = false; + post_answer(agent, url, body, &mut carried_login).map_err(|e| if carried_login { crate::logins::scrub(&e) } else { e }) + } + + fn post_answer(agent: &ureq::Agent, url: &str, body: &str, carried_login: &mut bool) -> Result, String> { use std::io::Read; - let mut resp = agent - .post(url) - .header("Content-Type", "application/json") - .header("Accept", "application/json") - .config() - .http_status_as_error(false) - .build() - .send(body) - .map_err(|e| e.to_string())?; + crate::logins::check(url).map_err(str::to_string)?; + let prepared = crate::s3::prepare(crate::logins::inner(url))?; + if prepared.signed { + return Err("S3 signed JSON POST requests are not supported.".into()); + } + let bound = crate::logins::inherit(url, &prepared.url); + let login = crate::logins::apply(&bound)?; + *carried_login = login.is_some(); + let mut req = agent.post(prepared.url.as_str()).header("Content-Type", "application/json").header("Accept", "application/json"); + if let Some((name, value)) = login.as_ref().and_then(|a| a.header.as_ref()) { + req = req.header(name.as_str(), value.as_str()); + } + let mut resp = req.config().http_status_as_error(false).build().send(body).map_err(|e| e.to_string())?; let status = resp.status().as_u16(); if !(200..300).contains(&status) { return Err(format!("http status: {status}")); @@ -1204,6 +1251,7 @@ mod native { if buf.len() > MAX_BYTES { return Err(format!("larger than {MAX_BYTES} bytes")); } + crate::logins::check(url).map_err(str::to_string)?; Ok(buf) } @@ -1286,7 +1334,10 @@ mod native { /// [`get`], telling what is known of a failure. pub(super) fn get_detailed(agent: &ureq::Agent, url: &str) -> Result, Failure> { - fetch_answer(agent, url, true).map_err(|f| Failure { url: url.to_string(), ..f }) + fetch_answer(agent, url, true).map_err(|f| Failure { + url: if url.contains(rustgis_data::login_address::PREFIX) { crate::logins::scrub(url) } else { url.to_string() }, + ..f + }) } /// A file's length from a `Content-Range` header (`bytes 0-0/1234`); none when the server @@ -1299,6 +1350,19 @@ mod native { /// The answer to `url`; a refusal keeps the server's answer only when `detail` asks for it, /// as tiles a server doesn't have are routine and only their status is read. fn fetch_answer(agent: &ureq::Agent, url: &str, detail: bool) -> Result, Failure> { + let mut carried_login = false; + let result = fetch_answer_inner(agent, url, detail, &mut carried_login); + let result = crate::logins::check(super::resource_of(url)).map_err(Failure::from).and(result); + result.map_err(|mut f| { + if carried_login { + f.message = crate::logins::scrub(&f.message); + f.url = crate::logins::scrub(&f.url); + } + f + }) + } + + fn fetch_answer_inner(agent: &ureq::Agent, url: &str, detail: bool, carried_login: &mut bool) -> Result, Failure> { use std::io::Read; if crate::mbtiles::is_tile(url) { return crate::mbtiles::read_local(url).map_err(Failure::from); @@ -1316,11 +1380,21 @@ mod native { let mut measuring: Option = None; // An `s3://` address becomes its web address, and a request to an S3 connection's // endpoint is signed. - let prepared = crate::s3::prepare(url)?; + crate::logins::check(url)?; + let prepared = crate::s3::prepare(crate::logins::inner(url))?; let mut at = prepared.url.clone(); + let first_origin = crate::logins::origin(&at); + let mut same_origin = true; let mut redirects = 0; let mut resp = loop { - let mut req = agent.get(&at); + let bound = crate::logins::inherit(url, &at); + let login = if !prepared.signed && same_origin { crate::logins::apply(&bound)? } else { None }; + *carried_login |= login.is_some(); + let mut req = agent.get(login.as_ref().map_or(at.as_str(), |a| a.url.as_str())); + let login_header = login.as_ref().and_then(|a| a.header.as_ref()); + if let Some((name, value)) = login_header { + req = req.header(name.as_str(), value.as_str()); + } if let Some((a, b)) = range { req = req.header("Range", &format!("bytes={a}-{b}")); } else if let Some(n) = tail { @@ -1381,10 +1455,9 @@ mod native { } } if status >= 400 { - // A signed request's refusal is told by its status alone: an answer's headers and - // body are the server's text, and a server may echo the request's Authorization - // header (the access key's id) back in it. - return Err(if detail && !prepared.signed { + // A signed or logged-in request's refusal is told by its status alone: a server + // may echo its credentials in the answer's headers or body. + return Err(if detail && !prepared.signed && login.is_none() { refused(&mut resp) } else { Failure::without_answer("", format!("http status: {status}"), Reason::Status(status)) @@ -1416,6 +1489,7 @@ mod native { if redirects > 5 { return Err("too many redirects".into()); } + same_origin &= crate::logins::origin(&next) == first_origin; at = next; }; if resp.status().as_u16() == 204 { @@ -1650,18 +1724,222 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] fn serve_bytes(answers: Vec>) -> String { + serve_recorded(answers).0 + } + + #[cfg(not(target_arch = "wasm32"))] + fn serve_recorded(answers: Vec>) -> (String, std::sync::mpsc::Receiver) { use std::io::{Read, Write}; let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); let addr = listener.local_addr().unwrap(); + let (sent, received) = std::sync::mpsc::channel(); std::thread::spawn(move || { for answer in answers { let Ok((mut stream, _)) = listener.accept() else { return }; let mut buf = [0u8; 2048]; - let _ = stream.read(&mut buf); + let n = stream.read(&mut buf).unwrap_or(0); + let _ = sent.send(String::from_utf8_lossy(&buf[..n]).into_owned()); let _ = stream.write_all(&answer); } }); - format!("http://{addr}") + (format!("http://{addr}"), received) + } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn logins_headers_follow_same_origin_redirects() { + use crate::logins::{self, Method}; + let (base, asked) = serve_recorded( + [ + "HTTP/1.1 302 Found\r\nLocation: /next\r\nContent-Length: 0\r\nConnection: close\r\n\r\n", + "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\ntile", + ] + .into_iter() + .map(|s| s.as_bytes().to_vec()) + .collect(), + ); + let binding = logins::activate(&base, "same-origin", Method::Header("X-Service-Key".into()), "same-origin-secret", true).unwrap(); + let url = logins::bind(&binding, &format!("{base}/start")).unwrap(); + assert_eq!(native::get(&native::agent(), &url).unwrap(), b"tile"); + for request in asked.iter().take(2) { + assert!(request.to_ascii_lowercase().contains("x-service-key: same-origin-secret\r\n")); + } + assert!(logins::deactivate(&binding)); + } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn logins_redirects_never_pick_up_another_origins_login() { + use crate::logins::{self, Method}; + let (other, asked) = serve_recorded(vec![b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\ntile".to_vec()]); + let base = serve(vec![format!("HTTP/1.1 302 Found\r\nLocation: {other}/tile\r\nContent-Length: 0\r\nConnection: close\r\n\r\n")]); + let first = logins::activate(&base, "first", Method::Header("X-Service-Key".into()), "first-secret", true).unwrap(); + let second = logins::activate(&other, "second", Method::Header("X-Service-Key".into()), "second-secret", true).unwrap(); + let url = logins::bind(&first, &format!("{base}/start")).unwrap(); + assert_eq!(native::get(&native::agent(), &url).unwrap(), b"tile"); + let request = asked.recv_timeout(std::time::Duration::from_secs(5)).unwrap().to_ascii_lowercase(); + assert!(!request.contains("x-service-key")); + assert!(!request.contains("first-secret") && !request.contains("second-secret")); + assert!(logins::deactivate(&first)); + assert!(logins::deactivate(&second)); + } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn logins_head_and_post_carry_the_login() { + use crate::logins::{self, Method}; + let answer = b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".to_vec(); + let (base, asked) = serve_recorded(vec![answer.clone(), answer]); + let binding = logins::activate(&base, "head-post", Method::Header("X-Service-Key".into()), "head/post secret", true).unwrap(); + let url = logins::bind(&binding, &format!("{base}/data?key=a%2Fb")).unwrap(); + let agent = native::agent(); + native::head_with(&agent, &url).unwrap(); + assert!(native::post_with(&agent, &url, "{}").unwrap().is_empty()); + for (method, request) in ["HEAD", "POST"].into_iter().zip(asked.iter().take(2)) { + assert!(request.starts_with(&format!("{method} /data?key=a%2Fb HTTP/1.1\r\n")), "{request}"); + assert!(request.to_ascii_lowercase().contains("x-service-key: head/post secret\r\n")); + } + assert!(logins::deactivate(&binding)); + } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn live_connection_bindings_separate_requests_and_cache_entries() { + use crate::logins::{self, Method}; + let answer = b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n{}".to_vec(); + let (base, asked) = serve_recorded(vec![answer.clone(), answer.clone(), answer]); + let raw = format!("{base}/same"); + let a = logins::activate(&base, "session-a/connection-a", Method::Header("X-Key".into()), "a", true).unwrap(); + let b = logins::activate(&base, "session-b/connection-b", Method::Header("X-Key".into()), "b", true).unwrap(); + let ua = logins::bind(&a, &raw).unwrap(); + let ub = logins::bind(&b, &raw).unwrap(); + for url in [&ua, &ub, &raw] { + native::get(&native::agent(), url).unwrap(); + } + let requests: Vec<_> = asked.iter().take(3).map(|r| r.to_ascii_lowercase()).collect(); + assert!(requests[0].contains("x-key: a\r\n")); + assert!(requests[1].contains("x-key: b\r\n")); + assert!(!requests[2].contains("x-key:")); + store(&ua, Entry::Ready(Arc::new(Payload::Json(serde_json::json!({"a": 1}))), 64)); + assert!(peek(&ua).is_some()); + assert!(peek(&ub).is_none() && peek(&raw).is_none()); + assert!(logins::deactivate(&a)); + assert!(peek(&ua).is_none()); + assert!(matches!(get(&ua, Want::Json), State::Failed(_))); + let replacement = logins::activate(&base, "session-a/connection-a", Method::Header("X-Key".into()), "new", true).unwrap(); + assert!(logins::apply(&ua).is_err()); + assert!(logins::apply(&ub).unwrap().is_some()); + assert!(logins::deactivate(&replacement)); + assert!(logins::deactivate(&b)); + } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn logout_during_pending_download_discards_old_authenticated_body() { + use crate::logins::{self, Method}; + use std::io::{Read, Write}; + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let binding = + logins::activate(&base, "pending-session/connection", Method::Header("X-Key".into()), "pending-secret", true).unwrap(); + let url = logins::bind(&binding, &format!("{base}/pending")).unwrap(); + let (started_tx, started_rx) = std::sync::mpsc::channel(); + let (release_tx, release_rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let (mut stream, _) = listener.accept().unwrap(); + let mut buf = [0u8; 2048]; + let n = stream.read(&mut buf).unwrap(); + assert!(String::from_utf8_lossy(&buf[..n]).to_ascii_lowercase().contains("x-key: pending-secret")); + started_tx.send(()).unwrap(); + release_rx.recv_timeout(std::time::Duration::from_secs(5)).unwrap(); + stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 8\r\nConnection: close\r\n\r\nold-body").unwrap(); + }); + let worker_url = url.clone(); + let worker = std::thread::spawn(move || native::get(&native::agent(), &worker_url)); + started_rx.recv_timeout(std::time::Duration::from_secs(5)).unwrap(); + assert!(logins::deactivate(&binding)); + let replacement = + logins::activate(&base, "pending-session/connection", Method::Header("X-Key".into()), "replacement-secret", true).unwrap(); + let replacement_url = logins::bind(&replacement, &format!("{base}/pending")).unwrap(); + assert_ne!(url, replacement_url); + release_tx.send(()).unwrap(); + assert!(worker.join().unwrap().is_err()); + store(&url, Entry::Ready(Arc::new(Payload::Json(serde_json::json!({"old": true}))), 64)); + assert!(peek(&url).is_none()); + assert!(matches!(get(&url, Want::Json), State::Failed(_))); + assert!(peek(&replacement_url).is_none()); + assert!(logins::apply(&replacement_url).unwrap().is_some()); + assert!(logins::deactivate(&replacement)); + } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn derived_style_tilejson_sprite_and_feature_requests_carry_the_binding() { + use crate::{ + logins::{self, Method}, + source, + style::Source, + template, + }; + let style = serde_json::json!({ + "version": 8, "sprite": "./sprite", + "sources": {"roads": {"type": "vector", "url": "./tiles.json"}}, "layers": [] + }); + let tilejson = serde_json::json!({"tiles": ["./tile/{z}/{x}/{y}.pbf"]}); + let answer = + |body: String| format!("HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()).into_bytes(); + let (base, asked) = serve_recorded(vec![ + answer(style.to_string()), + answer(tilejson.to_string()), + answer("{}".into()), + answer("{}".into()), + answer("{}".into()), + ]); + let binding = + logins::activate(&base, "derived-style", Method::Header("X-Service-Key".into()), "derived-style-secret", true).unwrap(); + let url = logins::bind(&binding, &format!("{base}/style.json")).unwrap(); + let tilejson_url = template::join(&url, "./tiles.json"); + for address in [&url, &tilejson_url] { + let bytes = native::get(&native::agent(), address).unwrap(); + let json = serde_json::from_slice(&bytes).unwrap(); + store(address, Entry::Ready(Arc::new(Payload::Json(json)), bytes.len())); + } + let spec = source::Spec { + kind: source::Kind::Style, + url: url.clone(), + subdomains: vec![], + min_zoom: 0, + max_zoom: 22, + tile_size: 256, + tms: false, + attribution: String::new(), + layers: vec![], + style_layers: String::new(), + hidden_layers: vec![], + }; + let source::Resolve::Ready(resolved) = source::resolve(&spec) else { panic!("style must resolve from fetched documents") }; + let Source::Vector { tiles, .. } = &resolved.style.sources[0].1 else { panic!("vector source") }; + let tile = template::expand(&tiles[0], template::TileId { z: 0, x: 0, y: 0 }, 256, false, &[]); + native::get(&native::agent(), &tile).unwrap(); + let sprite = resolved.sprite.as_ref().unwrap(); + native::get(&native::agent(), &format!("{sprite}.json")).unwrap(); + let info = rustgis_data::ogc::FeatureInfo { + url: template::join(&url, "./info"), + layer: "roads".into(), + version: "1.3.0".into(), + formats: vec!["application/json".into()], + ..Default::default() + }; + native::get(&native::agent(), &info.request(0.0, 0.0, 10.0).unwrap()).unwrap(); + for request in asked.iter().take(5) { + assert!(request.to_ascii_lowercase().contains("x-service-key: derived-style-secret\r\n")); + assert!(!request.contains("rustgis-login:")); + } + assert!(logins::deactivate(&binding)); + assert!(matches!(source::resolve(&spec), source::Resolve::Failed(_))); + assert!(matches!(crate::sprite::load(sprite, false), crate::sprite::Load::Failed)); + assert!(crate::pmtiles::info(&url).is_err()); + assert!(crate::cog::info(&url).is_err()); } /// The last bytes from a store that serves ranges but answers `bytes=-n` with 501 (GitHub's diff --git a/crates/tiles/src/cog.rs b/crates/tiles/src/cog.rs index 3b8cd6ed40..3407ad80d8 100644 --- a/crates/tiles/src/cog.rs +++ b/crates/tiles/src/cog.rs @@ -249,6 +249,7 @@ pub(crate) fn forget_file(url: &str) { /// A file's info: `Ok(None)` while it is being read (asking for it starts the read). pub fn info(url: &str) -> Result>, String> { + crate::logins::check(url).map_err(str::to_string)?; let url = split(url).0; { let i = infos().lock().unwrap_or_else(|e| e.into_inner()); @@ -264,6 +265,10 @@ pub fn info(url: &str) -> Result>, String> { } fn with_info(url: &str, then: Box, String>) + Send>) { + if let Err(e) = crate::logins::check(url) { + then(Err(e.to_string())); + return; + } { let mut i = infos().lock().unwrap_or_else(|e| e.into_inner()); match i.get_mut(url) { @@ -292,6 +297,10 @@ fn with_info(url: &str, then: Box, String>) + Send>) load( url.to_string(), Box::new(move |r| { + let r = crate::logins::check(&key) + .map_err(str::to_string) + .and(r) + .map_err(|e| if rustgis_data::login_address::is_wrapper(&key) { crate::logins::scrub(&e) } else { e }); let waiting = { let mut i = infos().lock().unwrap_or_else(|e| e.into_inner()); let next = match &r { @@ -761,6 +770,10 @@ fn fetch_all(info: Arc, url: String, level: usize, wanted: Vec, then: /// One tile of a level, decoded: from the tiles kept, or read. fn get_tile(info: Arc, url: String, level: usize, idx: u32, then: Box) + Send>) { + if let Err(e) = crate::logins::check(&url) { + then(Err(e.to_string())); + return; + } let Some(l) = info.layout.levels.get(level) else { then(Err("no such overview".into())); return; @@ -807,6 +820,7 @@ fn get_tile(info: Arc, url: String, level: usize, idx: u32, then: Box) -> Self { let message = message.into(); + let message = if message.contains(rustgis_data::login_address::PREFIX) { crate::logins::scrub(&message) } else { message }; let reason = crate::cache::http_status(&message).filter(|s| *s >= 400).map_or(Reason::Other, Reason::Status); - Failure { url: url.to_string(), message, reason, answer: None } + let url = if url.contains(rustgis_data::login_address::PREFIX) { crate::logins::scrub(url) } else { url.to_string() }; + Failure { url, message, reason, answer: None } } /// A failure that never reached an answer. @@ -87,7 +89,7 @@ impl Failure { } let html = kept.iter().any(|(k, v)| k == "content-type" && v.to_ascii_lowercase().contains("html")); Failure { - url: url.to_string(), + url: crate::logins::inner(url).to_string(), // As ureq has always said it, so text that reads the status back still finds it. message: format!("http status: {status}"), reason: Reason::Status(status), @@ -297,6 +299,14 @@ fn entities(s: &str) -> String { mod tests { use super::*; + #[test] + fn inactive_runtime_bindings_are_removed_from_failure_fields() { + let bound = rustgis_data::login_address::bind("0123456789abcdef", "https://failure.test", "https://failure.test/tile").unwrap(); + let failure = Failure::new(&bound, format!("failed to fetch {bound}")); + assert!(!failure.url.contains("rustgis-login:") && !failure.url.contains("0123456789abcdef")); + assert!(!failure.message.contains("rustgis-login:") && !failure.message.contains("0123456789abcdef")); + } + fn h(pairs: &[(&str, &str)]) -> Vec<(String, String)> { pairs.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect() } diff --git a/crates/tiles/src/lib.rs b/crates/tiles/src/lib.rs index 1254569a93..99a24154b7 100644 --- a/crates/tiles/src/lib.rs +++ b/crates/tiles/src/lib.rs @@ -19,6 +19,7 @@ pub mod earthdata; pub mod embeddings; pub mod failure; pub mod geocode; +pub mod logins; pub mod mbtiles; pub mod mvt; pub mod nldi; diff --git a/crates/tiles/src/logins.rs b/crates/tiles/src/logins.rs new file mode 100644 index 0000000000..0f3ad60c13 --- /dev/null +++ b/crates/tiles/src/logins.rs @@ -0,0 +1,506 @@ +//! Session-only web-service logins carried by explicit, secret-free request bindings. +//! Bare addresses never acquire a service login. Each activation creates a fresh generation; +//! the owning session revokes its token on replacement, logout and drop. + +use std::{ + collections::BTreeMap, + fmt, + sync::{Mutex, MutexGuard, OnceLock}, +}; + +use base64::Engine; +use rustgis_data::secrets::Secret; + +/// Maximum header name length, in bytes. +pub const MAX_NAME: usize = 64; +/// Maximum encoded credential header length, in bytes (including `Basic `). +pub const MAX_VALUE: usize = 4096; + +pub use rustgis_data::login_address::origin; + +/// An RFC 7230 token, excluding transport, browser-managed and proxy/security headers. +pub fn valid_header_name(name: &str) -> bool { + if name.is_empty() || name.len() > MAX_NAME || !name.bytes().all(|b| b.is_ascii_alphanumeric() || b"!#$%&'*+-.^_`|~".contains(&b)) { + return false; + } + let blocked = [ + "host", + "connection", + "content-length", + "transfer-encoding", + "te", + "upgrade", + "keep-alive", + "range", + "accept-encoding", + "cookie", + "origin", + "referer", + "user-agent", + ]; + !blocked.iter().any(|s| name.eq_ignore_ascii_case(s)) + && !name.get(..6).is_some_and(|s| s.eq_ignore_ascii_case("proxy-")) + && !name.get(..4).is_some_and(|s| s.eq_ignore_ascii_case("sec-")) +} + +/// A nonempty credential of at most [`MAX_VALUE`] bytes, without control characters. +pub fn valid_value(value: &str) -> bool { + !value.is_empty() && value.len() <= MAX_VALUE && !value.chars().any(char::is_control) +} + +/// An HTTP Basic authorization value, using standard padded Base64. +pub fn basic(user: &str, password: &str) -> String { + let input = format!("{user}:{password}"); + let mut result = String::with_capacity(6 + input.len().div_ceil(3) * 4); + result.push_str("Basic "); + base64::engine::general_purpose::STANDARD.encode_string(input, &mut result); + result +} + +/// Whether raw Basic inputs produce an allowed encoded header, without allocating it. +pub fn valid_basic(user: &str, password: &str) -> bool { + !user.contains(':') + && !user.chars().chain(password.chars()).any(char::is_control) + && user + .len() + .checked_add(password.len()) + .and_then(|n| n.checked_add(1)) + .is_some_and(|n| n.div_ceil(3).saturating_mul(4).saturating_add(6) <= MAX_VALUE) +} + +/// How a connection credential is sent. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Method { + Header(String), +} + +// Deliberately no Debug: neither credentials nor their derived redaction patterns are printable. +struct Entry { + owner: String, + origin: String, + method: Method, + value: Secret, + redactions: Vec, +} + +struct Redaction { + value: Secret, + encoded: bool, +} + +fn entries() -> MutexGuard<'static, BTreeMap> { + static ENTRIES: OnceLock>> = OnceLock::new(); + ENTRIES.get_or_init(Mutex::default).lock().unwrap_or_else(|e| e.into_inner()) +} + +/// 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 { + 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."); + } + if origin.starts_with("http://") && !insecure { + return Err("An HTTP login requires explicit permission to send credentials insecurely."); + } + let Method::Header(name) = &method; + if !valid_header_name(name) { + return Err("The login header name is not allowed."); + } + if !valid_value(value) { + return Err("The login credential must be nonempty, within the size limit, and contain no controls."); + } + let mut redactions = Vec::with_capacity(4); + add_redactions(&mut redactions, value); + if let Some(payload) = value.get(..6).filter(|s| s.eq_ignore_ascii_case("Basic ")).and_then(|_| value.get(6..)) + && !payload.is_empty() + { + add_redactions(&mut redactions, payload); + } + let mut entries = entries(); + // 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()?; + if !entries.contains_key(&binding) { + entries.insert( + binding.clone(), + Entry { owner: owner.to_string(), origin: origin.to_string(), method, value: Secret::new(value), redactions }, + ); + return Ok(binding); + } + } + Err("The service login binding could not be created.") +} + +/// 128 unpredictable bits as 32 lower-case hex digits. +fn random_token() -> Result { + let mut bytes = [0u8; 16]; + fill_random(&mut bytes)?; + Ok(format!("{:032x}", u128::from_be_bytes(bytes))) +} + +#[cfg(not(target_arch = "wasm32"))] +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); + if encoded != value { + redactions.push(Redaction { value: Secret::new(encoded), encoded: true }); + } +} + +/// Revokes exactly one login generation; another session's bindings are untouched. +pub fn deactivate(binding: &str) -> bool { + let removed = entries().remove(binding).is_some(); + if removed { + crate::cache::clear(); + } + removed +} + +pub fn owner(binding: &str) -> Option { + entries().get(binding).map(|entry| entry.owner.clone()) +} + +pub fn is_active(binding: &str) -> bool { + entries().contains_key(binding) +} + +pub use rustgis_data::login_address::inner; + +const REVOKED: &str = "The service login is no longer active. Sign in to this connection again."; + +/// Binds a same-origin address to a live login generation. +pub fn bind(binding: &str, url: &str) -> Result { + let entries = entries(); + let entry = entries.get(binding).ok_or(REVOKED)?; + let url = inner(url); + if origin(url).as_deref() != Some(entry.origin.as_str()) { + return Err("The service login does not cover this address."); + } + rustgis_data::login_address::bind(binding, &entry.origin, url) +} + +pub use rustgis_data::login_address::inherit; + +/// Checks runtime authority before any cache (including archive/style/sprite caches) is read. +pub fn check(address: &str) -> Result<(), &'static str> { + let address = + if rustgis_data::login_address::is_wrapper(address) { address } else { address.split_once('@').map_or(address, |(_, a)| a) }; + let Some(b) = rustgis_data::login_address::parse(address)? else { return Ok(()) }; + let entries = entries(); + let entry = entries.get(b.binding).ok_or(REVOKED)?; + if entry.origin != b.origin || origin(b.url).as_deref() != Some(b.origin) { + return Err("The service login does not cover this address."); + } + Ok(()) +} + +/// A request-only URL and optional header. Debug never reveals either field. +pub struct Applied { + pub url: String, + pub header: Option<(String, String)>, +} + +impl fmt::Debug for Applied { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("Applied()") + } +} + +/// Prepares only an explicitly bound login; bare URLs are anonymous. +pub fn apply(address: &str) -> Result, &'static str> { + let Some(b) = rustgis_data::login_address::parse(address)? else { return Ok(None) }; + let entries = entries(); + let entry = entries.get(b.binding).ok_or(REVOKED)?; + if entry.origin != b.origin || origin(b.url).as_deref() != Some(b.origin) { + return Err("The service login does not cover this address."); + } + let Method::Header(name) = &entry.method; + Ok(Some(Applied { url: b.url.to_string(), header: Some((name.clone(), entry.value.expose().to_string())) })) +} + +fn unreserved(b: u8) -> bool { + b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b'~') +} + +fn percent_encode(value: &str) -> String { + let mut result = String::with_capacity(value.len()); + append_percent_encoded(&mut result, value); + result +} + +fn append_percent_encoded(result: &mut String, value: &str) { + for b in value.bytes() { + if unreserved(b) { + result.push(char::from(b)); + } else { + result.push('%'); + let digit = |n| char::from(if n < 10 { b'0' + n } else { b'A' + n - 10 }); + result.push(digit(b >> 4)); + result.push(digit(b & 15)); + } + } +} + +/// Redacts active raw credentials, percent-encoded credentials (with either hex case), and +/// the bare Base64 payload of Basic credentials. Matching the original input in one pass +/// prevents overlapping values or a credential resembling `[redacted]` from leaking secrets. +pub fn scrub(text: &str) -> String { + let entries = entries(); + let mut patterns: Vec<_> = entries.values().flat_map(|entry| &entry.redactions).collect(); + patterns.sort_unstable_by_key(|pattern| std::cmp::Reverse(pattern.value.expose().len())); + // Strip capabilities even after revocation, when there are no active credential patterns. + let mut result = String::with_capacity(text.len()); + let mut rest = text; + while !rest.is_empty() { + if let Some(wrapper) = rest.strip_prefix(rustgis_data::login_address::PREFIX) { + // The hex token and length-prefixed origin precede the ordinary URL. + let url = wrapper + .split_once(':') + .and_then(|(_, r)| r.split_once(':')) + .and_then(|(len, r)| len.parse::().ok().and_then(|n| r.get(n..))) + .and_then(|r| r.strip_prefix(':')); + if let Some(url) = url.filter(|u| u.starts_with("http://") || u.starts_with("https://")) { + rest = url; + continue; + } + result.push_str("[redacted]"); + rest = wrapper.split_once(char::is_whitespace).map_or("", |(_, r)| r); + continue; + } + if let Some(pattern) = patterns.iter().find(|pattern| { + let value = pattern.value.expose(); + rest.get(..value.len()) + .is_some_and(|prefix| if pattern.encoded { encoded_matches(prefix.as_bytes(), value.as_bytes()) } else { prefix == value }) + }) { + result.push_str("[redacted]"); + rest = rest.get(pattern.value.expose().len()..).unwrap_or_default(); + } else if let Some(c) = rest.chars().next() { + result.push(c); + rest = rest.get(c.len_utf8()..).unwrap_or_default(); + } + } + result +} + +fn encoded_matches(actual: &[u8], expected: &[u8]) -> bool { + let mut actual = actual.iter(); + let mut expected = expected.iter(); + while let Some(b) = expected.next() { + if actual.next() != Some(b) { + return false; + } + if *b == b'%' { + for _ in 0..2 { + match (actual.next(), expected.next()) { + (Some(a), Some(b)) if a.eq_ignore_ascii_case(b) => {} + _ => return false, + } + } + } + } + actual.next().is_none() +} + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + + #[test] + fn canonical_origins() { + for (url, expected) in [ + ("HTTPS://Example.COM.:443/a?b=c#d", "https://example.com"), + ("HTTP://Example.COM.:80/a", "http://example.com"), + ("https://Example.COM:00443", "https://example.com"), + ("https://Example.COM.:8443/", "https://example.com:8443"), + ("https://[2001:0DB8::1]:443/", "https://[2001:db8::1]"), + ("http://localhost:8080", "http://localhost:8080"), + ] { + assert_eq!(origin(url).as_deref(), Some(expected)); + } + } + + #[test] + fn refuses_unsafe_origins() { + for url in [ + "https://user:password@example.com", + "https://@example.com", + "https://example.com/a b", + " https://example.com", + "https://example.com/\n", + "https://example.com/\u{a0}", + "ftp://example.com", + "https://", + "https:///path", + "https://:443", + "https://example.com:", + "https://example.com:65536", + "https://example.com:-1", + "https://example.com:abc", + "https://example.com\\other", + "https://example.com/a\\b", + "https://[bad]:443", + "https://[::1]extra", + "https://host:443:80", + "https://example.com%2fother", + "https://example..com", + ] { + assert_eq!(origin(url), None, "{url:?}"); + } + } + + #[test] + fn validates_names_and_values() { + assert!(valid_header_name("Authorization")); + assert!(valid_header_name("X-API_Key!")); + assert!(valid_header_name(&"a".repeat(MAX_NAME))); + for name in [ + "", + "Host", + "CONNECTION", + "content-length", + "Transfer-Encoding", + "TE", + "Upgrade", + "Keep-Alive", + "Range", + "Accept-Encoding", + "Cookie", + "Origin", + "Referer", + "User-Agent", + "Proxy-Authorization", + "SEC-Fetch-Site", + "x y", + "x:y", + "ümlaut", + ] { + assert!(!valid_header_name(name), "{name}"); + } + assert!(!valid_header_name(&"a".repeat(MAX_NAME + 1))); + assert!(valid_value(" a password with spaces é ")); + assert!(valid_value(&"a".repeat(MAX_VALUE))); + for value in ["", "bad\rvalue", "bad\nvalue", "bad\tvalue", "bad\0value", "bad\u{7f}value", "bad\u{85}value"] { + assert!(!valid_value(value)); + } + assert!(!valid_value(&"é".repeat(MAX_VALUE / 2 + 1))); + } + + #[test] + fn standard_basic_authorization() { + assert_eq!(basic("Aladdin", "open sesame"), "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="); + assert_eq!(basic("", ""), "Basic Og=="); + assert_eq!(basic("user", "p:a:ss"), "Basic dXNlcjpwOmE6c3M="); + } + + #[test] + fn http_requires_permission_and_bindings_reject_scheme_host_and_port_changes() { + assert!(activate("http://insecure-login.test", "http", Method::Header("X-Key".into()), "short", false).is_err()); + let binding = activate("https://scoped-login.test", "scope", Method::Header("X-Key".into()), "scope-secret", false).unwrap(); + let bound = bind(&binding, "https://scoped-login.test/tile").unwrap(); + for url in ["http://scoped-login.test/tile", "https://scoped-login.test:444/tile", "https://foreign.test/tile"] { + assert!(bind(&binding, url).is_err()); + assert_eq!(inherit(&bound, url), url); + assert!(apply(url).unwrap().is_none()); + } + assert!(is_active(&binding)); + assert!(deactivate(&binding)); + assert!(!is_active(&binding)); + } + + #[test] + fn bindings_isolate_connections_and_revoke_generations() { + let target = "https://binding-login.example"; + let a = activate(target, "session-a/connection", Method::Header("X-Key".into()), "a", false).unwrap(); + let b = activate(target, "session-b/connection", Method::Header("X-Key".into()), "b", false).unwrap(); + let ua = bind(&a, &format!("{target}/tile")).unwrap(); + let ub = bind(&b, &format!("{target}/tile")).unwrap(); + assert_eq!(apply(&ua).unwrap().unwrap().header.unwrap().1, "a"); + assert_eq!(apply(&ub).unwrap().unwrap().header.unwrap().1, "b"); + assert!(apply(target).unwrap().is_none()); + assert!(bind(&a, "https://other.example/tile").is_err()); + assert_eq!(inherit(&ua, "https://other.example/tile"), "https://other.example/tile"); + assert!(deactivate(&a)); + assert!(check(&ua).is_err()); + assert!(apply(&ua).is_err()); + assert!(apply(&ub).unwrap().is_some()); + assert!(deactivate(&b)); + } + + #[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)); + } + } + + #[test] + fn encoded_basic_limit_and_short_credentials() { + assert!(valid_basic("", "")); + assert!(valid_basic("a", "b")); + assert!(!valid_basic("a:b", "c")); + assert!(!valid_basic("a", "\n")); + for n in [3064, 3065, 3066, 4096] { + let password = "p".repeat(n); + assert_eq!(valid_basic("", &password), valid_value(&basic("", &password))); + } + } + + #[test] + fn scrubs_header_and_basic_payload() { + let value = basic("scrub-user", "scrub-password"); + let binding = activate("https://scrub-login.example", "scrub", Method::Header("Authorization".into()), &value, false).unwrap(); + let payload = value.strip_prefix("Basic ").unwrap(); + assert_eq!(scrub(&format!("{value} {payload} {}", percent_encode(&value))), "[redacted] [redacted] [redacted]"); + let url = bind(&binding, "https://scrub-login.example/tile").unwrap(); + assert!(!scrub(&format!("failed: {url}")).contains(rustgis_data::login_address::PREFIX)); + assert_eq!(format!("{:?}", apply(&url).unwrap().unwrap()), "Applied()"); + assert!(deactivate(&binding)); + let message = scrub(&format!("failed: {url}")); + assert!(!message.contains(rustgis_data::login_address::PREFIX) && !message.contains(&binding)); + } +} diff --git a/crates/tiles/src/pmtiles.rs b/crates/tiles/src/pmtiles.rs index cb66086334..90ea8ee01c 100644 --- a/crates/tiles/src/pmtiles.rs +++ b/crates/tiles/src/pmtiles.rs @@ -171,6 +171,9 @@ pub fn is_tile(url: &str) -> bool { /// (As `rustgis_map::tiles::is_local_archive` decides it, which a layer's address went through.) /// An address read through a saved S3 connection is never a file, well formed or not. pub fn is_local(archive: &str) -> bool { + if rustgis_data::login_address::is_wrapper(archive) { + return false; + } if rustgis_data::s3_address::is_wrapper(archive) { return false; } @@ -410,6 +413,7 @@ pub(crate) fn forget_archive(archive: &str) { /// An archive's header and metadata: `Ok(None)` while they are read (the map draws again once /// they arrive). pub fn info(archive: &str) -> Result>, String> { + crate::logins::check(archive).map_err(str::to_string)?; let mut all = archives().lock().unwrap_or_else(|e| e.into_inner()); match all.get(archive) { Some(State::Ready(i)) => return Ok(Some(i.clone())), @@ -434,6 +438,10 @@ pub fn info_blocking(archive: &str, timeout: std::time::Duration) -> Result, String>) + Send>) { + if let Err(e) = crate::logins::check(archive) { + f(Err(e.to_string())); + return; + } let mut all = archives().lock().unwrap_or_else(|e| e.into_inner()); match all.get_mut(archive) { Some(State::Ready(i)) => { @@ -492,7 +500,11 @@ fn load(archive: String) { } fn finish(archive: &str, r: Result) { - let r = r.map(Arc::new); + let r = crate::logins::check(archive) + .map_err(str::to_string) + .and(r) + .map(Arc::new) + .map_err(|e| if rustgis_data::login_address::is_wrapper(archive) { crate::logins::scrub(&e) } else { e }); let waiting = { let mut all = archives().lock().unwrap_or_else(|e| e.into_inner()); let old = match &r { diff --git a/crates/tiles/src/schedule.rs b/crates/tiles/src/schedule.rs index f44d32c5b7..984c5623cf 100644 --- a/crates/tiles/src/schedule.rs +++ b/crates/tiles/src/schedule.rs @@ -36,7 +36,9 @@ pub(crate) fn host(url: &str) -> String { if crate::mbtiles::is_tile(url) { return String::new(); } - let Some((_, rest)) = rustgis_data::s3_address::reached(url).split_once("://") else { return String::new() }; + let Some((_, rest)) = rustgis_data::login_address::reached(rustgis_data::s3_address::reached(url)).split_once("://") else { + return String::new(); + }; let end = rest.find(['/', '?', '#']).unwrap_or(rest.len()); let authority = rest.get(..end).unwrap_or(rest); // Credentials in the address aren't part of the server. diff --git a/crates/tiles/src/source.rs b/crates/tiles/src/source.rs index 923a2b481d..18707dfc08 100644 --- a/crates/tiles/src/source.rs +++ b/crates/tiles/src/source.rs @@ -144,6 +144,9 @@ pub const DEFAULT_STYLE: &str = include_str!("default_style.json"); /// The style to draw `spec` with, once the documents it needs have been fetched. pub fn resolve(spec: &Spec) -> Resolve { + if let Err(e) = crate::logins::check(&spec.url) { + return Resolve::Failed(e.to_string()); + } let key = spec.key(); // A PMTiles archive on this computer is a path, not a web address. let local = spec.kind == Kind::PmTiles && pmtiles::is_local(&spec.url); @@ -213,7 +216,7 @@ fn resolve_anew(spec: &Spec, key: String) -> Resolve { } } Ok(None) => return Resolve::Pending, - Err(e) => return Resolve::Failed(format!("{}: {e}", spec.url)), + Err(e) => return Resolve::Failed(format!("{}: {e}", crate::logins::inner(&spec.url))), }, Kind::Cog => match cog::info(&spec.url) { Ok(Some(info)) => { @@ -253,7 +256,7 @@ fn resolve_anew(spec: &Spec, key: String) -> Resolve { }; let mut style = match Style::parse(&doc) { Ok(s) => s, - Err(e) => return Resolve::Failed(format!("{}: {e}", spec.url)), + Err(e) => return Resolve::Failed(format!("{}: {e}", crate::logins::inner(&spec.url))), }; if !spec.hidden_layers.is_empty() { style.layers.retain(|l| l.source_layer.as_ref().is_none_or(|s| !spec.hidden_layers.contains(s))); @@ -286,6 +289,9 @@ fn resolve_anew(spec: &Spec, key: String) -> Resolve { .map(|s| template::join(&base, s)) .filter(|s| address::check(s, Some(&base)).is_ok()); let r = Arc::new(Resolved { style, attribution, sprite, left_out, made: Mutex::new(Instant::now()), waiting }); + if let Err(e) = crate::logins::check(&spec.url) { + return Resolve::Failed(e.to_string()); + } let mut all = resolved().lock().unwrap_or_else(|e| e.into_inner()); // A few layers are in use at a time: past that many, start again rather than grow. if all.len() >= MAX_RESOLVED { @@ -429,10 +435,10 @@ fn fetch_json(url: &str) -> Result, String> { match cache::get(url, Want::Json) { State::Ready(p) => match &*p { Payload::Json(v) => Ok(Some(v.clone())), - _ => Err(format!("{url}: not a JSON document")), + _ => Err(format!("{}: not a JSON document", crate::logins::inner(url))), }, State::Pending => Ok(None), - State::Failed(e) => Err(format!("{url}: {e}")), + State::Failed(e) => Err(format!("{}: {e}", crate::logins::inner(url))), } } diff --git a/crates/tiles/src/sprite.rs b/crates/tiles/src/sprite.rs index 2f42229d12..29996f2b6b 100644 --- a/crates/tiles/src/sprite.rs +++ b/crates/tiles/src/sprite.rs @@ -153,6 +153,9 @@ fn with_suffix(base: &str, suffix: &str) -> String { /// The sprite at `base` (an address without `.json`/`.png`), at high density when `hi_dpi` /// and the style has one, else at 1×. pub fn load(base: &str, hi_dpi: bool) -> Load { + if crate::logins::check(base).is_err() { + return Load::Failed; + } if hi_dpi { match load_one(base, "@2x") { Load::Failed => {} @@ -172,6 +175,9 @@ fn load_one(base: &str, density: &str) -> Load { match (index, sheet) { (State::Ready(i), State::Ready(p)) => match (&*i, &*p) { (Payload::Json(index), Payload::Image(sheet)) => { + if crate::logins::check(base).is_err() { + return Load::Failed; + } let s = Arc::new(Sprite::new(index, sheet.clone())); let mut all = sprites().lock().unwrap_or_else(|p| p.into_inner()); if all.len() > 16 { diff --git a/crates/tiles/src/stac.rs b/crates/tiles/src/stac.rs index c282c821ed..55ff6b7998 100644 --- a/crates/tiles/src/stac.rs +++ b/crates/tiles/src/stac.rs @@ -105,13 +105,15 @@ pub struct Search { pub fn api(url: Option<&str>) -> Result { let Some(u) = url.map(str::trim).filter(|u| !u.is_empty()) else { return Ok(EARTH_SEARCH.to_string()) }; crate::address::check(u, None)?; - let u = u.split(['?', '#']).next().unwrap_or(u).trim_end_matches('/'); - Ok(u.strip_suffix("/search").or_else(|| u.strip_suffix("/collections")).unwrap_or(u).to_string()) + let inner = crate::logins::inner(u); + let inner = inner.split(['?', '#']).next().unwrap_or(inner).trim_end_matches('/'); + let inner = inner.strip_suffix("/search").or_else(|| inner.strip_suffix("/collections")).unwrap_or(inner); + Ok(crate::logins::inherit(u, inner)) } /// The request for an API's collections. pub fn collections_url(api: &str) -> String { - format!("{api}/collections") + crate::template::join(api, &format!("{}/collections", crate::logins::inner(api))) } /// The request for a search (STAC API's `GET /search`). @@ -147,11 +149,12 @@ pub fn search_url(api: &str, s: &Search) -> Result { }; q.push(format!("sortby={}", encode(&format!("{sign}{field}")))); } - Ok(format!("{api}/search?{}", q.join("&"))) + Ok(crate::template::join(api, &format!("{}/search?{}", crate::logins::inner(api), q.join("&")))) } /// Whether an address is a web address. fn is_web(u: &str) -> bool { + let u = crate::logins::inner(u); let l = u.get(..8).unwrap_or(u).to_ascii_lowercase(); l.starts_with("https://") || l.starts_with("http://") } @@ -256,15 +259,16 @@ pub fn parse_matched(bytes: &[u8]) -> Option { /// `/collections/`, else its `root` link's (a static catalog's `….json` root is no API), else the /// collection its `parent` link names. `None` when nothing says. pub fn api_of(bytes: &[u8], from: &str) -> Option { - let before = |u: &str| u.split_once("/collections/").map(|(a, _)| a.to_string()); + let before = |u: &str| crate::logins::inner(u).split_once("/collections/").map(|(a, _)| crate::logins::inherit(u, a)); if let Some(a) = before(from) { return Some(a); } let v = json(bytes).ok()?; if let Some(root) = link(&v, "root", from) { - let r = root.split(['?', '#']).next().unwrap_or(&root).trim_end_matches('/').to_string(); - if !r.to_ascii_lowercase().ends_with(".json") && is_web(&r) { - return Some(r); + let inner = crate::logins::inner(&root); + let r = inner.split(['?', '#']).next().unwrap_or(inner).trim_end_matches('/'); + if !r.to_ascii_lowercase().ends_with(".json") && is_web(r) { + return Some(crate::logins::inherit(&root, r)); } } link(&v, "parent", from).and_then(|p| before(&p)) @@ -437,6 +441,14 @@ fn footprint_size(g: &Value) -> usize { /// An address relative to another, as a browser would resolve it (`s3://` and others are kept /// as they are, so they can be refused by name). pub fn resolve(base: &str, href: &str) -> Option { + // A login binding rides along only to an address on the base's own origin (`inherit`). + let plain_base = rustgis_data::login_address::parse(base).ok()?.map_or(base, |b| b.url); + rustgis_data::login_address::parse(href).ok()?; + let resolved = resolve_plain(plain_base, crate::logins::inner(href))?; + Some(crate::logins::inherit(base, &resolved)) +} + +fn resolve_plain(base: &str, href: &str) -> Option { let href = href.trim(); if href.is_empty() || href.len() > MAX_TEXT || href.chars().any(char::is_control) { return None; @@ -475,14 +487,14 @@ impl Asset { /// Whether it is a GeoTIFF (cloud-optimised or not). pub fn is_geotiff(&self) -> bool { let t = self.media_type.to_ascii_lowercase(); - let path = self.href.split(['?', '#']).next().unwrap_or("").to_ascii_lowercase(); + let path = crate::logins::inner(&self.href).split(['?', '#']).next().unwrap_or("").to_ascii_lowercase(); t.contains("geotiff") || t.starts_with("image/tiff") || path.ends_with(".tif") || path.ends_with(".tiff") } /// Whether it is a LiDAR point cloud (LAS, LAZ or COPC). pub fn is_point_cloud(&self) -> bool { let t = self.media_type.to_ascii_lowercase(); - let path = self.href.split(['?', '#']).next().unwrap_or("").to_ascii_lowercase(); + let path = crate::logins::inner(&self.href).split(['?', '#']).next().unwrap_or("").to_ascii_lowercase(); t.contains("vnd.las") || t.contains("laszip") || path.ends_with(".laz") || path.ends_with(".las") } } @@ -498,7 +510,7 @@ impl Item { }); } // Among those on the web first (others are refused by name when nothing else is there). - let web = |a: &&Asset| a.href.to_ascii_lowercase().starts_with("http"); + let web = |a: &&Asset| is_web(&a.href); let choose = |only_web: bool| { let ok = |a: &&Asset| !only_web || web(a); self.assets @@ -517,7 +529,7 @@ impl Item { pub fn thumbnail(&self) -> Option<&Asset> { let picture = |a: &&Asset| { let t = a.media_type.to_ascii_lowercase(); - let path = a.href.split(['?', '#']).next().unwrap_or("").to_ascii_lowercase(); + let path = crate::logins::inner(&a.href).split(['?', '#']).next().unwrap_or("").to_ascii_lowercase(); is_web(&a.href) && (t.starts_with("image/png") || t.starts_with("image/jpeg") @@ -535,7 +547,7 @@ impl Item { /// may point to, not one that needs signing or a cloud store's own protocol. pub fn asset_href(&self, key: Option<&str>) -> Result<(String, String), String> { let a = self.pick(key)?; - let lower = a.href.to_ascii_lowercase(); + let lower = crate::logins::inner(&a.href).to_ascii_lowercase(); if !(lower.starts_with("https://") || lower.starts_with("http://") || crate::s3::is_s3(&lower)) { let scheme = lower.split("://").next().unwrap_or(""); return Err(format!( @@ -553,6 +565,7 @@ impl Item { /// The host of a web address, lower-cased. fn host_of(url: &str) -> String { + let url = crate::logins::inner(url); let rest = url.split_once("://").map(|(_, r)| r).unwrap_or(url); let authority = rest.split(['/', '?', '#']).next().unwrap_or(""); let host = authority.rsplit_once('@').map(|(_, h)| h).unwrap_or(authority); @@ -562,7 +575,7 @@ fn host_of(url: &str) -> String { /// Whether an asset's address is in Azure Blob Storage without a signature: a Microsoft /// Planetary Computer asset, read with a token from its token service ([`sas_token_url`]). pub fn needs_signing(href: &str) -> bool { - let lower = href.to_ascii_lowercase(); + let lower = crate::logins::inner(href).to_ascii_lowercase(); if !lower.starts_with("https://") || !host_of(&lower).ends_with(".blob.core.windows.net") { return false; } @@ -573,7 +586,7 @@ pub fn needs_signing(href: &str) -> bool { /// The request for a token that reads `href`'s storage container (the Planetary Computer's /// `token/{account}/{container}`). pub fn sas_token_url(href: &str) -> Result { - let lower = href.to_ascii_lowercase(); + let lower = crate::logins::inner(href).to_ascii_lowercase(); let host = host_of(&lower); let account = host.strip_suffix(".blob.core.windows.net").unwrap_or(""); let path = lower.split_once("://").map(|(_, r)| r).unwrap_or(""); @@ -603,12 +616,14 @@ pub fn parse_sas(bytes: &[u8]) -> Result { /// An address with a token's query added. pub fn sign(href: &str, token: &str) -> String { - let (base, frag) = href.split_once('#').map(|(b, f)| (b, Some(f))).unwrap_or((href, None)); + let inner = crate::logins::inner(href); + let (base, frag) = inner.split_once('#').map(|(b, f)| (b, Some(f))).unwrap_or((inner, None)); let sep = if base.contains('?') { "&" } else { "?" }; - match frag { + let signed = match frag { Some(f) => format!("{base}{sep}{token}#{f}"), None => format!("{base}{sep}{token}"), - } + }; + crate::logins::inherit(href, &signed) } fn tokens() -> &'static std::sync::Mutex> { @@ -709,6 +724,33 @@ pub fn footprints_geojson(items: &[Item]) -> Value { mod tests { use super::*; + #[test] + fn bound_stac_api_catalog_and_assets_follow_only_same_origin_links() { + let origin = "https://stac-bound.test"; + // A binding is authority only while its runtime owns it, so install a live one. + let token = crate::logins::activate(origin, "test-owner", crate::logins::Method::Header("X-Key".into()), "secret", false).unwrap(); + let base = rustgis_data::login_address::bind(&token, origin, "https://stac-bound.test/catalog.json").unwrap(); + let document = br#"{"type":"Catalog","links":[ + {"rel":"child","href":"collection.json"}, + {"rel":"child","href":"https://other.test/collection.json"}]}"#; + let children = catalog::parse_children(document, &base).unwrap(); + assert!(children.iter().any(|c| rustgis_data::login_address::parse(&c.url).unwrap().is_some())); + assert!(children.iter().any(|c| c.url == "https://other.test/collection.json")); + let item = br#"{"type":"Feature","id":"x","properties":{},"geometry":null, + "assets":{"data":{"href":"data.tif","type":"image/tiff"}}, + "links":[{"rel":"self","href":"items/x.json"}]}"#; + let item = parse_item(item, &base).unwrap(); + assert_eq!(rustgis_data::login_address::parse(&item.assets[0].href).unwrap().unwrap().binding, token); + let api = api(Some(&rustgis_data::login_address::bind(&token, origin, "https://stac-bound.test/v1/search").unwrap())).unwrap(); + for url in [collections_url(&api), search_url(&api, &Search::default()).unwrap()] { + assert_eq!(rustgis_data::login_address::parse(&url).unwrap().unwrap().binding, token); + } + for foreign in ["http://stac-bound.test/tile", "https://stac-bound.test:444/tile", "https://other.test/tile"] { + assert_eq!(resolve(&base, foreign).as_deref(), Some(foreign)); + } + crate::logins::deactivate(&token); + } + #[test] fn api_of_a_linked_document() { // The address before /collections/. diff --git a/crates/tiles/src/stac/catalog.rs b/crates/tiles/src/stac/catalog.rs index c7a11a4410..afb6252bcc 100644 --- a/crates/tiles/src/stac/catalog.rs +++ b/crates/tiles/src/stac/catalog.rs @@ -5,6 +5,7 @@ //! cloud cover and phase. Like the rest of [`super`], this is sans I/O: [`Walk`] says what to //! read next and takes the answers. Documents are hostile input: the walk is bounded in documents //! read, depth, links followed and items kept. +//! A fetched document's login binding follows only links on its canonical origin. use std::collections::{HashSet, VecDeque}; @@ -27,6 +28,7 @@ const MAX_CHILDREN: usize = 2000; /// Whether an address names a static catalog's document (a `.json` file) rather than a STAC API. pub fn is_static(url: &str) -> bool { + let url = crate::logins::inner(url); let path = url.split(['?', '#']).next().unwrap_or(url).trim_end_matches('/').to_ascii_lowercase(); path.ends_with(".json") } @@ -41,6 +43,7 @@ pub struct Child { /// A label for a document without a title: its folder's name for a `catalog.json` or /// `collection.json`, else its file's name without `.json`. pub fn label_of(url: &str) -> String { + let url = crate::logins::inner(url); let path = url.split(['?', '#']).next().unwrap_or(url).trim_end_matches('/'); let mut parts = path.rsplit('/'); let file = parts.next().unwrap_or(""); @@ -396,7 +399,7 @@ fn collection_item(v: &Value, from: &str) -> Option { let roles: Vec<&str> = a.get("roles").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str).collect(); let t = a.get("type").and_then(Value::as_str).unwrap_or("").to_ascii_lowercase(); let href = a.get("href").and_then(Value::as_str).unwrap_or(""); - let path = href.split(['?', '#']).next().unwrap_or("").to_ascii_lowercase(); + let path = crate::logins::inner(href).split(['?', '#']).next().unwrap_or("").to_ascii_lowercase(); // Data RustGIS can add (by its media type or extension), or an asset that says it is data. const TYPES: [&str; 9] = ["geotiff", "image/tiff", "parquet", "pmtiles", "flatgeobuf", "geo+json", "vnd.las", "laszip", "zarr"]; const EXTENSIONS: [&str; 11] = diff --git a/crates/tiles/src/template.rs b/crates/tiles/src/template.rs index 99fa48c911..279915067a 100644 --- a/crates/tiles/src/template.rs +++ b/crates/tiles/src/template.rs @@ -200,6 +200,12 @@ pub fn expand(template: &str, t: TileId, tile_size: u32, tms: bool, subdomains: /// Resolves `href` against the document at `base` (for addresses inside a style or TileJSON). pub fn join(base: &str, href: &str) -> String { + let href = crate::logins::inner(href); + match rustgis_data::login_address::parse(base) { + Ok(Some(bound)) => return crate::logins::inherit(base, &join(bound.url, href)), + Err(_) => return base.to_string(), + Ok(None) => {} + } if href.contains("://") { return href.to_string(); } @@ -250,6 +256,22 @@ fn remove_dots(path: &str) -> String { mod tests { use super::*; + #[test] + fn derived_links_keep_only_the_document_binding() { + let base = rustgis_data::login_address::bind("0123456789abcdef", "https://a.test", "https://a.test/style.json").unwrap(); + for href in ["tiles/{z}/{x}/{y}.pbf", "/sprite", "https://A.test:443/info"] { + let next = join(&base, href); + assert_eq!(rustgis_data::login_address::parse(&next).unwrap().unwrap().binding, "0123456789abcdef"); + } + for href in ["http://a.test/tile", "https://a.test:444/tile", "https://other.test/tile"] { + assert_eq!(join(&base, href), href); + } + let supplied = rustgis_data::login_address::bind("fedcba9876543210", "https://a.test", "https://a.test/tile").unwrap(); + assert_eq!(join("https://a.test/style.json", &supplied), "https://a.test/tile"); + assert_eq!(rustgis_data::login_address::parse(&join(&base, &supplied)).unwrap().unwrap().binding, "0123456789abcdef"); + assert_eq!(join("rustgis-login:broken", "/tile"), "rustgis-login:broken"); + } + #[test] fn joins_relative_addresses() { let base = "https://x.org/a/b/styles/root.json"; From 7d19423dbade5233c4ecc503e0b8c5bbf0f7e8f8 Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Mon, 2 Nov 2026 12:11:17 +0100 Subject: [PATCH 02/19] feat(connections): session-only logins for web-service connections Saved WMS, WMTS, WFS, WCS, OGC API, ArcGIS REST, CSW and STAC connections can log in with HTTP Basic or an API key in a header. The connection keeps only the method, user and header name; the secret lasts for the session. connection.login takes password/apiKey and allowInsecure for plain http://; connection.logout ends it. Layers record their connection id in the project and ask for a missing connection or login instead of picking one silently. --- crates/engine/src/cmd/connection_browse.rs | 60 +- crates/engine/src/cmd/connection_logins.rs | 8 +- crates/engine/src/cmd/connections.rs | 214 +++++- crates/engine/src/cmd/cube.rs | 6 +- crates/engine/src/cmd/items.rs | 12 +- crates/engine/src/cmd/layer.rs | 26 +- crates/engine/src/cmd/plugins.rs | 15 +- crates/engine/src/cmd/service.rs | 57 +- crates/engine/src/cmd/service_group.rs | 149 +++- crates/engine/src/cmd/stac.rs | 112 ++- crates/engine/src/cmd/tiles.rs | 71 +- crates/engine/src/connections.rs | 79 +++ crates/engine/src/lib.rs | 22 +- crates/engine/src/project.rs | 223 +++++- crates/engine/src/service_logins.rs | 748 +++++++++++++++++++++ crates/engine/src/sources.rs | 10 +- crates/engine/src/tests.rs | 17 +- crates/engine/src/tests/service_logins.rs | 441 ++++++++++++ 18 files changed, 2160 insertions(+), 110 deletions(-) create mode 100644 crates/engine/src/service_logins.rs create mode 100644 crates/engine/src/tests/service_logins.rs diff --git a/crates/engine/src/cmd/connection_browse.rs b/crates/engine/src/cmd/connection_browse.rs index 01584d33ce..1c86d07fff 100644 --- a/crates/engine/src/cmd/connection_browse.rs +++ b/crates/engine/src/cmd/connection_browse.rs @@ -205,6 +205,10 @@ pub(super) fn browse(s: &mut Session, p: &Value) -> CmdResult { let level = level(&c, node)?; let conn = s3_login(s, &c, &level)?; let url = fetch_url(s, &c, &level, conn.as_ref())?; + let url = match url { + Some(url) if crate::service_logins::is_service(&c) => Some(crate::service_logins::request(s, Some(&c.id), &url)?), + other => other, + }; let mut out = json!({"connection": c.id, "node": node}); if b(p, "probe").unwrap_or(false) { out["fetch"] = json!(url); @@ -213,7 +217,7 @@ pub(super) fn browse(s: &mut Session, p: &Value) -> CmdResult { let found = match &level { Level::Database => database_schemas(s, &c)?, Level::DatabaseSchema { schema } => database_schema(s, &c, schema)?, - Level::Service { url, kind } => service(s, p, &mut out, url, kind.as_deref())?, + Level::Service { url, kind } => service(s, p, &mut out, &c, url, kind.as_deref())?, Level::S3Root if url.is_none() => { buckets(c.options.get("buckets").map(String::as_str).unwrap_or("").split(',').map(str::to_string)) } @@ -240,7 +244,16 @@ pub(super) fn browse(s: &mut Session, p: &Value) -> CmdResult { } } }; - out["children"] = json!(found.children); + let mut children = found.children; + for child in &mut children { + public_addresses(child); + } + if crate::service_logins::is_service(&c) { + for child in &mut children { + name_connection(child, &c.id, 0); + } + } + out["children"] = json!(children); if found.truncated { out["truncated"] = json!(true); } @@ -250,6 +263,42 @@ pub(super) fn browse(s: &mut Session, p: &Value) -> CmdResult { Ok(out) } +/// Browse candidates persist public addresses plus a connection id, not a live capability. +fn public_addresses(value: &mut Value) { + match value { + Value::String(text) => { + if let Some((prefix, tail)) = text.split_once(rustgis_data::login_address::PREFIX) { + let wrapped = format!("{}{tail}", rustgis_data::login_address::PREFIX); + if let Ok(Some(bound)) = rustgis_data::login_address::parse(&wrapped) { + *text = format!("{prefix}{}", bound.url); + } + } + } + Value::Array(values) => values.iter_mut().for_each(public_addresses), + Value::Object(values) => values.values_mut().for_each(public_addresses), + _ => {} + } +} + +/// Names the connection in the add commands a listed child (and its children) offers, so the +/// layers they add record it and are read with its login. +fn name_connection(child: &mut Value, id: &str, depth: usize) { + if depth > 4 { + return; + } + let command = child.get("add").and_then(|a| a.get("command")).and_then(Value::as_str).unwrap_or(""); + if crate::service_logins::ADD_COMMANDS.contains(&command) + && let Some(params) = child.get_mut("add").and_then(|a| a.get_mut("params")).and_then(Value::as_object_mut) + { + params.insert("connection".into(), json!(id)); + } + if let Some(list) = child.get_mut("children").and_then(Value::as_array_mut) { + for c in list { + name_connection(c, id, depth + 1); + } + } +} + /// A live database's schemas, each browsed separately so one large schema cannot hide later ones. fn database_schemas(s: &mut Session, c: &Connection) -> Result { let tables = super::connection_use::tables(s, c, None)?; @@ -305,8 +354,8 @@ fn schema_tables(tables: &[Value], schema: &str, address: &str) -> Found { } /// A service's layers (capabilities read here, or the app's `answer` and `documents`). -fn service(s: &mut Session, p: &Value, out: &mut Value, url: &str, kind: Option<&str>) -> Result { - let mut q = json!({"url": url}); +fn service(s: &mut Session, p: &Value, out: &mut Value, c: &Connection, url: &str, kind: Option<&str>) -> Result { + let mut q = json!({"url": crate::service_logins::request(s, Some(&c.id), url)?, "connection": c.id}); if let Some(k) = kind { q["type"] = json!(k); } @@ -382,7 +431,8 @@ fn service(s: &mut Session, p: &Value, out: &mut Value, url: &str, kind: Option< /// A catalogue's records, each with the services and files it links to. fn catalogue(s: &mut Session, c: &Connection, text: &str, out: &mut Value) -> Result { - let r = super::super::service::catalog_search(s, &json!({"url": c.address, "text": text}))?; + let url = crate::service_logins::request(s, Some(&c.id), &c.address)?; + let r = super::super::service::catalog_search(s, &json!({"url": url, "connection": c.id, "text": text}))?; if let Some(n) = r.get("notes") { out["notes"] = n.clone(); } diff --git a/crates/engine/src/cmd/connection_logins.rs b/crates/engine/src/cmd/connection_logins.rs index b69012f2e3..71d96aa63d 100644 --- a/crates/engine/src/cmd/connection_logins.rs +++ b/crates/engine/src/cmd/connection_logins.rs @@ -41,18 +41,19 @@ pub(super) fn request_password(s: &mut Session, c: &Connection) { key, connections: vec![c.id.clone()], layers, + tiles: 0, opened: s.opened, prompted: false, }); } } -/// Resolves every unopened database of the project: a project was opened or imported. +/// Resolves every unopened database of the project, and the web-service logins its layers read +/// with: a project was opened or imported. pub(crate) fn resolve_project(s: &mut Session) { - #[cfg(target_arch = "wasm32")] - let _ = s; #[cfg(not(target_arch = "wasm32"))] resolve(s, None); + crate::service_logins::resolve(s, None); } /// Resolves only the databases `keys` names: a saved definition or a single layer change can only @@ -100,6 +101,7 @@ fn resolve(s: &mut Session, only: Option<&[(&'static str, String)]>) { key: key.clone(), connections: ids, layers: layers.clone(), + tiles: 0, opened: s.opened, prompted: false, }); diff --git a/crates/engine/src/cmd/connections.rs b/crates/engine/src/cmd/connections.rs index 1ebe809452..68337a4a12 100644 --- a/crates/engine/src/cmd/connections.rs +++ b/crates/engine/src/cmd/connections.rs @@ -9,6 +9,7 @@ use super::{b, ps}; use crate::connections::{Connection, DATABASE_KINDS, DESKTOP_ONLY, KINDS}; use crate::render::rustgis_tiles::{s3, stac}; use crate::s3_keys as keys; +use crate::service_logins as web; use crate::{CmdResult, CommandSpec as C, Session}; #[path = "connection_browse.rs"] @@ -28,15 +29,18 @@ pub fn commands() -> Vec { r#"{"kind"?: "postgres"|"mysql"|"sqlserver"|"service"|"wms"|"wmts"|"wfs"|"wcs"|"ogcapi"|"arcgis"|"csw"|"stac"|"s3", "group"?: text}"#, ), C::new("connection.new", "New Connection", "Home › Add › Data Sources", new).view().params( - r#"{"kind": "postgres"|"mysql"|"sqlserver"|"service"|"wms"|"wmts"|"wfs"|"wcs"|"ogcapi"|"arcgis"|"csw"|"stac"|"s3", "address"?: "as the kind's add command takes it, without its password or token (postgres:host=… dbname=… user=…, mysql://user@host/db, mssql://user@host/db, a service's or STAC API's https:// address, an S3 endpoint: AWS when absent)", "name"?: "unique (default from the address)", "group"?: text, "options"?: {"region"?: text, "buckets"?: "a,b", "credentials"?: "keys"|"profile"|"environment"|"none", "profile"?: text} (S3 only)}"#, + r#"{"kind": "postgres"|"mysql"|"sqlserver"|"service"|"wms"|"wmts"|"wfs"|"wcs"|"ogcapi"|"arcgis"|"csw"|"stac"|"s3", "address"?: "as the kind's add command takes it, without its password or token (postgres:host=… dbname=… user=…, mysql://user@host/db, mssql://user@host/db, a service's or STAC API's https:// address, an S3 endpoint: AWS when absent)", "name"?: "unique (default from the address)", "group"?: text, "options"?: {"region"?: text, "buckets"?: "a,b", "credentials"?: "keys"|"profile"|"environment"|"none", "profile"?: text} (S3 only), "auth"?: "none"|"basic"|"header": how a web-service connection logs in (basic authentication or an API key in a header; the password or key is given to connection.login, never kept here), "user"?: "the basic login's user name", "keyName"?: "the header the API key goes in, e.g. X-API-Key"}"#, ), C::new("connection.edit", "Edit Connection", "Home › Add › Data Sources", edit).view().params( - r#"{"id": "a connection's id or name", "name"?: text, "group"?: text, "address"?: text, "options"?: "as connection.new (replaces them)", "kind"?: "its kind (unchanged)"}"#, + r#"{"id": "a connection's id or name", "name"?: text, "group"?: text, "address"?: text, "options"?: "as connection.new (replaces them)", "auth"?: "as connection.new", "user"?: text, "keyName"?: text, "kind"?: "its kind (unchanged)"} (another origin, login method, user or key name ends the web connection's session login)"#, ), C::new("connection.duplicate", "Duplicate Connection", "Home › Add › Data Sources", duplicate).view().params(r#"{"id": "a connection's id or name", "name"?: text}"#), - C::new("connection.remove", "Remove Connection", "Home › Add › Data Sources", remove).view().params(r#"{"id": "a connection's id or name"}"#), + C::new("connection.remove", "Remove Connection", "Home › Add › Data Sources", remove).view().params(r#"{"id": "a connection's id or name"} (deletes its saved database password or S3 keys; ends its web-service session login)"#), C::new("connection.login", "Log In to Connection", "Home › Add › Data Sources", login_command).view().params( - r#"{"id": "a connection's id or name; blank credentials explicitly use this ID's session or saved credentials", "password"?: "a database password, session-only by default", "accessKeyId"?: text, "secretAccessKey"?: text, "sessionToken"?: "temporary S3 credentials, never persisted", "remember"?: "boolean, default false: opt into this computer's credential store for a newly typed database password or complete S3 key pair (saved keys mode only). No credentials are stored in connections or projects; a failed save preserves the session login"}"#, + r#"{"id": "a connection's id or name; blank credentials explicitly use this ID's session credentials (or saved credentials for database/S3 connections)", "password"?: "a database password, or a web service's basic-authentication password", "apiKey"?: "a web service's API key (auth header)", "allowInsecure"?: "boolean, default false: confirms sending a web service's login to its plain http:// address, unencrypted", "accessKeyId"?: text, "secretAccessKey"?: text, "sessionToken"?: "temporary S3 credentials, never persisted", "remember"?: "boolean, default false: opt into this computer's credential store for a newly typed database password or complete S3 key pair (saved keys mode only). Web-service credentials are session-only and reject remember:true. No credentials are stored in connections or projects; a failed database/S3 save preserves the session login"} (a web service's login is sent only with this session's requests bound to that connection and only to its address's origin: the same scheme, host and port; logging in to one of several connections chooses it for unassigned requests to that address)"#, + ), + C::new("connection.logout", "Log Out of Connection", "Home › Add › Data Sources", logout).view().params( + r#"{"id": "a web-service connection's id or name"} (clears this session's login and its password or key, which is no longer sent)"#, ), C::new("connection.forgetSavedKeys", "Forget Saved Keys", "Home › Add › Data Sources", forget_saved_keys).view().params( r#"{"id": "a saved S3 connection's id or name"} (deletes stored keys; a login in this session keeps working)"#, @@ -46,7 +50,7 @@ pub fn commands() -> Vec { ), C::new("connection.forgetPassword", "Forget Saved Password", "Home › Add › Data Sources", forget_password).view().params(r#"{"id": "a database connection's id or name"}"#), C::new("connection.test", "Test Connection", "Home › Add › Data Sources", test).view().params( - r#"{"id"?: "a connection's id or name", "kind"?: "postgres"|"mysql"|"sqlserver"|"service"|"wms"|"wmts"|"wfs"|"wcs"|"ogcapi"|"arcgis"|"csw"|"stac"|"s3", "name"?: text, "group"?: text, "address"?: text, "options"?: "an unsaved connection, as connection.new (when no id)", "password"?: "for a named database, authentication refusal restores prior session credentials; a later listing failure keeps the new password; never persisted", "accessKeyId"?: text, "secretAccessKey"?: text, "sessionToken"?: "as connection.login (logs in first)", "probe"?: "true: only the address the test reads, {fetch} (null for databases, which their driver tests)", "answer"?: "non-database kinds only: what that address returned, when the app fetched it (no network request then); databases ignore answer and test through their driver"}"#, + r#"{"id"?: "a connection's id or name", "kind"?: "postgres"|"mysql"|"sqlserver"|"service"|"wms"|"wmts"|"wfs"|"wcs"|"ogcapi"|"arcgis"|"csw"|"stac"|"s3", "name"?: text, "group"?: text, "address"?: text, "options"?: "an unsaved connection, as connection.new (when no id)", "auth"?: text, "user"?: text, "keyName"?: "as connection.new (when no id)", "password"?: "for a named database, authentication refusal restores prior session credentials; a later listing failure keeps the new password; never persisted. A saved web service's: as connection.login (logs in first)", "apiKey"?: "a saved web service's, as connection.login (logs in first)", "allowInsecure"?: "as connection.login", "accessKeyId"?: text, "secretAccessKey"?: text, "sessionToken"?: "as connection.login (logs in first)", "probe"?: "true: only the address the test reads, {fetch} (null for databases, which their driver tests)", "answer"?: "non-database kinds only: what that address returned, when the app fetched it (no network request then); databases ignore answer and test through their driver"}"#, ), C::new("connection.browse", "Browse Connection", "Home › Add › Data Sources", browse::browse).view().params( r#"{"id": "a connection's id or name", "node"?: "a child's node, as a listing gives it (the connection's top level when absent)", "probe"?: "true: only the address this level reads, {fetch} (null: read without a fetch, or by the database driver); an S3 connection logs in first, as connection.test does", "answer"?: "what that address returned, when the app fetched it", "documents"?: [{"url": "a further document a listing asked for (next)", "text": "its answer"}]} → {connection, node, children: [{key, name, what, detail?, node? (browse it), children? (listed already), add? {command, params}: the command that adds it}], truncated?, matched?, notes?, service? {url, type}, next? (fetch it, then ask again with answer and documents)}. Database roots list schemas; open a schema node to list its tables."#, @@ -57,7 +61,13 @@ pub fn commands() -> Vec { fn shown(s: &Session, c: &Connection) -> Value { let database = DATABASE_KINDS.contains(&c.kind.as_str()); let available = !database || !cfg!(target_arch = "wasm32"); - let logged_in = if c.kind == "s3" { s.has_s3_login(&c.id) } else { database && s.database_logins.known_password(&c.id).is_some() }; + let logged_in = if c.kind == "s3" { + s.has_s3_login(&c.id) + } else if web::is_service(c) { + web::logged_in(s, c) + } else { + database && s.database_logins.known_password(&c.id).is_some() + }; let add_with = "connection.browse {id}, then {id, node} for a child's node; run a dataset's add {command, params}"; let mut out = serde_json::to_value(c).unwrap_or(Value::Null); out["available"] = json!(available); @@ -70,7 +80,17 @@ fn shown(s: &Session, c: &Connection) -> Value { if c.kind == "s3" { out["savedKeys"] = json!(c.saved_keys); } - out["remembered"] = json!(c.remembered); + if web::is_service(c) { + if let Some(out) = out.as_object_mut() { + out.remove("remembered"); + } + } else { + out["remembered"] = json!(c.remembered); + } + // A web login over plain http:// is sent only once confirmed (`allowInsecure`). + if !c.auth.is_empty() && c.address.get(..7).is_some_and(|s| s.eq_ignore_ascii_case("http://")) { + out["insecure"] = json!(true); + } out["addWith"] = json!(add_with); out } @@ -88,6 +108,25 @@ fn options(p: &Value) -> Result, String> { .collect() } +/// The login method's fields a command gives (`auth`, `user`, `keyName`), each text. +fn login_fields(p: &Value, c: &mut Connection) -> Result<(), String> { + for (key, field) in [("auth", &mut c.auth), ("user", &mut c.user), ("keyName", &mut c.key_name)] { + if let Some(value) = p.get(key) { + *field = value.as_str().ok_or_else(|| format!("{key} is text"))?.into(); + } + } + Ok(()) +} + +/// Logs the project's layers read from these connections' origins in again (a connection was +/// added, changed or removed: what is clear for each origin may have changed). +fn resolve_origins(s: &mut Session, cs: &[&Connection]) { + let origins: std::collections::BTreeSet = cs.iter().filter_map(|c| crate::connections::login_origin(c)).collect(); + if !origins.is_empty() { + web::resolve(s, Some(&origins)); + } +} + /// Saved database connections, in list order, with how many project layers wait for their login. pub(crate) fn waiting(s: &Session) -> Vec<(Connection, usize)> { #[cfg(not(target_arch = "wasm32"))] @@ -150,14 +189,19 @@ fn list(s: &mut Session, p: &Value) -> CmdResult { } out }).collect::>(), - "loginRequests": s.database_logins.requests(s.opened).map(|r| json!({ - "reason":r.reason.name(), "database":crate::connections::database_label(r.kind, &r.key), - "connections":r.connections, "layers":r.layers.len() - })).collect::>() })) + "loginRequests": s.database_logins.requests(s.opened).map(|r| { + let mut out = json!({"reason":r.reason.name(), "connections":r.connections, "layers":r.layers.len() + r.tiles}); + if r.kind == crate::connections::WEB { + out["origin"] = json!(r.key); + } else { + out["database"] = json!(crate::connections::database_label(r.kind, &r.key)); + } + out + }).collect::>() })) } fn new(s: &mut Session, p: &Value) -> CmdResult { - let c = Connection { + let mut c = Connection { kind: ps(p, "kind").ok_or_else(|| format!("kind is required ({})", KINDS.join(", ")))?.trim().into(), address: ps(p, "address").unwrap_or("").into(), name: ps(p, "name").unwrap_or("").into(), @@ -165,9 +209,11 @@ fn new(s: &mut Session, p: &Value) -> CmdResult { options: p.get("options").map(options).transpose()?.unwrap_or_default(), ..Connection::default() }; + login_fields(p, &mut c)?; let explicit = !c.name.trim().is_empty(); let (c, stripped) = s.connections.add(c, explicit)?; resolve_keys(s, &address_keys(&c.address)); + resolve_origins(s, &[&c]); Ok(shown_stripped(s, &c, stripped)) } @@ -204,9 +250,12 @@ fn edit(s: &mut Session, p: &Value) -> CmdResult { if let Some(v) = p.get("options") { c.options = options(v)?; } + login_fields(p, &mut c)?; // Validate everything, then locate the commit target before touching the store. let (mut prepared, stripped) = s.connections.prepare_update(&old.id, c)?; let database_changed = DATABASE_KINDS.contains(&old.kind.as_str()) && old.address != prepared.address; + // A web login is for one origin, method, user and key name. + let login_changed = web::is_service(&old) && !crate::connections::same_login(&old, &prepared); let credentials_changed = database_changed || (old.kind == "s3" && (endpoint_changed(&old, &prepared) @@ -222,6 +271,12 @@ fn edit(s: &mut Session, p: &Value) -> CmdResult { if database_changed { clear_identity(s, &old.id); } + if login_changed { + web::forget(s, &old); + } + if web::is_service(&c) && (login_changed || old.address != c.address) { + resolve_origins(s, &[&old, &c]); + } if old.address != c.address { // Only the two databases this definition names can change: another database's layers // waiting for a login keep their request, and an unrelated down server is not retried. @@ -248,6 +303,7 @@ fn duplicate(s: &mut Session, p: &Value) -> CmdResult { // A copy has no saved keys: `add` starts it without any. let (c, _) = s.connections.add(c, explicit)?; resolve_keys(s, &address_keys(&c.address)); + resolve_origins(s, &[&c]); Ok(shown(s, &c)) } @@ -256,6 +312,9 @@ fn remove(s: &mut Session, p: &Value) -> CmdResult { if DATABASE_KINDS.contains(&c.kind.as_str()) || c.kind == "s3" { delete_account(&mut s.database_logins, &c)?; } + if web::is_service(&c) { + web::forget(s, &c); + } if DATABASE_KINDS.contains(&c.kind.as_str()) { clear_identity(s, &c.id); } @@ -263,6 +322,7 @@ fn remove(s: &mut Session, p: &Value) -> CmdResult { // The removed definition named one database: only its layers can change. The requests naming // it went with `clear_identity`. resolve_keys(s, &address_keys(&c.address)); + resolve_origins(s, &[&c]); if c.kind == "s3" { s3::disconnect(&c.name); s.s3_bindings.remove(&c.id); @@ -283,6 +343,16 @@ fn forget_saved_keys(s: &mut Session, p: &Value) -> CmdResult { Ok(shown(s, &c)) } +/// Ends this session's login of a web-service connection and clears its transient secret. +fn logout(s: &mut Session, p: &Value) -> CmdResult { + let c = found(s, p)?.clone(); + if !web::is_service(&c) { + return Err(format!("{} connections don't log out: only web-service connections do", c.kind)); + } + web::logout(s, &c); + Ok(shown(s, &c)) +} + fn delete_account(logins: &mut crate::connections::DatabaseLogins, c: &Connection) -> Result<(), String> { let (marked, error) = if c.kind == "s3" { (c.saved_keys, keys::NOT_FORGOTTEN) @@ -416,6 +486,7 @@ fn reopen(s: &mut Session, c: &Connection, saved: bool, session: bool, all_unope key: key.1.clone(), connections: vec![c.id.clone()], layers, + tiles: 0, opened: s.opened, prompted: false, }); @@ -570,6 +641,9 @@ fn login(s: &mut Session, c: &Connection, p: &Value) -> Result return Ok(result); } } + if web::is_service(c) { + return service_login(s, c, p, remember); + } if c.kind != "s3" { return Err(format!("{} connections don't log in yet", c.kind)); } @@ -667,7 +741,15 @@ fn login(s: &mut Session, c: &Connection, p: &Value) -> Result fn login_command(s: &mut Session, p: &Value) -> CmdResult { let c = found(s, p)?.clone(); + // Logging in is the user's answer to a request waiting at this origin (the login ends it). + let answers = web::is_service(&c) && web::answers(s, &c); let result = login(s, &c, p)?; + if web::is_service(&c) { + if answers { + web::adopt(s, &c); + } + resolve_origins(s, &[&c]); + } let c = s.connections.find(&c.id).cloned().unwrap_or(c); let mut out = shown(s, &c); if let Some(saved) = result.password_saved { @@ -740,11 +822,77 @@ fn credential(s: &mut Session, c: &Connection) -> Result Result { + match p.get("allowInsecure") { + None | Some(Value::Null) => Ok(false), + Some(Value::Bool(b)) => Ok(*b), + Some(_) => Err("allowInsecure is true or false".into()), + } +} + +/// Logs in to a saved web-service connection with the password (basic) or key (header) +/// typed, or else the one given this session. The login is sent to the connection's origin, +/// and this connection is chosen for it. A connection without a login method is chosen for +/// its origin, which then gets no login. +fn service_login(s: &mut Session, c: &Connection, p: &Value, remember: bool) -> Result { + if remember { + return Err(web::SESSION_ONLY.into()); + } + let insecure = insecure_flag(p)?; + if c.id.is_empty() { + return Err(web::UNSAVED.into()); + } + let password = ps(p, "password").filter(|t| !t.is_empty()); + let key = ps(p, "apiKey").filter(|t| !t.is_empty()); + let typed = match (c.auth.as_str(), password, key) { + ("", None, None) => { + web::activate(s, c, None, false)?; + return Ok(Reopened::default()); + } + ("", ..) => return Err("this connection has no login method: set auth (basic or header) with connection.edit first".into()), + ("basic", typed, None) => typed, + ("basic", _, Some(_)) => return Err("a basic login takes a password, not an apiKey".into()), + (_, None, typed) => typed, + (_, Some(_), _) => return Err("an API-key login takes an apiKey, not a password".into()), + }; + let origin = crate::connections::login_origin(c).ok_or(web::NO_ORIGIN)?; + let Some(typed) = typed else { + // Only the active login given this session can be reused. + let secret = web::known_secret(s, c)?; + web::activate(s, c, Some(&secret), true)?; + return Ok(Reopened::default()); + }; + web::check_login_secret(c, typed)?; + if origin.starts_with("http://") && !insecure { + return Err(web::INSECURE.into()); + } + let secret = rustgis_data::secrets::Secret::new(typed); + web::activate(s, c, Some(&secret), insecure)?; + s.database_logins.set_session_password(&c.id, typed); + Ok(Reopened::default()) +} + fn tested(s: &Session, p: &Value) -> Result { if ps(p, "id").is_some() { - return Ok(found(s, p)?.clone()); + let c = found(s, p)?; + // Only a web-service login is tied to its saved definition; other kinds test as saved. + if web::is_service(c) { + for (field, saved) in + [("kind", c.kind.as_str()), ("address", &c.address), ("auth", &c.auth), ("user", &c.user), ("keyName", &c.key_name)] + { + if let Some(value) = p.get(field) { + let value = value.as_str().ok_or(web::SAVE_BEFORE_TEST)?.trim(); + let value = if field == "auth" && value == "none" { "" } else { value }; + if value != saved { + return Err(web::SAVE_BEFORE_TEST.into()); + } + } + } + } + return Ok(c.clone()); } - let c = Connection { + let mut c = Connection { kind: ps(p, "kind").ok_or_else(|| format!("id, or kind and address, is required ({})", KINDS.join(", ")))?.trim().into(), address: ps(p, "address").unwrap_or("").into(), name: ps(p, "name").unwrap_or("").into(), @@ -752,6 +900,7 @@ fn tested(s: &Session, p: &Value) -> Result { options: p.get("options").map(options).transpose()?.unwrap_or_default(), ..Connection::default() }; + login_fields(p, &mut c)?; Ok(crate::connections::clean(c)?.0) } @@ -759,7 +908,7 @@ fn first_bucket(c: &Connection) -> Option { c.options.get("buckets")?.split(',').map(str::trim).find(|b| !b.is_empty()).map(str::to_owned) } -fn probe_url(s: &Session, c: &Connection) -> Result, String> { +fn probe_url(s: &mut Session, c: &Connection) -> Result, String> { let url = match c.kind.as_str() { "postgres" | "mysql" | "sqlserver" => return Ok(None), "service" => rustgis_data::ogc::capabilities_url(&c.address, None), @@ -782,16 +931,17 @@ fn probe_url(s: &Session, c: &Connection) -> Result, String> { } _ => return Err(format!("unknown kind '{}'", c.kind)), }; + let url = if web::is_service(c) { web::request(s, (!c.id.is_empty()).then_some(c.id.as_str()), &url)? } else { url }; Ok(Some(url)) } fn answered(s: &mut Session, c: &Connection, text: &str) -> CmdResult { match c.kind.as_str() { - "service" => super::service::layers(s, &json!({"url": c.address, "text": text})), + "service" => super::service::layers(s, &json!({"url": c.address, "connection":c.id, "text": text})), "wms" | "wmts" | "wfs" | "wcs" | "ogcapi" | "arcgis" => { - super::service::layers(s, &json!({"url": c.address, "type": c.kind, "text": text})) + super::service::layers(s, &json!({"url": c.address, "connection":c.id, "type": c.kind, "text": text})) } - "csw" => super::service::catalog_search(s, &json!({"url": c.address, "text": text})), + "csw" => super::service::catalog_search(s, &json!({"url": c.address, "connection":c.id, "text": text})), "stac" => super::stac::collections_json(&stac::api(Some(&c.address))?, text.as_bytes()), "s3" => { if let Some(bucket) = first_bucket(c) { @@ -807,10 +957,22 @@ fn answered(s: &mut Session, c: &Connection, text: &str) -> CmdResult { fn test(s: &mut Session, p: &Value) -> CmdResult { let c = tested(s, p)?; + // An unsaved web service reads anonymously, whatever saved connection shares its origin. + let was = s.web_logins.set_anonymous(web::is_service(&c) && c.id.is_empty()); + let out = test_connection(s, p, c); + s.web_logins.set_anonymous(was); + out +} + +fn test_connection(s: &mut Session, p: &Value, c: Connection) -> CmdResult { + if web::is_service(&c) && c.id.is_empty() && !c.auth.is_empty() { + return Err(web::UNSAVED.into()); + } if DATABASE_KINDS.contains(&c.kind.as_str()) && b(p, "probe").unwrap_or(false) { return Ok(json!({"connection":c.id, "fetch":null})); } - let secrets = ["password", "accessKeyId", "secretAccessKey", "sessionToken"].iter().any(|k| ps(p, k).is_some_and(|v| !v.is_empty())); + let secrets = + ["password", "apiKey", "accessKeyId", "secretAccessKey", "sessionToken"].iter().any(|k| ps(p, k).is_some_and(|v| !v.is_empty())); if !DATABASE_KINDS.contains(&c.kind.as_str()) && secrets { let mut input = p.clone(); if let Some(o) = input.as_object_mut() { @@ -826,6 +988,10 @@ fn test(s: &mut Session, p: &Value) -> CmdResult { login(s, &c, &Value::Null)?; } } + // Missing or unusable session logins must not look like a successful anonymous test. + if web::is_service(&c) && !c.auth.is_empty() && !c.id.is_empty() && !secrets && !web::logged_in(s, &c) { + login(s, &c, &Value::Null)?; + } let mut out = json!({}); if !c.id.is_empty() { out["connection"] = json!(c.id); @@ -855,10 +1021,12 @@ fn test(s: &mut Session, p: &Value) -> CmdResult { } } } - "service" => super::service::layers(s, &json!({"url": c.address}))?, - "wms" | "wmts" | "wfs" | "wcs" | "ogcapi" | "arcgis" => super::service::layers(s, &json!({"url": c.address, "type": c.kind}))?, - "csw" => super::service::catalog_search(s, &json!({"url": c.address}))?, - "stac" => super::stac::collections(s, &json!({"url": c.address}))?, + "service" => super::service::layers(s, &json!({"url": c.address, "connection":c.id}))?, + "wms" | "wmts" | "wfs" | "wcs" | "ogcapi" | "arcgis" => { + super::service::layers(s, &json!({"url": c.address, "connection":c.id, "type": c.kind}))? + } + "csw" => super::service::catalog_search(s, &json!({"url": c.address, "connection":c.id}))?, + "stac" => super::stac::collections(s, &json!({"url": c.address, "connection":c.id}))?, "s3" => { s3::connection(&c.name).ok_or("log in first: connection.login {id, accessKeyId, secretAccessKey}")?; let mut q = json!({"connection": c.name, "limit": 100}); diff --git a/crates/engine/src/cmd/cube.rs b/crates/engine/src/cmd/cube.rs index 11527ec305..cc88bc7a4b 100644 --- a/crates/engine/src/cmd/cube.rs +++ b/crates/engine/src/cmd/cube.rs @@ -269,7 +269,7 @@ pub const TREND: Needs = Needs { what: "trend", min_dates: trend::MIN_VALUES, or pub const SUMMARY: Needs = Needs { what: "summary", min_dates: 2, ordered: false }; /// The cube a command's parameters name: a stack of layers, a Zarr variable, or a raster's bands. -pub fn cube_from(s: &Session, p: &Value, needs: Needs) -> Result, String> { +pub fn cube_from(s: &mut Session, p: &Value, needs: Needs) -> Result, String> { let dates = date_list(p)?; let what = needs.what; if let Some(list) = p.get("layers").filter(|v| !v.is_null()) { @@ -917,7 +917,7 @@ impl Stitch { } /// Prepares `cube.trend`: the cube, the dates, the test and its options checked. -pub fn trend_start(s: &Session, p: &Value) -> Result { +pub fn trend_start(s: &mut Session, p: &Value) -> Result { let merged = super::cube_catalog::with_entry(p)?; let p: &Value = &merged; // Read for the seasonal test only: another test leaves it alone, whatever it holds. @@ -1731,7 +1731,7 @@ fn stack_columns(outs: &mut Columns, mask: &[f32], columns: Columns, piece_mask: /// Prepares `cube.reduce`: the cube, the statistic, and the dates of the period (and months) /// asked for, in their groups. -pub fn reduce_start(s: &Session, p: &Value) -> Result { +pub fn reduce_start(s: &mut Session, p: &Value) -> Result { let merged = super::cube_catalog::with_entry(p)?; let p: &Value = &merged; let stat = Stat::parse(p)?; diff --git a/crates/engine/src/cmd/items.rs b/crates/engine/src/cmd/items.rs index ad7d6794e1..79027b8293 100644 --- a/crates/engine/src/cmd/items.rs +++ b/crates/engine/src/cmd/items.rs @@ -453,6 +453,7 @@ fn import_maps(s: &mut Session, p: &Value) -> CmdResult { for mut m in maps.into_iter().take(100) { // A binding in a file is no authority to use a login here. crate::s3_connections::clear_bindings(m.addresses_mut()); + crate::project::clear_login_bindings(m.addresses_mut()); let base = m.name.clone(); let mut i = 1; while s.project.maps.iter().any(|o| o.name.eq_ignore_ascii_case(&m.name)) { @@ -611,7 +612,16 @@ fn package_gp(s: &Session) -> Result, String> { if s.project.gp_history.is_empty() { return Err("no geoprocessing has been run yet".into()); } - let mut entries = vec![("history.json".to_string(), serde_json::to_vec_pretty(&s.project.gp_history).map_err(|e| e.to_string())?)]; + let mut history = s.project.gp_history.clone(); + let mut addresses = Vec::new(); + for record in &mut history { + crate::project::recorded_addresses(&mut record.params, &mut addresses); + if let Some((_, params)) = &mut record.command { + crate::project::recorded_addresses(params, &mut addresses); + } + } + crate::project::clear_login_bindings(addresses); + let mut entries = vec![("history.json".to_string(), serde_json::to_vec_pretty(&history).map_err(|e| e.to_string())?)]; let mut layers = std::collections::HashSet::new(); let mut files = std::collections::HashSet::new(); for rec in s.project.gp_history.iter().filter(|r| r.ok) { diff --git a/crates/engine/src/cmd/layer.rs b/crates/engine/src/cmd/layer.rs index 61062bdac6..0735134869 100644 --- a/crates/engine/src/cmd/layer.rs +++ b/crates/engine/src/cmd/layer.rs @@ -40,10 +40,10 @@ pub fn commands() -> Vec { .params(r#"{"layer"?: name|id, "crs": "EPSG:… or WKT; reinterpret native coordinates, without rewriting the source file"}"#), C::new("layer.loadAll", "Load All Features", "Layers", load_all).when(need_streamed).params(r#"{"layer"?: name|id}"#), C::new("layer.addUrl", "Add Data From URL", "Home › Add › Add Data", add_url).params( - r#"{"url": "https://…/data.geojson|.csv|.parquet|.fgb|.gpkg|.zip|.kml|.kmz|.gpx|.tif|.jp2 (JPEG 2000: desktop, through GDAL) (the format from the address, else the contents) | a .pmtiles archive", "name"?: text, "layer"?: "one layer of a zip, KML or GPX", "zoom"?: "bool: true zooms to what was added; false keeps the view of a GeoParquet or FlatGeobuf read in place (zoomed to only when the map was empty)", "x"?, "y"?, "wkt"?, "crs"?, "delimiter"?, "paperSpace"?, "stream"?, "as"? (as layer.addData), "whole"?: "bool: download a cloud-optimised GeoTIFF whole (as a raster layer) instead of reading it in place (an address without a file name is read in place when its first bytes are a cloud-optimised GeoTIFF)", "cog"?: "bool: the address is a cloud-optimised GeoTIFF whatever its name says (the app's probe found it so)", "start"?, "end"?, "unit"?, "interval"?, "dates"?: "a GeoTIFF address with date placeholders ({date:YYYY}, {YYYY}{MM}{DD}), one file per date: the dates it spans and steps through (as time.set), turning the time slider on"}"#, + r#"{"url": "https://…/data.geojson|.csv|.parquet|.fgb|.gpkg|.zip|.kml|.kmz|.gpx|.tif|.jp2 (JPEG 2000: desktop, through GDAL) (the format from the address, else the contents) | a .pmtiles archive", "name"?: text, "layer"?: "one layer of a zip, KML or GPX", "zoom"?: "bool: true zooms to what was added; false keeps the view of a GeoParquet or FlatGeobuf read in place (zoomed to only when the map was empty)", "x"?, "y"?, "wkt"?, "crs"?, "delimiter"?, "paperSpace"?, "stream"?, "as"? (as layer.addData), "whole"?: "bool: download a cloud-optimised GeoTIFF whole (as a raster layer) instead of reading it in place (an address without a file name is read in place when its first bytes are a cloud-optimised GeoTIFF)", "cog"?: "bool: the address is a cloud-optimised GeoTIFF whatever its name says (the app's probe found it so)", "start"?, "end"?, "unit"?, "interval"?, "dates"?: "a GeoTIFF address with date placeholders ({date:YYYY}, {YYYY}{MM}{DD}), one file per date: the dates it spans and steps through (as time.set), turning the time slider on", "connection"?: "the id of the saved web-service connection it comes from (as service.add)"}"#, ), C::new("layer.addDownload", "Add Downloaded Data", "Home › Add", add_download) - .params(r#"{"id": a download the app finished (Home › Add › Add Data › From URL), "url": its address, "name"?: text, "layer"?: text, "zoom"?: bool, "x"?, "y"?, "wkt"?, "crs"?, "delimiter"?, "paperSpace"?, "stream"?, "as"? (as layer.addData), "service"?: bool (a service's features: their axes and paging are checked), "bounds"?: [west, south, east, north] (with service: the layer's longitude/latitude bounds from its capabilities, which its axes are checked against; without bounds they are left as they come; ignored without service), "coverage"?: bool (a WCS coverage: a GeoTIFF, else the service's error), "geolibreProject"?: bool (internal: downloaded project JSON), "geolibreLayer"?: {"token", "record"} (internal: reserved imported layer and its saved style), "geolibreZarr"?: bool (internal: an imported Zarr slice as GeoTIFF)}"#), + .params(r#"{"id": a download the app finished (Home › Add › Add Data › From URL), "url": its address, "name"?: text, "layer"?: text, "zoom"?: bool, "x"?, "y"?, "wkt"?, "crs"?, "delimiter"?, "paperSpace"?, "stream"?, "as"? (as layer.addData), "service"?: bool (a service's features: their axes and paging are checked), "bounds"?: [west, south, east, north] (with service: the layer's longitude/latitude bounds from its capabilities, which its axes are checked against; without bounds they are left as they come; ignored without service), "coverage"?: bool (a WCS coverage: a GeoTIFF, else the service's error), "geolibreProject"?: bool (internal: downloaded project JSON), "geolibreLayer"?: {"token", "record"} (internal: reserved imported layer and its saved style), "geolibreZarr"?: bool (internal: an imported Zarr slice as GeoTIFF), "connection"?: "as layer.addUrl"}"#), C::new("layer.addEncodedPolyline", "Add Encoded Polyline", "Home › Add", add_encoded_polyline).params( r#"{"polyline": "encoded polyline text" | [texts] (one line each), "precision"?: 5 (Google) | 6 (OSRM, Valhalla), "name"?: text}"#, ), @@ -2570,6 +2570,7 @@ pub(crate) fn add_layer_text(s: &mut Session, text: &str) -> CmdResult { } // A binding in a file is no authority to use a login here. crate::s3_connections::clear_bindings(layer.addresses_mut()); + crate::project::clear_login_bindings(layer.addresses_mut()); // A fresh id and a unique name in this map; the rest goes through project repair. let base = layer.name.clone(); let mut i = 1; @@ -2788,13 +2789,15 @@ pub fn url_file_name(url: &str, bytes: &[u8]) -> Result { /// request is kept for the app ([`Session::request_url`]), which downloads and adds it as its own /// Home › Add › Add Data › From URL does, so a plugin, a script or the assistant can add data by address too. pub(crate) fn add_url(s: &mut Session, p: &Value) -> CmdResult { + let bound = crate::service_logins::request_params(s, p)?; + let p = bound.as_ref(); if ["geolibreProject", "geolibreLayer", "geolibreZarr"].iter().any(|key| p.get(key).is_some()) { return Err("GeoLibre download state is internal; use project.importGeolibre".into()); } let url = ps(p, "url").map(str::trim).filter(|u| !u.is_empty()).ok_or("url is required")?; // An address read through a saved S3 connection is judged by the address it wraps; the whole // address stays what is fetched and kept. - let lower = rustgis_data::s3_address::inner(url).to_ascii_lowercase(); + let lower = rustgis_data::s3_address::inner(crate::render::rustgis_tiles::logins::inner(url)).to_ascii_lowercase(); if !(lower.starts_with("https://") || lower.starts_with("http://") || lower.starts_with("s3://")) { return Err(format!("{url}: give an http://, https:// or s3:// address (Add Data opens files)")); } @@ -2810,6 +2813,9 @@ pub(crate) fn add_url(s: &mut Session, p: &Value) -> CmdResult { if let Some(n) = ps(p, "name") { q["name"] = json!(n); } + if let Some(connection) = p.get("connection") { + q["connection"] = connection.clone(); + } return super::tiles::add(s, &q); } // A dated address (one file per date, `{date:YYYY}`) is read in place, following the time @@ -2937,7 +2943,7 @@ fn named_layers(s: &Session, name: &str) -> usize { /// Whether a web address names a GeoTIFF (by its path's extension). pub fn is_tiff_url(url: &str) -> bool { - let url = rustgis_data::s3_address::inner(url); + let url = rustgis_data::s3_address::inner(crate::render::rustgis_tiles::logins::inner(url)); let path = url.split(['?', '#']).next().unwrap_or(url).to_ascii_lowercase(); path.ends_with(".tif") || path.ends_with(".tiff") } @@ -2945,7 +2951,7 @@ pub fn is_tiff_url(url: &str) -> bool { /// Whether a web address's last path segment has no extension (an API answering with a file, /// such as Vantor's `…/geotiff?…`): its first bytes tell whether it is a GeoTIFF to read in place. pub fn is_unnamed_url(url: &str) -> bool { - let url = rustgis_data::s3_address::inner(url); + let url = rustgis_data::s3_address::inner(crate::render::rustgis_tiles::logins::inner(url)); let path = url.split(['?', '#']).next().unwrap_or(url); let Some((_, rest)) = path.split_once("://") else { return false }; let Some((_, path)) = rest.split_once('/') else { return false }; @@ -2959,7 +2965,7 @@ pub(crate) fn add_cog(s: &mut Session, p: &Value, url: &str) -> CmdResult { let name = ps(p, "name").map(str::trim).filter(|n| !n.is_empty()).unwrap_or(&stem); let mut q = json!({"url": url, "kind": "cog", "name": name, "top": true}); // A dated address's span and steps (see map.addTiles). - for k in ["start", "end", "unit", "interval", "dates"] { + for k in ["start", "end", "unit", "interval", "dates", "connection"] { if let Some(v) = p.get(k) { q[k] = v.clone(); } @@ -3014,6 +3020,14 @@ fn cog_name(url: &str) -> String { /// Adds a download the app finished (its bytes handed to the session, not passed as text). fn add_download(s: &mut Session, p: &Value) -> CmdResult { + // The address may become the layer's source: a login wrapper another session owns is refused, + // and one given with a connection is bound to that connection. + let bound = if ps(p, "url").is_some_and(rustgis_data::login_address::is_wrapper) { + crate::service_logins::request_params(s, p)? + } else { + std::borrow::Cow::Borrowed(p) + }; + let p = bound.as_ref(); let id = f(p, "id").filter(|v| *v >= 0.0 && v.fract() == 0.0).map(|v| v as u64).ok_or("id is required")?; let url = ps(p, "url").unwrap_or("data").to_string(); // Read before the download is taken, so a bad one leaves it to be added again. diff --git a/crates/engine/src/cmd/plugins.rs b/crates/engine/src/cmd/plugins.rs index 2709fad93a..93487eaf65 100644 --- a/crates/engine/src/cmd/plugins.rs +++ b/crates/engine/src/cmd/plugins.rs @@ -283,6 +283,7 @@ const NEVER: &[&str] = &[ "connection.login", "connection.forgetPassword", "connection.forgetSavedKeys", + "connection.logout", "connection.resolveS3", "connection.test", "connection.browse", @@ -396,11 +397,18 @@ pub fn reaches(spec: &C, params: &Value) -> Option { } /// Why a plugin may not run `id` with `params`, if it may not. -fn refused(plugin: &Plugin, spec: &C, params: &Value) -> Option { +fn refused(s: &Session, plugin: &Plugin, spec: &C, params: &Value) -> Option { let m = &plugin.manifest; if user_only(spec.id) { return Some(format!("plugins can't run '{}'", spec.id)); } + // A saved web-service connection's login goes where the user chose it: a plugin never picks + // which login a request carries, as it never logs in or out. + if let Some(id) = params.get("connection").and_then(Value::as_str).map(str::trim) + && s.connections.connections.iter().any(|c| c.id == id && crate::service_logins::is_service(c)) + { + return Some(format!("plugins can't use a saved web-service connection's login ('{}')", spec.id)); + } // An SQL query reads files and web addresses alike, and a database is a file or a server. let database = DATABASES.contains(&spec.id) || (spec.id == "cube.open" && super::cube_catalog::opens_zarr(params)); if database && !(m.has(Permission::Files) && m.has(Permission::Network)) { @@ -517,7 +525,7 @@ impl Bridge<'_> { return Err("plugins can't run other plugins' commands".into()); } let spec = self.s.registry.get(id).ok_or_else(|| format!("unknown command '{id}'"))?; - if let Some(why) = refused(&self.plugin, spec, params) { + if let Some(why) = refused(self.s, &self.plugin, spec, params) { return Err(why); } let (enabled, run, label, mutates) = (spec.enabled, spec.run, spec.label, spec.mutates); @@ -544,8 +552,11 @@ impl Bridge<'_> { // command that only left a request for the front end ([`Session::request_url`]) changed // nothing; a later one must not erase the undo step for an earlier edit. self.s.request_only = false; + // No saved web-service login goes with what a plugin asks for: the user chooses where it is sent. + let anonymous = self.s.web_logins.set_anonymous(true); let mut res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run(self.s, params))) .unwrap_or_else(|_| Err(format!("{label} failed unexpectedly"))); + self.s.web_logins.set_anonymous(anonymous); let requested = std::mem::take(&mut self.s.request_only); self.requested |= requested; self.changed |= mutates && !requested; diff --git a/crates/engine/src/cmd/service.rs b/crates/engine/src/cmd/service.rs index 7d06972efe..c68ce4a684 100644 --- a/crates/engine/src/cmd/service.rs +++ b/crates/engine/src/cmd/service.rs @@ -27,10 +27,10 @@ pub fn commands() -> Vec { r#"{"url": "a WMS, WMTS or WFS address (GetCapabilities is added), an OGC API collections address or landing page (its map tiles and linked collections), or an ArcGIS FeatureServer, MapServer, ImageServer or VectorTileServer, or an ArcGIS services directory or folder (its folders and services, each listed by its own url)", "type"?: "wms"|"wmts"|"wfs"|"ogcapi"|"arcgis" (guessed from the address), "text"?: "the capabilities document, when already fetched", "documents"?: [{"url": "a discovered document address", "text": "its fetched JSON"}]} → layers with source_url, and next when supplied documents need another fetch"#, ), C::new("service.add", "Add Service Layer", "Home › Add", add).params( - r#"{"url": "as service.layers", "type"?, "layer": "the layer's name (service.layers lists them)", "name"?: text, "time"?: "a time-enabled WMS or WMTS layer: by default (or \"slider\", true) it follows the time slider, turned on with the layer's times when it is off; or one of its dates (2024-06-01), or \"default\" (false): its service's default", "text"?: "the capabilities document, when already fetched", "documents"?: "additional fetched documents, as service.layers", "source_url"?: "the listed layer's source_url", "describe"?: "a WCS coverage's DescribeCoverage document, when already fetched", "tiles"?: "an OGC API layer's tile template, when already read from its tileset (the app's)", "bbox"?: [west, south, east, north] | "view" | "full": a WCS coverage's area (the view by default); for a WFS, OGC API or ArcGIS feature layer, only the features that cross it (the whole layer, "full", by default)}"#, + r#"{"url": "as service.layers", "type"?, "layer": "the layer's name (service.layers lists them)", "name"?: text, "time"?: "a time-enabled WMS or WMTS layer: by default (or \"slider\", true) it follows the time slider, turned on with the layer's times when it is off; or one of its dates (2024-06-01), or \"default\" (false): its service's default", "text"?: "the capabilities document, when already fetched", "documents"?: "additional fetched documents, as service.layers", "source_url"?: "the listed layer's source_url", "describe"?: "a WCS coverage's DescribeCoverage document, when already fetched", "tiles"?: "an OGC API layer's tile template, when already read from its tileset (the app's)", "bbox"?: [west, south, east, north] | "view" | "full": a WCS coverage's area (the view by default); for a WFS, OGC API or ArcGIS feature layer, only the features that cross it (the whole layer, "full", by default), "connection"?: "the id of the saved web-service connection it comes from (connection.browse gives it): the layer records it, and is read with its login"}"#, ), C::new("service.coverage", "WCS Coverage Request", "Home › Add", coverage).view().params( - r#"{"url": "a WCS address", "type"?: "wcs", "layer": "the coverage's name", "text"?: "the capabilities document, when already fetched", "documents"?: "additional fetched documents, as service.layers", "source_url"?: "the listed coverage's source_url", "describe"?: "its DescribeCoverage answer, when already fetched", "bbox"?: [west, south, east, north] | "view" (the default) | "full"} → the GetCoverage address of a GeoTIFF (layer.addUrl adds it)"#, + r#"{"url": "a WCS address", "type"?: "wcs", "layer": "the coverage's name", "text"?: "the capabilities document, when already fetched", "documents"?: "additional fetched documents, as service.layers", "source_url"?: "the listed coverage's source_url", "describe"?: "its DescribeCoverage answer, when already fetched", "bbox"?: [west, south, east, north] | "view" (the default) | "full", "connection"?: "the saved web-service connection id whose login the request uses"} → the GetCoverage address of a GeoTIFF (layer.addUrl adds it)"#, ), C::new("catalog.search", "Search Catalogue", "Home › Add", catalog_search).view().params( r#"{"url": "a catalogue's CSW address", "query"?: "words anywhere in the records", "bbox"?: [west, south, east, north] | "view" (none by default), "start"?: "the first record's position (a page's next)", "text"?: "the GetRecords answer, when already fetched"} → records with their service and download links (service.add or layer.addUrl add them)"#, @@ -42,13 +42,17 @@ pub fn commands() -> Vec { fn url_of(p: &Value) -> Result<&str, String> { let url = ps(p, "url").map(str::trim).filter(|u| !u.is_empty()).ok_or("url is required")?; - let lower = url.to_ascii_lowercase(); + let lower = crate::render::rustgis_tiles::logins::inner(url).to_ascii_lowercase(); if !(lower.starts_with("https://") || lower.starts_with("http://")) { return Err(format!("{url}: a service is given by its http:// or https:// address")); } Ok(url) } +fn same_address(a: &str, b: &str) -> bool { + crate::render::rustgis_tiles::logins::inner(a) == crate::render::rustgis_tiles::logins::inner(b) +} + /// `p` with its ArcGIS service address's `?token=…` taken out and kept for the session (it then /// goes, as the request's credential, to every request to that server: the service's layers, tiles and feature /// pages), so the address the layer keeps has no token in it. `None` when there is none. @@ -108,7 +112,8 @@ const MAX_SERVICE_DOCUMENTS: usize = 4; fn capabilities(p: &Value) -> Result { let original = url_of(p)?; - let mut address = rustgis_data::ogc::capabilities_url(original, ps(p, "type")); + let mut address = + crate::render::rustgis_tiles::logins::inherit(original, &rustgis_data::ogc::capabilities_url(original, ps(p, "type"))); let supplied = ps(p, "text"); let documents = p.get("documents").map(|v| v.as_array().ok_or("documents must be an array")).transpose()?; if let Some(docs) = documents { @@ -138,7 +143,9 @@ fn capabilities(p: &Value) -> Result { let text = if seen.is_empty() { Some(root) } else { - documents.and_then(|docs| docs.iter().find(|d| ps(d, "url") == Some(address.as_str()))).and_then(|d| ps(d, "text")) + documents + .and_then(|docs| docs.iter().find(|d| ps(d, "url").is_some_and(|u| same_address(u, &address)))) + .and_then(|d| ps(d, "text")) }; let Some(text) = text else { result.next = Some(address); @@ -182,7 +189,7 @@ fn capabilities(p: &Value) -> Result { } let Some(next) = service.collections_url else { return Ok(result) }; parent = address; - address = next; + address = crate::render::rustgis_tiles::logins::inherit(&parent, &next); } } @@ -283,7 +290,7 @@ fn checked_template(service: &str, t: &str) -> Result { return Err(format!("{t}: not a tile template")); } crate::render::rustgis_tiles::address::check(t, Some(service))?; - Ok(t.to_string()) + Ok(crate::render::rustgis_tiles::logins::inherit(service, t)) } /// The tile template an OGC API tileset list advertises (its WebMercatorQuad tileset's), read now @@ -297,7 +304,7 @@ fn advertised_template(list: &str, vector: bool) -> Option { let body = crate::render::rustgis_tiles::cache::download_blocking(&doc).ok()?; match resolve_tiles(list, &doc, &body, vector)? { TilesStep::Template(t) => return checked_template(list, &t).ok(), - TilesStep::Next(next) => doc = next, + TilesStep::Next(next) => doc = crate::render::rustgis_tiles::logins::inherit(&doc, &next), } } None @@ -346,9 +353,11 @@ pub fn service_json(svc: &Service) -> Value { json!({"kind": svc.kind, "title": svc.title, "count": layers.len(), "layers": layers, "notes": svc.notes}) } -pub(crate) fn layers(_s: &mut Session, p: &Value) -> CmdResult { +pub(crate) fn layers(s: &mut Session, p: &Value) -> CmdResult { let owned = without_token(p)?; let p = owned.as_ref().unwrap_or(p); + let bound = crate::service_logins::request_params(s, p)?; + let p = bound.as_ref(); let found = capabilities(p)?; let mut out = service_json(&found.service); if let Some(layers) = out.get_mut("layers").and_then(Value::as_array_mut) { @@ -367,11 +376,14 @@ pub(crate) fn layers(_s: &mut Session, p: &Value) -> CmdResult { pub(crate) fn add(s: &mut Session, p: &Value) -> CmdResult { let owned = without_token(p)?; let p = owned.as_ref().unwrap_or(p); + let bound = crate::service_logins::request_params(s, p)?; + let p = bound.as_ref(); let url = url_of(p)?.to_string(); let want = ps(p, "layer").map(str::trim).filter(|l| !l.is_empty()).ok_or("layer is required")?; let found = capabilities(p)?; - let candidates = - || found.service.layers.iter().zip(&found.sources).filter(|(_, source)| ps(p, "source_url").is_none_or(|s| s == source.as_str())); + let candidates = || { + found.service.layers.iter().zip(&found.sources).filter(|(_, source)| ps(p, "source_url").is_none_or(|s| same_address(s, source))) + }; let (layer, source) = candidates() .find(|(l, _)| l.name == want) .or_else(|| candidates().find(|(l, _)| l.name.eq_ignore_ascii_case(want) || l.title.eq_ignore_ascii_case(want))) @@ -452,7 +464,7 @@ pub(crate) fn add(s: &mut Session, p: &Value) -> CmdResult { (None, None) => template.to_string(), }; // With its legends, as its service gives them (none: no key, as before). - let mut add = json!({"url": template, "kind": "raster", "name": name, "overlay": true, "bounds": layer.bbox}); + let mut add = json!({"url": template, "kind": "raster", "name": name, "overlay": true, "bounds": layer.bbox, "connection":p.get("connection")}); // A layer its service draws on request (a WMS, an ArcGIS map service) is asked for // as one picture of the view, so its symbols and labels aren't cut at tile edges // (#1162); `map.setTiles` turns it back to tiles. @@ -516,13 +528,16 @@ pub(crate) fn add(s: &mut Session, p: &Value) -> CmdResult { let template = advertised.as_deref().unwrap_or(template); let r = super::tiles::add( s, - &json!({"url": template, "kind": "vector", "name": name, "layers": layers, "overlay": true, "bounds": layer.bbox}), + &json!({"url": template, "kind": "vector", "name": name, "layers": layers, "overlay": true, "bounds": layer.bbox, "connection":p.get("connection")}), )?; zoom(s); Ok(r) } Offer::Style { url: style } => { - let r = super::tiles::add(s, &json!({"url": style, "kind": "style", "name": name, "bounds": layer.bbox}))?; + let r = super::tiles::add( + s, + &json!({"url": style, "kind": "style", "name": name, "bounds": layer.bbox, "connection":p.get("connection")}), + )?; zoom(s); Ok(r) } @@ -620,13 +635,13 @@ fn coverage_area(s: &Session, p: &Value, info: &rustgis_data::ogc::CoverageInfo, /// A coverage's GetCoverage address and the area it covers: its description from `"describe"`, or /// fetched (waiting for it). fn coverage_request(s: &Session, p: &Value, c: &Coverage) -> Result<(String, [f64; 4]), String> { - let describe = rustgis_data::ogc::describe_coverage_url(c); + let describe = crate::render::rustgis_tiles::logins::inherit(&c.endpoint, &rustgis_data::ogc::describe_coverage_url(c)); let info = match ps(p, "describe") { Some(text) => rustgis_data::ogc::parse_describe(c, text.as_bytes())?, None => fetch_describe(c, &describe)?, }; let area = coverage_area(s, p, &info, &c.id)?; - let get = rustgis_data::ogc::get_coverage_url(c, &info, area); + let get = crate::render::rustgis_tiles::logins::inherit(&c.endpoint, &rustgis_data::ogc::get_coverage_url(c, &info, area)); crate::render::rustgis_tiles::address::check(&get, Some(&c.endpoint))?; Ok((get, area)) } @@ -645,6 +660,8 @@ fn fetch_describe(c: &Coverage, _: &str) -> Result CmdResult { + let bound = crate::service_logins::request_params(s, p)?; + let p = bound.as_ref(); let url = url_of(p)?.to_string(); let want = ps(p, "layer").map(str::trim).filter(|l| !l.is_empty()).ok_or("layer is required")?; let found = capabilities(p)?; @@ -653,7 +670,7 @@ fn coverage(s: &mut Session, p: &Value) -> CmdResult { .layers .iter() .zip(&found.sources) - .filter(|(_, source)| ps(p, "source_url").is_none_or(|s| s == source.as_str())) + .filter(|(_, source)| ps(p, "source_url").is_none_or(|s| same_address(s, source))) .map(|(layer, _)| layer) .find(|l| l.name == want || l.name.eq_ignore_ascii_case(want)) .ok_or_else(|| format!("{url}: no coverage '{want}' (service.layers lists them)"))?; @@ -672,7 +689,7 @@ pub fn records_request(s: &Session, p: &Value) -> Result { Some(_) => Some(super::catalogs::area(s, p)?).filter(|a| a[0] <= a[2]), }; let start = p.get("start").and_then(Value::as_u64).unwrap_or(1).clamp(1, 1_000_000); - Ok(rustgis_data::ogc::records_url(url, words, bbox, start)) + Ok(crate::render::rustgis_tiles::logins::inherit(url, &rustgis_data::ogc::records_url(url, words, bbox, start))) } /// A WCS coverage's answer is a GeoTIFF; anything else (an XML exception, an HTML page, plain @@ -689,6 +706,8 @@ pub(crate) fn coverage_check(name: &str, bytes: &[u8]) -> Result<(), String> { /// `catalog.search`: a page of a catalogue's (CSW) records matching words and an area, with the /// links RustGIS can follow (those pointing where a document on the web may not send it left out). pub(super) fn catalog_search(s: &mut Session, p: &Value) -> CmdResult { + let bound = crate::service_logins::request_params(s, p)?; + let p = bound.as_ref(); let url = url_of(p)?; let request = records_request(s, p)?; let page = match ps(p, "text") { @@ -708,7 +727,7 @@ pub(super) fn catalog_search(s: &mut Session, p: &Value) -> CmdResult { dropped += usize::from(!ok); ok }) - .map(|l| json!({"url": l.url, "kind": l.kind, "name": l.name, "protocol": l.protocol})) + .map(|l| json!({"url": crate::render::rustgis_tiles::logins::inherit(&request, &l.url), "kind": l.kind, "name": l.name, "protocol": l.protocol})) .collect(); json!({"id": r.id, "title": r.title, "abstract": r.summary, "bbox": r.bbox, "links": links}) }) diff --git a/crates/engine/src/cmd/service_group.rs b/crates/engine/src/cmd/service_group.rs index a17a9e9a6b..386083c382 100644 --- a/crates/engine/src/cmd/service_group.rs +++ b/crates/engine/src/cmd/service_group.rs @@ -10,7 +10,7 @@ use super::{C, CmdResult, MAX_GROUP_BYTES, Session, capabilities, ps, service_js pub fn commands() -> Vec { vec![ C::new("service.import", "Import Service", "Home › Add", import).params( - r#"{"url": "an OGC API landing page or collections address", "type"?, "name"?: "group name (default My OGC Layer)", "text"?, "documents"?: "as service.layers", "responses"?: [{"url": "features URL", "text": "downloaded GeoJSON"}]} ("group" and "layer" are rejected: use service.refresh)"#, + r#"{"url": "an OGC API landing page or collections address", "type"?, "name"?: "group name (default My OGC Layer)", "text"?, "documents"?: "as service.layers", "responses"?: [{"url": "features URL", "text": "downloaded GeoJSON"}], "connection"?: "as service.add"} ("group" and "layer" are rejected: use service.refresh)"#, ), C::new("service.refresh", "Refresh", "Layers", refresh).params( r#"{"group"?: id, "layer"?: "one child layer id or name", "text"?, "documents"?, "responses"?: "as service.import"}"#, @@ -39,15 +39,47 @@ fn target(s: &Session, p: &Value) -> Result)>, Ok(Some((group.clone(), layer))) } -fn request(s: &Session, p: &Value) -> CmdResult { +fn request(s: &mut Session, p: &Value) -> CmdResult { let mut q = p.clone(); - if let Some((group, _)) = target(s, p)? { - q["url"] = json!(group.url); + if let Some((group, child)) = target(s, p)? { + if q.get("connection").is_none_or(Value::is_null) { + let mut ids = group + .children + .iter() + .filter(|c| child.is_none_or(|id| c.layer == id)) + .filter_map(|c| s.map().layer(c.layer)) + .map(|l| l.connection.as_str()); + if let Some(id) = ids.next().filter(|id| !id.is_empty()) + && ids.all(|other| other == id) + && valid_connection(s, id, &group.url) + { + q["connection"] = json!(id); + } + } + q["url"] = json!(crate::render::rustgis_tiles::logins::inner(&group.url)); q["type"] = json!(group.kind); q["name"] = json!(group.name); } super::url_of(&q)?; - Ok(q) + Ok(crate::service_logins::request_params(s, &q)?.into_owned()) +} + +fn valid_connection(s: &Session, id: &str, url: &str) -> bool { + let origin = crate::render::rustgis_tiles::logins::origin(url); + origin.is_some() + && s.connections.connections.iter().any(|c| { + c.id == id && crate::service_logins::is_service(c) && crate::render::rustgis_tiles::logins::origin(&c.address) == origin + }) +} + +fn record_connection(s: &mut Session, layer: u32, request: &Value) { + let Some(id) = ps(request, "connection") else { return }; + let Some(url) = s.map().layer(layer).and_then(|l| l.dataset.source.as_deref()) else { return }; + if valid_connection(s, id, url) + && let Some(layer) = s.map_mut().layer_mut(layer) + { + layer.connection = id.to_string(); + } } fn plan(s: &mut Session, p: &Value) -> CmdResult { @@ -92,7 +124,7 @@ fn listing(s: &mut Session, p: &Value) -> CmdResult { // One child: its collection at its saved source, or else the one collection with its id (a // rotated query or a moved collections document keeps the child's identity). if let Some(c) = &child { - let exact = offered.iter().position(|(l, src)| l.name == c.collection && *src == c.source_url); + let exact = offered.iter().position(|(l, src)| l.name == c.collection && super::same_address(src, &c.source_url)); let pos = match exact { Some(i) => Some(i), None => { @@ -120,7 +152,7 @@ fn listing(s: &mut Session, p: &Value) -> CmdResult { if let Some(next) = found.next { out["next"] = json!(next); } - out["request"] = json!({"url": q.get("url"), "type": ps(&q, "type").unwrap_or("ogcapi")}); + out["request"] = json!({"url": q.get("url"), "type": ps(&q, "type").unwrap_or("ogcapi"), "connection": q.get("connection")}); Ok(out) } @@ -153,7 +185,7 @@ fn read(s: &mut Session, p: &Value) -> Result<(Value, Vec<(ServiceChild, Dataset Some(responses) => { let text = responses .iter() - .find(|r| ps(r, "url") == Some(url)) + .find(|r| ps(r, "url").is_some_and(|u| super::same_address(u, url))) .and_then(|r| ps(r, "text")) .ok_or_else(|| format!("{name}: missing downloaded response"))?; if text.len() > MAX_GROUP_BYTES.saturating_sub(bytes_read) { @@ -211,6 +243,9 @@ fn import(s: &mut Session, p: &Value) -> CmdResult { let mut extent = rustgis_geom::Extent::EMPTY; for (mut child, dataset) in reads { child.layer = crate::cmd::layer::add_dataset(s, dataset); + if let Some(request) = listing.get("request") { + record_connection(s, child.layer, request); + } if let Some(layer) = s.map().layer(child.layer) { extent = extent.union(&layer.extent()); } @@ -238,7 +273,7 @@ fn refresh(s: &mut Session, p: &Value) -> CmdResult { let (old, child_id) = target(s, p)?.ok_or("group or layer is required")?; let hidden = old.children.iter().any(|c| s.map().layer(c.layer).is_some()) && old.children.iter().filter_map(|c| s.map().layer(c.layer)).all(|l| !l.visible); - let (_, reads) = read(s, p)?; + let (listing, reads) = read(s, p)?; if !s.map().service_groups.iter().any(|g| g.id == old.id) { return Err("the group was removed".into()); } @@ -254,7 +289,7 @@ fn refresh(s: &mut Session, p: &Value) -> CmdResult { .map(|(child, _)| { old.children .iter() - .find(|c| c.collection == child.collection && c.source_url == child.source_url) + .find(|c| c.collection == child.collection && super::same_address(&c.source_url, &child.source_url)) .or_else(|| { let unique = read_count(&child.collection) == 1 && old_count(&child.collection) == 1; old.children.iter().find(|c| unique && c.collection == child.collection) @@ -331,6 +366,9 @@ fn refresh(s: &mut Session, p: &Value) -> CmdResult { } } else { child.layer = crate::cmd::layer::add_dataset(s, dataset); + if let Some(request) = listing.get("request") { + record_connection(s, child.layer, request); + } if hidden && let Some(layer) = s.map_mut().layer_mut(child.layer) { layer.visible = false; } @@ -399,3 +437,94 @@ fn set_group(s: &mut Session, p: &Value) -> CmdResult { } Ok(json!({"group":group.id})) } + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + + fn document(ids: &[&str]) -> Value { + json!({"collections": ids.iter().map(|id| json!({ + "id": id, "title": id, + "links": [{"rel": "items", "type": "application/geo+json", + "href": format!("https://group-none757.example.org/collections/{id}/items")}] + })).collect::>()}) + } + + fn features() -> String { + json!({"type": "FeatureCollection", "features": [{ + "type": "Feature", "geometry": {"type": "Point", "coordinates": [1, 2]}, + "properties": {"collection": "any"} + }]}) + .to_string() + } + + /// Answers keyed by the offer addresses `service.layers` reports (a service may spell them + /// its own way), exactly as `service.import` and `service.refresh` look them up. + fn answers(s: &mut Session, p: &Value, text: &str) -> Value { + let plan = s.run("service.layers", p).unwrap(); + json!(plan["layers"].as_array().unwrap().iter().map(|l| json!({"url": l["offer"]["url"], "text": text})).collect::>()) + } + + #[test] + fn refresh_new_children_keep_the_unambiguous_auth_none_connection() { + let mut s = Session::new(crate::Project::default()); + let connection = s + .run( + "connection.new", + &json!({ + "kind": "ogcapi", "name": "Public", "auth": "none", + "address": "https://group-none757.example.org/collections" + }), + ) + .unwrap()["id"] + .as_str() + .unwrap() + .to_string(); + let mut p = json!({ + "url": "https://group-none757.example.org/collections", "type": "ogcapi", + "connection": connection, "text": document(&["old"]).to_string() + }); + p["responses"] = answers(&mut s, &p, &features()); + let imported = s.run("service.import", &p).unwrap(); + let old = imported["layers"][0].as_u64().unwrap() as u32; + let mut q = json!({ + "url": "https://group-none757.example.org/collections", + "group": imported["group"], "text": document(&["old", "new"]).to_string() + }); + q["responses"] = answers(&mut s, &q, &features()); + s.run("service.refresh", &q).unwrap(); + assert_eq!(s.map().layers.len(), 2); + assert!(s.map().layer(old).is_some()); + for layer in &s.map().layers { + assert_eq!(layer.connection, connection); + assert_eq!(layer.dataset.features.len(), 1); + } + } + + #[test] + fn an_unrelated_explicit_connection_is_not_attributed_to_imported_children() { + let mut s = Session::new(crate::Project::default()); + let connection = s + .run( + "connection.new", + &json!({ + "kind": "ogcapi", "name": "Elsewhere", "auth": "none", + "address": "https://elsewhere757.example.org/collections" + }), + ) + .unwrap()["id"] + .as_str() + .unwrap() + .to_string(); + let mut p = json!({ + "url": "https://group-none757.example.org/collections", "type": "ogcapi", + "connection": connection, "text": document(&["new"]).to_string() + }); + p["responses"] = answers(&mut s, &p, &features()); + s.run("service.import", &p).unwrap(); + assert_eq!(s.map().layers.len(), 1); + assert_eq!(s.map().layers[0].dataset.features.len(), 1); + assert!(s.map().layers[0].connection.is_empty()); + } +} diff --git a/crates/engine/src/cmd/stac.rs b/crates/engine/src/cmd/stac.rs index 74b8cddda8..5988e4dba1 100644 --- a/crates/engine/src/cmd/stac.rs +++ b/crates/engine/src/cmd/stac.rs @@ -13,21 +13,27 @@ pub fn commands() -> Vec { vec![ C::new("stac.collections", "List STAC Collections", "Home › Add › Web Data › STAC Search", collections) .view() - .params(r#"{"url"?: "a STAC API (default: Earth Search, https://earth-search.aws.element84.com/v1; Planetary Computer: https://planetarycomputer.microsoft.com/api/stac/v1), or a static catalog's catalog.json or collection.json (its child catalogs and collections are listed)"}"#), + .params(r#"{"url"?: "a STAC API (default: Earth Search, https://earth-search.aws.element84.com/v1; Planetary Computer: https://planetarycomputer.microsoft.com/api/stac/v1), or a static catalog's catalog.json or collection.json (its child catalogs and collections are listed)", "connection"?: "as service.add: the saved web-service connection whose login the request uses"}"#), C::new("stac.search", "STAC Search", "Home › Add › Web Data › STAC Search", search).params( - r#"{"url"?: "a STAC API (default: Earth Search), or a static catalog's catalog.json or collection.json (walked from there, at most 400 documents)", "collections"?: id | [ids] (APIs), "bbox"?: [west, south, east, north] | "view" (the map's extent), "datetime"?: "2024-06-01" | "2024-06-01/2024-06-30", "limit"?: 1–200 (default 10), "maxCloud"?: percent, "sortby"?: "eo:cloud_cover" | "-datetime" (APIs that sort), "phase"?: "pre" | "post" (static catalogs of events), "properties"?: bool (every property of each item, not only its date and cloud cover; default false), "footprints"?: bool (adds the items' footprints as a layer, default true)}"#, + r#"{"url"?: "a STAC API (default: Earth Search), or a static catalog's catalog.json or collection.json (walked from there, at most 400 documents)", "collections"?: id | [ids] (APIs), "bbox"?: [west, south, east, north] | "view" (the map's extent), "datetime"?: "2024-06-01" | "2024-06-01/2024-06-30", "limit"?: 1–200 (default 10), "maxCloud"?: percent, "sortby"?: "eo:cloud_cover" | "-datetime" (APIs that sort), "phase"?: "pre" | "post" (static catalogs of events), "properties"?: bool (every property of each item, not only its date and cloud cover; default false), "footprints"?: bool (adds the items' footprints as a layer, default true), "connection"?: "as service.add: the saved web-service connection whose login the request uses"}"#, ), C::new("stac.addFootprints", "Add STAC Footprints", "Home › Add › Web Data › STAC Search", add_footprints_cmd) .params(r#"{"footprints": GeoJSON FeatureCollection (items' footprints, as STAC Search finds them), "name"?: text}"#), C::new("stac.addCube", "Add STAC Data Cube", "Home › Add › Web Data › STAC Search", add_cube).params( - r#"{"url"?: a STAC API (default: Earth Search), "collection": id, "asset": key (a band, such as NDVI), "bbox"?: [west, south, east, north] | "view" (default: the map's extent), "start", "end": dates, "maxCloud"?: percent, "range"?: [low, high] (the values shown from black to white, in the asset's own units after its raster:bands scale and offset, the same for every date and tile; default: each file's 2nd–98th percentile), "name"?: text} → a tile layer per tile of the grid the items cover, each a file per date following the time slider, which turns on over their dates when it is off (desktop)"#, + r#"{"url"?: a STAC API (default: Earth Search), "collection": id, "asset": key (a band, such as NDVI), "bbox"?: [west, south, east, north] | "view" (default: the map's extent), "start", "end": dates, "maxCloud"?: percent, "range"?: [low, high] (the values shown from black to white, in the asset's own units after its raster:bands scale and offset, the same for every date and tile; default: each file's 2nd–98th percentile), "name"?: text, "connection"?: "as service.add: the saved web-service connection whose login the request uses"} → a tile layer per tile of the grid the items cover, each a file per date following the time slider, which turns on over their dates when it is off (desktop)"#, ), C::new("stac.addAsset", "Add STAC Asset", "Home › Add › Web Data › STAC Search", add_asset).params( - r#"{"item": a STAC item (as stac.search returns it) | "url": an item's address (Planetary Computer assets are signed), "asset"?: key (default: visual, else the first GeoTIFF, else the first data asset), "name"?: text}"#, + r#"{"item": a STAC item (as stac.search returns it) | "url": an item's address (Planetary Computer assets are signed), "asset"?: key (default: visual, else the first GeoTIFF, else the first data asset), "name"?: text, "connection"?: "as service.add"}"#, ), ] } +fn request_api(s: &mut Session, p: &Value) -> Result { + let bound = crate::service_logins::request_params(s, p)?; + let api = stac::api(ps(bound.as_ref(), "url"))?; + crate::service_logins::request(s, ps(bound.as_ref(), "connection"), &api) +} + /// The search a command describes (`view` is the map's current extent). pub fn search_params(s: &Session, p: &Value) -> Result { let collections = match p.get("collections") { @@ -107,8 +113,8 @@ pub(crate) fn add_collection(s: &mut Session, fc: &Value, name: &str) -> Result< Ok(Some(id)) } -pub(super) fn collections(_s: &mut Session, p: &Value) -> CmdResult { - let api = stac::api(ps(p, "url"))?; +pub(super) fn collections(s: &mut Session, p: &Value) -> CmdResult { + let api = request_api(s, p)?; #[cfg(target_arch = "wasm32")] { let _ = api; @@ -139,7 +145,7 @@ pub(super) fn collections_json(api: &str, bytes: &[u8]) -> CmdResult { } fn search(s: &mut Session, p: &Value) -> CmdResult { - let api = stac::api(ps(p, "url"))?; + let api = request_api(s, p)?; if stac::catalog::is_static(&api) { return search_static(s, p, &api); } @@ -240,13 +246,19 @@ fn item(p: &Value) -> Result { } fn add_asset(s: &mut Session, p: &Value) -> CmdResult { + let bound = crate::service_logins::request_params(s, p)?; + let p = bound.as_ref(); let i = item(p)?; let (key, href) = i.asset_href(ps(p, "asset"))?; let name = ps(p, "name").map(str::trim).filter(|n| !n.is_empty()).map(str::to_string).unwrap_or_else(|| format!("{} {key}", i.id)); // A Planetary Computer asset is read with its storage container's token. #[cfg(not(target_arch = "wasm32"))] let href = stac::sign_blocking(&href)?; - super::layer::add_url(s, &json!({"url": href, "name": name})) + let mut add = json!({"url": href, "name": name}); + if let Some(connection) = p.get("connection") { + add["connection"] = connection.clone(); + } + super::layer::add_url(s, &add) } /// The most items a data cube on the time slider takes (its search is paged up to this). @@ -308,8 +320,8 @@ pub(crate) fn period_after(v: &Value) -> Option { /// groups the items' asset by tile and day: `{"url"?, "collection", "asset", "bbox"? | "view", /// "start", "end", "maxCloud"?}`. #[cfg(not(target_arch = "wasm32"))] -pub fn cube_files(s: &Session, p: &Value, max_items: usize) -> Result { - let api = stac::api(ps(p, "url"))?; +pub fn cube_files(s: &mut Session, p: &Value, max_items: usize) -> Result { + let api = request_api(s, p)?; if stac::catalog::is_static(&api) { return Err("a data cube needs a STAC API's search; a static catalog can't be searched".into()); } @@ -348,6 +360,7 @@ pub fn cube_files(s: &Session, p: &Value, max_items: usize) -> Result Result Result { +pub fn stac_cube(s: &mut Session, p: &Value) -> Result { use crate::render::rustgis_tiles::cog; // The parameters read here, before the search and the first header are waited for. let size = match p.get("cellSize").filter(|v| !v.is_null()) { @@ -618,8 +631,8 @@ pub fn stac_cube(s: &Session, p: &Value) -> Result { if let Some(o) = searched.as_object_mut() { o.insert("bbox".into(), json!(bbox)); } - let CubeFiles { tiles, scales, nodata, collection, asset, .. } = - cube_files(s, &searched, if s.long_runs.is_some() { MAX_SERIES_ITEMS } else { MAX_CUBE_ITEMS })?; + let max_items = if s.long_runs.is_some() { MAX_SERIES_ITEMS } else { MAX_CUBE_ITEMS }; + let CubeFiles { tiles, scales, nodata, collection, asset, .. } = cube_files(s, &searched, max_items)?; // Every tile's file for a date, by day. let mut by_day: std::collections::BTreeMap)>> = Default::default(); for days in tiles.values() { @@ -876,6 +889,8 @@ fn add_cube(s: &mut Session, p: &Value) -> CmdResult { } #[cfg(not(target_arch = "wasm32"))] { + let bound = crate::service_logins::request_params(s, p)?; + let p = bound.as_ref(); let range = match p.get("range").filter(|v| !v.is_null()) { None => None, Some(v) => { @@ -945,8 +960,9 @@ fn add_cube(s: &mut Session, p: &Value) -> CmdResult { // Every file a web address of sane length, as a project keeps them (TileLayer::repair), // so the cube reopens as it was made. if let Some(bad) = tiles.values().flat_map(|by_day| by_day.values()).find(|h| { - let lower = h.to_ascii_lowercase(); - !(lower.starts_with("https://") || lower.starts_with("http://")) || h.len() > 4000 + let plain = crate::render::rustgis_tiles::logins::inner(h); + let lower = plain.to_ascii_lowercase(); + !(lower.starts_with("https://") || lower.starts_with("http://")) || plain.len() > 4000 }) { return Err(format!( "{}: a data cube's files are read in place from web addresses (http, https)", @@ -958,7 +974,7 @@ fn add_cube(s: &mut Session, p: &Value) -> CmdResult { dates.extend(urls.iter().map(|(d, _)| *d)); let Some((_, first)) = urls.first() else { continue }; let name = if many { format!("{base} {tile}") } else { base.clone() }; - let r = match super::layer::add_url(s, &json!({"url": first, "name": name})) { + let r = match super::layer::add_url(s, &json!({"url": first, "name": name, "connection":p.get("connection")})) { Ok(r) => r, Err(e) => { undo(s); @@ -1067,3 +1083,65 @@ mod tests { assert_eq!(super::offset_in(&GeoTransform { pixel_width: 463.3, ..grid }, &file), None); } } + +#[cfg(all(test, not(target_arch = "wasm32")))] +#[allow(clippy::unwrap_used)] +mod deferred_asset_tests { + use super::*; + + #[test] + fn a_deferred_asset_download_keeps_its_connection_on_the_finished_layer() { + let mut s = Session::new(crate::Project::default()); + let connection = s + .run( + "connection.new", + &json!({ + "kind": "stac", "name": "Public catalog", "auth": "none", + "address": "https://deferred757.example.org/stac" + }), + ) + .unwrap()["id"] + .as_str() + .unwrap() + .to_string(); + let pending = s + .run_leaving_downloads( + "stac.addAsset", + &json!({ + "connection": connection, "asset": "data", "name": "Downloaded asset", "as": "not-forwarded", + "item": {"type": "Feature", "stac_version": "1.0.0", "id": "scene", + "geometry": {"type": "Point", "coordinates": [1, 2]}, + "properties": {"datetime": "2025-01-01T00:00:00Z"}, + "links": [], "assets": {"data": { + "href": "https://deferred757.example.org/data.geojson", + "type": "application/geo+json", "roles": ["data"] + }}} + }), + ) + .unwrap(); + assert_eq!(pending["pending"], true); + let mut requests = s.take_url_requests(); + assert_eq!(requests.len(), 1); + let mut request = requests.remove(0); + let download = s.download_started(); + assert!( + s.download_landed( + download, + json!({ + "type": "FeatureCollection", "features": [{ + "type": "Feature", "geometry": {"type": "Point", "coordinates": [1, 2]}, + "properties": {"name": "landed"} + }] + }) + .to_string() + .into_bytes() + ) + ); + request["id"] = json!(download); + let added = s.run("layer.addDownload", &request).unwrap(); + let layer = s.map().layer(added["layer"].as_u64().unwrap() as u32).unwrap(); + assert_eq!(layer.connection, connection); + assert_eq!(layer.name, "Downloaded asset"); + assert_eq!(layer.dataset.features.len(), 1); + } +} diff --git a/crates/engine/src/cmd/tiles.rs b/crates/engine/src/cmd/tiles.rs index f4c48b0132..5730ff9823 100644 --- a/crates/engine/src/cmd/tiles.rs +++ b/crates/engine/src/cmd/tiles.rs @@ -65,7 +65,7 @@ pub const NETWORK: &[&str] = &[ pub fn commands() -> Vec { vec![ C::new("map.addTiles", "Add Tile Layer", "Home › Add", add).params( - r#"{"url": "https://…/{z}/{x}/{y}.png" | vector tiles ".pbf" or TileJSON | MapLibre style.json | a PMTiles archive (web address or file path) | an MBTiles file path, "kind"?: "raster"|"vector"|"style"|"pmtiles"|"mbtiles" (guessed from the url), "name"?: text, "bounds"?: [west, south, east, north] (longitude/latitude coverage, when known), "attribution"?: text, "minZoom"?: number, "maxZoom"?: number, "tileSize"?: 256|512 (1024 too for a WMS or map service asked for in Web Mercator by its tiles' box: their pictures are asked for that size), "singleImage"?: bool (a WMS or map service: one picture of the view instead of tiles, so symbols and labels aren't cut at tile edges; tiles on the globe, a tilted map or a projection the service doesn't offer; by default on for one, unless tileSize is given), "tms"?: bool, "subdomains"?: [text], "layers"?: [vector tile layer names, drawn in colours of their own], "styleLayers"?: [MapLibre style layers drawing vector tiles instead (their source is the tiles')], "opacity"?: 0–1, "blend"?: "normal"|"multiply"|"screen"|"overlay"|"darken"|"lighten"|"difference" (how it combines with what is beneath), "overlay"?: bool (on top of the map's layers, as every new layer goes; without it a basemap, under them; one with the same address and role is replaced when it has the same name or the same look), "top"?: bool (on top of the map's layers, as a layer added as data goes; by default an overlay's place), "replace"?: bool (removes the other tile basemaps, not overlays), "id"?: catalog id, "view"?: bool (a MapLibre style: go to the place, zoom, bearing and pitch its root properties give, once it loads), "start"?, "end"?: dates a url with date placeholders ({date:YYYY-MM-DD}, {YYYY}) spans, turning the time slider on (or, when it is on, setting its span), "unit"?, "interval"?, "dates"?: its steps (as in time.set), "legend"?: "the web address of its legend picture (PNG or JPEG)" | [address | {"url": address, "minScale"?: scale denominator, "maxScale"?: one (the scales it is for, from minScale up to below maxScale)}] (at most 16; the Layers pane shows the one for the map's scale under the layer; service.add gives a WMS or WMTS layer its service's)}"#, + r#"{"url": "https://…/{z}/{x}/{y}.png" | vector tiles ".pbf" or TileJSON | MapLibre style.json | a PMTiles archive (web address or file path) | an MBTiles file path, "kind"?: "raster"|"vector"|"style"|"pmtiles"|"mbtiles" (guessed from the url), "name"?: text, "bounds"?: [west, south, east, north] (longitude/latitude coverage, when known), "attribution"?: text, "minZoom"?: number, "maxZoom"?: number, "tileSize"?: 256|512 (1024 too for a WMS or map service asked for in Web Mercator by its tiles' box: their pictures are asked for that size), "singleImage"?: bool (a WMS or map service: one picture of the view instead of tiles, so symbols and labels aren't cut at tile edges; tiles on the globe, a tilted map or a projection the service doesn't offer; by default on for one, unless tileSize is given), "tms"?: bool, "subdomains"?: [text], "layers"?: [vector tile layer names, drawn in colours of their own], "styleLayers"?: [MapLibre style layers drawing vector tiles instead (their source is the tiles')], "opacity"?: 0–1, "blend"?: "normal"|"multiply"|"screen"|"overlay"|"darken"|"lighten"|"difference" (how it combines with what is beneath), "overlay"?: bool (on top of the map's layers, as every new layer goes; without it a basemap, under them; one with the same address and role is replaced when it has the same name or the same look), "top"?: bool (on top of the map's layers, as a layer added as data goes; by default an overlay's place), "replace"?: bool (removes the other tile basemaps, not overlays), "id"?: catalog id, "view"?: bool (a MapLibre style: go to the place, zoom, bearing and pitch its root properties give, once it loads), "start"?, "end"?: dates a url with date placeholders ({date:YYYY-MM-DD}, {YYYY}) spans, turning the time slider on (or, when it is on, setting its span), "unit"?, "interval"?, "dates"?: its steps (as in time.set), "legend"?: "the web address of its legend picture (PNG or JPEG)" | [address | {"url": address, "minScale"?: scale denominator, "maxScale"?: one (the scales it is for, from minScale up to below maxScale)}] (at most 16; the Layers pane shows the one for the map's scale under the layer; service.add gives a WMS or WMTS layer its service's), "connection"?: "the id of the saved web-service connection it comes from (as service.add): the tile layer records it, and its tiles are read with its login"}"#, ), C::new("map.removeTiles", "Remove Tile Layer", "Home › Add", remove) .params(r#"{"name"?: text, "index"?: number, "id"?: catalog id} (removes every tile layer when none is given)"#), @@ -200,6 +200,8 @@ pub(crate) fn list(s: &Session, redact: bool) -> Value { const NOT_ON_REQUEST: &str = "singleImage is for a layer its service draws on request (a WMS, an ArcGIS map or image service)"; pub(crate) fn add(s: &mut Session, p: &Value) -> CmdResult { + let bound = crate::service_logins::request_params(s, p)?; + let p = bound.as_ref(); let url = ps(p, "url").map(str::trim).filter(|u| !u.is_empty()).ok_or("url is required")?; let kind = match ps(p, "kind") { Some(k) => TileKind::from_code(k).ok_or_else(|| format!("unknown kind '{k}' (raster, vector, style, pmtiles or mbtiles)"))?, @@ -272,7 +274,7 @@ pub(crate) fn add(s: &mut Session, p: &Value) -> CmdResult { t.id = id.to_string(); } if let Some(v) = p.get("legend").filter(|v| !v.is_null()) { - t.legends = legends(v)?; + t.legends = legends(s, ps(p, "connection"), v)?; } t.repair()?; // A layer its service draws on request: one picture of the view, or tiles of the size given @@ -577,12 +579,13 @@ fn cog_look(p: &Value) -> Result Look>>, String> { /// A legend given to `map.addTiles` or `map.setTiles`: an address, or a list of addresses and /// `{url, minScale?, maxScale?}` (the scales each is for). Each must be a web address; a scale -/// that isn't a positive number is none. -fn legends(v: &Value) -> Result, String> { +/// that isn't a positive number is none. A login wrapper in it must be this session's own (or is +/// bound to `connection`): another session's token never gives this one its credential. +fn legends(s: &mut Session, connection: Option<&str>, v: &Value) -> Result, String> { use rustgis_data::ogc::{Legend, MAX_LEGENDS}; let bad = || "legend must be a web address, or a list of addresses and {url, minScale?, maxScale?}".to_string(); - let one = |v: &Value| -> Result { - let legend = match v { + let mut one = |v: &Value| -> Result { + let mut legend = match v { Value::String(url) => Legend { url: url.clone(), ..Legend::default() }, Value::Object(o) => { let url = o.get("url").and_then(Value::as_str).ok_or_else(bad)?; @@ -591,6 +594,11 @@ fn legends(v: &Value) -> Result, String> { } _ => return Err(bad()), }; + // A wrapper must be this session's own (checked on the trimmed text the layer keeps). + legend.url = legend.url.trim().to_string(); + if rustgis_data::login_address::is_wrapper(&legend.url) { + legend.url = crate::service_logins::request(s, connection.filter(|c| !c.trim().is_empty()), &legend.url)?; + } // Kept as a tile layer keeps it, or refused: an address the layer wouldn't keep. rustgis_map::tiles::checked_legends([legend.clone()]).pop().ok_or_else(|| { let url: String = legend.url.chars().take(200).collect(); @@ -628,7 +636,7 @@ fn set(s: &mut Session, p: &Value) -> CmdResult { let new_legends = match p.get("legend") { None => None, Some(Value::Null) => Some(Vec::new()), - Some(v) => Some(legends(v)?), + Some(v) => Some(legends(s, None, v)?), }; let hidden = hidden_layers(p)?; let old_name = s.map().tiles.get(i).map(|t| t.name.clone()); @@ -790,4 +798,53 @@ mod tests { assert_eq!(p.map().tiles[0].blend, Blend::Multiply); assert_eq!(text.matches("\"blend\"").count(), 1, "{text}"); } + + /// A login wrapper given as a legend or a download's address is that session's own, or refused. + #[test] + fn another_sessions_login_address_is_refused_in_legends_and_downloads() { + use crate::{render::rustgis_tiles::logins, service_logins::NOT_THIS_SESSION}; + let origin = "https://legend-owner757.example.org"; + let definition = json!({"kind": "wms", "address": format!("{origin}/wms"), "auth": "header", "keyName": "X-Key"}); + let fresh = || { + let mut s = Session::new(Project::default()); + s.database_logins.set_store(None); + s + }; + let id_of = |s: &mut Session| s.run("connection.new", &definition).unwrap()["id"].as_str().unwrap().to_string(); + let (mut a, mut b) = (fresh(), fresh()); + let (ida, idb) = (id_of(&mut a), id_of(&mut b)); + a.run("connection.login", &json!({"id": ida, "apiKey": "owner-a-757"})).unwrap(); + let theirs = crate::service_logins::request(&mut a, Some(&ida), &format!("{origin}/legend.png")).unwrap(); + let key = |address: &str| logins::apply(address).unwrap().and_then(|x| x.header).map(|(_, v)| v); + assert_eq!(key(&theirs).as_deref(), Some("owner-a-757")); + // The tile layer itself is on another origin, so only the legend carries a wrapper. + let tiles = + |legend: serde_json::Value| json!({"url": "https://basemap757.example.org/{z}/{x}/{y}.png", "name": "T", "legend": legend}); + for legend in [json!(theirs), json!(format!(" {theirs}")), json!({"url": theirs}), json!([theirs])] { + assert_eq!(b.run("map.addTiles", &tiles(legend)).unwrap_err(), NOT_THIS_SESSION); + } + assert!(b.map().tiles.is_empty()); + // The owner keeps its own wrapper. + a.run("map.addTiles", &tiles(json!(theirs))).unwrap(); + assert_eq!(a.map().tiles[0].legends[0].url, theirs); + // Set on an existing layer: refused and left as it was. + b.run("map.addTiles", &json!({"url": "https://basemap757.example.org/{z}/{x}/{y}.png", "name": "T"})).unwrap(); + assert_eq!(b.run("map.setTiles", &json!({"name": "T", "legend": theirs})).unwrap_err(), NOT_THIS_SESSION); + assert!(b.map().tiles[0].legends.is_empty()); + // Given with its own connection, the wrapper's plain address is bound to that login. + b.run("connection.login", &json!({"id": idb, "apiKey": "owner-b-757"})).unwrap(); + let mut add = tiles(json!(theirs)); + add["connection"] = json!(idb); + add["replace"] = json!(true); + b.run("map.addTiles", &add).unwrap(); + let mine = b.map().tiles[0].legends[0].url.clone(); + assert_ne!(mine, theirs); + assert_eq!(key(&mine).as_deref(), Some("owner-b-757")); + // A downloaded file's address: refused before the download is taken. + let id = b.download_started(); + assert!(b.download_landed(id, b"{}".to_vec())); + let err = b.run("layer.addDownload", &json!({"id": id, "url": theirs})).unwrap_err(); + assert_eq!(err, NOT_THIS_SESSION); + assert!(b.downloaded.iter().any(|(d, _)| *d == id)); + } } diff --git a/crates/engine/src/connections.rs b/crates/engine/src/connections.rs index 3fe2007892..76d848e000 100644 --- a/crates/engine/src/connections.rs +++ b/crates/engine/src/connections.rs @@ -19,6 +19,13 @@ const MAX_NAME: usize = 200; pub const KINDS: &[&str] = &["postgres", "mysql", "sqlserver", "service", "wms", "wmts", "wfs", "wcs", "ogcapi", "arcgis", "csw", "stac", "s3"]; pub const DATABASE_KINDS: &[&str] = &["postgres", "mysql", "sqlserver"]; +/// The web-service kinds, which can log in with a [`SERVICE_AUTH`] method. +pub const SERVICE_KINDS: &[&str] = &["service", "wms", "wmts", "wfs", "wcs", "ogcapi", "arcgis", "csw", "stac"]; +/// How a web-service connection logs in: not at all, HTTP basic authentication (a user name and a +/// password), or an API key in a header. +pub const SERVICE_AUTH: &[&str] = &["none", "basic", "header"]; +/// The kind of a [`LoginRequest`] for a web service's origin (its `key`). +pub const WEB: &str = "web"; pub const S3_CREDENTIALS: &[&str] = &["keys", "profile", "environment", "none"]; pub const DESKTOP_ONLY: &str = "database connections are used by the desktop app"; @@ -48,6 +55,16 @@ pub struct Connection { /// remember it, and cleared when the saved password is forgotten or no longer fits. #[serde(default, skip_serializing_if = "std::ops::Not::not")] pub remembered: bool, + /// How a web-service connection logs in ([`SERVICE_AUTH`]; empty: it doesn't). The method + /// only: the password or key is given to `connection.login`, never kept here. + #[serde(default, skip_serializing_if = "String::is_empty")] + pub auth: String, + /// The user name of a basic-authentication login. + #[serde(default, skip_serializing_if = "String::is_empty")] + pub user: String, + /// The header an API key goes in (`auth` header). + #[serde(default, rename = "keyName", skip_serializing_if = "String::is_empty")] + pub key_name: String, } #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] @@ -117,6 +134,61 @@ fn name_text(t: &str) -> String { t.trim().chars().filter(|c| !c.is_control()).take(MAX_NAME).collect() } +/// Checks a connection's login method and the fields it uses. +fn clean_login(c: &mut Connection) -> Result<(), String> { + use crate::render::rustgis_tiles::logins; + let auth = c.auth.trim().to_ascii_lowercase(); + c.auth = if auth == "none" { String::new() } else { auth }; + c.user = c.user.trim().to_string(); + c.key_name = c.key_name.trim().to_string(); + if c.auth.is_empty() { + c.user.clear(); + c.key_name.clear(); + return Ok(()); + } + if !SERVICE_KINDS.contains(&c.kind.as_str()) { + return Err(format!("auth is for web-service connections ({}), not {}", SERVICE_KINDS.join(", "), c.kind)); + } + match c.auth.as_str() { + "basic" => { + c.key_name.clear(); + if c.user.is_empty() || c.user.chars().count() > MAX_NAME || c.user.chars().any(|ch| ch.is_control() || ch == ':') { + return Err(format!( + "basic authentication needs a user of at most {MAX_NAME} characters, without ':' or control characters" + )); + } + Ok(()) + } + "header" => { + c.user.clear(); + if !logins::valid_header_name(&c.key_name) { + return Err(format!( + "keyName must be an HTTP header name of at most {} letters, digits or one of !#$%&'*+-.^_`|~ (not one the app sets itself: Host, Cookie, Range, Proxy-… or Sec-…, among others)", + logins::MAX_NAME + )); + } + Ok(()) + } + other => Err(format!("unknown auth '{other}' ({})", SERVICE_AUTH.join(", "))), + } +} + +/// The origin (`scheme://host[:port]`) a web-service connection's login goes to: the only one. +pub fn login_origin(c: &Connection) -> Option { + crate::render::rustgis_tiles::logins::origin(&c.address) +} + +/// Whether two definitions retain the same login identity: a database password uses the same +/// database address; a session web login uses the same origin, method, user and key name. +pub fn same_login(a: &Connection, b: &Connection) -> bool { + a.kind == b.kind + && if DATABASE_KINDS.contains(&a.kind.as_str()) { + a.address == b.address + } else { + a.auth == b.auth && a.user == b.user && a.key_name == b.key_name && login_origin(a) == login_origin(b) + } +} + pub fn clean(mut c: Connection) -> Result<(Connection, bool), String> { if !KINDS.contains(&c.kind.as_str()) { return Err(format!("unknown kind '{}' ({})", c.kind, KINDS.join(", "))); @@ -141,6 +213,7 @@ pub fn clean(mut c: Connection) -> Result<(Connection, bool), String> { } } c.address = stored; + clean_login(&mut c)?; c.name = name_text(&c.name); if c.name.is_empty() { c.name = if c.kind == "s3" && c.address.is_empty() { "AWS S3".into() } else { sources::default_name(&c.address) }; @@ -459,6 +532,9 @@ pub enum LoginReason { Choose, /// No password is usable (the saved one was refused): the user types one. Password, + /// Layers recorded a saved connection this computer doesn't have (or one for another + /// address), and none of its connections logs in to their address: the user makes one. + Connection, } impl LoginReason { @@ -466,6 +542,7 @@ impl LoginReason { match self { LoginReason::Choose => "choose", LoginReason::Password => "password", + LoginReason::Connection => "connection", } } } @@ -481,6 +558,8 @@ pub struct LoginRequest { pub connections: Vec, /// The layers that wait, by id. pub layers: Vec, + /// How many tile layers wait (they have no id). + pub tiles: usize, /// [`crate::Session::opened`] when it was made: a project opened since has other layers. pub opened: u64, /// The app has asked the user. diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 73dd1d920f..131ad29113 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -18,6 +18,7 @@ pub mod s3_keys; #[cfg(test)] mod s3_saved_tests; pub mod sample; +pub mod service_logins; pub mod sources; #[cfg(test)] mod tests; @@ -521,6 +522,9 @@ pub struct Session { /// and login requests. S3 uses the same store through `DatabaseLogins::secret_store`. /// Never saved; see [`connections::DatabaseLogins`]. pub database_logins: connections::DatabaseLogins, + /// The web-service logins this session made the ones sent to an origin, which the process's + /// fetches share: they end with it (see [`service_logins`]). + pub(crate) web_logins: service_logins::SessionLogins, /// How rasters added from now on are drawn (`raster.setDefaultStyle`; kept with the app's /// preferences, not the project). pub raster_defaults: rustgis_map::RasterDefaults, @@ -838,6 +842,7 @@ impl Session { s3_bindings: std::collections::BTreeMap::new(), s3_pending: Vec::new(), database_logins: connections::DatabaseLogins::default(), + web_logins: service_logins::SessionLogins::new(), raster_defaults: rustgis_map::RasterDefaults::default(), registry: Registry { commands: cmd::all() }, revision: 1, @@ -1128,7 +1133,15 @@ impl Session { return Err(e); } self.merge = merge; - let res = self.guarded(label, mutates, |s| run(s, params)); + // A layer added from a saved web-service connection records it, inside the command's step. + let res = self.guarded(label, mutates, |s| { + let before = service_logins::before(s, id, params)?; + let out = run(s, params)?; + if let Some(before) = before { + service_logins::tag(s, params, before, &out)?; + } + Ok(out) + }); if id == "layer.addDownload" && res.is_ok() && let Some(token) = params.get("geolibreLayer").and_then(|v| v.get("token")).and_then(serde_json::Value::as_str) @@ -1141,7 +1154,8 @@ impl Session { { self.sources.note(src); } - res + // An error never quotes a web-service connection's password or key. + res.map_err(|e| render::rustgis_tiles::logins::scrub(&e)) } /// Retires only this consumed reservation, preserving unrelated pending imports in history. @@ -1459,7 +1473,9 @@ impl Session { } pub fn notify(&mut self, text: impl Into, error: bool) { - self.notices.push(Notice { text: text.into(), error }); + // A web-service connection's password or key never shows, whatever quoted it. + let text = render::rustgis_tiles::logins::scrub(&text.into()); + self.notices.push(Notice { text, error }); self.notices_given += 1; if self.notices.len() > 200 { self.notices.remove(0); diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index aa574f29fb..3fcd561c9a 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -63,6 +63,9 @@ pub const MAX_BYTES: u64 = 512 << 20; /// through a saved S3 connection by the address it wraps, the wrapper rebuilt around it (its /// binding and endpoint are provenance, not secrets); a COG's look after either is kept. fn saved_address(address: &str) -> Option { + if let Some(public) = without_login_binding(address) { + return Some(saved_address(&public).unwrap_or(public)); + } use rustgis_data::s3_address; if let Some((head, archive)) = crate::sources::template_archive(address) { let (file, look) = split_look(archive); @@ -103,6 +106,60 @@ pub(crate) fn strip_addresses(addresses: Vec<&mut String>) -> Vec addresses.into_iter().map(|a| saved_address(a).map(|redacted| std::mem::replace(a, redacted))).collect() } +/// Runtime capabilities never survive a project boundary, including an in-process save restore. +/// `None` for an address that holds none, and for a wrapper that doesn't parse (kept as it is, +/// never emptied: its layer then fails to read, saying why). +fn without_login_binding(address: &str) -> Option { + if rustgis_data::login_address::is_wrapper(address) { + return rustgis_data::login_address::parse(address).ok().flatten().map(|b| b.url.to_string()); + } + if let Some((head, archive)) = crate::sources::template_archive(address) { + return without_login_binding(archive).map(|public| format!("{head}{public}")); + } + if let Ok(Some(bound)) = rustgis_data::s3_address::parse(address) { + return without_login_binding(bound.url) + .and_then(|public| rustgis_data::s3_address::bind(bound.binding, bound.endpoint, &public).ok()); + } + None +} + +/// Whether stripping `original` from a saved file lost a secret the user must give again: not +/// when all it held was a login binding (the layer records its connection, and asks for its login +/// when it opens), unless the address under the binding held one too. +fn lost_secret(original: &str) -> bool { + without_login_binding(original).is_none_or(|public| saved_address(&public).is_some()) +} + +pub(crate) fn clear_login_bindings<'a>(addresses: impl IntoIterator) { + for address in addresses { + if let Some(public) = without_login_binding(address) { + *address = public; + } + } +} + +/// Clears login bindings, keeping the originals in the visitor's order for the caller to restore. +fn take_login_bindings(addresses: Vec<&mut String>) -> Vec> { + addresses.into_iter().map(|a| without_login_binding(a).map(|public| std::mem::replace(a, public))).collect() +} + +pub(crate) fn recorded_addresses<'a>(value: &'a mut serde_json::Value, addresses: &mut Vec<&'a mut String>) { + match value { + serde_json::Value::String(text) => addresses.push(text), + serde_json::Value::Array(values) => { + for value in values { + recorded_addresses(value, addresses); + } + } + serde_json::Value::Object(values) => { + for value in values.values_mut() { + recorded_addresses(value, addresses); + } + } + _ => {} + } +} + struct Written { len: usize, hash: u64, @@ -515,6 +572,47 @@ impl Project { addresses } + /// The strings of recorded command parameters (geoprocessing history, tasks and models). They + /// are not layer addresses, and only login bindings are taken from them when saving or opening. + fn recorded_mut(&mut self) -> Vec<&mut String> { + let mut addresses = Vec::new(); + for record in &mut self.gp_history { + recorded_addresses(&mut record.params, &mut addresses); + if let Some((_, params)) = &mut record.command { + recorded_addresses(params, &mut addresses); + } + } + for task in &mut self.tasks { + for step in &mut task.steps { + recorded_addresses(&mut step.params, &mut addresses); + } + } + for model in &mut self.models { + for step in &mut model.steps { + recorded_addresses(&mut step.params, &mut addresses); + } + if let Some(graph) = &mut model.graph { + for node in &mut graph.nodes { + recorded_addresses(&mut node.params, &mut addresses); + } + } + } + addresses + } + + /// How many layers and tile layers read through a login binding but record no connection: + /// nothing would tell the opened project which login they need. + fn unrecorded_bound(&mut self) -> usize { + let mut n = 0; + for map in self.maps.iter_mut().chain(self.offline_areas.iter_mut().map(|area| &mut area.data)) { + n += map.tiles.iter().filter(|t| t.connection.is_empty() && without_login_binding(&t.url).is_some()).count(); + for layer in map.layers.iter_mut().filter(|l| l.connection.is_empty()) { + n += usize::from(layer.addresses_mut().any(|a| without_login_binding(a).is_some())); + } + } + n + } + /// The standalone table of an id. pub fn table(&self, id: u32) -> Option<&Table> { self.tables.iter().find(|t| t.id == id) @@ -768,8 +866,10 @@ impl Project { /// or RustGIS closes. Packages and imports never restore them. pub fn saved_text(&mut self, dir: Option<&Path>) -> Result<(String, usize), String> { self.ensure_imports_complete()?; + let unrecorded = self.unrecorded_bound(); let was = strip_addresses(self.addresses_mut()); - let n = was.iter().flatten().count(); + let recorded_was = take_login_bindings(self.recorded_mut()); + let n = was.iter().flatten().filter(|original| lost_secret(original)).count() + unrecorded; let originals: Vec> = if let Some(d) = dir.and_then(Path::to_str) { self.file_paths_mut() .into_iter() @@ -800,6 +900,11 @@ impl Project { *address = original.clone(); } } + for (address, original) in self.recorded_mut().into_iter().zip(&recorded_was) { + if let Some(original) = original { + *address = original.clone(); + } + } if let Ok(text) = &text && n > 0 { @@ -838,6 +943,7 @@ impl Project { map.tiles.retain(|tile| !tile.id.starts_with("geolibre-pending:")); } strip_addresses(self.addresses_mut()); + clear_login_bindings(self.recorded_mut()); let text = serde_json::to_string(&self).map_err(|e| e.to_string())?; if text.len() as u64 > MAX_BYTES { return Err("the project is too large to keep a recovery copy".into()); @@ -853,7 +959,7 @@ impl Project { if let Some(full) = original && saved_address(full).as_deref() == Some(address.as_str()) { - *address = full.clone(); + *address = without_login_binding(full).unwrap_or_else(|| full.clone()); } } } @@ -903,6 +1009,8 @@ impl Project { // A binding is this computer's runtime token for a login: one in a file is no authority // to use any login here. crate::s3_connections::clear_bindings(p.addresses_mut()); + clear_login_bindings(p.addresses_mut()); + clear_login_bindings(p.recorded_mut()); if let Some(d) = dir.and_then(Path::to_str) { p.resolve_paths(d); } @@ -949,3 +1057,114 @@ fn unique_names<'a>(names: impl Iterator) { } } } + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod login_binding_tests { + use super::*; + + #[test] + fn saves_and_imports_strip_runtime_capabilities_including_recorded_urls() { + let url = rustgis_data::login_address::bind( + "0123456789abcdef", + "https://project757.example.org", + "https://project757.example.org/tiles/{z}/{x}/{y}.png", + ) + .unwrap(); + let mut project = Project::default(); + project.map_mut().tiles.push(rustgis_map::TileLayer::new("Bound", rustgis_map::tiles::TileKind::Raster, &url)); + project.tasks.push(Task { + name: "Recorded".into(), + description: String::new(), + steps: vec![TaskStep { + title: "Add".into(), + instructions: String::new(), + command: "map.addTiles".into(), + params: serde_json::json!({"url": url, "connection": "saved-id"}), + }], + }); + let forged = serde_json::to_string(&project).unwrap(); + let (saved, _) = project.saved_text(None).unwrap(); + assert!(!saved.contains(rustgis_data::login_address::PREFIX)); + assert_eq!(project.map().tiles[0].url, url); + for text in [&saved, &forged] { + let opened = Project::from_json(text).unwrap(); + assert_eq!(opened.map().tiles[0].url, rustgis_data::login_address::inner(&url)); + assert_eq!(opened.tasks[0].steps[0].params["url"], rustgis_data::login_address::inner(&url)); + assert_eq!(opened.tasks[0].steps[0].params["connection"], "saved-id"); + } + let recovery = project.recovery_text().unwrap(); + assert!(!recovery.contains(rustgis_data::login_address::PREFIX)); + } + + fn bound(url: &str) -> String { + rustgis_data::login_address::bind("0123456789abcdef", "https://project757.example.org", url).unwrap() + } + + fn tiles(name: &str, url: &str, connection: &str) -> rustgis_map::TileLayer { + let mut t = rustgis_map::TileLayer::new(name, rustgis_map::tiles::TileKind::Raster, url); + t.connection = connection.into(); + t + } + + #[test] + fn recorded_parameters_are_not_layer_addresses() { + let mut project = Project::default(); + let note = "https://user@project757.example.org/data.csv#part"; + project.tasks.push(Task { + name: "Recorded".into(), + description: String::new(), + steps: vec![TaskStep { + title: "Add".into(), + instructions: String::new(), + command: "layer.addUrl".into(), + params: serde_json::json!({"url": note}), + }], + }); + assert!(project.addresses_mut().is_empty()); + let (saved, stripped) = project.saved_text(None).unwrap(); + assert_eq!(stripped, 0); + assert_eq!(Project::from_json(&saved).unwrap().tasks[0].steps[0].params["url"], note); + } + + #[test] + fn a_malformed_wrapper_is_never_emptied() { + let bad = "rustgis-login:nothex:5:https://project757.example.org/{z}/{x}/{y}.png"; + let mut project = Project::default(); + project.tasks.push(Task { + name: "Recorded".into(), + description: String::new(), + steps: vec![TaskStep { + title: "Add".into(), + instructions: String::new(), + command: "map.addTiles".into(), + params: serde_json::json!({"url": bad}), + }], + }); + let (saved, _) = project.saved_text(None).unwrap(); + let opened = Project::from_json(&saved).unwrap(); + assert_eq!(opened.tasks[0].steps[0].params["url"], bad); + let mut address = bad.to_string(); + clear_login_bindings([&mut address]); + assert_eq!(address, bad); + } + + #[test] + fn only_lost_secrets_are_counted_when_saving() { + let plain = "https://project757.example.org/{z}/{x}/{y}.png"; + let mut project = Project::default(); + project.map_mut().tiles.push(tiles("Recorded", &bound(plain), "saved-id")); + assert_eq!(project.saved_text(None).unwrap().1, 0, "a layer that records its connection asks for the login again"); + + project.map_mut().tiles.push(tiles("Unrecorded", &bound(plain), "")); + assert_eq!(project.saved_text(None).unwrap().1, 1, "a bound layer with no connection can't ask for one"); + + project.map_mut().tiles.clear(); + project.map_mut().tiles.push(tiles("Token", &format!("{plain}?token=abc123"), "")); + assert_eq!(project.saved_text(None).unwrap().1, 1, "a real token is still lost"); + + project.map_mut().tiles.clear(); + project.map_mut().tiles.push(tiles("Both", &bound(&format!("{plain}?token=abc123")), "saved-id")); + assert_eq!(project.saved_text(None).unwrap().1, 1, "the token under a binding is lost too"); + } +} diff --git a/crates/engine/src/service_logins.rs b/crates/engine/src/service_logins.rs new file mode 100644 index 0000000000..5f0f339752 --- /dev/null +++ b/crates/engine/src/service_logins.rs @@ -0,0 +1,748 @@ +//! Logins of saved web-service connections (WMS, WMTS, WFS, WCS, OGC API, ArcGIS REST, CSW, +//! STAC): HTTP basic authentication or an API key in a header. +//! +//! The connection keeps the method, user name and header name, never the password or key. +//! A secret given to `connection.login` lasts only for this session, held by connection id in +//! [`crate::connections::DatabaseLogins`]. Its runtime identity also binds it to the canonical +//! origin, method, user and header, so changing a definition cannot send an old secret elsewhere. +//! +//! Each session/connection login has an opaque request binding. Only requests carrying that +//! binding use its credential, and only at the connection's exact origin. Bindings distinguish +//! authenticated cache entries and are revoked on replacement, logout and session drop. Layers +//! record their saved connection id, never a trusted persisted runtime binding. A project opened +//! without its connection or login asks through the existing login-request queue. +//! Nothing here prints, returns or logs a secret: every failure is +//! a fixed sentence. + +use rustgis_data::secrets::Secret; +use serde_json::Value; +use std::collections::{BTreeMap, BTreeSet}; + +use crate::Session; +use crate::connections::{Connection, LoginReason, LoginRequest, SERVICE_KINDS, WEB, login_origin}; +use crate::render::rustgis_tiles::{cache, logins}; + +/// A transient secret whose connection identity changed. +pub const UNUSABLE: &str = "The session login could not be used. Enter it again."; +/// Persistence is a separate feature; a service login never accesses the credential store. +pub const SESSION_ONLY: &str = "Web-service logins last for this session only."; +/// A login typed for a plain http:// address without the confirmation. +pub const INSECURE: &str = "This connection's address uses plain http://, so its login would be sent unencrypted. Confirm with allowInsecure: true to send it anyway."; +/// A password or key that can't be sent. +pub const BAD_SECRET: &str = "The password or key must be at most 4096 bytes, without control characters."; +/// A login asked of a connection with nothing to log in with this session. +pub const REQUIRED: &str = "This connection's login is required: give its password (basic) or apiKey (header)."; +/// A login for a definition that isn't saved. +pub const UNSAVED: &str = "Save the connection first: a login belongs to a saved connection."; +/// An address a login can't go to. +pub const NO_ORIGIN: &str = "The connection's address has no http:// or https:// origin a login can go to."; +/// A Test must not send the edited form's credential to the saved endpoint. +pub const SAVE_BEFORE_TEST: &str = "Save changes before testing this connection."; +/// A bound address whose login is not one of this session's. +pub const NOT_THIS_SESSION: &str = "This request's login does not belong to this session."; +/// Several saved connections match an address and none was chosen. +pub const CHOOSE_CONNECTION: &str = "Several saved connections match this address. Choose a connection explicitly."; +/// A `connection` that is not a saved web-service connection's id. +pub const NO_SUCH_CONNECTION: &str = "No saved web-service connection with this id."; +/// The commands that add layers from a service, and so may record the connection they came from +/// (their `connection` parameter, or the connection whose login their address is read with). +pub const ADD_COMMANDS: &[&str] = &[ + "service.add", + "service.import", + "service.refresh", + "stac.search", + "stac.addAsset", + "stac.addCube", + "layer.addUrl", + "layer.addDownload", + "map.addTiles", +]; + +/// Whether `c` is a web-service connection (one that can log in with a method). +pub fn is_service(c: &Connection) -> bool { + SERVICE_KINDS.contains(&c.kind.as_str()) +} + +/// The secret a login of `c` sends, as its header method sends it. +fn method(c: &Connection, secret: &str) -> Option<(logins::Method, String)> { + match c.auth.as_str() { + "basic" => Some((logins::Method::Header("Authorization".into()), logins::basic(&c.user, secret))), + "header" => Some((logins::Method::Header(c.key_name.clone()), secret.to_string())), + _ => None, + } +} + +/// Checks a typed password or key. +pub fn check_secret(secret: &str) -> Result<(), &'static str> { + if logins::valid_value(secret) { Ok(()) } else { Err(BAD_SECRET) } +} + +/// Checks the complete wire credential before a form edit can invalidate an earlier login. +pub fn check_login_secret(c: &Connection, secret: &str) -> Result<(), String> { + check_secret(secret).map_err(str::to_string)?; + if c.auth == "basic" && !logins::valid_basic(&c.user, secret) { + return Err("The encoded Basic login exceeds the supported header size or contains invalid characters.".into()); + } + Ok(()) +} + +/// An active login belongs to one connection in one session; the token is not a secret, but +/// saved projects must never treat it as authority to use another session's login. +struct ActiveLogin { + origin: String, + binding: String, + auth: String, + user: String, + key_name: String, +} + +impl ActiveLogin { + fn matches(&self, c: &Connection) -> bool { + login_origin(c).as_deref() == Some(self.origin.as_str()) + && self.auth == c.auth + && self.user == c.user + && self.key_name == c.key_name + } +} + +pub struct SessionLogins { + session: u64, + active: BTreeMap, + /// A Test of a definition that isn't saved, or a plugin's command, is running: what it asks + /// for carries no login, whichever saved connection shares its origin. + anonymous: bool, +} + +impl SessionLogins { + pub fn new() -> Self { + static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1); + Self { session: NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed), active: BTreeMap::new(), anonymous: false } + } + + /// Starts or ends [`SessionLogins::anonymous`] reads, saying what it was. + pub(crate) fn set_anonymous(&mut self, on: bool) -> bool { + std::mem::replace(&mut self.anonymous, on) + } + + fn owner(&self, id: &str) -> String { + format!("{}/{id}", self.session) + } +} + +impl Default for SessionLogins { + fn default() -> Self { + Self::new() + } +} + +impl Drop for SessionLogins { + fn drop(&mut self) { + let mut ended = false; + for active in self.active.values() { + ended |= logins::deactivate(&active.binding); + } + if ended { + clear_cache(); + } + } +} + +#[cfg(test)] +thread_local! { + static CLEARS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// How many times this thread's logins dropped the tile cache (tests count it per thread, since +/// the cache itself is shared by every test running at once). +#[cfg(test)] +pub(crate) fn cache_clears() -> u32 { + CLEARS.with(std::cell::Cell::get) +} + +/// Drops cached responses: a login that ends or is replaced leaves authenticated ones behind. +fn clear_cache() { + #[cfg(test)] + CLEARS.with(|c| c.set(c.get() + 1)); + cache::clear(); +} + +/// Replaces only this connection's binding, after the complete new credential has validated. +/// Other sessions and other connections at this origin retain their own bindings. +pub(crate) fn activate(s: &mut Session, c: &Connection, secret: Option<&Secret>, insecure: bool) -> Result<(), String> { + let origin = login_origin(c).ok_or(NO_ORIGIN)?; + let next = match secret.and_then(|secret| method(c, secret.expose())) { + Some((method, value)) => { + let binding = logins::activate(&origin, &s.web_logins.owner(&c.id), method, &value, insecure).map_err(str::to_string)?; + Some(ActiveLogin { origin: origin.clone(), binding, auth: c.auth.clone(), user: c.user.clone(), key_name: c.key_name.clone() }) + } + None => None, + }; + let replaced = s.web_logins.active.remove(&c.id).map(|old| logins::deactivate(&old.binding)).is_some(); + if let Some(next) = next { + s.web_logins.active.insert(c.id.clone(), next); + } + s.database_logins.select(WEB, &origin, &c.id); + s.database_logins.clear_requests_for(WEB, &origin); + rebind_connection(s, c); + // Only a login that was replaced or removed leaves cached authenticated responses to drop; + // a first login, or none, changes no cached tile. + if replaced { + clear_cache(); + } + Ok(()) +} + +pub(crate) fn deactivate(s: &mut Session, c: &Connection, _origin: Option) { + if let Some(old) = s.web_logins.active.remove(&c.id) { + logins::deactivate(&old.binding); + clear_cache(); + } + if let Some(origin) = login_origin(c) + && s.database_logins.selection(WEB, &origin) == Some(c.id.as_str()) + { + s.database_logins.drop_selection(WEB, &origin); + } +} + +pub(crate) fn logout(s: &mut Session, c: &Connection) { + forget(s, c); +} + +pub(crate) fn forget(s: &mut Session, c: &Connection) { + deactivate(s, c, None); + s.database_logins.forget_connection(&c.id); +} + +pub fn logged_in(s: &Session, c: &Connection) -> bool { + s.web_logins.active.get(&c.id).is_some_and(|a| a.matches(c) && logins::is_active(&a.binding)) +} + +pub fn known_secret(s: &Session, c: &Connection) -> Result { + let Some(secret) = s.database_logins.session_password(&c.id) else { + return Err(REQUIRED.into()); + }; + if !s.web_logins.active.get(&c.id).is_some_and(|a| a.matches(c)) { + return Err(UNUSABLE.into()); + } + Ok(secret.clone()) +} + +/// Binds a request to a connection chosen in this session. A bare address can resolve to the +/// explicit session choice or sole saved definition, never the first of several matches. +pub fn request(s: &mut Session, connection: Option<&str>, url: &str) -> Result { + let plain = logins::inner(url); + let Some(origin) = logins::origin(plain) else { return Ok(plain.to_string()) }; + if s.web_logins.anonymous && connection.is_none() { + return Ok(plain.to_string()); + } + if plain != url && connection.is_none() { + let token = rustgis_data::login_address::parse(url).ok().flatten().map(|b| b.binding); + if s.web_logins.active.values().any(|a| token == Some(a.binding.as_str())) { + logins::check(url).map_err(str::to_string)?; + return Ok(url.to_string()); + } + return Err(NOT_THIS_SESSION.into()); + } + let candidates: Vec<_> = + s.connections.connections.iter().filter(|c| is_service(c) && login_origin(c).as_deref() == Some(origin.as_str())).collect(); + let chosen = if let Some(id) = connection { + let c = s.connections.connections.iter().find(|c| c.id == id && is_service(c)).ok_or(NO_SUCH_CONNECTION)?; + // A service may link to another origin, but never grants it this login. + if login_origin(c).as_deref() != Some(origin.as_str()) { + return Ok(plain.to_string()); + } + Some(c.clone()) + } else if let Some(id) = s.database_logins.selection(WEB, &origin) { + candidates.iter().find(|c| c.id == id).map(|c| (*c).clone()) + } else if candidates.len() == 1 { + candidates.first().map(|c| (*c).clone()) + } else { + None + }; + let Some(c) = chosen else { + if candidates.len() > 1 { + s.database_logins.request(LoginRequest { + reason: LoginReason::Choose, + kind: WEB, + key: origin, + connections: candidates.iter().map(|c| c.id.clone()).collect(), + layers: Vec::new(), + tiles: 0, + opened: s.opened, + prompted: false, + }); + return Err(CHOOSE_CONNECTION.into()); + } + return Ok(plain.to_string()); + }; + if c.auth.is_empty() { + return Ok(plain.to_string()); + } + if !logged_in(s, &c) { + let secret = known_secret(s, &c)?; + activate(s, &c, Some(&secret), true)?; + } + let active = s.web_logins.active.get(&c.id).ok_or(REQUIRED)?; + logins::bind(&active.binding, plain).map_err(str::to_string) +} + +pub fn request_params<'a>(s: &mut Session, p: &'a Value) -> Result, String> { + let Some(url) = p.get("url").and_then(Value::as_str) else { return Ok(std::borrow::Cow::Borrowed(p)) }; + let id = match p.get("connection") { + None | Some(Value::Null) => None, + Some(Value::String(id)) if id.trim().is_empty() => None, + Some(Value::String(id)) => Some(id.as_str()), + Some(_) => return Err("connection is a saved web-service connection id.".into()), + }; + if id.is_none() && s.web_logins.anonymous { + // An address bound to a login is no authority for it here either. + let plain = logins::inner(url); + if plain == url { + return Ok(std::borrow::Cow::Borrowed(p)); + } + let mut owned = p.clone(); + if let Some(address) = owned.get_mut("url") { + *address = Value::String(plain.to_string()); + } + return Ok(std::borrow::Cow::Owned(owned)); + } + let bound = request(s, id, url)?; + // Carry the originating connection into internal add/download commands too. A linked + // foreign URL must not silently select a different origin's session login. + let implicit = if id.is_none() { + let origin = logins::origin(url); + let token = rustgis_data::login_address::parse(&bound).ok().flatten().map(|b| b.binding); + s.web_logins + .active + .iter() + .find(|(_, a)| token == Some(a.binding.as_str())) + .map(|(id, _)| id.as_str()) + .or_else(|| origin.as_deref().and_then(|o| s.database_logins.selection(WEB, o))) + .or_else(|| { + let mut matches = s.connections.connections.iter().filter(|c| is_service(c) && login_origin(c) == origin); + let first = matches.next()?; + matches.next().is_none().then_some(first.id.as_str()) + }) + } else { + None + }; + if bound == url && implicit.is_none() { + return Ok(std::borrow::Cow::Borrowed(p)); + } + let mut owned = p.clone(); + *owned.get_mut("url").ok_or("url is required")? = Value::String(bound); + if let Some(id) = implicit + && let Some(object) = owned.as_object_mut() + { + object.insert("connection".into(), Value::String(id.to_string())); + } + Ok(std::borrow::Cow::Owned(owned)) +} + +/// The web address a tile layer reads (a tile template's archive for a PMTiles or COG template). +fn tile_address(url: &str) -> &str { + crate::sources::template_archive(url).map_or(url, |(_, archive)| archive) +} + +/// The web address a layer reads again later (a streamed source or a raster read in place). +fn layer_address(l: &rustgis_map::Layer) -> Option<&str> { + l.source.as_ref().and_then(|s| s.path.as_deref()).or_else(|| l.raster.as_ref().and_then(|r| r.path.as_deref())) +} + +/// Rebinds only addresses belonging to this connection. Cross-origin links stay anonymous. +fn rebind_connection(s: &mut Session, c: &Connection) { + let binding = s.web_logins.active.get(&c.id).map(|a| a.binding.as_str()); + let rewrite = |address: &mut String| { + let (prefix, raw) = crate::sources::template_archive(address).unwrap_or(("", address.as_str())); + let plain = logins::inner(raw); + let next = binding.and_then(|b| logins::bind(b, plain).ok()).unwrap_or_else(|| plain.to_string()); + let next = if prefix.is_empty() { next } else { format!("{prefix}{next}") }; + if *address != next { + *address = next; + } + }; + for m in &mut s.project.maps { + for t in m.tiles.iter_mut().filter(|t| t.connection == c.id) { + rewrite(&mut t.url); + if let Some(url) = &mut t.time_url { + rewrite(url); + } + for (_, url) in &mut t.time_urls { + rewrite(url); + } + for legend in &mut t.legends { + rewrite(&mut legend.url); + } + if let Some(info) = &mut t.feature_info { + rewrite(&mut info.url); + } + } + for l in m.layers.iter_mut().filter(|l| l.connection == c.id) { + if let Some(path) = l.source.as_mut().and_then(|source| source.path.as_mut()) { + rewrite(path); + } + if let Some(path) = l.raster.as_mut().and_then(|raster| raster.path.as_mut()) { + rewrite(path); + } + if let Some(path) = &mut l.dataset.source { + rewrite(path); + } + } + for group in &mut m.service_groups { + let ids: BTreeSet<_> = group + .children + .iter() + .filter_map(|child| m.layers.iter().find(|l| l.id == child.layer).map(|l| l.connection.as_str())) + .collect(); + if ids.len() == 1 && ids.contains(c.id.as_str()) { + rewrite(&mut group.url); + for child in &mut group.children { + rewrite(&mut child.source_url); + } + } + } + } +} + +fn record_choice(s: &mut Session, origin: &str, old: &str, c: &Connection) { + for m in &mut s.project.maps { + for t in &mut m.tiles { + if t.connection == old && logins::origin(tile_address(&t.url)).as_deref() == Some(origin) { + t.connection = c.id.clone(); + } + } + let group_layers: BTreeSet<_> = m + .service_groups + .iter() + .filter(|g| logins::origin(&g.url).as_deref() == Some(origin)) + .flat_map(|g| g.children.iter().map(|child| child.layer)) + .collect(); + for l in &mut m.layers { + if l.connection == old && (layer_address(l).and_then(logins::origin).as_deref() == Some(origin) || group_layers.contains(&l.id)) + { + l.connection = c.id.clone(); + } + } + } +} + +/// What a project reads from one origin again later: its layers and tile layers, and the saved +/// connections they record. +#[derive(Default)] +struct Reads { + recorded: BTreeSet, + layers: Vec, + tiles: usize, + layer_keys: BTreeSet<(usize, u32)>, +} + +/// The origins the project reads from, with what reads each. +fn reads(s: &Session) -> BTreeMap<(String, String), Reads> { + let mut out = BTreeMap::<(String, String), Reads>::new(); + for (map_index, m) in s.project.maps.iter().enumerate() { + for t in &m.tiles { + let Some(origin) = logins::origin(tile_address(&t.url)) else { continue }; + let r = out.entry((origin, t.connection.clone())).or_default(); + r.tiles += 1; + if !t.connection.is_empty() { + r.recorded.insert(t.connection.clone()); + } + } + for l in &m.layers { + let Some(origin) = layer_address(l).and_then(logins::origin) else { continue }; + let r = out.entry((origin, l.connection.clone())).or_default(); + if r.layer_keys.insert((map_index, l.id)) { + r.layers.push(l.id); + } + if !l.connection.is_empty() { + r.recorded.insert(l.connection.clone()); + } + } + // An imported service's group is read again by a refresh, from its address: it logs in + // with the connection its layers record. + for g in &m.service_groups { + let Some(origin) = logins::origin(&g.url) else { continue }; + for child in &g.children { + let id = m.layers.iter().find(|l| l.id == child.layer).map(|l| l.connection.as_str()).unwrap_or(""); + let r = out.entry((origin.clone(), id.to_string())).or_default(); + if r.layer_keys.insert((map_index, child.layer)) { + r.layers.push(child.layer); + } + if !id.is_empty() { + r.recorded.insert(id.to_string()); + } + } + } + } + out +} + +/// Matches what the project reads from each origin (every origin, or those `only` names) to the +/// saved connections of that origin, and logs in with the one that is clear: the connection the +/// layers record, the one chosen this session, or the only one. Several, a missing login, or a +/// recorded connection this computer doesn't have are asked about ([`LoginRequest`]), never +/// guessed. +pub(crate) fn resolve(s: &mut Session, only: Option<&BTreeSet>) { + let mut pending = Vec::new(); + let mut touched = BTreeSet::new(); + for ((origin, recorded_id), r) in reads(s) { + if only.is_some_and(|only| !only.contains(&origin)) { + continue; + } + touched.insert(origin.clone()); + let candidates: Vec = s + .connections + .connections + .iter() + .filter(|c| is_service(c) && login_origin(c).as_deref() == Some(origin.as_str())) + .cloned() + .collect(); + // The recorded ids that are connections of this origin (looked up by id only: a name in a + // project is no one's choice). + let recorded: Vec<&Connection> = candidates.iter().filter(|c| r.recorded.contains(&c.id)).collect(); + let selected = s.database_logins.selection(WEB, &origin).and_then(|id| candidates.iter().find(|c| c.id == id)); + let chosen = match recorded.as_slice() { + [only] => Some(*only), + // A recorded connection this computer doesn't have is asked about: not replaced by + // the origin's only connection, nor by an earlier choice (see [`adopt`]). + [] if !r.recorded.is_empty() => None, + [] => selected.or_else(|| (candidates.len() == 1).then(|| candidates.first()).flatten()), + _ => None, + }; + let chosen = chosen.cloned(); + let opened = s.opened; + let request = |reason, connections: Vec| LoginRequest { + reason, + kind: WEB, + key: origin.clone(), + connections, + layers: r.layers.clone(), + tiles: r.tiles, + opened, + prompted: false, + }; + let Some(c) = chosen else { + if candidates.len() > 1 || recorded.len() > 1 { + // Several connections for the address, and none of them clear: the user picks. + let ids: Vec = if recorded.len() > 1 { + recorded.iter().map(|c| c.id.clone()).collect() + } else { + candidates.iter().map(|c| c.id.clone()).collect() + }; + pending.push(request(LoginReason::Choose, ids)); + } else if recorded.is_empty() && !r.recorded.is_empty() { + // Layers came from a connection this computer doesn't have, or whose address has + // since moved to another origin (a layer records only a connection of its origin). + pending.push(request(LoginReason::Connection, Vec::new())); + } + continue; + }; + if recorded_id != c.id { + record_choice(s, &origin, &recorded_id, &c); + } + if c.auth.is_empty() || logged_in(s, &c) { + rebind_connection(s, &c); + continue; + } + // A secret given this session was confirmed for this origin when it was typed. + match known_secret(s, &c).and_then(|secret| activate(s, &c, Some(&secret), true)) { + Ok(()) => {} + Err(e) => { + let already_asked = s + .database_logins + .requests(s.opened) + .any(|r| r.kind == WEB && r.key == origin && r.connections == [c.id.clone()] && r.reason == LoginReason::Password); + if e != REQUIRED && !already_asked { + s.notify(format!("{}: {e}", c.name), true); + } + pending.push(request(LoginReason::Password, vec![c.id.clone()])); + } + } + } + for origin in touched { + if !pending.iter().any(|request| request.key == origin) { + s.database_logins.clear_requests_for(WEB, &origin); + } + } + for request in pending { + ask(s, request); + } +} + +/// Whether logging in to `c` answers a request waiting at its origin: the user choosing a +/// connection for layers whose recorded one isn't here. Asked before the login, which ends it. +pub(crate) fn answers(s: &Session, c: &Connection) -> bool { + let Some(origin) = login_origin(c) else { return false }; + s.database_logins.requests(s.opened).any(|r| { + r.kind == WEB + && r.key == origin + && (r.reason == LoginReason::Connection || r.reason == LoginReason::Choose && r.connections.contains(&c.id)) + }) +} + +/// Gives `c` to the layers of its origin that record a connection this computer doesn't have for +/// that origin: the answer to a request ([`answers`]), never a guess. +pub(crate) fn adopt(s: &mut Session, c: &Connection) { + let Some(origin) = login_origin(c) else { return }; + let here: BTreeSet = s + .connections + .connections + .iter() + .filter(|k| is_service(k) && login_origin(k).as_deref() == Some(origin.as_str())) + .map(|k| k.id.clone()) + .collect(); + let gone: BTreeSet = + reads(s).into_keys().filter(|(o, id)| *o == origin && !id.is_empty() && !here.contains(id)).map(|(_, id)| id).collect(); + for old in gone { + record_choice(s, &origin, &old, c); + } +} + +/// Adds a request for the app to ask, saying it once (for the command line and scripts too). +fn ask(s: &mut Session, request: LoginRequest) { + let known = s + .database_logins + .requests(s.opened) + .any(|r| r.kind == request.kind && r.key == request.key && r.reason == request.reason && r.connections == request.connections); + if !known { + let n = request.layers.len() + request.tiles; + let names: Vec = request + .connections + .iter() + .filter_map(|id| s.connections.connections.iter().find(|c| &c.id == id)) + .map(|c| format!("'{}' ({})", c.name, c.id)) + .collect(); + let text = match request.reason { + LoginReason::Choose => format!( + "{n} layer(s) read {} and several saved connections match it: {}. Choose one with connection.login {{id}}.", + request.key, + names.join(", ") + ), + LoginReason::Password => { + format!( + "{n} layer(s) read {} and need the login of the connection {}: connection.login {{id, …}}.", + request.key, + names.join(", ") + ) + } + LoginReason::Connection => format!( + "{n} layer(s) read {} with a saved connection this computer doesn't have: add a connection for it (connection.new) and log in.", + request.key + ), + }; + s.notify(text, false); + } + s.database_logins.request(request); +} + +/// What the project held before an add command: its layers by id and its tile layers, so the +/// ones the command added can be told apart. +pub(crate) struct Before { + layers: Vec>, + tiles: Vec>, +} + +/// The connection an add command names (its `connection`), checked before it runs: a saved +/// web-service connection, by id. +fn named(s: &Session, params: &Value) -> Result, String> { + match params.get("connection") { + None | Some(Value::Null) => Ok(None), + Some(Value::String(id)) => { + let c = s + .connections + .connections + .iter() + .find(|c| &c.id == id) + .filter(|c| is_service(c)) + .ok_or("connection is the id of a saved web-service connection (connection.list)")?; + Ok(Some(c.clone())) + } + Some(_) => Err("connection is the id of a saved web-service connection (connection.list)".into()), + } +} + +/// What to compare after command `id` runs, if it adds layers; an error for a `connection` that +/// isn't a saved web-service connection's id. +pub(crate) fn before(s: &Session, id: &str, params: &Value) -> Result, String> { + if !ADD_COMMANDS.contains(&id) { + return Ok(None); + } + named(s, params)?; + Ok(Some(Before { + layers: s.project.maps.iter().map(|m| m.layers.iter().map(|l| l.id).collect()).collect(), + tiles: s.project.maps.iter().map(|m| m.tiles.iter().map(|t| (t.url.clone(), t.name.clone())).collect()).collect(), + })) +} + +/// Records on the layers an add command made the connection they came from: the one it names +/// (`connection`) for those read from its origin or holding no address, else the one whose login +/// their origin is read with. Then logs in for their origins as a project's layers do. +pub(crate) fn tag(s: &mut Session, params: &Value, before: Before, out: &Value) -> Result<(), String> { + let named = named(s, params)?; + let named_origin = named.as_ref().and_then(login_origin); + let mut origins = BTreeSet::new(); + // The connection a layer of `origin` (none: it holds no address) records. + let pick = |origin: Option<&String>| -> Option { + match (&named, origin) { + (Some(c), None) => Some(c.id.clone()), + (Some(c), Some(o)) if named_origin.as_ref() == Some(o) => Some(c.id.clone()), + (_, Some(o)) => { + if let Some(id) = s.database_logins.selection(WEB, o) { + return Some(id.to_string()); + } + let mut matches = s.connections.connections.iter().filter(|c| is_service(c) && login_origin(c).as_ref() == Some(o)); + let first = matches.next()?; + matches.next().is_none().then(|| first.id.clone()) + } + (None, None) => None, + } + }; + for (i, m) in s.project.maps.iter_mut().enumerate() { + let mut old = before.tiles.get(i).cloned().unwrap_or_default(); + for t in &mut m.tiles { + // Tile layers have no id: those not there before (each old one matched once). + if let Some(at) = old.iter().position(|(url, name)| *url == t.url && *name == t.name) { + old.swap_remove(at); + continue; + } + let origin = logins::origin(tile_address(&t.url)); + if let Some(id) = pick(origin.as_ref()) { + t.connection = id; + } + origins.extend(origin); + } + let old_layers = before.layers.get(i); + let mut added = BTreeSet::new(); + for l in m.layers.iter_mut().filter(|l| old_layers.is_none_or(|old| !old.contains(&l.id))) { + added.insert(l.id); + let origin = layer_address(l).or(l.dataset.source.as_deref()).and_then(logins::origin); + if let Some(id) = pick(origin.as_ref()).filter(|_| named.is_some() || l.connection.is_empty()) { + l.connection = id; + } + if layer_address(l).is_some() { + origins.extend(origin); + } + } + // An imported service's group is refreshed from its address later. + for g in m.service_groups.iter().filter(|g| g.children.iter().any(|c| added.contains(&c.layer))) { + origins.extend(logins::origin(&g.url)); + } + } + // A tile layer the command says it added (`index`, with its `kind`): one that replaced a layer + // of the same address and name looks unchanged to the comparison above. + let tile_kinds = ["raster", "vector", "style", "pmtiles", "mbtiles", "cog"]; + if let (Some(at), Some(kind)) = (out.get("index").and_then(Value::as_u64), out.get("kind").and_then(Value::as_str)) + && tile_kinds.contains(&kind) + && let Some(t) = usize::try_from(at).ok().and_then(|at| s.project.map_mut().tiles.get_mut(at)) + { + let origin = logins::origin(tile_address(&t.url)); + if let Some(id) = pick(origin.as_ref()) { + t.connection = id; + } + origins.extend(origin); + } + if !origins.is_empty() { + resolve(s, Some(&origins)); + } + Ok(()) +} diff --git a/crates/engine/src/sources.rs b/crates/engine/src/sources.rs index 998d21d5f9..a008153784 100644 --- a/crates/engine/src/sources.rs +++ b/crates/engine/src/sources.rs @@ -342,13 +342,15 @@ pub(crate) fn template_archive(address: &str) -> Option<(&str, &str)> { /// S3 presigned URL), and a PostgreSQL connection string's password. A MySQL, MariaDB or SQL Server /// address keeps its user (the one to connect as), never its password. An address read through a /// saved S3 connection is redacted inside (its inner address), and its length-prefixed form -/// rebuilt; a tile template, in its archive. +/// rebuilt; a tile template, in its archive. Web-login runtime bindings are never published. pub fn redact(address: &str) -> String { - if let Some((head, archive)) = template_archive(address) - && rustgis_data::s3_address::is_wrapper(archive) - { + if let Some((head, archive)) = template_archive(address) { return format!("{head}{}", redact(archive)); } + let inner = crate::render::rustgis_tiles::logins::inner(address); + if inner != address { + return redact(inner); + } if let Ok(Some(b)) = rustgis_data::s3_address::parse(address) && let Ok(rebuilt) = rustgis_data::s3_address::bind(b.binding, b.endpoint, &redact(b.url)) { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 38f58c6a6b..a9977064fa 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -12,6 +12,8 @@ mod remembered_database; #[cfg(not(target_arch = "wasm32"))] #[path = "tests/remembered_database_packets.rs"] mod remembered_database_packets; +#[path = "tests/service_logins.rs"] +mod service_logins; /// Keeps tests away from the user's installed plugins. fn isolate_plugins() { @@ -2235,6 +2237,9 @@ fn hostile_params_never_panic(shard: usize) { // A categorical summary: countIn's classes, and a mode by groups with a share out of range. json!({"stat": "countIn", "classes": [1e308, -1, "x", null, 3.5, {"a": 1}]}), json!({"stat": "countIn", "classes": "all", "by": "year"}), + // A web-service connection's login method and secret, and the connection an add records. + json!({"id": "c1", "kind": "wms", "auth": "query", "keyName": "\u{0}", "user": ":", "apiKey": "\n", "password": [], "allowInsecure": "yes", "connection": 7}), + json!({"id": "c1", "auth": "basic", "user": 7, "keyName": {"a": 1}, "apiKey": "k".repeat(9000), "remember": "yes", "connection": "c999999"}), json!({"stat": "mode", "by": "month", "minValid": 2, "months": []}), json!({"stat": "frequency", "by": "year", "minValid": -1, "name": []}), // MySQL addresses that can't be read, so nothing connects. @@ -12438,7 +12443,7 @@ fn a_trend_over_a_stac_collection() { } // Cancelled, the readers stop early and the run says so, rather than work out a block only // partly read. - let job = crate::cmd::cube::trend_start(&s, ¶ms).unwrap(); + let job = crate::cmd::cube::trend_start(&mut s, ¶ms).unwrap(); let e = job.run(&std::sync::atomic::AtomicBool::new(true), &std::sync::atomic::AtomicUsize::new(0)).unwrap_err(); assert_eq!(e, "cancelled"); // A row too wide for a piece is said. @@ -13655,7 +13660,7 @@ fn a_data_cube_summarised_over_time() { let again = t.map().layer_by_name("anos 2020").and_then(|l| l.raster.as_ref()).and_then(|r| r.data.clone()).unwrap(); assert_eq!(again.bands[0].data[0], 2.0); // Cancelled, it says so. - let job = crate::cmd::cube::reduce_start(&s, &json!({"layers": names, "stat": "mean"})).unwrap(); + let job = crate::cmd::cube::reduce_start(&mut s, &json!({"layers": names, "stat": "mean"})).unwrap(); let e = job.run(&std::sync::atomic::AtomicBool::new(true), &std::sync::atomic::AtomicUsize::new(0)).unwrap_err(); assert_eq!(e, "cancelled"); // Refused, nothing added. @@ -14757,7 +14762,9 @@ fn connections_commands() { assert!(s3::connection("conn-test-s3").is_none()); s.run("connection.new", &json!({"kind":"wms","name":"Closed","address":"http://127.0.0.1:9/wms"})).unwrap(); assert!(s.run("connection.test", &json!({"id":"Closed"})).unwrap_err().contains("127.0.0.1:9")); - assert!(s.run("connection.login", &json!({"id":"Closed"})).unwrap_err().contains("don't log in")); + // A service without a login method logs in to nothing: it is only chosen for its address. + assert_eq!(s.run("connection.login", &json!({"id":"Closed"})).unwrap()["loggedIn"], false); + assert!(s.run("connection.login", &json!({"id":"Closed","apiKey":"k"})).unwrap_err().contains("no login method")); s.run("connection.edit", &json!({"id":"Closed","kind":" wms "})).unwrap(); let default = s.run("connection.new", &json!({"kind":"wms","address":"https://a.example/wms"})).unwrap(); let renamed = s.run("connection.edit", &json!({"id":"B","name":""})).unwrap(); @@ -14961,7 +14968,7 @@ fn connection_browse_levels_and_adds() { assert_eq!(v["children"][0]["what"], "item"); assert_eq!( v["children"][0]["children"][0]["add"], - json!({"command": "stac.addAsset", "params": {"url": self_link, "asset": "visual", "name": "i1 visual"}}) + json!({"command": "stac.addAsset", "params": {"url": self_link, "asset": "visual", "name": "i1 visual", "connection": v["connection"]}}) ); assert!(v.get("truncated").is_none()); @@ -14991,7 +14998,7 @@ fn connection_browse_levels_and_adds() { let v = s.run("connection.browse", &json!({"id": "Static", "node": item_node, "answer": item.to_string()})).unwrap(); assert_eq!( v["children"][0]["add"], - json!({"command": "stac.addAsset", "params": {"url": "https://s.example/static/c1/i1/i1.json", "asset": "visual", "name": "i1 visual"}}) + json!({"command": "stac.addAsset", "params": {"url": "https://s.example/static/c1/i1/i1.json", "asset": "visual", "name": "i1 visual", "connection": v["connection"]}}) ); // An asset's key names its item, so the same file under two items is two rows. assert_eq!(v["children"][0]["key"], "https://s.example/static/c1/i1/i1.json#visual"); diff --git a/crates/engine/src/tests/service_logins.rs b/crates/engine/src/tests/service_logins.rs new file mode 100644 index 0000000000..597e343e37 --- /dev/null +++ b/crates/engine/src/tests/service_logins.rs @@ -0,0 +1,441 @@ +//! Service logins belong to a session/connection, never an origin-global request context. +use super::*; +use crate::{render::rustgis_tiles::logins, service_logins as web}; +use rustgis_data::secrets::{MemorySecrets, SecretError, SecretStore}; +use std::sync::Arc; + +fn session(store: Option>) -> Session { + let mut s = Session::new(Project::default()); + s.database_logins.set_store(store); + s +} + +fn new(s: &mut Session, p: Value) -> String { + s.run("connection.new", &p).unwrap()["id"].as_str().unwrap().to_string() +} + +fn bound(s: &mut Session, id: &str, url: &str) -> String { + web::request(s, Some(id), url).unwrap() +} + +fn header(address: &str) -> Option<(String, String)> { + logins::apply(address).unwrap().and_then(|a| a.header) +} + +fn project(origin: &str, id: &str) -> String { + let mut p = Project::default(); + let mut t = rustgis_map::TileLayer::new("Secured", rustgis_map::TileKind::Raster, format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png")); + t.connection = id.into(); + p.map_mut().tiles.push(t); + serde_json::to_string(&p).unwrap() +} + +fn requests(s: &mut Session) -> Value { + s.run("connection.list", &Value::Null).unwrap()["loginRequests"].clone() +} + +#[test] +fn web_login_goes_only_to_its_connections_origin() { + let mut s = session(None); + let id = new(&mut s, json!({"kind":"wms","address":"https://scope757.example.org/wms","auth":"header","keyName":"X-Api-Key"})); + s.run("connection.login", &json!({"id":id,"apiKey":"scope-sentinel-757"})).unwrap(); + // A raw URL is never authority to use a service login, even at its origin. + assert!(header("https://scope757.example.org/wms").is_none()); + let address = bound(&mut s, &id, "https://SCOPE757.example.org:443/tiles/1/2/3.png"); + assert_eq!(header(&address).unwrap(), ("X-Api-Key".into(), "scope-sentinel-757".into())); + for other in [ + "https://other757.example.org/wms", + "https://scope757.example.org.evil.example/wms", + "https://scope757.example.org:8443/wms", + "http://scope757.example.org/wms", + ] { + assert!(header(&bound(&mut s, &id, other)).is_none(), "{other}"); + assert!(header(&logins::inherit(&address, other)).is_none(), "{other}"); + } + s.run("map.addTiles", &json!({"url":"https://scope757.example.org/tiles/{z}/{x}/{y}.png","name":"Named","connection":id})).unwrap(); + // Replacement has the same (URL, name), but the newly created layer still needs provenance. + s.run("map.addTiles", &json!({"url":"https://scope757.example.org/tiles/{z}/{x}/{y}.png","name":"Named","connection":id})).unwrap(); + let tile = s.map().tiles.iter().find(|t| t.name == "Named").unwrap(); + assert_eq!(tile.connection, id); + assert_eq!(header(&tile.url).unwrap().1, "scope-sentinel-757"); + let previous = tile.url.clone(); + s.run("connection.logout", &json!({"id":id})).unwrap(); + assert!(logins::apply(&previous).is_err()); + assert_eq!(web::request(&mut s, Some(&id), "https://scope757.example.org/wms").unwrap_err(), web::REQUIRED); +} + +#[test] +fn web_login_methods_and_encoded_basic_size_are_checked() { + let mut s = session(None); + for bad in [ + json!({"kind":"wms","address":"https://m757.example.org","auth":"digest"}), + json!({"kind":"wms","address":"https://m757.example.org","auth":"query","keyName":"key"}), + json!({"kind":"wms","address":"https://m757.example.org","auth":"basic"}), + json!({"kind":"wms","address":"https://m757.example.org","auth":"basic","user":"a:b"}), + json!({"kind":"wms","address":"https://m757.example.org","auth":"header","keyName":"Cookie"}), + json!({"kind":"wms","address":"https://m757.example.org","auth":"header","keyName":"X Key"}), + json!({"kind":"s3","auth":"header","keyName":"X-Key"}), + ] { + assert!(s.run("connection.new", &bad).is_err(), "{bad}"); + } + let id = new(&mut s, json!({"kind":"arcgis","address":"https://m757.example.org/rest/services","auth":"basic","user":"ana"})); + s.run("connection.login", &json!({"id":id,"password":"pw-757"})).unwrap(); + let address = bound(&mut s, &id, "https://m757.example.org/rest/services"); + assert_eq!(header(&address).unwrap(), ("Authorization".into(), logins::basic("ana", "pw-757"))); + for bad in [ + json!({"id":id,"apiKey":"do-not-echo-757"}), + json!({"id":id,"password":"bad\nline"}), + json!({"id":id,"password":"x".repeat(3070)}), + json!({"id":id,"password":"pw-757","allowInsecure":"yes"}), + ] { + let error = s.run("connection.login", &bad).unwrap_err(); + assert!(!error.contains("do-not-echo-757") && !error.contains("bad\nline") && !error.contains("pw-757")); + assert_eq!(header(&address).unwrap().1, logins::basic("ana", "pw-757")); + } +} + +#[test] +fn plain_http_confirmation_applies_only_to_this_sessions_login_identity() { + let mut s = session(None); + let id = new(&mut s, json!({"kind":"wfs","address":"http://http757.example.org/wfs","auth":"basic","user":"ana"})); + assert_eq!(s.run("connection.login", &json!({"id":id,"password":"http-sentinel-757"})).unwrap_err(), web::INSECURE); + assert!(!s.database_logins.has_session_password(&id)); + s.run("connection.login", &json!({"id":id,"password":"http-sentinel-757","allowInsecure":true})).unwrap(); + assert!(header(&bound(&mut s, &id, "http://http757.example.org/features")).is_some()); + assert!(header(&bound(&mut s, &id, "https://http757.example.org/features")).is_none()); + // Reuse within this session needs no new confirmation; a later session has no credential. + s.run("connection.login", &json!({"id":id})).unwrap(); + let prefs = s.connections.to_json(); + let mut later = session(None); + later.connections = crate::connections::Connections::from_json(&prefs); + assert_eq!(later.run("connection.login", &json!({"id":id})).unwrap_err(), web::REQUIRED); + s.run("connection.logout", &json!({"id":id})).unwrap(); + assert_eq!(s.run("connection.login", &json!({"id":id,"password":"http-sentinel-757"})).unwrap_err(), web::INSECURE); + s.run("connection.login", &json!({"id":id,"password":"http-sentinel-757","allowInsecure":true})).unwrap(); + s.run("connection.edit", &json!({"id":id,"address":"http://http757.example.org:8080/wfs"})).unwrap(); + assert_eq!(s.run("connection.login", &json!({"id":id})).unwrap_err(), web::REQUIRED); +} + +#[test] +fn service_login_edit_and_remove_do_not_depend_on_a_credential_store() { + let store = Arc::new(MemorySecrets::failing(SecretError::Denied)); + let mut s = session(Some(store.clone())); + let id = new(&mut s, json!({"kind":"ogcapi","address":"https://memory757.example.org/ogc","auth":"header","keyName":"X-Key"})); + assert_eq!(s.run("connection.login", &json!({"id":id,"apiKey":"memory-sentinel-757","remember":true})).unwrap_err(), web::SESSION_ONLY); + assert!(!s.database_logins.has_session_password(&id)); + s.run("connection.login", &json!({"id":id,"apiKey":"memory-sentinel-757"})).unwrap(); + let address = bound(&mut s, &id, "https://memory757.example.org/features"); + assert_eq!(header(&address).unwrap().1, "memory-sentinel-757"); + let duplicate = s.run("connection.duplicate", &json!({"id":id,"name":"Another account"})).unwrap(); + assert_eq!(duplicate["loggedIn"], false); + assert_eq!(s.run("connection.test", &json!({"id":duplicate["id"],"probe":true})).unwrap_err(), web::REQUIRED); + s.run("connection.edit", &json!({"id":id,"name":"Renamed","address":"https://memory757.example.org/v2"})).unwrap(); + assert_eq!(header(&address).unwrap().1, "memory-sentinel-757"); + // A stale remembered marker cannot opt a service into credential-store access. + let mut prefs: Value = serde_json::from_str(&s.connections.to_json()).unwrap(); + prefs["connections"][0]["remembered"] = json!(true); + let mut later = session(Some(store)); + later.connections = crate::connections::Connections::from_json(&prefs.to_string()); + assert!(!later.connections.find(&id).unwrap().remembered); + assert_eq!(later.run("connection.login", &json!({"id":id})).unwrap_err(), web::REQUIRED); + s.run("connection.edit", &json!({"id":id,"address":"https://changed-memory757.example.org/ogc"})).unwrap(); + assert!(logins::apply(&address).is_err()); + s.run("connection.login", &json!({"id":id,"apiKey":"replacement-sentinel-757"})).unwrap(); + let replacement = bound(&mut s, &id, "https://changed-memory757.example.org/features"); + s.run("connection.remove", &json!({"id":id})).unwrap(); + assert!(logins::apply(&replacement).is_err()); +} + +#[test] +fn simultaneous_sessions_and_connections_never_share_or_replace_logins() { + let origin = "https://sessions757.example.org"; + let definition = json!({"kind":"wms","address":format!("{origin}/wms"),"auth":"header","keyName":"X-Key"}); + let mut a = session(None); + let mut b = session(None); + let id = new(&mut a, definition.clone()); + assert_eq!(new(&mut b, definition.clone()), id); + a.run("connection.login", &json!({"id":id,"apiKey":"session-a-757"})).unwrap(); + let a_address = bound(&mut a, &id, &format!("{origin}/wms")); + assert!(web::request(&mut b, Some(&id), &format!("{origin}/wms")).is_err()); + assert!(web::request(&mut b, None, &a_address).is_err()); + b.run("connection.login", &json!({"id":id,"apiKey":"session-b-757"})).unwrap(); + let b_address = bound(&mut b, &id, &format!("{origin}/wms")); + assert_ne!(a_address, b_address); + assert_eq!(header(&a_address).unwrap().1, "session-a-757"); + assert_eq!(header(&b_address).unwrap().1, "session-b-757"); + let second = + new(&mut a, json!({"name":"Other account", "kind":"wms","address":format!("{origin}/other"),"auth":"header","keyName":"X-Key"})); + a.run("connection.login", &json!({"id":second,"apiKey":"second-a-757"})).unwrap(); + assert_eq!(header(&a_address).unwrap().1, "session-a-757"); + assert_eq!(header(&bound(&mut a, &second, &format!("{origin}/wms"))).unwrap().1, "second-a-757"); + drop(b); + assert!(logins::apply(&b_address).is_err()); + assert_eq!(header(&a_address).unwrap().1, "session-a-757"); + drop(a); + assert!(logins::apply(&a_address).is_err()); +} + +#[test] +fn a_project_without_its_connection_or_login_asks_then_rebinds_without_persisting_authority() { + let origin = "https://open757.example.org"; + let mut s = session(None); + s.run("project.openText", &json!({"text":project(origin,"c7")})).unwrap(); + let r = requests(&mut s); + assert_eq!((r[0]["reason"].as_str(), r[0]["origin"].as_str(), r[0]["layers"].as_u64()), (Some("connection"), Some(origin), Some(1))); + let id = new(&mut s, json!({"kind":"wmts","address":format!("{origin}/wmts"),"auth":"header","keyName":"X-Key"})); + // A connection made meanwhile is nobody's choice for layers that recorded another one. + assert_eq!(requests(&mut s)[0]["reason"], "connection"); + assert_eq!(s.map().tiles[0].connection, "c7"); + s.run("connection.login", &json!({"id":id,"apiKey":"open-sentinel-757"})).unwrap(); + assert_eq!(requests(&mut s), json!([])); + assert_eq!(s.map().tiles[0].connection, id); + let runtime = s.map().tiles[0].url.clone(); + assert_eq!(header(&runtime).unwrap().1, "open-sentinel-757"); + let (saved, _) = s.project.saved_text(None).unwrap(); + assert!(saved.contains(&format!(r#""connection":"{id}""#)) || saved.contains(&format!(r#""connection": "{id}""#))); + assert!(!saved.contains("rustgis-login") && !saved.contains("open-sentinel-757")); + assert_eq!(crate::sources::redact(&runtime), format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png")); + s.notify("server echoed open-sentinel-757", true); + let error = + s.run("map.addTiles", &json!({"url":format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png"),"kind":"open-sentinel-757"})).unwrap_err(); + let visible = format!( + "{}{}{}{:?}{:?}", + s.run("connection.list", &Value::Null).unwrap(), + s.connections.to_json(), + saved, + s.notices, + s.database_logins + ); + assert!(!error.contains("open-sentinel-757") && !visible.contains("open-sentinel-757")); + assert!(crate::sources::holds_secret(&json!({"apiKey":"open-sentinel-757"}))); + // A persisted runtime token from another session is not adopted on load. + let forged = serde_json::to_string(&s.project).unwrap(); + let mut other = session(None); + other.run("project.openText", &json!({"text":forged})).unwrap(); + assert_eq!(requests(&mut other)[0]["reason"], "connection"); + assert!(header(&other.map().tiles[0].url).is_none()); + s.run("connection.edit", &json!({"id":id,"address":"https://moved-open757.example.org/wmts"})).unwrap(); + s.run("project.openText", &json!({"text":saved})).unwrap(); + assert_eq!(requests(&mut s)[0]["reason"], "connection"); +} + +#[test] +fn recorded_connections_at_one_origin_remain_independent_and_unassigned_layers_ask() { + let origin = "https://two757.example.org"; + let mut s = session(None); + let a = new(&mut s, json!({"kind":"wms","name":"A","address":format!("{origin}/wms"),"auth":"header","keyName":"X-Key"})); + let b = new(&mut s, json!({"kind":"wms","name":"B","address":format!("{origin}/wms?map=b")})); + s.run("project.openText", &json!({"text":project(origin, "")})).unwrap(); + assert_eq!(requests(&mut s)[0]["reason"], "choose"); + s.run("connection.login", &json!({"id":a,"apiKey":"a-sentinel-757"})).unwrap(); + assert_eq!(s.map().tiles[0].connection, a); + assert_eq!(header(&s.map().tiles[0].url).unwrap().1, "a-sentinel-757"); + s.run("connection.login", &json!({"id":b})).unwrap(); + // Choosing anonymous B for unassigned requests cannot revoke A's recorded layer. + assert_eq!(header(&s.map().tiles[0].url).unwrap().1, "a-sentinel-757"); + assert!(header(&web::request(&mut s, None, &format!("{origin}/wms")).unwrap()).is_none()); +} + +#[test] +fn test_refuses_changed_endpoint_and_missing_login_instead_of_testing_anonymously() { + let mut s = session(None); + let id = new(&mut s, json!({"kind":"wms","address":"https://test757.example.org/wms","auth":"header","keyName":"X-Key"})); + for changed in [ + json!({"address":"https://else757.example.org/wms"}), + json!({"auth":"basic"}), + json!({"user":"other"}), + json!({"keyName":"X-Other"}), + json!({"kind":"stac"}), + ] { + let mut p = changed; + p["id"] = json!(id); + p["probe"] = json!(true); + p["apiKey"] = json!("not-forwarded-757"); + assert_eq!(s.run("connection.test", &p).unwrap_err(), web::SAVE_BEFORE_TEST); + } + assert_eq!(s.run("connection.test", &json!({"id":id,"probe":true})).unwrap_err(), web::REQUIRED); + assert!(!s.database_logins.has_session_password(&id)); +} + +#[test] +fn junk_login_parameters_never_panic() { + let mut s = session(Some(Arc::new(MemorySecrets::new()))); + let id = new(&mut s, json!({"kind":"csw","address":"https://junk757.example.org/csw","auth":"header","keyName":"X-Key"})); + let junk = [ + json!(null), + json!([]), + json!({"id":id,"apiKey":7,"password":{"a":1},"remember":[],"allowInsecure":null}), + json!({"id":id,"auth":"\u{0}","user":"\u{feff}","keyName":"x".repeat(10_000)}), + json!({"id":id,"apiKey":"\u{7f}","allowInsecure":true}), + json!({"id":[id],"connection":{"id":id}}), + ]; + for cmd in + ["connection.new", "connection.edit", "connection.login", "connection.logout", "connection.test", "map.addTiles", "service.add"] + { + for p in &junk { + let _ = s.run(cmd, p); + } + } +} + +#[test] +fn a_group_child_read_in_place_is_counted_once_and_refresh_respects_logout() { + let origin = "https://group757.example.org"; + let mut s = session(Some(Arc::new(MemorySecrets::new()))); + let id = new(&mut s, json!({"kind":"ogcapi","address":format!("{origin}/ogc"),"auth":"basic","user":"ana"})); + let mut layer = rustgis_map::Layer::new(41, rustgis_data::Dataset::new("Roads", rustgis_data::GeometryType::None, Vec::new())); + layer.connection = id.clone(); + layer.source = Some(serde_json::from_value(json!({"path":format!("{origin}/ogc/collections/roads/items")})).unwrap()); + s.project.map_mut().layers.push(layer); + s.project.map_mut().service_groups.push(rustgis_map::ServiceGroup { + id: 5, + name: "Imported".into(), + url: format!("{origin}/ogc/collections"), + kind: "ogcapi".into(), + expanded: true, + children: vec![rustgis_map::ServiceChild { + layer: 41, + collection: "roads".into(), + source_url: format!("{origin}/ogc/collections/roads"), + }], + }); + web::resolve(&mut s, None); + assert_eq!(requests(&mut s)[0]["layers"], 1); + s.run("connection.login", &json!({"id":id,"password":"group-sentinel-757"})).unwrap(); + assert_eq!( + header(s.map().layers[0].source.as_ref().unwrap().path.as_deref().unwrap()).unwrap().1, + logins::basic("ana", "group-sentinel-757") + ); + assert!(header(&s.map().service_groups[0].url).is_some()); + s.run("connection.logout", &json!({"id":id})).unwrap(); + assert_eq!(s.run("service.refresh", &json!({"group":5,"text":"not a document"})).unwrap_err(), web::REQUIRED); + assert_eq!(s.run("connection.list", &Value::Null).unwrap()["connections"][0]["loggedIn"], false); +} + +#[test] +fn a_definition_reloaded_with_another_identity_cannot_reuse_a_session_secret() { + let mut s = session(None); + let id = new(&mut s, json!({"kind":"wms","address":"https://identity757.example.org/wms","auth":"header","keyName":"X-Key"})); + s.run("connection.login", &json!({"id":id,"apiKey":"identity-sentinel-757"})).unwrap(); + let mut prefs: Value = serde_json::from_str(&s.connections.to_json()).unwrap(); + prefs["connections"][0]["address"] = json!("https://changed757.example.org/wms"); + s.connections = crate::connections::Connections::from_json(&prefs.to_string()); + assert_eq!(web::request(&mut s, Some(&id), "https://changed757.example.org/wms").unwrap_err(), web::UNUSABLE); + assert!(header("https://changed757.example.org/wms").is_none()); + assert_eq!(s.run("connection.list", &Value::Null).unwrap()["connections"][0]["loggedIn"], false); +} + +#[test] +fn logins_drop_the_tile_cache_only_when_one_ends_or_is_replaced() { + let mut s = session(None); + let public = new(&mut s, json!({"kind":"wms","name":"Public","address":"https://cache-public757.example.org/wms"})); + let private = new( + &mut s, + json!({"kind":"wms","name":"Private","address":"https://cache-private757.example.org/wms","auth":"header","keyName":"X-Key"}), + ); + let start = web::cache_clears(); + // Opening a connection without a login method, and a first login, change no cached response. + s.run("connection.login", &json!({"id":public})).unwrap(); + s.run("connection.login", &json!({"id":public})).unwrap(); + s.run("connection.login", &json!({"id":private,"apiKey":"cache-sentinel-757"})).unwrap(); + assert_eq!(web::cache_clears(), start); + // Replacing or ending a login leaves authenticated responses behind: those go. + s.run("connection.login", &json!({"id":private,"apiKey":"replacement-sentinel-757"})).unwrap(); + assert_eq!(web::cache_clears(), start + 1); + s.run("connection.logout", &json!({"id":private})).unwrap(); + assert_eq!(web::cache_clears(), start + 2); + s.run("connection.logout", &json!({"id":private})).unwrap(); + assert_eq!(web::cache_clears(), start + 2, "there was no login to end"); +} + +#[test] +fn a_recorded_connection_that_is_gone_is_asked_about_not_replaced_by_the_only_other_one() { + let origin = "https://gone757.example.org"; + let mut s = session(None); + let other = new(&mut s, json!({"kind":"wms","name":"Other","address":format!("{origin}/wms"),"auth":"header","keyName":"X-Key"})); + s.run("connection.login", &json!({"id":other,"apiKey":"other-sentinel-757"})).unwrap(); + s.run("project.openText", &json!({"text":project(origin,"deleted")})).unwrap(); + assert_eq!(requests(&mut s)[0]["reason"], "connection"); + assert_eq!(s.map().tiles[0].connection, "deleted"); + assert!(header(&s.map().tiles[0].url).is_none(), "the other connection's login is not sent"); + // Choosing it is the user's to do. + s.run("connection.login", &json!({"id":other})).unwrap(); + assert_eq!(requests(&mut s), json!([])); + assert_eq!(s.map().tiles[0].connection, other); + assert_eq!(header(&s.map().tiles[0].url).unwrap().1, "other-sentinel-757"); + + // Layers that record no connection still use the origin's only one. + let mut fresh = session(None); + let sole = new(&mut fresh, json!({"kind":"wms","address":format!("{origin}/wms"),"auth":"header","keyName":"X-Key"})); + fresh.run("connection.login", &json!({"id":sole,"apiKey":"sole-sentinel-757"})).unwrap(); + fresh.run("project.openText", &json!({"text":project(origin,"")})).unwrap(); + assert_eq!(requests(&mut fresh), json!([])); + assert_eq!(fresh.map().tiles[0].connection, sole); + + // Nor does an unrecorded layer's use of it become the choice for a layer that recorded a gone one. + let mut mixed = session(None); + let sole = new(&mut mixed, json!({"kind":"wms","address":format!("{origin}/wms")})); + let mut p = Project::default(); + for (name, recorded) in [("Unrecorded", ""), ("Gone", "deleted")] { + let mut t = rustgis_map::TileLayer::new(name, rustgis_map::TileKind::Raster, format!("{origin}/{name}/{{z}}/{{x}}/{{y}}.png")); + t.connection = recorded.into(); + p.map_mut().tiles.push(t); + } + mixed.run("project.openText", &json!({"text":serde_json::to_string(&p).unwrap()})).unwrap(); + let recorded = |s: &Session, name: &str| s.map().tiles.iter().find(|t| t.name == name).unwrap().connection.clone(); + assert_eq!(recorded(&mixed, "Unrecorded"), sole); + assert_eq!(recorded(&mixed, "Gone"), "deleted"); + assert_eq!(requests(&mut mixed)[0]["reason"], "connection"); +} + +#[test] +fn an_unsaved_anonymous_test_never_carries_a_saved_login() { + let mut s = session(None); + let address = "https://probe757.example.org/wms"; + let id = new(&mut s, json!({"kind":"wms","address":address,"auth":"header","keyName":"X-Key"})); + s.run("connection.login", &json!({"id":id,"apiKey":"probe-sentinel-757"})).unwrap(); + let unsaved = s.run("connection.test", &json!({"kind":"wms","address":address,"probe":true})).unwrap(); + assert!(header(unsaved["fetch"].as_str().unwrap()).is_none(), "{unsaved}"); + let saved = s.run("connection.test", &json!({"id":id,"probe":true})).unwrap(); + assert_eq!(header(saved["fetch"].as_str().unwrap()).unwrap().1, "probe-sentinel-757"); + // The anonymous read ended with the test. + assert!(header(&web::request(&mut s, None, address).unwrap()).is_some()); +} + +#[test] +fn plugins_cannot_send_a_saved_web_service_login() { + super::isolate_plugins(); + let mut s = session(None); + let origin = "https://plugin757.example.org"; + let id = new(&mut s, json!({"kind":"wms","address":format!("{origin}/wms"),"auth":"header","keyName":"X-Key"})); + s.run("connection.login", &json!({"id":id,"apiKey":"plugin-sentinel-757"})).unwrap(); + super::plugin("eng-login-757", &["edit", "network"]); + let tiles = format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png"); + // Naming the connection is refused, as logging in to it is. + for command in ["map.addTiles", "layer.addUrl", "service.layers"] { + let e = super::forward(&mut s, "eng-login-757", command, json!({"url":tiles,"connection":id})).unwrap_err(); + assert!(e.contains("web-service connection"), "{command}: {e}"); + } + assert!(s.map().tiles.is_empty()); + // Not naming it, the only one at the origin isn't used for it either; the user's own command is. + super::forward(&mut s, "eng-login-757", "map.addTiles", json!({"url":tiles})).unwrap(); + assert!(header(&s.map().tiles[0].url).is_none()); + assert!(s.map().tiles[0].connection.is_empty()); + s.run("map.addTiles", &json!({"url":tiles,"name":"Mine"})).unwrap(); + let mine = s.map().tiles.iter().find(|t| t.name == "Mine").unwrap(); + assert_eq!(header(&mine.url).unwrap().1, "plugin-sentinel-757"); + rustgis_plugins::unload("eng-login-757"); +} + +#[test] +fn only_web_services_compare_a_test_with_their_saved_definition() { + let mut s = session(None); + let db = new(&mut s, json!({"kind":"postgres","address":"postgres:host=db757.example.org dbname=gis user=ana"})); + let out = s + .run( + "connection.test", + &json!({"id":db,"probe":true,"kind":"mysql","address":"postgres:host=elsewhere dbname=gis user=ana","auth":null,"user":7}), + ) + .unwrap(); + assert_eq!(out["connection"], db); +} From f8896d31707323c7387428e038e0ea651e60c167 Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Mon, 2 Nov 2026 12:11:17 +0100 Subject: [PATCH 03/19] feat(ui): login method, fields and http confirmation in the connection dialog --- crates/ui-egui/src/analysispane.rs | 2 +- crates/ui-egui/src/connectiontree.rs | 204 ++++++++++-- crates/ui-egui/src/connectionui.rs | 351 ++++++++++++++++++++- crates/ui-egui/src/cswui.rs | 13 + crates/ui-egui/src/dialogs.rs | 23 +- crates/ui-egui/src/lib.rs | 63 +++- crates/ui-egui/src/panes.rs | 16 + crates/ui-egui/src/serviceui.rs | 98 ++++-- crates/ui-egui/src/sourcespane.rs | 20 +- crates/ui-egui/src/stacui.rs | 105 ++++-- crates/ui-egui/tests/service_logins/mod.rs | 241 ++++++++++++++ crates/ui-egui/tests/smoke.rs | 1 + 12 files changed, 1025 insertions(+), 112 deletions(-) create mode 100644 crates/ui-egui/tests/service_logins/mod.rs diff --git a/crates/ui-egui/src/analysispane.rs b/crates/ui-egui/src/analysispane.rs index 4fb3b2ef89..d2ae282350 100644 --- a/crates/ui-egui/src/analysispane.rs +++ b/crates/ui-egui/src/analysispane.rs @@ -1130,7 +1130,7 @@ fn trend(app: &mut RustGisApp, ui: &mut Ui, rasters: &[String]) { #[cfg(target_arch = "wasm32")] run(app, "cube.trend", p); #[cfg(not(target_arch = "wasm32"))] - match rustgis_engine::cmd::cube::trend_start(&app.session, &p) { + match rustgis_engine::cmd::cube::trend_start(&mut app.session, &p) { Ok(job) => { let id = app.session.download_started(); let r = Trending { diff --git a/crates/ui-egui/src/connectiontree.rs b/crates/ui-egui/src/connectiontree.rs index 85bc152236..08e5acef8c 100644 --- a/crates/ui-egui/src/connectiontree.rs +++ b/crates/ui-egui/src/connectiontree.rs @@ -1,7 +1,8 @@ //! The datasets a saved connection offers, listed under it in Data Sources › Connections: one //! level at a time as it is expanded (`connection.browse` names what each level reads, the app //! fetches it off the frame and asks again with the answer), with the selection that -//! Add Selected (in a dataset's menu) adds. Each dataset added is its own undo step. +//! Add Selected (in a dataset's menu) adds. Each dataset added is its own undo step and retains +//! its saved web-service connection id, separately from its display name in Recent. use std::collections::HashMap; use std::sync::Arc; @@ -40,6 +41,7 @@ struct Signing { href: String, name: String, origin: Option, + connection: Option, slot: Slot, String>>, } @@ -234,9 +236,12 @@ pub fn poll(app: &mut RustGisApp, ctx: &egui::Context) { match r.and_then(|b| stac::parse_sas(&b)) { Ok(token) => { stac::remember_token(&signing.token_url, &token); - let p = json!({"url": stac::sign(&signing.href, &token), "name": signing.name, "zoom": true}); + let mut p = json!({"url": stac::sign(&signing.href, &token), "name": signing.name, "zoom": true}); + if let Some(connection) = signing.connection { + p["connection"] = json!(connection); + } if let Err(e) = run_add_url(app, p, signing.origin.as_deref()) { - app.session.notify(e, true); + app.session.notify(crate::connectionui::translated(&e), true); } app.map.invalidate(); } @@ -250,21 +255,38 @@ pub fn poll(app: &mut RustGisApp, ctx: &egui::Context) { } /// Adds one dataset a listing offers. Each branch is one command, so each is one undo step. -/// `origin` is the connection it is for: Recent then shows the dataset's source (as added, its -/// address and options unchanged) under that name, once it is added. -fn add_from(app: &mut RustGisApp, child: &Value, source: Option<&ServiceSource>, origin: Option<&str>) -> Result<(), String> { +/// `origin` names the connection in Recent; `connection` is its saved web-service id, kept +/// through asynchronous discovery, signing and downloads so the resulting layer records it. +fn add_from( + app: &mut RustGisApp, + child: &Value, + source: Option<&ServiceSource>, + origin: Option<&str>, + connection: Option<&str>, +) -> Result<(), String> { let add = child.get("add").filter(|a| a.is_object()).ok_or_else(|| tl!("Nothing to add here").to_string())?; let command = add["command"].as_str().unwrap_or(""); - let params = add["params"].clone(); + let mut params = add["params"].clone(); + if let Some(connection) = connection { + if params.is_null() { + params = json!({}); + } + // Params that aren't an object are the command's to refuse. + if let Some(object) = params.as_object_mut() { + object.insert("connection".into(), json!(connection)); + } + } match (source, child.get("layer").filter(|l| l.is_object())) { (Some(source), Some(layer)) => { - serviceui::add_layer_from(app, source, layer, "", None, false, origin)?; + serviceui::add_layer_from(app, source, layer, "", None, false, origin, connection)?; } _ => match command { "layer.addUrl" => { run_add_url(app, params, origin)?; } - "stac.addAsset" => add_asset(app, child["href"].as_str().unwrap_or(""), params["name"].as_str().unwrap_or(""), origin)?, + "stac.addAsset" => { + add_asset(app, child["href"].as_str().unwrap_or(""), params["name"].as_str().unwrap_or(""), origin, connection)?; + } _ => { run_named(app, command, params, origin)?; } @@ -276,18 +298,24 @@ fn add_from(app: &mut RustGisApp, child: &Value, source: Option<&ServiceSource>, /// A STAC asset by its address, signed first when it needs a token (one in hand is used; one to /// fetch is fetched off the frame and the asset added when it lands). -fn add_asset(app: &mut RustGisApp, href: &str, name: &str, origin: Option<&str>) -> Result<(), String> { +fn add_asset(app: &mut RustGisApp, href: &str, name: &str, origin: Option<&str>, connection: Option<&str>) -> Result<(), String> { + let mut p = json!({"url": href, "name": name, "zoom": true}); + if let Some(connection) = connection { + p["connection"] = json!(connection); + } if !stac::needs_signing(href) { - run_add_url(app, json!({"url": href, "name": name, "zoom": true}), origin)?; + run_add_url(app, p, origin)?; return Ok(()); } let token_url = stac::sas_token_url(href)?; if let Some(token) = stac::cached_token(&token_url) { - run_add_url(app, json!({"url": stac::sign(href, &token), "name": name, "zoom": true}), origin)?; + p["url"] = json!(stac::sign(href, &token)); + run_add_url(app, p, origin)?; } else { let slot = crate::fetch_into(&token_url); let (href, name, origin) = (href.to_string(), name.to_string(), origin.map(str::to_string)); - app.ui.connection_tree.tokens.push(Signing { token_url, href, name, origin, slot }); + let connection = connection.map(str::to_string); + app.ui.connection_tree.tokens.push(Signing { token_url, href, name, origin, connection, slot }); } Ok(()) } @@ -320,7 +348,7 @@ fn add_with_options(app: &mut RustGisApp, c: &Connection, child: &Value, source: } _ => match (source, child.get("layer").filter(|l| l.is_object())) { (Some(source), Some(layer)) => { - app.run("ui.addService", json!({"url": source.url, "type": source.kind}))?; + app.run("ui.addService", json!({"url": source.url, "type": source.kind, "connection": c.id}))?; app.dialogs.connection = Some(c.id.clone()); app.ui.service_browse.filter = layer["name"].as_str().unwrap_or("").to_string(); Ok(()) @@ -404,9 +432,11 @@ fn add_picks(app: &mut RustGisApp) { prune(app); for pick in std::mem::take(&mut app.ui.connection_tree.picks) { // The name as it is now, for the whole add (pruned above: the connection is there). - let origin = app.session.connections.find(&pick.conn).map(|c| c.name.clone()); - if let Err(e) = add_from(app, &pick.child, pick.source.as_deref(), origin.as_deref()) { - app.session.notify(e, true); + let connection = app.session.connections.find(&pick.conn); + let origin = connection.map(|c| c.name.clone()); + let service = connection.filter(|c| rustgis_engine::service_logins::is_service(c)).map(|c| c.id.clone()); + if let Err(e) = add_from(app, &pick.child, pick.source.as_deref(), origin.as_deref(), service.as_deref()) { + app.session.notify(crate::connectionui::translated(&e), true); } } } @@ -448,7 +478,8 @@ fn level(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, c: &Connection, node: &s app.ui.connection_tree.entries.remove(&key); } let keys = c.kind == "s3" && c.options.get("credentials").is_some_and(|v| v == "keys"); - if (DATABASE_KINDS.contains(&c.kind.as_str()) || keys) && ui.small_button(tl!("Log In…")).clicked() { + let service_login = rustgis_engine::service_logins::is_service(c) && !c.auth.is_empty(); + if (DATABASE_KINDS.contains(&c.kind.as_str()) || keys || service_login) && ui.small_button(tl!("Log In…")).clicked() { crate::connectionui::log_in(app, &c.id); } }); @@ -590,7 +621,8 @@ fn dataset(app: &mut RustGisApp, ui: &mut Ui, c: &Connection, listing: &Listing, let result = if with_options { add_with_options(app, c, child, listing.source.as_deref()) } else if add_now { - add_from(app, child, listing.source.as_deref(), Some(c.name.as_str())) + let connection = rustgis_engine::service_logins::is_service(c).then_some(c.id.as_str()); + add_from(app, child, listing.source.as_deref(), Some(c.name.as_str()), connection) } else { Ok(()) }; @@ -650,7 +682,8 @@ mod tests { const WMS: &str = r#"Localimage/pngEPSG:3857aAbB"#; fn wms(url: &str) -> ServiceSource { - ServiceSource { url: url.into(), kind: "wms", text: WMS.into(), documents: Vec::new() } + let endpoint = format!("{}?", url.split('?').next().unwrap_or(url)); + ServiceSource { url: url.into(), kind: "wms", text: WMS.replace("http://127.0.0.1:9/wms?", &endpoint), documents: Vec::new() } } /// A listed WMS layer, as the tree adds it. @@ -672,7 +705,7 @@ mod tests { let app = h.state_mut(); let url = "http://127.0.0.1:9/wms"; let layers = app.session.map().tiles.len(); - add_from(app, &layer("a"), Some(&wms(url)), Some("Local")).unwrap(); + add_from(app, &layer("a"), Some(&wms(url)), Some("Local"), None).unwrap(); assert_eq!(app.session.map().tiles.len(), layers + 1); assert_eq!(app.session.sources.recent.first(), Some(&expected(url, "a", "Local"))); assert_eq!(app.session.sources.recent[0].extra, json!({"layer": "a", "type": "wms"})); @@ -687,19 +720,20 @@ mod tests { app.session.run("connection.edit", &json!({"id": id, "name": "Renamed"})).unwrap(); assert_eq!(app.session.sources.recent[0].name, "Local"); let name = app.session.connections.find(&id).unwrap().name.clone(); - add_from(app, &layer("b"), Some(&wms(url)), Some(&name)).unwrap(); + add_from(app, &layer("b"), Some(&wms(url)), Some(&name), Some(&id)).unwrap(); // One entry per kind and address: the re-add takes the current name and layer. + assert_eq!(app.session.map().tiles.last().unwrap().connection, id); assert_eq!(app.session.sources.recent.iter().filter(|s| s.address == url).count(), 1); assert_eq!(app.session.sources.recent.first(), Some(&expected(url, "b", "Renamed"))); // Another connection to the same address shares the entry. - add_from(app, &layer("a"), Some(&wms(url)), Some("Twin")).unwrap(); + add_from(app, &layer("a"), Some(&wms(url)), Some("Twin"), None).unwrap(); assert_eq!(app.session.sources.recent.iter().filter(|s| s.address == url).count(), 1); assert_eq!(app.session.sources.recent.first(), Some(&expected(url, "a", "Twin"))); // An address with a secret is redacted exactly as a plain add redacts it. let secret = "http://127.0.0.1:9/wms?token=SECRET&access_token=SECRET"; - add_from(app, &layer("a"), Some(&wms(secret)), Some("Local")).unwrap(); + add_from(app, &layer("a"), Some(&wms(secret)), Some("Local"), None).unwrap(); let noted = app.session.sources.recent.first().unwrap(); assert!(!noted.address.contains("SECRET"), "{}", noted.address); assert_eq!(noted.name, "Local"); @@ -710,9 +744,9 @@ mod tests { let mut h = harness(); let app = h.state_mut(); let before = app.session.sources.recent.clone(); - assert!(add_from(app, &layer("nope"), Some(&wms("http://127.0.0.1:9/wms")), Some("Local")).is_err()); + assert!(add_from(app, &layer("nope"), Some(&wms("http://127.0.0.1:9/wms")), Some("Local"), None).is_err()); let missing = json!({"add": {"command": "layer.addData", "params": {"path": "/no/such/file.geojson"}}}); - assert!(add_from(app, &missing, None, Some("Local")).is_err()); + assert!(add_from(app, &missing, None, Some("Local"), None).is_err()); assert_eq!(app.session.sources.recent, before); } @@ -739,6 +773,11 @@ mod tests { assert!(app.ui.connection_tree.picks.is_empty()); assert_eq!(app.session.sources.recent.first(), Some(&expected(two, "a", "Two"))); assert_eq!(app.session.sources.recent.get(1), Some(&expected(one, "a", "One"))); + let added = &app.session.map().tiles; + for (id, url) in ids.iter().zip([one, two]) { + let tile = added.iter().find(|tile| tile.url.starts_with(url)).unwrap(); + assert_eq!(&tile.connection, id); + } } /// A download lands after the add returned: its entry is named for the connection it came @@ -757,14 +796,14 @@ mod tests { let (good, bad) = (format!("{base}/points.geojson"), format!("{base}/missing.geojson")); let url_add = |address: &str| json!({"add": {"command": "layer.addUrl", "params": {"url": address, "name": "Points"}}}); let app = h.state_mut(); - let id = app.session.run("connection.new", &json!({"kind": "wms", "address": "http://127.0.0.1:9/wms", "name": "Local"})).unwrap() - ["id"] - .as_str() - .unwrap() - .to_string(); + let id = + app.session.run("connection.new", &json!({"kind": "wms", "address": format!("{base}/wms"), "name": "Local"})).unwrap()["id"] + .as_str() + .unwrap() + .to_string(); let before = app.session.sources.recent.clone(); - add_from(app, &url_add(&good), None, Some("Local")).unwrap(); - add_from(app, &url_add(&bad), None, Some("Local")).unwrap(); + add_from(app, &url_add(&good), None, Some("Local"), Some(&id)).unwrap(); + add_from(app, &url_add(&bad), None, Some("Local"), Some(&id)).unwrap(); assert_eq!(app.session.sources.recent, before, "nothing is recorded before the data lands"); // The connection is renamed and the selection changes before it lands. app.session.run("connection.edit", &json!({"id": id, "name": "Renamed"})).unwrap(); @@ -776,6 +815,100 @@ mod tests { let landed = recent.iter().find(|s| s.address == good).unwrap(); assert_eq!((landed.kind.as_str(), landed.name.as_str()), ("url", "Local")); assert!(recent.iter().all(|s| s.address != bad), "a failed download isn't recorded"); + assert_eq!(h.state().session.map().layer_by_name("Points").unwrap().connection, id); + } + + #[test] + fn a_service_dialog_download_keeps_the_browsed_connection_not_the_current_selection() { + let base = serve(|_| (200, br#"{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[1,2]}}"#.to_vec())); + let mut h = harness(); + let app = h.state_mut(); + let url = format!("{base}/wms"); + let one = app.run("connection.new", json!({"kind":"wms", "name":"One", "address":url})).unwrap(); + let two = app.run("connection.new", json!({"kind":"wms", "name":"Two", "address":url})).unwrap(); + app.run("ui.addService", json!({"url":url, "type":"wms", "connection":one["id"]})).unwrap(); + app.dialogs.connection = two["id"].as_str().map(str::to_string); + let src = wms(&url); + let feature = json!({"name":"points", "offer":{"type":"features", "url":format!("{base}/points.geojson")}}); + serviceui::add_layer(app, &src, &feature, "", None, true).unwrap(); + assert_eq!(app.ui.downloads.last().unwrap().2["connection"], one["id"]); + until(&mut h, |app| app.ui.downloads.is_empty()); + assert_eq!(h.state().session.map().layer_by_name("points").unwrap().connection, one["id"].as_str().unwrap()); + } + + #[test] + fn a_service_dialog_does_not_forward_a_different_origins_connection() { + let mut app = RustGisApp::with_sample(); + let saved = app.run("connection.new", json!({"kind":"wms", "address":"https://other-service.invalid/wms"})).unwrap(); + let src = wms("http://127.0.0.1:9/wms"); + let feature = json!({"name":"points", "offer":{"type":"features", "url":"http://127.0.0.1:9/points.geojson"}}); + serviceui::add_layer_from(&mut app, &src, &feature, "", None, true, None, saved["id"].as_str()).unwrap(); + assert!(app.ui.downloads.last().unwrap().2.get("connection").is_none()); + } + + #[test] + fn url_downloads_use_the_explicit_basic_or_header_connection_and_record_it() { + for basic in [false, true] { + let base = serve(move |head| { + let head = head.to_ascii_lowercase(); + let authorized = if basic { + head.contains("authorization: basic cmvhzgvyonbhc3n3b3jk") + } else { + head.contains("x-api-key: ui-download-key") + }; + if authorized { + (200, br#"{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[1,2]}}"#.to_vec()) + } else { + (401, b"Login required".to_vec()) + } + }); + let mut h = harness(); + let app = h.state_mut(); + let c = app + .run( + "connection.new", + json!({ + "kind":"service", "address":base, "auth":if basic { "basic" } else { "header" }, + "user":if basic { "reader" } else { "" }, "keyName":if basic { "" } else { "X-Api-Key" } + }), + ) + .unwrap(); + let mut login = json!({"id":c["id"], "allowInsecure":true}); + login[if basic { "password" } else { "apiKey" }] = json!(if basic { "password" } else { "ui-download-key" }); + app.run("connection.login", login).unwrap(); + app.run("ui.addUrl", json!({"url":format!("{base}/points.geojson"), "name":"Private points", "connection":c["id"]})).unwrap(); + until(&mut h, |app| app.ui.downloads.is_empty()); + let layer = h.state().session.map().layer_by_name("Private points").unwrap(); + assert_eq!(layer.connection, c["id"].as_str().unwrap()); + } + } + + #[test] + fn a_listing_whose_params_are_not_an_object_cannot_panic_the_add() { + let mut h = harness(); + let app = h.state_mut(); + for params in [json!("text"), json!(7), json!([1]), Value::Null] { + let child = json!({"add": {"command": "map.zoomIn", "params": params}}); + let _ = add_from(app, &child, None, Some("Local"), Some("c1")); + } + } + + #[test] + fn a_foreign_followup_does_not_adopt_that_origins_saved_login() { + let mut app = RustGisApp::with_sample(); + let foreign = "https://foreign-ui-followup.invalid/points.geojson"; + let c = app + .run( + "connection.new", + json!({ + "kind":"service", "address":foreign, "auth":"header", "keyName":"X-Api-Key" + }), + ) + .unwrap(); + app.run("connection.login", json!({"id":c["id"], "apiKey":"foreign-followup-key"})).unwrap(); + let address = serviceui::request_from(&mut app, None, "https://original-ui-followup.invalid/service", foreign).unwrap(); + assert_eq!(address, foreign); + assert!(rustgis_engine::render::rustgis_tiles::logins::apply(&address).unwrap().is_none()); } #[test] @@ -843,13 +976,15 @@ mod tests { }); let url = format!("{base}/wcs"); let mut h = harness(); + let connection = h.state_mut().run("connection.new", json!({"kind":"wcs", "address":url, "name":"Soils"})).unwrap(); + let id = connection["id"].as_str().unwrap().to_string(); let app = h.state_mut(); app.run("map.zoomToExtent", json!({"extent": [5.0, 49.2, 5.8, 50.0]})).unwrap(); let listed = app.session.run("service.layers", &json!({"url": url, "text": caps})).unwrap(); let source = ServiceSource { url: url.clone(), kind: "wcs", text: caps.into(), documents: Vec::new() }; let child = json!({"add": {"command": "service.add", "params": {}}, "layer": listed["layers"][0].clone()}); let before = app.session.sources.recent.clone(); - add_from(app, &child, Some(&source), Some("Soils")).unwrap(); + add_from(app, &child, Some(&source), Some("Soils"), Some(&id)).unwrap(); assert_eq!(app.session.sources.recent, before, "nothing is recorded while the description is read"); until(&mut h, |app| { app.ui.service_steps.is_empty() @@ -860,6 +995,7 @@ mod tests { let noted = h.state().session.sources.recent.first().unwrap(); assert_eq!(noted.kind, "url"); assert!(noted.address.starts_with(&url) && noted.address.contains("GetCoverage"), "{}", noted.address); + assert_eq!(h.state().session.map().layer_by_name("Soil carbon").unwrap().connection, id); } /// The whole path on screen: open a connection in the Data Sources pane and double-click a diff --git a/crates/ui-egui/src/connectionui.rs b/crates/ui-egui/src/connectionui.rs index 9c59c02d48..4293746e7f 100644 --- a/crates/ui-egui/src/connectionui.rs +++ b/crates/ui-egui/src/connectionui.rs @@ -1,11 +1,14 @@ -//! Named endpoint definitions, optional remembered database passwords and S3 keys, off-frame +//! Named endpoint definitions, optional remembered passwords, web logins and S3 keys, off-frame //! web probes, and explicit saved-connection choices for project sources. use egui::vec2; -use rustgis_engine::connections::{Connection, DATABASE_KINDS, LoginReason, LoginRequest, database_key, database_label}; +use rustgis_engine::connections::{ + Connection, DATABASE_KINDS, LoginReason, LoginRequest, SERVICE_KINDS as WEB_KINDS, WEB, database_key, database_label, login_origin, +}; use rustgis_engine::render::rustgis_tiles::{cache, s3}; use rustgis_engine::s3_connections::STALE; use rustgis_engine::s3_keys as keys; +use rustgis_engine::service_logins as web; use rustgis_engine::sources::Source; use serde_json::{Value, json}; @@ -23,6 +26,11 @@ pub struct State { pub credentials: String, pub profile: String, pub password: String, + pub auth: String, + pub user: String, + pub key_name: String, + pub api_key: String, + pub allow_insecure: bool, pub remember: bool, /// The current project's login prompt, never a password. pub request: Option, @@ -31,8 +39,8 @@ pub struct State { pub secret_key: String, pub session_token: String, pub test: Option<(Value, Slot, String>>)>, - /// The form was opened to log in (Log In…): Save logs in even with no keys typed (an S3 - /// connection's saved keys are then read), where a plain edit only changes the definition. + /// The form was opened to log in (Log In…): Save logs in even with no secret typed, + /// using the connection's existing session login (or saved DB/S3 credentials). pub login: bool, /// A login that worked with a warning (translated, e.g. the keys weren't remembered): the /// dialog stays open to say so until it is closed or edited again. @@ -69,6 +77,17 @@ pub(crate) fn translated(e: &str) -> String { s3::UNRESOLVED => tl!("Choose a saved S3 connection before opening this source."), s3::MISMATCH => tl!("The saved S3 connection no longer matches this source."), STALE => tl!("This S3 connection choice is no longer available."), + web::UNUSABLE => tl!("The session login could not be used. Enter it again."), + web::SESSION_ONLY => tl!("Web-service logins last for this session only."), + web::INSECURE => tl!("This connection uses unencrypted http://. Tick the confirmation before sending its password or key."), + web::BAD_SECRET => tl!("The password or key must fit a 4096-byte HTTP header, without control characters."), + web::REQUIRED => tl!("Enter this connection's password or API key to log in."), + web::UNSAVED => tl!("Save the connection first: a login belongs to a saved connection."), + web::NO_ORIGIN => tl!("The connection's address has no http:// or https:// origin a login can go to."), + web::SAVE_BEFORE_TEST => tl!("Save changes before testing this connection."), + web::NOT_THIS_SESSION => tl!("This request's login does not belong to this session."), + web::CHOOSE_CONNECTION => tl!("Several saved connections match this address. Choose a connection explicitly."), + web::NO_SUCH_CONNECTION => tl!("No saved web-service connection with this id."), other => other, } .to_string() @@ -223,6 +242,9 @@ pub fn open_edit(app: &mut RustGisApp, id: &str) -> Result<(), String> { buckets: c.options.get("buckets").cloned().unwrap_or_default(), credentials: c.options.get("credentials").cloned().unwrap_or_else(|| "none".into()), profile: c.options.get("profile").cloned().unwrap_or_default(), + auth: c.auth, + user: c.user, + key_name: c.key_name, id: Some(c.id), kind: c.kind, name: c.name, @@ -244,6 +266,8 @@ pub fn log_in(app: &mut RustGisApp, id: &str) -> bool { && app.ui.connection.id.as_deref().and_then(|id| app.session.connections.find(id)).is_some_and(|c| c.saved_keys); app.dialogs.info = Some(if DATABASE_KINDS.contains(&app.ui.connection.kind.as_str()) { tl!("Type the password, then Save. Remember on this computer is optional.").to_string() + } else if WEB_KINDS.contains(&app.ui.connection.kind.as_str()) { + tl!("Type the password or key, or leave it blank to use this connection's session login, then Save.").to_string() } else if saved { tl!("Leave the keys blank to log in with the keys saved on this computer, or type others, then Save.").to_string() } else { @@ -273,6 +297,11 @@ fn definition(st: &State) -> Value { p["options"]["profile"] = json!(st.profile); } } + if WEB_KINDS.contains(&st.kind.as_str()) { + p["auth"] = json!(if st.auth.is_empty() { "none" } else { &st.auth }); + p["user"] = json!(st.user); + p["keyName"] = json!(st.key_name); + } p } @@ -287,6 +316,35 @@ fn secrets(st: &State, p: &mut Value) { } } } + if WEB_KINDS.contains(&st.kind.as_str()) { + let (key, value) = if st.auth == "basic" { ("password", &st.password) } else { ("apiKey", &st.api_key) }; + if !st.auth.is_empty() && !value.is_empty() { + p[key] = json!(value); + p["allowInsecure"] = json!(st.allow_insecure); + } + } +} + +/// A saved service Test uses its saved login-relevant definition, never an edited endpoint +/// with an old id. A new anonymous endpoint can still be tested without saving it. +fn test_blocked(app: &RustGisApp) -> Option<&'static str> { + let st = &app.ui.connection; + let saved = st.id.as_deref().and_then(|id| app.session.connections.find(id)); + if !WEB_KINDS.contains(&st.kind.as_str()) && !saved.is_some_and(web::is_service) { + return None; + } + let auth = if st.auth == "none" { "" } else { &st.auth }; + if st.id.is_none() && auth.is_empty() { + return None; + } + let matches = saved.is_some_and(|c| { + c.kind == st.kind + && c.address == rustgis_engine::sources::redact(st.address.trim()) + && c.auth == auth + && c.user == st.user.trim() + && c.key_name == st.key_name.trim() + }); + (!matches).then_some(tl!("Save changes before testing this connection.")) } pub fn start_test(app: &mut RustGisApp) { @@ -294,6 +352,18 @@ pub fn start_test(app: &mut RustGisApp) { app.dialogs.info = None; app.ui.connection.test = None; let mut p = definition(&app.ui.connection); + let service = WEB_KINDS.contains(&app.ui.connection.kind.as_str()); + if let Some(reason) = test_blocked(app) { + app.dialogs.error = Some(reason.into()); + return; + } + if service && typed_secrets(&app.ui.connection) && insecure(&app.ui.connection) && !app.ui.connection.allow_insecure { + app.dialogs.error = Some(translated(web::INSECURE)); + return; + } + if service && let Some(id) = &app.ui.connection.id { + p["id"] = json!(id); + } secrets(&app.ui.connection, &mut p); if DATABASE_KINDS.contains(&app.ui.connection.kind.as_str()) { match app.run("connection.test", p) { @@ -303,10 +373,14 @@ pub fn start_test(app: &mut RustGisApp) { return; } p["probe"] = json!(true); - let v = match app.run("connection.test", p) { + let result = app.run("connection.test", p); + if service { + clear_secrets(&mut app.ui.connection); + } + let v = match result { Ok(v) => v, Err(e) => { - app.dialogs.error = Some(e); + app.dialogs.error = Some(translated(&e)); return; } }; @@ -314,7 +388,10 @@ pub fn start_test(app: &mut RustGisApp) { app.dialogs.error = Some(tl!("Nothing to test").into()); return; }; - let q = definition(&app.ui.connection); + let mut q = definition(&app.ui.connection); + if service && let Some(id) = &app.ui.connection.id { + q["id"] = json!(id); + } let slot: Slot, String>> = Slot::default(); let land = slot.clone(); cache::download(url, Box::new(move |r| *land.lock().unwrap_or_else(|e| e.into_inner()) = Some(r))); @@ -332,6 +409,9 @@ fn candidates(app: &RustGisApp, request: &LoginRequest) -> Vec { .iter() .filter_map(|id| { let c = app.session.connections.find(id)?; + if request.kind == WEB { + return (web::is_service(c) && login_origin(c).as_deref() == Some(&request.key)).then(|| c.clone()); + } let (kind, key) = database_key(&c.address)?; (kind == request.kind && key == request.key).then(|| c.clone()) }) @@ -350,6 +430,13 @@ fn password_prompt(app: &mut RustGisApp, id: &str, request: Option match open_edit(app, id) { Ok(()) => { app.ui.connection.request = request; + if WEB_KINDS.contains(&app.ui.connection.kind.as_str()) { + app.ui.connection.login = true; + app.dialogs.info = + Some(tl!("Enter the password or key for this connection. Web-service logins last for this session only.").into()); + app.dialogs.error = error.map(|e| translated(&e)); + return; + } app.dialogs.info = Some(tl!("The saved password could not be used. Type a password to try again; check Remember on this computer to replace the saved password.").into()); app.dialogs.error = error; } @@ -367,12 +454,36 @@ pub(crate) fn choose(app: &mut RustGisApp, request: LoginRequest) { pub(crate) fn chooser(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { let Some(request) = app.ui.connection.request.clone() else { return true }; + if request.kind == WEB && request.reason == LoginReason::Connection { + ui.label(i18n::fmt("This project uses a connection for {origin} that is not saved on this computer.", &[("origin", &request.key)])); + return ui + .horizontal(|ui| { + if ui.button(tl!("New Connection…")).clicked() { + match open_new(app, "service") { + Ok(()) => { + app.ui.connection.address = request.key.clone(); + // Saving it logs in, so the layers waiting for a connection take it. + app.ui.connection.request = Some(request.clone()); + } + Err(e) => app.dialogs.error = Some(e), + } + return false; + } + ui.button(tl!("Cancel")).clicked() + }) + .inner; + } let list = candidates(app, &request); if list.is_empty() { return true; } - ui.label(tl!("Choose which saved connection to use for this database. This choice lasts for this session.")); - ui.label(database_label(request.kind, &request.key)); + if request.kind == WEB { + ui.label(tl!("Choose which saved connection to use for this address. This choice lasts for this session.")); + ui.label(&request.key); + } else { + ui.label(tl!("Choose which saved connection to use for this database. This choice lasts for this session.")); + ui.label(database_label(request.kind, &request.key)); + } egui::ScrollArea::vertical().max_height(300.0).show(ui, |ui| { for c in &list { ui.group(|ui| { @@ -406,7 +517,7 @@ pub(crate) fn chooser(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { fn poll_requests(app: &mut RustGisApp) { if let Some(request) = &app.ui.connection.request && matches!(app.dialogs.open, Some("chooseConnection" | "editConnection")) - && (request.opened != app.session.opened || candidates(app, request).is_empty()) + && (request.opened != app.session.opened || request.reason != LoginReason::Connection && candidates(app, request).is_empty()) { app.dialogs.open = None; app.ui.connection = State::default(); @@ -427,6 +538,11 @@ fn poll_requests(app: &mut RustGisApp) { // it. In particular, cancelling it must not immediately open it again. continue; } + if request.kind == WEB && request.reason == LoginReason::Connection { + app.ui.connection = State { request: Some(request), ..Default::default() }; + app.dialogs = crate::dialogs::Dialogs { open: Some("chooseConnection"), ..Default::default() }; + break; + } let list = candidates(app, &request); if list.is_empty() { continue; @@ -498,9 +614,19 @@ fn summary(kind: &str, r: &Value) -> String { } } +fn insecure(st: &State) -> bool { + !st.auth.is_empty() && st.address.trim().get(..7).is_some_and(|s| s.eq_ignore_ascii_case("http://")) +} + /// Whether a password or any key is typed in the form. fn typed_secrets(st: &State) -> bool { (DATABASE_KINDS.contains(&st.kind.as_str()) && !st.password.is_empty()) + || (WEB_KINDS.contains(&st.kind.as_str()) + && match st.auth.as_str() { + "basic" => !st.password.is_empty(), + "header" => !st.api_key.is_empty(), + _ => false, + }) || (st.kind == "s3" && st.credentials == "keys" && [&st.access_key, &st.secret_key, &st.session_token].iter().any(|v| !v.is_empty())) @@ -508,19 +634,36 @@ fn typed_secrets(st: &State) -> bool { fn clear_secrets(st: &mut State) { st.password.clear(); + st.api_key.clear(); + st.allow_insecure = false; st.access_key.clear(); st.secret_key.clear(); st.session_token.clear(); st.remember = false; } -/// Saves the definition, then logs in when keys or a password are typed, or when the form was -/// opened to log in (an S3 connection with keys then reads those saved on this computer). Only -/// the login is asked to remember keys. A login that worked with a warning keeps the dialog open -/// to say so. +/// Saves the definition, then logs in when a secret is typed, or the form was opened to log in +/// (using this connection's session login or saved DB/S3 credentials). Only DB/S3 logins remember +/// secrets. A login that worked with a warning keeps the dialog open to say so. fn save(app: &mut RustGisApp) -> Result<(), String> { let store = app.session.database_logins.has_store(); let st = &app.ui.connection; + let service = WEB_KINDS.contains(&st.kind.as_str()); + if service && typed_secrets(st) { + let (connection, _) = rustgis_engine::connections::clean(Connection { + kind: st.kind.clone(), + address: st.address.clone(), + auth: st.auth.clone(), + user: st.user.clone(), + key_name: st.key_name.clone(), + ..Default::default() + })?; + let secret = if st.auth == "basic" { &st.password } else { &st.api_key }; + web::check_login_secret(&connection, secret).map_err(|e| translated(&e))?; + } + if service && typed_secrets(st) && insecure(st) && !st.allow_insecure { + return Err(translated(web::INSECURE)); + } let keys_mode = st.kind == "s3" && st.credentials == "keys"; let remember = keys_mode && st.remember && remember_blocked(store, &st.session_token).is_none(); if remember && (st.access_key.trim().is_empty() || st.secret_key.trim().is_empty()) { @@ -529,7 +672,8 @@ fn save(app: &mut RustGisApp) -> Result<(), String> { if DATABASE_KINDS.contains(&st.kind.as_str()) && st.remember && st.password.is_empty() { return Err(tl!("Type the password to remember it on this computer.").into()); } - let wants_login = typed_secrets(st) || (st.login && keys_mode && st.id.is_some()); + let adopts = service && st.request.as_ref().is_some_and(|r| r.kind == WEB && r.reason == LoginReason::Connection); + let wants_login = typed_secrets(st) || (st.login && (keys_mode || service) && st.id.is_some()) || adopts; let mut p = definition(st); let command = if let Some(id) = &st.id { p["id"] = json!(id); @@ -567,6 +711,11 @@ fn save(app: &mut RustGisApp) -> Result<(), String> { login["remember"] = json!(true); } let result = app.run("connection.login", login); + if service { + // A web login is not kept in the form once sent, whether it worked or not. + clear_secrets(&mut app.ui.connection); + crate::connectiontree::refresh(app, Some(&id)); + } if database { crate::connectiontree::refresh(app, Some(&id)); let refused = password_request(app, &id); @@ -612,18 +761,22 @@ pub fn dialog(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { "A named endpoint you can reuse. Keys are kept until RustGIS closes unless you choose to remember them on this computer; the connection itself never keeps them." ) } else { - tl!("A named endpoint you can reuse. Keys and tokens are kept until RustGIS closes, never saved.") + tl!("Web-service logins last for this session only.") }; ui.label(egui::RichText::new(intro).weak()); let store = app.session.database_logins.has_store(); // Status metadata only: rendering must never read the credential store. let saved_keys = app.ui.connection.id.as_deref().and_then(|id| app.session.connections.find(id)).is_some_and(|c| c.kind == "s3" && c.saved_keys); + let current = app.ui.connection.id.as_deref().and_then(|id| app.session.connections.find(id)); + let logged_in = current.is_some_and(|c| web::logged_in(&app.session, c)); let st = &mut app.ui.connection; let database = DATABASE_KINDS.contains(&st.kind.as_str()); + let service = WEB_KINDS.contains(&st.kind.as_str()); let mut changed = false; let mut source_changed = false; let mut forget = false; + let mut logout = false; egui::Grid::new("connection").num_columns(2).spacing(vec2(10.0, 8.0)).show(ui, |ui| { ui.label(tl!("Type")); ui.label(tl!(label(&st.kind))); @@ -737,6 +890,63 @@ pub fn dialog(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { .labelled_by(input_label.id) .changed(); ui.end_row(); + if service { + ui.label(egui::RichText::new(tl!("Login")).strong()); + ui.label(""); + ui.end_row(); + let input_label = ui.label(tl!("Method")); + let methods = [("", tl!("None")), ("basic", tl!("Basic authentication")), ("header", tl!("API key in a header"))]; + let shown = methods.iter().find(|(code, _)| *code == st.auth).map_or(tl!("None"), |(_, name)| *name); + let mut method_changed = false; + egui::ComboBox::from_id_salt("connection_auth") + .selected_text(shown) + .width(360.0) + .show_ui(ui, |ui| { + for (code, name) in methods { + method_changed |= ui.selectable_value(&mut st.auth, code.to_string(), name).changed(); + } + }) + .response + .labelled_by(input_label.id); + ui.end_row(); + if method_changed { + clear_secrets(st); + changed = true; + } + if st.auth == "basic" { + let input_label = ui.label(tl!("User")); + changed |= ui.add(egui::TextEdit::singleline(&mut st.user).desired_width(360.0)).labelled_by(input_label.id).changed(); + ui.end_row(); + } else if st.auth == "header" { + let input_label = ui.label(tl!("Header name")); + changed |= + ui.add(egui::TextEdit::singleline(&mut st.key_name).desired_width(360.0)).labelled_by(input_label.id).changed(); + ui.end_row(); + } + if !st.auth.is_empty() { + let basic = st.auth == "basic"; + let input_label = ui.label(if basic { tl!("Password") } else { tl!("Key") }); + let field = if basic { &mut st.password } else { &mut st.api_key }; + changed |= + ui.add(egui::TextEdit::singleline(field).password(true).desired_width(360.0)).labelled_by(input_label.id).changed(); + ui.end_row(); + if insecure(st) { + ui.label(""); + changed |= ui.checkbox(&mut st.allow_insecure, tl!("Send the login over unencrypted http://")).changed(); + ui.end_row(); + } else { + st.allow_insecure = false; + } + if logged_in { + ui.label(tl!("Status")); + ui.vertical(|ui| { + ui.label(tl!("Logged in")); + logout = ui.button(tl!("Log Out")).clicked(); + }); + ui.end_row(); + } + } + } } }); // A credential-source switch never carries opt-in to the newly chosen source. @@ -764,6 +974,12 @@ pub fn dialog(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { } } } + if logout && let Some(id) = app.ui.connection.id.clone() { + match app.run("connection.logout", json!({"id":id})) { + Ok(_) => app.dialogs.info = Some(tl!("Logged out. Enter the password or key to log in again.").into()), + Err(e) => app.dialogs.error = Some(translated(&e)), + } + } // A login that worked but couldn't do all it was asked: said until closed (or edited again). if let Some(warning) = app.ui.connection.warned.clone() { ui.add_space(4.0); @@ -779,13 +995,18 @@ pub fn dialog(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { let st = &app.ui.connection; let testing = st.test.is_some(); let unavailable = database && cfg!(target_arch = "wasm32"); + let blocked = test_blocked(app); let clicked = ui .horizontal(|ui| { let response = ui - .add_enabled_ui(!testing && !unavailable, |ui| crate::widgets::dialog_button(ui, tl!("Test"), None, vec2(84.0, 28.0))) + .add_enabled_ui(!testing && !unavailable && blocked.is_none(), |ui| { + crate::widgets::dialog_button(ui, tl!("Test"), None, vec2(84.0, 28.0)) + }) .inner; let response = if unavailable { response.on_disabled_hover_text(tl!("Database connections are used by the desktop app")) + } else if let Some(reason) = blocked { + response.on_disabled_hover_text(reason) } else { response }; @@ -804,6 +1025,14 @@ pub fn dialog(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { } pub fn open_connection(app: &mut RustGisApp, c: &Connection) -> Result<(), String> { + if web::is_service(c) + && !c.auth.is_empty() + && !web::logged_in(&app.session, c) + && let Err(e) = app.run("connection.login", json!({"id":c.id})) + { + password_prompt(app, &c.id, password_request(app, &c.id), Some(e)); + return Ok(()); + } let (kind, extra) = match c.kind.as_str() { "postgres" | "mysql" | "sqlserver" => { if cfg!(target_arch = "wasm32") { @@ -867,6 +1096,9 @@ pub fn open_connection(app: &mut RustGisApp, c: &Connection) -> Result<(), Strin } _ => return Err(format!("unknown kind '{}'", c.kind)), }; + if kind == "service" { + return app.run("ui.addService", json!({"url":c.address, "type":extra["type"], "connection":c.id})).map(|_| ()); + } let r = crate::sourcespane::open(app, &Source { kind: kind.into(), name: c.name.clone(), address: c.address.clone(), extra }); if r.is_ok() { if kind == "stac" { @@ -1034,6 +1266,91 @@ fn open_chosen(app: &mut RustGisApp) -> bool { mod tests { use super::*; + #[test] + fn edited_service_test_is_blocked_without_sending_or_saving_the_key() { + let mut app = RustGisApp::with_sample(); + let c = app + .run( + "connection.new", + json!({ + "kind":"wms", "address":"https://test-form.example/wms", "auth":"header", "keyName":"X-Api-Key" + }), + ) + .unwrap(); + let id = c["id"].as_str().unwrap(); + let saved = app.session.connections.find(id).unwrap().clone(); + for field in ["address", "auth", "user", "keyName", "kind", "non-web kind"] { + open_edit(&mut app, id).unwrap(); + let st = &mut app.ui.connection; + st.api_key = "never-sent-form-key".into(); + match field { + "address" => st.address = "https://other-test-form.example/wms".into(), + "auth" => st.auth = "basic".into(), + "user" => st.user = "unsaved-user".into(), + "keyName" => st.key_name = "X-Other-Key".into(), + "kind" => st.kind = "wmts".into(), + "non-web kind" => st.kind = "postgres".into(), + _ => {} + } + assert!(test_blocked(&app).is_some(), "{field}"); + start_test(&mut app); + assert_eq!(app.dialogs.error.as_deref(), Some(web::SAVE_BEFORE_TEST), "{field}"); + assert!(app.ui.connection.test.is_none()); + assert_eq!(app.ui.connection.api_key, "never-sent-form-key"); + assert!(!app.session.database_logins.has_session_password(id)); + assert!(!web::logged_in(&app.session, &saved)); + assert_eq!(app.session.connections.find(id), Some(&saved)); + } + open_edit(&mut app, id).unwrap(); + app.ui.connection.name = "An unsaved display name".into(); + assert!(test_blocked(&app).is_none(), "display-only edits don't change the login"); + } + + #[test] + fn invalid_service_replacement_preserves_session_login_and_definition() { + let mut app = RustGisApp::with_sample(); + let c = app + .run( + "connection.new", + json!({ + "kind":"wms", "address":"https://replacement-form.example/wms", "auth":"basic", "user":"reader" + }), + ) + .unwrap(); + let id = c["id"].as_str().unwrap(); + app.run("connection.login", json!({"id":id, "password":"original-password"})).unwrap(); + let saved = app.session.connections.find(id).unwrap().clone(); + for password in ["bad\npassword".to_string(), "x".repeat(4097), "x".repeat(3070)] { + open_edit(&mut app, id).unwrap(); + app.ui.connection.address = "https://unsaved-replacement-form.example/wms".into(); + app.ui.connection.password = password; + assert!(save(&mut app).is_err()); + assert_eq!(app.session.connections.find(id), Some(&saved)); + assert_eq!(web::known_secret(&app.session, &saved).unwrap().expose(), "original-password"); + } + } + + #[test] + fn opening_a_connection_without_a_login_method_logs_nothing_in() { + let mut app = RustGisApp::with_sample(); + let made = app.run("connection.new", json!({"kind":"wms", "address":"http://127.0.0.1:9/wms"})).unwrap(); + let c = app.session.connections.find(made["id"].as_str().unwrap()).unwrap().clone(); + let origin = login_origin(&c).unwrap(); + let _ = open_connection(&mut app, &c); + // A login would choose this connection for its origin (and drop the tile cache). + assert_eq!(app.session.database_logins.selection(WEB, &origin), None); + } + + #[test] + fn a_refused_s3_login_keeps_the_typed_keys_in_the_form() { + let mut app = RustGisApp::with_sample(); + open_new(&mut app, "s3").unwrap(); + app.ui.connection.name = "Refused keys".into(); + app.ui.connection.access_key = "AKIAEXAMPLE".into(); + assert!(save(&mut app).is_err(), "a key ID without its secret is refused"); + assert_eq!(app.ui.connection.access_key, "AKIAEXAMPLE"); + } + #[test] fn picked_requires_identity_and_the_unchanged_public_address() { let list = vec![ diff --git a/crates/ui-egui/src/cswui.rs b/crates/ui-egui/src/cswui.rs index bd2f548a56..6c0c1c1e07 100644 --- a/crates/ui-egui/src/cswui.rs +++ b/crates/ui-egui/src/cswui.rs @@ -54,6 +54,19 @@ pub fn search(app: &mut RustGisApp, start: u64) { } else { st.asked.clone() }; + if start <= 1 { + let url = p.get("url").and_then(Value::as_str).unwrap_or(""); + if let Some(id) = crate::serviceui::connection_for(app, app.dialogs.connection.as_deref(), url) { + p["connection"] = json!(id); + } + } + match rustgis_engine::service_logins::request_params(&mut app.session, &p) { + Ok(bound) => p = bound.into_owned(), + Err(e) => { + app.ui.csw.error = Some(crate::connectionui::translated(&e)); + return; + } + } let asked = p.clone(); p["start"] = json!(start); match rustgis_engine::cmd::service::records_request(&app.session, &p) { diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 097e763a86..faef54921b 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -780,7 +780,8 @@ fn add_csv(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { /// becomes `way` once connected, and stays `before` when connecting fails. fn go_to(app: &mut RustGisApp, url: &str, kind: &str, way: Vec<(String, &'static str)>, before: Vec<(String, &'static str)>) { app.dialogs.info = None; - match app.run("ui.addService", json!({"url": url, "type": kind})) { + let connection = app.ui.service.as_ref().and_then(|source| source.connection.clone()); + match app.run("ui.addService", json!({"url": url, "type": kind, "connection": connection})) { Ok(_) => app.ui.service_browse.back = way, Err(e) => { app.ui.service_browse.back = before; @@ -1081,14 +1082,24 @@ fn add_service(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { if keep_root && let Some((url, kind)) = keepable { // "auto" is the dialog's detection mode; saved connections call that kind "service". match crate::connectionui::keep(app, json!({"kind": if kind == "auto" { "service" } else { kind }, "address": url})) { - Ok(id) => app.dialogs.connection = Some(id), + Ok(id) => { + app.dialogs.connection = Some(id.clone()); + if let Some(source) = &mut app.ui.service { + source.connection = Some(id); + } + } Err(e) => app.dialogs.error = Some(e), } } - if let Some(p) = import - && let Err(e) = app.run("ui.importService", p) - { - app.dialogs.error = Some(e); + if let Some(mut p) = import { + let connection = app.ui.service.as_ref().and_then(|source| source.connection.clone()); + let url = p.get("url").and_then(Value::as_str).unwrap_or(""); + if let Some(id) = crate::serviceui::connection_for(app, connection.as_deref(), url) { + p["connection"] = json!(id); + } + if let Err(e) = app.run("ui.importService", p) { + app.dialogs.error = Some(e); + } } // Into a directory's folder or service, keeping the way back; or back out of it. let here = app.ui.service.as_ref().map(|c| (c.url.clone(), c.kind)); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index d1801985a9..de32b8b383 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -349,6 +349,8 @@ fn open_with_system(_: &std::path::Path) -> Result<(), String> { /// background, then its layers are listed. pub struct ServiceConn { pub url: String, + /// The saved connection captured when browsing began, never the current dialog selection. + pub connection: Option, /// `wms`, `wmts`, `wfs`, `ogcapi`, `arcgis`, or `auto`. pub kind: &'static str, pub slot: Slot, Failure>>, @@ -2443,6 +2445,8 @@ impl RustGisApp { } // Data by its web address: the dialog, then (with an address) the download, off the // frame; it's added when it lands. A PMTiles archive is a tile layer, never downloaded. + // Optional `connection`: the saved web-service id forwarded to layer.addUrl or + // layer.addDownload, including probes, whole-file fallbacks and paged features. "ui.addUrl" => { if p.get("geolibreZarr").and_then(Value::as_bool) == Some(true) { if let Err(e) = geolibreui::start(self, p) { @@ -2456,9 +2460,14 @@ impl RustGisApp { let Some(url) = p.get("url").and_then(Value::as_str).map(str::trim).filter(|u| !u.is_empty()) else { return self.run("ui.dialog", json!({"name": "addUrl"})); }; + let url = rustgis_engine::data::login_address::inner(url); + let bound = + rustgis_engine::service_logins::request_params(&mut self.session, p).map_err(|e| connectionui::translated(&e))?; + let p = bound.as_ref(); if url.to_ascii_lowercase().split(['?', '#']).next().is_some_and(|u| u.ends_with(".pmtiles")) { return connectiontree::run_named(self, "layer.addUrl", p.clone(), origin.as_deref()); } + let address = p.get("url").and_then(Value::as_str).unwrap_or(url).to_string(); // A GeoTIFF, FlatGeobuf or GeoParquet: its header (or footer) first, off the // frame; one that can be read in place is, another is downloaded (see // `poll_downloads`). An address without a file name (an API answering with a @@ -2478,7 +2487,7 @@ impl RustGisApp { let land = slot.clone(); if remote { rustgis_engine::data::source::open_url( - url, + &address, Box::new(move |r| { let r = match r { Ok(_) => Ok(true), @@ -2490,7 +2499,7 @@ impl RustGisApp { ); } else { rustgis_engine::render::rustgis_tiles::cog::probe( - url, + &address, Box::new(move |r| { let r = if unnamed { Ok(r.unwrap_or(false)) } else { r }; *land.lock().unwrap_or_else(|e| e.into_inner()) = Some(r); @@ -2501,6 +2510,7 @@ impl RustGisApp { let shown = rustgis_engine::data::s3_address::inner(url); self.session.notify(i18n::fmt("Reading {url}…", &[("url", shown)]), false); let mut probe = p.clone(); + probe["url"] = json!(address); if let Some(o) = probe.as_object_mut() { o.remove(connectiontree::ORIGIN); if let Some(origin) = &origin { @@ -2510,7 +2520,7 @@ impl RustGisApp { self.ui.cog_probes.push((probe, slot)); return Ok(Value::Null); } - let url = url.to_string(); + let url = address; let slot: Slot, String>> = Slot::default(); let land = slot.clone(); rustgis_engine::render::rustgis_tiles::cache::download_limited( @@ -2523,7 +2533,7 @@ impl RustGisApp { Box::new(move |r| *land.lock().unwrap_or_else(|e| e.into_inner()) = Some(r)), ); let mut q = json!({}); - for k in ["name", "layer", "crs"] { + for k in ["name", "layer", "crs", "connection"] { if let Some(v) = p.get(k).filter(|v| v.as_str().is_some_and(|s| !s.trim().is_empty())) { q[k] = v.clone(); } @@ -2581,12 +2591,18 @@ impl RustGisApp { let Some(url) = p.get("url").and_then(Value::as_str).map(str::trim).filter(|u| !u.is_empty()) else { return self.run("ui.dialog", json!({"name": "addService"})); }; + let connection = p + .get("connection") + .and_then(Value::as_str) + .map(str::to_string) + .or_else(|| serviceui::connection_for(self, self.dialogs.connection.as_deref(), url).map(str::to_string)); // A catalogue: the dialog's search, run for the words given. if p.get("type").and_then(Value::as_str) == Some("csw") { if self.dialogs.open != Some("addService") { self.run("ui.dialog", json!({"name": "addService"}))?; } self.dialogs.mode = "csw"; + self.dialogs.connection = connection; self.ui.csw.url = url.to_string(); self.ui.csw.query = p.get("query").and_then(Value::as_str).unwrap_or("").to_string(); self.ui.csw.use_view = p.get("bbox").and_then(Value::as_str) == Some("view"); @@ -2611,13 +2627,23 @@ impl RustGisApp { // Another service: its own list, unfiltered (Open keeps the way back). self.ui.service_browse.filter.clear(); self.ui.service_browse.back.clear(); + let mut source = json!({"url":url}); + if let Some(connection) = &connection { + source["connection"] = json!(connection); + } + let bound = + rustgis_engine::service_logins::request_params(&mut self.session, &source).map_err(|e| connectionui::translated(&e))?; + let resolved_connection = bound.get("connection").and_then(Value::as_str).map(str::to_string); + let url = bound.get("url").and_then(Value::as_str).unwrap_or(url); let caps = rustgis_engine::data::ogc::capabilities_url(url, (kind != "auto").then_some(kind)); // A refusal is told with what the server said (#509). let slot = fetch_detailed_into(&caps); // The dialog shows the address connected to, however the connection was asked for. - self.dialogs.text = url.to_string(); + self.dialogs.text = rustgis_engine::data::login_address::inner(url).to_string(); + self.dialogs.connection = connection; self.ui.service = Some(ServiceConn { url: url.to_string(), + connection: resolved_connection, kind, slot, text: None, @@ -3618,9 +3644,9 @@ impl RustGisApp { fn report_step(&mut self, dialog: bool, r: Result) { match (dialog, r) { (true, Ok(m)) => self.dialogs.info = Some(m), - (true, Err(e)) => self.dialogs.error = Some(e), + (true, Err(e)) => self.dialogs.error = Some(connectionui::translated(&e)), (false, Ok(m)) => self.session.notify(m, false), - (false, Err(e)) => self.session.notify(e, true), + (false, Err(e)) => self.session.notify(connectionui::translated(&e), true), } } @@ -3642,7 +3668,18 @@ impl RustGisApp { Some(TilesStep::Next(next)) if resolve.read < 2 => { resolve.read += 1; resolve.doc = next.clone(); - resolve.slot = fetch_into(&next); + match serviceui::request_from( + self, + resolve.params.get("connection").and_then(Value::as_str), + resolve.params.get("url").and_then(Value::as_str).unwrap_or(""), + &next, + ) { + Ok(url) => resolve.slot = fetch_into(&url), + Err(e) => { + self.report_step(dialog, Err(e)); + continue; + } + } self.ui.service_steps.push(ServiceStep::Tiles { resolve, dialog, origin }); continue; } @@ -3667,8 +3704,13 @@ impl RustGisApp { params["describe"] = json!(String::from_utf8_lossy(&bytes)); // Not `self.run`: an error belongs where the step reports, not the notices. let c = self.session.run("service.coverage", ¶ms)?; + let connection = params.as_object_mut().and_then(|p| p.remove("connection")); let get = c.get("url").and_then(Value::as_str).unwrap_or("").to_string(); - connectiontree::run_add_url(self, json!({"url": get, "name": title, "coverage": true}), origin.as_deref()) + let mut p = json!({"url": get, "name": title, "coverage": true}); + if let Some(connection) = connection { + p["connection"] = connection; + } + connectiontree::run_add_url(self, p, origin.as_deref()) }); self.report_step(dialog, r.map(|_| format!("{} {title}", tl!("Downloading…")))); } @@ -3700,6 +3742,9 @@ impl RustGisApp { if conn.kind != "auto" { p["type"] = json!(conn.kind); } + if let Some(connection) = &conn.connection { + p["connection"] = json!(connection); + } // Not `self.run`: an error belongs in the dialog, not the notices. match self.session.run("service.layers", &p) { Ok(v) => { diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index 0ce6bdb04b..bf64251d7e 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -1667,6 +1667,7 @@ fn tile_row(app: &mut RustGisApp, ui: &mut Ui, i: usize, moves: [bool; 2], reord } let resp = resp.on_hover_text(hover); resp.context_menu(|ui| { + service_login_menu(app, ui, &tile.connection); // Stored bounds, an archive or GeoTIFF say what the layer covers. if tile.bounds.is_some() || matches!(tile.kind, TileKind::PmTiles | TileKind::Cog) { if ui.button(tl!("Zoom To Layer")).clicked() { @@ -2108,6 +2109,19 @@ fn layers_menu(app: &mut RustGisApp, ui: &mut Ui, picked: &[u32], reorder: bool) } } +/// Reopens the login of the saved service a layer records, without guessing from its address. +fn service_login_menu(app: &mut RustGisApp, ui: &mut Ui, connection: &str) { + let Some(c) = app.session.connections.find(connection).filter(|c| rustgis_engine::service_logins::is_service(c) && !c.auth.is_empty()) + else { + return; + }; + if ui.button(crate::i18n::fmt("Log In to {name}…", &[("name", &c.name)])).clicked() { + let id = c.id.clone(); + crate::connectionui::log_in(app, &id); + ui.close(); + } +} + /// A layer's right-click menu (its row in any Layers view). Move Up and Move Down only with /// `reorder` (the drawing order view, unfiltered). fn layer_menu(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, reorder: bool) { @@ -2127,6 +2141,7 @@ fn layer_menu(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, reorder: bool) { let _ = app.run("ui.refreshService", json!({"layer":l.id})); ui.close(); } + service_login_menu(app, ui, &l.connection); let login = l.source.as_ref().and_then(|s| { let (kind, database) = rustgis_engine::connections::layer_database(s)?; let connections: Vec<_> = app.session.connections.matching_database(kind, database).into_iter().cloned().collect(); @@ -2153,6 +2168,7 @@ fn layer_menu(app: &mut RustGisApp, ui: &mut Ui, l: &Layer, reorder: bool) { key, connections: connections.iter().map(|c| c.id.clone()).collect(), layers: vec![l.id], + tiles: 0, opened, prompted: true, }, diff --git a/crates/ui-egui/src/serviceui.rs b/crates/ui-egui/src/serviceui.rs index aad69113f5..2084985eb3 100644 --- a/crates/ui-egui/src/serviceui.rs +++ b/crates/ui-egui/src/serviceui.rs @@ -1,5 +1,6 @@ //! Background group import/refresh for both desktop and browser. Discovery and updates live in -//! engine commands; this state machine only supplies their downloaded documents. +//! engine commands; this state machine supplies their downloaded documents and preserves the +//! saved connection id through every deferred add. use crate::{RustGisApp, Slot, fetch_into, fetch_into_limited, i18n}; use rustgis_engine::data::ogc::{self, Pages, Step}; @@ -9,6 +10,8 @@ use std::collections::VecDeque; pub struct Import { params: Value, refresh: bool, + /// The saved web-service connection to record on imported layers, not a group-plan parameter. + connection: Option, token: u64, /// The originating map: its index and name (names are unique among a project's maps), so a /// deleted or replaced map at that index cancels the import. @@ -44,6 +47,27 @@ pub(crate) fn kind_of(t: &str) -> &'static str { } } +/// A dialog's provenance is usable only for a saved service of the displayed source's origin. +pub(crate) fn connection_for<'a>(app: &RustGisApp, connection: Option<&'a str>, url: &str) -> Option<&'a str> { + connection.filter(|id| { + app.session.connections.find(id).is_some_and(|c| { + rustgis_engine::service_logins::is_service(c) + && rustgis_engine::connections::login_origin(c) + .is_some_and(|origin| rustgis_engine::render::rustgis_tiles::logins::origin(url).as_deref() == Some(origin.as_str())) + }) + }) +} + +/// A linked document never acquires another origin's saved login, even without explicit provenance. +pub(crate) fn request_from(app: &mut RustGisApp, connection: Option<&str>, base: &str, url: &str) -> Result { + use rustgis_engine::render::rustgis_tiles::logins; + if logins::origin(base).is_some_and(|origin| logins::origin(url).as_deref() == Some(origin.as_str())) { + rustgis_engine::service_logins::request(&mut app.session, connection, url).map_err(|e| crate::connectionui::translated(&e)) + } else { + Ok(logins::inner(url).to_string()) + } +} + /// Adds one of a service's layers (as `service.layers` lists it): features are downloaded in the /// background as From URL… does, map layers are added now, and an OGC API layer or a WCS coverage /// waits on a document (a step the app moves along). A feature layer is asked only for the @@ -57,12 +81,13 @@ pub(crate) fn add_layer( area: Option<[f64; 4]>, dialog: bool, ) -> Result<&'static str, String> { - add_layer_from(app, src, l, time, area, dialog, None) + let connection = dialog.then(|| app.ui.service.as_ref().and_then(|source| source.connection.clone())).flatten(); + add_layer_from(app, src, l, time, area, dialog, None, connection.as_deref()) } -/// [`add_layer`] for a layer of the connection named `origin` (the Connections tree): once the -/// layer is added, now or when its document or download lands, Recent shows its source under that -/// name. An add that fails or is dropped is not renamed. +/// [`add_layer`] for a saved connection: `connection` is its id, recorded on the resulting layer; +/// `origin` is its display name for Recent. Both survive deferred documents and downloads. +#[allow(clippy::too_many_arguments)] pub(crate) fn add_layer_from( app: &mut RustGisApp, src: &ServiceSource, @@ -71,7 +96,9 @@ pub(crate) fn add_layer_from( area: Option<[f64; 4]>, dialog: bool, origin: Option<&str>, + connection: Option<&str>, ) -> Result<&'static str, String> { + let connection = connection_for(app, connection, &src.url); let name = l.get("name").and_then(Value::as_str).unwrap_or("").to_string(); let title = l.get("title").and_then(Value::as_str).unwrap_or(&name).to_string(); let offer = l.get("offer").cloned().unwrap_or_default(); @@ -89,8 +116,11 @@ pub(crate) fn add_layer_from( let items = asked.as_deref().unwrap_or(items); // The layer's bounds go along, for its features' axes (#1053). let bounds = l.get("bbox").cloned().unwrap_or_default(); - return crate::connectiontree::run_add_url(app, json!({"url": items, "name": title, "service": true, "bounds": bounds}), origin) - .map(|_| tl!("Downloading…")); + let mut p = json!({"url": items, "name": title, "service": true, "bounds": bounds}); + if let Some(connection) = connection { + p["connection"] = json!(connection); + } + return crate::connectiontree::run_add_url(app, p, origin).map(|_| tl!("Downloading…")); } let origin = origin.map(str::to_string); if offer.get("type").and_then(Value::as_str) == Some("coverage") { @@ -100,6 +130,10 @@ pub(crate) fn add_layer_from( if src.kind != "auto" { p["type"] = json!(src.kind); } + if let Some(connection) = connection { + p["connection"] = json!(connection); + } + let describe = request_from(app, connection, &src.url, &describe)?; let slot = crate::fetch_into(&describe); app.ui.service_steps.push(crate::ServiceStep::Coverage { params: p, slot, dialog, origin }); return Ok(tl!("Describing")); @@ -108,6 +142,9 @@ pub(crate) fn add_layer_from( if src.kind != "auto" { p["type"] = json!(src.kind); } + if let Some(connection) = connection { + p["connection"] = json!(connection); + } if l.get("time").is_some() && !time.is_empty() { p["time"] = json!(time); } @@ -118,8 +155,9 @@ pub(crate) fn add_layer_from( p["name"] = json!(title); let fallback = offer.get("url").and_then(Value::as_str).unwrap_or("").to_string(); let vector = offer.get("type").and_then(Value::as_str) == Some("vectorTiles"); - let slot = crate::fetch_into(list); - let resolve = crate::TilesResolve { params: p, list: list.to_string(), doc: list.to_string(), read: 0, vector, fallback, slot }; + let list = request_from(app, connection, &src.url, list)?; + let slot = crate::fetch_into(&list); + let resolve = crate::TilesResolve { params: p, list: list.clone(), doc: list, read: 0, vector, fallback, slot }; app.ui.service_steps.push(crate::ServiceStep::Tiles { resolve, dialog, origin }); Ok(tl!("Reading the tileset of")) } @@ -137,7 +175,11 @@ pub fn start(app: &mut RustGisApp, p: &Value, refresh: bool) -> Result<(), Strin if app.ui.service_import.is_some() { return Err(tl!("A service import or refresh is already running.").into()); } - let mut source = p.clone(); + // ui.importService accepts `connection`, as service.import does. The discovery command does + // not: keep it apart until the final add, without leaking it into service.groupPlan calls. + let bound = rustgis_engine::service_logins::request_params(&mut app.session, p).map_err(|e| crate::connectionui::translated(&e))?; + let mut source = bound.into_owned(); + let connection = if refresh { None } else { source.as_object_mut().and_then(|p| p.remove("connection")) }; source["sourceOnly"] = json!(true); let mut params = app.session.run("service.groupPlan", &source)?; if let Some(p) = params.as_object_mut() { @@ -151,6 +193,7 @@ pub fn start(app: &mut RustGisApp, p: &Value, refresh: bool) -> Result<(), Strin let mut task = Import { params, refresh, + connection, token: app.session.download_started(), map: active_map(app), slot: Slot::default(), @@ -169,6 +212,13 @@ pub fn start(app: &mut RustGisApp, p: &Value, refresh: bool) -> Result<(), Strin let url = task.params.get("url").and_then(Value::as_str).unwrap_or_default(); let kind = task.params.get("type").and_then(Value::as_str); let caps = rustgis_engine::data::ogc::capabilities_url(url, kind); + let caps = match request_from(app, task.connection.as_ref().and_then(Value::as_str), url, &caps) { + Ok(caps) => caps, + Err(e) => { + app.session.download_failed(task.token); + return Err(e); + } + }; task.slot = fetch_into(&caps); task.document = Some(caps); } @@ -181,8 +231,10 @@ impl Import { fn discover(&mut self, app: &mut RustGisApp) -> Result<(), String> { let listing = app.session.run("service.groupPlan", &self.params)?; if let Some(next) = listing.get("next").and_then(Value::as_str) { - self.document = Some(next.into()); - self.slot = fetch_into(next); + let url = self.params.get("url").and_then(Value::as_str).unwrap_or_default(); + let next = request_from(app, self.connection.as_ref().and_then(Value::as_str), url, next)?; + self.slot = fetch_into(&next); + self.document = Some(next); } else { self.remaining = listing .get("layers") @@ -191,16 +243,19 @@ impl Import { .flatten() .filter_map(|l| l.get("offer").and_then(|o| o.get("url")).and_then(Value::as_str).map(str::to_string)) .collect(); - self.next(); + self.next(app)?; } Ok(()) } - fn next(&mut self) { + fn next(&mut self, app: &mut RustGisApp) -> Result<(), String> { self.current = self.remaining.pop_front(); if let Some(url) = &self.current { - self.slot = fetch_into_limited(url, rustgis_engine::cmd::service::MAX_GROUP_BYTES.saturating_sub(self.bytes)); + let base = self.params.get("url").and_then(Value::as_str).unwrap_or_default(); + let url = request_from(app, self.connection.as_ref().and_then(Value::as_str), base, url)?; + self.slot = fetch_into_limited(&url, rustgis_engine::cmd::service::MAX_GROUP_BYTES.saturating_sub(self.bytes)); } + Ok(()) } fn landed(&mut self, app: &mut RustGisApp, bytes: Vec) -> Result<(), String> { @@ -222,7 +277,7 @@ impl Import { } let text = String::from_utf8(bytes).map_err(|_| tl!("The service's answer isn't UTF-8 text").to_string())?; self.responses.push(json!({"url":self.current.take(),"text":text})); - self.next(); + self.next(app)?; Ok(()) } } @@ -259,13 +314,16 @@ pub fn poll(app: &mut RustGisApp, ctx: &egui::Context) { p.remove(key); } } + if let Some(connection) = task.connection { + task.params["connection"] = connection; + } let result = app.run(if task.refresh { "service.refresh" } else { "service.import" }, task.params); match result { Ok(_) => { app.dialogs.info = Some(tl!("Done").into()); app.map.invalidate(); } - Err(e) => app.dialogs.error = Some(e), + Err(e) => app.dialogs.error = Some(crate::connectionui::translated(&e)), } } else { ctx.request_repaint_after(std::time::Duration::from_millis(30)); @@ -336,8 +394,10 @@ pub fn poll_pages(app: &mut RustGisApp, ctx: &egui::Context) { fn go_on(app: &mut RustGisApp, landed: Landed, left: usize, step: Step) { match step { Step::Fetch(pages) => { - let slot = match rustgis_engine::render::rustgis_tiles::address::check(pages.next_url(), Some(pages.referrer())) { - Ok(()) => fetch_into_limited(pages.next_url(), left), + let next = rustgis_engine::render::rustgis_tiles::address::check(pages.next_url(), Some(pages.referrer())) + .and_then(|()| request_from(app, landed.params.get("connection").and_then(Value::as_str), &landed.url, pages.next_url())); + let slot = match next { + Ok(url) => fetch_into_limited(&url, left), Err(e) => Slot::new(std::sync::Mutex::new(Some(Err(e)))), }; app.ui.service_pages.push(PageRead { landed, pages, left, slot }); diff --git a/crates/ui-egui/src/sourcespane.rs b/crates/ui-egui/src/sourcespane.rs index 09f9c6bbad..ab0224351c 100644 --- a/crates/ui-egui/src/sourcespane.rs +++ b/crates/ui-egui/src/sourcespane.rs @@ -350,21 +350,29 @@ fn connection_header(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, c: &Connecti for (label, command, params) in items { if ui.button(label).clicked() { if let Err(e) = app.run(command, params) { - app.session.notify(e, true); + app.session.notify(crate::connectionui::translated(&e), true); } ui.close(); } } if !unavailable { let keys = c.kind == "s3" && c.options.get("credentials").is_some_and(|v| v == "keys"); - if (DATABASE_KINDS.contains(&c.kind.as_str()) || keys) && ui.button(tl!("Log In…")).clicked() { + let service_login = rustgis_engine::service_logins::is_service(c) && !c.auth.is_empty(); + if (DATABASE_KINDS.contains(&c.kind.as_str()) || keys || service_login) && ui.button(tl!("Log In…")).clicked() { crate::connectionui::log_in(app, &c.id); ui.close(); } if DATABASE_KINDS.contains(&c.kind.as_str()) && c.remembered && ui.button(tl!("Forget Saved Password")).clicked() { match app.run("connection.forgetPassword", json!({"id":c.id})) { Ok(_) => crate::connectiontree::refresh(app, Some(&c.id)), - Err(e) => app.session.notify(e, true), + Err(e) => app.session.notify(crate::connectionui::translated(&e), true), + } + ui.close(); + } + if service_login && rustgis_engine::service_logins::logged_in(&app.session, c) && ui.button(tl!("Log Out")).clicked() { + match app.run("connection.logout", json!({"id": c.id})) { + Ok(_) => crate::connectiontree::refresh(app, Some(&c.id)), + Err(e) => app.session.notify(crate::connectionui::translated(&e), true), } ui.close(); } @@ -378,7 +386,7 @@ fn connection_header(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, c: &Connecti // The kind's own add dialog, as a click on the connection opened it before. if ui.button(tl!("Add With Options…")).clicked() { if let Err(e) = crate::connectionui::open_connection(app, c) { - app.session.notify(e, true); + app.session.notify(crate::connectionui::translated(&e), true); } ui.close(); } @@ -388,7 +396,7 @@ fn connection_header(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, c: &Connecti tl!("The keys saved on this computer were forgotten; a login in this session keeps working.").to_string(), false, ), - Err(e) => app.session.notify(e, true), + Err(e) => app.session.notify(crate::connectionui::translated(&e), true), } ui.close(); } @@ -400,7 +408,7 @@ fn connection_header(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, c: &Connecti if ui.button(tl!("Remove")).clicked() { match app.run("connection.remove", json!({"id":c.id})) { Ok(_) => crate::connectiontree::refresh(app, Some(&c.id)), - Err(e) => app.session.notify(e, true), + Err(e) => app.session.notify(crate::connectionui::translated(&e), true), } ui.close(); } diff --git a/crates/ui-egui/src/stacui.rs b/crates/ui-egui/src/stacui.rs index b4a5cb10e1..6c42191e9a 100644 --- a/crates/ui-egui/src/stacui.rs +++ b/crates/ui-egui/src/stacui.rs @@ -30,8 +30,8 @@ enum Asked { /// A search, by its address (which the answer's links are resolved against). Search(String), /// A token for an asset that needs signing: the token service's address, the asset's and the - /// layer's name. - Token { token_url: String, href: String, name: String }, + /// layer's name and saved connection, captured when Add was requested. + Token { token_url: String, href: String, name: String, connection: Option }, /// An item's or a collection's document (a `?stac=` link), by its address. Link(String), /// A static catalog's document, for its children, by its address. @@ -219,6 +219,16 @@ fn fetch(url: &str) -> Answer { slot } +/// Reads with the selected API's login only; a catalog's foreign links stay anonymous. +fn fetch_service(app: &mut RustGisApp, url: &str) -> Answer { + let connection = selected_connection(app); + let base = app.ui.stac.api.clone(); + match crate::serviceui::request_from(app, connection.as_deref(), &base, url) { + Ok(url) => fetch(&url), + Err(e) => Slot::new(std::sync::Mutex::new(Some(Err(crate::connectionui::translated(&e))))), + } +} + /// An item's preview picture: being fetched, shown, or not to be had. enum Thumb { Loading(Answer), @@ -269,7 +279,10 @@ fn poll_thumbs(app: &mut RustGisApp, ctx: &egui::Context) { /// Starts fetching the preview pictures of the items found (those on the web an item may point /// to, not ones that need signing). -fn want_thumbs(st: &mut State) { +fn want_thumbs(app: &mut RustGisApp) { + let connection = selected_connection(app); + let base = app.ui.stac.api.clone(); + let (session, st) = (&mut app.session, &mut app.ui.stac); if st.thumbs.len() > MAX_THUMBS { st.thumbs.clear(); } @@ -284,13 +297,23 @@ fn want_thumbs(st: &mut State) { Some(item.origin.as_deref().unwrap_or("https://item.invalid/")), ) .is_ok(); - let picture = || { + let address = if ok { + use rustgis_engine::render::rustgis_tiles::logins; + if logins::origin(&base).is_some_and(|origin| logins::origin(&a.href).as_deref() == Some(origin.as_str())) { + rustgis_engine::service_logins::request(session, connection.as_deref(), &a.href).ok() + } else { + Some(logins::inner(&a.href).to_string()) + } + } else { + None + }; + let picture = address.map(|address| { let slot: Answer = Slot::default(); let land = slot.clone(); - cache::download_limited(&a.href, THUMB_BYTES, Box::new(move |r| *land.lock().unwrap_or_else(|e| e.into_inner()) = Some(r))); - slot - }; - st.thumbs.insert(a.href.clone(), if ok { Thumb::Loading(picture()) } else { Thumb::Failed }); + cache::download_limited(&address, THUMB_BYTES, Box::new(move |r| *land.lock().unwrap_or_else(|e| e.into_inner()) = Some(r))); + Thumb::Loading(slot) + }); + st.thumbs.insert(a.href.clone(), picture.unwrap_or(Thumb::Failed)); } } @@ -333,10 +356,10 @@ pub fn poll(app: &mut RustGisApp, ctx: &egui::Context) { } Err(e) => st.error = Some(e), }, - Asked::Token { token_url, href, name } => match r.and_then(|b| stac::parse_sas(&b)) { + Asked::Token { token_url, href, name, connection } => match r.and_then(|b| stac::parse_sas(&b)) { Ok(token) => { stac::remember_token(&token_url, &token); - add_url(app, &stac::sign(&href, &token), &name); + add_url_from(app, &stac::sign(&href, &token), &name, connection.as_deref()); } Err(e) => st.error = Some(e), }, @@ -372,7 +395,7 @@ pub fn poll(app: &mut RustGisApp, ctx: &egui::Context) { /// Drives a static catalog's walk: lands the documents fetched, asks for the next ones (a few at /// a time) and, once it is over, shows the items found. fn poll_walk(app: &mut RustGisApp, ctx: &egui::Context) { - let Some((walk, fetching)) = &mut app.ui.stac.walk else { return }; + let Some((mut walk, mut fetching)) = app.ui.stac.walk.take() else { return }; let mut i = 0; while let Some((url, slot)) = fetching.get(i) { let landed = slot.lock().unwrap_or_else(|e| e.into_inner()).take(); @@ -386,7 +409,7 @@ fn poll_walk(app: &mut RustGisApp, ctx: &egui::Context) { } while fetching.len() < catalog::AT_ONCE { let Some(url) = walk.next_url() else { break }; - let slot = fetch(&url); + let slot = fetch_service(app, &url); fetching.push((url, slot)); } if !walk.finished() { @@ -395,9 +418,9 @@ fn poll_walk(app: &mut RustGisApp, ctx: &egui::Context) { &[("n", &walk.reads.to_string()), ("m", &walk.items().len().to_string())], )); ctx.request_repaint_after(std::time::Duration::from_millis(100)); + app.ui.stac.walk = Some((walk, fetching)); return; } - let Some((walk, _)) = app.ui.stac.walk.take() else { return }; let st = &mut app.ui.stac; let n = walk.items().len().to_string(); st.info = Some(if walk.complete() { @@ -506,7 +529,8 @@ fn add_signed(app: &mut RustGisApp, href: &str, name: &str) { None => { let slot = fetch(&token_url); app.ui.stac.info = Some(tl!("Signing the asset (Planetary Computer)…").to_string()); - app.ui.stac.waiting = Some((Asked::Token { token_url, href: href.to_string(), name: name.to_string() }, slot)); + let connection = selected_connection(app); + app.ui.stac.waiting = Some((Asked::Token { token_url, href: href.to_string(), name: name.to_string(), connection }, slot)); } }, Err(e) => app.ui.stac.error = Some(e), @@ -514,7 +538,21 @@ fn add_signed(app: &mut RustGisApp, href: &str, name: &str) { } fn add_url(app: &mut RustGisApp, href: &str, name: &str) { - if let Err(e) = app.run("ui.addUrl", json!({"url": href, "name": name, "zoom": true})) { + let connection = selected_connection(app); + add_url_from(app, href, name, connection.as_deref()); +} + +fn selected_connection(app: &RustGisApp) -> Option { + crate::connectionui::picked(&app.session.connections.connections, app.ui.stac.connection.as_deref(), &app.ui.stac.api) + .map(|c| c.id.clone()) +} + +fn add_url_from(app: &mut RustGisApp, href: &str, name: &str, connection: Option<&str>) { + let mut p = json!({"url": href, "name": name, "zoom": true}); + if let Some(connection) = connection { + p["connection"] = json!(connection); + } + if let Err(e) = app.run("ui.addUrl", p) { app.ui.stac.error = Some(e); } } @@ -533,7 +571,8 @@ pub fn open_link(app: &mut RustGisApp, url: &str) -> Result<(), String> { let url = url.trim(); rustgis_engine::render::rustgis_tiles::address::check(url, None)?; app.run("ui.dialog", serde_json::json!({"name": "stac"}))?; - let slot = fetch(url); + let address = rustgis_engine::service_logins::request(&mut app.session, None, url)?; + let slot = fetch(&address); app.ui.stac.error = None; app.ui.stac.info = Some(i18n::fmt("Opening {url}…", &[("url", url)])); app.ui.stac.waiting = Some((Asked::Link(url.to_string()), slot)); @@ -660,7 +699,8 @@ fn fetch_children(app: &mut RustGisApp) { } st.info = Some(tl!("Reading the catalog…").to_string()); st.counted = false; - st.waiting = Some((Asked::Children(url.clone()), fetch(&url))); + let slot = fetch_service(app, &url); + app.ui.stac.waiting = Some((Asked::Children(url), slot)); } /// The preset the dialog is showing, if it is still on its API or catalog. @@ -871,7 +911,8 @@ pub fn dialog(app: &mut RustGisApp, ui: &mut egui::Ui) { match stac::api(Some(app.ui.stac.api.as_str())) { Ok(api) => { let typed = app.ui.stac.api.trim().to_string(); - app.ui.stac.waiting = Some((Asked::Collections(typed), fetch(&stac::collections_url(&api)))); + let slot = fetch_service(app, &stac::collections_url(&api)); + app.ui.stac.waiting = Some((Asked::Collections(typed), slot)); } Err(e) => app.ui.stac.error = Some(e), } @@ -1043,7 +1084,7 @@ fn find_items(app: &mut RustGisApp) { let url = stac::api(Some(app.ui.stac.api.as_str())).and_then(|api| search.and_then(|s| stac::search_url(&api, &s))); match url { Ok(url) => { - let slot = fetch(&url); + let slot = fetch_service(app, &url); app.ui.stac.waiting = Some((Asked::Search(url), slot)); } Err(e) => app.ui.stac.error = Some(e), @@ -1056,7 +1097,7 @@ fn results(app: &mut RustGisApp, ui: &mut egui::Ui) { if app.ui.stac.items.is_empty() { return; } - want_thumbs(&mut app.ui.stac); + want_thumbs(app); let (mut zoom, mut add, mut details) = (None, None, None); // A window starts from its last size: without a floor the list would get egui's 64 px back. let tall = (app.ui.stac.items.len() as f32 * 96.0 + if app.ui.stac.details.is_some() { 200.0 } else { 0.0 }).min(440.0); @@ -1160,6 +1201,30 @@ mod tests { use super::*; use crate::RustGisApp; + #[test] + fn a_deferred_token_add_keeps_its_original_connection_when_selection_changes() { + let mut app = RustGisApp::with_sample(); + let api = "http://127.0.0.1:9/stac"; + let one = app.run("connection.new", json!({"kind":"stac", "name":"One", "address":api})).unwrap(); + let two = app.run("connection.new", json!({"kind":"stac", "name":"Two", "address":api})).unwrap(); + app.ui.stac.api = api.into(); + app.ui.stac.connection = one["id"].as_str().map(str::to_string); + let asked = Asked::Token { + token_url: "https://snapshot-token.invalid/token".into(), + href: "http://127.0.0.1:9/points.geojson".into(), + name: "Original asset".into(), + connection: selected_connection(&app), + }; + let slot = Slot::default(); + *slot.lock().unwrap() = Some(Ok(br#"{"token":"sig=snapshot"}"#.to_vec())); + app.ui.stac.waiting = Some((asked, slot)); + app.ui.stac.connection = two["id"].as_str().map(str::to_string); + poll(&mut app, &egui::Context::default()); + let (_, _, params, _) = app.ui.downloads.last().unwrap(); + assert_eq!(params["connection"], one["id"], "token completion must not re-read the picker"); + assert_ne!(params["connection"], two["id"]); + } + #[test] fn presets_open_the_dialog_set_up() { for p in Preset::ALL { diff --git a/crates/ui-egui/tests/service_logins/mod.rs b/crates/ui-egui/tests/service_logins/mod.rs new file mode 100644 index 0000000000..c5c3741368 --- /dev/null +++ b/crates/ui-egui/tests/service_logins/mod.rs @@ -0,0 +1,241 @@ +use super::*; +use egui::accesskit::Role; +use egui_kittest::kittest::NodeT; +use rustgis_engine::connections::{LoginReason, LoginRequest, WEB}; + +type Harness = egui_kittest::Harness<'static, RustGisApp>; + +fn session_ui() -> Harness { + with_app(RustGisApp::with_sample()) +} + +fn with_app(app: RustGisApp) -> Harness { + egui_kittest::Harness::builder().with_size(egui::vec2(1400.0, 1000.0)).with_max_steps(64).build_ui_state( + |ui, app: &mut RustGisApp| { + let ctx = ui.ctx().clone(); + app.logic(&ctx); + app.ui(ui); + }, + app, + ) +} +fn settle(h: &mut Harness) { + h.run_steps(4); +} +fn type_field(h: &mut Harness, label: &str, text: &str, secret: bool) { + h.get_by_role_and_label(if secret { Role::PasswordInput } else { Role::TextInput }, label).focus(); + h.step(); + h.event(egui::Event::Text(text.into())); + settle(h); +} +fn header_form(h: &mut Harness, address: &str) { + h.state_mut().run("ui.connection", json!({"kind":"wms"})).unwrap(); + settle(h); + type_field(h, "Name", "Saved web login", false); + type_field(h, "Address", address, false); + h.get_by_role_and_label(Role::ComboBox, "Method").click(); + settle(h); + h.get_by_label("API key in a header").click(); + settle(h); + type_field(h, "Header name", "X-Api-Key", false); + type_field(h, "Key", "web-widget-secret-sentinel", true); + assert!(!h.state().ui.connection.remember); +} +fn assert_redacted(h: &Harness) { + let app = h.state(); + let secret = "web-widget-secret-sentinel"; + assert!(!app.session.connections.to_json().to_string().contains(secret)); + assert!(!app.session.notices.iter().any(|n| n.text.contains(secret))); + assert!(app.ui.connection.api_key.is_empty()); + assert!(app.ui.connection.password.is_empty()); + assert!(!app.ui.connection.remember); + assert!(!app.ui.connection.allow_insecure); + assert!(!app.dialogs.error.as_deref().unwrap_or("").contains(secret)); + assert!(!app.dialogs.info.as_deref().unwrap_or("").contains(secret)); + assert!(!app.ui.connection.warned.as_deref().unwrap_or("").contains(secret)); +} + +#[test] +fn service_key_save_redaction_status_and_logout() { + let mut h = session_ui(); + header_form(&mut h, "https://saved-web-widget.example/wms"); + assert!(h.query_by_label("Remember on this computer").is_none()); + h.get_by_role_and_label(Role::Button, "Save").click(); + settle(&mut h); + assert_eq!(h.state().dialogs.open, None); + let c = h.state().session.connections.find("Saved web login").unwrap().clone(); + assert!(!c.remembered); + assert_redacted(&h); + h.state_mut().run("ui.connection", json!({"id":c.id})).unwrap(); + settle(&mut h); + h.get_by_label("Logged in"); + assert!(h.query_by_label("Saved on this computer").is_none()); + assert!(h.query_by_label("Forget Saved Login").is_none()); + assert!(rustgis_engine::service_logins::logged_in(&h.state().session, &c)); + h.get_by_role_and_label(Role::Button, "Log Out").click(); + settle(&mut h); + assert!(!rustgis_engine::service_logins::logged_in(&h.state().session, &c)); +} + +#[test] +fn basic_login_is_masked_and_session_only_even_with_a_credential_store() { + use rustgis_engine::data::secrets::{MemorySecrets, SecretError}; + let app = RustGisApp::with_sample().with_services(rustgis_ui_egui::Services { + secrets: Some(std::sync::Arc::new(MemorySecrets::failing(SecretError::Denied))), + ..Default::default() + }); + let mut h = with_app(app); + h.state_mut().run("ui.connection", json!({"kind":"wms"})).unwrap(); + settle(&mut h); + assert!(!h.get_by_role_and_label(Role::Button, "Test").accesskit_node().is_disabled(), "new anonymous forms can test"); + type_field(&mut h, "Name", "Session basic login", false); + type_field(&mut h, "Address", "https://basic-web-widget.example/wms", false); + h.get_by_role_and_label(Role::ComboBox, "Method").click(); + settle(&mut h); + h.get_by_label("Basic authentication").click(); + settle(&mut h); + type_field(&mut h, "User", "reader", false); + type_field(&mut h, "Password", "web-widget-secret-sentinel", true); + assert!(h.query_by_label("Remember on this computer").is_none()); + assert!(h.get_by_role_and_label(Role::Button, "Test").accesskit_node().is_disabled()); + h.get_by_role_and_label(Role::Button, "Save").click(); + settle(&mut h); + assert_eq!(h.state().dialogs.open, None); + let c = h.state().session.connections.find("Session basic login").unwrap(); + assert!(!c.remembered); + assert!(rustgis_engine::service_logins::logged_in(&h.state().session, c)); + assert_redacted(&h); +} + +#[test] +fn service_secret_validation_precedes_saving_the_definition() { + let mut h = session_ui(); + header_form(&mut h, "https://refused-web-widget.example/wms"); + let invalid = "web-widget-secret-sentinel".repeat(200); + h.state_mut().ui.connection.api_key = invalid.clone(); + h.get_by_role_and_label(Role::Button, "Save").click(); + settle(&mut h); + assert_eq!(h.state().dialogs.open, Some("newConnection")); + assert!(h.state().dialogs.error.is_some()); + assert!(h.state().ui.connection.id.is_none()); + assert!(h.state().session.connections.connections.is_empty()); + assert_eq!(h.state().ui.connection.api_key, invalid, "unsent input remains editable"); +} + +#[test] +fn service_http_needs_confirmation_and_unsaved_login_cannot_be_tested() { + let mut h = session_ui(); + header_form(&mut h, "http://insecure-web-widget.example/wms"); + assert!(h.get_by_role_and_label(Role::Button, "Test").accesskit_node().is_disabled()); + assert!(h.state().session.connections.connections.is_empty()); + h.get_by_role_and_label(Role::Button, "Save").click(); + settle(&mut h); + assert!(h.state().session.connections.connections.is_empty()); + assert!(h.state().dialogs.error.as_deref().unwrap().contains("unencrypted")); + h.get_by_label("Send the login over unencrypted http://").click(); + settle(&mut h); + h.get_by_role_and_label(Role::Button, "Save").click(); + settle(&mut h); + assert_eq!(h.state().dialogs.open, None); + assert!(!h.state().session.connections.find("Saved web login").unwrap().remembered); + assert_redacted(&h); +} + +#[test] +fn edited_service_test_button_is_disabled_and_retains_the_unsent_key() { + let mut h = session_ui(); + let c = h + .state_mut() + .run( + "connection.new", + json!({ + "kind":"wms", "address":"https://unchanged-widget.example/wms", "auth":"header", "keyName":"X-Api-Key" + }), + ) + .unwrap(); + h.state_mut().run("ui.connection", json!({"id":c["id"]})).unwrap(); + settle(&mut h); + assert!(!h.get_by_role_and_label(Role::Button, "Test").accesskit_node().is_disabled()); + h.state_mut().ui.connection.address = "https://edited-widget.example/wms".into(); + h.state_mut().ui.connection.api_key = "unsent-widget-key".into(); + settle(&mut h); + assert!(h.get_by_role_and_label(Role::Button, "Test").accesskit_node().is_disabled()); + assert_eq!(h.state().ui.connection.api_key, "unsent-widget-key"); + assert!(h.state().ui.connection.test.is_none()); + assert!(!h.state().session.database_logins.has_session_password(c["id"].as_str().unwrap())); +} + +fn request(h: &mut Harness, reason: LoginReason, key: &str, connections: Vec) { + let opened = h.state().session.opened; + h.state_mut().session.database_logins.request(LoginRequest { + reason, + kind: WEB, + key: key.into(), + connections, + layers: Vec::new(), + tiles: 1, + opened, + prompted: false, + }); + settle(h); +} + +#[test] +fn service_missing_connection_prompt_prefills_origin_and_cancel_stays_closed() { + let mut h = session_ui(); + let origin = "https://missing-web-widget.example"; + request(&mut h, LoginReason::Connection, origin, Vec::new()); + assert_eq!(h.state().dialogs.open, Some("chooseConnection")); + h.get_by_label("New Connection…").click(); + settle(&mut h); + assert_eq!(h.state().dialogs.open, Some("newConnection")); + assert_eq!(h.state().ui.connection.kind, "service"); + assert_eq!(h.state().ui.connection.address, origin); + h.get_by_role_and_label(Role::Button, "Cancel").click(); + settle(&mut h); + assert_eq!(h.state().dialogs.open, None); +} + +#[test] +fn a_connection_made_for_a_missing_one_is_taken_by_the_layers_waiting_for_it() { + let mut h = session_ui(); + let origin = "https://adopt-web-widget.example"; + h.state_mut().run("map.addTiles", json!({"url":format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png")})).unwrap(); + h.state_mut().session.map_mut().tiles[0].connection = "deleted-connection".into(); + request(&mut h, LoginReason::Connection, origin, Vec::new()); + h.get_by_label("New Connection…").click(); + settle(&mut h); + type_field(&mut h, "Name", "Replacement", false); + h.get_by_role_and_label(Role::Button, "Save").click(); + settle(&mut h); + assert_eq!(h.state().dialogs.open, None); + let id = h.state().session.connections.find("Replacement").unwrap().id.clone(); + assert_eq!(h.state().session.map().tiles[0].connection, id); +} + +#[test] +fn service_chooser_accepts_auth_none_and_password_request_opens_login() { + let mut h = session_ui(); + let origin = "https://choose-web-widget.example"; + let public = h.state_mut().run("connection.new", json!({"kind":"wms","name":"Public web service","address":origin})).unwrap(); + let private = h + .state_mut() + .run("connection.new", json!({"kind":"wms","name":"Private web service","address":origin,"auth":"basic","user":"reader"})) + .unwrap(); + let public = public["id"].as_str().unwrap().to_string(); + let private = private["id"].as_str().unwrap().to_string(); + request(&mut h, LoginReason::Choose, origin, vec![public.clone(), private.clone()]); + assert_eq!(h.state().dialogs.open, Some("chooseConnection")); + h.query_all_by_label("Public web service").next().unwrap().click(); + settle(&mut h); + h.get_by_role_and_label(Role::Button, "Use Connection").click(); + settle(&mut h); + assert_eq!(h.state().dialogs.open, None); + assert_eq!(h.state().session.database_logins.selection(WEB, origin), Some(public.as_str())); + request(&mut h, LoginReason::Password, origin, vec![private.clone()]); + assert_eq!(h.state().dialogs.open, Some("editConnection")); + assert!(h.state().ui.connection.login); + assert_eq!(h.state().ui.connection.id.as_deref(), Some(private.as_str())); + h.get_by_role_and_label(Role::PasswordInput, "Password"); + assert!(h.query_by_label("Remember on this computer").is_none()); +} diff --git a/crates/ui-egui/tests/smoke.rs b/crates/ui-egui/tests/smoke.rs index 19a6155b8f..315894e2de 100644 --- a/crates/ui-egui/tests/smoke.rs +++ b/crates/ui-egui/tests/smoke.rs @@ -8,6 +8,7 @@ use serde_json::json; mod remembered_database; mod remembered_s3; +mod service_logins; /// Builds a headless World Atlas UI for interaction tests. fn harness() -> egui_kittest::Harness<'static, RustGisApp> { egui_kittest::Harness::builder().with_size(egui::vec2(1400.0, 860.0)).with_max_steps(64).build_ui_state( From 09afac92e51c80654fa78974853424d505101e16 Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Mon, 2 Nov 2026 12:11:18 +0100 Subject: [PATCH 04/19] docs(data): web-service logins and connections recorded in projects --- docs/user-guide/data.md | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index daec878fc2..1833c36eb6 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -736,8 +736,8 @@ session registry used by the S3 dialog and `s3.connect`, so editing or removing connection also affects a session entry of the same name created there. Renaming clears runtime entries under both the old and new names, so the renamed connection cannot inherit an unrelated login at its destination name. -Web-service logins are not supported. Plugins may list connections but cannot manage, log in -to, forget passwords or S3 keys for, choose an S3 connection for, test or browse them. The in-app +Plugins may list connections but cannot manage, log in to, log out of, forget passwords, logins +or S3 keys for, choose an S3 connection for, test or browse them. The in-app assistant asks for fresh approval each time for those operations, including Forget, S3 source selection and scripted calls; external assistants using MCP cannot run them. Even `probe: true` needs approval: connection operations cannot be approved for the whole conversation. @@ -751,6 +751,36 @@ session endpoint and credentials under the form's connection name, including a s session created in the S3 dialog. Cancel does not disconnect that login or restore the previous one. CLI and Python connections last only for the run. +#### Web-service logins + +A WMS, WMTS, WFS, WCS, OGC API, ArcGIS REST, CSW or STAC connection can log in: its form's +**Login** picks **None**, **Basic authentication** (a user name and a password) or **API key in a +header** (the header's name, such as `X-API-Key`, and the key). The connection keeps only the method, +the user name and the header name, never the password or key. +The password or key goes to `connection.login {"id":"c1","password":"…"}` (basic) or +`{"id":"c1","apiKey":"…"}` and lasts only for this session. Web-service logins never access the +computer's credential store. Editing the origin, method, user or header name ends the login; +renaming the connection or changing its path at the same origin does not. **Log Out** +(`connection.logout`) removes the session login. Log in again when a new session needs it. + +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. A redirect to another origin is +followed without it (the browser doesn't follow it), and never gets another connection's login +either. A plain `http://` address sends the login unencrypted, so the form asks for confirmation +and the command needs `"allowInsecure": true`. Error messages, notices, the preferences, +projects, History and what the [AI Assistant](assistant.md) sees never hold the password or key. + +A project's layers use the connection they record; layers recording none use an explicit session +choice or the only saved connection for their origin. Different connections and sessions can use +different logins at the same origin without sharing credentials or authenticated cached responses. +When several saved connections match the address, RustGIS asks which one to use (**Choose +Connection…**, `connection.login {id}` for the one chosen) and never picks one itself. +A layer that has no login yet asks for one, and a layer whose recorded connection isn't on this computer offers +to make one for its address. `connection.list` reports these as `loginRequests` with the `origin`. +CLI and Python use the same session-only `connection.login` commands. ArcGIS sign-in and tokens +keep their own [ArcGIS sign-in](#web-data). A login ends with the session that made it. +Unbound fetches never adopt another session's service login. + ## Large files GeoPackage, GeoParquet, FlatGeobuf and (on the desktop) Shapefile files with more than 100,000 @@ -1074,6 +1104,10 @@ whose bytes exactly match one of those snapshots restores its secrets; saving mo snapshots evicts the oldest. Changed files, packages, imports and files saved by another process never restore secrets. After a snapshot is evicted or RustGIS closes, add those layers again with their credentials. +A layer added from a saved web-service connection records the connection's id (`"connection"` +in the `.rgis` file; never its login), so the project needs no token in its addresses. See +[Web-service logins](#web-service-logins) for what happens when it opens where that connection or +its login is missing. ### Complete streamed Shapefile rendering From ea5f2b0243f341e1263bb0f5d83045ed77662422 Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Mon, 2 Nov 2026 12:54:44 +0100 Subject: [PATCH 05/19] fix(connections): public connections never ask which to use; logging in again keeps the binding Several saved connections at one origin ask which to use only when one of them has a login: public ones all read the same anonymously. A blank connection.login on a connection already logged in chooses it again without minting a new binding, so its layers and cached tiles stay. The web app no longer suppresses ArcGIS tokens on login requests (ArcGIS is out of scope; native was already reverted). --- apps/rustgis-web/src/web.rs | 12 ++---------- crates/engine/src/cmd/connections.rs | 5 +++++ crates/engine/src/service_logins.rs | 21 +++++++++++++++------ crates/engine/src/tests/service_logins.rs | 18 ++++++++++++++++++ 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/apps/rustgis-web/src/web.rs b/apps/rustgis-web/src/web.rs index 05e11c8234..6800316334 100644 --- a/apps/rustgis-web/src/web.rs +++ b/apps/rustgis-web/src/web.rs @@ -594,16 +594,8 @@ async fn fetch_bytes(url: &str) -> Result, String> { let target = logins::inherit(bound, url); let login = if prepared.signed { None } else { logins::apply(&target).map_err(str::to_string)? }; let login_url = login.as_ref().map_or(url, |applied| applied.url.as_str()); - // An explicit login overrides ArcGIS's equivalent built-in credential on this hop. - let overrides_arcgis = login - .as_ref() - .and_then(|applied| applied.header.as_ref()) - .is_some_and(|(name, _)| name.eq_ignore_ascii_case("Authorization") || name.eq_ignore_ascii_case("X-Esri-Authorization")); - let arcgis = if prepared.signed || overrides_arcgis { - None - } else { - rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::arcgis_auth::with_token(login_url) - }; + let arcgis = + if prepared.signed { None } else { rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::arcgis_auth::with_token(login_url) }; // Only the request carries credentials: messages and cache keys keep the original address. let request = arcgis.as_deref().unwrap_or(login_url); let result = async { diff --git a/crates/engine/src/cmd/connections.rs b/crates/engine/src/cmd/connections.rs index 68337a4a12..c3a1c4eb6c 100644 --- a/crates/engine/src/cmd/connections.rs +++ b/crates/engine/src/cmd/connections.rs @@ -858,6 +858,11 @@ fn service_login(s: &mut Session, c: &Connection, p: &Value, remember: bool) -> }; let origin = crate::connections::login_origin(c).ok_or(web::NO_ORIGIN)?; let Some(typed) = typed else { + // Already logged in: choose it again, keeping its binding and its cached tiles. + if web::logged_in(s, c) { + web::reselect(s, c, &origin); + return Ok(Reopened::default()); + } // Only the active login given this session can be reused. let secret = web::known_secret(s, c)?; web::activate(s, c, Some(&secret), true)?; diff --git a/crates/engine/src/service_logins.rs b/crates/engine/src/service_logins.rs index 5f0f339752..6bf78323b5 100644 --- a/crates/engine/src/service_logins.rs +++ b/crates/engine/src/service_logins.rs @@ -181,9 +181,7 @@ pub(crate) fn activate(s: &mut Session, c: &Connection, secret: Option<&Secret>, if let Some(next) = next { s.web_logins.active.insert(c.id.clone(), next); } - s.database_logins.select(WEB, &origin, &c.id); - s.database_logins.clear_requests_for(WEB, &origin); - rebind_connection(s, c); + reselect(s, c, &origin); // Only a login that was replaced or removed leaves cached authenticated responses to drop; // a first login, or none, changes no cached tile. if replaced { @@ -192,6 +190,14 @@ pub(crate) fn activate(s: &mut Session, c: &Connection, secret: Option<&Secret>, Ok(()) } +/// Chooses `c` for its origin again and rebinds its layers, keeping its live binding (and so +/// its cached tiles): logging in again to a connection that is already logged in. +pub(crate) fn reselect(s: &mut Session, c: &Connection, origin: &str) { + s.database_logins.select(WEB, origin, &c.id); + s.database_logins.clear_requests_for(WEB, origin); + rebind_connection(s, c); +} + pub(crate) fn deactivate(s: &mut Session, c: &Connection, _origin: Option) { if let Some(old) = s.web_logins.active.remove(&c.id) { logins::deactivate(&old.binding); @@ -260,7 +266,9 @@ pub fn request(s: &mut Session, connection: Option<&str>, url: &str) -> Result 1 { + // Several connections, at least one with a login: the user picks. Public connections + // alone all send the same anonymous request, so there is nothing to ask. + if candidates.len() > 1 && candidates.iter().any(|c| !c.auth.is_empty()) { s.database_logins.request(LoginRequest { reason: LoginReason::Choose, kind: WEB, @@ -522,8 +530,9 @@ pub(crate) fn resolve(s: &mut Session, only: Option<&BTreeSet>) { prompted: false, }; let Some(c) = chosen else { - if candidates.len() > 1 || recorded.len() > 1 { - // Several connections for the address, and none of them clear: the user picks. + if (candidates.len() > 1 || recorded.len() > 1) && candidates.iter().any(|c| !c.auth.is_empty()) { + // Several connections for the address, at least one with a login, and none of them + // clear: the user picks. Public ones alone read the same anonymously. let ids: Vec = if recorded.len() > 1 { recorded.iter().map(|c| c.id.clone()).collect() } else { diff --git a/crates/engine/src/tests/service_logins.rs b/crates/engine/src/tests/service_logins.rs index 597e343e37..c70b52a9f9 100644 --- a/crates/engine/src/tests/service_logins.rs +++ b/crates/engine/src/tests/service_logins.rs @@ -236,6 +236,19 @@ fn recorded_connections_at_one_origin_remain_independent_and_unassigned_layers_a assert!(header(&web::request(&mut s, None, &format!("{origin}/wms")).unwrap()).is_none()); } +#[test] +fn public_connections_at_one_origin_never_ask_which_to_use() { + // Two public connections of one server: any choice reads the same anonymously. + let origin = "https://public757.example.org"; + let mut s = session(None); + new(&mut s, json!({"kind":"wms","name":"Public WMS","address":format!("{origin}/wms")})); + new(&mut s, json!({"kind":"wfs","name":"Public WFS","address":format!("{origin}/wfs")})); + s.run("map.addTiles", &json!({"url":format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png"),"name":"Bare"})).unwrap(); + assert_eq!(requests(&mut s), json!([])); + s.run("project.openText", &json!({"text":project(origin, "")})).unwrap(); + assert_eq!(requests(&mut s), json!([])); +} + #[test] fn test_refuses_changed_endpoint_and_missing_login_instead_of_testing_anonymously() { let mut s = session(None); @@ -339,6 +352,11 @@ fn logins_drop_the_tile_cache_only_when_one_ends_or_is_replaced() { s.run("connection.login", &json!({"id":public})).unwrap(); s.run("connection.login", &json!({"id":private,"apiKey":"cache-sentinel-757"})).unwrap(); assert_eq!(web::cache_clears(), start); + // Logging in again without typing (choosing it again) keeps the binding and the cache. + let first = bound(&mut s, &private, "https://cache-private757.example.org/wms"); + s.run("connection.login", &json!({"id":private})).unwrap(); + assert_eq!(bound(&mut s, &private, "https://cache-private757.example.org/wms"), first); + assert_eq!(web::cache_clears(), start); // Replacing or ending a login leaves authenticated responses behind: those go. s.run("connection.login", &json!({"id":private,"apiKey":"replacement-sentinel-757"})).unwrap(); assert_eq!(web::cache_clears(), start + 1); From 7c65e05be99c41f10927d0752b9d5b7a35b90d8d Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Mon, 2 Nov 2026 13:55:11 +0100 Subject: [PATCH 06/19] test(tiles): two tests wait for cached state that other tests may clear cache::clear() drops every cached document and PMTiles header, and tests running at once call it (logins::deactivate among them). The derived-style test now stores its documents again until the style resolves, and the vector-tile pick test waits for its archive to be read again, as its earlier pick already did. Fixes the test / Test (ubuntu-latest) failure in draw::tests::picks_the_vector_tile_features_under_a_point. --- crates/tiles/src/cache.rs | 19 ++++++++++++++++--- crates/tiles/src/draw.rs | 23 ++++++++++++++++++++--- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index 51165e76ab..b22011c6f1 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -1899,10 +1899,12 @@ mod tests { logins::activate(&base, "derived-style", Method::Header("X-Service-Key".into()), "derived-style-secret", true).unwrap(); let url = logins::bind(&binding, &format!("{base}/style.json")).unwrap(); let tilejson_url = template::join(&url, "./tiles.json"); + // The cache is shared with tests running at once, and any of them may clear it: store the + // fetched documents again on each try until the style resolves from them. + let mut documents = Vec::new(); for address in [&url, &tilejson_url] { let bytes = native::get(&native::agent(), address).unwrap(); - let json = serde_json::from_slice(&bytes).unwrap(); - store(address, Entry::Ready(Arc::new(Payload::Json(json)), bytes.len())); + documents.push((address.clone(), serde_json::from_slice::(&bytes).unwrap(), bytes.len())); } let spec = source::Spec { kind: source::Kind::Style, @@ -1917,7 +1919,18 @@ mod tests { style_layers: String::new(), hidden_layers: vec![], }; - let source::Resolve::Ready(resolved) = source::resolve(&spec) else { panic!("style must resolve from fetched documents") }; + let mut resolved = None; + for _ in 0..200 { + for (address, json, len) in &documents { + store(address, Entry::Ready(Arc::new(Payload::Json(json.clone())), *len)); + } + if let source::Resolve::Ready(r) = source::resolve(&spec) { + resolved = Some(r); + break; + } + std::thread::sleep(std::time::Duration::from_millis(5)); + } + let resolved = resolved.expect("style must resolve from fetched documents"); let Source::Vector { tiles, .. } = &resolved.style.sources[0].1 else { panic!("vector source") }; let tile = template::expand(&tiles[0], template::TileId { z: 0, x: 0, y: 0 }, 256, false, &[]); native::get(&native::agent(), &tile).unwrap(); diff --git a/crates/tiles/src/draw.rs b/crates/tiles/src/draw.rs index 187673367a..d6e0d173b0 100644 --- a/crates/tiles/src/draw.rs +++ b/crates/tiles/src/draw.rs @@ -1292,11 +1292,28 @@ pub(crate) mod tests { std::thread::sleep(std::time::Duration::from_millis(5)); } } - // A hidden source layer is left out of the style: not picked, the others still are. - assert_eq!(crate::source::source_layers(spec.kind, &spec.url).unwrap(), ["parks", "roads", "pois", "plots"]); + // A hidden source layer is left out of the style: not picked, the others still are. Other + // tests running at once may clear the cache, so wait for the archive to be read again. + let mut layers = None; + for _ in 0..500 { + layers = crate::source::source_layers(spec.kind, &spec.url); + if layers.is_some() { + break; + } + std::thread::sleep(std::time::Duration::from_millis(10)); + } + assert_eq!(layers.unwrap(), ["parks", "roads", "pois", "plots"]); let without_roads = Spec { hidden_layers: vec!["roads".into()], ..spec.clone() }; let (x, y) = at(2048.0, 2048.0); - assert_eq!(pick(&without_roads, x, y, m_per_px, 3.0, 10).len(), 1, "the park is still there"); + let mut park = Vec::new(); + for _ in 0..500 { + park = pick(&without_roads, x, y, m_per_px, 3.0, 10); + if !park.is_empty() { + break; + } + std::thread::sleep(std::time::Duration::from_millis(10)); + } + assert_eq!(park.len(), 1, "the park is still there"); let (x, y) = at(200.0, 505.0); assert!(pick(&without_roads, x, y, m_per_px, 3.0, 10).is_empty(), "the road isn't"); // An own style's layers too. From 452eacd25f94881f28cd725d935f9ff99ec1079f Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Mon, 2 Nov 2026 14:51:33 +0100 Subject: [PATCH 07/19] fix(connections): review fixes for anonymous reads, blank connections, header keys and the save count - An anonymous read (a plugin's command) no longer infers a service group's recorded connection, so it never sends that login. - connection.test puts the anonymous-read setting back even when the test panics. - A blank "connection" is no connection everywhere, as request_params already read it. - An API key sent in a header must be ASCII: the browser sends header bytes as Latin-1 and refuses other characters, the desktop sends UTF-8. - Saving counts a login-bound layer that has no connection and holds a token once, not twice. --- crates/engine/src/cmd/connections.rs | 6 +- crates/engine/src/cmd/service_group.rs | 3 +- crates/engine/src/project.rs | 12 +++- crates/engine/src/service_logins.rs | 24 ++++++++ crates/engine/src/tests/service_logins.rs | 71 +++++++++++++++++++++++ crates/ui-egui/src/connectionui.rs | 1 + 6 files changed, 109 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/cmd/connections.rs b/crates/engine/src/cmd/connections.rs index c3a1c4eb6c..ddcaace578 100644 --- a/crates/engine/src/cmd/connections.rs +++ b/crates/engine/src/cmd/connections.rs @@ -963,10 +963,8 @@ fn answered(s: &mut Session, c: &Connection, text: &str) -> CmdResult { fn test(s: &mut Session, p: &Value) -> CmdResult { let c = tested(s, p)?; // An unsaved web service reads anonymously, whatever saved connection shares its origin. - let was = s.web_logins.set_anonymous(web::is_service(&c) && c.id.is_empty()); - let out = test_connection(s, p, c); - s.web_logins.set_anonymous(was); - out + let anonymous = web::is_service(&c) && c.id.is_empty(); + web::anonymously(s, anonymous, |s| test_connection(s, p, c)).unwrap_or_else(|_| Err("connection.test failed unexpectedly".into())) } fn test_connection(s: &mut Session, p: &Value, c: Connection) -> CmdResult { diff --git a/crates/engine/src/cmd/service_group.rs b/crates/engine/src/cmd/service_group.rs index 386083c382..52a5f12af4 100644 --- a/crates/engine/src/cmd/service_group.rs +++ b/crates/engine/src/cmd/service_group.rs @@ -42,7 +42,8 @@ fn target(s: &Session, p: &Value) -> Result)>, fn request(s: &mut Session, p: &Value) -> CmdResult { let mut q = p.clone(); if let Some((group, child)) = target(s, p)? { - if q.get("connection").is_none_or(Value::is_null) { + // Anonymous reads (a plugin's command, an unsaved Test) never borrow the group's login. + if !s.web_logins.is_anonymous() && q.get("connection").is_none_or(Value::is_null) { let mut ids = group .children .iter() diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 3fcd561c9a..ba118defa2 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -601,13 +601,15 @@ impl Project { } /// How many layers and tile layers read through a login binding but record no connection: - /// nothing would tell the opened project which login they need. + /// nothing would tell the opened project which login they need. One whose address also holds + /// a token isn't counted here: [`lost_secret`] counts it already. fn unrecorded_bound(&mut self) -> usize { + let only_bound = |a: &str| without_login_binding(a).is_some_and(|public| saved_address(&public).is_none()); let mut n = 0; for map in self.maps.iter_mut().chain(self.offline_areas.iter_mut().map(|area| &mut area.data)) { - n += map.tiles.iter().filter(|t| t.connection.is_empty() && without_login_binding(&t.url).is_some()).count(); + n += map.tiles.iter().filter(|t| t.connection.is_empty() && only_bound(&t.url)).count(); for layer in map.layers.iter_mut().filter(|l| l.connection.is_empty()) { - n += usize::from(layer.addresses_mut().any(|a| without_login_binding(a).is_some())); + n += usize::from(layer.addresses_mut().any(|a| only_bound(a))); } } n @@ -1166,5 +1168,9 @@ mod login_binding_tests { project.map_mut().tiles.clear(); project.map_mut().tiles.push(tiles("Both", &bound(&format!("{plain}?token=abc123")), "saved-id")); assert_eq!(project.saved_text(None).unwrap().1, 1, "the token under a binding is lost too"); + + project.map_mut().tiles.clear(); + project.map_mut().tiles.push(tiles("Unrecorded token", &bound(&format!("{plain}?token=abc123")), "")); + assert_eq!(project.saved_text(None).unwrap().1, 1, "a bound layer with no connection and a token is one layer"); } } diff --git a/crates/engine/src/service_logins.rs b/crates/engine/src/service_logins.rs index 6bf78323b5..c8251e7ae6 100644 --- a/crates/engine/src/service_logins.rs +++ b/crates/engine/src/service_logins.rs @@ -30,6 +30,8 @@ pub const SESSION_ONLY: &str = "Web-service logins last for this session only."; pub const INSECURE: &str = "This connection's address uses plain http://, so its login would be sent unencrypted. Confirm with allowInsecure: true to send it anyway."; /// A password or key that can't be sent. pub const BAD_SECRET: &str = "The password or key must be at most 4096 bytes, without control characters."; +/// An API key a header can't carry the same way everywhere (the browser sends Latin-1 only). +pub const HEADER_ASCII: &str = "An API key sent in a header must use plain ASCII characters."; /// A login asked of a connection with nothing to log in with this session. pub const REQUIRED: &str = "This connection's login is required: give its password (basic) or apiKey (header)."; /// A login for a definition that isn't saved. @@ -83,6 +85,11 @@ pub fn check_login_secret(c: &Connection, secret: &str) -> Result<(), String> { if c.auth == "basic" && !logins::valid_basic(&c.user, secret) { return Err("The encoded Basic login exceeds the supported header size or contains invalid characters.".into()); } + // The browser's fetch sends a header byte per character (Latin-1) and refuses characters past + // U+00FF, while the desktop sends UTF-8: only ASCII reaches a server the same way from both. + if c.auth == "header" && !secret.is_ascii() { + return Err(HEADER_ASCII.into()); + } Ok(()) } @@ -124,6 +131,11 @@ impl SessionLogins { std::mem::replace(&mut self.anonymous, on) } + /// Whether [`SessionLogins::anonymous`] reads are running: nothing may pick a login for them. + pub(crate) fn is_anonymous(&self) -> bool { + self.anonymous + } + fn owner(&self, id: &str) -> String { format!("{}/{id}", self.session) } @@ -135,6 +147,16 @@ impl Default for SessionLogins { } } +/// Runs `f` with anonymous reads `on` (or off), and puts the earlier setting back even when `f` +/// panics: `Session::run` catches the panic and the session goes on, so a setting left behind +/// would silently send later requests without their login. +pub(crate) fn anonymously(s: &mut Session, on: bool, f: impl FnOnce(&mut Session) -> T) -> std::thread::Result { + let was = s.web_logins.set_anonymous(on); + let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(s))); + s.web_logins.set_anonymous(was); + out +} + impl Drop for SessionLogins { fn drop(&mut self) { let mut ended = false; @@ -655,7 +677,9 @@ pub(crate) struct Before { /// web-service connection, by id. fn named(s: &Session, params: &Value) -> Result, String> { match params.get("connection") { + // Blank is no connection, as `request_params` reads it. None | Some(Value::Null) => Ok(None), + Some(Value::String(id)) if id.trim().is_empty() => Ok(None), Some(Value::String(id)) => { let c = s .connections diff --git a/crates/engine/src/tests/service_logins.rs b/crates/engine/src/tests/service_logins.rs index c70b52a9f9..8b74269aa7 100644 --- a/crates/engine/src/tests/service_logins.rs +++ b/crates/engine/src/tests/service_logins.rs @@ -325,6 +325,77 @@ fn a_group_child_read_in_place_is_counted_once_and_refresh_respects_logout() { assert_eq!(s.run("connection.list", &Value::Null).unwrap()["connections"][0]["loggedIn"], false); } +#[test] +fn anonymous_reads_never_borrow_a_groups_recorded_login() { + let origin = "https://anon-group757.example.org"; + let mut s = session(None); + let id = new(&mut s, json!({"kind":"ogcapi","address":format!("{origin}/ogc"),"auth":"header","keyName":"X-Key"})); + let mut layer = rustgis_map::Layer::new(42, rustgis_data::Dataset::new("Roads", rustgis_data::GeometryType::None, Vec::new())); + layer.connection = id.clone(); + s.project.map_mut().layers.push(layer); + s.project.map_mut().service_groups.push(rustgis_map::ServiceGroup { + id: 6, + name: "Imported".into(), + url: format!("{origin}/ogc/collections"), + kind: "ogcapi".into(), + expanded: true, + children: vec![rustgis_map::ServiceChild { + layer: 42, + collection: "roads".into(), + source_url: format!("{origin}/ogc/collections/roads"), + }], + }); + s.run("connection.login", &json!({"id":id,"apiKey":"anon-group-sentinel-757"})).unwrap(); + let plan = |s: &mut Session| s.run("service.groupPlan", &json!({"group":6,"sourceOnly":true})).unwrap(); + // The session's own plan reads the group with the connection its layers record... + assert_eq!(header(plan(&mut s)["url"].as_str().unwrap()).unwrap().1, "anon-group-sentinel-757"); + // ...but an anonymous read (a plugin's command) never infers it. + let was = s.web_logins.set_anonymous(true); + let anonymous = plan(&mut s); + s.web_logins.set_anonymous(was); + assert!(header(anonymous["url"].as_str().unwrap()).is_none(), "{anonymous}"); + assert!(!anonymous.to_string().contains("anon-group-sentinel-757")); +} + +#[test] +fn a_panic_during_an_anonymous_read_puts_the_logins_back() { + let mut s = session(None); + let out: std::thread::Result<()> = web::anonymously(&mut s, true, |s| { + assert!(s.web_logins.is_anonymous()); + panic!("a command failed unexpectedly") + }); + assert!(out.is_err()); + assert!(!s.web_logins.is_anonymous(), "later requests must carry their logins again"); +} + +#[test] +fn a_blank_connection_is_no_connection() { + let mut s = session(None); + for (name, connection) in [("Without", None), ("Blank", Some("")), ("Spaces", Some(" "))] { + let mut p = json!({"url":"https://blank757.example.org/tiles/{z}/{x}/{y}.png","name":name}); + if let Some(c) = connection { + p["connection"] = json!(c); + } + s.run("map.addTiles", &p).unwrap(); + } + assert!(s.map().tiles.iter().all(|t| t.connection.is_empty())); +} + +#[test] +fn header_keys_are_plain_ascii_basic_passwords_need_not_be() { + let mut s = session(None); + let key = new(&mut s, json!({"kind":"wms","address":"https://ascii757.example.org/wms","auth":"header","keyName":"X-Key"})); + for typed in ["café-757", "€757"] { + let e = s.run("connection.login", &json!({"id":key,"apiKey":typed})).unwrap_err(); + assert_eq!(e, web::HEADER_ASCII); + assert!(!e.contains(typed)); + } + s.run("connection.login", &json!({"id":key,"apiKey":"plain-ascii-757"})).unwrap(); + // Basic logins are base64 of UTF-8, built the same way on every platform. + let basic = new(&mut s, json!({"kind":"wms","address":"https://ascii-basic757.example.org/wms","auth":"basic","user":"ana"})); + s.run("connection.login", &json!({"id":basic,"password":"café-757"})).unwrap(); +} + #[test] fn a_definition_reloaded_with_another_identity_cannot_reuse_a_session_secret() { let mut s = session(None); diff --git a/crates/ui-egui/src/connectionui.rs b/crates/ui-egui/src/connectionui.rs index 4293746e7f..9ab5c18c68 100644 --- a/crates/ui-egui/src/connectionui.rs +++ b/crates/ui-egui/src/connectionui.rs @@ -81,6 +81,7 @@ pub(crate) fn translated(e: &str) -> String { web::SESSION_ONLY => tl!("Web-service logins last for this session only."), web::INSECURE => tl!("This connection uses unencrypted http://. Tick the confirmation before sending its password or key."), web::BAD_SECRET => tl!("The password or key must fit a 4096-byte HTTP header, without control characters."), + web::HEADER_ASCII => tl!("An API key sent in a header must use plain ASCII characters."), web::REQUIRED => tl!("Enter this connection's password or API key to log in."), web::UNSAVED => tl!("Save the connection first: a login belongs to a saved connection."), web::NO_ORIGIN => tl!("The connection's address has no http:// or https:// origin a login can go to."), From 954ea7c8ad3647f96a165fae0de2efbcfe66e85e Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Mon, 2 Nov 2026 14:51:33 +0100 Subject: [PATCH 08/19] docs(data): logged-in requests aren't redirected in the web app --- docs/user-guide/data.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index 1833c36eb6..f56d285b15 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -764,9 +764,12 @@ renaming the connection or changing its path at the same origin does not. **Log (`connection.logout`) removes the session login. Log in again when a new session needs it. 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. A redirect to another origin is -followed without it (the browser doesn't follow it), and never gets another connection's login -either. A plain `http://` address sends the login unencrypted, so the form asks for confirmation +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 +another connection's login either. In the web app a request carrying a login isn't redirected at +all, since the browser can't take the login off on the way: a service that redirects (to +`https://` or to another path) fails there, so save the connection with the address it ends at. +A plain `http://` address sends the login unencrypted, so the form asks for confirmation and the command needs `"allowInsecure": true`. Error messages, notices, the preferences, projects, History and what the [AI Assistant](assistant.md) sees never hold the password or key. From f86c1235a5e0e7e0d6e1a346c1e21377b0e25e63 Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Mon, 2 Nov 2026 19:22:32 +0100 Subject: [PATCH 09/19] fix(connections): second-round review fixes - A streamed layer that failed opening before its connection logged in reopens on its rebound address (its error is cleared). - Following service pages back to the login origin through another origin stays anonymous (the referring page is the base). - Add as Time Series sends the picked connection. - The connection form's Test pre-check compares the address as the engine does, so a typed token blocks it up front. - map.setTiles binds a plain same-origin legend to the tile's logged-in connection. - A refresh with an explicit connection records it on existing children too. - Content-Type and Accept can't be a key header (the app sets them itself). - Command results the assistant and MCP clients see carry plain addresses, not login bindings. --- crates/assistant/src/tools.rs | 75 ++++++++++++++++++++-- crates/engine/src/cmd/service_group.rs | 20 ++++++ crates/engine/src/cmd/tiles.rs | 9 ++- crates/engine/src/service_logins.rs | 20 +++++- crates/engine/src/tests/service_logins.rs | 40 ++++++++++++ crates/tiles/src/logins.rs | 4 ++ crates/ui-egui/src/connectionui.rs | 2 +- crates/ui-egui/src/serviceui.rs | 45 ++++++++++++- crates/ui-egui/src/stacui.rs | 57 +++++++++++----- crates/ui-egui/tests/service_logins/mod.rs | 12 ++++ 10 files changed, 259 insertions(+), 25 deletions(-) diff --git a/crates/assistant/src/tools.rs b/crates/assistant/src/tools.rs index 6a995e4c84..c15680186b 100644 --- a/crates/assistant/src/tools.rs +++ b/crates/assistant/src/tools.rs @@ -278,13 +278,15 @@ pub fn run_with_budget(s: &mut Session, call: &ToolCall, tokens: usize) -> Resul return Err(format!("unknown command '{id}'; find ids with search_commands")); } let (before, mark) = (layer_snap(s), s.notice_mark()); - let r = s.run(&id, ¶ms_arg(a)?).map_err(|e| error_with_notices(e, s, mark))?; + let r = s.run(&id, ¶ms_arg(a)?).map_err(|e| public_text(error_with_notices(e, s, mark)))?; with_notices(with_new_layers(r, s, &before), s, mark) } "run_tool" => { let tool = str_arg(a, "tool").ok_or("tool is required")?; let (before, mark) = (layer_snap(s), s.notice_mark()); - let r = s.run("gp.run", &json!({"tool": tool, "params": params_arg(a)?})).map_err(|e| error_with_notices(e, s, mark))?; + let r = s + .run("gp.run", &json!({"tool": tool, "params": params_arg(a)?})) + .map_err(|e| public_text(error_with_notices(e, s, mark)))?; with_notices(with_new_layers(r, s, &before), s, mark) } "read_features" => read_features(s, a)?, @@ -310,7 +312,32 @@ pub fn run_with_budget(s: &mut Session, call: &ToolCall, tokens: usize) -> Resul "test_panic" => panic!("a test tool panicked"), other => return Err(format!("unknown tool '{other}'")), }; - Ok(bounded_json(&v, tokens)) + Ok(bounded_json(&public_addresses(v, 0), tokens)) +} + +/// How deep a result is searched for runtime login addresses; what is deeper is withheld. +const MAX_DEPTH: usize = 16; + +/// `text` with every runtime login wrapper (`rustgis-login:…`, alone or after a prefix such as +/// `pmtiles:…@`) replaced by its plain address. Commands return the bound addresses the app needs +/// to use a connection's login; the model and MCP clients get only the public ones. +fn public_text(text: String) -> String { + if text.contains(rustgis_engine::data::login_address::PREFIX) { + rustgis_engine::render::rustgis_tiles::logins::scrub(&text) + } else { + text + } +} + +/// `v` with its strings made public ([`public_text`]), to a depth of [`MAX_DEPTH`]. +fn public_addresses(v: Value, depth: usize) -> Value { + match v { + Value::String(s) => Value::String(public_text(s)), + Value::Array(a) if depth < MAX_DEPTH => Value::Array(a.into_iter().map(|x| public_addresses(x, depth + 1)).collect()), + Value::Object(o) if depth < MAX_DEPTH => Value::Object(o.into_iter().map(|(k, x)| (k, public_addresses(x, depth + 1))).collect()), + Value::Array(_) | Value::Object(_) => json!("[too deep]"), + other => other, + } } /// Serialize complete JSON. Oversized results become an explicit text preview envelope; @@ -463,14 +490,14 @@ fn run_script(s: &mut Session, a: &Value, tokens: usize) -> Result { // Nothing was kept, so no line ran and no layer was added. let said: Vec<&&str> = notices.iter().filter(|t| **t != e).collect(); let out = json!({"success": false, "result": null, "lines": 0, "layersAdded": [], "notices": said, "error": clip(&e, 8000), "duration_s": secs}); - Err(bounded_json(&out, tokens)) + Err(bounded_json(&public_addresses(out, 0), tokens)) } } } @@ -810,3 +837,41 @@ pub fn context(s: &Session) -> String { } out } + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod tests { + use super::*; + use rustgis_engine::render::rustgis_tiles::logins; + + #[test] + fn results_carry_plain_addresses_not_login_wrappers() { + let origin = "https://assistant-login.example"; + let binding = logins::activate(origin, "assistant", logins::Method::Header("X-Key".into()), "assistant-secret", false).unwrap(); + let tile = logins::bind(&binding, &format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png")).unwrap(); + let archive = logins::bind(&binding, &format!("{origin}/a.pmtiles")).unwrap(); + assert!(tile.starts_with("rustgis-login:")); + let result = json!({ + "fetch": tile, + "layers": [{"source_url": archive, "next": {"request": {"url": format!("pmtiles:x@{archive}")}}}], + "count": 2, + }); + let text = bounded_json(&public_addresses(result, 0), DEFAULT_OUTPUT_TOKENS); + assert!(!text.contains("rustgis-login:") && !text.contains(&binding), "{text}"); + let v: Value = serde_json::from_str(&text).unwrap(); + assert_eq!(v["fetch"], format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png")); + assert_eq!(v["layers"][0]["source_url"], format!("{origin}/a.pmtiles")); + assert_eq!(v["layers"][0]["next"]["request"]["url"], format!("pmtiles:x@{origin}/a.pmtiles")); + assert_eq!(v["count"], 2); + assert!(logins::deactivate(&binding)); + } + + #[test] + fn too_deep_results_are_withheld_not_searched_forever() { + let mut v = json!("rustgis-login:leak"); + for _ in 0..40 { + v = json!([v]); + } + assert!(!public_addresses(v, 0).to_string().contains("rustgis-login:")); + } +} diff --git a/crates/engine/src/cmd/service_group.rs b/crates/engine/src/cmd/service_group.rs index 52a5f12af4..a5f350327b 100644 --- a/crates/engine/src/cmd/service_group.rs +++ b/crates/engine/src/cmd/service_group.rs @@ -365,6 +365,9 @@ fn refresh(s: &mut Session, p: &Value) -> CmdResult { if let Some(lock) = &mut s.map_mut().label_lock { lock.retain(|((id, _, _, _), _)| *id != child.layer); } + if let Some(request) = listing.get("request") { + record_connection(s, child.layer, request); + } } else { child.layer = crate::cmd::layer::add_dataset(s, dataset); if let Some(request) = listing.get("request") { @@ -503,6 +506,23 @@ mod tests { } } + #[test] + fn refresh_with_an_explicit_connection_records_it_on_existing_children() { + let mut s = Session::new(crate::Project::default()); + let url = "https://group-none757.example.org/collections"; + let mut p = json!({"url": url, "type": "ogcapi", "text": document(&["old"]).to_string()}); + p["responses"] = answers(&mut s, &p, &features()); + let imported = s.run("service.import", &p).unwrap(); + let old = imported["layers"][0].as_u64().unwrap() as u32; + assert_eq!(s.map().layer(old).unwrap().connection, ""); + let definition = json!({"kind": "ogcapi", "name": "Public", "auth": "none", "address": url}); + let connection = s.run("connection.new", &definition).unwrap()["id"].as_str().unwrap().to_string(); + let mut q = json!({"url": url, "group": imported["group"], "connection": connection, "text": document(&["old"]).to_string()}); + q["responses"] = answers(&mut s, &q, &features()); + s.run("service.refresh", &q).unwrap(); + assert_eq!(s.map().layer(old).unwrap().connection, connection); + } + #[test] fn an_unrelated_explicit_connection_is_not_attributed_to_imported_children() { let mut s = Session::new(crate::Project::default()); diff --git a/crates/engine/src/cmd/tiles.rs b/crates/engine/src/cmd/tiles.rs index 5730ff9823..1b7605d696 100644 --- a/crates/engine/src/cmd/tiles.rs +++ b/crates/engine/src/cmd/tiles.rs @@ -633,11 +633,18 @@ fn set(s: &mut Session, p: &Value) -> CmdResult { let i = find(s, p)?.ok_or("name or index is required")?; let mode = blend(p)?; let look = cog_look(p)?; - let new_legends = match p.get("legend") { + let mut new_legends = match p.get("legend") { None => None, Some(Value::Null) => Some(Vec::new()), Some(v) => Some(legends(s, None, v)?), }; + // A plain legend address of a layer that recorded a logged-in connection reads through it, + // as the layer's own address does. + if let Some(connection) = s.map().tiles.get(i).map(|t| t.connection.clone()).filter(|c| !c.is_empty()) { + for legend in new_legends.iter_mut().flatten() { + legend.url = crate::service_logins::bind_logged_in(s, &connection, &legend.url); + } + } let hidden = hidden_layers(p)?; let old_name = s.map().tiles.get(i).map(|t| t.name.clone()); let m = s.map_mut(); diff --git a/crates/engine/src/service_logins.rs b/crates/engine/src/service_logins.rs index c8251e7ae6..c78c85226d 100644 --- a/crates/engine/src/service_logins.rs +++ b/crates/engine/src/service_logins.rs @@ -245,6 +245,17 @@ pub fn logged_in(s: &Session, c: &Connection) -> bool { s.web_logins.active.get(&c.id).is_some_and(|a| a.matches(c) && logins::is_active(&a.binding)) } +/// A plain address at the origin of a logged-in service connection, bound to that login. Anything +/// else (another origin, a wrapper, a connection not logged in) comes back unchanged. +pub(crate) fn bind_logged_in(s: &Session, connection: &str, url: &str) -> String { + let Some(c) = s.connections.connections.iter().find(|c| c.id == connection && is_service(c)) else { return url.to_string() }; + let origin = logins::origin(url); + if !logged_in(s, c) || origin.is_none() || login_origin(c) != origin || rustgis_data::login_address::is_wrapper(url) { + return url.to_string(); + } + s.web_logins.active.get(&c.id).and_then(|a| logins::bind(&a.binding, url).ok()).unwrap_or_else(|| url.to_string()) +} + pub fn known_secret(s: &Session, c: &Connection) -> Result { let Some(secret) = s.database_logins.session_password(&c.id) else { return Err(REQUIRED.into()); @@ -408,8 +419,15 @@ fn rebind_connection(s: &mut Session, c: &Connection) { } } for l in m.layers.iter_mut().filter(|l| l.connection == c.id) { - if let Some(path) = l.source.as_mut().and_then(|source| source.path.as_mut()) { + if let Some(source) = l.source.as_mut() + && let Some(path) = source.path.as_mut() + { + let before = path.clone(); rewrite(path); + // A failed open is kept, not retried; the changed address deserves a new try. + if *path != before { + source.error = None; + } } if let Some(path) = l.raster.as_mut().and_then(|raster| raster.path.as_mut()) { rewrite(path); diff --git a/crates/engine/src/tests/service_logins.rs b/crates/engine/src/tests/service_logins.rs index 8b74269aa7..75052c46da 100644 --- a/crates/engine/src/tests/service_logins.rs +++ b/crates/engine/src/tests/service_logins.rs @@ -528,3 +528,43 @@ fn only_web_services_compare_a_test_with_their_saved_definition() { .unwrap(); assert_eq!(out["connection"], db); } + +#[test] +fn a_streamed_layer_that_failed_before_the_login_reopens_on_its_new_address() { + let origin = "https://stream757.example.org"; + let mut s = session(None); + let id = new(&mut s, json!({"kind":"ogcapi","address":format!("{origin}/ogc"),"auth":"header","keyName":"X-Key"})); + let failed = |n: u32, path: String| { + let mut layer = rustgis_map::Layer::new(n, rustgis_data::Dataset::new("Roads", rustgis_data::GeometryType::None, Vec::new())); + layer.connection = id.clone(); + let mut source: rustgis_map::source::SourceLayer = serde_json::from_value(json!({ "path": path })).unwrap(); + source.error = Some("the server refused the request".into()); + layer.source = Some(source); + layer + }; + // One layer reads at the connection's origin; another, linked from it, reads elsewhere. + s.project.map_mut().layers.push(failed(1, format!("{origin}/ogc/collections/roads/items"))); + s.project.map_mut().layers.push(failed(2, "https://linked757.example.org/items".into())); + s.run("connection.login", &json!({"id":id,"apiKey":"stream-sentinel-757"})).unwrap(); + let [own, linked] = &s.map().layers[..] else { panic!("two layers") }; + let own = own.source.as_ref().unwrap(); + assert_eq!(header(own.path.as_deref().unwrap()).unwrap().1, "stream-sentinel-757"); + assert!(own.error.is_none() && !own.is_open()); + // Its address did not change, so its failure stands. + assert!(linked.source.as_ref().unwrap().error.is_some()); +} + +#[test] +fn a_plain_replacement_legend_reads_through_the_tiles_logged_in_connection() { + let origin = "https://legend757.example.org"; + let mut s = session(None); + let id = new(&mut s, json!({"kind":"wms","address":format!("{origin}/wms"),"auth":"header","keyName":"X-Key"})); + s.run("connection.login", &json!({"id":id,"apiKey":"legend-sentinel-757"})).unwrap(); + s.run("map.addTiles", &json!({"url":format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png"),"name":"T","connection":id})).unwrap(); + s.run("map.setTiles", &json!({"name":"T","legend":[format!("{origin}/legend.png"),"https://other757.example.org/legend.png"]})) + .unwrap(); + let legends = &s.map().tiles[0].legends; + assert_eq!(header(&legends[0].url).unwrap().1, "legend-sentinel-757"); + assert_eq!(crate::sources::redact(&legends[0].url), format!("{origin}/legend.png")); + assert_eq!(legends[1].url, "https://other757.example.org/legend.png"); +} diff --git a/crates/tiles/src/logins.rs b/crates/tiles/src/logins.rs index 0f3ad60c13..579c07b4d4 100644 --- a/crates/tiles/src/logins.rs +++ b/crates/tiles/src/logins.rs @@ -37,6 +37,8 @@ pub fn valid_header_name(name: &str) -> bool { "origin", "referer", "user-agent", + "content-type", + "accept", ]; !blocked.iter().any(|s| name.eq_ignore_ascii_case(s)) && !name.get(..6).is_some_and(|s| s.eq_ignore_ascii_case("proxy-")) @@ -402,6 +404,8 @@ mod tests { "Origin", "Referer", "User-Agent", + "Content-Type", + "ACCEPT", "Proxy-Authorization", "SEC-Fetch-Site", "x y", diff --git a/crates/ui-egui/src/connectionui.rs b/crates/ui-egui/src/connectionui.rs index 9ab5c18c68..3543bd219f 100644 --- a/crates/ui-egui/src/connectionui.rs +++ b/crates/ui-egui/src/connectionui.rs @@ -340,7 +340,7 @@ fn test_blocked(app: &RustGisApp) -> Option<&'static str> { } let matches = saved.is_some_and(|c| { c.kind == st.kind - && c.address == rustgis_engine::sources::redact(st.address.trim()) + && c.address == st.address.trim() && c.auth == auth && c.user == st.user.trim() && c.key_name == st.key_name.trim() diff --git a/crates/ui-egui/src/serviceui.rs b/crates/ui-egui/src/serviceui.rs index 2084985eb3..89c2df39ba 100644 --- a/crates/ui-egui/src/serviceui.rs +++ b/crates/ui-egui/src/serviceui.rs @@ -389,13 +389,20 @@ pub fn poll_pages(app: &mut RustGisApp, ctx: &egui::Context) { } } +/// The address a paged read fetches next. Its login comes only from the page that led to it +/// (the referrer), so a page reached through another origin stays anonymous even when it points +/// back at the first page's origin. +fn next_request(app: &mut RustGisApp, connection: Option<&str>, pages: &Pages) -> Result { + rustgis_engine::render::rustgis_tiles::address::check(pages.next_url(), Some(pages.referrer())) + .and_then(|()| request_from(app, connection, pages.referrer(), pages.next_url())) +} + /// The next answer fetched, or the collection added (with a note when some features were left /// unread). fn go_on(app: &mut RustGisApp, landed: Landed, left: usize, step: Step) { match step { Step::Fetch(pages) => { - let next = rustgis_engine::render::rustgis_tiles::address::check(pages.next_url(), Some(pages.referrer())) - .and_then(|()| request_from(app, landed.params.get("connection").and_then(Value::as_str), &landed.url, pages.next_url())); + let next = next_request(app, landed.params.get("connection").and_then(Value::as_str), &pages); let slot = match next { Ok(url) => fetch_into_limited(&url, left), Err(e) => Slot::new(std::sync::Mutex::new(Some(Err(e)))), @@ -411,3 +418,37 @@ fn go_on(app: &mut RustGisApp, landed: Landed, left: usize, step: Step) { } } } + +#[cfg(test)] +mod tests { + use super::*; + + /// A linked page reached through another origin is fetched anonymously even when it points + /// back at the first page's origin: the login follows the referring page, not the first one. + #[test] + fn a_page_reached_through_another_origin_does_not_regain_the_login() { + let mut app = RustGisApp::with_sample(); + let c = app + .run("connection.new", json!({"kind":"ogcapi", "address":"https://maps.example.org", "auth":"header", "keyName":"X-Api-Key"})) + .unwrap(); + let connection = c["id"].as_str().unwrap().to_string(); + let first = + rustgis_engine::data::login_address::bind("0123456789abcdef", "https://maps.example.org", "https://maps.example.org/items") + .unwrap(); + let page = |id: u32, href: &str| { + serde_json::to_vec(&json!({ + "type":"FeatureCollection", "features":[{"id":id}], + "links":[{"rel":"next","type":"application/geo+json","href":href}] + })) + .unwrap() + }; + let Step::Fetch(pages) = ogc::read_pages(&first, page(1, "https://other.example.org/items?page=2"), 100) else { + panic!("the foreign page must be fetched"); + }; + let Step::Fetch(pages) = pages.land(Ok(page(2, "https://maps.example.org/items?page=3"))) else { + panic!("the returning page must be fetched"); + }; + assert_eq!(pages.next_url(), "https://maps.example.org/items?page=3"); + assert_eq!(next_request(&mut app, Some(&connection), &pages).as_deref(), Ok("https://maps.example.org/items?page=3")); + } +} diff --git a/crates/ui-egui/src/stacui.rs b/crates/ui-egui/src/stacui.rs index 6c42191e9a..523a659ae5 100644 --- a/crates/ui-egui/src/stacui.rs +++ b/crates/ui-egui/src/stacui.rs @@ -963,6 +963,7 @@ fn cube_row(app: &mut RustGisApp, ui: &mut egui::Ui, busy: bool) { if keys.is_empty() { return; } + let connection = selected_connection(app); let st = &mut app.ui.stac; if !keys.contains(&st.cube_asset) { st.cube_asset = keys.first().cloned().unwrap_or_default(); @@ -990,11 +991,40 @@ fn cube_row(app: &mut RustGisApp, ui: &mut egui::Ui, busy: bool) { if !add { return; } + let p = match cube_params(st, first, last, connection) { + Ok(p) => p, + Err(e) => { + st.error = Some(e); + return; + } + }; + match app.run("stac.addCube", p) { + Ok(r) => { + app.ui.stac.error = None; + app.ui.stac.info = Some(i18n::fmt( + "{layers} tiles, {dates} dates: the time slider steps through them", + &[("layers", &r["layers"].to_string()), ("dates", &r["dates"].to_string())], + )); + app.map.invalidate(); + } + Err(e) => app.ui.stac.error = Some(e), + } +} + +/// The `stac.addCube` parameters for the dialog as it stands: its API, collection, band, dates, +/// area, cloud limit and display range, and the connection picked for the API (so the cube's +/// requests use that login, not the origin's last-selected one). `first` and `last` are the +/// dates the items span, used where no date is typed. +#[cfg(not(target_arch = "wasm32"))] +fn cube_params(st: &State, first: String, last: String, connection: Option) -> Result { let mut p = json!({ "url": st.api, "collection": st.collection.trim(), "asset": st.cube_asset, "start": if st.from.trim().is_empty() { first } else { st.from.trim().to_string() }, "end": if st.to.trim().is_empty() { last } else { st.to.trim().to_string() }, }); + if let Some(connection) = connection { + p["connection"] = json!(connection); + } // The area the search asked for, so the cube covers what it found (every page of it), not the // view now; a search anywhere, the area of the items listed. let area = st.searched_area.or_else(|| { @@ -1013,22 +1043,9 @@ fn cube_row(app: &mut RustGisApp, ui: &mut egui::Ui, busy: bool) { match (typed.len(), range.len()) { (0, _) => {} (2, 2) => p["range"] = json!(range), - _ => { - st.error = Some(tl!("The range is two numbers, low and high (0, 0.9)").to_string()); - return; - } - } - match app.run("stac.addCube", p) { - Ok(r) => { - app.ui.stac.error = None; - app.ui.stac.info = Some(i18n::fmt( - "{layers} tiles, {dates} dates: the time slider steps through them", - &[("layers", &r["layers"].to_string()), ("dates", &r["dates"].to_string())], - )); - app.map.invalidate(); - } - Err(e) => app.ui.stac.error = Some(e), + _ => return Err(tl!("The range is two numbers, low and high (0, 0.9)").to_string()), } + Ok(p) } /// Finds items: an API's search, or a walk of a static catalog from the node picked. @@ -1225,6 +1242,16 @@ mod tests { assert_ne!(params["connection"], two["id"]); } + /// The cube's request names the connection picked for the API, so it never falls back to the + /// origin's last-selected login (possibly another connection's). + #[test] + fn the_time_series_request_names_the_picked_connection() { + let st = State { api: "https://stac.example.org".into(), collection: "c".into(), ..State::default() }; + let p = cube_params(&st, "2024-01-01".into(), "2024-02-01".into(), Some("picked".into())).unwrap(); + assert_eq!(p["connection"], "picked"); + assert!(cube_params(&st, "a".into(), "b".into(), None).unwrap().get("connection").is_none()); + } + #[test] fn presets_open_the_dialog_set_up() { for p in Preset::ALL { diff --git a/crates/ui-egui/tests/service_logins/mod.rs b/crates/ui-egui/tests/service_logins/mod.rs index c5c3741368..ac2ec40dfa 100644 --- a/crates/ui-egui/tests/service_logins/mod.rs +++ b/crates/ui-egui/tests/service_logins/mod.rs @@ -165,6 +165,18 @@ fn edited_service_test_button_is_disabled_and_retains_the_unsent_key() { assert!(!h.state().session.database_logins.has_session_password(c["id"].as_str().unwrap())); } +#[test] +fn a_typed_address_secret_blocks_testing_before_the_engine_refuses() { + let mut h = session_ui(); + let c = h.state_mut().run("connection.new", json!({"kind":"wms", "address":"https://typed-widget.example/wms"})).unwrap(); + h.state_mut().run("ui.connection", json!({"id":c["id"]})).unwrap(); + settle(&mut h); + assert!(!h.get_by_role_and_label(Role::Button, "Test").accesskit_node().is_disabled()); + h.state_mut().ui.connection.address.push_str("?token=abc"); + settle(&mut h); + assert!(h.get_by_role_and_label(Role::Button, "Test").accesskit_node().is_disabled()); +} + fn request(h: &mut Harness, reason: LoginReason, key: &str, connections: Vec) { let opened = h.state().session.opened; h.state_mut().session.database_logins.request(LoginRequest { From 63ebc8b90f913b2ef7179793626023ced7a73cc9 Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Mon, 2 Nov 2026 19:22:32 +0100 Subject: [PATCH 10/19] docs(data): a kind change ends a login; one saved connection reads its origin's addresses --- docs/user-guide/data.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index f56d285b15..5173288a39 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -759,7 +759,7 @@ header** (the header's name, such as `X-API-Key`, and the key). The connection k the user name and the header name, never the password or key. The password or key goes to `connection.login {"id":"c1","password":"…"}` (basic) or `{"id":"c1","apiKey":"…"}` and lasts only for this session. Web-service logins never access the -computer's credential store. Editing the origin, method, user or header name ends the login; +computer's credential store. Editing the origin, kind, method, user or header name ends the login; renaming the connection or changing its path at the same origin does not. **Log Out** (`connection.logout`) removes the session login. Log in again when a new session needs it. @@ -778,6 +778,9 @@ choice or the only saved connection for their origin. Different connections and different logins at the same origin without sharing credentials or authenticated cached responses. When several saved connections match the address, RustGIS asks which one to use (**Choose Connection…**, `connection.login {id}` for the one chosen) and never picks one itself. +When an origin has exactly one saved connection and it has a login method, an address at that +origin is read through that connection, so it needs that login: log in, or save a public +connection for the address too. A layer that has no login yet asks for one, and a layer whose recorded connection isn't on this computer offers to make one for its address. `connection.list` reports these as `loginRequests` with the `origin`. CLI and Python use the same session-only `connection.login` commands. ArcGIS sign-in and tokens From b055a5e4ea39eec4f2b8bc546aac51adba24b7ea Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Mon, 2 Nov 2026 20:42:56 +0100 Subject: [PATCH 11/19] fix(connections): third-round review fixes - Geoprocessing history: a record's messages are scrubbed of login bindings and secrets as it enters the history (every push site), so saved projects and exported history.json never carry a binding; a tool quoting a bound address in its error kept it before. - A streamed layer already open when its login changes reopens on its new address (SourceLayer::reopen); one not open yet still opens on the next sync. - Opening a STAC API from the Connections tree switches the panel through stacui::switch_api, which also drops a catalog walk still running for the last API. - The assistant-boundary test goes through run_with_budget (it called the helper alone, and passed with the boundary removed). --- crates/assistant/src/tools.rs | 39 ++++++++++++++++------- crates/engine/src/cmd/gp.rs | 36 +++++++-------------- crates/engine/src/lib.rs | 13 +++++++- crates/engine/src/service_logins.rs | 10 ++++-- crates/engine/src/tests/service_logins.rs | 17 ++++++++++ crates/ui-egui/src/sourcespane.rs | 9 ++---- crates/ui-egui/src/stacui.rs | 21 +++++++++++- 7 files changed, 99 insertions(+), 46 deletions(-) diff --git a/crates/assistant/src/tools.rs b/crates/assistant/src/tools.rs index a47247acf5..61f35ad462 100644 --- a/crates/assistant/src/tools.rs +++ b/crates/assistant/src/tools.rs @@ -849,21 +849,36 @@ mod tests { #[test] fn results_carry_plain_addresses_not_login_wrappers() { + let mut s = Session::new(rustgis_engine::Project::default()); + let origin = "https://assistant-login757.example.org"; + let definition = json!({"kind":"wmts", "address": format!("{origin}/tiles"), "auth":"header", "keyName":"X-Key"}); + let id = s.run("connection.new", &definition).unwrap()["id"].as_str().unwrap().to_string(); + s.run("connection.login", &json!({"id": id, "apiKey": "assistant-secret-757"})).unwrap(); + let plain = format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png"); + let tile = rustgis_engine::service_logins::request(&mut s, Some(&id), &plain).unwrap(); + assert!(tile.starts_with(rustgis_engine::data::login_address::PREFIX)); + // The app's command result keeps the bound address it reads through; the model's doesn't. + let call = |command: &str, params: Value| ToolCall { + id: String::new(), + name: "run_command".into(), + args: json!({"command": command, "params": params}), + }; + let added = run_with_budget(&mut s, &call("map.addTiles", json!({"url": tile, "name": "T"})), DEFAULT_OUTPUT_TOKENS).unwrap(); + let listed = run_with_budget(&mut s, &call("map.tiles", json!({})), DEFAULT_OUTPUT_TOKENS).unwrap(); + assert!(s.map().tiles[0].url.starts_with("rustgis-login:")); + for text in [&added, &listed] { + assert!(!text.contains("rustgis-login:") && !text.contains("assistant-secret-757"), "{text}"); + } + assert!(listed.contains(&plain), "the model still learns the layer's address: {listed}"); + } + + #[test] + fn wrappers_after_a_prefix_are_made_plain_too() { let origin = "https://assistant-login.example"; let binding = logins::activate(origin, "assistant", logins::Method::Header("X-Key".into()), "assistant-secret", false).unwrap(); - let tile = logins::bind(&binding, &format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png")).unwrap(); let archive = logins::bind(&binding, &format!("{origin}/a.pmtiles")).unwrap(); - assert!(tile.starts_with("rustgis-login:")); - let result = json!({ - "fetch": tile, - "layers": [{"source_url": archive, "next": {"request": {"url": format!("pmtiles:x@{archive}")}}}], - "count": 2, - }); - let text = bounded_json(&public_addresses(result, 0), DEFAULT_OUTPUT_TOKENS); - assert!(!text.contains("rustgis-login:") && !text.contains(&binding), "{text}"); - let v: Value = serde_json::from_str(&text).unwrap(); - assert_eq!(v["fetch"], format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png")); - assert_eq!(v["layers"][0]["source_url"], format!("{origin}/a.pmtiles")); + let result = json!({"layers": [{"next": {"request": {"url": format!("pmtiles:x@{archive}")}}}], "count": 2}); + let v = public_addresses(result, 0); assert_eq!(v["layers"][0]["next"]["request"]["url"], format!("pmtiles:x@{origin}/a.pmtiles")); assert_eq!(v["count"], 2); assert!(logins::deactivate(&binding)); diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index c18cf994ed..d35e77214d 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -1099,7 +1099,7 @@ fn file_delivered(s: &mut Session, p: &Value) -> CmdResult { s.notify(message.clone(), !file.record.ok); } let out = json!({"file": file.file_name, "delivered": file.record.ok, "messages": file.record.messages}); - s.project.gp_history.push(file.record); + s.project.gp_history.push(file.record.scrubbed()); if s.project.gp_history.len() > 500 { s.project.gp_history.remove(0); } @@ -1191,17 +1191,10 @@ fn apply(s: &mut Session, ran: Ran, params: Value, res: Result { - s.project.gp_history.push(GpRecord { - tool: name, - id, - params, - command, - at, - ok: false, - messages: vec![e.clone()], - millis, - outputs: Vec::new(), - }); + s.project.gp_history.push( + GpRecord { tool: name, id, params, command, at, ok: false, messages: vec![e.clone()], millis, outputs: Vec::new() } + .scrubbed(), + ); return Err(e); } Ok(r) => r, @@ -1223,17 +1216,10 @@ fn apply(s: &mut Session, ran: Ran, params: Value, res: Result Vec::new(), }; let outputs = ids.iter().filter_map(|id| s.map().layer(*id).map(|l| l.name.clone())).collect(); - s.project.gp_history.push(GpRecord { tool: name, id, params, command, at, ok: true, messages: result.messages, millis, outputs }); + s.project + .gp_history + .push(GpRecord { tool: name, id, params, command, at, ok: true, messages: result.messages, millis, outputs }.scrubbed()); if s.project.gp_history.len() > 500 { s.project.gp_history.remove(0); } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index b29ee0ee63..ebb47ace7a 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -397,6 +397,17 @@ pub struct GpRecord { pub outputs: Vec, } +impl GpRecord { + /// The record with its messages scrubbed of login bindings and secrets: a tool's error can + /// quote a bound address, and history rows are saved with the project and exported. + pub(crate) fn scrubbed(mut self) -> Self { + for message in &mut self.messages { + *message = render::rustgis_tiles::logins::scrub(message); + } + self + } +} + fn is_zero(v: &u64) -> bool { *v == 0 } @@ -1336,7 +1347,7 @@ impl Session { .skip(history_from) .filter(|record| !record.ok && rustgis_analysis::tool(&record.id).is_some_and(cmd::gp::writes_files)) { - self.project.gp_history.push(record); + self.project.gp_history.push(record.scrubbed()); recorded_failure = true; } if recorded_failure { diff --git a/crates/engine/src/service_logins.rs b/crates/engine/src/service_logins.rs index c78c85226d..1b0a9781ea 100644 --- a/crates/engine/src/service_logins.rs +++ b/crates/engine/src/service_logins.rs @@ -424,9 +424,15 @@ fn rebind_connection(s: &mut Session, c: &Connection) { { let before = path.clone(); rewrite(path); - // A failed open is kept, not retried; the changed address deserves a new try. + // The changed address deserves a new try: an open source still reads through the + // old binding, so it opens afresh (a failure is kept in its `error`, as any open's + // is); one not open yet, or whose open failed, opens on the next sync. if *path != before { - source.error = None; + if source.is_open() { + let _ = source.reopen(); + } else { + source.error = None; + } } } if let Some(path) = l.raster.as_mut().and_then(|raster| raster.path.as_mut()) { diff --git a/crates/engine/src/tests/service_logins.rs b/crates/engine/src/tests/service_logins.rs index 75052c46da..8f57846913 100644 --- a/crates/engine/src/tests/service_logins.rs +++ b/crates/engine/src/tests/service_logins.rs @@ -568,3 +568,20 @@ fn a_plain_replacement_legend_reads_through_the_tiles_logged_in_connection() { assert_eq!(crate::sources::redact(&legends[0].url), format!("{origin}/legend.png")); assert_eq!(legends[1].url, "https://other757.example.org/legend.png"); } + +#[test] +fn geoprocessing_history_never_keeps_a_login_binding() { + let mut s = Session::new(crate::sample::world_project()); + let id = new(&mut s, json!({"kind":"wfs","address":"https://gp757.example.org/wfs","auth":"header","keyName":"X-Key"})); + s.run("connection.login", &json!({"id":id,"apiKey":"gp-sentinel-757"})).unwrap(); + let bound = bound(&mut s, &id, "https://gp757.example.org/wfs/roads.pmtiles"); + assert!(bound.starts_with(rustgis_data::login_address::PREFIX)); + // A failed write quotes where it went, and the history keeps the failed run's message. + let input = s.map().layers.iter().find(|l| l.name.eq_ignore_ascii_case("cities")).map(|l| l.name.clone()).unwrap(); + let params = json!({"input":input,"minZoom":0,"maxZoom":0,"output":bound}); + let error = s.run("gp.run", &json!({"tool":"vectorToPmtiles","params":params})).unwrap_err(); + let record = s.project.gp_history.last().unwrap_or_else(|| panic!("the failed run is recorded: {error}")); + let text = record.messages.join("\n"); + assert!(text.contains("https://gp757.example.org/wfs/roads.pmtiles"), "the message quotes the address: {text}"); + assert!(!text.contains(rustgis_data::login_address::PREFIX) && !text.contains("gp-sentinel-757"), "{text}"); +} diff --git a/crates/ui-egui/src/sourcespane.rs b/crates/ui-egui/src/sourcespane.rs index ab0224351c..7a47640e38 100644 --- a/crates/ui-egui/src/sourcespane.rs +++ b/crates/ui-egui/src/sourcespane.rs @@ -168,13 +168,10 @@ pub fn open(app: &mut RustGisApp, s: &Source) -> Result<(), String> { } "zarr" => app.run("ui.addZarr", json!({"address": s.address})).map(|_| ()), "stac" if text(x, "api").is_some() => { - app.ui.stac.api = s.address.clone(); + // The panel's own switch: it also drops a catalog walk still on its way from the last + // API, which would otherwise land in this listing through this connection. + crate::stacui::switch_api(&mut app.ui.stac, &s.address); app.ui.stac.preset = None; - (app.ui.stac.node, app.ui.stac.node_title) = (String::new(), String::new()); - app.ui.stac.trail.clear(); - app.ui.stac.children.clear(); - app.ui.stac.collections.clear(); - app.ui.stac.items.clear(); app.run("ui.dialog", json!({"name": "stac"})).map(|_| ()) } "stac" => app.run("ui.stacLink", json!({"url": s.address})).map(|_| ()), diff --git a/crates/ui-egui/src/stacui.rs b/crates/ui-egui/src/stacui.rs index 523a659ae5..6fad570792 100644 --- a/crates/ui-egui/src/stacui.rs +++ b/crates/ui-egui/src/stacui.rs @@ -713,7 +713,9 @@ pub fn title(app: &RustGisApp) -> &'static str { shown_preset(&app.ui.stac).map(Preset::title).unwrap_or("STAC Search") } -fn switch_api(st: &mut State, url: &str) { +/// Points the panel at another API, dropping what the last one listed and what is still on its +/// way from it (a catalog walk among them), wherever the switch comes from. +pub(crate) fn switch_api(st: &mut State, url: &str) { st.api = url.to_string(); drop_listing(st); } @@ -1218,6 +1220,23 @@ mod tests { use super::*; use crate::RustGisApp; + #[test] + fn opening_a_stac_api_from_the_tree_drops_the_last_apis_catalog_walk() { + let mut app = RustGisApp::with_sample(); + let old = "https://old-walk757.example.org/catalog.json"; + app.ui.stac.api = old.into(); + app.ui.stac.walk = Some((Walk::new(old, catalog::Filter::default(), 10), Vec::new())); + let source = rustgis_engine::sources::Source { + kind: "stac".into(), + name: "New API".into(), + address: "https://new-walk757.example.org/stac".into(), + extra: json!({"api": "https://new-walk757.example.org/stac"}), + }; + crate::sourcespane::open(&mut app, &source).unwrap(); + assert!(app.ui.stac.walk.is_none(), "the old catalog's documents must not land in the new listing"); + assert_eq!(app.ui.stac.api, "https://new-walk757.example.org/stac"); + } + #[test] fn a_deferred_token_add_keeps_its_original_connection_when_selection_changes() { let mut app = RustGisApp::with_sample(); From d1cf078fafe23928dd140b317c3fe26e0774b081 Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Tue, 3 Nov 2026 00:31:43 +0100 Subject: [PATCH 12/19] fix(connections): fourth-round review fixes - A project package (project.package) clears login bindings from its recorded parameters (geoprocessing history, tasks, models) as saving does; it kept them before. - Switching the STAC panel to another API drops its cached thumbnails, read through the last API's login. - Tests: the history test also checks the saved project and the package; a refresh reads through the connection its layers record even after a login to another connection at the same origin. --- crates/engine/src/cmd/items.rs | 2 ++ crates/engine/src/project.rs | 2 +- crates/engine/src/tests/service_logins.rs | 12 +++++++++ crates/ui-egui/src/serviceui.rs | 33 +++++++++++++++++++++++ crates/ui-egui/src/stacui.rs | 4 +++ 5 files changed, 52 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/items.rs b/crates/engine/src/cmd/items.rs index c3462e3595..86bbe21744 100644 --- a/crates/engine/src/cmd/items.rs +++ b/crates/engine/src/cmd/items.rs @@ -559,6 +559,8 @@ fn package_project(s: &Session) -> Result, String> { *m = embedded_map(m, &mut budget); } crate::project::strip_addresses(p.addresses_mut()); + // Recorded parameters (history, tasks, models) lose only their login bindings, as when saving. + crate::project::clear_login_bindings(p.recorded_mut()); let text = serde_json::to_string(&p).map_err(|e| e.to_string())?; let readme = format!("{} — a RustGIS project package.\nOpen it with RustGIS (Project › Open), or unzip it for {}.rgis.\n", p.name, p.name); diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index ba118defa2..79643b4d86 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -574,7 +574,7 @@ impl Project { /// The strings of recorded command parameters (geoprocessing history, tasks and models). They /// are not layer addresses, and only login bindings are taken from them when saving or opening. - fn recorded_mut(&mut self) -> Vec<&mut String> { + pub(crate) fn recorded_mut(&mut self) -> Vec<&mut String> { let mut addresses = Vec::new(); for record in &mut self.gp_history { recorded_addresses(&mut record.params, &mut addresses); diff --git a/crates/engine/src/tests/service_logins.rs b/crates/engine/src/tests/service_logins.rs index 8f57846913..4c226c880a 100644 --- a/crates/engine/src/tests/service_logins.rs +++ b/crates/engine/src/tests/service_logins.rs @@ -584,4 +584,16 @@ fn geoprocessing_history_never_keeps_a_login_binding() { let text = record.messages.join("\n"); assert!(text.contains("https://gp757.example.org/wfs/roads.pmtiles"), "the message quotes the address: {text}"); assert!(!text.contains(rustgis_data::login_address::PREFIX) && !text.contains("gp-sentinel-757"), "{text}"); + // The record's params keep the bound output to run again; what is written keeps none. + assert!(record.params["output"].as_str().is_some_and(|o| o.starts_with(rustgis_data::login_address::PREFIX))); + let (saved, _) = s.project.clone().saved_text(None).unwrap(); + assert!(!saved.contains(rustgis_data::login_address::PREFIX), "the saved project keeps a binding: {saved}"); + let package = std::env::temp_dir().join(format!("gp757-{}.rgispkg", std::process::id())); + s.run("project.package", &json!({"path": package.to_string_lossy()})).unwrap(); + let bytes = std::fs::read(&package).unwrap(); + let _ = std::fs::remove_file(&package); + for (name, entry) in rustgis_data::zip::read(&bytes).unwrap() { + let entry = String::from_utf8_lossy(&entry); + assert!(!entry.contains(rustgis_data::login_address::PREFIX), "{name} keeps a binding: {entry}"); + } } diff --git a/crates/ui-egui/src/serviceui.rs b/crates/ui-egui/src/serviceui.rs index 89c2df39ba..5099d27d6a 100644 --- a/crates/ui-egui/src/serviceui.rs +++ b/crates/ui-egui/src/serviceui.rs @@ -451,4 +451,37 @@ mod tests { assert_eq!(pages.next_url(), "https://maps.example.org/items?page=3"); assert_eq!(next_request(&mut app, Some(&connection), &pages).as_deref(), Ok("https://maps.example.org/items?page=3")); } + + /// Two saved connections at one origin: a group imported through one of them refreshes + /// through it, even after a login to the other made that one the origin's choice (the plan's + /// address comes bound to the connection its layers record). + #[test] + fn a_refresh_reads_through_the_connection_its_layers_record() { + let mut app = RustGisApp::with_sample(); + let url = "https://127.0.0.1:9/collections"; + let new = |app: &mut RustGisApp, name: &str, key: &str| { + let definition = json!({"kind":"ogcapi", "name":name, "address":url, "auth":"header", "keyName":"X-Key"}); + let id = app.run("connection.new", definition).unwrap()["id"].as_str().unwrap().to_string(); + app.run("connection.login", json!({"id": id, "apiKey": key})).unwrap(); + id + }; + let a = new(&mut app, "A", "refresh-a-757"); + let items = format!("{url}/roads/items"); + let document = json!({"collections":[{"id":"roads", "title":"roads", + "links":[{"rel":"items", "type":"application/geo+json", "href": items}]}]}); + let features = json!({"type":"FeatureCollection", "features":[{"type":"Feature", + "geometry":{"type":"Point", "coordinates":[1, 2]}, "properties":{}}]}); + let mut p = json!({"url": url, "type":"ogcapi", "connection": a, "text": document.to_string()}); + let plan = app.session.run("service.layers", &p).unwrap(); + let offers = plan["layers"].as_array().unwrap().iter(); + p["responses"] = json!(offers.map(|l| json!({"url": l["offer"]["url"], "text": features.to_string()})).collect::>()); + let imported = app.session.run("service.import", &p).unwrap(); + // Logging in to B makes it the origin's choice; the group's layers still record A. + new(&mut app, "B", "refresh-b-757"); + start(&mut app, &json!({"group": imported["group"]}), true).unwrap(); + let task = app.ui.service_import.as_ref().unwrap(); + let capabilities = task.document.as_deref().unwrap(); + let header = rustgis_engine::render::rustgis_tiles::logins::apply(capabilities).unwrap().and_then(|login| login.header); + assert_eq!(header.map(|(_, key)| key).as_deref(), Some("refresh-a-757")); + } } diff --git a/crates/ui-egui/src/stacui.rs b/crates/ui-egui/src/stacui.rs index 6fad570792..3bfa7c8a8a 100644 --- a/crates/ui-egui/src/stacui.rs +++ b/crates/ui-egui/src/stacui.rs @@ -736,6 +736,8 @@ fn drop_listing(st: &mut State) { st.waiting = None; } st.walk = None; + // Previews are read through the last API's login: another one shows its own, read afresh. + st.thumbs.clear(); // The last listing's status ("Showing 10 items") isn't the new API's; an asset being signed // or a link being opened keeps its own. if st.waiting.is_none() { @@ -1226,6 +1228,7 @@ mod tests { let old = "https://old-walk757.example.org/catalog.json"; app.ui.stac.api = old.into(); app.ui.stac.walk = Some((Walk::new(old, catalog::Filter::default(), 10), Vec::new())); + app.ui.stac.thumbs.insert("https://new-walk757.example.org/preview.png".into(), Thumb::Failed); let source = rustgis_engine::sources::Source { kind: "stac".into(), name: "New API".into(), @@ -1235,6 +1238,7 @@ mod tests { crate::sourcespane::open(&mut app, &source).unwrap(); assert!(app.ui.stac.walk.is_none(), "the old catalog's documents must not land in the new listing"); assert_eq!(app.ui.stac.api, "https://new-walk757.example.org/stac"); + assert!(app.ui.stac.thumbs.is_empty(), "a preview read through the last API's login isn't shown for the next"); } #[test] From c9bb33d6b81df71df37408764602ff31ca274586 Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Tue, 3 Nov 2026 01:05:53 +0100 Subject: [PATCH 13/19] fix(connections): fifth-round review fixes - Templates, task files and exported models clear login bindings from what they record, as project saves and packages do; a model node's input address included (saves too). - A refresh reads its linked documents and layers through the connection its layers record, not the origin's current choice. - A legend at a logged-in connection's origin that can't carry its login (a space in it) is refused, not read without the login. - Comment: why reusing a session secret skips the plain-http check. - Tests: the history test also checks the results package. --- crates/engine/src/cmd/items.rs | 12 ++++- crates/engine/src/cmd/tiles.rs | 2 +- crates/engine/src/cmd/workflows.rs | 11 ++++- crates/engine/src/project.rs | 4 ++ crates/engine/src/service_logins.rs | 26 +++++++---- crates/engine/src/tests/service_logins.rs | 55 ++++++++++++++++++++--- crates/ui-egui/src/serviceui.rs | 6 +++ 7 files changed, 97 insertions(+), 19 deletions(-) diff --git a/crates/engine/src/cmd/items.rs b/crates/engine/src/cmd/items.rs index 86bbe21744..26911458ec 100644 --- a/crates/engine/src/cmd/items.rs +++ b/crates/engine/src/cmd/items.rs @@ -1060,6 +1060,9 @@ pub fn item_bytes(s: &mut Session, kind: &str, p: &Value) -> Result<(String, Vec "template" => { s.project.ensure_imports_complete()?; let mut t = template(&s.project); + // As a package: what it reads keeps its plain address, what it records no binding. + crate::project::strip_addresses(t.addresses_mut()); + crate::project::clear_login_bindings(t.recorded_mut()); if let Some(n) = ps(p, "name").map(str::trim).filter(|n| !n.is_empty()) { t.name = n.chars().take(200).collect(); } @@ -1073,8 +1076,13 @@ pub fn item_bytes(s: &mut Session, kind: &str, p: &Value) -> Result<(String, Vec "gpPackage" => ("results".to_string(), package_gp(s)?), "bookmarks" => (format!("{} bookmarks", s.map().name), super::navigate::bookmarks_file(s.map())?), "task" => { - let t = task_ref(s, p)?; - (t.name.clone(), to_json(t)?) + let mut t = task_ref(s, p)?.clone(); + let mut addresses = Vec::new(); + for step in &mut t.steps { + crate::project::recorded_addresses(&mut step.params, &mut addresses); + } + crate::project::clear_login_bindings(addresses); + (t.name.clone(), to_json(&t)?) } "obj" => { let id = s.layer_id(p)?; diff --git a/crates/engine/src/cmd/tiles.rs b/crates/engine/src/cmd/tiles.rs index 1b7605d696..a8a31f1c3d 100644 --- a/crates/engine/src/cmd/tiles.rs +++ b/crates/engine/src/cmd/tiles.rs @@ -642,7 +642,7 @@ fn set(s: &mut Session, p: &Value) -> CmdResult { // as the layer's own address does. if let Some(connection) = s.map().tiles.get(i).map(|t| t.connection.clone()).filter(|c| !c.is_empty()) { for legend in new_legends.iter_mut().flatten() { - legend.url = crate::service_logins::bind_logged_in(s, &connection, &legend.url); + legend.url = crate::service_logins::bind_logged_in(s, &connection, &legend.url)?; } } let hidden = hidden_layers(p)?; diff --git a/crates/engine/src/cmd/workflows.rs b/crates/engine/src/cmd/workflows.rs index bd72ac96b8..b3aa6a4494 100644 --- a/crates/engine/src/cmd/workflows.rs +++ b/crates/engine/src/cmd/workflows.rs @@ -358,7 +358,16 @@ fn save_model(s: &mut Session, p: &Value) -> CmdResult { /// A model as a `*.model.json` file: written to `path`, or its text returned. fn export_model(s: &mut Session, p: &Value) -> CmdResult { - let (name, g, _) = model_graph(s, p)?; + let (name, mut g, _) = model_graph(s, p)?; + // A file outlives the session: its steps keep their addresses, not this session's logins. + let mut addresses = Vec::new(); + for node in &mut g.nodes { + crate::project::recorded_addresses(&mut node.params, &mut addresses); + if let Some(layer) = &mut node.layer { + addresses.push(layer); + } + } + crate::project::clear_login_bindings(addresses); let text = serde_json::to_string_pretty(&crate::model::to_file(&name, &g)).map_err(|e| e.to_string())?; let file: String = name.chars().map(|c| if c.is_alphanumeric() || c == '-' || c == '_' { c } else { '_' }).collect(); match ps(p, "path") { diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 79643b4d86..8780febb55 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -594,6 +594,10 @@ impl Project { if let Some(graph) = &mut model.graph { for node in &mut graph.nodes { recorded_addresses(&mut node.params, &mut addresses); + // A node's input may be an address rather than a layer's name. + if let Some(layer) = &mut node.layer { + addresses.push(layer); + } } } } diff --git a/crates/engine/src/service_logins.rs b/crates/engine/src/service_logins.rs index 1b0a9781ea..dd68d18786 100644 --- a/crates/engine/src/service_logins.rs +++ b/crates/engine/src/service_logins.rs @@ -246,14 +246,21 @@ pub fn logged_in(s: &Session, c: &Connection) -> bool { } /// A plain address at the origin of a logged-in service connection, bound to that login. Anything -/// else (another origin, a wrapper, a connection not logged in) comes back unchanged. -pub(crate) fn bind_logged_in(s: &Session, connection: &str, url: &str) -> String { - let Some(c) = s.connections.connections.iter().find(|c| c.id == connection && is_service(c)) else { return url.to_string() }; - let origin = logins::origin(url); - if !logged_in(s, c) || origin.is_none() || login_origin(c) != origin || rustgis_data::login_address::is_wrapper(url) { - return url.to_string(); - } - s.web_logins.active.get(&c.id).and_then(|a| logins::bind(&a.binding, url).ok()).unwrap_or_else(|| url.to_string()) +/// else (another origin, a wrapper, a connection not logged in) comes back unchanged. An address +/// at that origin that can't carry the login is refused, never read without it. +pub(crate) fn bind_logged_in(s: &Session, connection: &str, url: &str) -> Result { + let Some(c) = s.connections.connections.iter().find(|c| c.id == connection && is_service(c)) else { return Ok(url.to_string()) }; + let Some(origin) = login_origin(c) else { return Ok(url.to_string()) }; + // At the origin by its text too: an address that doesn't parse (a space in it) still names it. + let named = url.get(..origin.len()).is_some_and(|start| start.eq_ignore_ascii_case(&origin)) + && url.get(origin.len()..).is_some_and(|rest| rest.is_empty() || rest.starts_with(['/', '?', '#'])); + let at_origin = named || logins::origin(url).as_deref() == Some(origin.as_str()); + if !logged_in(s, c) || !at_origin || rustgis_data::login_address::is_wrapper(url) { + return Ok(url.to_string()); + } + let Some(active) = s.web_logins.active.get(&c.id) else { return Ok(url.to_string()) }; + logins::bind(&active.binding, url) + .map_err(|_| format!("{url}: this address can't carry the connection's login (it has a space, a control character or a backslash)")) } pub fn known_secret(s: &Session, c: &Connection) -> Result { @@ -320,6 +327,9 @@ pub fn request(s: &mut Session, connection: Option<&str>, url: &str) -> Result Result<(), Strin p.remove("documents"); } } + // A refresh reads its linked documents and layers through the connection its layers record, + // which the plan names when every child agrees: not the origin's current choice. + let connection = if refresh { params.get("connection").filter(|c| !c.is_null()).cloned() } else { connection }; let mut task = Import { params, refresh, @@ -483,5 +486,8 @@ mod tests { let capabilities = task.document.as_deref().unwrap(); let header = rustgis_engine::render::rustgis_tiles::logins::apply(capabilities).unwrap().and_then(|login| login.header); assert_eq!(header.map(|(_, key)| key).as_deref(), Some("refresh-a-757")); + // The linked documents and the layers' features (`Import::discover`, `Import::next`) are + // requested through the task's connection. + assert_eq!(task.connection.as_ref().and_then(Value::as_str), Some(a.as_str())); } } From 358a2efc4a1091c43a57782e6950a5057832adfd Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Tue, 3 Nov 2026 01:29:43 +0100 Subject: [PATCH 14/19] fix(connections): service requests list their connection parameter - service.layers, catalog.search, service.groupPlan and service.refresh take a `connection` (the app sends one through a saved connection), so they no longer warn that they ignore it; the assistant and MCP clients see it in their descriptions. - Add Service's Keep matches and keeps the plain address, not the one bound to a login: no Keep for a connection already saved, and no false "password or token left out" notice. - docs: a connection's kind doesn't change (make a new one). --- crates/engine/src/cmd/service.rs | 4 ++-- crates/engine/src/cmd/service_group.rs | 4 ++-- crates/engine/src/tests/service_logins.rs | 15 +++++++++++++++ crates/ui-egui/src/dialogs.rs | 2 ++ docs/user-guide/data.md | 5 +++-- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/cmd/service.rs b/crates/engine/src/cmd/service.rs index c68ce4a684..d069476ccb 100644 --- a/crates/engine/src/cmd/service.rs +++ b/crates/engine/src/cmd/service.rs @@ -24,7 +24,7 @@ pub const MAX_GROUP_BYTES: usize = 64 << 20; pub fn commands() -> Vec { let mut commands = vec![ C::new("service.layers", "Service Layers", "Home › Add", layers).view().params( - r#"{"url": "a WMS, WMTS or WFS address (GetCapabilities is added), an OGC API collections address or landing page (its map tiles and linked collections), or an ArcGIS FeatureServer, MapServer, ImageServer or VectorTileServer, or an ArcGIS services directory or folder (its folders and services, each listed by its own url)", "type"?: "wms"|"wmts"|"wfs"|"ogcapi"|"arcgis" (guessed from the address), "text"?: "the capabilities document, when already fetched", "documents"?: [{"url": "a discovered document address", "text": "its fetched JSON"}]} → layers with source_url, and next when supplied documents need another fetch"#, + r#"{"url": "a WMS, WMTS or WFS address (GetCapabilities is added), an OGC API collections address or landing page (its map tiles and linked collections), or an ArcGIS FeatureServer, MapServer, ImageServer or VectorTileServer, or an ArcGIS services directory or folder (its folders and services, each listed by its own url)", "type"?: "wms"|"wmts"|"wfs"|"ogcapi"|"arcgis" (guessed from the address), "text"?: "the capabilities document, when already fetched", "documents"?: [{"url": "a discovered document address", "text": "its fetched JSON"}], "connection"?: "the saved web-service connection id whose login the requests use"} → layers with source_url, and next when supplied documents need another fetch"#, ), C::new("service.add", "Add Service Layer", "Home › Add", add).params( r#"{"url": "as service.layers", "type"?, "layer": "the layer's name (service.layers lists them)", "name"?: text, "time"?: "a time-enabled WMS or WMTS layer: by default (or \"slider\", true) it follows the time slider, turned on with the layer's times when it is off; or one of its dates (2024-06-01), or \"default\" (false): its service's default", "text"?: "the capabilities document, when already fetched", "documents"?: "additional fetched documents, as service.layers", "source_url"?: "the listed layer's source_url", "describe"?: "a WCS coverage's DescribeCoverage document, when already fetched", "tiles"?: "an OGC API layer's tile template, when already read from its tileset (the app's)", "bbox"?: [west, south, east, north] | "view" | "full": a WCS coverage's area (the view by default); for a WFS, OGC API or ArcGIS feature layer, only the features that cross it (the whole layer, "full", by default), "connection"?: "the id of the saved web-service connection it comes from (connection.browse gives it): the layer records it, and is read with its login"}"#, @@ -33,7 +33,7 @@ pub fn commands() -> Vec { r#"{"url": "a WCS address", "type"?: "wcs", "layer": "the coverage's name", "text"?: "the capabilities document, when already fetched", "documents"?: "additional fetched documents, as service.layers", "source_url"?: "the listed coverage's source_url", "describe"?: "its DescribeCoverage answer, when already fetched", "bbox"?: [west, south, east, north] | "view" (the default) | "full", "connection"?: "the saved web-service connection id whose login the request uses"} → the GetCoverage address of a GeoTIFF (layer.addUrl adds it)"#, ), C::new("catalog.search", "Search Catalogue", "Home › Add", catalog_search).view().params( - r#"{"url": "a catalogue's CSW address", "query"?: "words anywhere in the records", "bbox"?: [west, south, east, north] | "view" (none by default), "start"?: "the first record's position (a page's next)", "text"?: "the GetRecords answer, when already fetched"} → records with their service and download links (service.add or layer.addUrl add them)"#, + r#"{"url": "a catalogue's CSW address", "query"?: "words anywhere in the records", "bbox"?: [west, south, east, north] | "view" (none by default), "start"?: "the first record's position (a page's next)", "text"?: "the GetRecords answer, when already fetched", "connection"?: "as service.layers"} → records with their service and download links (service.add or layer.addUrl add them)"#, ), ]; commands.extend(group::commands()); diff --git a/crates/engine/src/cmd/service_group.rs b/crates/engine/src/cmd/service_group.rs index efe83b969c..ded1822d02 100644 --- a/crates/engine/src/cmd/service_group.rs +++ b/crates/engine/src/cmd/service_group.rs @@ -13,10 +13,10 @@ pub fn commands() -> Vec { r#"{"url": "an OGC API landing page or collections address", "type"?, "name"?: "group name (default My OGC Layer)", "text"?, "documents"?: "as service.layers", "responses"?: [{"url": "features URL", "text": "downloaded GeoJSON"}], "connection"?: "as service.add"} ("group" and "layer" are rejected: use service.refresh)"#, ), C::new("service.refresh", "Refresh", "Layers", refresh).params( - r#"{"group"?: id, "layer"?: "one child layer id or name", "text"?, "documents"?, "responses"?: "as service.import"}"#, + r#"{"group"?: id, "layer"?: "one child layer id or name", "text"?, "documents"?, "responses"?: "as service.import", "connection"?: "as service.add (default: the one its layers record)"}"#, ), C::new("service.groupPlan", "Service Import Requests", "Layers", plan).view().params( - r#"{"url"?, "type"?, "name"?, "group"?: id, "layer"?: "one child layer", "sourceOnly"?: "return the connection without fetching", "text"?, "documents"?} → request, layers, and next while discovery is incomplete"#, + r#"{"url"?, "type"?, "name"?, "group"?: id, "layer"?: "one child layer", "sourceOnly"?: "return the connection without fetching", "text"?, "documents"?, "connection"?: "as service.add"} → request, layers, and next while discovery is incomplete"#, ), C::new("service.group", "Service Group", "Layers", set_group).params( r#"{"group"?: id, "layer"?: "a child identifying its group", "name"?: text, "expanded"?: bool, "visible"?: "set all children", "remove"?: "remove the group and its children"}"#, diff --git a/crates/engine/src/tests/service_logins.rs b/crates/engine/src/tests/service_logins.rs index 7df040bfb5..e039aa3b54 100644 --- a/crates/engine/src/tests/service_logins.rs +++ b/crates/engine/src/tests/service_logins.rs @@ -638,3 +638,18 @@ fn a_legend_that_cant_carry_the_login_is_refused_not_read_without_it() { assert!(error.contains("can't carry the connection's login"), "{error}"); assert!(s.map().tiles[0].legends.is_empty(), "refused before anything changed"); } + +#[test] +fn service_requests_take_a_connection_without_saying_they_ignore_it() { + let url = "https://browse757.example.org/collections"; + let mut s = session(None); + let id = new(&mut s, json!({"kind":"ogcapi","address":url,"auth":"header","keyName":"X-Key"})); + s.run("connection.login", &json!({"id":id,"apiKey":"browse-sentinel-757"})).unwrap(); + let document = json!({"collections":[{"id":"roads","title":"roads", + "links":[{"rel":"items","type":"application/geo+json","href":format!("{url}/roads/items")}]}]}); + let mark = s.notice_mark(); + s.run("service.layers", &json!({"url":url,"type":"ogcapi","text":document.to_string(),"connection":id})).unwrap(); + s.run("service.groupPlan", &json!({"url":url,"type":"ogcapi","sourceOnly":true,"connection":id})).unwrap(); + let said: Vec<&str> = s.notices_since(mark).iter().map(|n| n.text.as_str()).collect(); + assert!(said.iter().all(|n| !n.contains("ignores")), "{said:?}"); +} diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index faef54921b..da4335aeab 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -878,6 +878,8 @@ fn add_service(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { let mut back = false; let keepable = app.ui.service.as_ref().and_then(|conn| { let (url, kind) = app.ui.service_browse.back.first().map(|(u, k)| (u.as_str(), *k)).unwrap_or((&conn.url, conn.kind)); + // The address as typed: one read through a login is matched and kept without its binding. + let url = rustgis_engine::data::login_address::inner(url); crate::connectionui::picked(&list, app.dialogs.connection.as_deref(), url).is_none().then(|| (url.to_string(), kind)) }); let mut keep_root = false; diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index 58be31f38a..1b9532c48f 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -760,8 +760,9 @@ header** (the header's name, such as `X-API-Key`, and the key). The connection k the user name and the header name, never the password or key. The password or key goes to `connection.login {"id":"c1","password":"…"}` (basic) or `{"id":"c1","apiKey":"…"}` and lasts only for this session. Web-service logins never access the -computer's credential store. Editing the origin, kind, method, user or header name ends the login; -renaming the connection or changing its path at the same origin does not. **Log Out** +computer's credential store. Editing the origin, method, user or header name ends the login (a +connection's kind doesn't change: make a new one); renaming the connection or changing its path +at the same origin does not. **Log Out** (`connection.logout`) removes the session login. Log in again when a new session needs it. A login is sent only to the connection's origin: the same scheme, host and port, for its From e8e04565fdb67685f15cf4a7603fcaed5251389f Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Tue, 3 Nov 2026 01:43:38 +0100 Subject: [PATCH 15/19] fix(ui): Search Catalogue's Keep matches and keeps the plain address --- crates/ui-egui/src/cswui.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/cswui.rs b/crates/ui-egui/src/cswui.rs index 6c0c1c1e07..0a722f70ca 100644 --- a/crates/ui-egui/src/cswui.rs +++ b/crates/ui-egui/src/cswui.rs @@ -159,7 +159,8 @@ fn records(app: &mut RustGisApp, ui: &mut egui::Ui) { return; } ui.separator(); - let url = st.asked["url"].as_str().unwrap_or("").to_string(); + // The address as typed: one read through a login is matched and kept without its binding. + let url = rustgis_engine::data::login_address::inner(st.asked["url"].as_str().unwrap_or("")).to_string(); let keepable = !url.is_empty() && crate::connectionui::picked(&list, app.dialogs.connection.as_deref(), &url).is_none(); let mut keep = false; ui.horizontal(|ui| { From c2c8eb3e403efb5b8ec5cf4dddb3f347f1188ed7 Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Tue, 3 Nov 2026 18:34:58 +0100 Subject: [PATCH 16/19] fix(connections): plugins' legends and notebook output carry no login - A legend a plugin sets on a logged-in tile layer stays plain: plugin commands read anonymously, and `bind_logged_in` now honours that, as every other login path does. - A notebook command cell's output is scrubbed of login bindings before it is stored (and cut), so saved projects and recovery copies keep none. --- crates/engine/src/cmd/notebook.rs | 2 ++ crates/engine/src/service_logins.rs | 4 ++++ crates/engine/src/tests/service_logins.rs | 27 ++++++++++++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/notebook.rs b/crates/engine/src/cmd/notebook.rs index b9d99e8940..c82763a713 100644 --- a/crates/engine/src/cmd/notebook.rs +++ b/crates/engine/src/cmd/notebook.rs @@ -337,6 +337,8 @@ fn run_cells(s: &mut Session, p: &Value) -> CmdResult { nb.cells.get_mut(at) }); if let Some(c) = ran_cell { + // Scrubbed before the cut, so no part of a login binding a result quoted is kept. + let text = crate::render::rustgis_tiles::logins::scrub(&text); c.output = Some(text.chars().take(MAX_OUTPUT).collect()); c.error = failed; // The Jupyter outputs are from an earlier run; this one's output replaces them. diff --git a/crates/engine/src/service_logins.rs b/crates/engine/src/service_logins.rs index dd68d18786..fba32d8bfd 100644 --- a/crates/engine/src/service_logins.rs +++ b/crates/engine/src/service_logins.rs @@ -249,6 +249,10 @@ pub fn logged_in(s: &Session, c: &Connection) -> bool { /// else (another origin, a wrapper, a connection not logged in) comes back unchanged. An address /// at that origin that can't carry the login is refused, never read without it. pub(crate) fn bind_logged_in(s: &Session, connection: &str, url: &str) -> Result { + // A plugin's command reads anonymously: a legend it sets never takes the user's login. + if s.web_logins.is_anonymous() { + return Ok(url.to_string()); + } let Some(c) = s.connections.connections.iter().find(|c| c.id == connection && is_service(c)) else { return Ok(url.to_string()) }; let Some(origin) = login_origin(c) else { return Ok(url.to_string()) }; // At the origin by its text too: an address that doesn't parse (a space in it) still names it. diff --git a/crates/engine/src/tests/service_logins.rs b/crates/engine/src/tests/service_logins.rs index e039aa3b54..cafcff5873 100644 --- a/crates/engine/src/tests/service_logins.rs +++ b/crates/engine/src/tests/service_logins.rs @@ -498,7 +498,8 @@ fn plugins_cannot_send_a_saved_web_service_login() { let origin = "https://plugin757.example.org"; let id = new(&mut s, json!({"kind":"wms","address":format!("{origin}/wms"),"auth":"header","keyName":"X-Key"})); s.run("connection.login", &json!({"id":id,"apiKey":"plugin-sentinel-757"})).unwrap(); - super::plugin("eng-login-757", &["edit", "network"]); + // With files and network granted too: neither lends the plugin the user's login. + super::plugin("eng-login-757", &["edit", "network", "files"]); let tiles = format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png"); // Naming the connection is refused, as logging in to it is. for command in ["map.addTiles", "layer.addUrl", "service.layers"] { @@ -513,6 +514,13 @@ fn plugins_cannot_send_a_saved_web_service_login() { s.run("map.addTiles", &json!({"url":tiles,"name":"Mine"})).unwrap(); let mine = s.map().tiles.iter().find(|t| t.name == "Mine").unwrap(); assert_eq!(header(&mine.url).unwrap().1, "plugin-sentinel-757"); + // A legend it sets on the user's logged-in layer stays plain: the layer's login isn't lent. + s.run("map.addTiles", &json!({"url":tiles,"name":"Named","connection":id})).unwrap(); + let legend = format!("{origin}/legend.png"); + super::forward(&mut s, "eng-login-757", "map.setTiles", json!({"name":"Named","legend":legend})).unwrap(); + let named = s.map().tiles.iter().find(|t| t.name == "Named").unwrap(); + assert_eq!(named.connection, id, "the layer records the connection, so its legend could be bound"); + assert_eq!(named.legends[0].url, legend); rustgis_plugins::unload("eng-login-757"); } @@ -653,3 +661,20 @@ fn service_requests_take_a_connection_without_saying_they_ignore_it() { let said: Vec<&str> = s.notices_since(mark).iter().map(|n| n.text.as_str()).collect(); assert!(said.iter().all(|n| !n.contains("ignores")), "{said:?}"); } + +#[test] +fn notebook_output_keeps_no_login_binding() { + let origin = "https://notebook757.example.org"; + let mut s = session(None); + let id = new(&mut s, json!({"kind":"wmts","address":format!("{origin}/tiles"),"auth":"header","keyName":"X-Key"})); + s.run("connection.login", &json!({"id":id,"apiKey":"notebook-sentinel-757"})).unwrap(); + s.run("map.addTiles", &json!({"url":format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png"),"name":"T","connection":id})).unwrap(); + assert!(s.map().tiles[0].url.starts_with(rustgis_data::login_address::PREFIX)); + s.run("notebook.new", &json!({"name": "Tiles"})).unwrap(); + s.run("notebook.addCell", &json!({"source": "map.tiles"})).unwrap(); + s.run("notebook.run", &Value::Null).unwrap(); + let output = s.project.notebooks[0].cells.iter().filter_map(|c| c.output.clone()).collect::>().join("\n"); + assert!(output.contains(&format!("{origin}/tiles/")), "the cell still shows the address: {output}"); + let (saved, _) = s.project.clone().saved_text(None).unwrap(); + assert!(!saved.contains(rustgis_data::login_address::PREFIX), "the saved project keeps a binding: {saved}"); +} From db5f46586fc2372856c181acb753bd410e380c0f Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Tue, 3 Nov 2026 23:18:08 +0100 Subject: [PATCH 17/19] fix(connections): a plugin can't leave a legend for a later login to bind A plain legend a plugin set on a layer recording a connection with a login was bound at that connection's next login (`rebind_connection` rewrites every legend of its layers), so the plugin picked a path the user's login was then sent to. `map.setTiles` now refuses, while reads are anonymous, a legend at the origin of such a connection; one at another origin, or for a connection without a login, is still taken and stays plain. docs: fetch_bytes's comment re-wrapped. --- apps/rustgis-web/src/web.rs | 8 ++++---- crates/engine/src/service_logins.rs | 20 ++++++++++++++------ crates/engine/src/tests/service_logins.rs | 18 +++++++++++++----- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/apps/rustgis-web/src/web.rs b/apps/rustgis-web/src/web.rs index 3f65e60284..c02f452dcb 100644 --- a/apps/rustgis-web/src/web.rs +++ b/apps/rustgis-web/src/web.rs @@ -571,10 +571,10 @@ async fn unless_stalled( } /// Downloads `url`, or the byte range it asks for (a PMTiles archive's part: see -/// `rustgis_tiles::cache::split_range`). -/// A status in `omit` is an expected answer (a tile past a map's edge): its failure isn't -/// recorded, only noted once per server, as on the desktop. A request carrying a login is recorded by its plain -/// address and scrubbed text ([`RequestDiagnostics::failed`]), never its binding or credential. +/// `rustgis_tiles::cache::split_range`). A status in `omit` is an expected answer (a tile past a +/// map's edge): its failure isn't recorded, only noted once per server, as on the desktop. A +/// request carrying a login is recorded by its plain address and scrubbed text +/// ([`RequestDiagnostics::failed`]), never its binding or credential. async fn fetch_bytes(url: &str, capture: Capture, omit: &'static [u16]) -> Result, String> { use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{cache::RequestDiagnostics, failure::Failure}; let mut diagnostics = RequestDiagnostics::with_capture(url, "GET", capture); diff --git a/crates/engine/src/service_logins.rs b/crates/engine/src/service_logins.rs index fba32d8bfd..f916717818 100644 --- a/crates/engine/src/service_logins.rs +++ b/crates/engine/src/service_logins.rs @@ -247,19 +247,27 @@ pub fn logged_in(s: &Session, c: &Connection) -> bool { /// A plain address at the origin of a logged-in service connection, bound to that login. Anything /// else (another origin, a wrapper, a connection not logged in) comes back unchanged. An address -/// at that origin that can't carry the login is refused, never read without it. +/// at that origin that can't carry the login is refused, never read without it; so is one a +/// plugin sets there. pub(crate) fn bind_logged_in(s: &Session, connection: &str, url: &str) -> Result { - // A plugin's command reads anonymously: a legend it sets never takes the user's login. - if s.web_logins.is_anonymous() { - return Ok(url.to_string()); - } let Some(c) = s.connections.connections.iter().find(|c| c.id == connection && is_service(c)) else { return Ok(url.to_string()) }; let Some(origin) = login_origin(c) else { return Ok(url.to_string()) }; // At the origin by its text too: an address that doesn't parse (a space in it) still names it. let named = url.get(..origin.len()).is_some_and(|start| start.eq_ignore_ascii_case(&origin)) && url.get(origin.len()..).is_some_and(|rest| rest.is_empty() || rest.starts_with(['/', '?', '#'])); let at_origin = named || logins::origin(url).as_deref() == Some(origin.as_str()); - if !logged_in(s, c) || !at_origin || rustgis_data::login_address::is_wrapper(url) { + if !at_origin || rustgis_data::login_address::is_wrapper(url) { + return Ok(url.to_string()); + } + // A plugin's command reads anonymously, and a legend it left here would be bound at the + // connection's next login: refused, as naming the connection is (a public one lends none). + if s.web_logins.is_anonymous() { + if c.auth.is_empty() { + return Ok(url.to_string()); + } + return Err(format!("plugins can't set a legend at the address of a web-service connection with a login ('{}')", c.name)); + } + if !logged_in(s, c) { return Ok(url.to_string()); } let Some(active) = s.web_logins.active.get(&c.id) else { return Ok(url.to_string()) }; diff --git a/crates/engine/src/tests/service_logins.rs b/crates/engine/src/tests/service_logins.rs index cafcff5873..30ccf488e9 100644 --- a/crates/engine/src/tests/service_logins.rs +++ b/crates/engine/src/tests/service_logins.rs @@ -514,13 +514,21 @@ fn plugins_cannot_send_a_saved_web_service_login() { s.run("map.addTiles", &json!({"url":tiles,"name":"Mine"})).unwrap(); let mine = s.map().tiles.iter().find(|t| t.name == "Mine").unwrap(); assert_eq!(header(&mine.url).unwrap().1, "plugin-sentinel-757"); - // A legend it sets on the user's logged-in layer stays plain: the layer's login isn't lent. + // A legend it sets at the user's logged-in layer's origin is refused: the next login would + // bind it, and the plugin would have picked where the login goes. s.run("map.addTiles", &json!({"url":tiles,"name":"Named","connection":id})).unwrap(); - let legend = format!("{origin}/legend.png"); - super::forward(&mut s, "eng-login-757", "map.setTiles", json!({"name":"Named","legend":legend})).unwrap(); + let e = super::forward(&mut s, "eng-login-757", "map.setTiles", json!({"name":"Named","legend":format!("{origin}/legend.png")})) + .unwrap_err(); + assert!(e.contains("plugins can't set a legend"), "{e}"); let named = s.map().tiles.iter().find(|t| t.name == "Named").unwrap(); - assert_eq!(named.connection, id, "the layer records the connection, so its legend could be bound"); - assert_eq!(named.legends[0].url, legend); + assert_eq!(named.connection, id, "the layer records the connection, so a legend there would be bound"); + assert!(named.legends.is_empty()); + // One at another origin is its own, and stays plain when the user logs in again. + let elsewhere = "https://plugin-other757.example.org/legend.png"; + super::forward(&mut s, "eng-login-757", "map.setTiles", json!({"name":"Named","legend":elsewhere})).unwrap(); + s.run("connection.login", &json!({"id":id,"apiKey":"plugin-sentinel-757"})).unwrap(); + let named = s.map().tiles.iter().find(|t| t.name == "Named").unwrap(); + assert_eq!(named.legends[0].url, elsewhere); rustgis_plugins::unload("eng-login-757"); } From 2e1169acf8c1cd4dc21b3412cff71fc07f365514 Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Wed, 4 Nov 2026 08:17:32 +0100 Subject: [PATCH 18/19] fix(connections): keep plugin layers anonymous across later logins --- crates/engine/src/cmd/layer.rs | 4 +- crates/engine/src/cmd/plugins.rs | 14 +- crates/engine/src/lib.rs | 27 +++- crates/engine/src/service_logins.rs | 104 +++++++++++---- crates/engine/src/tests/service_logins.rs | 155 +++++++++++++++++++++- crates/map/src/lib.rs | 5 + crates/map/src/tiles.rs | 5 + crates/ui-egui/src/lib.rs | 49 ++++++- docs/user-guide/data.md | 5 + 9 files changed, 328 insertions(+), 40 deletions(-) diff --git a/crates/engine/src/cmd/layer.rs b/crates/engine/src/cmd/layer.rs index 1e9ed3f74d..f656578445 100644 --- a/crates/engine/src/cmd/layer.rs +++ b/crates/engine/src/cmd/layer.rs @@ -40,10 +40,10 @@ pub fn commands() -> Vec { .params(r#"{"layer"?: name|id, "crs": "EPSG:… or WKT; reinterpret native coordinates, without rewriting the source file"}"#), C::new("layer.loadAll", "Load All Features", "Layers", load_all).when(need_streamed).params(r#"{"layer"?: name|id}"#), C::new("layer.addUrl", "Add Data From URL", "Home › Add › Add Data", add_url).params( - r#"{"url": "https://…/data.geojson|.csv|.parquet|.fgb|.gpkg|.zip|.kml|.kmz|.gpx|.tif|.jp2 (JPEG 2000: desktop, through GDAL) (the format from the address, else the contents) | a .pmtiles archive", "name"?: text, "layer"?: "one layer of a zip, KML or GPX", "zoom"?: "bool: true zooms to what was added; false keeps the view of a GeoParquet or FlatGeobuf read in place (zoomed to only when the map was empty)", "x"?, "y"?, "wkt"?, "crs"?, "delimiter"?, "paperSpace"?, "stream"?, "as"? (as layer.addData), "whole"?: "bool: download a cloud-optimised GeoTIFF whole (as a raster layer) instead of reading it in place (an address without a file name is read in place when its first bytes are a cloud-optimised GeoTIFF)", "cog"?: "bool: the address is a cloud-optimised GeoTIFF whatever its name says (the app's probe found it so)", "bbox"?: "[west, south, east, north] in degrees | "view": a cloud-optimised GeoTIFF's window as a raster layer (desktop and command line), read by range at full resolution, in the file's coordinate system, with its colour table and class names; at most 64 million values (all bands)", "cellSize"?: "with bbox: the coarsest overview whose cells are at most this size is read (the file's units; metres for longitude/latitude, a degree taken as 111.32 km)", "start"?, "end"?, "unit"?, "interval"?, "dates"?: "a GeoTIFF address with date placeholders ({date:YYYY}, {YYYY}{MM}{DD}), one file per date: the dates it spans and steps through (as time.set), turning the time slider on", "connection"?: "the id of the saved web-service connection it comes from (as service.add)"}"#, + r#"{"url": "https://…/data.geojson|.csv|.parquet|.fgb|.gpkg|.zip|.kml|.kmz|.gpx|.tif|.jp2 (JPEG 2000: desktop, through GDAL) (the format from the address, else the contents) | a .pmtiles archive", "name"?: text, "layer"?: "one layer of a zip, KML or GPX", "zoom"?: "bool: true zooms to what was added; false keeps the view of a GeoParquet or FlatGeobuf read in place (zoomed to only when the map was empty)", "x"?, "y"?, "wkt"?, "crs"?, "delimiter"?, "paperSpace"?, "stream"?, "as"? (as layer.addData), "whole"?: "bool: download a cloud-optimised GeoTIFF whole (as a raster layer) instead of reading it in place (an address without a file name is read in place when its first bytes are a cloud-optimised GeoTIFF)", "cog"?: "bool: the address is a cloud-optimised GeoTIFF whatever its name says (the app's probe found it so)", "bbox"?: "[west, south, east, north] in degrees | "view": a cloud-optimised GeoTIFF's window as a raster layer (desktop and command line), read by range at full resolution, in the file's coordinate system, with its colour table and class names; at most 64 million values (all bands)", "cellSize"?: "with bbox: the coarsest overview whose cells are at most this size is read (the file's units; metres for longitude/latitude, a degree taken as 111.32 km)", "start"?, "end"?, "unit"?, "interval"?, "dates"?: "a GeoTIFF address with date placeholders ({date:YYYY}, {YYYY}{MM}{DD}), one file per date: the dates it spans and steps through (as time.set), turning the time slider on", "connection"?: "the id of the saved web-service connection it comes from (as service.add)", "anonymous"?: bool (internal: a plugin-origin download must never adopt a saved login)}"#, ), C::new("layer.addDownload", "Add Downloaded Data", "Home › Add", add_download) - .params(r#"{"id": a download the app finished (Home › Add › Add Data › From URL), "url": its address, "name"?: text, "layer"?: text, "zoom"?: bool, "x"?, "y"?, "wkt"?, "crs"?, "delimiter"?, "paperSpace"?, "stream"?, "as"? (as layer.addData), "service"?: bool (a service's features: their axes and paging are checked, and the address is kept as the layer's source), "bounds"?: [west, south, east, north] (with service: the layer's longitude/latitude bounds from its capabilities, which its axes are checked against; without bounds they are left as they come; ignored without service), "coverage"?: bool (a WCS coverage: a GeoTIFF, else the service's error, and its request kept as the layer's source), "geolibreProject"?: bool (internal: downloaded project JSON), "geolibreLayer"?: {"token", "record"} (internal: reserved imported layer and its saved style), "geolibreZarr"?: bool (internal: an imported Zarr slice as GeoTIFF), "connection"?: "as layer.addUrl"}"#), + .params(r#"{"id": a download the app finished (Home › Add › Add Data › From URL), "url": its address, "name"?: text, "layer"?: text, "zoom"?: bool, "x"?, "y"?, "wkt"?, "crs"?, "delimiter"?, "paperSpace"?, "stream"?, "as"? (as layer.addData), "service"?: bool (a service's features: their axes and paging are checked, and the address is kept as the layer's source), "bounds"?: [west, south, east, north] (with service: the layer's longitude/latitude bounds from its capabilities, which its axes are checked against; without bounds they are left as they come; ignored without service), "coverage"?: bool (a WCS coverage: a GeoTIFF, else the service's error, and its request kept as the layer's source), "geolibreProject"?: bool (internal: downloaded project JSON), "geolibreLayer"?: {"token", "record"} (internal: reserved imported layer and its saved style), "geolibreZarr"?: bool (internal: an imported Zarr slice as GeoTIFF), "connection"?: "as layer.addUrl", "anonymous"?: bool (internal: preserves a plugin-origin download's anonymous reads)}"#), C::new("layer.addEncodedPolyline", "Add Encoded Polyline", "Home › Add", add_encoded_polyline).params( r#"{"polyline": "encoded polyline text" | [texts] (one line each), "precision"?: 5 (Google) | 6 (OSRM, Valhalla), "name"?: text}"#, ), diff --git a/crates/engine/src/cmd/plugins.rs b/crates/engine/src/cmd/plugins.rs index 3bca097f54..31e94293b2 100644 --- a/crates/engine/src/cmd/plugins.rs +++ b/crates/engine/src/cmd/plugins.rs @@ -560,8 +560,18 @@ impl Bridge<'_> { self.s.request_only = false; // No saved web-service login goes with what a plugin asks for: the user chooses where it is sent. let anonymous = self.s.web_logins.set_anonymous(true); - let mut res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run(self.s, params))) - .unwrap_or_else(|_| Err(format!("{label} failed unexpectedly"))); + let mut res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + let before = if mutates { crate::service_logins::before(self.s, id, params)? } else { None }; + let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run(self.s, params))) + .unwrap_or_else(|_| Err(format!("{label} failed unexpectedly"))); + // Mark additions even when a command reports an error after changing the project. + // Their addresses must remain anonymous at later logins and after saving/reopening. + if let Some(before) = before { + crate::service_logins::tag(self.s, params, before, out.as_ref().unwrap_or(&Value::Null))?; + } + out + })) + .unwrap_or_else(|_| Err(format!("{label} failed unexpectedly"))); self.s.web_logins.set_anonymous(anonymous); let requested = std::mem::take(&mut self.s.request_only); self.requested |= requested; diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 93fa041b96..6d4a7dd125 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -1015,11 +1015,17 @@ impl Session { /// requests the next is refused rather than silently dropping one already promised. pub fn request_url(&mut self, p: &serde_json::Value) -> Result { let url = p.get("url").and_then(serde_json::Value::as_str).unwrap_or("").to_string(); - if !self.url_requests.iter().any(|q| q == p) { + // The front end runs the queued request after the plugin's anonymous scope has ended. + // Keep its opt-out through probes, whole-file fallbacks and the eventual addDownload. + let mut p = std::borrow::Cow::Borrowed(p); + if self.web_logins.is_anonymous() { + p.to_mut().as_object_mut().ok_or("download parameters must be an object")?.insert("anonymous".into(), serde_json::json!(true)); + } + if !self.url_requests.iter().any(|q| q == p.as_ref()) { if self.url_requests.len() >= MAX_URL_REQUESTS { return Err(format!("{url}: {MAX_URL_REQUESTS} downloads are already waiting; try again when they have landed")); } - self.url_requests.push(p.clone()); + self.url_requests.push(p.into_owned()); } self.request_only = true; Ok(serde_json::json!({"pending": true, "url": url})) @@ -1200,12 +1206,19 @@ impl Session { self.merge = merge; // A layer added from a saved web-service connection records it, inside the command's step. let res = self.guarded(label, mutates, |s| { - let before = service_logins::before(s, id, params)?; - let out = run(s, params)?; - if let Some(before) = before { - service_logins::tag(s, params, before, &out)?; + let add = |s: &mut Session| { + let before = service_logins::before(s, id, params)?; + let out = run(s, params)?; + if let Some(before) = before { + service_logins::tag(s, params, before, &out)?; + } + Ok(out) + }; + if matches!(id, "layer.addUrl" | "layer.addDownload") && params.get("anonymous").and_then(Value::as_bool) == Some(true) { + service_logins::anonymously(s, true, add).unwrap_or_else(|_| Err(format!("{label} failed unexpectedly"))) + } else { + add(s) } - Ok(out) }); if id == "layer.addDownload" && res.is_ok() diff --git a/crates/engine/src/service_logins.rs b/crates/engine/src/service_logins.rs index f916717818..7f1d52b978 100644 --- a/crates/engine/src/service_logins.rs +++ b/crates/engine/src/service_logins.rs @@ -259,13 +259,10 @@ pub(crate) fn bind_logged_in(s: &Session, connection: &str, url: &str) -> Result if !at_origin || rustgis_data::login_address::is_wrapper(url) { return Ok(url.to_string()); } - // A plugin's command reads anonymously, and a legend it left here would be bound at the - // connection's next login: refused, as naming the connection is (a public one lends none). + // Even a public connection can gain a login later. A plugin must not leave a legend for + // that login to bind. if s.web_logins.is_anonymous() { - if c.auth.is_empty() { - return Ok(url.to_string()); - } - return Err(format!("plugins can't set a legend at the address of a web-service connection with a login ('{}')", c.name)); + return Err(format!("plugins can't set a legend at the address of a saved web-service connection ('{}')", c.name)); } if !logged_in(s, c) { return Ok(url.to_string()); @@ -357,7 +354,7 @@ pub fn request_params<'a>(s: &mut Session, p: &'a Value) -> Result Some(id.as_str()), Some(_) => return Err("connection is a saved web-service connection id.".into()), }; - if id.is_none() && s.web_logins.anonymous { + if id.is_none() && (s.web_logins.anonymous || p.get("anonymous").and_then(Value::as_bool) == Some(true)) { // An address bound to a login is no authority for it here either. let plain = logins::inner(url); if plain == url { @@ -425,7 +422,7 @@ fn rebind_connection(s: &mut Session, c: &Connection) { } }; for m in &mut s.project.maps { - for t in m.tiles.iter_mut().filter(|t| t.connection == c.id) { + for t in m.tiles.iter_mut().filter(|t| !t.anonymous && t.connection == c.id) { rewrite(&mut t.url); if let Some(url) = &mut t.time_url { rewrite(url); @@ -440,7 +437,7 @@ fn rebind_connection(s: &mut Session, c: &Connection) { rewrite(&mut info.url); } } - for l in m.layers.iter_mut().filter(|l| l.connection == c.id) { + for l in m.layers.iter_mut().filter(|l| !l.anonymous && l.connection == c.id) { if let Some(source) = l.source.as_mut() && let Some(path) = source.path.as_mut() { @@ -483,7 +480,7 @@ fn rebind_connection(s: &mut Session, c: &Connection) { fn record_choice(s: &mut Session, origin: &str, old: &str, c: &Connection) { for m in &mut s.project.maps { for t in &mut m.tiles { - if t.connection == old && logins::origin(tile_address(&t.url)).as_deref() == Some(origin) { + if !t.anonymous && t.connection == old && logins::origin(tile_address(&t.url)).as_deref() == Some(origin) { t.connection = c.id.clone(); } } @@ -494,7 +491,9 @@ fn record_choice(s: &mut Session, origin: &str, old: &str, c: &Connection) { .flat_map(|g| g.children.iter().map(|child| child.layer)) .collect(); for l in &mut m.layers { - if l.connection == old && (layer_address(l).and_then(logins::origin).as_deref() == Some(origin) || group_layers.contains(&l.id)) + if !l.anonymous + && l.connection == old + && (layer_address(l).and_then(logins::origin).as_deref() == Some(origin) || group_layers.contains(&l.id)) { l.connection = c.id.clone(); } @@ -516,7 +515,7 @@ struct Reads { fn reads(s: &Session) -> BTreeMap<(String, String), Reads> { let mut out = BTreeMap::<(String, String), Reads>::new(); for (map_index, m) in s.project.maps.iter().enumerate() { - for t in &m.tiles { + for t in m.tiles.iter().filter(|t| !t.anonymous) { let Some(origin) = logins::origin(tile_address(&t.url)) else { continue }; let r = out.entry((origin, t.connection.clone())).or_default(); r.tiles += 1; @@ -524,7 +523,7 @@ fn reads(s: &Session) -> BTreeMap<(String, String), Reads> { r.recorded.insert(t.connection.clone()); } } - for l in &m.layers { + for l in m.layers.iter().filter(|l| !l.anonymous) { let Some(origin) = layer_address(l).and_then(logins::origin) else { continue }; let r = out.entry((origin, l.connection.clone())).or_default(); if r.layer_keys.insert((map_index, l.id)) { @@ -539,7 +538,11 @@ fn reads(s: &Session) -> BTreeMap<(String, String), Reads> { for g in &m.service_groups { let Some(origin) = logins::origin(&g.url) else { continue }; for child in &g.children { - let id = m.layers.iter().find(|l| l.id == child.layer).map(|l| l.connection.as_str()).unwrap_or(""); + let layer = m.layers.iter().find(|l| l.id == child.layer); + if layer.is_some_and(|l| l.anonymous) { + continue; + } + let id = layer.map(|l| l.connection.as_str()).unwrap_or(""); let r = out.entry((origin.clone(), id.to_string())).or_default(); if r.layer_keys.insert((map_index, child.layer)) { r.layers.push(child.layer); @@ -559,6 +562,11 @@ fn reads(s: &Session) -> BTreeMap<(String, String), Reads> { /// recorded connection this computer doesn't have are asked about ([`LoginRequest`]), never /// guessed. pub(crate) fn resolve(s: &mut Session, only: Option<&BTreeSet>) { + // Imports and nested commands a plugin runs must not adopt a login before their additions + // have been marked anonymous at the plugin boundary. + if s.web_logins.is_anonymous() { + return; + } let mut pending = Vec::new(); let mut touched = BTreeSet::new(); for ((origin, recorded_id), r) in reads(s) { @@ -712,11 +720,11 @@ fn ask(s: &mut Session, request: LoginRequest) { s.database_logins.request(request); } -/// What the project held before an add command: its layers by id and its tile layers, so the +/// What the project held before an add command: its layers by live identity and its tile layers, so the /// ones the command added can be told apart. pub(crate) struct Before { - layers: Vec>, - tiles: Vec>, + layers: Vec>, + tiles: Vec)>>, } /// The connection an add command names (its `connection`), checked before it runs: a saved @@ -743,21 +751,39 @@ fn named(s: &Session, params: &Value) -> Result, String> { /// What to compare after command `id` runs, if it adds layers; an error for a `connection` that /// isn't a saved web-service connection's id. pub(crate) fn before(s: &Session, id: &str, params: &Value) -> Result, String> { - if !ADD_COMMANDS.contains(&id) { + let anonymous = s.web_logins.is_anonymous(); + if !anonymous && !ADD_COMMANDS.contains(&id) { return Ok(None); } - named(s, params)?; + if !anonymous { + named(s, params)?; + } Ok(Some(Before { - layers: s.project.maps.iter().map(|m| m.layers.iter().map(|l| l.id).collect()).collect(), - tiles: s.project.maps.iter().map(|m| m.tiles.iter().map(|t| (t.url.clone(), t.name.clone())).collect()).collect(), + layers: s.project.maps.iter().map(|m| m.layers.iter().map(|l| l.instance_id()).collect()).collect(), + tiles: s + .project + .maps + .iter() + .map(|m| { + m.tiles + .iter() + .map(|t| { + let legends = if anonymous { t.legends.iter().map(|legend| legend.url.clone()).collect() } else { Vec::new() }; + (t.url.clone(), t.name.clone(), legends) + }) + .collect() + }) + .collect(), })) } /// Records on the layers an add command made the connection they came from: the one it names /// (`connection`) for those read from its origin or holding no address, else the one whose login /// their origin is read with. Then logs in for their origins as a project's layers do. +/// Anonymous plugin additions instead keep a persisted opt-out from all later login adoption. pub(crate) fn tag(s: &mut Session, params: &Value, before: Before, out: &Value) -> Result<(), String> { - let named = named(s, params)?; + let anonymous = s.web_logins.is_anonymous(); + let named = if anonymous { None } else { named(s, params)? }; let named_origin = named.as_ref().and_then(login_origin); let mut origins = BTreeSet::new(); // The connection a layer of `origin` (none: it holds no address) records. @@ -776,12 +802,26 @@ pub(crate) fn tag(s: &mut Session, params: &Value, before: Before, out: &Value) (None, None) => None, } }; + let mut old_tiles = before.tiles.into_iter(); for (i, m) in s.project.maps.iter_mut().enumerate() { - let mut old = before.tiles.get(i).cloned().unwrap_or_default(); + let mut old = old_tiles.next().unwrap_or_default(); for t in &mut m.tiles { // Tile layers have no id: those not there before (each old one matched once). - if let Some(at) = old.iter().position(|(url, name)| *url == t.url && *name == t.name) { - old.swap_remove(at); + if let Some(at) = old.iter().position(|(url, name, _)| *url == t.url && (anonymous || *name == t.name)) { + let (_, _, legends) = old.swap_remove(at); + // An unconnected user layer may be adopted later too. A plugin changing its + // legends must not leave a path for that future login to bind. + if anonymous + && t.connection.is_empty() + && !legends.iter().map(String::as_str).eq(t.legends.iter().map(|legend| legend.url.as_str())) + { + t.anonymous = true; + } + continue; + } + if anonymous { + t.anonymous = true; + t.connection.clear(); continue; } let origin = logins::origin(tile_address(&t.url)); @@ -792,8 +832,13 @@ pub(crate) fn tag(s: &mut Session, params: &Value, before: Before, out: &Value) } let old_layers = before.layers.get(i); let mut added = BTreeSet::new(); - for l in m.layers.iter_mut().filter(|l| old_layers.is_none_or(|old| !old.contains(&l.id))) { + for l in m.layers.iter_mut().filter(|l| old_layers.is_none_or(|old| !old.contains(&l.instance_id()))) { added.insert(l.id); + if anonymous { + l.anonymous = true; + l.connection.clear(); + continue; + } let origin = layer_address(l).or(l.dataset.source.as_deref()).and_then(logins::origin); if let Some(id) = pick(origin.as_ref()).filter(|_| named.is_some() || l.connection.is_empty()) { l.connection = id; @@ -803,7 +848,7 @@ pub(crate) fn tag(s: &mut Session, params: &Value, before: Before, out: &Value) } } // An imported service's group is refreshed from its address later. - for g in m.service_groups.iter().filter(|g| g.children.iter().any(|c| added.contains(&c.layer))) { + for g in m.service_groups.iter().filter(|g| !anonymous && g.children.iter().any(|c| added.contains(&c.layer))) { origins.extend(logins::origin(&g.url)); } } @@ -814,6 +859,11 @@ pub(crate) fn tag(s: &mut Session, params: &Value, before: Before, out: &Value) && tile_kinds.contains(&kind) && let Some(t) = usize::try_from(at).ok().and_then(|at| s.project.map_mut().tiles.get_mut(at)) { + if anonymous { + t.anonymous = true; + t.connection.clear(); + return Ok(()); + } let origin = logins::origin(tile_address(&t.url)); if let Some(id) = pick(origin.as_ref()) { t.connection = id; diff --git a/crates/engine/src/tests/service_logins.rs b/crates/engine/src/tests/service_logins.rs index 30ccf488e9..c57d7839ab 100644 --- a/crates/engine/src/tests/service_logins.rs +++ b/crates/engine/src/tests/service_logins.rs @@ -508,7 +508,13 @@ fn plugins_cannot_send_a_saved_web_service_login() { } assert!(s.map().tiles.is_empty()); // Not naming it, the only one at the origin isn't used for it either; the user's own command is. - super::forward(&mut s, "eng-login-757", "map.addTiles", json!({"url":tiles})).unwrap(); + super::forward( + &mut s, + "eng-login-757", + "map.addTiles", + json!({"url":format!("{origin}/plugin/{{z}}/{{x}}/{{y}}.png"),"name":"Plugin"}), + ) + .unwrap(); assert!(header(&s.map().tiles[0].url).is_none()); assert!(s.map().tiles[0].connection.is_empty()); s.run("map.addTiles", &json!({"url":tiles,"name":"Mine"})).unwrap(); @@ -529,9 +535,156 @@ fn plugins_cannot_send_a_saved_web_service_login() { s.run("connection.login", &json!({"id":id,"apiKey":"plugin-sentinel-757"})).unwrap(); let named = s.map().tiles.iter().find(|t| t.name == "Named").unwrap(); assert_eq!(named.legends[0].url, elsewhere); + let plugin_tile = s.map().tiles.iter().find(|t| t.name == "Plugin").unwrap(); + assert!(header(&plugin_tile.url).is_none(), "a later login must not adopt the plugin's tile"); + assert!(plugin_tile.connection.is_empty()); + assert_eq!(header(&named.url).unwrap().1, "plugin-sentinel-757", "the user's layer still uses its login"); + let (saved, _) = s.project.clone().saved_text(None).unwrap(); + s.run("project.openText", &json!({"text":saved})).unwrap(); + s.run("connection.login", &json!({"id":id,"apiKey":"plugin-sentinel-757"})).unwrap(); + let plugin_tile = s.map().tiles.iter().find(|t| t.name == "Plugin").unwrap(); + assert!(header(&plugin_tile.url).is_none(), "saving and reopening must preserve anonymous reads"); + assert!(plugin_tile.connection.is_empty()); rustgis_plugins::unload("eng-login-757"); } +#[test] +fn plugin_legends_are_refused_before_a_public_connection_gains_a_login() { + super::isolate_plugins(); + let mut s = session(None); + let origin = "https://public-plugin757.example.org"; + let id = new(&mut s, json!({"kind":"wms","address":format!("{origin}/wms"),"auth":"none"})); + s.run("map.addTiles", &json!({"url":format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png"),"name":"Public","connection":id})).unwrap(); + super::plugin("eng-public-login-757", &["edit", "network", "files"]); + let error = super::forward( + &mut s, + "eng-public-login-757", + "map.setTiles", + json!({"name":"Public","legend":format!("{origin}/chosen-by-plugin.png")}), + ) + .unwrap_err(); + assert!(error.contains("plugins can't set a legend"), "{error}"); + s.run("connection.edit", &json!({"id":id,"auth":"header","keyName":"X-Key"})).unwrap(); + s.run("connection.login", &json!({"id":id,"apiKey":"later-sentinel-757"})).unwrap(); + let tile = s.map().tiles.iter().find(|t| t.name == "Public").unwrap(); + assert!(tile.legends.is_empty(), "no plugin path was stored for the new login to bind"); + assert_eq!(header(&tile.url).unwrap().1, "later-sentinel-757"); + rustgis_plugins::unload("eng-public-login-757"); +} + +#[test] +fn plugin_imports_never_acquire_a_later_connections_login() { + super::isolate_plugins(); + let mut s = session(None); + let origin = "https://import-plugin757.example.org"; + let mut imported = Project::default().map().clone(); + imported.name = "Plugin imports".into(); + let mut tile = rustgis_map::TileLayer::new("Dates", rustgis_map::TileKind::Raster, format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png")); + tile.time_url = Some(format!("{origin}/{{date:YYYY}}.png")); + tile.time_urls = vec![(1.0, format!("{origin}/dated.png"))]; + tile.legends = vec![rustgis_data::ogc::Legend { url: format!("{origin}/legend.png"), ..Default::default() }]; + imported.tiles.push(tile); + let mut layer = rustgis_map::Layer::new(1, rustgis_data::Dataset::new("Remote", rustgis_data::GeometryType::None, Vec::new())); + layer.dataset.source = Some(format!("{origin}/data.geojson")); + layer.source = Some(serde_json::from_value(json!({"path":format!("{origin}/source.fgb")})).unwrap()); + layer.raster = Some(serde_json::from_value(json!({"path":format!("{origin}/raster.tif"),"style":{"renderer":"Colormap"}})).unwrap()); + imported.layers.push(layer); + super::plugin("eng-import-login-757", &["edit", "network", "files"]); + super::forward(&mut s, "eng-import-login-757", "map.import", json!({"text":serde_json::to_string(&imported).unwrap()})).unwrap(); + // Identify metadata is runtime-only; it belongs to the same anonymous tile. + s.project.map_mut().tiles[0].feature_info = + Some(rustgis_data::ogc::FeatureInfo { url: format!("{origin}/identify"), ..Default::default() }); + let id = new(&mut s, json!({"kind":"wms","address":format!("{origin}/wms"),"auth":"header","keyName":"X-Key"})); + for reopened in [false, true] { + if reopened { + let (text, _) = s.project.clone().saved_text(None).unwrap(); + s.run("project.openText", &json!({"text":text})).unwrap(); + } + s.run("connection.login", &json!({"id":id,"apiKey":"import-sentinel-757"})).unwrap(); + let map = s.map(); + let tile = &map.tiles[0]; + assert!(tile.connection.is_empty()); + for address in std::iter::once(tile.url.as_str()) + .chain(tile.time_url.as_deref()) + .chain(tile.time_urls.iter().map(|(_, url)| url.as_str())) + .chain(tile.legends.iter().map(|legend| legend.url.as_str())) + .chain(tile.feature_info.as_ref().map(|info| info.url.as_str())) + { + assert!(header(address).is_none(), "plugin tile acquired a login: {address}"); + } + let layer = &map.layers[0]; + assert!(layer.connection.is_empty()); + for address in [ + layer.dataset.source.as_deref().unwrap(), + layer.source.as_ref().unwrap().path.as_deref().unwrap(), + layer.raster.as_ref().unwrap().path.as_deref().unwrap(), + ] { + assert!(header(address).is_none(), "plugin layer acquired a login: {address}"); + } + } + rustgis_plugins::unload("eng-import-login-757"); +} + +#[test] +fn plugin_downloads_stay_anonymous_after_the_front_end_adds_them() { + super::isolate_plugins(); + let mut s = session(None); + let origin = "https://deferred-plugin757.example.org"; + let id = new(&mut s, json!({"kind":"wms","address":format!("{origin}/wms"),"auth":"header","keyName":"X-Key"})); + s.run("connection.login", &json!({"id":id,"apiKey":"deferred-sentinel-757"})).unwrap(); + super::plugin("eng-deferred-login-757", &["edit", "network", "files"]); + s.run_leaving_downloads( + "plugin:eng-deferred-login-757/f", + &json!({ + "op":"layer.addUrl","params":{"url":format!("{origin}/roads.geojson"),"name":"Deferred","anonymous":false} + }), + ) + .unwrap(); + let mut pending = s.take_url_requests().pop().unwrap(); + // ui.addUrl binds/probes after the plugin has returned. It must still fetch anonymously. + let prepared = web::request_params(&mut s, &pending).unwrap().into_owned(); + assert!(header(prepared["url"].as_str().unwrap()).is_none()); + let token = s.download_started(); + assert!(s.download_landed(token, br#"{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"properties":{}}]}"#.to_vec())); + pending["id"] = json!(token); + pending["service"] = json!(true); + s.run("layer.addDownload", &pending).unwrap(); + s.run("layer.addUrl", &json!({"url":format!("{origin}/deferred.pmtiles"),"anonymous":true,"name":"Archive"})).unwrap(); + s.run("connection.login", &json!({"id":id,"apiKey":"deferred-sentinel-757"})).unwrap(); + let layer = s.map().layers.iter().find(|l| l.name == "Deferred").unwrap(); + assert!(layer.connection.is_empty()); + assert!(header(layer.dataset.source.as_deref().unwrap()).is_none()); + let tile = s.map().tiles.iter().find(|t| t.name == "Archive").unwrap(); + assert!(tile.connection.is_empty()); + assert!(header(&tile.url).is_none()); + rustgis_plugins::unload("eng-deferred-login-757"); +} + +#[test] +fn plugin_legends_on_unconnected_layers_never_acquire_a_login() { + super::isolate_plugins(); + let mut s = session(None); + let origin = "https://unconnected-plugin757.example.org"; + s.run("map.addTiles", &json!({"url":format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png"),"name":"Unconnected"})).unwrap(); + super::plugin("eng-unconnected-login-757", &["edit", "network", "files"]); + super::forward( + &mut s, + "eng-unconnected-login-757", + "map.setTiles", + json!({ + "name":"Unconnected","legend":format!("{origin}/plugin-legend.png") + }), + ) + .unwrap(); + let id = new(&mut s, json!({"kind":"wms","address":format!("{origin}/wms"),"auth":"header","keyName":"X-Key"})); + s.run("connection.login", &json!({"id":id,"apiKey":"unconnected-sentinel-757"})).unwrap(); + let tile = s.map().tiles.iter().find(|t| t.name == "Unconnected").unwrap(); + assert!(tile.connection.is_empty()); + assert!(header(&tile.url).is_none()); + assert!(header(&tile.legends[0].url).is_none()); + rustgis_plugins::unload("eng-unconnected-login-757"); +} + #[test] fn only_web_services_compare_a_test_with_their_saved_definition() { let mut s = session(None); diff --git a/crates/map/src/lib.rs b/crates/map/src/lib.rs index d39bef233f..ee4a269127 100644 --- a/crates/map/src/lib.rs +++ b/crates/map/src/lib.rs @@ -217,6 +217,10 @@ pub struct Layer { /// connection. Never a secret. #[serde(default, skip_serializing_if = "String::is_empty")] pub connection: String, + /// Added by an anonymous plugin command: never adopt a saved web-service login, including + /// after saving and reopening the project. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub anonymous: bool, /// Process-local identity, preserved by snapshots but never stored in a project. #[serde(skip, default = "next_layer_instance")] instance_id: u64, @@ -410,6 +414,7 @@ impl Layer { postgis: None, gpkg: None, connection: String::new(), + anonymous: false, projected: Default::default(), display: None, display_rejected: Vec::new(), diff --git a/crates/map/src/tiles.rs b/crates/map/src/tiles.rs index 3b7ca1e00c..4932d425db 100644 --- a/crates/map/src/tiles.rs +++ b/crates/map/src/tiles.rs @@ -184,6 +184,10 @@ pub struct TileLayer { /// address. Empty for a layer that came from no connection. Never a secret. #[serde(default, skip_serializing_if = "String::is_empty")] pub connection: String, + /// Added by an anonymous plugin command: none of its addresses may acquire a saved + /// web-service login, including after saving and reopening the project. + #[serde(default, skip_serializing_if = "is_false")] + pub anonymous: bool, /// Picked in the Layers pane (`map.selectTiles`), for the session only: it holds while no /// map layer is active (the engine's `Session::selected_tile`). #[serde(skip)] @@ -378,6 +382,7 @@ impl TileLayer { feature_info: None, legends: Vec::new(), connection: String::new(), + anonymous: false, expanded: false, selected: false, single_image: false, diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index a2f2361fe0..69bfa531fd 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -2626,7 +2626,7 @@ impl RustGisApp { if p.get("zoom").and_then(Value::as_bool) == Some(true) { q["zoom"] = json!(true); } - for k in ["service", "coverage"] { + for k in ["service", "coverage", "anonymous"] { if p.get(k).and_then(Value::as_bool) == Some(true) { q[k] = json!(true); } @@ -4479,6 +4479,53 @@ mod record_tests { app.record_step("layer.color", &json!({"layer": "Lakes", "color": "#123456"}), None); assert_eq!(app.ws.recording.as_ref().map(Vec::len), Some(1)); } + + #[test] + #[cfg(not(target_arch = "wasm32"))] + fn an_anonymous_download_never_acquires_a_saved_login() { + use rustgis_engine::render::rustgis_tiles::{cache, logins}; + use std::io::{Read, Write}; + let server = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let origin = format!("http://{}", server.local_addr().unwrap()); + let reply = std::thread::spawn(move || { + let (mut stream, _) = server.accept().unwrap(); + stream.set_read_timeout(Some(std::time::Duration::from_secs(5))).unwrap(); + let mut request = Vec::new(); + let mut chunk = [0; 1024]; + while !request.windows(4).any(|window| window == b"\r\n\r\n") { + let count = stream.read(&mut chunk).unwrap(); + if count == 0 { + break; + } + request.extend_from_slice(&chunk[..count]); + } + let body = br#"{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"properties":{}}]}"#; + write!( + stream, + "HTTP/1.1 200 OK\r\nContent-Type: application/geo+json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + body.len() + ) + .unwrap(); + stream.write_all(body).unwrap(); + String::from_utf8(request).unwrap() + }); + let mut app = RustGisApp::with_sample(); + let id = app + .run("connection.new", json!({"kind":"wms","address":format!("{origin}/wms"),"auth":"header","keyName":"X-Ui-Key"})) + .unwrap()["id"] + .clone(); + app.run("connection.login", json!({"id":id,"apiKey":"ui-sentinel-757","allowInsecure":true})).unwrap(); + app.run("ui.addUrl", json!({"url":format!("{origin}/roads.geojson"),"name":"Anonymous download","service":true,"anonymous":true})) + .unwrap(); + cache::wait(std::time::Duration::from_secs(5)); + app.poll_data_downloads(); + crate::serviceui::poll_pages(&mut app, &egui::Context::default()); + assert!(!reply.join().unwrap().to_ascii_lowercase().contains("x-ui-key:")); + app.run("connection.login", json!({"id":id,"apiKey":"ui-sentinel-757","allowInsecure":true})).unwrap(); + let layer = app.session.map().layers.iter().find(|layer| layer.name == "Anonymous download").unwrap(); + assert!(layer.connection.is_empty()); + assert!(logins::apply(layer.dataset.source.as_deref().unwrap()).unwrap().is_none()); + } } #[cfg(test)] diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index 108ec81b78..e9b5376b96 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -827,6 +827,11 @@ to make one for its address. `connection.list` reports these as `loginRequests` CLI and Python use the same session-only `connection.login` commands. ArcGIS sign-in and tokens keep their own [ArcGIS sign-in](#web-data). A login ends with the session that made it. Unbound fetches never adopt another session's service login. +Layers added by plugins stay anonymous, including after saving and reopening a project or adding +a saved connection later. To use a connection's login, add the layer yourself through that connection. +A plugin cannot set a same-origin legend on a layer tied to a saved connection, even when that +connection is currently public. If it changes the legends of an unconnected tile layer, that +layer stays anonymous too. ## Large files From 04e480ca53848191c3120bb444b0262457af6787 Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Wed, 4 Nov 2026 09:40:07 +0100 Subject: [PATCH 19/19] fix(connections): preserve anonymous service groups during refresh --- crates/engine/src/cmd/service_group.rs | 34 +++- crates/engine/src/service_logins.rs | 33 ++-- crates/engine/src/tests.rs | 1 + crates/engine/src/tests/service_logins.rs | 211 ++++++++++++++++++++++ crates/map/src/lib.rs | 13 +- crates/ui-egui/src/connectiontree.rs | 130 +++++++++++++ crates/ui-egui/src/serviceui.rs | 26 ++- docs/user-guide/data.md | 3 + 8 files changed, 427 insertions(+), 24 deletions(-) diff --git a/crates/engine/src/cmd/service_group.rs b/crates/engine/src/cmd/service_group.rs index ded1822d02..c12534abd9 100644 --- a/crates/engine/src/cmd/service_group.rs +++ b/crates/engine/src/cmd/service_group.rs @@ -10,13 +10,13 @@ use super::{C, CmdResult, MAX_GROUP_BYTES, Session, capabilities, ps, service_js pub fn commands() -> Vec { vec![ C::new("service.import", "Import Service", "Home › Add", import).params( - r#"{"url": "an OGC API landing page or collections address", "type"?, "name"?: "group name (default My OGC Layer)", "text"?, "documents"?: "as service.layers", "responses"?: [{"url": "features URL", "text": "downloaded GeoJSON"}], "connection"?: "as service.add"} ("group" and "layer" are rejected: use service.refresh)"#, + r#"{"url": "an OGC API landing page or collections address", "type"?, "name"?: "group name (default My OGC Layer)", "text"?, "documents"?: "as service.layers", "responses"?: [{"url": "features URL", "text": "downloaded GeoJSON"}], "connection"?: "as service.add", "anonymous"?: bool (internal: preserve plugin-selected service provenance)} ("group" and "layer" are rejected: use service.refresh)"#, ), C::new("service.refresh", "Refresh", "Layers", refresh).params( - r#"{"group"?: id, "layer"?: "one child layer id or name", "text"?, "documents"?, "responses"?: "as service.import", "connection"?: "as service.add (default: the one its layers record)"}"#, + r#"{"group"?: id, "layer"?: "one child layer id or name", "text"?, "documents"?, "responses"?: "as service.import", "connection"?: "as service.add (default: the one its layers record)", "anonymous"?: bool (internal: do not borrow a saved login)}"#, ), C::new("service.groupPlan", "Service Import Requests", "Layers", plan).view().params( - r#"{"url"?, "type"?, "name"?, "group"?: id, "layer"?: "one child layer", "sourceOnly"?: "return the connection without fetching", "text"?, "documents"?, "connection"?: "as service.add"} → request, layers, and next while discovery is incomplete"#, + r#"{"url"?, "type"?, "name"?, "group"?: id, "layer"?: "one child layer", "sourceOnly"?: "return the connection without fetching", "text"?, "documents"?, "connection"?: "as service.add", "anonymous"?: bool (internal: preserve plugin-selected service provenance)} → request, layers, and next while discovery is incomplete"#, ), C::new("service.group", "Service Group", "Layers", set_group).params( r#"{"group"?: id, "layer"?: "a child identifying its group", "name"?: text, "expanded"?: bool, "visible"?: "set all children", "remove"?: "remove the group and its children"}"#, @@ -41,9 +41,11 @@ fn target(s: &Session, p: &Value) -> Result)>, fn request(s: &mut Session, p: &Value) -> CmdResult { let mut q = p.clone(); + let mut anonymous = s.web_logins.is_anonymous() || p.get("anonymous").and_then(Value::as_bool) == Some(true); if let Some((group, child)) = target(s, p)? { - // Anonymous reads (a plugin's command, an unsaved Test) never borrow the group's login. - if !s.web_logins.is_anonymous() && q.get("connection").is_none_or(Value::is_null) { + anonymous |= group.anonymous || child.and_then(|id| s.map().layer(id)).is_some_and(|l| l.anonymous); + // A plugin-selected source never borrows a login, including a later user refresh. + if !anonymous && q.get("connection").is_none_or(Value::is_null) { let mut ids = group .children .iter() @@ -61,6 +63,12 @@ fn request(s: &mut Session, p: &Value) -> CmdResult { q["type"] = json!(group.kind); q["name"] = json!(group.name); } + if anonymous { + q["anonymous"] = json!(true); + if let Some(q) = q.as_object_mut() { + q.remove("connection"); + } + } super::url_of(&q)?; Ok(crate::service_logins::request_params(s, &q)?.into_owned()) } @@ -74,6 +82,13 @@ fn valid_connection(s: &Session, id: &str, url: &str) -> bool { } fn record_connection(s: &mut Session, layer: u32, request: &Value) { + if request.get("anonymous").and_then(Value::as_bool) == Some(true) { + if let Some(layer) = s.map_mut().layer_mut(layer) { + layer.anonymous = true; + layer.connection.clear(); + } + return; + } let Some(id) = ps(request, "connection") else { return }; let Some(url) = s.map().layer(layer).and_then(|l| l.dataset.source.as_deref()) else { return }; if valid_connection(s, id, url) @@ -153,7 +168,12 @@ fn listing(s: &mut Session, p: &Value) -> CmdResult { if let Some(next) = found.next { out["next"] = json!(next); } - out["request"] = json!({"url": q.get("url"), "type": ps(&q, "type").unwrap_or("ogcapi"), "connection": q.get("connection")}); + out["request"] = json!({"url": q.get("url"), "type": ps(&q, "type").unwrap_or("ogcapi")}); + if q.get("anonymous").and_then(Value::as_bool) == Some(true) { + out["request"]["anonymous"] = json!(true); + } else if let Some(connection) = q.get("connection").filter(|v| !v.is_null()) { + out["request"]["connection"] = connection.clone(); + } Ok(out) } @@ -259,6 +279,7 @@ fn import(s: &mut Session, p: &Value) -> CmdResult { name, url: ps(req, "url").unwrap_or_default().into(), kind: ps(req, "type").unwrap_or("ogcapi").into(), + anonymous: req.get("anonymous").and_then(Value::as_bool) == Some(true), expanded: true, children, }; @@ -417,6 +438,7 @@ fn refresh(s: &mut Session, p: &Value) -> CmdResult { let map = s.map_mut(); let group = map.service_groups.iter_mut().find(|g| g.id == old.id).ok_or("the group was removed")?; if child_id.is_none() { + group.anonymous |= listing["request"].get("anonymous").and_then(Value::as_bool) == Some(true); group.children = children; } else { // The child's provenance follows the service (a new source address or token). diff --git a/crates/engine/src/service_logins.rs b/crates/engine/src/service_logins.rs index 7f1d52b978..872ab40443 100644 --- a/crates/engine/src/service_logins.rs +++ b/crates/engine/src/service_logins.rs @@ -461,11 +461,11 @@ fn rebind_connection(s: &mut Session, c: &Connection) { rewrite(path); } } - for group in &mut m.service_groups { + for group in m.service_groups.iter_mut().filter(|g| !g.anonymous) { let ids: BTreeSet<_> = group .children .iter() - .filter_map(|child| m.layers.iter().find(|l| l.id == child.layer).map(|l| l.connection.as_str())) + .filter_map(|child| m.layers.iter().find(|l| l.id == child.layer && !l.anonymous).map(|l| l.connection.as_str())) .collect(); if ids.len() == 1 && ids.contains(c.id.as_str()) { rewrite(&mut group.url); @@ -487,7 +487,7 @@ fn record_choice(s: &mut Session, origin: &str, old: &str, c: &Connection) { let group_layers: BTreeSet<_> = m .service_groups .iter() - .filter(|g| logins::origin(&g.url).as_deref() == Some(origin)) + .filter(|g| !g.anonymous && logins::origin(&g.url).as_deref() == Some(origin)) .flat_map(|g| g.children.iter().map(|child| child.layer)) .collect(); for l in &mut m.layers { @@ -535,7 +535,7 @@ fn reads(s: &Session) -> BTreeMap<(String, String), Reads> { } // An imported service's group is read again by a refresh, from its address: it logs in // with the connection its layers record. - for g in &m.service_groups { + for g in m.service_groups.iter().filter(|g| !g.anonymous) { let Some(origin) = logins::origin(&g.url) else { continue }; for child in &g.children { let layer = m.layers.iter().find(|l| l.id == child.layer); @@ -720,11 +720,12 @@ fn ask(s: &mut Session, request: LoginRequest) { s.database_logins.request(request); } -/// What the project held before an add command: its layers by live identity and its tile layers, so the -/// ones the command added can be told apart. +/// What the project held before an add command: its layers by live identity, tile layers and +/// service groups, so even an empty group added by a plugin retains anonymous provenance. pub(crate) struct Before { layers: Vec>, tiles: Vec)>>, + groups: Vec>, } /// The connection an add command names (its `connection`), checked before it runs: a saved @@ -760,6 +761,11 @@ pub(crate) fn before(s: &Session, id: &str, params: &Value) -> Result>()}) + .to_string() + }; + let responses = |plan: &Value| { + plan["layers"] + .as_array() + .unwrap() + .iter() + .map(|layer| { + json!({ + "url":layer["offer"]["url"], + "text":json!({"type":"FeatureCollection","features":[{ + "type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"properties":{"collection":layer["name"]} + }]}).to_string() + }) + }) + .collect::>() + }; + let mut s = session(None); + super::plugin(plugin, &["edit", "network", "files"]); + if import_map { + let mut map = Project::default().map().clone(); + map.name = "Plugin service groups".into(); + let mut layer = rustgis_map::Layer::new(1, rustgis_data::Dataset::new("roads", rustgis_data::GeometryType::None, Vec::new())); + layer.dataset.source = Some(format!("{url}/roads/items")); + map.layers.push(layer); + map.service_groups = vec![ + rustgis_map::ServiceGroup { + id: 100, + name: "Plugin".into(), + url: url.clone(), + kind: "ogcapi".into(), + anonymous: false, + expanded: true, + children: vec![rustgis_map::ServiceChild { layer: 1, collection: "roads".into(), source_url: url.clone() }], + }, + rustgis_map::ServiceGroup { + id: 101, + name: "Initially empty".into(), + url: url.clone(), + kind: "ogcapi".into(), + anonymous: false, + expanded: true, + children: vec![], + }, + ]; + super::forward(&mut s, plugin, "map.import", json!({"text":serde_json::to_string(&map).unwrap()})).unwrap(); + } else { + let plan = super::forward( + &mut s, + plugin, + "service.groupPlan", + json!({ + "url":url,"type":"ogcapi","text":collections(&["roads"]) + }), + ) + .unwrap(); + super::forward( + &mut s, + plugin, + "service.import", + json!({ + "url":url,"type":"ogcapi","text":collections(&["roads"]),"responses":responses(&plan) + }), + ) + .unwrap(); + } + let group = s.map().service_groups[0].id; + let id = new(&mut s, json!({"kind":"ogcapi","address":url,"auth":auth,"user":"ana","keyName":"X-Key"})); + let login = if auth == "basic" { + json!({"id":id,"password":"group-import-sentinel-757"}) + } else { + json!({"id":id,"apiKey":"group-import-sentinel-757"}) + }; + s.run("connection.login", &login).unwrap(); + for g in &s.map().service_groups { + assert!(g.anonymous, "even a plugin-imported empty group needs provenance"); + } + for group in s.map().service_groups.iter().map(|g| g.id).collect::>() { + let source = s.run("service.groupPlan", &json!({"group":group,"sourceOnly":true})).unwrap(); + let prepared = web::request_params(&mut s, &source).unwrap(); + assert!(header(prepared["url"].as_str().unwrap()).is_none()); + } + let assert_anonymous = |s: &mut Session| { + let source = s.run("service.groupPlan", &json!({"group":group,"sourceOnly":true,"connection":id})).unwrap(); + assert_eq!(source["anonymous"], true); + assert!(source.get("connection").is_none()); + // Exercise the consumer, not just the flag: it must not infer the saved login. + let prepared = web::request_params(s, &source).unwrap(); + assert_eq!(prepared["url"], url); + assert!(header(prepared["url"].as_str().unwrap()).is_none()); + let g = s.map().service_groups.iter().find(|g| g.id == group).unwrap(); + assert!(g.anonymous); + assert!(header(&g.url).is_none()); + for child in &g.children { + let layer = s.map().layer(child.layer).unwrap(); + assert!(layer.anonymous && layer.connection.is_empty()); + assert!(header(layer.dataset.source.as_deref().unwrap()).is_none()); + assert!(header(&child.source_url).is_none()); + } + assert_eq!(requests(s), json!([])); + }; + assert_anonymous(&mut s); + for reopen in [false, true] { + if reopen { + let (text, _) = s.project.clone().saved_text(None).unwrap(); + s.run("project.openText", &json!({"text":text})).unwrap(); + s.run("connection.login", &login).unwrap(); + } + let plan = s.run("service.groupPlan", &json!({"group":group,"text":collections(&["roads","water"])})).unwrap(); + let prepared = web::request_params(&mut s, &plan["request"]).unwrap(); + assert!(header(prepared["url"].as_str().unwrap()).is_none()); + s.run( + "service.refresh", + &json!({ + "group":group,"text":collections(&["roads","water"]),"responses":responses(&plan) + }), + ) + .unwrap(); + assert_anonymous(&mut s); + let g = s.map().service_groups.iter().find(|g| g.id == group).unwrap(); + assert_eq!(g.children.len(), 2); + assert!(g.children.iter().any(|c| c.collection == "water")); + for child in &g.children { + assert_eq!(s.map().layer(child.layer).unwrap().dataset.features.len(), 1); + } + let (text, _) = s.project.clone().saved_text(None).unwrap(); + s.run("project.openText", &json!({"text":text})).unwrap(); + s.run("connection.login", &login).unwrap(); + assert_anonymous(&mut s); + // Removing every child must not erase the group's independent provenance. + s.run("service.refresh", &json!({"group":group,"text":collections(&[]),"responses":[]})).unwrap(); + assert!(s.map().service_groups.iter().find(|g| g.id == group).unwrap().children.is_empty()); + assert_anonymous(&mut s); + } + rustgis_plugins::unload(plugin); + } +} + +#[test] +fn refreshing_an_anonymous_child_does_not_adopt_or_anonymize_its_user_group() { + let mut s = session(None); + let url = "https://mixed-group757.example.org/collections"; + let id = new(&mut s, json!({"kind":"ogcapi","address":url,"auth":"header","keyName":"X-Key"})); + s.run("connection.login", &json!({"id":id,"apiKey":"mixed-group-sentinel-757"})).unwrap(); + let collections = ["roads", "water"].map(|name| { + json!({ + "id":name,"title":name,"links":[{"rel":"items","type":"application/geo+json","href":format!("{url}/{name}/items")}] + }) + }); + let document = json!({"collections":collections}).to_string(); + let features = r#"{"type":"FeatureCollection","features":[]}"#; + let responses = |plan: &Value| { + plan["layers"] + .as_array() + .unwrap() + .iter() + .map(|layer| { + json!({ + "url":layer["offer"]["url"],"text":features + }) + }) + .collect::>() + }; + let plan = s.run("service.groupPlan", &json!({"url":url,"type":"ogcapi","connection":id,"text":document})).unwrap(); + let added = s + .run( + "service.import", + &json!({ + "url":url,"type":"ogcapi","connection":id,"text":document, + "responses":responses(&plan) + }), + ) + .unwrap(); + let group = added["group"].clone(); + let child = s.map().service_groups[0].children[0].layer; + let other = s.map().service_groups[0].children[1].layer; + s.map_mut().layer_mut(child).unwrap().anonymous = true; + s.map_mut().layer_mut(child).unwrap().connection.clear(); + let source = s.run("service.groupPlan", &json!({"layer":child,"sourceOnly":true,"connection":id})).unwrap(); + let prepared = web::request_params(&mut s, &source).unwrap(); + assert!(header(prepared["url"].as_str().unwrap()).is_none()); + let plan = s.run("service.groupPlan", &json!({"layer":child,"connection":id,"text":document})).unwrap(); + s.run( + "service.refresh", + &json!({ + "layer":child,"connection":id,"text":document,"responses":responses(&plan) + }), + ) + .unwrap(); + assert!(s.map().layer(child).unwrap().anonymous); + assert!(s.map().layer(child).unwrap().connection.is_empty()); + assert!(header(s.map().layer(child).unwrap().dataset.source.as_deref().unwrap()).is_none()); + assert!(!s.map().service_groups[0].anonymous, "the user's other children keep their login"); + assert_eq!(s.map().layer(other).unwrap().connection, id); + let source = s.run("service.groupPlan", &json!({"group":group,"sourceOnly":true})).unwrap(); + assert_eq!(header(source["url"].as_str().unwrap()).unwrap().1, "mixed-group-sentinel-757"); +} + #[test] fn plugin_downloads_stay_anonymous_after_the_front_end_adds_them() { super::isolate_plugins(); diff --git a/crates/map/src/lib.rs b/crates/map/src/lib.rs index ee4a269127..2c86a6382d 100644 --- a/crates/map/src/lib.rs +++ b/crates/map/src/lib.rs @@ -1116,6 +1116,9 @@ pub struct ServiceGroup { pub name: String, pub url: String, pub kind: String, + /// Plugin-selected service: never adopt a saved login, even without children or after reopen. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub anonymous: bool, #[serde(default = "yes")] pub expanded: bool, pub children: Vec, @@ -2954,7 +2957,15 @@ mod tests { fn ids_stay_unique_when_a_project_reaches_the_largest_id() { let mut m = Map::new("m"); m.layers.push(Layer::new(u32::MAX, points())); - let group = ServiceGroup { id: 0, name: "g".into(), url: String::new(), kind: String::new(), expanded: true, children: vec![] }; + let group = ServiceGroup { + id: 0, + name: "g".into(), + url: String::new(), + kind: String::new(), + anonymous: false, + expanded: true, + children: vec![], + }; let a = m.add_dataset(points()); let b = m.add_service_group(group); let c = m.add_dataset(points()); diff --git a/crates/ui-egui/src/connectiontree.rs b/crates/ui-egui/src/connectiontree.rs index 0f971c61a5..c2b478864d 100644 --- a/crates/ui-egui/src/connectiontree.rs +++ b/crates/ui-egui/src/connectiontree.rs @@ -882,6 +882,136 @@ mod tests { } } + /// A plugin's anonymous group remains anonymous when the user refreshes it, including new + /// children and every discovery/download phase after saving and reopening the project. + #[test] + fn anonymous_service_groups_refresh_without_logins_before_and_after_reopen() { + use rustgis_engine::render::rustgis_tiles::logins; + use std::sync::{ + Mutex, + atomic::{AtomicUsize, Ordering}, + }; + let _turn = crate::fetching::turn(); + for basic in [false, true] { + let revision = Arc::new(AtomicUsize::new(0)); + let requests = Arc::new(Mutex::new(Vec::::new())); + let (served_revision, seen) = (revision.clone(), requests.clone()); + let base = serve(move |head| { + seen.lock().unwrap().push(head.to_string()); + let path = head.split_whitespace().nth(1).unwrap_or("").split('?').next().unwrap_or(""); + let revision = served_revision.load(Ordering::SeqCst); + let body = match path { + "/api" => json!({"links":[{"rel":"data","type":"application/json","href":"/collections"}]}), + "/collections" => { + let collections = ["roads", "buildings"] + .into_iter() + .take(if revision == 0 { 1 } else { 2 }) + .map(|id| { + json!({"id":id,"title":id,"links":[ + {"rel":"items","type":"application/geo+json","href":format!("/{id}/items")} + ]}) + }) + .collect::>(); + json!({"collections":collections}) + } + "/roads/items" | "/buildings/items" => json!({ + "type":"FeatureCollection", + "features":(0..=revision).map(|id| json!({ + "type":"Feature","id":id, + "geometry":{"type":"Point","coordinates":[1,2]}, + "properties":{"revision":revision} + })).collect::>() + }), + _ => return (404, b"Unknown test path".to_vec()), + }; + (200, serde_json::to_vec(&body).unwrap()) + }); + let url = format!("{base}/api"); + let mut h = harness(); + let app = h.state_mut(); + app.run("ui.language", json!({"code":"en"})).unwrap(); + app.run("project.new", json!(null)).unwrap(); + let connection = app + .run( + "connection.new", + json!({ + "kind":"ogcapi","address":url,"auth":if basic { "basic" } else { "header" }, + "user":if basic { "reader" } else { "" },"keyName":if basic { "" } else { "X-Api-Key" } + }), + ) + .unwrap(); + let mut login = json!({"id":connection["id"],"allowInsecure":true}); + login[if basic { "password" } else { "apiKey" }] = json!("anonymous-group-secret-757"); + app.run("connection.login", login.clone()).unwrap(); + let authenticated = rustgis_engine::service_logins::request(&mut app.session, None, &url).unwrap(); + assert!(logins::apply(&authenticated).unwrap().is_some(), "the same origin has a selected session login"); + // This is the persisted marker produced for a plugin-selected service, without + // keeping an anonymous engine context active while the GUI downloads its documents. + app.run("ui.importService", json!({"url":url,"type":"ogcapi","name":"Anonymous plugin group","anonymous":true})).unwrap(); + until(&mut h, |app| app.ui.service_import.is_none()); + assert!(h.state().dialogs.error.is_none(), "{:?}", h.state().dialogs.error); + let group = h.state().session.map().service_groups[0].id; + assert!(h.state().session.map().service_groups[0].anonymous); + let roads = h.state().session.map().layer_by_name("roads").unwrap().id; + assert_eq!(h.state().session.map().layer(roads).unwrap().dataset.features.len(), 1); + let dir = tempfile::tempdir().unwrap(); + let project = dir.path().join("anonymous-service.rgis"); + for step in 1..=3 { + if step == 2 { + h.state_mut().run("project.saveAs", json!({"path":project.display().to_string()})).unwrap(); + h.state_mut().run("project.open", json!({"path":project.display().to_string()})).unwrap(); + h.state_mut().run("connection.login", login.clone()).unwrap(); + assert!(h.state().session.map().service_groups[0].anonymous); + } + revision.store(step, Ordering::SeqCst); + let before = requests.lock().unwrap().len(); + for _ in 0..4 { + h.step(); + } + // Refresh the newly discovered child on its own too: it cannot silently adopt + // the selected connection when it later becomes the user's refresh target. + h.get_by_label(if step == 3 { "buildings" } else { "Anonymous plugin group" }).click_secondary(); + h.step(); + h.step(); + h.get_by_label("Refresh").click(); + h.step(); + until(&mut h, |app| app.ui.service_import.is_none()); + assert!(h.state().dialogs.error.is_none(), "{:?}", h.state().dialogs.error); + let app = h.state(); + let saved = &app.session.map().service_groups[0]; + assert_eq!(saved.id, group); + assert!(saved.anonymous); + assert_eq!(saved.children.len(), 2, "refresh discovers a new anonymous child"); + assert_eq!(app.session.map().layer_by_name("roads").unwrap().id, roads); + for name in ["roads", "buildings"] { + let layer = app.session.map().layer_by_name(name).unwrap(); + assert!(layer.anonymous, "{name} must keep anonymous provenance"); + assert!(layer.connection.is_empty(), "{name} cannot record the user's connection"); + let expected = if step == 3 && name == "roads" { 3 } else { step + 1 }; + assert_eq!(layer.dataset.features.len(), expected, "{name} data is updated"); + assert!(logins::apply(layer.dataset.source.as_deref().unwrap()).unwrap().is_none()); + } + let requests = requests.lock().unwrap(); + let paths = requests[before..] + .iter() + .map(|head| head.split_whitespace().nth(1).unwrap().split('?').next().unwrap()) + .collect::>(); + let mut expected = vec!["/api", "/collections"]; + if step != 3 { + expected.push("/roads/items"); + } + expected.push("/buildings/items"); + assert_eq!(paths, expected, "capabilities, linked collections and all features are fetched"); + } + for head in requests.lock().unwrap().iter() { + let lower = head.to_ascii_lowercase(); + assert!(!lower.contains("authorization:"), "Basic login leaked: {head}"); + assert!(!lower.contains("x-api-key:"), "header login leaked: {head}"); + assert!(!head.contains("anonymous-group-secret-757")); + } + } + } + #[test] fn a_foreign_followup_does_not_adopt_that_origins_saved_login() { let mut app = RustGisApp::with_sample(); diff --git a/crates/ui-egui/src/serviceui.rs b/crates/ui-egui/src/serviceui.rs index b31c22e63e..66f5a94850 100644 --- a/crates/ui-egui/src/serviceui.rs +++ b/crates/ui-egui/src/serviceui.rs @@ -1,6 +1,6 @@ //! Background group import/refresh for both desktop and browser. Discovery and updates live in //! engine commands; this state machine supplies their downloaded documents and preserves the -//! saved connection id through every deferred add. +//! saved connection or anonymous provenance through every deferred add. use crate::{RustGisApp, Slot, fetch_into, fetch_into_limited, i18n}; use rustgis_engine::data::ogc::{self, Pages, Step}; @@ -215,7 +215,7 @@ pub fn start(app: &mut RustGisApp, p: &Value, refresh: bool) -> Result<(), Strin let url = task.params.get("url").and_then(Value::as_str).unwrap_or_default(); let kind = task.params.get("type").and_then(Value::as_str); let caps = rustgis_engine::data::ogc::capabilities_url(url, kind); - let caps = match request_from(app, task.connection.as_ref().and_then(Value::as_str), url, &caps) { + let caps = match task.request(app, &caps) { Ok(caps) => caps, Err(e) => { app.session.download_failed(task.token); @@ -231,11 +231,26 @@ pub fn start(app: &mut RustGisApp, p: &Value, refresh: bool) -> Result<(), Strin } impl Import { + /// Anonymous service provenance forbids selecting a login, including for same-origin links. + fn request(&self, app: &mut RustGisApp, url: &str) -> Result { + if self.params.get("anonymous").and_then(Value::as_bool) == Some(true) { + return Ok(rustgis_engine::data::login_address::inner(url).to_string()); + } + let base = self.params.get("url").and_then(Value::as_str).unwrap_or_default(); + request_from(app, self.connection.as_ref().and_then(Value::as_str), base, url) + } + fn discover(&mut self, app: &mut RustGisApp) -> Result<(), String> { let listing = app.session.run("service.groupPlan", &self.params)?; + if listing.get("request").and_then(|p| p.get("anonymous")).and_then(Value::as_bool) == Some(true) { + self.params["anonymous"] = json!(true); + if let Some(params) = self.params.as_object_mut() { + params.remove("connection"); + } + self.connection = None; + } if let Some(next) = listing.get("next").and_then(Value::as_str) { - let url = self.params.get("url").and_then(Value::as_str).unwrap_or_default(); - let next = request_from(app, self.connection.as_ref().and_then(Value::as_str), url, next)?; + let next = self.request(app, next)?; self.slot = fetch_into(&next); self.document = Some(next); } else { @@ -254,8 +269,7 @@ impl Import { fn next(&mut self, app: &mut RustGisApp) -> Result<(), String> { self.current = self.remaining.pop_front(); if let Some(url) = &self.current { - let base = self.params.get("url").and_then(Value::as_str).unwrap_or_default(); - let url = request_from(app, self.connection.as_ref().and_then(Value::as_str), base, url)?; + let url = self.request(app, url)?; self.slot = fetch_into_limited(&url, rustgis_engine::cmd::service::MAX_GROUP_BYTES.saturating_sub(self.bytes)); } Ok(()) diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index e9b5376b96..64db069fe4 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -832,6 +832,9 @@ a saved connection later. To use a connection's login, add the layer yourself th A plugin cannot set a same-origin legend on a layer tied to a saved connection, even when that connection is currently public. If it changes the legends of an unconnected tile layer, that layer stays anonymous too. +Refreshing a plugin-added service group also stays anonymous: its capabilities, linked documents, +feature downloads and newly discovered collections do not borrow a saved login. The group keeps +that provenance after saving and reopening, even if a refresh temporarily leaves it empty. ## Large files