mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-11-25 23:01:09 -05:00
Merge branch 'main' into fix/plugin-network-permissions
This commit is contained in:
74 files changed
+2064
-785
No files matched your search
@@ -49,8 +49,7 @@ thread_local std::string g_active_plugin_key;
|
||||
std::mutex g_registry_mutex;
|
||||
std::unordered_map<std::string, std::vector<py::object>> g_pending_capabilities;
|
||||
std::unordered_map<std::string, py::object> g_pending_package;
|
||||
std::unordered_map<std::string, std::vector<std::string>> g_pending_fs_read_permissions;
|
||||
std::unordered_map<std::string, std::vector<std::string>> g_pending_network_permissions;
|
||||
std::unordered_map<std::string, PluginPermissions> g_pending_permissions;
|
||||
struct PluginInstanceHandle
|
||||
{
|
||||
// The C++ plugin interface points into a Python object. Keep both alive through one
|
||||
@@ -89,8 +88,7 @@ void discard_pending_capture_without_python(const std::string& plugin_key)
|
||||
g_pending_package.erase(package);
|
||||
}
|
||||
|
||||
g_pending_fs_read_permissions.erase(plugin_key);
|
||||
g_pending_network_permissions.erase(plugin_key);
|
||||
g_pending_permissions.erase(plugin_key);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -115,8 +113,7 @@ void PythonPluginBridge::begin_plugin_capture(const std::string& plugin_key)
|
||||
// for this same entry path.
|
||||
g_pending_capabilities.erase(plugin_key);
|
||||
g_pending_package.erase(plugin_key);
|
||||
g_pending_fs_read_permissions.erase(plugin_key);
|
||||
g_pending_network_permissions.erase(plugin_key);
|
||||
g_pending_permissions.erase(plugin_key);
|
||||
}
|
||||
// From now until finalize/cancel, @orca.plugin and register_capability() calls made by
|
||||
// Python code on this thread are attributed to this plugin.
|
||||
@@ -147,8 +144,7 @@ std::vector<CapturedCapability> PythonPluginBridge::finalize_plugin_capture(cons
|
||||
std::lock_guard<std::mutex> lock(g_registry_mutex);
|
||||
g_pending_capabilities.erase(capture_key);
|
||||
g_pending_package.erase(capture_key);
|
||||
g_pending_fs_read_permissions.erase(capture_key);
|
||||
g_pending_network_permissions.erase(capture_key);
|
||||
g_pending_permissions.erase(capture_key);
|
||||
};
|
||||
|
||||
try {
|
||||
@@ -181,47 +177,27 @@ std::vector<CapturedCapability> PythonPluginBridge::finalize_plugin_capture(cons
|
||||
// Permission declarations are collected while register_capabilities() runs so the
|
||||
// plugin can describe its needs without touching wx from the Python load worker. Ask
|
||||
// only after registration returns, before capability instances are materialized.
|
||||
std::vector<std::string> fs_read_permissions;
|
||||
PluginPermissions requested_permissions;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_registry_mutex);
|
||||
auto it = g_pending_fs_read_permissions.find(capture_key);
|
||||
if (it != g_pending_fs_read_permissions.end()) {
|
||||
fs_read_permissions = std::move(it->second);
|
||||
g_pending_fs_read_permissions.erase(it);
|
||||
auto it = g_pending_permissions.find(capture_key);
|
||||
if (it != g_pending_permissions.end()) {
|
||||
requested_permissions = std::move(it->second);
|
||||
g_pending_permissions.erase(it);
|
||||
}
|
||||
}
|
||||
if (!fs_read_permissions.empty()) {
|
||||
const bool has_permission_request =
|
||||
!requested_permissions.fs_read.empty() || !requested_permissions.fs_readwrite.empty() ||
|
||||
!requested_permissions.network_http.empty() || !requested_permissions.network_socket.empty() ||
|
||||
!requested_permissions.process.empty() || !requested_permissions.threading.empty();
|
||||
if (has_permission_request) {
|
||||
bool granted = false;
|
||||
{
|
||||
py::gil_scoped_release release;
|
||||
granted = PluginAuditManager::instance().request_filesystem_read_permissions(plugin_key, fs_read_permissions);
|
||||
granted = PluginAuditManager::instance().request_permissions(plugin_key, requested_permissions);
|
||||
}
|
||||
if (!granted) {
|
||||
error = _u8L("Plugin filesystem read permission request denied");
|
||||
BOOST_LOG_TRIVIAL(warning) << error << " for key " << plugin_key;
|
||||
discard_pending_for_key();
|
||||
clear_active_key();
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> network_permissions;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_registry_mutex);
|
||||
auto it = g_pending_network_permissions.find(capture_key);
|
||||
if (it != g_pending_network_permissions.end()) {
|
||||
network_permissions = std::move(it->second);
|
||||
g_pending_network_permissions.erase(it);
|
||||
}
|
||||
}
|
||||
if (!network_permissions.empty()) {
|
||||
bool granted = false;
|
||||
{
|
||||
py::gil_scoped_release release;
|
||||
granted = PluginAuditManager::instance().request_network_permissions(plugin_key, network_permissions);
|
||||
}
|
||||
if (!granted) {
|
||||
error = _u8L("Plugin network permission request denied");
|
||||
error = _u8L("Plugin permission request denied");
|
||||
BOOST_LOG_TRIVIAL(warning) << error << " for key " << plugin_key;
|
||||
discard_pending_for_key();
|
||||
clear_active_key();
|
||||
@@ -350,8 +326,7 @@ void PythonPluginBridge::cancel_plugin_capture(const std::string& plugin_key)
|
||||
// may already have registered under this key.
|
||||
g_pending_capabilities.erase(plugin_key);
|
||||
g_pending_package.erase(plugin_key);
|
||||
g_pending_fs_read_permissions.erase(plugin_key);
|
||||
g_pending_network_permissions.erase(plugin_key);
|
||||
g_pending_permissions.erase(plugin_key);
|
||||
}
|
||||
|
||||
if (g_active_plugin_key == plugin_key)
|
||||
@@ -378,8 +353,7 @@ void PythonPluginBridge::clear_pending_captures()
|
||||
(void) pkg.release();
|
||||
}
|
||||
g_pending_package.clear();
|
||||
g_pending_fs_read_permissions.clear();
|
||||
g_pending_network_permissions.clear();
|
||||
g_pending_permissions.clear();
|
||||
g_active_plugin_key.clear();
|
||||
return;
|
||||
}
|
||||
@@ -388,8 +362,7 @@ void PythonPluginBridge::clear_pending_captures()
|
||||
BOOST_LOG_TRIVIAL(info) << "Clearing " << g_pending_capabilities.size() << " pending Python plugin capture(s)";
|
||||
g_pending_capabilities.clear();
|
||||
g_pending_package.clear();
|
||||
g_pending_fs_read_permissions.clear();
|
||||
g_pending_network_permissions.clear();
|
||||
g_pending_permissions.clear();
|
||||
g_active_plugin_key.clear();
|
||||
}
|
||||
|
||||
@@ -589,12 +562,33 @@ void bind_python_api(pybind11::module_& m)
|
||||
|
||||
m.def(
|
||||
"request_permissions",
|
||||
[](const std::vector<std::string>& fs_read, const std::vector<std::string>& network) {
|
||||
[](const std::vector<std::string>& fs_read,
|
||||
const std::vector<std::string>& fs_readwrite,
|
||||
const std::vector<std::string>& network_http,
|
||||
const std::vector<std::string>& network_socket,
|
||||
const std::vector<std::string>& process,
|
||||
bool threading,
|
||||
const std::vector<std::string>& network) {
|
||||
if (g_active_plugin_key.empty())
|
||||
throw py::value_error("request_permissions() called outside plugin discovery context");
|
||||
|
||||
// Validate and canonicalize everything before touching the pending registry, so a
|
||||
// partially invalid call cannot leave a half-recorded request behind.
|
||||
const auto reject_empty = [](const char* name, const std::vector<std::string>& targets) {
|
||||
for (const std::string& target : targets)
|
||||
if (target.empty())
|
||||
throw py::value_error(std::string("request_permissions(") + name +
|
||||
"=...) does not accept empty values");
|
||||
};
|
||||
reject_empty("fs_read", fs_read);
|
||||
reject_empty("fs_readwrite", fs_readwrite);
|
||||
reject_empty("network_http", network_http);
|
||||
reject_empty("network_socket", network_socket);
|
||||
reject_empty("process", process);
|
||||
|
||||
// A network URL is reduced to scheme://host, so one grant covers every path, query
|
||||
// and port on that host. It is recorded as an HTTP grant; network_permission_matches
|
||||
// applies that grant to raw socket events for the same host too.
|
||||
std::vector<std::string> normalized_network;
|
||||
normalized_network.reserve(network.size());
|
||||
for (const std::string& url : network) {
|
||||
@@ -602,37 +596,49 @@ void bind_python_api(pybind11::module_& m)
|
||||
if (normalized.empty())
|
||||
throw py::value_error(
|
||||
"request_permissions(network=...) requires an http:// or https:// URL with a host");
|
||||
if (std::find(normalized_network.begin(), normalized_network.end(), normalized) == normalized_network.end())
|
||||
if (std::find(normalized_network.begin(), normalized_network.end(), normalized) ==
|
||||
normalized_network.end())
|
||||
normalized_network.push_back(normalized);
|
||||
}
|
||||
|
||||
for (const std::string& path : fs_read) {
|
||||
if (path.empty())
|
||||
throw py::value_error("request_permissions(fs_read=...) does not accept empty paths");
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(g_registry_mutex);
|
||||
auto& requested_fs = g_pending_fs_read_permissions[g_active_plugin_key];
|
||||
for (const std::string& path : fs_read) {
|
||||
if (std::find(requested_fs.begin(), requested_fs.end(), path) == requested_fs.end())
|
||||
requested_fs.push_back(path);
|
||||
}
|
||||
PluginPermissions& requested = g_pending_permissions[g_active_plugin_key];
|
||||
|
||||
if (!normalized_network.empty()) {
|
||||
auto& requested_network = g_pending_network_permissions[g_active_plugin_key];
|
||||
for (const std::string& entry : normalized_network) {
|
||||
if (std::find(requested_network.begin(), requested_network.end(), entry) == requested_network.end())
|
||||
requested_network.push_back(entry);
|
||||
}
|
||||
}
|
||||
const auto add_targets = [](std::vector<std::string>& bucket, const std::vector<std::string>& targets) {
|
||||
for (const std::string& target : targets)
|
||||
if (std::find(bucket.begin(), bucket.end(), target) == bucket.end())
|
||||
bucket.push_back(target);
|
||||
};
|
||||
|
||||
add_targets(requested.fs_read, fs_read);
|
||||
add_targets(requested.fs_readwrite, fs_readwrite);
|
||||
add_targets(requested.network_http, network_http);
|
||||
add_targets(requested.network_http, normalized_network);
|
||||
add_targets(requested.network_socket, network_socket);
|
||||
add_targets(requested.process, process);
|
||||
|
||||
if (threading && std::find(requested.threading.begin(), requested.threading.end(),
|
||||
kThreadPermissionTarget) == requested.threading.end())
|
||||
requested.threading.emplace_back(kThreadPermissionTarget);
|
||||
},
|
||||
py::arg("fs_read") = std::vector<std::string>{}, py::arg("network") = std::vector<std::string>{},
|
||||
py::arg("fs_read") = std::vector<std::string>{},
|
||||
py::arg("fs_readwrite") = std::vector<std::string>{},
|
||||
py::arg("network_http") = std::vector<std::string>{},
|
||||
py::arg("network_socket") = std::vector<std::string>{},
|
||||
py::arg("process") = std::vector<std::string>{},
|
||||
py::arg("threading") = false,
|
||||
py::arg("network") = std::vector<std::string>{},
|
||||
R"pbdoc(Request permissions while OrcaSlicer loads your module.
|
||||
|
||||
`fs_read` lists filesystem paths to read. `network` lists http(s) URLs; each is reduced to its
|
||||
scheme and host, so one grant covers every path, query and port on that host. The host presents
|
||||
the requests to the user after register_capabilities() returns. Denying a request aborts the
|
||||
plugin load.)pbdoc");
|
||||
Each bucket takes the target strings the plugin expects the corresponding audit event to
|
||||
report: filesystem paths for fs_read and fs_readwrite, URLs for network_http, socket addresses
|
||||
for network_socket, and commands for process. threading=True requests permission to start
|
||||
threads. Targets already granted to the plugin are not asked for again.
|
||||
|
||||
`network` also lists http(s) URLs, but each is reduced to its scheme and host, so one grant covers
|
||||
every path, query and port on that host and also authorizes raw socket events for it. The host
|
||||
presents one request to the user after register_capabilities() returns and before any capability
|
||||
instance is created. Denying the request aborts the plugin load.)pbdoc");
|
||||
|
||||
m.def("plugin", [](py::object cls) {
|
||||
if (g_active_plugin_key.empty())
|
||||
@@ -660,6 +666,93 @@ plugin load.)pbdoc");
|
||||
|
||||
}
|
||||
|
||||
void install_python_thread_audit_shim()
|
||||
{
|
||||
// Runs with the GIL held from PythonInterpreter::initialize(). CPython's own thread bootstrap
|
||||
// calls the target with no knowledge of the audit context that created it: the calling
|
||||
// thread is the only place the plugin key and capability are still available. Capture them
|
||||
// here, then re-establish the same ScopedPluginAuditContext inside the spawned thread before
|
||||
// the plugin body runs. The identity lives only in this C++ closure -- no Python-visible
|
||||
// setter is added.
|
||||
try {
|
||||
py::object thread_module = py::module_::import("_thread");
|
||||
|
||||
const int installed = PyObject_HasAttrString(thread_module.ptr(), "_orca_thread_audit_shim_installed");
|
||||
if (installed == 1) {
|
||||
BOOST_LOG_TRIVIAL(debug) << "[AUDIT] Python thread attribution shim already installed";
|
||||
return;
|
||||
}
|
||||
if (installed < 0)
|
||||
PyErr_Clear();
|
||||
|
||||
py::object original = py::reinterpret_steal<py::object>(
|
||||
PyObject_GetAttrString(thread_module.ptr(), "start_new_thread"));
|
||||
if (!original || !PyCallable_Check(original.ptr())) {
|
||||
PyErr_Clear();
|
||||
BOOST_LOG_TRIVIAL(error) << "[AUDIT] _thread.start_new_thread is missing or not callable; "
|
||||
"plugin threads will run without audit attribution";
|
||||
return;
|
||||
}
|
||||
|
||||
py::object shim = py::cpp_function(
|
||||
[original](py::object function, py::object args, py::object kwargs) -> py::object {
|
||||
const std::string plugin_key = PluginAuditManager::instance().current_plugin();
|
||||
if (plugin_key.empty()) {
|
||||
// Not called from a plugin: preserve the stock implementation untouched.
|
||||
return kwargs.is_none() ? original(function, args) : original(function, args, kwargs);
|
||||
}
|
||||
const std::string capability = PluginAuditManager::instance().current_capability();
|
||||
|
||||
py::object guarded = py::cpp_function(
|
||||
[function, plugin_key, capability](py::args call_args, py::kwargs call_kwargs) -> py::object {
|
||||
ScopedPluginAuditContext audit_scope(plugin_key, capability);
|
||||
PyObject* result = PyObject_Call(function.ptr(), call_args.ptr(), call_kwargs.ptr());
|
||||
if (result == nullptr)
|
||||
throw py::error_already_set();
|
||||
return py::reinterpret_steal<py::object>(result);
|
||||
});
|
||||
return kwargs.is_none() ? original(guarded, args) : original(guarded, args, kwargs);
|
||||
},
|
||||
py::arg("function"), py::arg("args"), py::arg("kwargs") = py::none());
|
||||
|
||||
if (PyObject_SetAttrString(thread_module.ptr(), "start_new_thread", shim.ptr()) < 0) {
|
||||
PyErr_Clear();
|
||||
BOOST_LOG_TRIVIAL(error)
|
||||
<< "[AUDIT] Failed to install Python thread attribution shim on _thread.start_new_thread";
|
||||
return;
|
||||
}
|
||||
if (PyObject_SetAttrString(thread_module.ptr(), "_orca_thread_audit_shim_installed", Py_True) < 0)
|
||||
PyErr_Clear();
|
||||
|
||||
// threading.py caches the function at import time (as `_start_new_thread` in 3.12, and as
|
||||
// `start_new_thread` in older versions). Patch the cached reference too when the module is
|
||||
// already loaded; otherwise its Thread.start() would keep calling the unwrapped original.
|
||||
py::object sys_modules = py::module_::import("sys").attr("modules");
|
||||
PyObject* threading_module = PyDict_GetItemString(sys_modules.ptr(), "threading");
|
||||
if (threading_module && threading_module != Py_None) {
|
||||
const char* cached_names[] = {"_start_new_thread", "start_new_thread"};
|
||||
for (const char* cached_name : cached_names) {
|
||||
const int has_cached_name = PyObject_HasAttrString(threading_module, cached_name);
|
||||
if (has_cached_name < 0) {
|
||||
PyErr_Clear();
|
||||
continue;
|
||||
}
|
||||
if (has_cached_name != 1)
|
||||
continue;
|
||||
if (PyObject_SetAttrString(threading_module, cached_name, shim.ptr()) < 0) {
|
||||
PyErr_Clear();
|
||||
BOOST_LOG_TRIVIAL(warning) << "[AUDIT] Failed to update threading." << cached_name
|
||||
<< " with the Python thread attribution shim";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_LOG_TRIVIAL(info) << "[AUDIT] Python thread attribution shim installed";
|
||||
} catch (const std::exception& ex) {
|
||||
BOOST_LOG_TRIVIAL(error) << "[AUDIT] Failed to install Python thread attribution shim: " << ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Slic3r
|
||||
|
||||
#ifdef ORCA_PYTHON_STUBGEN_MODULE
|
||||
|
||||
Reference in new issue
Block a user