From 8dadac33e21eb977730969fbca5a5a84dc0ae9f1 Mon Sep 17 00:00:00 2001 From: aum Date: Mon, 9 Nov 2026 18:54:40 +0530 Subject: [PATCH] fix(engine): preserve bare schemes in redacted errors Keep scheme-only examples and ellipsis placeholders intact in error text, while still redacting credentials from real addresses. Cover prose hints, secret-bearing URLs, and the layer.addUrl error path. Co-Authored-By: Claude Code --- crates/engine/src/sources.rs | 19 +++++++++++++++++++ crates/engine/src/tests.rs | 7 +++++++ 2 files changed, 26 insertions(+) diff --git a/crates/engine/src/sources.rs b/crates/engine/src/sources.rs index e8315d6e69..8428b872d3 100644 --- a/crates/engine/src/sources.rs +++ b/crates/engine/src/sources.rs @@ -325,6 +325,9 @@ pub fn redact_text(text: &str) -> String { let scheme = |c: &char| c.is_ascii_alphanumeric() || "+.-".contains(*c); let start = word.get(..at).and_then(|b| b.char_indices().rev().find(|(_, c)| !scheme(c))).map_or(0, |(i, c)| i + c.len_utf8()); let end = word.trim_end_matches([':', ',', ';', '.', ')', ']', '>', '"', '\'', '»', '›', '”', '’']).len().max(at + 3); + if matches!(word.get(at + 3..end), Some("" | "…")) { + return piece.to_string(); + } let (before, address, after) = (word.get(..start).unwrap_or(""), word.get(start..end).unwrap_or(""), word.get(end..).unwrap_or("")); format!("{before}{}{after}{}", redact(address), piece.get(word.len()..).unwrap_or("")) @@ -536,6 +539,22 @@ fn build_library() -> Vec<(&'static str, Source)> { mod tests { use super::*; + #[test] + fn error_text_keeps_bare_schemes_but_redacts_addresses() { + for text in [ + "give an http://, https:// or s3:// address", + "needs an http:// or https:// address", + "uses unencrypted http://. Tick the confirmation", + "only http:// and https:// addresses are fetched", + "a placeholder: \"https://…\"", + ] { + assert_eq!(redact_text(text), text, "{text}"); + } + assert_eq!(redact_text("see https://user:pw@host/x, or http://"), "see https://host/x, or http://"); + assert_eq!(redact_text("see https://host/x?token=secret, or s3://"), "see https://host/x, or s3://"); + assert!(!redact_text("https://user:pw@host://").contains("pw")); + } + #[test] fn restored_sources_never_reuse_a_previous_process_login_binding() { let address = rustgis_data::s3_address::bind("0123456789abcdef", "https://s3.example.org", "s3://data/a.fgb").unwrap(); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 13edd10b8e..f01a8b063f 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -40,6 +40,13 @@ fn world() -> Session { s } +#[test] +fn add_url_error_keeps_bare_schemes() { + let mut s = Session::new(Project::default()); + let error = s.run("layer.addUrl", &json!({"url": "C:/data/a.tif"})).unwrap_err(); + assert_eq!(error, "C:/data/a.tif: give an http://, https:// or s3:// address (Add Data opens files)"); +} + /// Runs a test that owns process-wide state in a fresh instance of the test executable. #[cfg(not(target_arch = "wasm32"))] pub(crate) fn isolate_process_test(name: &str) -> bool {