mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-11-22 21:50:51 -05:00
fix: fail closed on replaced plugin audit state and partial discovery
This commit is contained in:
4 files changed
+67
-9
No files matched your search
@@ -2855,8 +2855,12 @@ bool audit_state_belongs_to_loaded_identity(const std::string& plugin_key, const
|
||||
const std::string& installation_revision)
|
||||
{
|
||||
const PluginBrowserProfileIdentityRef identity = current_plugin_browser_profile_identity(plugin_key);
|
||||
return identity && !identity->installation_revision.empty() && identity->plugin_root == plugin_root &&
|
||||
identity->installation_revision == installation_revision;
|
||||
if (!identity || identity->plugin_root != plugin_root)
|
||||
return false;
|
||||
// A sidecar with no revision is a legacy install, or one that was lost and is being seeded
|
||||
// again; it has no installation identity to compare against. A load writes the revision before
|
||||
// plugin code runs, so a real replacement always carries one and still mismatches here.
|
||||
return installation_revision.empty() || identity->installation_revision == installation_revision;
|
||||
}
|
||||
|
||||
// Records a grant in the plugin's sidecar so it is not asked again. Each persist helper re-reads
|
||||
@@ -3529,6 +3533,10 @@ int audit_plugin_event(PluginAuditManager& mgr, const std::string& event_name, A
|
||||
if (!have_install_state) {
|
||||
BOOST_LOG_TRIVIAL(warning) << __FUNCTION__ << " Refusing install state for a missing or replaced installation: "
|
||||
<< mgr.current_plugin();
|
||||
// Never decide with a sidecar that does not belong to this load instance: it may describe
|
||||
// a replacement installed by another process. With no usable state no saved grant applies,
|
||||
// so the operation is prompted for and persist_grant refuses to record it.
|
||||
state = PluginInstallState{};
|
||||
}
|
||||
|
||||
const std::string plugin_name = state.plugin_name.empty() ? mgr.current_plugin() : state.plugin_name;
|
||||
|
||||
@@ -592,7 +592,11 @@ void assign_discovered_plugin_key(PluginDescriptor& descriptor, const boost::fil
|
||||
descriptor.plugin_key = make_local_plugin_key(name_source.stem().string());
|
||||
}
|
||||
|
||||
void scan_plugin_directory(const std::string& dir_path, std::vector<PluginDescriptor>& out)
|
||||
// Fills `out` with the packages found directly under `dir_path`. A directory that cannot be
|
||||
// enumerated or whose packages cannot be inspected (for example the install-state lock is held by
|
||||
// another instance) sets `error` and leaves `out` partial: the caller must not merge such a list,
|
||||
// because the packages it is missing are present but unread.
|
||||
void scan_plugin_directory(const std::string& dir_path, std::vector<PluginDescriptor>& out, std::string& error)
|
||||
{
|
||||
namespace fs = boost::filesystem;
|
||||
|
||||
@@ -663,7 +667,11 @@ void scan_plugin_directory(const std::string& dir_path, std::vector<PluginDescri
|
||||
BOOST_LOG_TRIVIAL(info) << "Discovered plugin: " << out.back().name << " (version: " << out.back().version << ")";
|
||||
}
|
||||
} catch (const std::exception& ex) {
|
||||
error = "Error scanning directory " + dir_path + ": " + ex.what();
|
||||
BOOST_LOG_TRIVIAL(error) << "Error scanning directory " << dir_path << ": " << ex.what();
|
||||
} catch (...) {
|
||||
error = "Error scanning directory " + dir_path + ": unknown error";
|
||||
BOOST_LOG_TRIVIAL(error) << "Error scanning directory " << dir_path << ": unknown error";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -711,17 +719,30 @@ std::vector<PluginDescriptor> discover_plugin_packages(const std::vector<std::st
|
||||
try {
|
||||
BOOST_LOG_TRIVIAL(info) << "Scanning " << dirs.size() << " plugin directories...";
|
||||
|
||||
for (const std::string& dir : dirs)
|
||||
scan_plugin_directory(dir, discovered);
|
||||
for (const std::string& dir : dirs) {
|
||||
scan_plugin_directory(dir, discovered, error);
|
||||
// A directory that could not be scanned leaves `discovered` partial. Stop here so the
|
||||
// caller gets one clear error instead of a merged list that silently drops installed
|
||||
// plugins still present on disk.
|
||||
if (!error.empty())
|
||||
break;
|
||||
}
|
||||
|
||||
const auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start_time);
|
||||
BOOST_LOG_TRIVIAL(info) << "Plugin discovery completed in " << duration.count() << "ms. Found " << discovered.size()
|
||||
<< " plugin manifests";
|
||||
if (error.empty())
|
||||
BOOST_LOG_TRIVIAL(info) << "Plugin discovery completed in " << duration.count() << "ms. Found " << discovered.size()
|
||||
<< " plugin manifests";
|
||||
else
|
||||
BOOST_LOG_TRIVIAL(warning) << "Plugin discovery stopped after " << duration.count() << "ms with " << discovered.size()
|
||||
<< " plugin manifests; the result is partial: " << error;
|
||||
} catch (const std::exception& ex) {
|
||||
error = std::string("Plugin discovery failed: ") + ex.what();
|
||||
// A scan error already describes the first failure; keep it.
|
||||
if (error.empty())
|
||||
error = std::string("Plugin discovery failed: ") + ex.what();
|
||||
BOOST_LOG_TRIVIAL(error) << error;
|
||||
} catch (...) {
|
||||
error = "Plugin discovery failed: unknown error";
|
||||
if (error.empty())
|
||||
error = "Plugin discovery failed: unknown error";
|
||||
BOOST_LOG_TRIVIAL(error) << error;
|
||||
}
|
||||
|
||||
|
||||
@@ -116,6 +116,11 @@ std::vector<std::string> get_plugin_directories(const std::string& cloud_user_id
|
||||
// parsed comes back with metadata_valid == false and its error set (descriptor.is_invalid_package()).
|
||||
// The package-level auto-load flag is read from each package's .install_state.json into
|
||||
// descriptor.enabled. Capabilities are NOT discovered here — a package has none until it is loaded.
|
||||
//
|
||||
// `error` is non-empty when a directory could not be scanned (for example its packages could not be
|
||||
// inspected because the install-state lock is held by another instance); scanning then stops. The
|
||||
// returned list is partial in that case and must not be merged into an existing catalog, because the
|
||||
// packages it is missing are installed and still on disk.
|
||||
std::vector<PluginDescriptor> discover_plugin_packages(const std::vector<std::string>& dirs, std::string& error);
|
||||
|
||||
bool is_ignored_plugin_directory(const boost::filesystem::path& path);
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include <libslic3r/Utils.hpp>
|
||||
#include <libslic3r/libslic3r.h> // GCODEVIEWER_APP_KEY, SLIC3R_APP_KEY (via libslic3r_version.h)
|
||||
#include <slic3r/plugin/PluginAuditManager.hpp>
|
||||
#include <slic3r/plugin/PluginBrowserProfile.hpp>
|
||||
#include <slic3r/plugin/PluginDescriptor.hpp>
|
||||
#include <slic3r/plugin/PluginFsUtils.hpp>
|
||||
#include <slic3r/plugin/PluginManager.hpp>
|
||||
#include <slic3r/plugin/PythonInterpreter.hpp>
|
||||
@@ -1602,12 +1604,30 @@ struct AuditHarness
|
||||
std::ofstream((root / (key + ".py")).string(), std::ios::binary) << source;
|
||||
save_permissions(key, {});
|
||||
PluginManager::instance().discover_plugins(/*async=*/false, /*clear=*/true);
|
||||
bind_loaded_identity(key);
|
||||
}
|
||||
|
||||
// A real load binds the plugin key to its package root and sidecar revision before plugin code
|
||||
// runs; the audit gate trusts saved grants only for that installation.
|
||||
void bind_loaded_identity(const std::string& key)
|
||||
{
|
||||
PluginDescriptor descriptor;
|
||||
if (!PluginManager::instance().try_get_plugin_descriptor(key, descriptor) || descriptor.plugin_root.empty())
|
||||
return;
|
||||
PluginInstallState state;
|
||||
read_install_state(boost::filesystem::path(descriptor.plugin_root), state);
|
||||
begin_plugin_browser_profile(key, descriptor.plugin_root, state.installation_revision);
|
||||
}
|
||||
|
||||
// Grants as if saved in an earlier session.
|
||||
void save_permissions(const std::string& key, const PluginPermissions& permissions)
|
||||
{
|
||||
PluginInstallState state;
|
||||
PluginInstallState existing;
|
||||
if (read_install_state(data.plugins_dir() / key, existing)) {
|
||||
state.browser_profile_id = existing.browser_profile_id;
|
||||
state.installation_revision = existing.installation_revision;
|
||||
}
|
||||
state.installed_from = "local";
|
||||
state.installed_version = "1.0";
|
||||
state.plugin_name = key;
|
||||
@@ -3231,6 +3251,10 @@ TEST_CASE("A permission mutation changes only the named entries and preserves th
|
||||
initial.permissions.fs_create = {"/kept/create", "/removed/create"};
|
||||
initial.permissions.fs_delete = {"/kept/delete", "/removed/delete"};
|
||||
initial.permissions.network_http = {"https://kept.example"};
|
||||
PluginInstallState existing;
|
||||
REQUIRE(read_install_state(plugin_dir, existing));
|
||||
initial.browser_profile_id = existing.browser_profile_id;
|
||||
initial.installation_revision = existing.installation_revision;
|
||||
REQUIRE(write_install_state(plugin_dir, initial));
|
||||
|
||||
PluginPermissionMutation mutation;
|
||||
|
||||
Reference in new issue
Block a user