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
27 changes: 23 additions & 4 deletions crates/engine/src/cmd/plugin_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,33 @@ pub fn checked(url: &str) -> Result<String, String> {
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<address::Reach, String> {

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.

The doc says "the widest reach", but for a plugin this returns Reach::Public, which is the narrowest value in Reach's ordering (Public < Local < Forbidden, and callers use .min()). The meaning is right, but the wording reads backwards for anyone who knows the ordering. Suggest saying "the most a caller may reach" or "held to the internet".

The catalog_search_read comment in service.rs (~line 1898) has a similar issue: it says the catalogue address is "unless a document read from the internet or a plugin named it". For a plugin, named_reach doesn't consult any naming. It refuses local addresses and holds internet ones to Public.

Category: quality

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: "the widest reach" becomes "how far discovery may reach from", and the catalogue comment says a plugin's address is held to the internet rather than "named".

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<Value, String> {
Expand Down
25 changes: 14 additions & 11 deletions crates/engine/src/cmd/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ fn read_documents_with(
fetch: FetchDocument<'_>,
) -> Result<Discovery, String> {
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");
Expand Down Expand Up @@ -289,22 +293,20 @@ 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

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.

This is the root of a possible side effect that is new with this change. Under a plugin, named is now Public for every internet address, where it was Local (unrecorded) before. So ceiling == Reach::Public now holds for plugin discovery of URLs the user never named. At line ~340 that makes the branch call cache::restrict_to_public(&address) for plugin-supplied text or left reads. restrict_to_public writes to a process-wide table (crates/tiles/src/cache.rs) that persists for later reads of the same URL by the user. That table is capped at 16,384 exact entries. Once full, it sets saturated, and every unknown URL, including the user's own LAN services, fails closed to Public. A plugin that issues enough service.add calls with distinct internet addresses could therefore degrade the user's later local-network discovery without any notice.

I have not confirmed this is reachable in practice, and the restriction may be the intended outcome for plugin-named internet hosts. Worth checking: either scope the restriction to what the plugin's own fetches need, or add a test that a plugin's discovery leaves a later, unrelated user read unchanged.

Category: bug

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 doesn't add a new kind of entry. Every discovery read that connects on the internet already records its address, for any caller, plugins included: service.rs:375 calls restrict_to_public on every fetched document observed Public. So a plugin calling service.layers on many distinct internet addresses filled that table before this change too.

What this change adds is the same entry for a plugin's supplied-text and off-frame (left) reads of an internet root. That entry is how the hold reaches the app's off-frame read, which knows a document only by its address (service.rs:340). Dropping it would let the app read a plugin-named address that resolves locally. A full table fails closed, narrowing to the internet and never widening. That is the cache's existing limit for every document-derived address (MAX_PUBLIC_ONLY), shared by all callers, and this PR doesn't change it.

No change for this one.

} else {
reaches.get(crate::render::rustgis_tiles::logins::inner(&parent)).copied().unwrap_or(Reach::Public)
};
let ceiling = inherited.min(cache::ceiling_for(&address));
// 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,
Expand Down Expand Up @@ -1893,9 +1895,10 @@ pub(super) fn catalog_search_read(s: &mut Session, p: &Value, read: Option<Reach
let url = url_of(p)?;
let request = records_request(s, p)?;
// Where the answer was read: natively, where the fetch connected; otherwise what the app's
// fetch recorded of it ([`cache::ceiling_for`]). The catalogue's own address is the user's
// choice, unless a document read from the internet named it.
let within = cache::ceiling_for(url).min(cache::ceiling_for(&request));
// fetch recorded of it ([`cache::ceiling_for`]). The catalogue's own address is the caller's
// choice, unless a document read from the internet or a plugin named it
// ([`Session::named_reach`]).
let within = s.named_reach(url)?.min(cache::ceiling_for(&request));
// An answer supplied by anything but the app says nothing trustworthy about where it was read.
let within = match read {
Some(read) => within.min(read),
Expand Down
13 changes: 13 additions & 0 deletions crates/engine/src/policy/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
78 changes: 78 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"<csw:GetRecordsResponse xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" xmlns:dc="http://purl.org/dc/elements/1.1/"><csw:SearchResults numberOfRecordsMatched="1"><csw:Record><dc:identifier>a</dc:identifier><dc:title>A</dc:title><dc:URI protocol="OGC:WMS">{catalogue}/wms</dc:URI></csw:Record></csw:SearchResults></csw:GetRecordsResponse>"#
);
(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 [

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.

The list covers service.add, service.layers, service.coverage, service.groupPlan, service.import and catalog.search, but not service.refresh. The PR description and docs/plugins.md both name it. It probably goes through the same read_documents root, but a one-line case here would pin it, since the rule is stated for it in the docs.

Category: quality

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. service.refresh reaches the same root (refresh → read → listing → capabilities → read_documents_with, which calls named_reach). The extra case needs a group imported first, so it waits for that pass.

("service.add", with(json!({"layer": "parcels"})), local),

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.

The case list covers service.add, service.layers, service.coverage, service.groupPlan, service.import and catalog.search, but not service.refresh. The PR description and docs/plugins.md both name it. It probably goes through the same read_documents root, but one more case here would pin it, since the docs state the rule for it.

Category: quality

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. service.refresh reaches the same root (refresh → read → listing → capabilities → read_documents_with, which calls named_reach). The extra case needs a group imported first, so it waits for that pass.

("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]
Expand Down
5 changes: 5 additions & 0 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

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.

Expand Down
Loading