diff --git a/crates/data/src/login_address.rs b/crates/data/src/login_address.rs index bc6d7c2b52..3f1029ffac 100644 --- a/crates/data/src/login_address.rs +++ b/crates/data/src/login_address.rs @@ -113,11 +113,56 @@ pub fn reached(address: &str) -> &str { } } +/// Canonical origin of the scheme and authority alone (`https://host:port`), ignoring the path, +/// query and fragment. The authority itself is held to `raw_origin`'s rules, so whitespace, +/// controls, backslashes or user information before the first `/`, `?` or `#` still refuse it. +fn authority_origin(url: &str) -> Option { + let (scheme, rest) = url.split_once("://")?; + let end = rest.find(['/', '?', '#']).unwrap_or(rest.len()); + raw_origin(url.get(..scheme.len() + 3 + end)?) +} + +/// A URL whose path, query or fragment holds whitespace, controls or backslashes, rewritten only +/// in those characters so the bound wrapper accepts it: trailing spaces and controls, tabs and +/// line breaks are dropped and a backslash in the path is a `/` (as a WHATWG URL parser such as +/// the `url` crate does on sending); other whitespace, controls and a backslash in the query or +/// fragment are percent-encoded (WHATWG sends a query backslash raw, which the wrapper refuses). +/// Other characters, template braces included, are left as they are; the HTTP client encodes +/// them as it would for any address. Only called once the authority is known to be clean, +/// which this keeps. +fn as_sent(url: &str) -> String { + let url = url.trim_end_matches(|c: char| c <= ' '); + let mut out = String::with_capacity(url.len()); + let mut in_path = true; + for c in url.chars() { + match c { + '\t' | '\n' | '\r' => {} + '?' | '#' => { + in_path = false; + out.push(c); + } + '\\' if in_path => out.push('/'), + c if c == '\\' || c.is_whitespace() || c.is_control() => { + let mut buf = [0; 4]; + for b in c.encode_utf8(&mut buf).bytes() { + out.push_str(&format!("%{b:02X}")); + } + } + c => out.push(c), + } + } + out +} + /// Retains a document's binding for same-origin derived addresses only. pub fn inherit(base: &str, url: &str) -> String { let url = inner(url); let Ok(Some(b)) = parse(base) else { return url.to_string() }; - if origin(url).as_deref() == Some(b.origin) { + // Compare on the authority alone, so a same-origin link whose path or query holds a space, + // a backslash or a control character keeps its login instead of silently going anonymous. + // Such a link is written as it would be sent; the authority, and so the origin, is unchanged. + if authority_origin(url).as_deref() == Some(b.origin) { + let url = if raw_origin(url).is_some() { url.to_string() } else { as_sent(url) }; // Keep the actual derived binding even when its size exceeds the parser's bound. // Request validation then refuses it instead of sending an anonymous request. format!("{PREFIX}{}:{}:{}:{url}", b.binding, b.origin.len(), b.origin) @@ -173,6 +218,41 @@ mod tests { assert_eq!(inherit(&base, &foreign), foreign); } + #[test] + fn same_origin_link_with_unsafe_characters_keeps_its_login() { + let base = bind("0123456789abcdef", "https://a.test", "https://a.test/wms?SERVICE=WMS").unwrap(); + for (derived, sent) in [ + ("https://a.test/wms?LAYERS=a b", "https://a.test/wms?LAYERS=a%20b"), + ("https://a.test/a b/{z}/{x}/{y}.png", "https://a.test/a%20b/{z}/{x}/{y}.png"), + ("https://a.test/dir\\file.png?q=a\\b", "https://a.test/dir/file.png?q=a%5Cb"), + ("https://a.test/x?y=1\u{7}2", "https://a.test/x?y=1%072"), + ("https://a.test/x\ty?z=\n1 ", "https://a.test/xy?z=1"), + ("https://A.TEST:443/x#frag ment", "https://A.TEST:443/x#frag%20ment"), + ("https://a.test?LAYERS=a\u{a0}b", "https://a.test?LAYERS=a%C2%A0b"), + ] { + let inherited = inherit(&base, derived); + let bound = parse(&inherited).unwrap().unwrap(); + assert_eq!((bound.binding, bound.origin, bound.url), ("0123456789abcdef", "https://a.test", sent), "{derived}"); + } + // The authority itself still has to be the same canonical origin: these never keep the login. + for foreign in [ + "https://b.test/wms?LAYERS=a b", + "https://a.test.evil.test/x y", + "https://a.test\\@evil.test/x", + "https://a.test\\.evil.test/x", + "https://a.te st/x", + "https://a.test\t.evil.test/x", + "https://u@a.test/x y", + "http://a.test/x y", + "https://a.test:8443/x y", + " https://a.test/x", + ] { + assert_eq!(inherit(&base, foreign), foreign, "{foreign}"); + } + // Clean same-origin links are kept exactly. + assert_eq!(parse(&inherit(&base, "https://a.test/wms?LAYERS=a+b")).unwrap().unwrap().url, "https://a.test/wms?LAYERS=a+b"); + } + #[test] fn round_trip_and_fixed_errors() { let b = bind("0123456789abcdef", "https://a.test", "https://a.test/x?y=1").unwrap(); diff --git a/crates/data/src/ogc_document_tests.rs b/crates/data/src/ogc_document_tests.rs index 5054fdc487..c0e6cdd3dc 100644 --- a/crates/data/src/ogc_document_tests.rs +++ b/crates/data/src/ogc_document_tests.rs @@ -159,6 +159,34 @@ fn oversized_same_origin_document_targets_fail_closed() { } } +#[test] +fn same_origin_document_links_with_unsafe_characters_keep_the_login() { + let bound_base = crate::login_address::bind(BASE_BINDING, ORIGIN, BASE).unwrap(); + for (href, target, same_origin) in [ + ("https://maps.example.org/a b/advertised", "https://maps.example.org/a%20b/advertised", true), + ("/a b/advertised", "https://maps.example.org/a%20b/advertised", true), + ("a b/advertised", "https://maps.example.org/folder/a%20b/advertised", true), + ("a\\b/advertised", "https://maps.example.org/folder/a/b/advertised", true), + ("?map=a b", "https://maps.example.org/folder/caps.xml?map=a%20b", true), + ("https://foreign.example.org/a b/advertised", "https://foreign.example.org/a b/advertised", false), + ] { + for (base, has_login) in [(BASE, false), (bound_base.as_str(), true)] { + for (boundary, document) in documents::documents(href) { + let service = parse(base, document.as_bytes()).unwrap(); + let context = format!("{boundary} {href}"); + let expected = (has_login && same_origin).then_some(BASE_BINDING); + for request in requests(&service) { + let bound = crate::login_address::parse(&request).unwrap(); + assert_eq!(bound.map(|b| b.binding), expected, "{context}: {request}"); + // An unbound link is used as written, like before; a bound one as it is sent. + let target = if expected.is_some() { target.to_string() } else { target.replace("%20", " ").replace("a/b", "a\\b") }; + assert!(crate::login_address::inner(&request).starts_with(&target), "{context}: {request}"); + } + } + } + } +} + fn assert_operation(request: &str, service: &str, operation: &str, version: &str) { let inner = crate::login_address::inner(request); let url = url::Url::parse(inner).unwrap(); diff --git a/crates/engine/src/cmd/service.rs b/crates/engine/src/cmd/service.rs index f17cc3092e..c25f045474 100644 --- a/crates/engine/src/cmd/service.rs +++ b/crates/engine/src/cmd/service.rs @@ -123,6 +123,8 @@ impl Discovery { } const MAX_SERVICE_DOCUMENTS: usize = 4; +/// A notice for legend pictures a service listed that its layer cannot keep (an address too long). +const LEGENDS_LEFT_OUT_NOTICE: &str = "{n} legend picture(s) the service listed were left out: their addresses are too long"; /// Whether what this `service.add` would wait for is left off the frame: it is a plugin command's, /// run by the desktop app, which runs the command again when it has come (#1319). @@ -907,8 +909,11 @@ fn add_discovered(s: &mut Session, p: &Value, url: &str, want: &str, found: &Dis } } } - // (Those the layer wouldn't keep, an address too long, are left out, not refused.) + // (Those the layer wouldn't keep, an address too long, are left out, not refused, + // and counted in a notice.) let legends = rustgis_map::tiles::checked_legends(layer.legends.iter().cloned()); + let offered = layer.legends.len().min(rustgis_data::ogc::MAX_LEGENDS); + s.notify_loss(LEGENDS_LEFT_OUT_NOTICE, u32::try_from(offered.saturating_sub(legends.len())).unwrap_or(u32::MAX)); if !legends.is_empty() { add["legend"] = json!(legends); } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 82bbe39289..f811647f25 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -13822,6 +13822,28 @@ fn wms_layers_draw_as_one_picture_or_tiles_of_a_size() { assert!(t.url.contains("&size=512,512&") && t.tile_size == 512 && !t.single_image, "{}", t.url); } +/// #1592: a legend picture the layer cannot keep (an address too long, as a same-origin link can +/// become once its spaces are encoded for the login) is counted in a notice, not dropped silently. +#[test] +fn service_legends_left_out_are_reported() { + const WMS: &str = r#" +Test WMS +image/png +EPSG:3857roadsRoads +"#; + let long = format!("https://maps.example.com/legends/{}.png", "a".repeat(rustgis_map::tiles::MAX_LEGEND_URL)); + for (legend, left_out) in [(long.as_str(), true), ("https://maps.example.com/legends/roads.png", false)] { + let mut s = Session::new(Project::default()); + let mark = s.notice_mark(); + s.run("service.add", &json!({"url": "https://maps.example.com/wms", "layer": "roads", "text": WMS.replace("LEGEND", legend)})) + .unwrap(); + let notices = s.notices_since(mark); + let reported = notices.iter().any(|n| n.text.contains("1 legend picture(s) the service listed were left out")); + assert_eq!(reported, left_out, "{notices:?}"); + assert_eq!(s.map().tiles.first().map(|t| t.legends.len()), Some(usize::from(!left_out))); + } +} + /// A WMS layer added from its service takes its legends (the service's GetLegendGraphic, then /// its style's picture); any tile layer can be given one, or several for their scales; they and /// whether they show are kept with the project and undo; junk is refused. diff --git a/crates/map/src/tiles.rs b/crates/map/src/tiles.rs index 79f8f910aa..d70adec904 100644 --- a/crates/map/src/tiles.rs +++ b/crates/map/src/tiles.rs @@ -671,6 +671,16 @@ mod tests { assert!(!is_web("rustgis-login:broken")); } + #[test] + fn same_origin_legend_with_a_space_keeps_its_login() { + let document = rustgis_data::login_address::bind("0123456789abcdef", "https://maps.test", "https://maps.test/wms").unwrap(); + let url = rustgis_data::login_address::inherit(&document, "https://maps.test/legend?LAYER=a b"); + let legend = rustgis_data::ogc::Legend { url: url.clone(), ..Default::default() }; + assert_eq!(checked_legends([legend]).first().map(|l| l.url.as_str()), Some(url.as_str())); + assert_eq!(rustgis_data::login_address::inner(&url), "https://maps.test/legend?LAYER=a%20b"); + assert!(check_url(&url).is_ok()); + } + #[test] fn style_layers_are_checked() { assert!(checked_style_layers(r#"[{"id": "a", "type": "fill"}]"#).is_ok()); diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index 8a3a19e08e..a1b8c7c7dd 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -863,6 +863,9 @@ Relative links, including parent paths, resolve against the document's address w template placeholders or query values, including proxy parameters containing `https://` URLs. KVP requests replace capabilities operation parameters, keeping unrelated parameters such as MapServer's `map=`. +A same-origin link whose path or query holds a space, backslash or control character keeps the +login: tabs, line breaks and trailing spaces are dropped, a backslash in the path becomes `/` and +other spaces, control characters and backslashes are percent-encoded (a space as `%20`). If a service supplies a same-origin link too long to carry its login, the request is refused instead of being fetched anonymously. Addresses that cannot be rebound to a login are left unchanged and reported.