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
52 changes: 52 additions & 0 deletions apps/rustgis-web/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ 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.
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 }
}

/// Checks the browser's normalized destination under the caller's inherited ceiling.
/// Normalization may reveal a local host; its original spelling cannot narrow an explicit
/// local choice or widen a public document's permission. The normalizer performs no I/O.
pub(crate) fn destination(
url: &str,
ceiling: rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::address::Reach,
normalize: impl FnOnce(&str) -> Result<String, String>,
) -> Result<String, String> {
let normalized = normalize(url)?;
rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::address::check_reach(&normalized, ceiling)?;
Ok(normalized)
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -49,6 +68,39 @@ mod tests {
task::{Context, Poll, Waker},
};

/// The browser parser's output is checked at the same preparation boundary used by GET,
/// HEAD and POST: an explicit local choice survives normalization, but public input does not.
#[test]
fn normalized_destinations_keep_the_request_ceiling() {
use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::address::Reach;
for ceiling in [Reach::Local, Reach::Public] {
for (spelling, normalized, permitted) in [
("http://local%68ost:3000/style.json", "http://localhost:3000/style.json", ceiling == Reach::Local),
("http://%31%36%39.254.169.254/tile", "http://169.254.169.254/tile", false),
("https://public.example/tile", "https://public.example/tile", true),
] {
let result = destination(spelling, ceiling, |_| Ok(normalized.into()));
assert_eq!(result.is_ok(), permitted, "{spelling} under {ceiling:?}");
if permitted {
assert_eq!(result.unwrap(), normalized);
}
}
}
}

/// Status-only authenticated failures retain their machine-readable routine gap/backoff signal.
#[test]
fn authenticated_fetch_errors_preserve_http_statuses() {
for authenticated in [false, true] {
for error in ["HTTP 404", "HTTP 410", "HTTP 429 (retry after 30 s)"] {
assert_eq!(fetch_error("https://status.example/tile", error.into(), authenticated), error);
}
}
let error = fetch_error("https://status.example/tile?token=synthetic", "couldn't reach it".into(), true);
assert!(!error.contains("synthetic"));
assert!(cache::http_status(&error).is_none());
}

fn poll<F: Future + ?Sized>(f: Pin<&mut F>) -> Poll<F::Output> {
f.poll(&mut Context::from_waker(Waker::noop()))
}
Expand Down
68 changes: 53 additions & 15 deletions apps/rustgis-web/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,11 +597,24 @@ async fn fetch_bytes(
still: Option<rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::cache::Still>,
omit: &'static [u16],
) -> Result<Vec<u8>, String> {
use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::address::Reach;
fetch_reached(url, Reach::Local, capture, still, omit).await.map(|fetched| fetched.bytes)
}

/// GET carries its inherited reach into final URL preparation and returns the normalized
/// credential-free destination with its bytes. Fetch never follows an opaque redirect.
async fn fetch_reached(
url: &str,
ceiling: rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::address::Reach,
capture: Capture,
still: Option<rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::cache::Still>,
omit: &'static [u16],
) -> Result<rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::cache::Fetched, String> {
use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{cache::RequestDiagnostics, failure::Failure};
let mut diagnostics = RequestDiagnostics::with_capture(url, "GET", capture);
diagnostics.omit_statuses(omit);
let answer = async {
use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{cache, logins};
use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{address, cache, logins};
let (promise, abort, range, tail, max, url, bound, authenticated) = crate::request::prepare_and_send(
&still,
|| async {
Expand All @@ -617,6 +630,7 @@ async fn fetch_bytes(
let bound = url;
let prepared = rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::s3::prepare(logins::inner(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
// is about to expire. It goes as the request's `token` parameter, not a header: ArcGIS's
// query endpoints answer the header's CORS preflight with an error.
Expand All @@ -631,6 +645,10 @@ async fn fetch_bytes(
if prepared.signed { None } else { rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::arcgis_auth::with_token(login_url) };
// Only the request carries credentials: messages and cache keys keep the original address.
let request = arcgis.as_deref().unwrap_or(login_url);
// URL parsing in a browser can normalize a numeric or escaped hostname. Judge
// that actual destination against the already checked, prepared address.
let normalized = web_sys::Url::new(request).map_err(|_| "not a valid request address")?;
address::check_reach(&normalized.href(), ceiling)?;
Comment on lines +649 to +651

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.

Security (medium-low confidence, ~45%): a refusal here can echo the credential. request is arcgis.unwrap_or(login_url), and with_token / login.apply append the session token or login credential as a query parameter. check_reach(&normalized.href(), ceiling) then formats "{url}: …" with that full href. The ? returns this error out of prepare_and_send and skips fetch_error, so it reaches cache as State::Failed and then the drawing notices unscrubbed. History records are masked by redact(), but the returned string is not.

The credential-free address was already judged at line 633, and the token only adds a query parameter, so judging the credential-free url keeps the boundary without echoing the token:

Suggested change
// that actual destination against the already checked, prepared address.
let normalized = web_sys::Url::new(request).map_err(|_| "not a valid request address")?;
address::check_reach(&normalized.href(), ceiling)?;
let normalized = web_sys::Url::new(request).map_err(|_| "not a valid request address")?;
// Judge the credential-free address: login and ArcGIS parameters are appended to `request` only,
// and a refusal must not echo them.
address::check_reach(&web_sys::Url::new(url).map_err(|_| "not a valid request address")?.href(), ceiling)?;

Confidence is moderate because the leak needs an authenticated request whose destination normalizes to a local or forbidden address.

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.

The earlier check_destination(url, ceiling) already checks Url::new(url).href() through request::destination, before renewal or credential attachment. An escaped host that normalizes to a refused destination exits there. Service logins currently add a header; the session-token helper appends an encoded query value without changing the origin.

I checked 18 cases using actual browser normalization and the production request/address helpers: 13 refusals occurred before credential attachment and five permitted controls passed. Skipping the earlier check made the same probe detect the synthetic credential in the error. No network requests were sent. I could not reproduce the reported leak on 404d4ad7.

let authenticated = prepared.signed || arcgis.is_some() || login.is_some();
let err = |e: wasm_bindgen::JsValue| {
if authenticated { "the browser couldn't process the authenticated request".to_string() } else { format!("{e:?}") }
Expand Down Expand Up @@ -662,16 +680,15 @@ async fn fetch_bytes(
}
init.set_headers(&headers);
}
// A signed or authorized request goes only where its credentials are for.
if authenticated {
init.set_redirect(web_sys::RequestRedirect::Error);
}
// Fetch hides a manual redirect's Location, so the app cannot check its next
// destination. Refuse it instead of letting the browser follow it unchecked.
init.set_redirect(if authenticated { web_sys::RequestRedirect::Error } else { web_sys::RequestRedirect::Manual });

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.

Behavior change (medium confidence): unauthenticated browser requests now use RequestRedirect::Manual, so every public tile, style, TileJSON and sprite answer that redirects (for example http→https or a CDN hop) now fails with "cannot verify this redirect's destination". Before, the browser followed these. The same applies to head (around line 905).

The PR says this is intentional, and the error text and docs state it. Please confirm it is an accepted regression. If it is, consider naming it in the PR description and in the changelog so users who hit it know to use the final address.

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.

This is intentional: Fetch hides a manual redirect's destination, so following it would bypass the destination check. The map guide now names HTTP-to-HTTPS and CDN redirects, and tells users to use the final address. There is no changelog in this repository. f689640

Workspace CI, strict WebAssembly and all-file hooks pass.

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.

Redirect refusal applies to every browser fetch, but the docs name only tiles, styles, TileJSON and sprites.

fetch_reached (used by fetch_bytes) is the transport for all browser fetches, including user downloads (download_detailed_within via the same fetcher), the app's fetch_font and fetch callbacks, and PMTiles/COG archive range reads. With Manual set here, a CDN or HTTP→HTTPS redirect on any of these now fails with "cannot verify this redirect's destination".

The earlier thread accepted the behavior for tile, style, TileJSON and sprite requests. The docs in maps.md and data.md don't mention downloads, archives or fonts, so a user with a redirecting download host gets a new failure with no guidance.

Suggest either documenting these in data.md (Network downloads) and the PR description, or scoping Manual to the document and tile requests.

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.

The Network downloads documentation and PR compatibility notes now describe redirect refusal for all browser GET requests, including file downloads, fonts, service documents, and PMTiles/COG range reads. They give the final-address and external-download workarounds. The related long type path in fetch_bytes is shortened in this batch.

Fixed in b1657868. All 346 tile tests, workspace CI, strict WebAssembly and all-file hooks pass.

// A credential-bearing address goes nowhere else: no referrer from it, no cookies with it.
if arcgis.is_some() || login.is_some() {
init.set_referrer_policy(web_sys::ReferrerPolicy::NoReferrer);
init.set_credentials(web_sys::RequestCredentials::Omit);
}
Ok((window, request.to_string(), init, abort, range, tail, max, prepared.url, bound.to_string(), authenticated))
Ok((window, normalized.href(), init, abort, range, tail, max, destination, bound.to_string(), authenticated))
},
|(window, request, init, abort, range, tail, max, url, bound, authenticated)| {
(window.fetch_with_str_and_init(&request, &init), abort, range, tail, max, url, bound, authenticated)
Expand Down Expand Up @@ -704,6 +721,7 @@ async fn fetch_bytes(
.dyn_into()
.map_err(err)?;
diagnostics.answered(resp.status());
check_redirect(&resp)?;
if !resp.ok() {
return Err(refused(&resp));
}
Expand Down Expand Up @@ -771,12 +789,12 @@ async fn fetch_bytes(
if whole_tail.is_some_and(|n| bytes.len() as u64 > n) {
return Err(format!("{url}: {}", cache::NO_RANGES));
}
Ok(bytes)
Ok(cache::Fetched { bytes, origin: address::Origin::named(&url).retain_binding(&bound)? })
}
.await;
cache::check_still(&still)?;
logins::check(&bound).map_err(str::to_string)?;
result.map_err(|error| if authenticated { logins::scrub_error(&format!("{url}: {error}")) } else { error })
result.map_err(|error| crate::request::fetch_error(&url, error, authenticated))
}
.await;
diagnostics.record(answer.map_err(|e| Failure::new(url, e))).map_err(|failure| failure.message)
Expand All @@ -793,7 +811,7 @@ async fn post_json(
use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{cache::RequestDiagnostics, failure::Failure};
let mut diagnostics = RequestDiagnostics::with_capture(url, "POST", capture);
let answer = async {
use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{cache, logins, s3};
use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{address, cache, logins, s3};
let (promise, authenticated) = crate::request::prepare_and_send(
&still,
|| async {
Expand All @@ -807,6 +825,7 @@ async fn post_json(
let target = logins::inherit(url, &prepared.url);
let login = logins::apply(&target).map_err(str::to_string)?;
let request = prepared.url.as_str();
check_destination(request, address::Reach::Local)?;
let err = |e: wasm_bindgen::JsValue| {
if login.is_some() { "the browser couldn't process the authenticated request".to_string() } else { format!("{e:?}") }
};
Expand Down Expand Up @@ -857,7 +876,7 @@ async fn post_json(
.await;
cache::check_still(&still)?;
logins::check(url).map_err(str::to_string)?;
result.map_err(|error| if authenticated { logins::scrub_error(&format!("{url}: {error}")) } else { error })
result.map_err(|error| crate::request::fetch_error(url, error, authenticated))
}
.await;
diagnostics.record(answer.map_err(|e| Failure::new(url, e))).map_err(|failure| failure.message)
Expand All @@ -874,7 +893,7 @@ async fn head(
use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{cache::RequestDiagnostics, failure::Failure};
let mut diagnostics = RequestDiagnostics::with_capture(url, "HEAD", capture);
let answer = async {
use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{cache, logins, s3};
use rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::{address, cache, logins, s3};
let sent = crate::request::prepare_and_send(
&still,
|| async {
Expand All @@ -887,6 +906,7 @@ async fn head(
return Ok(None);
}
let url = prepared.url.as_str();
check_destination(url, address::Reach::Local)?;
let target = logins::inherit(bound, url);
let login = logins::apply(&target).map_err(str::to_string)?;
let request = login.as_ref().map_or(url, |applied| applied.url.as_str());
Expand All @@ -895,6 +915,7 @@ async fn head(
};
let init = web_sys::RequestInit::new();
init.set_method("HEAD");
init.set_redirect(if login.is_some() { web_sys::RequestRedirect::Error } else { web_sys::RequestRedirect::Manual });
let login_header = login.as_ref().and_then(|applied| applied.header.as_ref());
if !prepared.headers.is_empty() || login_header.is_some() {
let headers = web_sys::Headers::new().map_err(err)?;
Expand All @@ -909,7 +930,6 @@ async fn head(
init.set_headers(&headers);
}
if login.is_some() {
init.set_redirect(web_sys::RequestRedirect::Error);
init.set_referrer_policy(web_sys::ReferrerPolicy::NoReferrer);
init.set_credentials(web_sys::RequestCredentials::Omit);
}
Expand All @@ -933,6 +953,7 @@ async fn head(
.dyn_into()
.map_err(err)?;
diagnostics.answered(resp.status());
check_redirect(&resp)?;
if !resp.ok() {
return Err(refused(&resp));
}
Expand All @@ -947,12 +968,29 @@ async fn head(
.await;
cache::check_still(&still)?;
logins::check(url).map_err(str::to_string)?;
result.map_err(|error| if authenticated { logins::scrub_error(&format!("{reached}: {error}")) } else { error })
result.map_err(|error| crate::request::fetch_error(&reached, error, authenticated))
}
.await;
diagnostics.record(answer.map_err(|e| Failure::new(url, e))).map_err(|failure| failure.message)
}

/// Checks the URL as the browser parses it, before any request or login renewal is sent.
fn check_destination(url: &str, ceiling: rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::address::Reach) -> Result<String, String> {
crate::request::destination(url, ceiling, |url| {
web_sys::Url::new(url).map(|normalized| normalized.href()).map_err(|_| "not a valid request address".into())
})
}

/// A manual redirect is an opaque, zero-status response: Fetch exposes no Location to check.
/// Other zero-status responses are unusable too; none is treated as a successful download.
fn check_redirect(response: &web_sys::Response) -> Result<(), String> {
if response.status() == 0 {
Err(rustgis_ui_egui::tl!("The browser cannot verify this redirect's destination. Use the final service address.").into())
} else {
Ok(())
}
}

/// Makes an HTTP request (the AI assistant's calls to its model), returning the response of any
/// status.
async fn http(
Expand Down Expand Up @@ -1023,9 +1061,9 @@ fn clipboard() -> Option<web_sys::Clipboard> {
/// the app must not read what the user saved on the site.
fn services(inbox: Inbox, font_inbox: Inbox, notes: Notes, ctx: egui::Context, embedded: bool) -> Services {
// Map tiles come through the browser's fetch (the app repaints when they arrive).
rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::cache::set_fetcher_omitting(|url, done, omit| {
rustgis_ui_egui::rustgis_engine::render::rustgis_tiles::cache::set_fetcher_with_origin(|url, ceiling, done, omit| {
wasm_bindgen_futures::spawn_local(crate::request::handoff(
move |capture, still| async move { fetch_bytes(&url, capture, still, omit).await },
move |capture, still| async move { fetch_reached(&url, ceiling, capture, still, omit).await },
done,
));
});
Expand Down
33 changes: 30 additions & 3 deletions crates/data/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,19 +344,46 @@ impl Capture {
/// request's own facts (address, method, status, duration) and its failure's message: the
/// transport's, or the browser's. `details` may hold the few response headers a refusal
/// keeps. A response body is never passed. Everything is redacted and bounded here.
pub fn network_failure(&self, source: &str, message: &str, details: &str, mut network: Network) {
pub fn network_failure(&self, source: &str, message: &str, details: &str, network: Network) {
self.keep_network_failure(source, message, details, network, false);
}

/// Keeps repeated policy refusals once while their sanitized facts remain in this bounded
/// history. Clearing or evicting the record permits it to be reported again.
pub fn network_failure_once(&self, source: &str, message: &str, details: &str, network: Network) {
self.keep_network_failure(source, message, details, network, true);
}

/// Compares sanitized facts under the retention lock, so Clear cannot lose a refusal.
fn keep_network_failure(&self, source: &str, message: &str, details: &str, mut network: Network, once: bool) {
let Some(state) = self.0.upgrade() else { return };
network.url = clean_address(&network.url);
if once {
// Rejected tile paths on one server are one policy refusal, not distinct attempts.
network.url = url::Url::parse(&network.url)
.ok()
.filter(|u| u.origin().is_tuple())
.map_or(network.url, |u| u.origin().ascii_serialization());
}
network.method = known_method(&network.method);
History(state).retain(Record {
let record = Record {
id: 0,
timestamp_ms: timestamp_ms(),
severity: Severity::Error,
source: bounded(source, MAX_SOURCE_BYTES),
message: bounded(message, MAX_MESSAGE_BYTES),
details: bounded(details, MAX_DETAILS_BYTES),
network: Some(network),
});
};
let mut state = lock(&state);
if once
&& state.records.iter().any(|r| {
r.source == record.source && r.message == record.message && r.details == record.details && r.network == record.network
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
{
return;
}
store(&mut state, record);
}
}

Expand Down
Loading
Loading