From 89a9eb6dbb728ef22aed2883c5ca1a2572f076ae Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Fri, 6 Nov 2026 23:06:50 +0100 Subject: [PATCH 1/6] fix(ui): test authenticated service connections before saving --- crates/engine/src/service_logins.rs | 52 ++++++ crates/engine/src/tests/service_logins.rs | 182 +++++++++++++++++++++ crates/ui-egui/src/connectionui.rs | 129 +++++++++++++-- crates/ui-egui/tests/service_logins/mod.rs | 161 +++++++++++++++++- docs/user-guide/data.md | 7 + 5 files changed, 515 insertions(+), 16 deletions(-) diff --git a/crates/engine/src/service_logins.rs b/crates/engine/src/service_logins.rs index f539b570cb..838e02bfd8 100644 --- a/crates/engine/src/service_logins.rs +++ b/crates/engine/src/service_logins.rs @@ -93,6 +93,58 @@ pub fn check_login_secret(c: &Connection, secret: &str) -> Result<(), String> { Ok(()) } +/// A form test owns its login independently of every saved connection and session. +/// Keep this guard alive until the fetch (including its asynchronous callback) has finished. +/// It is deliberately neither cloneable nor printable, and exposes no credential or binding. +#[must_use = "keep the temporary login alive until the service test fetch has finished"] +pub struct TemporaryProbe { + binding: String, +} + +impl Drop for TemporaryProbe { + fn drop(&mut self) { + // The networking registry also clears authenticated caches on revocation. Cache reads + // and responses arriving after this drop recheck the binding and cannot reuse it. + logins::deactivate(&self.binding); + } +} + +/// Binds an anonymous form-test probe to the login just typed in that form. This never reads +/// saved logins, selects a connection, or retains the typed credential in a session. +/// +/// `url` is the address returned by the existing anonymous `connection.test` probe. Only a +/// plain, credential-free address at the cleaned definition's exact origin may carry it. +pub fn temporary_probe(c: &Connection, secret: &str, insecure: bool, url: &str) -> Result<(String, TemporaryProbe), String> { + const BAD_DEFINITION: &str = "The service test requires a valid web-service connection and login."; + const BAD_ADDRESS: &str = "The service test requires a credential-free HTTP or HTTPS address at the connection's origin."; + if !c.id.is_empty() || !is_service(c) { + return Err(BAD_DEFINITION.into()); + } + let (c, stripped) = crate::connections::clean(c.clone()).map_err(|_| BAD_DEFINITION.to_string())?; + if stripped || crate::sources::redact(url) != url { + return Err(BAD_ADDRESS.into()); + } + let origin = login_origin(&c).ok_or(BAD_ADDRESS)?; + if rustgis_data::login_address::is_wrapper(url) || logins::origin(url).as_deref() != Some(origin.as_str()) { + return Err(BAD_ADDRESS.into()); + } + if c.auth.is_empty() { + return Err(BAD_DEFINITION.into()); + } + if secret.is_empty() { + return Err(REQUIRED.into()); + } + check_login_secret(&c, secret)?; + if origin.starts_with("http://") && !insecure { + return Err(INSECURE.into()); + } + let (method, value) = method(&c, secret).ok_or(BAD_DEFINITION)?; + let guard = TemporaryProbe { binding: logins::activate(&origin, "temporary-probe", method, &value, insecure).map_err(str::to_string)? }; + // Construct the guard before binding: even a too-long URL cannot leak a live login. + let bound = logins::bind(&guard.binding, url).map_err(|_| BAD_ADDRESS.to_string())?; + Ok((bound, guard)) +} + /// 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 { diff --git a/crates/engine/src/tests/service_logins.rs b/crates/engine/src/tests/service_logins.rs index b700d1f546..478f8df042 100644 --- a/crates/engine/src/tests/service_logins.rs +++ b/crates/engine/src/tests/service_logins.rs @@ -22,6 +22,188 @@ fn header(address: &str) -> Option<(String, String)> { logins::apply(address).unwrap().and_then(|a| a.header) } +fn probe_connection(kind: &str, address: &str, auth: &str, user: &str, key_name: &str) -> crate::connections::Connection { + crate::connections::Connection { + kind: kind.trim().into(), + address: address.into(), + auth: auth.into(), + user: user.into(), + key_name: key_name.into(), + ..crate::connections::Connection::default() + } +} + +#[test] +fn temporary_probes_use_cleaned_basic_inputs_and_revoke_on_unwind() { + let origin = "https://temporary-basic757.example.org"; + let url = format!("{origin}/wmts?SERVICE=WMTS&REQUEST=GetCapabilities"); + let definition = probe_connection("wmts", &format!(" {origin}/wmts "), " BASIC ", " ana ", "ignored"); + let (address, guard) = web::temporary_probe(&definition, "temporary-password-757", false, &url).unwrap(); + assert_eq!(header(&address), Some(("Authorization".into(), logins::basic("ana", "temporary-password-757")))); + assert_eq!(logins::inner(&address), url); + assert!(!address.contains("temporary-password-757")); + assert!(header(&url).is_none(), "the ordinary URL must stay anonymous"); + let inherited = logins::inherit(&address, &format!("{origin}/legend")); + assert!(header(&inherited).is_some()); + for foreign in [ + "http://temporary-basic757.example.org/legend", + "https://temporary-basic757.example.org:444/legend", + "https://temporary-basic757.example.org.evil.test/legend", + ] { + assert!(header(&logins::inherit(&address, foreign)).is_none()); + } + let unwound = std::panic::catch_unwind(move || { + let _guard = guard; + panic!("simulated test cancellation"); + }); + assert!(unwound.is_err()); + assert!(logins::apply(&address).is_err()); + assert!(logins::apply(&inherited).is_err()); + assert!(rustgis_data::login_address::check(&address).is_err()); + assert_eq!(logins::scrub("temporary-password-757"), "temporary-password-757", "revocation must also discard the credential"); +} + +#[test] +fn temporary_probes_never_replace_saved_logins_or_connection_selections() { + let origin = "https://temporary-saved757.example.org"; + let url = format!("{origin}/wmts"); + let mut s = session(Some(Arc::new(MemorySecrets::new()))); + let id = new(&mut s, json!({"kind":"wmts","address":url,"auth":"header","keyName":"X-Key"})); + s.run("connection.login", &json!({"id":id,"apiKey":"saved-temporary-757"})).unwrap(); + let saved = bound(&mut s, &id, &url); + let definitions = s.connections.clone(); + let pending = requests(&mut s); + assert!(web::temporary_probe(&s.connections.connections[0], "must-not-replace-saved-757", false, &url).is_err()); + let (first, first_guard) = + web::temporary_probe(&probe_connection("wmts", &url, "header", "", " X-Key "), "first-temporary-757", false, &url).unwrap(); + let (second, second_guard) = + web::temporary_probe(&probe_connection("wmts", &url, "header", "", "X-Key"), "second-temporary-757", false, &url).unwrap(); + assert_ne!(saved, first); + assert_ne!(first, second, "each test needs an isolated cache identity"); + assert_eq!(header(&first).unwrap().1, "first-temporary-757"); + assert_eq!(header(&second).unwrap().1, "second-temporary-757"); + assert_eq!(header(&saved).unwrap().1, "saved-temporary-757"); + assert_eq!(web::request(&mut s, None, &url).unwrap(), saved); + assert_eq!(web::request(&mut s, None, &first).unwrap_err(), web::NOT_THIS_SESSION); + drop(first_guard); + assert!(logins::apply(&first).is_err()); + assert_eq!(header(&second).unwrap().1, "second-temporary-757"); + assert_eq!(header(&saved).unwrap().1, "saved-temporary-757"); + drop(second_guard); + assert_eq!(s.connections, definitions); + assert_eq!(s.database_logins.selection(crate::connections::WEB, origin), Some(id.as_str())); + assert_eq!(requests(&mut s), pending); + assert_eq!(web::known_secret(&s, &s.connections.connections[0]).unwrap().expose(), "saved-temporary-757"); + assert_eq!(bound(&mut s, &id, &url), saved); +} + +#[test] +fn temporary_probes_reject_unsafe_definitions_credentials_and_mismatched_origins() { + let url = "https://temporary-validation757.example.org/wmts"; + let secret = "temporary-validation-secret-757"; + for (auth, user, key) in [("basic", "ana", ""), ("header", "", "X-Key")] { + let definition = probe_connection("wmts", url, auth, user, key); + assert_eq!(web::temporary_probe(&definition, "", false, url).err().unwrap(), web::REQUIRED); + assert_eq!(web::temporary_probe(&definition, "bad\nsecret", false, url).err().unwrap(), web::BAD_SECRET); + } + let invalid = [ + ("s3", url, "header", "", "X-Key", secret, false, url), + ("postgres", url, "header", "", "X-Key", secret, false, url), + ("wmts", url, "none", "", "", secret, false, url), + ("wmts", url, "unknown-auth-secret-757", "", "", secret, false, url), + ("wmts", url, "basic", "", "", secret, false, url), + ("wmts", url, "basic", "bad:user", "", secret, false, url), + ("wmts", url, "header", "", "Cookie", secret, false, url), + ("wmts", url, "header", "", "Sec-Key", secret, false, url), + ("wmts", url, "header", "", "X-Key", "", false, url), + ("wmts", url, "header", "", "X-Key", "injected\r\nX-Leak: secret", false, url), + ("wmts", url, "header", "", "X-Key", "non-ASCII-é", false, url), + ("wmts", "https://user:secret@temporary-validation757.example.org/wmts", "header", "", "X-Key", secret, false, url), + ("wmts", "https://temporary-validation757.example.org/wmts?token=secret", "header", "", "X-Key", secret, false, url), + ("wmts", "https://temporary-validation757.example.org\\evil/wmts", "header", "", "X-Key", secret, false, url), + ("wmts", url, "header", "", "X-Key", secret, false, "http://temporary-validation757.example.org/wmts"), + ("wmts", url, "header", "", "X-Key", secret, false, "https://temporary-validation757.example.org:444/wmts"), + ("wmts", url, "header", "", "X-Key", secret, false, "https://temporary-validation757.example.org.evil.test/wmts"), + ("wmts", url, "header", "", "X-Key", secret, false, "https://user:secret@temporary-validation757.example.org/wmts"), + ("wmts", url, "header", "", "X-Key", secret, false, "https://temporary-validation757.example.org/wmts?api_key=secret"), + ("wmts", url, "header", "", "X-Key", secret, false, "https://temporary-validation757.example.org/wmts#token=secret"), + ("wmts", url, "header", "", "X-Key", secret, false, "https://temporary-validation757.example.org/wm ts"), + ("wmts", url, "header", "", "X-Key", secret, false, "https://temporary-validation757.example.org\\evil/wmts"), + ]; + for (kind, address, auth, user, key, credential, insecure, probe) in invalid { + let error = web::temporary_probe(&probe_connection(kind, address, auth, user, key), credential, insecure, probe).err().unwrap(); + assert!(!error.contains(secret)); + assert!(!error.contains("unknown-auth-secret-757")); + assert!(!error.contains("injected")); + } + for (auth, user, key, credential) in [("basic", "ana", "", "x".repeat(4090)), ("header", "", "X-Key", "x".repeat(4097))] { + assert!(web::temporary_probe(&probe_connection("wmts", url, auth, user, key), &credential, false, url).is_err()); + } + let (wrapped, guard) = web::temporary_probe(&probe_connection("wmts", url, "header", "", "X-Key"), secret, false, url).unwrap(); + assert!(web::temporary_probe(&probe_connection("wmts", url, "header", "", "X-Key"), secret, false, &wrapped).is_err()); + drop(guard); + let oversized = format!("{url}?filter={}", "x".repeat(17000)); + assert!(web::temporary_probe(&probe_connection("wmts", url, "header", "", "X-Key"), secret, false, &oversized).is_err()); + assert_eq!(logins::scrub(secret), secret, "a failure after activation must not leave a binding alive"); +} + +#[test] +fn temporary_http_probes_require_explicit_consent_each_time() { + let url = "http://temporary-consent757.example.org/wmts"; + assert_eq!( + web::temporary_probe(&probe_connection("wmts", url, "header", "", "X-Key"), "temporary-consent-757", false, url).err().unwrap(), + web::INSECURE + ); + let (address, guard) = + web::temporary_probe(&probe_connection("wmts", url, "header", "", "X-Key"), "temporary-consent-757", true, url).unwrap(); + assert_eq!(header(&address).unwrap().1, "temporary-consent-757"); + assert!(web::temporary_probe(&probe_connection("wmts", url, "header", "", "X-Key"), "temporary-consent-757", false, url).is_err()); + assert_eq!(header(&address).unwrap().1, "temporary-consent-757", "a failed test must not replace a live test"); + drop(guard); + assert!(logins::apply(&address).is_err()); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn temporary_probe_caches_are_isolated_and_revoked_data_cannot_be_read() { + use crate::render::rustgis_tiles::cache::{self, Payload, State, Want}; + let server = LoginFile::new(br#"{"temporaryProbe":true}"#.to_vec()); + let url = format!("{}/wmts", server.base); + let definition = probe_connection("wmts", &url, "header", "", "X-Key"); + let (allowed, allowed_guard) = web::temporary_probe(&definition, "fixture-login-757", true, &url).unwrap(); + let (denied, denied_guard) = web::temporary_probe(&definition, "wrong-temporary-cache-757", true, &url).unwrap(); + let fetch = |address: &str| { + let started = std::time::Instant::now(); + loop { + match cache::get(address, Want::Json) { + State::Pending => { + assert!(started.elapsed() < std::time::Duration::from_secs(10), "temporary probe fetch timed out"); + std::thread::sleep(std::time::Duration::from_millis(2)); + } + state => break state, + } + } + }; + match fetch(&allowed) { + State::Ready(payload) => match payload.as_ref() { + Payload::Json(value) => assert_eq!(value["temporaryProbe"], true), + _ => panic!("expected the authenticated JSON response"), + }, + _ => panic!("the typed test login did not authenticate"), + } + assert!(matches!(fetch(&denied), State::Failed(_)), "another test must not inherit cached authenticated data"); + assert!(matches!(fetch(&url), State::Failed(_)), "a bare URL must not inherit a temporary credential or its cache"); + let sent = server.requests.lock().unwrap().len(); + drop(allowed_guard); + assert!(cache::peek(&allowed).is_none()); + assert!(matches!(cache::get(&allowed, Want::Json), State::Failed(_))); + assert!(cache::download_blocking(&allowed).is_err()); + assert_eq!(server.requests.lock().unwrap().len(), sent, "revocation must fail before a network request"); + assert!(logins::check(&denied).is_ok(), "dropping one test must not revoke another"); + drop(denied_guard); + assert!(logins::check(&denied).is_err()); +} + 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")); diff --git a/crates/ui-egui/src/connectionui.rs b/crates/ui-egui/src/connectionui.rs index 3543bd219f..a591a64031 100644 --- a/crates/ui-egui/src/connectionui.rs +++ b/crates/ui-egui/src/connectionui.rs @@ -38,7 +38,7 @@ pub struct State { pub access_key: String, pub secret_key: String, pub session_token: String, - pub test: Option<(Value, Slot, String>>)>, + pub test: Option<(Value, Slot, String>>, TestContext)>, /// The form was opened to log in (Log In…): Save logs in even with no secret typed, /// using the connection's existing session login (or saved DB/S3 credentials). pub login: bool, @@ -50,6 +50,14 @@ pub struct State { pub resolving: Option<(String, String)>, } +/// Authority and form identity for one off-frame Test. Dropping the pending Test revokes its +/// temporary login, including when a late download still owns the detached result slot. +pub struct TestContext { + opened: u64, + form: Value, + _login: Option, +} + /// The chooser of the saved S3 connection that reads a project's S3 sources (Data Sources › /// Choose S3 connection…): what was picked, for the project it is for. Nothing is picked for the /// user. @@ -89,6 +97,12 @@ pub(crate) fn translated(e: &str) -> String { web::NOT_THIS_SESSION => tl!("This request's login does not belong to this session."), web::CHOOSE_CONNECTION => tl!("Several saved connections match this address. Choose a connection explicitly."), web::NO_SUCH_CONNECTION => tl!("No saved web-service connection with this id."), + "The service test requires a valid web-service connection and login." => { + tl!("The service test requires a valid web-service connection and login.") + } + "The service test requires a credential-free HTTP or HTTPS address at the connection's origin." => { + tl!("The service test requires a credential-free HTTP or HTTPS address at the connection's origin.") + } other => other, } .to_string() @@ -327,7 +341,7 @@ fn secrets(st: &State, p: &mut Value) { } /// A saved service Test uses its saved login-relevant definition, never an edited endpoint -/// with an old id. A new anonymous endpoint can still be tested without saving it. +/// with an old id. A new endpoint uses only credentials explicitly typed into its form. fn test_blocked(app: &RustGisApp) -> Option<&'static str> { let st = &app.ui.connection; let saved = st.id.as_deref().and_then(|id| app.session.connections.find(id)); @@ -335,9 +349,7 @@ fn test_blocked(app: &RustGisApp) -> Option<&'static str> { return None; } let auth = if st.auth == "none" { "" } else { &st.auth }; - if st.id.is_none() && auth.is_empty() { - return None; - } + st.id.as_ref()?; let matches = saved.is_some_and(|c| { c.kind == st.kind && c.address == st.address.trim() @@ -365,7 +377,14 @@ pub fn start_test(app: &mut RustGisApp) { if service && let Some(id) = &app.ui.connection.id { p["id"] = json!(id); } - secrets(&app.ui.connection, &mut p); + let temporary = service && app.ui.connection.id.is_none() && !matches!(app.ui.connection.auth.as_str(), "" | "none"); + if temporary { + // Public commands remain anonymous. The typed secret is passed directly to the helper, + // never through command parameters, command history or a saved connection. + p["auth"] = json!("none"); + } else { + secrets(&app.ui.connection, &mut p); + } if DATABASE_KINDS.contains(&app.ui.connection.kind.as_str()) { match app.run("connection.test", p) { Ok(v) => app.dialogs.info = Some(summary(&app.ui.connection.kind, &v["result"])), @@ -375,7 +394,7 @@ pub fn start_test(app: &mut RustGisApp) { } p["probe"] = json!(true); let result = app.run("connection.test", p); - if service { + if service && !temporary { clear_secrets(&mut app.ui.connection); } let v = match result { @@ -390,13 +409,38 @@ pub fn start_test(app: &mut RustGisApp) { return; }; let mut q = definition(&app.ui.connection); + let (url, login) = if temporary { + q["auth"] = json!("none"); + let st = &app.ui.connection; + let secret = if st.auth == "basic" { &st.password } else { &st.api_key }; + let connection = Connection { + kind: st.kind.clone(), + address: st.address.clone(), + auth: st.auth.clone(), + user: st.user.clone(), + key_name: st.key_name.clone(), + ..Default::default() + }; + let result = web::temporary_probe(&connection, secret, st.allow_insecure, url); + clear_secrets(&mut app.ui.connection); + match result { + Ok((url, login)) => (url, Some(login)), + Err(e) => { + app.dialogs.error = Some(translated(&e)); + return; + } + } + } else { + (url.to_string(), None) + }; if service && let Some(id) = &app.ui.connection.id { q["id"] = json!(id); } + let context = TestContext { opened: app.session.opened, form: definition(&app.ui.connection), _login: login }; let slot: Slot, String>> = Slot::default(); let land = slot.clone(); - cache::download(url, Box::new(move |r| *land.lock().unwrap_or_else(|e| e.into_inner()) = Some(r))); - app.ui.connection.test = Some((q, slot)); + cache::download(&url, Box::new(move |r| *land.lock().unwrap_or_else(|e| e.into_inner()) = Some(r))); + app.ui.connection.test = Some((q, slot, context)); app.dialogs.info = Some(tl!("Testing…").into()); } @@ -560,17 +604,33 @@ fn poll_requests(app: &mut RustGisApp) { pub fn poll(app: &mut RustGisApp, ctx: &egui::Context) { poll_requests(app); if !matches!(app.dialogs.open, Some("newConnection" | "editConnection")) { + if app.ui.connection.test.take().is_some() { + app.dialogs.info = None; + app.dialogs.error = None; + } + return; + } + let stale = app + .ui + .connection + .test + .as_ref() + .is_some_and(|(_, _, context)| context.opened != app.session.opened || context.form != definition(&app.ui.connection)); + if stale { app.ui.connection.test = None; + app.dialogs.info = None; + app.dialogs.error = None; return; } - let Some((q, slot)) = app.ui.connection.test.as_mut() else { return }; + let Some((_, slot, _)) = app.ui.connection.test.as_mut() else { return }; let result = slot.lock().unwrap_or_else(|e| e.into_inner()).take(); let Some(result) = result else { ctx.request_repaint_after(std::time::Duration::from_millis(100)); return; }; - let mut q = q.take(); - app.ui.connection.test = None; + // Hold the temporary login while the answer is parsed too: response/error redaction still + // needs it, and no saved login may be substituted during this anonymous command. + let Some((mut q, _, _context)) = app.ui.connection.test.take() else { return }; app.dialogs.info = None; match result { Ok(bytes) => { @@ -1022,7 +1082,12 @@ pub fn dialog(app: &mut RustGisApp, ui: &mut egui::Ui) -> bool { } let close = crate::dialogs::buttons(app, ui, "Save", save); // A warning to read keeps it open. - close && app.ui.connection.warned.is_none() + let close = close && app.ui.connection.warned.is_none(); + if close && app.ui.connection.test.take().is_some() { + app.dialogs.info = None; + app.dialogs.error = None; + } + close } pub fn open_connection(app: &mut RustGisApp, c: &Connection) -> Result<(), String> { @@ -1267,6 +1332,44 @@ fn open_chosen(app: &mut RustGisApp) -> bool { mod tests { use super::*; + #[test] + fn unsaved_service_test_requires_valid_typed_credentials_not_a_saved_login() { + let mut app = RustGisApp::with_sample(); + let address = "https://unsaved-test-validation.example/wmts"; + let made = app.run("connection.new", json!({"kind":"wmts","address":address,"auth":"header","keyName":"X-Api-Key"})).unwrap(); + let id = made["id"].as_str().unwrap(); + app.run("connection.login", json!({"id":id,"apiKey":"saved-validation-key"})).unwrap(); + let saved = app.session.connections.find(id).unwrap().clone(); + for auth in ["basic", "header"] { + for secret in ["", "invalid\ncredential"] { + open_new(&mut app, "wmts").unwrap(); + let st = &mut app.ui.connection; + st.address = address.into(); + st.auth = auth.into(); + st.user = "reader".into(); + st.key_name = "X-Api-Key".into(); + if auth == "basic" { + st.password = secret.into(); + } else { + st.api_key = secret.into(); + } + assert!(test_blocked(&app).is_none()); + start_test(&mut app); + let reason = if secret.is_empty() { web::REQUIRED } else { web::BAD_SECRET }; + assert_eq!(app.dialogs.error, Some(translated(reason)), "{auth}"); + assert!(app.ui.connection.test.is_none()); + assert!(app.ui.connection.id.is_none()); + assert_eq!(app.session.connections.connections, vec![saved.clone()]); + assert_eq!(web::known_secret(&app.session, &saved).unwrap().expose(), "saved-validation-key"); + for entry in app.session.log.entries().filter(|e| e.command == "connection.test") { + assert_eq!(entry.params["auth"], "none"); + assert!(entry.params.get("apiKey").is_none()); + assert!(entry.params.get("password").is_none()); + } + } + } + } + #[test] fn edited_service_test_is_blocked_without_sending_or_saving_the_key() { let mut app = RustGisApp::with_sample(); diff --git a/crates/ui-egui/tests/service_logins/mod.rs b/crates/ui-egui/tests/service_logins/mod.rs index ac2ec40dfa..35e0b2f785 100644 --- a/crates/ui-egui/tests/service_logins/mod.rs +++ b/crates/ui-egui/tests/service_logins/mod.rs @@ -97,7 +97,7 @@ fn basic_login_is_masked_and_session_only_even_with_a_credential_store() { type_field(&mut h, "User", "reader", false); type_field(&mut h, "Password", "web-widget-secret-sentinel", true); assert!(h.query_by_label("Remember on this computer").is_none()); - assert!(h.get_by_role_and_label(Role::Button, "Test").accesskit_node().is_disabled()); + assert!(!h.get_by_role_and_label(Role::Button, "Test").accesskit_node().is_disabled()); h.get_by_role_and_label(Role::Button, "Save").click(); settle(&mut h); assert_eq!(h.state().dialogs.open, None); @@ -123,11 +123,16 @@ fn service_secret_validation_precedes_saving_the_definition() { } #[test] -fn service_http_needs_confirmation_and_unsaved_login_cannot_be_tested() { +fn service_http_test_and_save_need_confirmation() { let mut h = session_ui(); header_form(&mut h, "http://insecure-web-widget.example/wms"); - assert!(h.get_by_role_and_label(Role::Button, "Test").accesskit_node().is_disabled()); + assert!(!h.get_by_role_and_label(Role::Button, "Test").accesskit_node().is_disabled()); assert!(h.state().session.connections.connections.is_empty()); + h.get_by_role_and_label(Role::Button, "Test").click(); + settle(&mut h); + assert!(h.state().ui.connection.test.is_none()); + assert!(h.state().dialogs.error.as_deref().unwrap().contains("unencrypted")); + assert_eq!(h.state().ui.connection.api_key, "web-widget-secret-sentinel", "unsent credentials stay editable"); h.get_by_role_and_label(Role::Button, "Save").click(); settle(&mut h); assert!(h.state().session.connections.connections.is_empty()); @@ -177,6 +182,156 @@ fn a_typed_address_secret_blocks_testing_before_the_engine_refuses() { assert!(h.get_by_role_and_label(Role::Button, "Test").accesskit_node().is_disabled()); } +/// One delayed response lets the UI edit or cancel while authenticated I/O is in flight. +fn probe_server() -> (String, std::sync::mpsc::Receiver, std::sync::mpsc::Sender<()>, std::thread::JoinHandle<()>) { + use std::io::{Read, Write}; + use std::time::{Duration, Instant}; + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let address = format!("http://{}/wmts", listener.local_addr().unwrap()); + listener.set_nonblocking(true).unwrap(); + let (sent, requests) = std::sync::mpsc::channel(); + let (release, wait) = std::sync::mpsc::channel(); + let server = std::thread::spawn(move || { + let deadline = Instant::now() + Duration::from_secs(10); + let mut stream = loop { + match listener.accept() { + Ok((stream, _)) => break stream, + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock && Instant::now() < deadline => { + std::thread::sleep(Duration::from_millis(10)); + } + Err(e) => panic!("the Test did not connect: {e}"), + } + }; + stream.set_read_timeout(Some(Duration::from_secs(5))).unwrap(); + let mut request = Vec::new(); + while !request.ends_with(b"\r\n\r\n") { + let mut buf = [0; 1024]; + let n = stream.read(&mut buf).unwrap(); + assert!(n > 0); + request.extend_from_slice(&buf[..n]); + } + sent.send(String::from_utf8(request).unwrap()).unwrap(); + wait.recv_timeout(Duration::from_secs(10)).unwrap(); + let body = r#" +Test layertestgm + +gmEPSG:3857 +0559082264.0287178-20037508.34278925 20037508.34278925256256 +"#; + write!(stream, "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()).unwrap(); + }); + (address, requests, release, server) +} + +fn finish_probe(h: &mut Harness) { + for _ in 0..250 { + h.step(); + if h.state().ui.connection.test.is_none() { + return; + } + std::thread::sleep(std::time::Duration::from_millis(20)); + } + panic!("the Test did not finish"); +} + +#[test] +fn unsaved_wmts_test_uses_typed_header_or_basic_without_touching_saved_login() { + use rustgis_engine::service_logins as web; + for basic in [false, true] { + let (address, requests, release, server) = probe_server(); + let mut h = session_ui(); + let made = + h.state_mut().run("connection.new", json!({"kind":"wmts","address":address,"auth":"header","keyName":"X-Api-Key"})).unwrap(); + h.state_mut().run("connection.login", json!({"id":made["id"],"apiKey":"existing-saved-login","allowInsecure":true})).unwrap(); + let saved = h.state().session.connections.find(made["id"].as_str().unwrap()).unwrap().clone(); + header_form(&mut h, &address); + h.state_mut().ui.connection.kind = "wmts".into(); + if basic { + h.get_by_role_and_label(Role::ComboBox, "Method").click(); + settle(&mut h); + h.get_by_label("Basic authentication").click(); + settle(&mut h); + type_field(&mut h, "User", "reader", false); + type_field(&mut h, "Password", "web-widget-secret-sentinel", true); + } + h.get_by_label("Send the login over unencrypted http://").click(); + settle(&mut h); + h.get_by_role_and_label(Role::Button, "Test").click(); + h.step(); + let request = requests.recv_timeout(std::time::Duration::from_secs(5)).unwrap(); + let expected = if basic { + rustgis_engine::render::rustgis_tiles::logins::basic("reader", "web-widget-secret-sentinel") + } else { + "web-widget-secret-sentinel".into() + }; + assert!(request.contains(&expected), "{request}"); + assert!(request.to_ascii_lowercase().contains(if basic { "authorization:" } else { "x-api-key:" })); + assert!(!request.contains("existing-saved-login")); + assert!(request.contains("GetCapabilities")); + assert!(h.state().ui.connection.test.is_some(), "the temporary login survives the asynchronous request"); + assert_redacted(&h); + release.send(()).unwrap(); + finish_probe(&mut h); + assert_eq!(h.state().dialogs.open, Some("newConnection")); + assert_eq!(h.state().dialogs.error, None); + assert!(h.state().dialogs.info.as_deref().unwrap().contains("1 layers")); + assert!(h.state().ui.connection.id.is_none()); + assert_eq!(h.state().session.connections.connections, vec![saved.clone()]); + assert_eq!(web::known_secret(&h.state().session, &saved).unwrap().expose(), "existing-saved-login"); + for entry in h.state().session.log.entries().filter(|e| e.command == "connection.test") { + assert_eq!(entry.params["auth"], "none"); + assert!(entry.params.get("id").is_none()); + assert!(entry.params.get("password").is_none()); + assert!(entry.params.get("apiKey").is_none()); + assert!(!entry.params.to_string().contains("web-widget-secret-sentinel")); + } + assert_redacted(&h); + server.join().unwrap(); + } +} + +#[test] +fn unsaved_authenticated_test_discards_late_results_after_edit_cancel_or_project_change() { + for action in ["edit", "cancel", "project"] { + let (address, requests, release, server) = probe_server(); + let mut h = session_ui(); + header_form(&mut h, &address); + h.state_mut().ui.connection.kind = "wmts".into(); + h.get_by_label("Send the login over unencrypted http://").click(); + settle(&mut h); + h.get_by_role_and_label(Role::Button, "Test").click(); + h.step(); + requests.recv_timeout(std::time::Duration::from_secs(5)).unwrap(); + let old = h.state().ui.connection.test.as_ref().unwrap().1.clone(); + match action { + "edit" => type_field(&mut h, "Key", "replacement-form-key", true), + "cancel" => { + h.get_by_role_and_label(Role::Button, "Cancel").click(); + settle(&mut h); + } + "project" => { + h.state_mut().run("project.new", json!({})).unwrap(); + settle(&mut h); + } + _ => unreachable!(), + } + assert!(h.state().ui.connection.test.is_none(), "{action}"); + release.send(()).unwrap(); + for _ in 0..250 { + h.step(); + if old.lock().unwrap().is_some() { + break; + } + std::thread::sleep(std::time::Duration::from_millis(20)); + } + assert!(old.lock().unwrap().is_some(), "the detached request must actually complete"); + assert_eq!(h.state().dialogs.info, None, "{action}"); + assert_eq!(h.state().dialogs.error, None, "{action}"); + assert!(h.state().session.connections.connections.is_empty()); + server.join().unwrap(); + } +} + fn request(h: &mut Harness, reason: LoginReason, key: &str, connections: Vec) { let opened = h.state().session.opened; h.state_mut().session.database_logins.request(LoginRequest { diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index 7100cddb03..dff304746d 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -668,6 +668,13 @@ database tests run synchronously on desktop. The web app doesn't offer database connections; saved database rows remain visible but cannot be opened or tested there. Earth Search and Planetary Computer remain available under **Library**. +For a new web-service connection, enter its Basic password or header API key to test it before +saving. The test uses only the entered login, not a saved connection's login at the same address. +For an `http://` address, tick the confirmation before sending the login unencrypted. After a test +starts, RustGIS clears the password or key and the confirmation from the form; enter them again +if you want to save a session login. The test does not save the connection or retain its login. +For an existing service connection, save changes to its address or login definition before testing. + **From Service…** (including **Catalogue search (CSW)**), **Database…**, **STAC Search…** and **S3…** can use saved connections directly. Service, database and S3 dialogs offer a **Saved connection** picker; STAC includes saved connections in its existing API/catalog list. From 685f7e219f00219e5357afb813f6723896a3cea3 Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Sat, 7 Nov 2026 18:06:31 +0100 Subject: [PATCH 2/6] fix(ui): protect connection test feedback and credential history --- crates/engine/src/project_log.rs | 20 ++++++--- crates/tiles/src/logins.rs | 14 ++++++- crates/ui-egui/src/connectionui.rs | 49 ++++++++++++++++++++-- crates/ui-egui/tests/service_logins/mod.rs | 42 ++++++++++++++++++- docs/user-guide/data.md | 2 + 5 files changed, 116 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/project_log.rs b/crates/engine/src/project_log.rs index 12ac74c07a..9c07d6c055 100644 --- a/crates/engine/src/project_log.rs +++ b/crates/engine/src/project_log.rs @@ -44,10 +44,10 @@ pub(crate) fn is_logged(s: &Session, id: &str, params: &Value) -> bool { mutates || crate::cmd::items::names_place(params, crate::cmd::plugins::PLACE_KEYS, 0) } -/// `params` as the log keeps them: a document given as text (a map, a project, a capabilities -/// answer, GeoJSON) as its size only, since one may hold secrets no redaction finds in it (an S3 -/// source's token inside a map's JSON); the rest clipped (so a long script costs little), then -/// without secrets (as History's Copy as code takes them out). A secret cut short is still taken +/// `params` as the log keeps them: response bodies (`answer`) and documents given as text +/// (maps, projects, capabilities, GeoJSON) as their sizes only, since they may hold secrets no +/// field-name redaction finds; the rest clipped (so a long script costs little), then without +/// secrets (as History's Copy as code takes them out). A secret cut short is still taken /// out: what is left of it follows its name. Within [`LOG_PARAMS_BYTES`] of JSON in three tries, /// each giving up more: texts at that bound and lists at their first [`LOG_LIST_ITEMS`]; then /// texts at 512 characters; then the first keys with their short values only. @@ -291,7 +291,17 @@ fn clip(v: &Value, chars: usize, items: usize, depth: usize) -> Value { // goes. let mut out = serde_json::Map::new(); for (k, x) in o { - let value = || if k.chars().nth(200).is_some() { Value::String("…".into()) } else { clip(x, chars, items, depth + 1) }; + let value = || { + if k.chars().nth(200).is_some() { + Value::String("…".into()) + } else if k == "answer" + && let Some(text) = x.as_str() + { + Value::String(format!("(a response of {} characters)", text.chars().count())) + } else { + clip(x, chars, items, depth + 1) + } + }; out.entry(k.chars().take(200).collect::()).or_insert_with(value); } Value::Object(out) diff --git a/crates/tiles/src/logins.rs b/crates/tiles/src/logins.rs index 38a7099d4e..03449354a4 100644 --- a/crates/tiles/src/logins.rs +++ b/crates/tiles/src/logins.rs @@ -129,6 +129,15 @@ fn activate_with_random( && !payload.is_empty() { add_redactions(&mut redactions, payload); + // Basic responses can echo the decoded password as well as the wire header. Keep + // both patterns bound to this login's lifetime, including percent-encoded echoes. + if let Ok(decoded) = base64::engine::general_purpose::STANDARD.decode(payload) + && let Ok(decoded) = std::str::from_utf8(&decoded) + && let Some((_, password)) = decoded.split_once(':') + && !password.is_empty() + { + add_redactions(&mut redactions, password); + } } let mut entries = entries(); // A token is the only thing that scopes a bound address to its login, so it is random. A @@ -521,10 +530,13 @@ mod tests { #[test] fn scrubs_header_and_basic_payload() { - let value = basic("scrub-user", "scrub-password"); + let password = "scrub-password/+:"; + let value = basic("scrub-user", password); let binding = activate("https://scrub-login.example", "scrub", Method::Header("Authorization".into()), &value, false).unwrap(); let payload = value.strip_prefix("Basic ").unwrap(); assert_eq!(scrub(&format!("{value} {payload} {}", percent_encode(&value))), "[redacted] [redacted] [redacted]"); + assert_eq!(scrub(password), "[redacted]"); + assert_eq!(scrub(&percent_encode(password).to_ascii_lowercase()), "[redacted]"); let url = bind(&binding, "https://scrub-login.example/tile").unwrap(); assert!(!scrub(&format!("failed: {url}")).contains(rustgis_data::login_address::PREFIX)); assert_eq!(format!("{:?}", apply(&url).unwrap().unwrap()), "Applied()"); diff --git a/crates/ui-egui/src/connectionui.rs b/crates/ui-egui/src/connectionui.rs index a591a64031..c1083faf6a 100644 --- a/crates/ui-egui/src/connectionui.rs +++ b/crates/ui-egui/src/connectionui.rs @@ -400,11 +400,17 @@ pub fn start_test(app: &mut RustGisApp) { let v = match result { Ok(v) => v, Err(e) => { + if temporary { + clear_secrets(&mut app.ui.connection); + } app.dialogs.error = Some(translated(&e)); return; } }; let Some(url) = v["fetch"].as_str() else { + if temporary { + clear_secrets(&mut app.ui.connection); + } app.dialogs.error = Some(tl!("Nothing to test").into()); return; }; @@ -604,7 +610,7 @@ fn poll_requests(app: &mut RustGisApp) { pub fn poll(app: &mut RustGisApp, ctx: &egui::Context) { poll_requests(app); if !matches!(app.dialogs.open, Some("newConnection" | "editConnection")) { - if app.ui.connection.test.take().is_some() { + if app.ui.connection.test.take().is_some() && app.dialogs.open.is_none() { app.dialogs.info = None; app.dialogs.error = None; } @@ -637,11 +643,11 @@ pub fn poll(app: &mut RustGisApp, ctx: &egui::Context) { q["answer"] = json!(String::from_utf8_lossy(&bytes)); let kind = q["kind"].as_str().unwrap_or("").to_owned(); match app.run("connection.test", q) { - Ok(v) => app.dialogs.info = Some(summary(&kind, &v["result"])), - Err(e) => app.dialogs.error = Some(e), + Ok(v) => app.dialogs.info = Some(rustgis_engine::render::rustgis_tiles::logins::scrub(&summary(&kind, &v["result"]))), + Err(e) => app.dialogs.error = Some(rustgis_engine::render::rustgis_tiles::logins::scrub(&e)), } } - Err(e) => app.dialogs.error = Some(e), + Err(e) => app.dialogs.error = Some(rustgis_engine::render::rustgis_tiles::logins::scrub(&e)), } } @@ -1332,6 +1338,41 @@ fn open_chosen(app: &mut RustGisApp) -> bool { mod tests { use super::*; + #[test] + fn failed_unsaved_service_probe_clears_credentials() { + let mut app = RustGisApp::with_sample(); + for auth in ["basic", "header"] { + open_new(&mut app, "wmts").unwrap(); + let st = &mut app.ui.connection; + st.address = String::new(); + st.auth = auth.into(); + st.user = "reader".into(); + st.key_name = "X-Key".into(); + st.password = "probe-failure-password".into(); + st.api_key = "probe-failure-key".into(); + st.allow_insecure = true; + start_test(&mut app); + assert!(app.dialogs.error.is_some()); + assert!(app.ui.connection.test.is_none()); + assert!(app.ui.connection.password.is_empty()); + assert!(app.ui.connection.api_key.is_empty()); + assert!(!app.ui.connection.allow_insecure); + } + } + + #[test] + fn abandoned_probe_preserves_replacement_dialog_status() { + let mut app = RustGisApp::with_sample(); + open_new(&mut app, "wmts").unwrap(); + let context = TestContext { opened: app.session.opened, form: definition(&app.ui.connection), _login: None }; + app.ui.connection.test = Some((json!({}), Slot::default(), context)); + app.dialogs.open = Some("addDelimited"); + app.dialogs.error = Some("replacement file read failed".into()); + poll(&mut app, &egui::Context::default()); + assert!(app.ui.connection.test.is_none()); + assert_eq!(app.dialogs.error.as_deref(), Some("replacement file read failed")); + } + #[test] fn unsaved_service_test_requires_valid_typed_credentials_not_a_saved_login() { let mut app = RustGisApp::with_sample(); diff --git a/crates/ui-egui/tests/service_logins/mod.rs b/crates/ui-egui/tests/service_logins/mod.rs index 35e0b2f785..5b8dc3e90b 100644 --- a/crates/ui-egui/tests/service_logins/mod.rs +++ b/crates/ui-egui/tests/service_logins/mod.rs @@ -184,6 +184,13 @@ fn a_typed_address_secret_blocks_testing_before_the_engine_refuses() { /// One delayed response lets the UI edit or cancel while authenticated I/O is in flight. fn probe_server() -> (String, std::sync::mpsc::Receiver, std::sync::mpsc::Sender<()>, std::thread::JoinHandle<()>) { + probe_server_with_answer(None) +} + +fn probe_server_with_answer( + answer: Option<&str>, +) -> (String, std::sync::mpsc::Receiver, std::sync::mpsc::Sender<()>, std::thread::JoinHandle<()>) { + let answer = answer.map(str::to_owned); use std::io::{Read, Write}; use std::time::{Duration, Instant}; let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); @@ -195,7 +202,10 @@ fn probe_server() -> (String, std::sync::mpsc::Receiver, std::sync::mpsc let deadline = Instant::now() + Duration::from_secs(10); let mut stream = loop { match listener.accept() { - Ok((stream, _)) => break stream, + Ok((stream, _)) => { + stream.set_nonblocking(false).unwrap(); + break stream; + } Err(e) if e.kind() == std::io::ErrorKind::WouldBlock && Instant::now() < deadline => { std::thread::sleep(Duration::from_millis(10)); } @@ -218,6 +228,12 @@ fn probe_server() -> (String, std::sync::mpsc::Receiver, std::sync::mpsc gmEPSG:3857 0559082264.0287178-20037508.34278925 20037508.34278925256256 "#; + let title = format!( + "Fixture web-widget-secret-sentinel {}", + rustgis_engine::render::rustgis_tiles::logins::basic("reader", "web-widget-secret-sentinel") + ); + let body = body.replace("", &title); + let body = answer.unwrap_or(body); write!(stream, "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()).unwrap(); }); (address, requests, release, server) @@ -275,6 +291,8 @@ fn unsaved_wmts_test_uses_typed_header_or_basic_without_touching_saved_login() { assert_eq!(h.state().dialogs.open, Some("newConnection")); assert_eq!(h.state().dialogs.error, None); assert!(h.state().dialogs.info.as_deref().unwrap().contains("1 layers")); + assert!(h.state().dialogs.info.as_deref().unwrap().contains("[redacted]")); + assert!(!h.state().dialogs.info.as_deref().unwrap().contains(&expected)); assert!(h.state().ui.connection.id.is_none()); assert_eq!(h.state().session.connections.connections, vec![saved.clone()]); assert_eq!(web::known_secret(&h.state().session, &saved).unwrap().expose(), "existing-saved-login"); @@ -290,6 +308,28 @@ fn unsaved_wmts_test_uses_typed_header_or_basic_without_touching_saved_login() { } } +#[test] +fn short_service_answers_do_not_retain_echoed_credentials_in_history() { + for answer in ["web-widget-secret-sentinel", "web-widget-secret-sentinel"] { + let (address, requests, release, server) = probe_server_with_answer(Some(answer)); + let mut h = session_ui(); + header_form(&mut h, &address); + h.get_by_label("Send the login over unencrypted http://").click(); + settle(&mut h); + h.get_by_role_and_label(Role::Button, "Test").click(); + h.step(); + requests.recv_timeout(std::time::Duration::from_secs(5)).unwrap(); + release.send(()).unwrap(); + finish_probe(&mut h); + assert!(h.state().dialogs.error.is_some()); + let entry = h.state().session.log.entries().filter(|e| e.command == "connection.test").last().unwrap(); + assert!(entry.params.get("answer").is_some(), "the response parsing command must actually be recorded"); + assert!(!entry.params.to_string().contains("web-widget-secret-sentinel"), "the response must not retain the credential"); + assert_redacted(&h); + server.join().unwrap(); + } +} + #[test] fn unsaved_authenticated_test_discards_late_results_after_edit_cancel_or_project_change() { for action in ["edit", "cancel", "project"] { diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index dff304746d..d259fc074d 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -673,6 +673,8 @@ saving. The test uses only the entered login, not a saved connection's login at For an `http://` address, tick the confirmation before sending the login unencrypted. After a test starts, RustGIS clears the password or key and the confirmation from the form; enter them again if you want to save a session login. The test does not save the connection or retain its login. +Test status masks echoed login credentials, and the command log keeps response sizes rather than +response bodies. For an existing service connection, save changes to its address or login definition before testing. **From Service…** (including **Catalogue search (CSW)**), **Database…**, **STAC Search…** and From bda1c2730989c9f0d1c8bb7b8c31689c5ade4502 Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Sun, 8 Nov 2026 08:11:12 +0100 Subject: [PATCH 3/6] fix(tiles): prioritize explicit service logins over S3 --- apps/rustgis-web/src/web.rs | 6 +- crates/tiles/src/cache.rs | 114 ++++++++++++++++++++- crates/ui-egui/src/connectionui.rs | 1 - crates/ui-egui/tests/service_logins/mod.rs | 18 +++- docs/user-guide/data.md | 3 +- 5 files changed, 129 insertions(+), 13 deletions(-) diff --git a/apps/rustgis-web/src/web.rs b/apps/rustgis-web/src/web.rs index ab41fdc11e..919f6a6101 100644 --- a/apps/rustgis-web/src/web.rs +++ b/apps/rustgis-web/src/web.rs @@ -611,11 +611,9 @@ async fn fetch_bytes( let max = limit.map(|n| n as u64).unwrap_or(64 << 20); let (url, tail) = cache::split_tail(url); 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). - logins::check(url).map_err(str::to_string)?; + // Share native GET routing: explicit service logins outrank implicit S3 endpoint signing. let bound = url; - let prepared = rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::s3::prepare(logins::inner(url))?; + let prepared = cache::prepare_get(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 diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index d3d89c9250..3e575a9596 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -1249,6 +1249,18 @@ pub fn head(url: &str, done: HeadDone) { } } +/// Resolves a GET's destination and S3 headers for both native and browser transports. +/// A validated explicit service login takes precedence over an implicit endpoint signer. +/// Its credential is still applied separately at send time, so revocation cannot fall back +/// to anonymous access or ambient S3 credentials. +pub fn prepare_get(url: &str) -> Result { + crate::logins::check(url).map_err(str::to_string)?; + if rustgis_data::login_address::is_wrapper(url) { + return Ok(crate::s3::Prepared { url: crate::logins::inner(url).to_string(), headers: Vec::new(), signed: false }); + } + crate::s3::prepare(url) +} + /// Fetches an address's bytes, uncached (through the browser's fetcher, or the worker threads). /// 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). @@ -1940,10 +1952,8 @@ mod native { let (url, mut range) = super::split_range(url); // The last bytes asked for, while the file's length is asked for instead (see below). let mut measuring: Option = None; - // An `s3://` address becomes its web address, and a request to an S3 connection's - // endpoint is signed. - crate::logins::check(url)?; - let prepared = crate::s3::prepare(crate::logins::inner(url))?; + // Explicit service logins outrank implicit S3 endpoint signing. + let prepared = super::prepare_get(url)?; let mut at = prepared.url.clone(); let first_origin = crate::logins::origin(&at); let mut same_origin = true; @@ -2967,6 +2977,102 @@ mod tests { (format!("http://{addr}"), received) } + #[test] + fn get_preparation_keeps_explicit_service_authority_over_ambient_s3() { + let _turn = crate::cache::turn(); + use crate::{ + logins::{self, Method}, + s3, + }; + let base = "https://get-preparation-login.example"; + let name = "get-preparation-ambient-s3"; + s3::connect(s3::Connection { + name: name.into(), + endpoint: Some(base.into()), + region: s3::DEFAULT_REGION.into(), + buckets: Vec::new(), + credentials: Some(s3::Credentials { + access_key_id: "PREPARATIONKEY".into(), + secret_access_key: "preparation-s3-secret".into(), + session_token: Some("preparation-s3-token".into()), + }), + }) + .unwrap(); + let plain = format!("{base}/service%2Fpath?query=a%20b"); + assert!(prepare_get(&plain).unwrap().signed); + let binding = + logins::activate(base, "get-preparation/service", Method::Header("Authorization".into()), "Bearer service-secret", false) + .unwrap(); + let bound = logins::bind(&binding, &plain).unwrap(); + // This is the shared preparation used before browser RequestInit construction: + // no S3 canonicalization or signing headers survive the explicit binding. + let prepared = prepare_get(&bound).unwrap(); + assert_eq!(prepared.url, plain); + assert!(!prepared.signed); + assert!(prepared.headers.is_empty()); + let target = logins::inherit(&bound, &prepared.url); + let login = logins::apply(&target).unwrap().unwrap(); + assert_eq!(login.url, plain); + assert_eq!(login.header, Some(("Authorization".into(), "Bearer service-secret".into()))); + // Preparation itself carries no credential or authority: after revocation both + // fresh preparation and send-time application fail instead of using ambient S3. + assert!(logins::deactivate(&binding)); + assert!(prepare_get(&bound).unwrap_err().contains("no longer active")); + assert!(logins::apply(&target).is_err()); + assert!(prepare_get(&plain).unwrap().signed); + assert!(s3::disconnect(name)); + } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn explicit_service_login_beats_ambient_s3_on_native_get_and_redirect() { + let _turn = crate::cache::turn(); + use crate::{ + logins::{self, Method}, + s3, + }; + let ok = b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\ntile".to_vec(); + let redirect = b"HTTP/1.1 302 Found\r\nLocation: /next\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".to_vec(); + let (base, asked) = serve_recorded(vec![ok.clone(), redirect.clone(), ok.clone(), redirect, ok]); + let name = "native-get-ambient-s3"; + s3::connect(s3::Connection { + name: name.into(), + endpoint: Some(base.clone()), + region: s3::DEFAULT_REGION.into(), + buckets: Vec::new(), + credentials: Some(s3::Credentials { + access_key_id: "NATIVEGETKEY".into(), + secret_access_key: "native-get-s3-secret".into(), + session_token: Some("native-get-s3-token".into()), + }), + }) + .unwrap(); + let agent = native::agent(); + let plain = format!("{base}/start"); + assert_eq!(native::get(&agent, &plain, &[]).unwrap(), b"tile"); + let received = asked.recv_timeout(Duration::from_secs(5)).unwrap().to_ascii_lowercase(); + assert!(received.contains("authorization: aws4-hmac-sha256 credential=nativegetkey/")); + assert!(received.contains("x-amz-security-token: native-get-s3-token\r\n")); + for (header, value) in + [("Authorization", logins::basic("service-user", "service-password")), ("X-Service-Key", "explicit-service-key".to_string())] + { + let binding = logins::activate(&base, "native-get/service", Method::Header(header.into()), &value, true).unwrap(); + let bound = logins::bind(&binding, &plain).unwrap(); + assert_eq!(native::get(&agent, &bound, &[]).unwrap(), b"tile"); + for path in ["/start", "/next"] { + let received = asked.recv_timeout(Duration::from_secs(5)).unwrap().to_ascii_lowercase(); + assert!(received.starts_with(&format!("get {path} http/1.1\r\n")), "{received}"); + assert!(received.contains(&format!("{}: {}\r\n", header.to_ascii_lowercase(), value.to_ascii_lowercase())), "{received}"); + assert!(!received.contains("aws4-hmac-sha256") && !received.contains("x-amz-"), "{received}"); + assert!(!received.contains("nativegetkey") && !received.contains("native-get-s3-token"), "{received}"); + } + assert!(logins::deactivate(&binding)); + let failure = native::get(&agent, &bound, &[]).unwrap_err(); + assert!(failure.contains("no longer active"), "{failure}"); + } + assert!(s3::disconnect(name)); + } + #[cfg(not(target_arch = "wasm32"))] #[test] fn logins_headers_follow_same_origin_redirects() { diff --git a/crates/ui-egui/src/connectionui.rs b/crates/ui-egui/src/connectionui.rs index 2b49491811..654c4453be 100644 --- a/crates/ui-egui/src/connectionui.rs +++ b/crates/ui-egui/src/connectionui.rs @@ -1407,7 +1407,6 @@ mod tests { assert_eq!(app.session.connections.connections, vec![saved.clone()]); assert_eq!(web::known_secret(&app.session, &saved).unwrap().expose(), "saved-validation-key"); for entry in app.session.log.entries().filter(|e| e.command == "connection.test") { - assert_eq!(entry.params["auth"], "none"); assert!(entry.params.get("apiKey").is_none()); assert!(entry.params.get("password").is_none()); } diff --git a/crates/ui-egui/tests/service_logins/mod.rs b/crates/ui-egui/tests/service_logins/mod.rs index 5b8dc3e90b..1c7d21293c 100644 --- a/crates/ui-egui/tests/service_logins/mod.rs +++ b/crates/ui-egui/tests/service_logins/mod.rs @@ -297,7 +297,6 @@ fn unsaved_wmts_test_uses_typed_header_or_basic_without_touching_saved_login() { assert_eq!(h.state().session.connections.connections, vec![saved.clone()]); assert_eq!(web::known_secret(&h.state().session, &saved).unwrap().expose(), "existing-saved-login"); for entry in h.state().session.log.entries().filter(|e| e.command == "connection.test") { - assert_eq!(entry.params["auth"], "none"); assert!(entry.params.get("id").is_none()); assert!(entry.params.get("password").is_none()); assert!(entry.params.get("apiKey").is_none()); @@ -310,7 +309,11 @@ fn unsaved_wmts_test_uses_typed_header_or_basic_without_touching_saved_login() { #[test] fn short_service_answers_do_not_retain_echoed_credentials_in_history() { - for answer in ["web-widget-secret-sentinel", "web-widget-secret-sentinel"] { + for answer in [ + "web-widget-secret-sentinel", + "web-widget-secret-sentinel", + "Rejected credential web-widget-secret-sentinel", + ] { let (address, requests, release, server) = probe_server_with_answer(Some(answer)); let mut h = session_ui(); header_form(&mut h, &address); @@ -321,10 +324,19 @@ fn short_service_answers_do_not_retain_echoed_credentials_in_history() { requests.recv_timeout(std::time::Duration::from_secs(5)).unwrap(); release.send(()).unwrap(); finish_probe(&mut h); - assert!(h.state().dialogs.error.is_some()); + let ui_error = h.state().dialogs.error.as_deref().expect("the service response must produce a UI error"); + assert!(!ui_error.contains("web-widget-secret-sentinel"), "the UI error must not retain the credential"); let entry = h.state().session.log.entries().filter(|e| e.command == "connection.test").last().unwrap(); assert!(entry.params.get("answer").is_some(), "the response parsing command must actually be recorded"); assert!(!entry.params.to_string().contains("web-widget-secret-sentinel"), "the response must not retain the credential"); + let logged_error = entry.error.as_deref().expect("the response parsing error must actually be recorded"); + assert!(!logged_error.contains("web-widget-secret-sentinel"), "the command log error must not retain the credential"); + if answer.starts_with("") { + for error in [logged_error, ui_error] { + assert!(error.contains("the service answered with an error"), "the service exception must reach the consumer: {error}"); + assert!(error.contains("Rejected credential [redacted]"), "the echoed credential must be redacted in the error: {error}"); + } + } assert_redacted(&h); server.join().unwrap(); } diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index 0680b4bb9e..37b3f8e341 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -669,7 +669,8 @@ connections; saved database rows remain visible but cannot be opened or tested t Search and Planetary Computer remain available under **Library**. For a new web-service connection, enter its Basic password or header API key to test it before -saving. The test uses only the entered login, not a saved connection's login at the same address. +saving. The test uses only the entered login, not a saved service login or an S3 session login +at the same address. For an `http://` address, tick the confirmation before sending the login unencrypted. After a test starts, RustGIS clears the password or key and the confirmation from the form; enter them again if you want to save a session login. The test does not save the connection or retain its login. From 0275a50e5dfa4d2d18e72ef215d857db859cf3d0 Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Sun, 8 Nov 2026 19:48:09 +0100 Subject: [PATCH 4/6] fix: protect abandoned service tests --- crates/tiles/src/cache.rs | 110 ++++++++++++++++++++++++++++- crates/ui-egui/src/connectionui.rs | 50 +++++++++---- 2 files changed, 142 insertions(+), 18 deletions(-) diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index 3e575a9596..904f103e6e 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -2029,9 +2029,9 @@ mod native { } } if status >= 400 { - // 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() { + // A signed request or any hop carrying a login makes the refusal status-only: + // a server may echo credentials even after a cross-origin redirect or revocation. + return Err(if detail && !prepared.signed && !carried_login { refused(&mut resp) } else { Failure::without_answer( @@ -3227,6 +3227,110 @@ mod tests { assert!(logins::deactivate(&foreign)); } + /// GET keeps refusals private after an authenticated redirect, even after login revocation. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn delayed_get_refusal_after_cross_origin_redirect_is_status_only() { + if isolate_cache_fixture("cache::tests::delayed_get_refusal_after_cross_origin_redirect_is_status_only") { + return; + } + let _turn = crate::cache::turn(); + use crate::failure::Reason; + use crate::logins::{self, Method}; + use rustgis_data::diagnostics::{Capture, Filter, History}; + use std::io::Write; + use std::sync::mpsc::channel; + use std::time::Duration; + + for revoke in [false, true] { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let other = format!("http://{}", listener.local_addr().unwrap()); + let secret = "redirect-get-sentinel-8430"; + let body = format!("private refusal echoed {secret}"); + let response = format!( + "HTTP/1.1 403 Forbidden\r\nx-request-id: {secret}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", + body.len() + ); + let (received_tx, received_rx) = channel(); + let (release_tx, release_rx) = channel(); + let server = std::thread::spawn(move || { + let (mut stream, _) = listener.accept().unwrap(); + let request = read_request(&mut stream); + assert!(request.starts_with("GET /refused HTTP/1.1\r\n"), "{request}"); + assert!(!request.to_ascii_lowercase().contains("x-key:") && !request.contains(secret), "{request}"); + received_tx.send(()).unwrap(); + release_rx.recv_timeout(Duration::from_secs(10)).unwrap(); + stream.write_all(response.as_bytes()).unwrap(); + }); + let (base, asked) = serve_recorded(vec![ + format!("HTTP/1.1 302 Found\r\nLocation: {other}/refused\r\nContent-Length: 0\r\nConnection: close\r\n\r\n").into_bytes(), + ]); + let binding = logins::activate(&base, "redirect-get", Method::Header("X-Key".into()), secret, true).unwrap(); + let plain = format!("{base}/start"); + let url = logins::bind(&binding, &plain).unwrap(); + let history = History::default(); + let _scope = history.enter(); + let capture = Capture::current(); + let worker = std::thread::spawn(move || { + let _scope = capture.enter(); + native::get_detailed(&native::agent(), &url) + }); + let request = asked.recv_timeout(Duration::from_secs(10)).unwrap(); + assert!(request.to_ascii_lowercase().contains(&format!("x-key: {secret}\r\n")), "{request}"); + received_rx.recv_timeout(Duration::from_secs(10)).unwrap(); + if revoke { + assert!(logins::deactivate(&binding)); + } + release_tx.send(()).unwrap(); + let failure = worker.join().unwrap().unwrap_err(); + server.join().unwrap(); + assert!(failure.answer.is_none(), "{failure:?}"); + if revoke { + assert!(failure.message.contains("no longer active"), "{failure:?}"); + } else { + assert_eq!(failure.reason, Reason::Status(403)); + assert_eq!(failure.message, "http status: 403"); + assert!(logins::deactivate(&binding)); + } + let records = network_records(&history, &plain); + assert_eq!(records.len(), 1); + assert_eq!(records[0].network.as_ref().unwrap().status, Some(403)); + assert!(records[0].details.is_empty(), "{records:?}"); + let text = format!("{failure:?}\n{records:?}\n{}", history.report(Filter::All)); + for private in [secret, "x-request-id", "private refusal echoed", binding.as_str()] { + assert!(!text.contains(private), "{private} reached the failure or diagnostics: {text}"); + } + } + + // A genuinely anonymous redirect still retains useful refusal headers and body. + let body = "useful anonymous refusal"; + let (other, asked) = serve_recorded(vec![ + format!( + "HTTP/1.1 403 Forbidden\r\nx-request-id: anonymous-trace-8430\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", + body.len() + ) + .into_bytes(), + ]); + let base = + serve(vec![format!("HTTP/1.1 302 Found\r\nLocation: {other}/refused\r\nContent-Length: 0\r\nConnection: close\r\n\r\n")]); + let url = format!("{base}/anonymous"); + let history = History::default(); + let _scope = history.enter(); + let failure = native::get_detailed(&native::agent(), &url).unwrap_err(); + let request = asked.recv_timeout(Duration::from_secs(10)).unwrap(); + assert!(request.starts_with("GET /refused HTTP/1.1\r\n"), "{request}"); + assert!(!request.to_ascii_lowercase().contains("x-key:"), "{request}"); + assert_eq!(failure.reason, Reason::Status(403)); + assert_eq!(failure.excerpt(), Some(body)); + assert!( + failure.answer.as_ref().unwrap().headers.iter().any(|(name, value)| name == "x-request-id" && value == "anonymous-trace-8430") + ); + let records = network_records(&history, &url); + assert_eq!(records.len(), 1); + assert_eq!(records[0].network.as_ref().unwrap().status, Some(403)); + assert!(records[0].details.contains("x-request-id: anonymous-trace-8430"), "{records:?}"); + } + #[cfg(not(target_arch = "wasm32"))] #[test] fn logins_head_and_post_carry_the_login() { diff --git a/crates/ui-egui/src/connectionui.rs b/crates/ui-egui/src/connectionui.rs index 654c4453be..7028ba60aa 100644 --- a/crates/ui-egui/src/connectionui.rs +++ b/crates/ui-egui/src/connectionui.rs @@ -55,6 +55,8 @@ pub struct State { pub struct TestContext { opened: u64, form: Value, + /// Capture the displayed translation so a language change cannot orphan this Test's status. + status: &'static str, _login: Option, } @@ -442,12 +444,13 @@ pub fn start_test(app: &mut RustGisApp) { if service && let Some(id) = &app.ui.connection.id { q["id"] = json!(id); } - let context = TestContext { opened: app.session.opened, form: definition(&app.ui.connection), _login: login }; + let status = tl!("Testing…"); + let context = TestContext { opened: app.session.opened, form: definition(&app.ui.connection), status, _login: login }; let slot: Slot, String>> = Slot::default(); let land = slot.clone(); cache::download(&url, Box::new(move |r| *land.lock().unwrap_or_else(|e| e.into_inner()) = Some(r))); app.ui.connection.test = Some((q, slot, context)); - app.dialogs.info = Some(tl!("Testing…").into()); + app.dialogs.info = Some(status.into()); } /// A request remains authorized only for its original project and exact saved definitions. @@ -610,9 +613,13 @@ fn poll_requests(app: &mut RustGisApp) { pub fn poll(app: &mut RustGisApp, ctx: &egui::Context) { poll_requests(app); if !matches!(app.dialogs.open, Some("newConnection" | "editConnection")) { - if app.ui.connection.test.take().is_some() && app.dialogs.open.is_none() { - app.dialogs.info = None; - app.dialogs.error = None; + if let Some((_, _, context)) = app.ui.connection.test.take() { + if app.dialogs.open.is_none() { + app.dialogs.info = None; + app.dialogs.error = None; + } else if app.dialogs.info.as_deref() == Some(context.status) { + app.dialogs.info = None; + } } return; } @@ -1365,16 +1372,29 @@ mod tests { } #[test] - fn abandoned_probe_preserves_replacement_dialog_status() { - let mut app = RustGisApp::with_sample(); - open_new(&mut app, "wmts").unwrap(); - let context = TestContext { opened: app.session.opened, form: definition(&app.ui.connection), _login: None }; - app.ui.connection.test = Some((json!({}), Slot::default(), context)); - app.dialogs.open = Some("addDelimited"); - app.dialogs.error = Some("replacement file read failed".into()); - poll(&mut app, &egui::Context::default()); - assert!(app.ui.connection.test.is_none()); - assert_eq!(app.dialogs.error.as_deref(), Some("replacement file read failed")); + fn abandoned_probe_clears_only_its_own_status_in_a_replacement_dialog() { + // A captured translation differs from the current language without changing global state. + let status = "Prüfung läuft…"; + for (dialog, info, expected_info, expected_error) in [ + (Some("addDelimited"), Some(status), None, Some("replacement file read failed")), + (Some("addDelimited"), Some("replacement file loaded"), Some("replacement file loaded"), Some("replacement file read failed")), + (Some("addDelimited"), None, None, Some("replacement file read failed")), + (None, Some(status), None, None), + (None, Some("previous info"), None, None), + ] { + let mut app = RustGisApp::with_sample(); + open_new(&mut app, "wmts").unwrap(); + let context = TestContext { opened: app.session.opened, form: definition(&app.ui.connection), status, _login: None }; + app.ui.connection.test = Some((json!({}), Slot::default(), context)); + app.dialogs.open = dialog; + app.dialogs.info = info.map(str::to_owned); + app.dialogs.error = Some("replacement file read failed".into()); + poll(&mut app, &egui::Context::default()); + assert!(app.ui.connection.test.is_none()); + assert_eq!(app.dialogs.open, dialog); + assert_eq!(app.dialogs.info.as_deref(), expected_info); + assert_eq!(app.dialogs.error.as_deref(), expected_error); + } } #[test] From c764cbe35168f4fa638b29a55d4ed080d466e891 Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Sun, 8 Nov 2026 20:36:03 +0100 Subject: [PATCH 5/6] fix: redact rejected authenticated redirects --- crates/tiles/src/cache.rs | 66 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/crates/tiles/src/cache.rs b/crates/tiles/src/cache.rs index 904f103e6e..06fed93870 100644 --- a/crates/tiles/src/cache.rs +++ b/crates/tiles/src/cache.rs @@ -2059,11 +2059,16 @@ mod native { if crate::earthdata::is_data_host(&at) && crate::earthdata::is_login(&next) { return Err(Failure::without_answer("", crate::earthdata::refused_message(url), Reason::Status(401))); } - // Where a redirect leads is the server's choice, not the user's. - crate::address::check(&next, Some(&at))?; + // A server can echo a login in its chosen destination, even after revocation. + crate::address::check(&next, Some(&at)) + .map_err(|e| if carried_login { "redirect destination is not allowed".to_string() } else { e })?; // Never from HTTPS down to plain HTTP. if at.starts_with("https://") && !next.starts_with("https://") { - return Err(format!("{next}: a redirect from https to http").into()); + return Err(if carried_login { + "a redirect from https to http".into() + } else { + format!("{next}: a redirect from https to http").into() + }); } redirects += 1; if redirects > 5 { @@ -3331,6 +3336,61 @@ mod tests { assert!(records[0].details.contains("x-request-id: anonymous-trace-8430"), "{records:?}"); } + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn delayed_rejected_redirect_after_revocation_does_not_record_login() { + if isolate_cache_fixture("cache::tests::delayed_rejected_redirect_after_revocation_does_not_record_login") { + return; + } + let _turn = crate::cache::turn(); + use crate::logins::{self, Method}; + use rustgis_data::diagnostics::{Capture, Filter, History}; + use std::io::Write; + use std::sync::mpsc::channel; + use std::time::Duration; + + let secret = "redirect-rejection-sentinel"; + let destination = format!("ftp://example.com/{secret}"); + let response = format!("HTTP/1.1 302 Found\r\nLocation: {destination}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"); + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + let (received_tx, received_rx) = channel(); + let (release_tx, release_rx) = channel(); + let server = std::thread::spawn(move || { + let (mut stream, _) = listener.accept().unwrap(); + let request = read_request(&mut stream); + assert!(request.to_ascii_lowercase().contains(&format!("x-key: {secret}\r\n")), "{request}"); + received_tx.send(()).unwrap(); + release_rx.recv_timeout(Duration::from_secs(10)).unwrap(); + stream.write_all(response.as_bytes()).unwrap(); + }); + let binding = logins::activate(&base, "rejected-redirect", Method::Header("X-Key".into()), secret, true).unwrap(); + let plain = format!("{base}/start"); + let url = logins::bind(&binding, &plain).unwrap(); + let history = History::default(); + let _scope = history.enter(); + let capture = Capture::current(); + let worker = std::thread::spawn(move || { + let _scope = capture.enter(); + native::get_detailed(&native::agent(), &url) + }); + received_rx.recv_timeout(Duration::from_secs(10)).unwrap(); + assert!(logins::deactivate(&binding)); + release_tx.send(()).unwrap(); + let failure = worker.join().unwrap().unwrap_err(); + server.join().unwrap(); + let records = network_records(&history, &plain); + assert_eq!(records.len(), 1); + let text = format!("{failure:?}\n{records:?}\n{}", history.report(Filter::All)); + assert!(!text.contains(secret), "credential reached failure or diagnostics: {text}"); + + let (base, _) = serve_recorded(vec![ + format!("HTTP/1.1 302 Found\r\nLocation: {destination}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n").into_bytes(), + ]); + let failure = native::get_detailed(&native::agent(), &format!("{base}/anonymous")).unwrap_err(); + assert!(failure.message.contains(&destination), "{failure:?}"); + } + #[cfg(not(target_arch = "wasm32"))] #[test] fn logins_head_and_post_carry_the_login() { From b5cc1e2c50f4d9981ac6da40bccd486b0339f141 Mon Sep 17 00:00:00 2001 From: Daniel Hofbauer Date: Tue, 10 Nov 2026 08:51:38 +0100 Subject: [PATCH 6/6] fix(engine): log a response size once --- crates/engine/src/project_log.rs | 22 ++++++++++++---------- crates/engine/src/tests.rs | 7 +++++++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/project_log.rs b/crates/engine/src/project_log.rs index 59536d8cc7..f912b708aa 100644 --- a/crates/engine/src/project_log.rs +++ b/crates/engine/src/project_log.rs @@ -75,12 +75,13 @@ pub(crate) fn is_logged(s: &Session, id: &str, params: &Value) -> bool { /// each giving up more: texts at that bound and lists at their first [`LOG_LIST_ITEMS`]; then /// texts at 512 characters; then the first keys with their short values only. pub(crate) fn kept_params(params: &Value) -> Value { - let v = redacted(&clip(params, LOG_PARAMS_BYTES, LOG_LIST_ITEMS, 0)); + let v = redacted(&clip(params, LOG_PARAMS_BYTES, LOG_LIST_ITEMS, 0, true)); if serde_json::to_vec(&v).map_or(0, |b| b.len()) <= LOG_PARAMS_BYTES { return v; } - // Texts shorter; lists as they are (already cut, their "… N items in all" kept). - let clipped = clip(&v, 512, usize::MAX, 0); + // Texts shorter; lists as they are (already cut, their "… N items in all" kept); responses + // already given as their sizes. + let clipped = clip(&v, 512, usize::MAX, 0, false); if serde_json::to_vec(&clipped).map_or(0, |b| b.len()) <= LOG_PARAMS_BYTES { return clipped; } @@ -457,10 +458,10 @@ fn is_base64(t: &str) -> bool { } /// `v` with each text cut to `chars` characters (a key to 200), a text that reads as a document -/// (JSON or XML past [`DOCUMENT_CHARS`]) or as a file's contents in base64 given as its size, and -/// each list cut to its first `items` (and how many there were); nesting past eight levels is -/// left out. -fn clip(v: &Value, chars: usize, items: usize, depth: usize) -> Value { +/// (JSON or XML past [`DOCUMENT_CHARS`]) or as a file's contents in base64 given as its size, a +/// response (`answer`) given as its size when `answers`, and each list cut to its first `items` +/// (and how many there were); nesting past eight levels is left out. +fn clip(v: &Value, chars: usize, items: usize, depth: usize, answers: bool) -> Value { match v { // A byte-order mark before it is still a document's start. Value::String(t) @@ -471,7 +472,7 @@ fn clip(v: &Value, chars: usize, items: usize, depth: usize) -> Value { Value::String(t) if is_base64(t) => Value::String(format!("(file contents of {} characters, base64)", t.len())), Value::String(t) if t.chars().nth(chars).is_some() => Value::String(format!("{}…", t.chars().take(chars).collect::())), Value::Array(a) if depth < 8 => { - let mut out: Vec = a.iter().take(items).map(|x| clip(x, chars, items, depth + 1)).collect(); + let mut out: Vec = a.iter().take(items).map(|x| clip(x, chars, items, depth + 1, answers)).collect(); if a.len() > items { out.push(Value::String(format!("… {} items in all", a.len()))); } @@ -486,12 +487,13 @@ fn clip(v: &Value, chars: usize, items: usize, depth: usize) -> Value { let value = || { if k.chars().nth(200).is_some() { Value::String("…".into()) - } else if k == "answer" + } else if answers + && k == "answer" && let Some(text) = x.as_str() { Value::String(format!("(a response of {} characters)", text.chars().count())) } else { - clip(x, chars, items, depth + 1) + clip(x, chars, items, depth + 1, answers) } }; out.entry(k.chars().take(200).collect::()).or_insert_with(value); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 43b76f3a49..6262c76508 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -32061,6 +32061,13 @@ fn the_session_log_keeps_parameters_bounded_and_without_secrets() { let kept = crate::project_log::kept_params(&json!({"script": line})); assert!(!kept.to_string().contains("S3CRET") && kept.to_string().contains("s3.connect"), "{kept}"); } + // A response is logged as its own size, also when the rest makes the entry large. + let answer = "a".repeat(41); + let notes: Vec = (0..50).map(|_| json!("n".repeat(200))).collect(); + let kept = crate::project_log::kept_params(&json!({"answer": answer, "notes": notes})); + assert_eq!(kept["answer"], json!("(a response of 41 characters)"), "{kept}"); + let kept = crate::project_log::kept_params(&json!({"answer": "short"})); + assert_eq!(kept["answer"], json!("(a response of 5 characters)"), "{kept}"); } /// Plugins can neither empty the session log nor turn it off, nor push the user's entries out of