From 02aa291efcb7999c22c6934e52dfbf114d5f9675 Mon Sep 17 00:00:00 2001 From: giswqs Date: Sun, 8 Nov 2026 09:58:32 -0500 Subject: [PATCH 1/6] fix(data): keep a same-origin link's login when its path has a space `login_address::inherit` compared origins with `raw_origin` over the whole URL, which refuses any URL holding whitespace, a control character or a backslash anywhere, query included. A same-origin link such as `https://maps.example.org/wms?map=a b`, or a relative href with a space joined onto a bound document, therefore came back unbound and was fetched anonymously with no notice. Compare on the scheme and authority alone, still held to `raw_origin`'s rules, so only a provably same-origin link keeps the binding. The wrapper then fails `parse` because of the unsafe characters, and request validation refuses it instead of sending an anonymous request, as for oversized same-origin links. No other origin gains a login. Fixes #1592 --- crates/data/src/login_address.rs | 48 +++++++++++++++++++++++++-- crates/data/src/ogc_document_tests.rs | 33 ++++++++++++++++++ docs/user-guide/data.md | 5 +-- 3 files changed, 82 insertions(+), 4 deletions(-) diff --git a/crates/data/src/login_address.rs b/crates/data/src/login_address.rs index bc6d7c2b52..760211444f 100644 --- a/crates/data/src/login_address.rs +++ b/crates/data/src/login_address.rs @@ -113,12 +113,23 @@ 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)?) +} + /// 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) { - // Keep the actual derived binding even when its size exceeds the parser's bound. + // Compare on the authority alone: a same-origin link whose path or query holds a space, a + // backslash or a control character keeps its binding rather than silently going anonymous. + if authority_origin(url).as_deref() == Some(b.origin) { + // Keep the actual derived binding even when its size or characters fail the parser. // Request validation then refuses it instead of sending an anonymous request. format!("{PREFIX}{}:{}:{}:{url}", b.binding, b.origin.len(), b.origin) } else { @@ -173,6 +184,39 @@ mod tests { assert_eq!(inherit(&base, &foreign), foreign); } + #[test] + fn same_origin_link_with_unsafe_characters_is_refused_not_anonymous() { + let base = bind("0123456789abcdef", "https://a.test", "https://a.test/wms?SERVICE=WMS").unwrap(); + for derived in [ + "https://a.test/wms?LAYERS=a b", + "https://a.test/dir\\file.png", + "https://a.test/x?y=1\u{7}", + "https://A.TEST:443/x#frag ment", + "https://a.test?LAYERS=a b", + ] { + let inherited = inherit(&base, derived); + assert!(is_wrapper(&inherited), "{derived}"); + assert!(inherited.ends_with(derived), "{derived}"); + assert_eq!(parse(&inherited).unwrap_err(), BAD, "{derived}"); + assert_eq!(check(&inherited).unwrap_err(), BAD, "{derived}"); + } + // The authority itself still has to be a 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.te st/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 keep a valid binding. + assert_eq!(parse(&inherit(&base, "https://a.test/wms?LAYERS=a%20b")).unwrap().unwrap().url, "https://a.test/wms?LAYERS=a%20b"); + } + #[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..44dfeab0db 100644 --- a/crates/data/src/ogc_document_tests.rs +++ b/crates/data/src/ogc_document_tests.rs @@ -159,6 +159,39 @@ fn oversized_same_origin_document_targets_fail_closed() { } } +#[test] +fn same_origin_document_links_with_unsafe_characters_fail_closed() { + let base = crate::login_address::bind(BASE_BINDING, ORIGIN, BASE).unwrap(); + let prefix = format!("rustgis-login:{BASE_BINDING}:{}:{ORIGIN}:", ORIGIN.len()); + for (href, target) in [ + ("https://maps.example.org/a b/advertised", "https://maps.example.org/a b/advertised"), + ("/a b/advertised", "https://maps.example.org/a b/advertised"), + ("a b/advertised", "https://maps.example.org/folder/a b/advertised"), + ("a\\b/advertised", "https://maps.example.org/folder/a\\b/advertised"), + ("?map=a b", "https://maps.example.org/folder/caps.xml?map=a b"), + ] { + for (boundary, document) in documents::documents(href) { + let service = parse(&base, document.as_bytes()).unwrap(); + assert_eq!(service.layers.len(), 1, "{boundary}"); + let layer = &service.layers[0]; + let mut requests = match &layer.offer { + Offer::Tiles { template } => vec![template.clone()], + Offer::Features { url } => vec![url.clone()], + Offer::Coverage(c) => vec![describe_coverage_url(c)], + other => panic!("unexpected offer: {other:?}"), + }; + requests.extend(layer.legends.iter().map(|legend| legend.url.clone())); + for request in requests { + let context = format!("{boundary} {href}"); + let kept = request.strip_prefix(&prefix); + assert!(kept.is_some_and(|url| url.starts_with(target)), "{context}: must keep the login and target: {request}"); + assert!(crate::login_address::parse(&request).is_err(), "{context}"); + assert!(crate::login_address::check(&request).is_err(), "{context}"); + } + } + } +} + 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/docs/user-guide/data.md b/docs/user-guide/data.md index 8a3a19e08e..2139f1bb7c 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -863,8 +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=`. -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 +If a service supplies a same-origin link too long to carry its login, or one whose path or query +holds a space, backslash or control character, the request is refused instead of being fetched +anonymously. Addresses that cannot be rebound to a login are left unchanged and reported. A plain `http://` address sends the login unencrypted, so the form asks for confirmation and the command needs `"allowInsecure": true`. Error messages, notices, the preferences, From e228df561216214fba1abd2500a12b98d01a11bb Mon Sep 17 00:00:00 2001 From: giswqs Date: Sun, 8 Nov 2026 10:17:45 -0500 Subject: [PATCH 2/6] fix(data): send a same-origin link with a space as a browser would Refusing the malformed wrapper made callers that check `is_web(inner(..))` drop such links silently: legends, dated tile addresses on project load, STAC assets, and the tileset fallback lost its connection. Once the authority matches the document's origin, write the rest of the link the way a WHATWG URL parser sends it: drop trailing spaces and controls, tabs and line breaks, turn a backslash in the path into `/` and percent-encode the remaining whitespace, controls and backslashes. The authority is untouched, so the login stays at the same origin, and the wrapper parses, so every caller keeps and fetches the link. Clean links are kept exactly. --- crates/data/src/login_address.rs | 66 ++++++++++++++++++++------- crates/data/src/ogc_document_tests.rs | 45 ++++++++---------- crates/map/src/tiles.rs | 10 ++++ docs/user-guide/data.md | 7 +-- 4 files changed, 83 insertions(+), 45 deletions(-) diff --git a/crates/data/src/login_address.rs b/crates/data/src/login_address.rs index 760211444f..367448297a 100644 --- a/crates/data/src/login_address.rs +++ b/crates/data/src/login_address.rs @@ -122,14 +122,44 @@ fn authority_origin(url: &str) -> Option { raw_origin(url.get(..scheme.len() + 3 + end)?) } +/// A URL whose path, query or fragment holds whitespace, controls or backslashes, written the +/// way a WHATWG URL parser (browsers, the `url` crate) sends it: trailing spaces and controls, +/// tabs and line breaks are dropped, a backslash before the query is a `/`, and the rest are +/// percent-encoded. 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() }; - // Compare on the authority alone: a same-origin link whose path or query holds a space, a - // backslash or a control character keeps its binding rather than silently going anonymous. + // 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) { - // Keep the actual derived binding even when its size or characters fail the parser. + 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) } else { @@ -185,27 +215,29 @@ mod tests { } #[test] - fn same_origin_link_with_unsafe_characters_is_refused_not_anonymous() { + 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 in [ - "https://a.test/wms?LAYERS=a b", - "https://a.test/dir\\file.png", - "https://a.test/x?y=1\u{7}", - "https://A.TEST:443/x#frag ment", - "https://a.test?LAYERS=a b", + 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); - assert!(is_wrapper(&inherited), "{derived}"); - assert!(inherited.ends_with(derived), "{derived}"); - assert_eq!(parse(&inherited).unwrap_err(), BAD, "{derived}"); - assert_eq!(check(&inherited).unwrap_err(), BAD, "{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 a canonical origin: these never keep the login. + // 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", @@ -213,8 +245,8 @@ mod tests { ] { assert_eq!(inherit(&base, foreign), foreign, "{foreign}"); } - // Clean same-origin links keep a valid binding. - assert_eq!(parse(&inherit(&base, "https://a.test/wms?LAYERS=a%20b")).unwrap().unwrap().url, "https://a.test/wms?LAYERS=a%20b"); + // 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] diff --git a/crates/data/src/ogc_document_tests.rs b/crates/data/src/ogc_document_tests.rs index 44dfeab0db..c0e6cdd3dc 100644 --- a/crates/data/src/ogc_document_tests.rs +++ b/crates/data/src/ogc_document_tests.rs @@ -160,33 +160,28 @@ fn oversized_same_origin_document_targets_fail_closed() { } #[test] -fn same_origin_document_links_with_unsafe_characters_fail_closed() { - let base = crate::login_address::bind(BASE_BINDING, ORIGIN, BASE).unwrap(); - let prefix = format!("rustgis-login:{BASE_BINDING}:{}:{ORIGIN}:", ORIGIN.len()); - for (href, target) in [ - ("https://maps.example.org/a b/advertised", "https://maps.example.org/a b/advertised"), - ("/a b/advertised", "https://maps.example.org/a b/advertised"), - ("a b/advertised", "https://maps.example.org/folder/a b/advertised"), - ("a\\b/advertised", "https://maps.example.org/folder/a\\b/advertised"), - ("?map=a b", "https://maps.example.org/folder/caps.xml?map=a b"), +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 (boundary, document) in documents::documents(href) { - let service = parse(&base, document.as_bytes()).unwrap(); - assert_eq!(service.layers.len(), 1, "{boundary}"); - let layer = &service.layers[0]; - let mut requests = match &layer.offer { - Offer::Tiles { template } => vec![template.clone()], - Offer::Features { url } => vec![url.clone()], - Offer::Coverage(c) => vec![describe_coverage_url(c)], - other => panic!("unexpected offer: {other:?}"), - }; - requests.extend(layer.legends.iter().map(|legend| legend.url.clone())); - for request in requests { + 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 kept = request.strip_prefix(&prefix); - assert!(kept.is_some_and(|url| url.starts_with(target)), "{context}: must keep the login and target: {request}"); - assert!(crate::login_address::parse(&request).is_err(), "{context}"); - assert!(crate::login_address::check(&request).is_err(), "{context}"); + 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}"); + } } } } 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 2139f1bb7c..e00dc9b147 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -863,9 +863,10 @@ 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=`. -If a service supplies a same-origin link too long to carry its login, or one whose path or query -holds a space, backslash or control character, the request is refused instead of being fetched -anonymously. Addresses that cannot be rebound to a login are left +A same-origin link whose path or query holds a space, backslash or control character keeps the +login and is sent as a browser would send it (a space as `%20`, a backslash in the path as `/`). +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. A plain `http://` address sends the login unencrypted, so the form asks for confirmation and the command needs `"allowInsecure": true`. Error messages, notices, the preferences, From 3250ce04f66a781831f7a9b66308c3bce183a0a2 Mon Sep 17 00:00:00 2001 From: giswqs Date: Sun, 8 Nov 2026 10:41:15 -0500 Subject: [PATCH 3/6] docs(data): state exactly how a same-origin link with a space is rewritten --- crates/data/src/login_address.rs | 12 ++++++++---- docs/user-guide/data.md | 3 ++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/crates/data/src/login_address.rs b/crates/data/src/login_address.rs index 367448297a..3f1029ffac 100644 --- a/crates/data/src/login_address.rs +++ b/crates/data/src/login_address.rs @@ -122,10 +122,14 @@ fn authority_origin(url: &str) -> Option { raw_origin(url.get(..scheme.len() + 3 + end)?) } -/// A URL whose path, query or fragment holds whitespace, controls or backslashes, written the -/// way a WHATWG URL parser (browsers, the `url` crate) sends it: trailing spaces and controls, -/// tabs and line breaks are dropped, a backslash before the query is a `/`, and the rest are -/// percent-encoded. Only called once the authority is known to be clean, which this keeps. +/// 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()); diff --git a/docs/user-guide/data.md b/docs/user-guide/data.md index e00dc9b147..a1b8c7c7dd 100644 --- a/docs/user-guide/data.md +++ b/docs/user-guide/data.md @@ -864,7 +864,8 @@ template placeholders or query values, including proxy parameters containing `ht 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 and is sent as a browser would send it (a space as `%20`, a backslash in the path as `/`). +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. From ebcb14a019235e54db0b8a3380ec09319744b11d Mon Sep 17 00:00:00 2001 From: giswqs Date: Sun, 8 Nov 2026 11:06:44 -0500 Subject: [PATCH 4/6] fix(map): hold encoded legend and dated addresses to their advertised length A same-origin link that inherits a login now has its spaces written as `%20`, which made it longer than the address the service advertised. Legends and dated tile addresses near the 4096-byte limit could then be left out silently. Count each percent escape as the byte it stands for when checking those limits. --- crates/map/src/tiles.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/crates/map/src/tiles.rs b/crates/map/src/tiles.rs index d70adec904..8b224aa0f8 100644 --- a/crates/map/src/tiles.rs +++ b/crates/map/src/tiles.rs @@ -203,6 +203,15 @@ pub struct TileLayer { /// The longest legend address kept. pub const MAX_LEGEND_URL: usize = 4096; +/// The length of an address's URL with each percent escape counted as the one byte it stands +/// for, so a same-origin link that inherited a login and had its spaces encoded on the way +/// (`rustgis_data::login_address::inherit`) is held to the limit it was advertised under. +fn advertised_len(address: &str) -> usize { + let url = rustgis_data::login_address::inner(address).as_bytes(); + let escapes = url.windows(3).filter(|w| w.first() == Some(&b'%') && w.iter().skip(1).all(u8::is_ascii_hexdigit)).count(); + url.len().saturating_sub(2 * escapes) +} + /// Legend pictures as a tile layer keeps them: web addresses of a sane length, scales that are /// positive numbers (others are open-ended), at most [`MAX_LEGENDS`](rustgis_data::ogc::MAX_LEGENDS). pub fn checked_legends(legends: impl IntoIterator) -> Vec { @@ -210,7 +219,7 @@ pub fn checked_legends(legends: impl IntoIterator Result<(), String> { self.url = self.url.trim().to_string(); // A dated address is a web address whose date is filled in after the scheme and host. - if self.time_url.as_ref().is_some_and(|u| !is_web(u) || rustgis_data::login_address::inner(u).len() > 4096) { + if self.time_url.as_ref().is_some_and(|u| !is_web(u) || advertised_len(u) > 4096) { self.time_url = None; } // A file per date: web addresses, finite dates, in order. - self.time_urls.retain(|(d, u)| d.is_finite() && is_web(u) && rustgis_data::login_address::inner(u).len() <= 4096); + self.time_urls.retain(|(d, u)| d.is_finite() && is_web(u) && advertised_len(u) <= 4096); // Sorted before the cap, so a list out of order keeps its first dates, not any. self.time_urls.sort_by(|a, b| a.0.total_cmp(&b.0)); self.time_urls.dedup_by(|a, b| a.0 == b.0); @@ -679,6 +688,18 @@ mod tests { 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()); + // The limit applies to the link as advertised, not to its longer encoded form. + let near = format!("https://maps.test/l?x={}", "a ".repeat((MAX_LEGEND_URL - 22) / 2)); + assert!(near.len() <= MAX_LEGEND_URL); + let url = rustgis_data::login_address::inherit(&document, &near); + assert!(rustgis_data::login_address::inner(&url).len() > MAX_LEGEND_URL); + assert_eq!(checked_legends([rustgis_data::ogc::Legend { url: url.clone(), ..Default::default() }]).len(), 1); + let mut layer = TileLayer::new("T", TileKind::Raster, "https://maps.test/{z}/{x}/{y}.png"); + layer.time_url = Some(url.clone()); + layer.time_urls = vec![(0.0, url)]; + assert!(layer.repair().is_ok()); + assert!(layer.time_url.is_some()); + assert_eq!(layer.time_urls.len(), 1); } #[test] From 48586caa8a86b45594416405f089e14255436c09 Mon Sep 17 00:00:00 2001 From: giswqs Date: Sun, 8 Nov 2026 11:24:38 -0500 Subject: [PATCH 5/6] Revert "fix(map): hold encoded legend and dated addresses to their advertised length" This reverts commit ebcb14a019235e54db0b8a3380ec09319744b11d. --- crates/map/src/tiles.rs | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) diff --git a/crates/map/src/tiles.rs b/crates/map/src/tiles.rs index 8b224aa0f8..d70adec904 100644 --- a/crates/map/src/tiles.rs +++ b/crates/map/src/tiles.rs @@ -203,15 +203,6 @@ pub struct TileLayer { /// The longest legend address kept. pub const MAX_LEGEND_URL: usize = 4096; -/// The length of an address's URL with each percent escape counted as the one byte it stands -/// for, so a same-origin link that inherited a login and had its spaces encoded on the way -/// (`rustgis_data::login_address::inherit`) is held to the limit it was advertised under. -fn advertised_len(address: &str) -> usize { - let url = rustgis_data::login_address::inner(address).as_bytes(); - let escapes = url.windows(3).filter(|w| w.first() == Some(&b'%') && w.iter().skip(1).all(u8::is_ascii_hexdigit)).count(); - url.len().saturating_sub(2 * escapes) -} - /// Legend pictures as a tile layer keeps them: web addresses of a sane length, scales that are /// positive numbers (others are open-ended), at most [`MAX_LEGENDS`](rustgis_data::ogc::MAX_LEGENDS). pub fn checked_legends(legends: impl IntoIterator) -> Vec { @@ -219,7 +210,7 @@ pub fn checked_legends(legends: impl IntoIterator Result<(), String> { self.url = self.url.trim().to_string(); // A dated address is a web address whose date is filled in after the scheme and host. - if self.time_url.as_ref().is_some_and(|u| !is_web(u) || advertised_len(u) > 4096) { + if self.time_url.as_ref().is_some_and(|u| !is_web(u) || rustgis_data::login_address::inner(u).len() > 4096) { self.time_url = None; } // A file per date: web addresses, finite dates, in order. - self.time_urls.retain(|(d, u)| d.is_finite() && is_web(u) && advertised_len(u) <= 4096); + self.time_urls.retain(|(d, u)| d.is_finite() && is_web(u) && rustgis_data::login_address::inner(u).len() <= 4096); // Sorted before the cap, so a list out of order keeps its first dates, not any. self.time_urls.sort_by(|a, b| a.0.total_cmp(&b.0)); self.time_urls.dedup_by(|a, b| a.0 == b.0); @@ -688,18 +679,6 @@ mod tests { 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()); - // The limit applies to the link as advertised, not to its longer encoded form. - let near = format!("https://maps.test/l?x={}", "a ".repeat((MAX_LEGEND_URL - 22) / 2)); - assert!(near.len() <= MAX_LEGEND_URL); - let url = rustgis_data::login_address::inherit(&document, &near); - assert!(rustgis_data::login_address::inner(&url).len() > MAX_LEGEND_URL); - assert_eq!(checked_legends([rustgis_data::ogc::Legend { url: url.clone(), ..Default::default() }]).len(), 1); - let mut layer = TileLayer::new("T", TileKind::Raster, "https://maps.test/{z}/{x}/{y}.png"); - layer.time_url = Some(url.clone()); - layer.time_urls = vec![(0.0, url)]; - assert!(layer.repair().is_ok()); - assert!(layer.time_url.is_some()); - assert_eq!(layer.time_urls.len(), 1); } #[test] From 54d913d6ce4d6d48b0e205710cd8ca121e810fe8 Mon Sep 17 00:00:00 2001 From: giswqs Date: Sun, 8 Nov 2026 11:28:18 -0500 Subject: [PATCH 6/6] fix(engine): report service legends a tile layer leaves out `service.add` dropped legend pictures whose addresses were too long for the tile layer without a word. A same-origin link can now grow past the limit once its spaces are encoded for the login, so count the legends left out and say so in one notice. This replaces measuring the limits on a discounted length, which also loosened them for plain addresses and disagreed with `check_url`. --- crates/engine/src/cmd/service.rs | 7 ++++++- crates/engine/src/tests.rs | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) 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.