mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 20:31:02 +01:00
Plug-ins: removing an installed plug-in whose module can't be deleted fails
plugin.remove now deletes an installed plug-in's stored module along with
its grant, before its settings and cache are deleted. When the module file
can't be deleted, the command returns "can't delete <file>", the settings
and cache are moved back, the plug-in stays installed, and the grant the
signed grants file held for it is written back, so the stored module
still loads at the next launch. Before, the command returned {removed}
and left the module in the plug-in folder.
This commit is contained in:
1 parent
ee947082ed
commit
232c17b25a
5 files changed
+68
-6
No files matched your search
@@ -1252,18 +1252,31 @@ fn remove(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// A module stored for an id that isn't installed (one no grant covers is skipped at startup)
|
||||
// counts as removed once its file is gone.
|
||||
let module = !installed && vectorcraft_plugins::manifest::valid_id(id) && s.holds_stored_plugin(id);
|
||||
// An installed plug-in's stored module is deleted with its grant too, so that one that can't be
|
||||
// deleted leaves the plug-in installed. Without a plug-in folder it has none.
|
||||
let unstore = module || (installed && s.plugin_dir.is_some());
|
||||
// The grant the store keeps for an installed plug-in, which may differ from the one the session
|
||||
// holds (an update whose module couldn't be stored): written back when its module can't be
|
||||
// deleted, so that the stored module still loads at the next launch.
|
||||
let kept_grant = if installed { s.stored_plugin_grant(id) } else { None };
|
||||
let module_left = std::cell::Cell::new(false);
|
||||
let cant_delete = || format!("plugin.remove: can't delete {}", s.stored_plugin_file(id).unwrap_or_default());
|
||||
// Installed again, it asks again. A grant that can't be removed may still approve the module, and
|
||||
// so may a grants file that can't be opened or read, or a signed copy of the file put back when
|
||||
// the generation couldn't be raised (the key store can't be reached). Without a plug-in folder
|
||||
// nothing keeps a grant. The stored module of an id that isn't installed is deleted next, while
|
||||
// its settings and cache can still be moved back.
|
||||
// nothing keeps a grant. The stored module is deleted next, while its settings and cache can
|
||||
// still be moved back.
|
||||
let forget = || {
|
||||
if !s.store_grant_in(&turn, id, None) && s.plugin_dir.is_some() {
|
||||
return false;
|
||||
}
|
||||
let deleted = !module || s.unstore_plugin(&turn, id);
|
||||
let deleted = !unstore || s.unstore_plugin(&turn, id);
|
||||
if !deleted
|
||||
&& let Some(grant) = kept_grant.clone()
|
||||
&& !s.store_grant_in(&turn, id, Some(grant))
|
||||
{
|
||||
log::warn!("plugin.remove: can't keep the grant of {id}, whose module stays: it asks again at the next launch");
|
||||
}
|
||||
module_left.set(!deleted);
|
||||
deleted
|
||||
};
|
||||
@@ -1366,7 +1379,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Remove Plug-in",
|
||||
[],
|
||||
None,
|
||||
"{id} uninstall a plug-in (its live effects then leave the geometry as it is) and delete the settings and cache it kept (when their folder can't be moved aside, or its grant can't be removed from the plug-in store, the command fails and the plug-in stays installed, as it does when the system key store can't be reached to make sure no earlier copy of grants.json grants it again; when the settings can't be moved back either, the error names the folder they stay in, and the next load, install or removal of the plug-in moves them back); for an id that isn't installed, deletes the settings and cache the plug-in store still keeps for it and the module stored for it that didn't load, and reports it removed; it waits while another window or process changes the plug-in store, and when the store's disabled.lock can't be locked, the command fails and nothing is removed → {removed}",
|
||||
"{id} uninstall a plug-in (its live effects then leave the geometry as it is) and delete the settings and cache it kept (when their folder can't be moved aside, or its grant can't be removed from the plug-in store, or its stored module can't be deleted, the command fails and the plug-in stays installed, as it does when the system key store can't be reached to make sure no earlier copy of grants.json grants it again; when the settings can't be moved back either, the error names the folder they stay in, and the next load, install or removal of the plug-in moves them back); for an id that isn't installed, deletes the settings and cache the plug-in store still keeps for it and the module stored for it that didn't load, and reports it removed; it waits while another window or process changes the plug-in store, and when the store's disabled.lock can't be locked, the command fails and nothing is removed → {removed}",
|
||||
always,
|
||||
remove
|
||||
),
|
||||
|
||||
@@ -876,6 +876,13 @@ impl Session {
|
||||
})
|
||||
}
|
||||
|
||||
/// The grant the attached plug-in store's signed grants file holds for the plug-in `id` (`None`
|
||||
/// without a folder, and when the file grants nothing: unsigned, unreadable or without an entry
|
||||
/// for `id`).
|
||||
pub(crate) fn stored_plugin_grant(&self, id: &str) -> Option<vectorcraft_plugins::permissions::Grant> {
|
||||
self.plugin_dir.as_deref().and_then(|dir| stored_grants(dir).remove(id))
|
||||
}
|
||||
|
||||
/// Does the attached plug-in store hold a module file for the plug-in `id`, loaded or not (one
|
||||
/// that no grant covers is skipped at startup and stays in the folder)?
|
||||
pub(crate) fn holds_stored_plugin(&self, id: &str) -> bool {
|
||||
|
||||
@@ -4789,6 +4789,45 @@ fn removing_a_stored_plugin_that_did_not_load_reports_it_removed() {
|
||||
assert_eq!(kept_generation(&keys, &folder.0), Some(2), "a valid id's removal still retires signed copies");
|
||||
}
|
||||
|
||||
/// Removing an installed plug-in whose stored module can't be deleted fails, and the plug-in stays
|
||||
/// installed with its settings and its stored grant; a second removal, once the module can be
|
||||
/// deleted, removes it.
|
||||
#[test]
|
||||
fn removing_an_installed_plugin_whose_module_cant_be_deleted_keeps_it_installed() {
|
||||
let keys = std::sync::Arc::new(crate::store::grant_keys::MemoryGrantKeys::with_key(crate::store::grant_keys::GrantKey::from_bytes([5; 32])));
|
||||
let _keys = ThreadKeys::using(SharedKeys::new(&keys));
|
||||
let folder = crate::tests_links::Folder::new("plugin-store-installed-remove-held");
|
||||
let id = "test.engine.permissions.installedremoveheld";
|
||||
let data = vectorcraft_format::base64_encode(&asking(id, r#"["doc.read"]"#, &json!({"objects": []})));
|
||||
let module = folder.0.join(format!("{}.wasm", crate::store::file_stem(id)));
|
||||
let mut s = session();
|
||||
s.attach_plugin_store(folder.0.clone());
|
||||
run(&mut s, "plugin.install", json!({"dataBase64": data, "grant": ["doc.read"]}));
|
||||
assert!(module.is_file(), "the module is stored");
|
||||
assert!(s.holds_plugin_grant(id), "and its grant");
|
||||
let stored_grant = s.stored_plugin_grant(id);
|
||||
assert!(stored_grant.is_some(), "in the signed grants file");
|
||||
let data_dir = crate::store::plugin_data_dir(&folder.0, id);
|
||||
std::fs::create_dir_all(&data_dir).unwrap();
|
||||
let setting = data_dir.join("settings.json");
|
||||
std::fs::write(&setting, b"{}").unwrap();
|
||||
|
||||
// A folder in the module's place can't be deleted as a file.
|
||||
std::fs::remove_file(&module).unwrap();
|
||||
std::fs::create_dir_all(module.join("held")).unwrap();
|
||||
let refused = s.execute("plugin.remove", &json!({"id": id})).unwrap_err().to_string();
|
||||
assert!(refused.contains("can't delete"), "{refused}");
|
||||
assert!(vectorcraft_plugins::registry::get(id).is_some(), "the plug-in stays installed");
|
||||
assert!(module.exists());
|
||||
assert!(s.stored_plugin_grant(id) == stored_grant, "the stored grant is kept for the next launch");
|
||||
assert_eq!(std::fs::read(&setting).ok().as_deref(), Some(&b"{}"[..]), "the settings are moved back");
|
||||
|
||||
std::fs::remove_dir_all(&module).unwrap();
|
||||
assert_eq!(s.execute("plugin.remove", &json!({"id": id})).ok(), Some(json!({"removed": id})), "a second removal removes it");
|
||||
assert!(vectorcraft_plugins::registry::get(id).is_none() && !module.exists() && !data_dir.exists());
|
||||
assert!(!s.holds_plugin_grant(id), "and its grant");
|
||||
}
|
||||
|
||||
/// A MAC is made over the generation, the grants and the folder, and only the whole MAC of the same
|
||||
/// key, generation, grants and folder verifies. A key's bytes are never printed.
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user