diff --git a/apps/rustgis-web/src/web.rs b/apps/rustgis-web/src/web.rs index dd3d530696..c02f452dcb 100644 --- a/apps/rustgis-web/src/web.rs +++ b/apps/rustgis-web/src/web.rs @@ -571,17 +571,17 @@ 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. +/// `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); diagnostics.omit_statuses(omit); let answer = async { - 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); @@ -589,7 +589,9 @@ async fn fetch_bytes(url: &str, capture: Capture, omit: &'static [u16]) -> Resul 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 @@ -597,128 +599,148 @@ async fn fetch_bytes(url: &str, capture: Capture, omit: &'static [u16]) -> Resul if !prepared.signed { crate::arcgis::renew_if_due(url).await; } + 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()); 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 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)?; + 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 { + 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(|| { - diagnostics.timed_out(); - 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)?; - diagnostics.answered(resp.status()); - 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() { - return Err(format!("{url}: {}", cache::NO_RANGES)); + // 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); } - 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 { + // 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(|| { diagnostics.timed_out(); - 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; + 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)?; + diagnostics.answered(resp.status()); + 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() { + 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 { + diagnostics.timed_out(); + 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::<_, String>(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 }) } .await; diagnostics.record(answer.map_err(|e| Failure::new(url, e))).map_err(|failure| failure.message) @@ -730,32 +752,59 @@ async fn post_json(url: &str, body: &str, capture: Capture) -> Result, S use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{cache::RequestDiagnostics, failure::Failure}; let mut diagnostics = RequestDiagnostics::with_capture(url, "POST", capture); let answer = async { - 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)?; - diagnostics.answered(resp.status()); - 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)?; + diagnostics.answered(resp.status()); + 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::<_, String>(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 }) } .await; diagnostics.record(answer.map_err(|e| Failure::new(url, e))).map_err(|failure| failure.message) @@ -768,39 +817,63 @@ async fn head(url: &str, capture: Capture) -> Result(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 }) } .await; diagnostics.record(answer.map_err(|e| Failure::new(url, e))).map_err(|failure| failure.message) diff --git a/crates/assistant/src/tools.rs b/crates/assistant/src/tools.rs index 97e93b57f9..8b6fe528ca 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,52 @@ 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(public_result(s, v, 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 + } +} + +fn public_result(s: &mut Session, value: Value, tokens: usize) -> String { + let mut withheld = 0usize; + let mut value = public_addresses(value, 0, &mut withheld); + if withheld > 0 { + let mark = s.notice_mark(); + s.notify(format!("Tool result: {withheld} subtrees at nesting depth {MAX_DEPTH} were withheld."), false); + if let Some(notices) = value.get_mut("notices").and_then(Value::as_array_mut) { + notices.extend(s.notices_since(mark).iter().map(|n| json!(n.text))); + } else { + value = with_notices(value, s, mark); + } + } + bounded_json(&value, tokens) +} + +/// `v` with its strings made public ([`public_text`]), to a depth of [`MAX_DEPTH`]. +fn public_addresses(v: Value, depth: usize, withheld: &mut 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, withheld)).collect()), + Value::Object(o) if depth < MAX_DEPTH => { + Value::Object(o.into_iter().map(|(k, x)| (k, public_addresses(x, depth + 1, withheld))).collect()) + } + Value::Array(_) | Value::Object(_) => { + *withheld = withheld.saturating_add(1); + json!("[too deep]") + } + other => other, + } } /// Serialize complete JSON. Oversized results become an explicit text preview envelope; @@ -463,7 +510,7 @@ fn run_script(s: &mut Session, a: &Value, tokens: usize) -> Result { // Nothing was kept, so no line ran and no layer was added. @@ -472,7 +519,7 @@ fn run_script(s: &mut Session, a: &Value, tokens: usize) -> Result = notices.iter().filter(|t| **t != e && **t != told).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(public_result(s, out, tokens)) } } } @@ -813,3 +860,74 @@ 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 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 archive = logins::bind(&binding, &format!("{origin}/a.pmtiles")).unwrap(); + let result = json!({"layers": [{"next": {"request": {"url": format!("pmtiles:x@{archive}")}}}], "count": 2}); + let v = public_addresses(result, 0, &mut 0); + 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_report_each_withheld_subtree_without_losing_success() { + let mut s = Session::new(rustgis_engine::Project::default()); + let mut deep = json!({"value": "rustgis-login:leak"}); + for _ in 0..MAX_DEPTH { + deep = json!([deep]); + } + let result = json!({"success": true, "left": deep.clone(), "right": deep, "notices": ["existing warning"]}); + let out: Value = serde_json::from_str(&public_result(&mut s, result, DEFAULT_OUTPUT_TOKENS)).unwrap(); + assert_eq!(out["success"], true, "withholding output must not report an executed command as failed"); + assert!(!out.to_string().contains("rustgis-login:")); + assert_eq!(out["notices"][0], "existing warning"); + let notice = out["notices"][1].as_str().unwrap(); + let counts: Vec = notice.split_whitespace().filter_map(|word| word.parse().ok()).collect(); + assert_eq!(counts, [2, MAX_DEPTH], "the notice reports truncated subtrees, not an unbounded count of their contents"); + assert_eq!(s.notices_since(0).len(), 1, "the app also receives one aggregate loss notice"); + } + + #[test] + fn clean_nested_results_need_no_withholding_notice() { + let mut s = Session::new(rustgis_engine::Project::default()); + let result = json!({"result": {"nested": [{"value": "complete"}]}, "success": true}); + let out: Value = serde_json::from_str(&public_result(&mut s, result.clone(), DEFAULT_OUTPUT_TOKENS)).unwrap(); + assert_eq!(out, result); + assert!(s.notices_since(0).is_empty()); + } +} diff --git a/crates/data/src/lib.rs b/crates/data/src/lib.rs index cf7e0e9adb..0e5b2ac07a 100644 --- a/crates/data/src/lib.rs +++ b/crates/data/src/lib.rs @@ -25,6 +25,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 memory; diff --git a/crates/data/src/login_address.rs b/crates/data/src/login_address.rs new file mode 100644 index 0000000000..bc6d7c2b52 --- /dev/null +++ b/crates/data/src/login_address.rs @@ -0,0 +1,188 @@ +//! 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) { + // Keep the actual derived binding even when its size exceeds the parser's bound. + // Request validation then refuses it instead of sending an anonymous request. + format!("{PREFIX}{}:{}:{}:{url}", b.binding, b.origin.len(), b.origin) + } 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 oversized_same_origin_inheritance_is_refused_not_anonymous() { + let base = bind("0123456789abcdef", "https://a.test", "https://a.test/collections").unwrap(); + let derived = format!("https://a.test/items?filter={}", "a".repeat(17000)); + let inherited = inherit(&base, &derived); + assert!(is_wrapper(&inherited)); + assert!(inherited.ends_with(&derived), "keep the derived target, not the base document"); + assert_eq!(parse(&inherited).unwrap_err(), BAD); + assert_eq!(check(&inherited).unwrap_err(), BAD); + let valid = inherit(&base, "https://a.test/items"); + assert_eq!(parse(&valid).unwrap().unwrap().url, "https://a.test/items"); + let foreign = format!("https://b.test/items?filter={}", "a".repeat(17000)); + assert_eq!(inherit(&base, &foreign), foreign); + } + + #[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 a89916a71b..6aa014a3f1 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('#') @@ -2773,6 +2797,9 @@ fn with_format(query: &str, format: &str) -> String { /// 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()) { @@ -3790,6 +3817,74 @@ 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(start) = begin(&first_url, 100) else { + panic!("the first page must be fetched"); + }; + let Step::Fetch(pages) = start.land(Ok(page(1, "https://MAPS.example.org:443/api/items?page=2"))) 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"); + } + /// Reads as [`super::join_pages`] does, the address asked for answered with `first` (once) and /// every other with `get`. fn join_pages( 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 62ef213f4a..2f62822245 100644 --- a/crates/data/src/source/tests.rs +++ b/crates/data/src/source/tests.rs @@ -1213,6 +1213,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/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..ddcaace578 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,82 @@ 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 { + // 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)?; + 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 +905,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 +913,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 +936,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 +962,20 @@ 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 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 { + 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 +991,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 +1024,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 856867411c..ee23d189e1 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()) { @@ -921,7 +921,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. @@ -1757,7 +1757,7 @@ fn reserve(v: &mut Vec, cells: usize) -> Result<(), String> { /// 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/gp.rs b/crates/engine/src/cmd/gp.rs index abcb17b881..67e7059654 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -1103,7 +1103,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); } @@ -1195,17 +1195,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, @@ -1227,17 +1220,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/cmd/items.rs b/crates/engine/src/cmd/items.rs index 4bbd792570..bfc8c565cd 100644 --- a/crates/engine/src/cmd/items.rs +++ b/crates/engine/src/cmd/items.rs @@ -448,6 +448,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)) { @@ -554,6 +555,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 limit = crate::project::MAX_BYTES.min(rustgis_data::zip::MAX_TOTAL as u64); if text.len() as u64 > limit { @@ -612,7 +615,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) { @@ -1048,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(); } @@ -1061,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/layer.rs b/crates/engine/src/cmd/layer.rs index 6b70261ab5..bcc205754c 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|.shp (its .dbf fetched beside it, and its .shx, .prj and .cpg when there)|.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 frames everything added; false keeps the view, even on an empty map; omitted, the first content on an empty map is framed (a GeoParquet, FlatGeobuf or cloud-optimised GeoTIFF read in place likewise; a PMTiles or MBTiles archive ignores zoom and is framed on its bounds unless it covers the whole world)", "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"}"#, + r#"{"url": "https://…/data.geojson|.csv|.parquet|.fgb|.gpkg|.zip|.shp (its .dbf fetched beside it, and its .shx, .prj and .cpg when there)|.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 frames everything added; false keeps the view, even on an empty map; omitted, the first content on an empty map is framed (a GeoParquet, FlatGeobuf or cloud-optimised GeoTIFF read in place likewise; a PMTiles or MBTiles archive ignores zoom and is framed on its bounds unless it covers the whole world)", "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 (when they came as GeoJSON) 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)}"#), + .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 (when they came as GeoJSON) 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}"#, ), @@ -2649,6 +2649,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; @@ -2902,13 +2903,15 @@ pub(crate) fn add_url(s: &mut Session, p: &Value) -> CmdResult { } fn add_url_as_given(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)")); } @@ -2960,6 +2963,9 @@ fn add_url_as_given(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 @@ -3339,7 +3345,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") } @@ -3347,7 +3353,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 }; @@ -3362,7 +3368,7 @@ pub(crate) fn add_cog(s: &mut Session, p: &Value, url: &str) -> CmdResult { let empty_before = super::map_is_empty(s); 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(); } @@ -3827,6 +3833,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/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/cmd/plugins.rs b/crates/engine/src/cmd/plugins.rs index 2a6e156732..31e94293b2 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", @@ -403,6 +404,13 @@ fn refused(s: &Session, plugin: &Plugin, spec: &C, params: &Value) -> Option { // 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; - let mut res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run(self.s, params))) - .unwrap_or_else(|_| Err(format!("{label} failed unexpectedly"))); + // 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(|| { + 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; self.changed |= mutates && !requested; diff --git a/crates/engine/src/cmd/service.rs b/crates/engine/src/cmd/service.rs index cebbcfc832..b9a2bf1e06 100644 --- a/crates/engine/src/cmd/service.rs +++ b/crates/engine/src/cmd/service.rs @@ -25,16 +25,16 @@ 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)}"#, + 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)"#, + 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()); @@ -43,13 +43,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. @@ -109,7 +113,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 { @@ -139,7 +144,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); @@ -183,7 +190,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); } } @@ -345,7 +352,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 @@ -359,7 +366,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 @@ -408,9 +415,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) { @@ -429,11 +438,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))) @@ -517,7 +529,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. @@ -581,7 +593,7 @@ 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) @@ -590,7 +602,10 @@ pub(crate) fn add(s: &mut Session, p: &Value) -> CmdResult { // and does not count as content (see `super::map_is_empty`): adding a layer after it frames // the map again. Documented in docs/user-guide/data-formats.md. 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) } @@ -700,13 +715,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)) } @@ -725,6 +740,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)?; @@ -733,7 +750,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)"))?; @@ -752,7 +769,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 @@ -769,6 +786,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") { @@ -788,7 +807,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 3e20f1acf4..e13118bd69 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"}]} ("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"}"#, + 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"?} → 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"}"#, @@ -39,15 +39,63 @@ 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); + 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 |= 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() + .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); } + if anonymous { + q["anonymous"] = json!(true); + if let Some(q) = q.as_object_mut() { + q.remove("connection"); + } + } 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) { + 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) + && let Some(layer) = s.map_mut().layer_mut(layer) + { + layer.connection = id.to_string(); + } } fn plan(s: &mut Session, p: &Value) -> CmdResult { @@ -92,7 +140,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 => { @@ -121,6 +169,11 @@ fn listing(s: &mut Session, p: &Value) -> CmdResult { out["next"] = json!(next); } 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) } @@ -153,7 +206,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) { @@ -215,6 +268,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()); } @@ -227,6 +283,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, }; @@ -244,7 +301,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()); } @@ -260,7 +317,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) @@ -341,8 +398,14 @@ 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") { + record_connection(s, child.layer, request); + } if hidden && let Some(layer) = s.map_mut().layer_mut(child.layer) { layer.visible = false; } @@ -381,6 +444,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). @@ -421,3 +485,111 @@ 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 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()); + 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 61c66b7f40..f23cbaed3c 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)"#), @@ -206,6 +206,8 @@ pub(crate) fn add(s: &mut Session, p: &Value) -> CmdResult { /// Adds after a caller's header check; a zero wait avoids spending that caller's budget twice. pub(crate) fn add_with_metadata_wait(s: &mut Session, p: &Value, metadata_wait: web_time::Duration) -> 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)"))?, @@ -278,7 +280,7 @@ pub(crate) fn add_with_metadata_wait(s: &mut Session, p: &Value, metadata_wait: 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 @@ -606,12 +608,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)?; @@ -620,6 +623,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(); @@ -654,11 +662,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(v)?), + 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(); @@ -819,4 +834,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/cmd/workflows.rs b/crates/engine/src/cmd/workflows.rs index 469f7110ae..c5c81b875a 100644 --- a/crates/engine/src/cmd/workflows.rs +++ b/crates/engine/src/cmd/workflows.rs @@ -539,7 +539,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/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 c16f039cd0..e1d7da4202 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; @@ -414,6 +415,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 } @@ -542,6 +554,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, @@ -883,6 +898,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(), layout_favourite: serde_json::Value::String("classic".into()), registry: Registry { commands: cmd::all() }, @@ -1037,11 +1053,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})) @@ -1155,7 +1177,7 @@ impl Session { } // A refused command is said once, without secrets, as the front ends show it. if let Err(error) = &res { - let text = rustgis_data::diagnostics::Redacted::new(error); + let text = Session::redacted(error); if !self.notices_since(notice_mark).iter().any(|notice| notice.error && notice.text == text.as_str()) { self.notify_redacted(text, true); } @@ -1220,7 +1242,22 @@ 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 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) + } + }); 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) @@ -1233,7 +1270,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. @@ -1385,7 +1423,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 { @@ -1569,14 +1607,24 @@ impl Session { } fn notice(&mut self, text: String, severity: rustgis_data::diagnostics::Severity) { - // Redacted for the status bar and the history alike. `redact` bounds the notice to 8 KiB - // (an ellipsis marks the cut) and omits one over 16 KiB whole, so a clipped credential - // can't survive; the history's copy is bounded further, to a record's 2 KiB. - self.notice_redacted(rustgis_data::diagnostics::Redacted::new(&text), severity); + // A web-service connection's password or key never shows, whatever quoted it: scrubbed + // first, as its login values are known only here, then redacted for the status bar and + // the history alike. `redact` bounds the notice to 8 KiB (an ellipsis marks the cut) and + // omits one over 16 KiB whole, so a clipped credential can't survive; the history's copy + // is bounded further, to a record's 2 KiB. + self.notice_redacted(Self::redacted(&text), severity); + } + + /// `text` scrubbed of every active web-service login (and any login capability), then + /// redacted: what a notice or history record may show. Login values are known only here, so + /// a failure redacted by [`Session::notify_redacted`]'s callers goes through this first. + pub fn redacted(text: &str) -> rustgis_data::diagnostics::Redacted { + rustgis_data::diagnostics::Redacted::new(&render::rustgis_tiles::logins::scrub(text)) } - /// [`Session::notify`] for text already redacted (a failure the caller redacted to compare it - /// with the notices): the type says so, so it isn't redacted again. + /// [`Session::notify`] for text already scrubbed and redacted by [`Session::redacted`] (a + /// failure the caller redacted to compare it with the notices): the type says so, so it isn't + /// redacted again. pub fn notify_redacted(&mut self, text: rustgis_data::diagnostics::Redacted, error: bool) { use rustgis_data::diagnostics::Severity; self.notice_redacted(text, if error { Severity::Error } else { Severity::Info }); diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 80aa45d328..c188b69722 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,138 @@ 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 +} + +/// A recorded value may contain several addresses inside ordinary text. Remove just each +/// capability header, not URL queries or words that happen to match an active credential. +fn without_recorded_login_bindings(text: &str) -> Option { + let whole = without_login_binding(text); + let text = whole.as_deref().unwrap_or(text); + let mut rest = text; + let mut public = None; + while let Some(at) = rest.find(rustgis_data::login_address::PREFIX) { + let candidate = &rest[at..]; + if let Some(url) = recorded_login_url(candidate) { + let out = public.get_or_insert_with(|| { + let mut out = String::with_capacity(text.len()); + out.push_str(&text[..text.len() - rest.len()]); + out + }); + out.push_str(&rest[..at]); + rest = url; + } else { + // Keep malformed wrappers verbatim, advancing past this prefix so hostile input + // cannot repeatedly rescan it. + let end = at + rustgis_data::login_address::PREFIX.len(); + if let Some(out) = &mut public { + out.push_str(&rest[..end]); + } + rest = &rest[end..]; + } + } + public + .map(|mut out| { + out.push_str(rest); + out + }) + .or(whole) +} + +/// Finds a header's end without treating the surrounding prose as part of its URL. Validate +/// only through the URL authority with the shared parser; each candidate is size bounded. +fn recorded_login_url(text: &str) -> Option<&str> { + let rest = text.strip_prefix(rustgis_data::login_address::PREFIX)?; + let (binding, rest) = rest.split_once(':')?; + if !(16..=64).contains(&binding.len()) || !binding.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) { + return None; + } + let (len, rest) = rest.split_once(':')?; + if len.is_empty() || len.len() > 4 || !len.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + let len: usize = len.parse().ok()?; + let url = rest.get(len..)?.strip_prefix(':')?; + let scheme = ["https://", "http://"].into_iter().find(|s| url.get(..s.len()).is_some_and(|head| head.eq_ignore_ascii_case(s)))?; + let header_len = text.len() - url.len(); + let limit = 16384usize.checked_sub(header_len + scheme.len())?; + let authority = &url[scheme.len()..]; + let end = authority + .char_indices() + .take_while(|(at, _)| *at < limit) + .find(|(_, c)| matches!(*c, '/' | '?' | '#' | '\\') || c.is_whitespace() || c.is_control()) + .map(|(at, _)| at) + .or_else(|| (authority.len() <= limit).then_some(authority.len()))?; + let parsed = |end| { + text.get(..header_len + scheme.len() + end).and_then(|candidate| rustgis_data::login_address::parse(candidate).ok().flatten()) + }; + if parsed(end).is_none() { + // A bare host can be followed immediately by prose punctuation. Retry without that + // suffix, but retain the closing bracket of an IPv6 host. Keep the original URL slice: + // trimming here only chooses what the shared parser validates, not what we preserve. + let authority = authority.get(..end)?; + let ipv6_close = authority.strip_prefix('[').and_then(|rest| rest.find(']')).map(|at| at + 1); + let trimmed = authority + .char_indices() + .rev() + .find(|(at, c)| Some(*at) == ipv6_close || !matches!(*c, ']' | ')' | '}' | '"' | '\'' | ',' | ';' | '!' | '>' | ':' | '.')) + .map_or(0, |(at, c)| at + c.len_utf8()); + parsed(trimmed)?; + } + Some(url) +} + +/// 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_recorded_login_bindings(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_recorded_login_bindings(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 +650,53 @@ 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. + 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); + 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); + // A node's input may be an address rather than a layer's name. + if let Some(layer) = &mut node.layer { + addresses.push(layer); + } + } + } + } + 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. 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() && 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| only_bound(a))); + } + } + n + } + /// The standalone table of an id. pub fn table(&self, id: u32) -> Option<&Table> { self.tables.iter().find(|t| t.id == id) @@ -769,8 +951,10 @@ impl Project { /// Refuses text larger than the opening limit, restoring live paths and addresses on failure. 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() @@ -801,6 +985,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(); + } + } let text = text?; if text.len() as u64 > MAX_BYTES { return Err(format!("the project is too large to save (maximum {} MiB)", MAX_BYTES >> 20)); @@ -841,6 +1030,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()); @@ -856,7 +1046,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()); } } } @@ -906,6 +1096,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); } @@ -974,3 +1166,241 @@ 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 embedded_recorded_bindings_are_removed_from_saves_recovery_and_exports() { + let first = "https://project757.example.org/first.csv?token=ordinary#part"; + let second = "https://project757.example.org/second.csv"; + let note = format!("préface [{}], then [{}]; tail", bound(first), bound(second)); + let plain = format!("préface [{first}], then [{second}]; tail"); + let clean = "ordinary abc123 https://user@project757.example.org/data.csv?token=abc123#part"; + let params = serde_json::json!({"nested": [{"note": note}], "clean": clean}); + let mut session = crate::Session::new(Project::default()); + session + .run( + "task.new", + &serde_json::json!({"name": "Recorded", "steps": [{"title": "Read", "command": "layer.addUrl", "params": params}]}), + ) + .unwrap(); + session.run("model.new", &serde_json::json!({"name": "Recorded", "steps": [{"tool": "buffer", "params": params}]})).unwrap(); + session.project.gp_history.push(GpRecord { + tool: "Recorded".into(), + id: "buffer".into(), + params: params.clone(), + command: Some(("layer.addUrl".into(), params.clone())), + at: 0, + ok: true, + messages: Vec::new(), + millis: 0, + outputs: Vec::new(), + }); + let forged = serde_json::to_string(&session.project).unwrap(); + let (saved, stripped) = session.project.saved_text(None).unwrap(); + assert_eq!(stripped, 0); + let recovery = session.project.clone().recovery_text().unwrap(); + for text in [&saved, &recovery] { + assert!(!text.contains(rustgis_data::login_address::PREFIX)); + assert!(!text.contains("0123456789abcdef")); + } + for text in [&saved, &recovery, &forged] { + let mut opened = Project::from_json(text).unwrap(); + for value in opened.recorded_mut() { + assert!(!value.contains(rustgis_data::login_address::PREFIX)); + assert!(value.as_str() == plain || value.as_str() == clean, "{value}"); + } + assert_eq!(opened.tasks[0].steps[0].params["nested"][0]["note"], plain); + assert_eq!(opened.models[0].steps[0].params["nested"][0]["note"], plain); + assert_eq!(opened.gp_history[0].params["nested"][0]["note"], plain); + assert_eq!(opened.gp_history[0].command.as_ref().unwrap().1["nested"][0]["note"], plain); + } + let path = std::env::temp_dir().join(format!("recorded-text-757-{}.rgistask", std::process::id())); + session.run("task.save", &serde_json::json!({"task": "Recorded", "path": path.to_string_lossy()})).unwrap(); + let task = std::fs::read_to_string(&path).unwrap(); + std::fs::remove_file(path).unwrap(); + let model = session.run("model.export", &serde_json::json!({"model": "Recorded"})).unwrap(); + for text in [task.as_str(), model["text"].as_str().unwrap()] { + assert!(!text.contains(rustgis_data::login_address::PREFIX)); + assert!(!text.contains("0123456789abcdef")); + assert!(text.contains(&plain)); + assert!(text.contains(clean)); + } + assert_eq!(session.project.tasks[0].steps[0].params, params); + assert_eq!(session.project.models[0].steps[0].params, params); + assert_eq!(session.project.gp_history[0].params, params); + assert_eq!(session.project.gp_history[0].command.as_ref().unwrap().1, params); + } + + #[test] + fn recorded_binding_scanner_preserves_malformed_and_wrapped_values() { + let public = "https://project757.example.org/archive.pmtiles"; + let url = bound(public); + for (wrapped, plain) in [ + (format!("pmtiles:{{z}}/{{x}}/{{y}}@{url}"), format!("pmtiles:{{z}}/{{x}}/{{y}}@{public}")), + (format!("rustgis-s3:0123456789abcdef:0::{url}"), format!("rustgis-s3:0123456789abcdef:0::{public}")), + ] { + let mut value = wrapped; + clear_login_bindings([&mut value]); + assert_eq!(value, plain); + } + let malformed = [ + "rustgis-login:nothex:5:https://project757.example.org/path", + "rustgis-login:0123456789abcdef:999999999999999999999999:https://project757.example.org/path", + "rustgis-login:0123456789abcdef:1:é:https://project757.example.org/path", + "rustgis-login:0123456789abcdef:9999:short", + ]; + for bad in malformed { + let mut unchanged = bad.to_string(); + clear_login_bindings([&mut unchanged]); + assert_eq!(unchanged, bad); + let mut mixed = format!("before {bad} then {url} after"); + clear_login_bindings([&mut mixed]); + assert_eq!(mixed, format!("before {bad} then {public} after")); + } + let mut long = format!("{} {url} {}", "é".repeat(20_000), "tail ".repeat(4_000)); + clear_login_bindings([&mut long]); + assert_eq!(long, format!("{} {public} {}", "é".repeat(20_000), "tail ".repeat(4_000))); + } + + #[test] + fn recorded_bare_origins_keep_prose_punctuation_and_validate_the_binding() { + for (origin, url) in [ + ("https://project757.example.org", "https://project757.example.org"), + ("https://project757.example.org", "HTTPS://PROJECT757.EXAMPLE.ORG.:443"), + ("https://project757.example.org:8443", "https://project757.example.org:8443"), + ("https://[::1]", "https://[::1]"), + ("https://[::1]", "HTTPS://[0:0:0:0:0:0:0:1]:443"), + ] { + let bound = rustgis_data::login_address::bind("0123456789abcdef", origin, url).unwrap(); + for (left, right) in [("[", "]"), ("(", ")"), ("\"", "\""), ("[{(", ")}];"), ("", "!")] { + let mut text = format!("before {left}{bound}{right} after"); + clear_login_bindings([&mut text]); + assert_eq!(text, format!("before {left}{url}{right} after")); + } + } + let origin = "https://project757.example.org"; + for invalid in [ + format!("rustgis-login:0123456789abcdef:{}:{origin}:https://foreign757.example.org", origin.len()), + format!("rustgis-login:nothex:{}:{origin}:{origin}", origin.len()), + format!("rustgis-login:0123456789abcdef:99999999999999999999:{origin}:{origin}"), + ] { + let original = format!("before [{invalid}] after"); + let mut text = original.clone(); + clear_login_bindings([&mut text]); + assert_eq!(text, original); + } + } + + #[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"); + + 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 new file mode 100644 index 0000000000..f539b570cb --- /dev/null +++ b/crates/engine/src/service_logins.rs @@ -0,0 +1,940 @@ +//! 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."; +/// 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. +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()); + } + // 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(()) +} + +/// 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) + } + + /// 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) + } +} + +impl Default for SessionLogins { + fn default() -> Self { + Self::new() + } +} + +/// 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; + 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. +/// Returns the number of addresses that could not be rebound; resolution reports it once. +pub(crate) fn activate(s: &mut Session, c: &Connection, secret: Option<&Secret>, insecure: bool) -> Result { + 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); + } + let failed = 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 { + clear_cache(); + } + Ok(failed) +} + +/// 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. +/// Returns the full connection's failed-address count for resolution to report. +pub(crate) fn reselect(s: &mut Session, c: &Connection, origin: &str) -> usize { + 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); + 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)) +} + +/// 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; so is one a +/// plugin sets there. +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 !at_origin || rustgis_data::login_address::is_wrapper(url) { + return Ok(url.to_string()); + } + // 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() { + 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()); + } + 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 { + 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 { + // 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, + 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) { + // Confirmed already: a web connection's session secret is kept only after its login passed + // the plain-http:// check (`connection.login`), and is forgotten when the connection's + // server changes (`connection.update`), so it never reaches a server nobody confirmed. + 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 || 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 { + 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. +/// Keeps failed addresses unchanged and returns their count without reporting it mid-activation. +fn rebind_connection(s: &mut Session, c: &Connection) -> usize { + let binding = s.web_logins.active.get(&c.id).map(|a| a.binding.as_str()); + let origin = login_origin(c); + let mut failed = 0usize; + let mut rewrite = |address: &mut String| { + let (prefix, raw) = crate::sources::template_archive(address).unwrap_or(("", address.as_str())); + let plain = logins::inner(raw); + let next = if let Some(binding) = binding + && logins::origin(plain) == origin + { + match logins::bind(binding, plain) { + Ok(next) => next, + Err(_) => { + failed += 1; + return false; + } + } + } else { + plain.to_string() + }; + let next = if prefix.is_empty() { next } else { format!("{prefix}{next}") }; + if *address != next { + *address = next; + true + } else { + false + } + }; + for m in &mut s.project.maps { + 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); + } + 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.anonymous && l.connection == c.id) { + if let Some(source) = l.source.as_mut() + && let Some(path) = source.path.as_mut() + { + let changed = rewrite(path); + // 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 changed { + if source.is_open() { + let _ = source.reopen(); + } else { + source.error = None; + } + } + } + if let Some(raster) = l.raster.as_mut() + && let Some(path) = raster.path.as_mut() + && rewrite(path) + { + // Reload keeps the style and publishes any failure through raster.error. + let _ = raster.reload(); + } + if let Some(path) = &mut l.dataset.source { + rewrite(path); + } + } + 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 && !l.anonymous).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); + } + } + } + } + failed +} + +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.anonymous && 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| !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 { + 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(); + } + } + } +} + +/// 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.iter().filter(|t| !t.anonymous) { + let recorded_origin = s.connections.connections.iter().find(|c| c.id == t.connection && is_service(c)).and_then(login_origin); + // A dated link on another origin still belongs to the recorded connection, not to + // that other origin's saved login. If the connection is gone, only the displayed + // address is evidence for the request: do not guess which dated origin owned it. + let origin = if let Some(recorded) = recorded_origin { + std::iter::once(t.url.as_str()) + .chain(t.time_url.as_deref()) + .chain(t.time_urls.iter().map(|(_, url)| url.as_str())) + .any(|url| logins::origin(tile_address(url)).as_deref() == Some(recorded.as_str())) + .then_some(recorded) + .or_else(|| logins::origin(tile_address(&t.url))) + } else { + logins::origin(tile_address(&t.url)) + }; + let Some(origin) = origin 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.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)) { + 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.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); + 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); + } + 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>) { + // 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(); + let mut rebind_failures = BTreeMap::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) && 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 { + 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); + } + let rebound = if c.auth.is_empty() || logged_in(s, &c) { + Ok(rebind_connection(s, &c)) + } else { + // A secret given this session was confirmed for this origin when it was typed. + known_secret(s, &c).and_then(|secret| activate(s, &c, Some(&secret), true)) + }; + match rebound { + Ok(failed) => { + // Each rebind covers the whole connection, not only this read entry. + if failed > 0 { + rebind_failures.insert(c.id, (c.name, failed)); + } else { + rebind_failures.remove(&c.id); + } + } + 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 (_, (name, failed)) in rebind_failures { + if failed > 0 { + s.notify(format!("{name}: {failed} address(es) could not carry the service login; their previous addresses were kept."), true); + } + } + 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 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 +/// 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 + .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> { + let anonymous = s.web_logins.is_anonymous(); + if !anonymous && !ADD_COMMANDS.contains(&id) { + return Ok(None); + } + if !anonymous { + named(s, params)?; + } + Ok(Some(Before { + layers: s.project.maps.iter().map(|m| m.layers.iter().map(|l| l.instance_id()).collect()).collect(), + groups: if anonymous { + s.project.maps.iter().map(|m| m.service_groups.iter().map(|g| g.id).collect()).collect() + } else { + Vec::new() + }, + 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 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. + 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, + } + }; + let mut old_tiles = before.tiles.into_iter(); + for (i, m) in s.project.maps.iter_mut().enumerate() { + 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 && (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 { + t.anonymous = true; + t.connection.clear(); + 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.instance_id()))) { + added.insert(l.id); + if anonymous || l.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; + } + if layer_address(l).is_some() { + origins.extend(origin); + } + } + // An imported service's group is refreshed from its address later. + for g in &mut m.service_groups { + if anonymous && before.groups.get(i).is_none_or(|old| !old.contains(&g.id)) { + g.anonymous = true; + } + if !anonymous && !g.anonymous && 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)) + { + if anonymous || t.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; + } + 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 e5e18421fe..c9e9f5e770 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() { @@ -2920,6 +2922,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. @@ -14743,7 +14748,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. @@ -16013,7 +16018,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. @@ -17011,6 +17016,7 @@ fn saved_projects_keep_no_layer_tokens() { name: "Protected service".into(), url: "https://gis.example/wfs?key=k3y&map=m".into(), kind: "wfs".into(), + anonymous: false, expanded: true, children: vec![rustgis_map::ServiceChild { layer, @@ -17121,7 +17127,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(); @@ -17325,7 +17333,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()); @@ -17355,7 +17363,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..6437dc508c --- /dev/null +++ b/crates/engine/src/tests/service_logins.rs @@ -0,0 +1,1280 @@ +//! 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() +} + +#[cfg(not(target_arch = "wasm32"))] +struct LoginFile { + base: String, + requests: Arc>>, + stop: Arc, + worker: Option>, +} + +#[cfg(not(target_arch = "wasm32"))] +impl LoginFile { + fn new(body: Vec) -> Self { + use std::io::{Read, Write}; + use std::sync::atomic::{AtomicBool, Ordering}; + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + listener.set_nonblocking(true).unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let stop = Arc::new(AtomicBool::new(false)); + let requests = Arc::new(std::sync::Mutex::new(Vec::new())); + let (stopping, log) = (stop.clone(), requests.clone()); + let worker = std::thread::spawn(move || { + while !stopping.load(Ordering::Acquire) { + let (mut stream, _) = match listener.accept() { + Ok(stream) => stream, + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { + std::thread::sleep(std::time::Duration::from_millis(2)); + continue; + } + Err(_) => break, + }; + stream.set_read_timeout(Some(std::time::Duration::from_secs(5))).unwrap(); + let mut request = Vec::new(); + let mut chunk = [0u8; 4096]; + while request.len() < 32768 && !request.windows(4).any(|w| w == b"\r\n\r\n") { + let cap = chunk.len().min(32768 - request.len()); + match stream.read(&mut chunk[..cap]) { + Ok(0) | Err(_) => break, + Ok(n) => request.extend_from_slice(&chunk[..n]), + } + } + let request = String::from_utf8_lossy(&request).into_owned(); + let authorized = request.to_ascii_lowercase().contains("\r\nx-key: fixture-login-757\r\n") + || request.split_whitespace().nth(1).is_some_and(|p| p.starts_with("/public")); + log.lock().unwrap().push(request); + let (status, bytes) = if authorized { ("200 OK", body.as_slice()) } else { ("401 Unauthorized", &[][..]) }; + let _ = write!(stream, "HTTP/1.1 {status}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", bytes.len()); + let _ = stream.write_all(bytes); + } + }); + Self { base, requests, stop, worker: Some(worker) } + } +} + +#[cfg(not(target_arch = "wasm32"))] +impl Drop for LoginFile { + fn drop(&mut self) { + self.stop.store(true, std::sync::atomic::Ordering::Release); + if let Some(worker) = self.worker.take() { + worker.join().unwrap(); + } + } +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_reopened_http_raster_recovers_its_pixels_after_login_without_reloading_unchanged_paths() { + let bytes = rustgis_data::raster::cog::example(256, 256); + let pixels = rustgis_data::raster::read_bytes("scene.tif", &bytes).unwrap(); + let server = LoginFile::new(bytes); + let mut seed = session(None); + let id = new(&mut seed, json!({"kind":"ogcapi","address":format!("{}/collections",server.base),"auth":"header","keyName":"X-Key"})); + seed.run("connection.login", &json!({"id":id,"apiKey":"fixture-login-757","allowInsecure":true})).unwrap(); + let mut layer = rustgis_map::Layer::new(1, rustgis_data::Dataset::new("Secured raster", rustgis_data::GeometryType::None, Vec::new())); + layer.connection = id.clone(); + layer.raster = Some(rustgis_map::RasterLayer::from_path(pixels, bound(&mut seed, &id, &format!("{}/scene.tif", server.base)))); + layer.transparency = 0.25; + let style = serde_json::to_value(&layer.raster.as_ref().unwrap().style).unwrap(); + seed.map_mut().layers.push(layer); + let text = seed.project.saved_text(None).unwrap().0; + let mut reopened = session(None); + reopened.connections = crate::connections::Connections::from_json(&seed.connections.to_json()); + reopened.run("project.openText", &json!({"text":text})).unwrap(); + assert!(reopened.map().layers[0].raster.as_ref().unwrap().data.is_none()); + assert_eq!(requests(&mut reopened)[0]["reason"], "password"); + let from = server.requests.lock().unwrap().len(); + reopened.run("connection.login", &json!({"id":id,"apiKey":"fixture-login-757","allowInsecure":true})).unwrap(); + let view = rustgis_geom::Extent::new(-180.0, -85.0, 180.0, 85.0); + let scale = reopened.map().scale(); + let start = std::time::Instant::now(); + while reopened.map_mut().sync_sources(&view, scale, false) { + assert!(start.elapsed() < std::time::Duration::from_secs(30), "raster recovery timed out"); + std::thread::sleep(std::time::Duration::from_millis(5)); + } + let layer = &reopened.map().layers[0]; + let raster = layer.raster.as_ref().unwrap(); + let pixels = raster.data.as_ref().expect("login must restart the failed raster read"); + assert_eq!((pixels.width, pixels.height), (256, 256)); + assert!(raster.error.is_none(), "{:?}", raster.error); + assert_eq!(layer.transparency, 0.25); + assert_eq!(serde_json::to_value(&raster.style).unwrap(), style); + let requests = server.requests.lock().unwrap(); + assert_eq!(requests[from..].len(), 1); + assert!(requests[from].to_ascii_lowercase().contains("\r\nx-key: fixture-login-757\r\n")); + drop(requests); + let pixels = pixels.clone(); + let count = server.requests.lock().unwrap().len(); + for _ in 0..3 { + web::resolve(&mut reopened, None); + assert!(!reopened.map_mut().sync_sources(&view, scale, false)); + } + assert!(Arc::ptr_eq(&pixels, reopened.map().layers[0].raster.as_ref().unwrap().data.as_ref().unwrap())); + assert_eq!(server.requests.lock().unwrap().len(), count); +} + +#[test] +fn a_reopened_dated_tile_authenticates_its_recorded_origin_not_the_displayed_foreign_origin() { + let (a_origin, b_origin) = ("https://dates-a757.example.org", "https://dates-b757.example.org"); + let mut seed = session(None); + let a = new(&mut seed, json!({"kind":"wmts","address":a_origin,"auth":"header","keyName":"X-Key"})); + let b = new(&mut seed, json!({"kind":"wmts","address":b_origin,"auth":"header","keyName":"X-Key"})); + let mut tile = rustgis_map::TileLayer::new("Dates", rustgis_map::TileKind::Raster, format!("{b_origin}/b/{{z}}/{{x}}/{{y}}.png")); + tile.connection = a.clone(); + tile.time_urls = vec![(1704067200000.0, format!("{a_origin}/a/{{z}}/{{x}}/{{y}}.png")), (1704153600000.0, tile.url.clone())]; + seed.map_mut().tiles.push(tile); + seed.run("time.set", &json!({"dates":["2024-01-01","2024-01-02"],"date":"2024-01-02"})).unwrap(); + let text = seed.project.saved_text(None).unwrap().0; + let mut reopened = session(None); + reopened.connections = crate::connections::Connections::from_json(&seed.connections.to_json()); + reopened.run("connection.login", &json!({"id":b,"apiKey":"dated-b-757"})).unwrap(); + reopened.run("project.openText", &json!({"text":text})).unwrap(); + let pending = requests(&mut reopened); + assert_eq!(pending.as_array().unwrap().len(), 1, "{pending}"); + assert_eq!(pending[0]["origin"], a_origin); + assert_eq!(pending[0]["connections"], json!([a])); + assert_eq!(pending[0]["layers"], 1, "the dated addresses count as one tile"); + assert!(header(&reopened.map().tiles[0].url).is_none()); + reopened.run("connection.login", &json!({"id":a,"apiKey":"dated-a-757"})).unwrap(); + assert_eq!(requests(&mut reopened), json!([])); + assert!(header(&reopened.map().tiles[0].url).is_none(), "B stays anonymous even with B logged in"); + reopened.run("time.set", &json!({"date":"2024-01-01"})).unwrap(); + assert_eq!(header(&reopened.map().tiles[0].url).unwrap().1, "dated-a-757"); + reopened.run("time.set", &json!({"date":"2024-01-02"})).unwrap(); + assert!(header(&reopened.map().tiles[0].url).is_none()); + assert_eq!(reopened.map().tiles[0].connection, a); +} + +#[test] +fn a_missing_dated_tiles_connection_asks_only_about_its_displayed_origin() { + let mut s = session(None); + let b = new(&mut s, json!({"kind":"wmts","address":"https://missing-dates-b757.example.org","auth":"header","keyName":"X-Key"})); + s.run("connection.login", &json!({"id":b,"apiKey":"missing-b-757"})).unwrap(); + let mut p = Project::default(); + let mut tile = rustgis_map::TileLayer::new( + "Unknown dates", + rustgis_map::TileKind::Raster, + "https://missing-dates-b757.example.org/b/{z}/{x}/{y}.png", + ); + tile.connection = "gone".into(); + tile.time_urls = + vec![(1704067200000.0, "https://missing-dates-a757.example.org/a/{z}/{x}/{y}.png".into()), (1704153600000.0, tile.url.clone())]; + p.map_mut().tiles.push(tile); + let mut seed = Session::new(p); + seed.run("time.set", &json!({"dates":["2024-01-01","2024-01-02"],"date":"2024-01-02"})).unwrap(); + let text = seed.project.saved_text(None).unwrap().0; + s.run("project.openText", &json!({"text":text})).unwrap(); + let pending = requests(&mut s); + assert_eq!(pending.as_array().unwrap().len(), 1); + assert_eq!(pending[0]["reason"], "connection"); + assert_eq!(pending[0]["origin"], "https://missing-dates-b757.example.org"); + assert!(header(&s.map().tiles[0].url).is_none()); + assert_eq!(s.map().tiles[0].connection, "gone"); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn an_oversized_ogc_items_binding_is_refused_before_native_fetch() { + use crate::render::rustgis_tiles::cache; + let server = LoginFile::new(br#"{"type":"FeatureCollection","features":[]}"#.to_vec()); + let public = LoginFile::new(b"public".to_vec()); + let mut s = session(None); + let id = new(&mut s, json!({"kind":"ogcapi","address":format!("{}/collections",server.base),"auth":"header","keyName":"X-Key"})); + s.run("connection.login", &json!({"id":id,"apiKey":"fixture-login-757","allowInsecure":true})).unwrap(); + let address = bound(&mut s, &id, &format!("{}/collections", server.base)); + let long = format!("{}/items?filter={}&limit=10&f=json", server.base, "a".repeat(17000)); + let document = serde_json::to_vec(&json!({"collections":[ + {"id":"long","links":[{"rel":"items","type":"application/geo+json","href":long}]}, + {"id":"valid","links":[{"rel":"items","type":"application/geo+json","href":format!("{}/items?limit=10",server.base)}]}, + {"id":"foreign","links":[{"rel":"items","type":"application/geo+json","href":format!("{}/public?limit=10",public.base)}]} + ]})) + .unwrap(); + let service = rustgis_data::ogc::parse(&address, &document).unwrap(); + let items = |id: &str| match &service.layers.iter().find(|l| l.name == id).unwrap().offer { + rustgis_data::ogc::Offer::Features { url } => url.as_str(), + _ => panic!("feature offer"), + }; + let oversized = items("long"); + assert!(rustgis_data::login_address::is_wrapper(oversized)); + assert!(rustgis_data::login_address::parse(oversized).is_err()); + assert!(cache::download_blocking(oversized).is_err()); + assert!(server.requests.lock().unwrap().is_empty(), "an oversized binding must never fetch anonymously"); + assert!(cache::download_blocking(items("valid")).is_ok()); + assert!(server.requests.lock().unwrap()[0].to_ascii_lowercase().contains("\r\nx-key: fixture-login-757\r\n")); + assert!(!rustgis_data::login_address::is_wrapper(items("foreign"))); + assert_eq!(cache::download_blocking(items("foreign")).unwrap(), b"public"); + assert!(!public.requests.lock().unwrap()[0].to_ascii_lowercase().contains("\r\nx-key:")); +} + +#[test] +fn failed_same_origin_rebinding_keeps_the_old_address_and_reports_its_count() { + let mut s = session(None); + let origin = "https://rebind-limit757.example.org"; + let id = new(&mut s, json!({"kind":"wmts","address":origin,"auth":"header","keyName":"X-Key"})); + let long = format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png?filter={}", "a".repeat(17000)); + let mut tile = rustgis_map::TileLayer::new("Long", rustgis_map::TileKind::Raster, &long); + tile.connection = id.clone(); + s.map_mut().tiles.push(tile); + let mark = s.notice_mark(); + s.run("connection.login", &json!({"id":id,"apiKey":"rebind-limit-757"})).unwrap(); + assert_eq!(s.map().tiles[0].url, long); + let notices = s.notices_since(mark); + assert_eq!(notices.len(), 1); + let counts: Vec = notices[0].text.split_whitespace().filter_map(|word| word.parse().ok()).collect(); + assert_eq!(counts, [1], "count failed rebinding without treating foreign-origin anonymity as an error"); + s.map_mut().tiles[0].url = "https://foreign-rebind757.example.org/tiles/{z}/{x}/{y}.png".into(); + let mark = s.notice_mark(); + s.run("connection.login", &json!({"id":id,"apiKey":"rebind-limit-757"})).unwrap(); + assert!(s.notices_since(mark).is_empty()); +} + +#[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 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); + 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(), + anonymous: false, + 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 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(), + anonymous: false, + 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); + 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); + // 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); + 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(); + // 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"] { + 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":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(); + 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 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 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 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); + 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_service_groups_stay_anonymous_through_refresh_and_reopen() { + super::isolate_plugins(); + for (plugin, import_map, auth) in [("eng-group-map-login-757", true, "basic"), ("eng-group-service-login-757", false, "header")] { + let origin = format!("https://{plugin}.example.org"); + let url = format!("{origin}/collections"); + let collections = |names: &[&str]| { + json!({"collections":names.iter().map(|name| json!({ + "id":name,"title":name, + "links":[{"rel":"items","type":"application/geo+json","href":format!("{url}/{name}/items")}] + })).collect::>()}) + .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(); + 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); + 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); +} + +#[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"); +} + +#[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}"); + // 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}"); + // Neither the project package nor the results package (its history.json) keeps one. + for (command, file) in [("project.package", "project"), ("gp.package", "results")] { + let path = std::env::temp_dir().join(format!("gp757-{file}-{}.rgispkg", std::process::id())); + s.run(command, &json!({"path": path.to_string_lossy()})).unwrap(); + let bytes = std::fs::read(&path).unwrap(); + let _ = std::fs::remove_file(&path); + 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), "{command}: {name} keeps a binding: {entry}"); + } + } +} + +#[test] +fn templates_tasks_and_models_written_out_keep_no_login_binding() { + let mut s = Session::new(crate::sample::world_project()); + let id = new(&mut s, json!({"kind":"wmts","address":"https://export757.example.org/tiles","auth":"header","keyName":"X-Key"})); + s.run("connection.login", &json!({"id":id,"apiKey":"export-sentinel-757"})).unwrap(); + let tile = bound(&mut s, &id, "https://export757.example.org/tiles/{z}/{x}/{y}.png"); + s.run("map.addTiles", &json!({"url": tile, "name": "Bound"})).unwrap(); + s.run("task.new", &json!({"name":"Fetch","steps":[{"title":"Add","command":"map.addTiles","params":{"url": tile}}]})).unwrap(); + s.run("model.new", &json!({"name":"Tiles","steps":[{"tool":"buffer","params":{"input": tile, "distance": 1}}]})).unwrap(); + // A model drawn as a graph keeps its input's address in the node itself. + let graph = json!({"nodes":[{"id":"in","kind":"input","layer": tile}], "edges": []}); + s.run("model.new", &json!({"name":"Drawn","graph": graph})).unwrap(); + let dir = std::env::temp_dir().join(format!("export757-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let (template, task) = (dir.join("bound.rgistemplate"), dir.join("bound.rgistask")); + s.run("project.saveTemplate", &json!({"path": template.to_string_lossy()})).unwrap(); + s.run("task.save", &json!({"task":"Fetch","path": task.to_string_lossy()})).unwrap(); + let model = s.run("model.export", &json!({"model":"Tiles"})).unwrap()["text"].as_str().unwrap().to_string(); + let written = [std::fs::read_to_string(&template).unwrap(), std::fs::read_to_string(&task).unwrap(), model]; + let _ = std::fs::remove_dir_all(&dir); + for text in &written { + assert!(!text.contains(rustgis_data::login_address::PREFIX) && !text.contains("export-sentinel-757"), "{text}"); + assert!(text.contains("export757.example.org/tiles"), "the plain address stays: {text}"); + } +} + +#[test] +fn a_legend_that_cant_carry_the_login_is_refused_not_read_without_it() { + let origin = "https://legend-space757.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":"space-sentinel-757"})).unwrap(); + s.run("map.addTiles", &json!({"url":format!("{origin}/tiles/{{z}}/{{x}}/{{y}}.png"),"name":"T","connection":id})).unwrap(); + let error = s.run("map.setTiles", &json!({"name":"T","legend":format!("{origin}/my legend.png")})).unwrap_err(); + 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:?}"); +} + +#[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}"); +} diff --git a/crates/map/src/lib.rs b/crates/map/src/lib.rs index 8280127820..091040d143 100644 --- a/crates/map/src/lib.rs +++ b/crates/map/src/lib.rs @@ -211,6 +211,16 @@ 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, + /// 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, @@ -403,6 +413,8 @@ impl Layer { source: None, postgis: None, gpkg: None, + connection: String::new(), + anonymous: false, projected: Default::default(), display: None, display_rejected: Vec::new(), @@ -1104,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, @@ -1382,8 +1397,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 { @@ -1400,6 +1415,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); @@ -2946,7 +2964,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/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..4932d425db 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,15 @@ 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, + /// 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)] @@ -200,7 +210,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 +381,8 @@ impl TileLayer { error: None, feature_info: None, legends: Vec::new(), + connection: String::new(), + anonymous: false, expanded: false, selected: false, single_image: false, @@ -423,11 +436,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 +501,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 +530,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 +560,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 +574,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 228d099b97..07ecdf8ac7 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -198,6 +198,9 @@ const TILE_OMIT: &[u16] = &[404, 410]; /// [`get`] with the statuses that are expected answers for this fetch. fn request(url: &str, want: Want, omit: &'static [u16]) -> 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) { @@ -244,6 +247,7 @@ fn request(url: &str, want: Want, omit: &'static [u16]) -> 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); } @@ -502,7 +506,7 @@ fn fetch_tile(url: String, host: String, want: Want, omit: &'static [u16]) { 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()) } }); @@ -676,6 +680,14 @@ impl<'a> RequestDiagnostics<'a> { let (url, _) = split_download_limit(self.url); let (url, tail) = split_tail(url); let (url, range) = split_range(url); + // A request carrying a login is recorded by its plain address and scrubbed text: neither + // the binding that names the login nor a credential belongs in the history. + let carried = rustgis_data::login_address::is_wrapper(url); + let url = crate::logins::inner(url); + let url = if rustgis_data::login_address::is_wrapper(url) { "[address omitted]" } else { url }; + let clean = |text: &str| { + if carried || text.contains(rustgis_data::login_address::PREFIX) { crate::logins::scrub(text) } else { text.to_string() } + }; // A status the caller expects is an answer, not a failure for the history; a caller // that treats the refusal as an error says so itself. One bounded record per server says // the omission, so a misconfigured tile source isn't silent. @@ -696,7 +708,7 @@ impl<'a> RequestDiagnostics<'a> { if let Some(answer) = &failure.answer { for (name, value) in &answer.headers { if crate::failure::KEPT_HEADERS.contains(&name.as_str()) { - details.push_str(&rustgis_data::diagnostics::redact(&format!("{name}: {value}"))); + details.push_str(&rustgis_data::diagnostics::redact(&clean(&format!("{name}: {value}")))); details.push('\n'); } } @@ -713,8 +725,8 @@ impl<'a> RequestDiagnostics<'a> { }; self.capture.network_failure( "Network", - &failure.message, - &details, + &clean(&failure.message), + &clean(&details), Network { url: url.to_string(), method: self.method.to_string(), @@ -781,7 +793,8 @@ fn download_detailed_within(url: &str, max: usize, timeout: std::time::Duration, // 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(); @@ -888,8 +901,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(); @@ -953,8 +967,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(); @@ -968,6 +983,12 @@ pub fn head(url: &str, done: HeadDone) { /// A status in `omit` is an expected answer: its failure isn't recorded, only noted once per /// server (a tile past a map's edge). pub(crate) fn fetch_bytes(url: String, done: Done, omit: &'static [u16]) { + 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, omit), @@ -1029,6 +1050,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. @@ -1195,7 +1223,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. @@ -1339,14 +1369,25 @@ mod native { /// them. pub(super) fn head_with(agent: &ureq::Agent, url: &str) -> Result { let mut diagnostics = super::RequestDiagnostics::new(url, "HEAD"); + let mut carried_login = false; let answer = (|| -> Result { - let prepared = crate::s3::prepare(url)?; + 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); + // The login goes to its own origin only: once a redirect leaves it, never again. + 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()); @@ -1362,6 +1403,7 @@ mod native { if at.starts_with("https://") && !next.starts_with("https://") { return Err(format!("{next}: a redirect from https to http").into()); } + same_origin &= crate::logins::origin(&next) == first_origin; at = next; continue; } @@ -1376,6 +1418,7 @@ mod native { .collect(); return Err(Failure::answered("", resp.status().as_u16(), &headers, &[])); } + 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), @@ -1384,7 +1427,7 @@ mod native { } Err("too many redirects".into()) })(); - diagnostics.record(answer).map_err(|failure| failure.message) + diagnostics.record(answer).map_err(|failure| if carried_login { crate::logins::scrub(&failure.message) } else { failure.message }) } /// POSTs a JSON document on a thread of its own. @@ -1402,13 +1445,21 @@ mod native { pub(super) fn post_with(agent: &ureq::Agent, url: &str, body: &str) -> Result, String> { use std::io::Read; let mut diagnostics = super::RequestDiagnostics::new(url, "POST"); + let mut carried_login = false; let answer = (|| -> Result, Failure> { - let mut resp = agent - .post(url) - .header("Content-Type", "application/json") - .header("Accept", "application/json") - .send(body) - .map_err(unanswered)?; + 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.send(body).map_err(unanswered)?; let status = resp.status().as_u16(); diagnostics.answered(status); if !(200..300).contains(&status) { @@ -1425,9 +1476,10 @@ mod native { if buf.len() > MAX_BYTES { return Err(format!("larger than {MAX_BYTES} bytes").into()); } + crate::logins::check(url).map_err(str::to_string)?; Ok(buf) })(); - diagnostics.record(answer).map_err(|failure| failure.message) + diagnostics.record(answer).map_err(|failure| if carried_login { crate::logins::scrub(&failure.message) } else { failure.message }) } /// Downloads on a thread of its own, so a large file doesn't hold up the tiles. @@ -1524,7 +1576,10 @@ mod native { /// [`get_detailed`], a status in `omit` not recorded as a failure. pub(super) fn get_detailed_omitting(agent: &ureq::Agent, url: &str, omit: &'static [u16]) -> Result, Failure> { - fetch_answer(agent, url, true, omit).map_err(|f| Failure { url: url.to_string(), ..f }) + fetch_answer(agent, url, true, omit).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 @@ -1536,9 +1591,12 @@ 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. A status in - /// `omit` is an expected answer: its failure isn't recorded, only noted once per server. + /// `omit` is an expected answer: its failure isn't recorded, only noted once per server. A + /// request carrying a login is recorded by its plain address and scrubbed text only + /// ([`RequestDiagnostics::failed`]). fn fetch_answer(agent: &ureq::Agent, url: &str, detail: bool, omit: &'static [u16]) -> Result, Failure> { use std::io::Read; + let mut carried_login = false; // Local file failures are not network traffic. let local = crate::mbtiles::is_tile(url) || url.starts_with(LOCAL); let mut diagnostics = super::RequestDiagnostics::new(url, "GET"); @@ -1560,11 +1618,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); + // The login goes to its own origin only: once a redirect leaves it, never again. + 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())); + 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 let Some((a, b)) = range { req = req.header("Range", &format!("bytes={a}-{b}")); } else if let Some(n) = tail { @@ -1626,10 +1694,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)) @@ -1663,6 +1730,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 { @@ -1720,7 +1788,15 @@ mod native { } Ok(buf) })(); - if local { answer } else { diagnostics.record(answer) } + let result = if local { answer } else { diagnostics.record(answer) }; + 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 + }) } } @@ -2239,9 +2315,15 @@ 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 }; @@ -2267,10 +2349,269 @@ mod tests { break; } } + let _ = sent.send(String::from_utf8_lossy(&request).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)); + } + + /// A failed request that carried a login is recorded by its plain address and scrubbed text: + /// neither the binding that names the login nor its secret reaches the history or its report. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn logins_diagnostics_never_record_the_binding_or_the_secret() { + use crate::failure::Reason; + use crate::logins::{self, Method}; + use rustgis_data::diagnostics::{Filter, History}; + let refused = b"HTTP/1.1 503 Unavailable\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".to_vec(); + let (base, asked) = serve_recorded(vec![refused; 5]); + let secret = "diagnostics-login-secret"; + let binding = logins::activate(&base, "diagnostics", Method::Header("X-Service-Key".into()), secret, true).unwrap(); + let history = History::default(); + let _scope = history.enter(); + let plain = format!("{base}/data.parquet"); + let url = logins::bind(&binding, &plain).unwrap(); + let prefix = rustgis_data::login_address::PREFIX; + assert!(url.contains(&binding) && url.contains(prefix)); + let agent = native::agent(); + // A whole read, a range read, an expected status (omitted: said once per server), a HEAD + // and a POST: every request goes with the login, and each failure is recorded. + native::get(&agent, &url, &[]).unwrap_err(); + native::get(&agent, &format!("{url}{RANGE}0-9"), &[]).unwrap_err(); + native::get(&agent, &url, &[503]).unwrap_err(); + native::head_with(&agent, &url).unwrap_err(); + native::post_with(&agent, &url, "{}").unwrap_err(); + for request in asked.iter().take(5) { + assert!(request.to_ascii_lowercase().contains(&format!("x-service-key: {secret}\r\n")), "{request}"); + } + // A server that echoes the credential, and a transport error that names the address. + let diagnostics = RequestDiagnostics::new(&url, "GET"); + diagnostics.failed(&Failure::without_answer("", format!("the server repeated {secret}"), Reason::Other)); + let echoed = vec![("www-authenticate".to_string(), format!("Bearer realm={secret}"))]; + diagnostics.failed(&Failure::answered("", 401, &echoed, &[])); + diagnostics.failed(&Failure::new(&url, format!("{url}: connection reset"))); + let records = history.records(); + // Five requests (the omitted status as a single absence note) and three synthetic failures. + assert_eq!(records.len(), 8, "{records:?}"); + assert_eq!(network_records(&history, &plain).len(), 8, "every record names the plain address: {records:?}"); + let everything = format!("{records:?}\n{}", history.report(Filter::All)); + for private in [binding.as_str(), secret, prefix] { + assert!(!everything.contains(private), "{private} reached the diagnostics: {everything}"); + } + assert!(everything.contains(&plain)); + assert!(logins::deactivate(&binding)); + } + + #[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"); + // 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(); + documents.push((address.clone(), serde_json::from_slice::(&bytes).unwrap(), 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 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(); + assert_eq!(resolved.sprites.len(), 1); + let sprite = &resolved.sprites[0].1; + 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(&resolved.sprites, 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 6768ee3a03..8e98a73785 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 { @@ -765,6 +774,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; @@ -813,6 +826,7 @@ fn get_tile(info: Arc, url: String, level: usize, idx: u32, then: Box