Skip to content
Merged
6 changes: 2 additions & 4 deletions apps/rustgis-web/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,11 +626,9 @@ async fn fetch_reached(
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();
let destination = check_destination(url, ceiling)?;
// An ArcGIS sign-in's token, for the portal and the servers it covers, renewed first when it
Expand Down
38 changes: 25 additions & 13 deletions crates/engine/src/project_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,21 @@ 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.
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;
}
Expand Down Expand Up @@ -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)
Expand All @@ -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::<String>())),
Value::Array(a) if depth < 8 => {
let mut out: Vec<Value> = a.iter().take(items).map(|x| clip(x, chars, items, depth + 1)).collect();
let mut out: Vec<Value> = 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())));
}
Expand All @@ -483,7 +484,18 @@ 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 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, answers)
}
};
out.entry(k.chars().take(200).collect::<String>()).or_insert_with(value);
}
Value::Object(out)
Expand Down
52 changes: 52 additions & 0 deletions crates/engine/src/service_logins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance: every authenticated Test teardown wipes the whole tile cache (medium confidence). logins::deactivate calls crate::cache::clear() when it removes a binding. That function keeps only Pending slots and then clears the sources, sprites, PMTiles, MBTiles, COG and warp caches. So each time an unsaved authenticated Test is dropped, after its result, on edit, on cancel, or when a project changes, every public basemap tile in the open map is evicted and re-downloaded.

The existing saved-login revocation has the same effect, but this PR adds a new, frequent trigger. Scoping the clear to entries bound to this login, or deferring it to the next frame, would avoid it. This is a performance concern, not a correctness one. The Drop ordering and lock release are fine.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left for the release quality pass: scoped cache eviction is a performance redesign beyond the existing safe login-revocation mechanism (reviewed at e71e60e).

}
}

/// 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 {
Expand Down
7 changes: 7 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32262,6 +32262,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<Value> = (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
Expand Down
Loading
Loading