Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions apps/rustgis-web/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ pub(crate) async fn prepare_and_send<T, R, F: Future<Output = Result<T, String>>
Ok(send(prepared))
}

/// Formats a fetch failure without exposing the authenticated request's credentials.
/// Formats a fetch failure without exposing the authenticated request's credentials. The
/// address is sanitized on its own first, so a secret query doesn't take the reason with it.
pub(crate) fn fetch_error(url: &str, error: String, authenticated: bool) -> String {
use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::logins;
if authenticated && cache::http_status(&error).is_none() { logins::scrub_error(&format!("{url}: {error}")) } else { error }
use rustgis_ui_egui::rustgis_engine::{data::diagnostics::message_with_address, render::rustgis_tiles::logins};
if authenticated && cache::http_status(&error).is_none() { logins::scrub_error(&message_with_address(url, &error)) } else { error }
}

/// Checks the browser's normalized destination under the caller's inherited ceiling.
Expand Down
24 changes: 12 additions & 12 deletions apps/rustgis-web/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::rc::Rc;

use rustgis_ui_egui::rustgis_engine::data::diagnostics::Capture;
use rustgis_ui_egui::rustgis_engine::{data::diagnostics::Capture, sources::message_with_address};
use rustgis_ui_egui::{Inbox, RustGisApp, Services, Slot};

use crate::parts::Notes;
Expand Down Expand Up @@ -164,7 +164,7 @@ pub fn start() {
// to different maps depending on when the import completes.
if let Some(url) = &project {
if let Err(e) = app.run("ui.openUrl", serde_json::json!({"url": url})) {
app.session.notify(format!("?project={url}: {e}"), true);
app.session.notify(format!("?project={}", message_with_address(url, &e)), true);
}
let skipped: Vec<&str> =
["data", "stac", "style", "tiles", "zarr"].into_iter().filter(|k| !params(&q, k, 1).is_empty()).collect();
Expand Down Expand Up @@ -199,7 +199,7 @@ pub fn start() {
// in a notice.
for url in params(links, "data", 16) {
if let Err(e) = app.run("ui.addUrl", serde_json::json!({"url": url, "zoom": !linked})) {
app.session.notify(format!("?data={url}: {e}"), true);
app.session.notify(format!("?data={}", message_with_address(&url, &e)), true);
}
}
// `?stac=<url>`: a STAC item (its default asset is added, signed when it is a
Expand All @@ -210,7 +210,7 @@ pub fn start() {
if let Some(url) = stac.first()
&& let Err(e) = app.run("ui.stacLink", serde_json::json!({"url": url}))
{
app.session.notify(format!("?stac={url}: {e}"), true);
app.session.notify(format!("?stac={}", message_with_address(url, &e)), true);
}
if stac.len() > 1 {
app.session.notify(format!("only the first ?stac= link is opened ({} more were left out)", stac.len() - 1), false);
Expand All @@ -228,7 +228,7 @@ pub fn start() {
match app.run("map.addTiles", p) {
// The first style added opens at its view, not one that failed.
Ok(_) => style_view &= kind.is_none(),
Err(e) => app.session.notify(format!("?{key}={url}: {e}"), true),
Err(e) => app.session.notify(format!("?{key}={}", message_with_address(&url, &e)), true),
}
}
}
Expand All @@ -237,7 +237,7 @@ pub fn start() {
if let Some(url) = params(links, "zarr", 1).into_iter().next()
&& let Err(e) = app.run("ui.addZarr", serde_json::json!({"address": url}))
{
app.session.notify(format!("?zarr={url}: {e}"), true);
app.session.notify(format!("?zarr={}", message_with_address(&url, &e)), true);
}
// `?registry=<url>` opens the Plugin Manager on another plugin registry for this
// visit (a pull request's preview, say); installing still asks as usual.
Expand Down Expand Up @@ -735,7 +735,7 @@ async fn fetch_reached(
// holds the whole file (a small archive), checked once it's read; else no ranges.
let whole = range.is_some() && resp.status() != 206;
if range.is_some_and(|(a, _)| whole && a > 0) {
return Err(format!("{url}: {}", cache::NO_RANGES));
return Err(message_with_address(&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.
Expand All @@ -744,7 +744,7 @@ async fn fetch_reached(
{
// 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));
return Err(message_with_address(&url, cache::NO_RANGES));
}
return Err(format!("{len} bytes is too large"));
}
Expand Down Expand Up @@ -772,7 +772,7 @@ async fn fetch_reached(
if (bytes.len() + n) as u64 > max {
let _ = reader.cancel();
if whole_tail.is_some() {
return Err(format!("{url}: {}", cache::NO_RANGES));
return Err(message_with_address(&url, cache::NO_RANGES));
}
return Err(format!("larger than {max} bytes"));
}
Expand All @@ -784,10 +784,10 @@ async fn fetch_reached(
if let Some((a, b)) = range.filter(|_| whole)
&& bytes.len() as u64 > b - a + 1
{
return Err(format!("{url}: {}", cache::NO_RANGES));
return Err(message_with_address(&url, cache::NO_RANGES));
}
if whole_tail.is_some_and(|n| bytes.len() as u64 > n) {
return Err(format!("{url}: {}", cache::NO_RANGES));
return Err(message_with_address(&url, cache::NO_RANGES));
}
Ok(cache::Fetched { bytes, origin: address::Origin::named(&url).retain_binding(&bound)? })
}
Expand Down Expand Up @@ -1249,7 +1249,7 @@ fn services(inbox: Inbox, font_inbox: Inbox, notes: Notes, ctx: egui::Context, e
let capture = Capture::current();
let still = rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::cache::current_still();
wasm_bindgen_futures::spawn_local(async move {
let r = fetch_bytes(&url, capture, still, &[]).await.map_err(|e| format!("{url}: {e}"));
let r = fetch_bytes(&url, capture, still, &[]).await.map_err(|e| message_with_address(&url, &e));
*slot.lock().unwrap_or_else(|e| e.into_inner()) = Some(r);
ctx.request_repaint();
});
Expand Down
4 changes: 2 additions & 2 deletions crates/assistant/src/chatgpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,10 +1136,10 @@ mod native {
return Err(Error {
status: Some(r.status),
request_id: r.request_id().map(str::to_string),
..Error::auth(format!("{url}: HTTP {}", r.status))
..Error::auth(rustgis_engine::data::diagnostics::message_with_address(url, &format!("HTTP {}", r.status)))
});
}
serde_json::from_slice(&r.body).map_err(|_| Error::auth(format!("{url}: not JSON")))
serde_json::from_slice(&r.body).map_err(|_| Error::auth(rustgis_engine::data::diagnostics::message_with_address(url, "not JSON")))
}

/// Verifies a new id token with OpenAI's published keys.
Expand Down
96 changes: 88 additions & 8 deletions crates/assistant/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::provider::{HttpRequest, HttpResponse, allowed_url, reply_limit};
use crate::{Error, error::TransportFailure};
use rustgis_engine::data::diagnostics;

/// Sends `req` and reads the response, whatever its status.
pub fn send(req: &HttpRequest) -> Result<HttpResponse, Error> {
Expand All @@ -23,7 +24,9 @@ fn send_with(req: &HttpRequest, timeout: std::time::Duration, mut on_chunk: Opti
// The address checked is the one used.
let url = req.url.trim();
if !allowed_url(url) {
return Err(format!("{url}: only HTTPS addresses, or plain HTTP on this computer or network, are allowed").into());
return Err(
diagnostics::message_with_address(url, "only HTTPS addresses, or plain HTTP on this computer or network, are allowed").into()
);
}
let agent: ureq::Agent = ureq::Agent::config_builder()
// Models can think for minutes on a hard request.
Expand Down Expand Up @@ -75,12 +78,17 @@ fn send_with(req: &HttpRequest, timeout: std::time::Duration, mut on_chunk: Opti
Ok(HttpResponse { status, body, headers })
}

/// The scheme and host of a URL (errors never repeat a path that might carry a key).
fn host(url: &str) -> String {
let (scheme, rest) = url.split_once("://").unwrap_or(("", url));
let authority = rest.split(['/', '?', '#']).next().unwrap_or("");
// Never the user name or password.
format!("{scheme}://{}", authority.rsplit('@').next().unwrap_or(""))
/// The scheme and authority of an already-sanitized address, without its omission suffix. A
/// root address keeps the `/` that separates it from a following `: `; a path is dropped whole.
fn host(shown: &str) -> &str {
let url = shown.strip_suffix("[query omitted]").or_else(|| shown.strip_suffix("[fragment omitted]")).unwrap_or(shown);
let Some((scheme, rest)) = url.split_once("://") else { return url };
let end = match rest.find('/') {
Some(at) if at + 1 == rest.len() => url.len(),
Some(at) => scheme.len() + 3 + at,
None => url.len(),
};
url.get(..end).unwrap_or(url)
}

/// Only connection failures can be replayed. Timeouts may already have consumed a long request.
Expand All @@ -95,7 +103,17 @@ fn transport_error(e: &ureq::Error, url: &str) -> Error {
}
_ => TransportFailure::Interrupted,
};
Error::transport(kind, format!("couldn't reach {}: {e}", host(url)))
// Establish known-address boundaries before applying the stricter host-only display policy.
// Replacing raw text first could match a prefix of another address's longer credential.
let message = diagnostics::message_with_address(url, &e.to_string());
let shown = diagnostics::address_in_message(url);
let message = if shown.starts_with("https://") || shown.starts_with("http://") {
let host = host(&shown);
if host == shown { message } else { message.replace(&shown, host) }
} else {
message
};
Error::transport(kind, format!("couldn't reach {message}"))
}

#[cfg(test)]
Expand All @@ -107,6 +125,68 @@ mod tests {
time::Duration,
};

#[test]
fn rejected_secret_addresses_keep_the_policy_reason() {
let req = HttpRequest { method: "GET", url: "ftp://models.test/private?token=secret value".into(), headers: vec![], body: None };
let error = send(&req).unwrap_err().to_string();
let error = diagnostics::redact(&diagnostics::redact(&error));
assert!(error.contains("only HTTPS addresses"), "{error}");
assert!(!error.contains("secret") && !error.contains("value"), "{error}");
}

#[test]
fn transport_errors_keep_reasons_without_revealing_request_paths() {
let url = "https://user:password@models.test/private-key?token=secret value";
let cause = ureq::Error::Io(std::io::Error::other(format!("request {url} failed; retry {url}: connection closed")));
let error = transport_error(&cause, url);
assert_eq!(error.transport, Some(TransportFailure::Interrupted));
let error = diagnostics::redact(&diagnostics::redact(&error.to_string()));
assert!(error.contains("couldn't reach https://models.test: ") && error.contains("connection closed"), "{error}");
for hidden in ["user", "password", "private-key", "secret", "value"] {
assert!(!error.contains(hidden), "{error}");
}
}

#[test]
fn omitted_userinfo_is_not_restored_by_host_projection() {
for url in ["https://alice:hidden/x@models.test/private", "https://alice/x:hidden@models.test/private"] {
let cause = ureq::Error::Io(std::io::Error::other(format!("request {url}: connection closed")));
let error = transport_error(&cause, url).to_string();
for secret in ["alice", "hidden", "private"] {
assert!(!error.contains(secret), "{url} -> {error}");
}
assert!(error.contains("connection closed"), "{error}");
assert_eq!(diagnostics::redact(&diagnostics::redact(&error)), error);
}
}

#[test]
fn host_only_addresses_keep_the_host_after_repeated_redaction() {
let url = "https://models.test?token=hidden value";
let cause = ureq::Error::Io(std::io::Error::other("connection closed"));
let error = transport_error(&cause, url).to_string();
let error = diagnostics::redact(&diagnostics::redact(&error));
assert!(error.contains("https://models.test") && error.contains("connection closed"), "{error}");
assert!(!error.contains("hidden") && !error.contains("value"), "{error}");
}

#[test]
fn unknown_transport_addresses_keep_conservative_credential_boundaries() {
let url = "https://models.test/private-key?token=abc";
for reason in [
format!("request {url}XYZ credential tail: connection closed"),
"request https://other.test/a?token=unknown credential tail: connection closed".into(),
] {
let cause = ureq::Error::Io(std::io::Error::other(reason));
let error = transport_error(&cause, url).to_string();
let error = diagnostics::redact(&diagnostics::redact(&error));
assert!(error.contains("couldn't reach https://models.test"), "{error}");
for hidden in ["abc", "XYZ", "unknown", "credential", "tail", "connection closed"] {
assert!(!error.contains(hidden), "{error}");
}
}
}

#[test]
fn native_transport_preserves_connection_timeout_and_interruption_types() {
// Check the typed refusal policy independently of OS-specific TCP retry timing.
Expand Down
Loading
Loading