diff --git a/crates/engine/src/cmd/plugin_fetch.rs b/crates/engine/src/cmd/plugin_fetch.rs index b15422068f..fddd8989a9 100644 --- a/crates/engine/src/cmd/plugin_fetch.rs +++ b/crates/engine/src/cmd/plugin_fetch.rs @@ -163,14 +163,33 @@ pub fn checked(url: &str) -> Result { let url = url.split('#').next().unwrap_or(url); address::check(url, None)?; if address::reach(url) != address::Reach::Public && !allow_local() { - return Err(crate::sources::message_with_address( - url, - "plugins fetch only from the internet, not this computer or the local network", - )); + return Err(crate::sources::message_with_address(url, INTERNET_ONLY)); } Ok(url.to_string()) } +/// Why a plugin can't reach an address: plugins reach only the internet. +const INTERNET_ONLY: &str = "plugins fetch only from the internet, not this computer or the local network"; + +impl Session { + /// The widest reach a service or catalogue the caller names (`url`, the address discovery + /// starts from) is read within. A plugin reaches only the internet, as its own `host.fetch` + /// does: under its authority an address on this computer or the local network is refused, + /// and one on the internet starts discovery within the internet, so the documents read from + /// it, the text the plugin supplies for it and everything they name are held there too. Any + /// other caller's address is its own choice, held only by what the app recorded of it. + pub(crate) fn named_reach(&self, url: &str) -> Result { + if !self.policy_plugin() { + return Ok(crate::render::rustgis_tiles::cache::ceiling_for(url)); + } + match address::check_reach(url, address::Reach::Public) { + Ok(()) => Ok(address::Reach::Public), + Err(e) if address::is_refusal(&e) => Err(crate::sources::message_with_address(url, INTERNET_ONLY)), + Err(e) => Err(e), + } + } +} + /// The answer for a fetched document: `{url, text}`, and `json` when the text is a JSON object /// or array. pub fn answer(url: &str, bytes: &[u8]) -> Result { diff --git a/crates/engine/src/cmd/service.rs b/crates/engine/src/cmd/service.rs index 8c555627ab..d16cf33337 100644 --- a/crates/engine/src/cmd/service.rs +++ b/crates/engine/src/cmd/service.rs @@ -262,6 +262,10 @@ fn read_documents_with( fetch: FetchDocument<'_>, ) -> Result { let original = url_of(p)?; + // The service's own address is the caller's choice, unless a document read from the internet + // named it (a catalogue's record: [`cache::ceiling_for`] holds it there) or a plugin did + // (plugins reach only the internet: [`Session::named_reach`]). + let named = s.named_reach(original)?; let mut address = crate::render::rustgis_tiles::logins::inherit(original, &rustgis_data::ogc::capabilities_url(original, ps(p, "type"))); let supplied = ps(p, "text"); @@ -289,14 +293,12 @@ fn read_documents_with( loop { rustgis_data::progress::check().map_err(|e| e.to_string())?; let crossed_origin = histories.get(&parent).copied().unwrap_or(false) || crosses_origin(&parent, &address); - // The service's own address is the user's choice, unless a document read from the - // internet named it (a catalogue's record: [`cache::ceiling_for`] holds it there); a linked - // one inherits its document's reach (a document not recorded, which can't happen, reaches - // only the internet). Either is judged by that reach, never by how the document's address - // is spelled: a public-looking name may connect on the local network, and its local links - // are then the service's own. + // The service's own address is read within `named`; a linked one inherits its document's + // reach (a document not recorded, which can't happen, reaches only the internet). Either is + // judged by that reach, never by how the document's address is spelled: a public-looking + // name may connect on the local network, and its local links are then the service's own. let inherited = if seen.is_empty() { - cache::ceiling_for(original) + named } else { reaches.get(crate::render::rustgis_tiles::logins::inner(&parent)).copied().unwrap_or(Reach::Public) }; @@ -304,7 +306,7 @@ fn read_documents_with( // A document the app supplies was recorded by its fetch if it came from the internet; one // supplied by anything else (the Assistant, MCP, a plugin) says nothing trustworthy about // where it was read, so what it names is taken as named on the internet. Its address - // itself stays the caller's own choice. + // itself is the caller's choice, as `named` holds it. let read_within = if seen.is_empty() && supplied.is_some() && !s.policy_user() { ceiling.min(Reach::Public) } else { ceiling }; let rejected = match crate::render::rustgis_tiles::address::check_reach(&address, ceiling) { Ok(()) => false, @@ -1893,9 +1895,10 @@ pub(super) fn catalog_search_read(s: &mut Session, p: &Value, read: Option within.min(read), diff --git a/crates/engine/src/policy/dispatch.rs b/crates/engine/src/policy/dispatch.rs index 6d0c433689..344d3b275c 100644 --- a/crates/engine/src/policy/dispatch.rs +++ b/crates/engine/src/policy/dispatch.rs @@ -336,6 +336,19 @@ impl Session { pub(crate) fn policy_user(&self) -> bool { self.policy.scopes.last().is_none_or(|s| matches!(s.kind, Kind::User)) } + /// Whether a plugin's authority is in the active scope: one of its commands, or what its + /// session program asks between them, through any item it runs. + pub(crate) fn policy_plugin(&self) -> bool { + /// Looks through item narrowing for a plugin. + fn plugin(kind: &Kind) -> bool { + match kind { + Kind::Plugin { .. } => true, + Kind::Item { within, .. } => plugin(within), + _ => false, + } + } + self.policy.scopes.last().is_some_and(|s| plugin(&s.kind)) + } /// Whether the active scope may exercise reserved connection/account authority. pub(crate) fn policy_allows_reserved(&self) -> bool { match &self.scope().kind { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index c773e714ce..921504a44c 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -20377,6 +20377,84 @@ fn plugins_fetch_documents_with_network() { rustgis_plugins::unload("eng-fetch"); } +/// A service's collections at `local`, on this computer, whose one layer is there too (nothing +/// listens on port 9, so a read that is tried fails at once), and a catalogue's answer for a +/// catalogue on the local network linking to a service there: `(listing, search)`, each with its +/// document given as `text`. +fn local_service_and_catalogue(local: &str, catalogue: &str) -> (Value, Value) { + let items = format!("{local}/parcels/items"); + let collections = + json!({"collections":[{"id":"parcels","title":"Parcels","links":[{"rel":"items","type":"application/geo+json","href":items}]}]}); + let record = format!( + r#"aA{catalogue}/wms"# + ); + (json!({"url": local, "type": "ogcapi", "text": collections.to_string()}), json!({"url": catalogue, "text": record})) +} + +/// Plugins reach only the internet, and so does the service discovery one starts: a service or +/// catalogue address on this computer or the local network is refused for a plugin, naming it and +/// why, by every command that discovers a service or searches a catalogue, and nothing is added. +#[test] +fn a_plugin_cannot_discover_a_service_off_the_internet() { + let mut s = world(); + let id = "eng-service-local"; + plugin(id, &["network", "edit"]); + let (local, catalogue) = ("http://127.0.0.1:9/ogc/collections", "http://192.168.21.4/csw"); + let (listing, search) = local_service_and_catalogue(local, catalogue); + let with = |extra: Value| { + let mut p = listing.clone(); + if let (Some(p), Some(extra)) = (p.as_object_mut(), extra.as_object()) { + p.extend(extra.clone()); + } + p + }; + for (op, p, url) in [ + ("service.add", with(json!({"layer": "parcels"})), local), + ("service.add", json!({"url": "http://127.0.0.1:9/wms", "type": "wms", "layer": "roads"}), "http://127.0.0.1:9/wms"), + ("service.layers", listing.clone(), local), + ("service.coverage", with(json!({"layer": "parcels"})), local), + ("service.groupPlan", listing.clone(), local), + ("service.import", with(json!({"name": "Parcels"})), local), + ("catalog.search", search.clone(), catalogue), + ] { + let e = forward(&mut s, id, op, p).unwrap_err(); + assert!(e.contains(url) && e.contains("plugins fetch only from the internet, not this computer or the local network"), "{op}: {e}"); + } + assert!(s.map().layers.iter().all(|l| l.name != "Parcels" && l.name != "roads"), "nothing was added"); + rustgis_plugins::unload(id); +} + +/// The user's own service and catalogue on this computer and the local network are read as ever: +/// only a plugin's discovery is held to the internet. +#[test] +fn the_users_own_local_service_is_still_discovered() { + let mut s = world(); + let (listing, search) = local_service_and_catalogue("http://127.0.0.1:9/ogc/collections", "http://192.168.21.4/csw"); + let own = s.run("service.layers", &listing).unwrap(); + assert_eq!(own["layers"].as_array().map_or(0, Vec::len), 1, "{own}"); + let own = s.run("catalog.search", &search).unwrap(); + assert_eq!(own["records"][0]["links"].as_array().map_or(0, Vec::len), 1, "{own}"); +} + +/// A document a plugin supplies for a service on the internet has what it names held there: its +/// link into the local network is refused, naming it, as is a catalogue answer's. +#[test] +fn a_document_a_plugin_supplies_names_only_the_internet() { + let mut s = world(); + let id = "eng-service-text"; + plugin(id, &["network"]); + let linked = "http://10.0.21.5/collections"; + let landing = json!({"links":[{"rel":"data","type":"application/json","href":linked}]}).to_string(); + let p = json!({"url": "https://gis-21.corp-looks-public.example/ogc", "type": "ogcapi", "text": landing}); + let e = forward(&mut s, id, "service.layers", p).unwrap_err(); + assert!(e.contains(linked) && e.contains("local network"), "{e}"); + let (_, search) = local_service_and_catalogue("http://127.0.0.1:9/ogc/collections", "http://192.168.21.4/csw"); + let search = json!({"url": "https://csw-21.corp-looks-public.example/csw", "text": search["text"]}); + let out = forward(&mut s, id, "catalog.search", search).unwrap(); + assert_eq!(out["records"][0]["links"].as_array().map_or(0, Vec::len), 0, "{out}"); + rustgis_plugins::unload(id); +} + /// Where nothing may wait (the browser), `host.fetch` answers pending: the command is undone, /// quietly, and runs again once the answer is in, when the cache answers. #[test] diff --git a/docs/plugins.md b/docs/plugins.md index 742ea9d60f..85a42bb4fe 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -760,6 +760,11 @@ that needs it left out. address taken from the one before, runs once per step, up to 64 times. The browser follows redirects itself, and the server must allow requests from web pages (CORS). +Service discovery a plugin starts follows the same rule. The service or catalogue address it gives +`service.add`, `service.layers`, `service.coverage`, `service.groupPlan`, `service.import`, +`service.refresh` or `catalog.search` must be on the internet, and the documents read from it, +any `text` the plugin supplies and everything they name are held to the internet too. + Use `host::call("host.fetch", json!({"url": url}))` in the Rust SDK, or `host::call("host.fetch", json!({"url": url, "json": false}))` for the text only.