From 84a2400a906e40a04eda856c69c5ca0870fc7377 Mon Sep 17 00:00:00 2001 From: GNUGiminot Date: Fri, 6 Nov 2026 15:35:44 +0300 Subject: [PATCH] Plugin audit: keep non-Latin path names intact in the keyword check (#16925) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Keep non-Latin path names intact in the plugin audit keyword check is_denied_path_keyword() lowercased every path component with std::tolower(), which follows the C locale. With a single-byte locale, which the UI language sets on Windows (Russian_Russia.1251), that rewrites bytes of UTF-8 sequences: "Новая папка" is no longer valid UTF-8, and has_denied_config_extension() then throws "boost::filesystem ::path codecvt to wstring: partial" while turning it back into a path. The exception left the CPython audit hook, so the plugin call never returned, and the app crashed later in PyFrame_GetCode. The keyword check now uses the file's ascii_lower(), which lowercases A-Z only; the keywords are ASCII, so matching stays case-insensitive. ascii_lower() itself no longer goes through a locale-aware boost::algorithm::to_lower(), so the network permission keys get the same guarantee. The audit hook also no longer lets any C++ exception reach the interpreter: a check that fails denies the event, like an unanswered prompt. Co-Authored-By: Claude Opus 5.5 * Address review: narrower try in audit_hook, shorter comments, a test for the catch - Wrap only audit_plugin_event() in the try, so release_frames() is called once after the event is decided. - Shorten the ascii_lower() comment and drop the one that repeated it. - Use only the .1251 locale in the non-Latin path test. - Test that a permission prompt that throws denies the event, which reaches the new catch on every platform. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: GNUGiminot <215263196+GNUGiminot@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 Co-authored-by: SoftFever <103989404+SoftFever@users.noreply.github.com> (cherry picked from commit 2ec1a0732f2545737ab55967e27e85285e7c76eb) --- src/slic3r/plugin/PluginAuditManager.cpp | 27 +++++--- tests/slic3rutils/test_plugin_audit.cpp | 81 ++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 8 deletions(-) diff --git a/src/slic3r/plugin/PluginAuditManager.cpp b/src/slic3r/plugin/PluginAuditManager.cpp index 2c1a13b990..8291e3e9d8 100644 --- a/src/slic3r/plugin/PluginAuditManager.cpp +++ b/src/slic3r/plugin/PluginAuditManager.cpp @@ -5,7 +5,6 @@ #include "libslic3r/libslic3r.h" // GCODEVIEWER_APP_KEY, and SLIC3R_APP_KEY via libslic3r_version.h #include "libslic3r_version.h" -#include #include #include #include @@ -16,9 +15,9 @@ #include #include -#include #include #include +#include #include #include #include @@ -177,9 +176,12 @@ static bool is_network_category(AuditEventCategory category) // Network permission identity // --------------------------------------------------------------------------- +// Lowercases A-Z only. A locale-aware lowercase can break the UTF-8 of non-Latin names. static std::string ascii_lower(std::string value) { - boost::algorithm::to_lower(value); + for (char& c : value) + if (c >= 'A' && c <= 'Z') + c = static_cast(c - 'A' + 'a'); return value; } @@ -631,8 +633,7 @@ void PluginAuditManager::add_denied_path_keyword(const std::string& keyword) if (keyword.empty()) return; - std::string lower = keyword; - std::transform(lower.begin(), lower.end(), lower.begin(), [](unsigned char c) { return std::tolower(c); }); + const std::string lower = ascii_lower(keyword); std::lock_guard lock(m_mutex); m_denied_path_keywords.push_back(lower); @@ -689,10 +690,9 @@ bool PluginAuditManager::is_denied_path_keyword(const boost::filesystem::path& c return false; for (const auto& component : scope) { - std::string name = component.string(); + const std::string name = ascii_lower(component.string()); if (name.empty()) continue; - std::transform(name.begin(), name.end(), name.begin(), [](unsigned char c) { return std::tolower(c); }); for (const auto& keyword : m_denied_path_keywords) { if (name == keyword || (keyword == "conf" && has_denied_config_extension(name))) return true; @@ -1792,7 +1792,18 @@ int PluginAuditManager::audit_hook(const char* event, PyObject* args, void* user // finalizer they run re-enters a hook whose held frames are already consistent. std::vector returned_frames; PluginAuditDetail::take_returned_frames(m_root_frames, returned_frames); - const int result = PluginAuditDetail::audit_plugin_event(*mgr, event_name, event_type, args); + int result = -1; + // A C++ exception must not unwind through the interpreter that raised the event, so a check + // that throws denies the event, like an unanswered prompt. + try { + result = PluginAuditDetail::audit_plugin_event(*mgr, event_name, event_type, args); + } catch (const std::exception& error) { + BOOST_LOG_TRIVIAL(error) << "[AUDIT] event=" << event_name << " denied, its check failed: " << error.what(); + result = PluginAuditDetail::report_denied(*mgr, event_name, {false, "audit check failed"}); + } catch (...) { + BOOST_LOG_TRIVIAL(error) << "[AUDIT] event=" << event_name << " denied, its check failed"; + result = PluginAuditDetail::report_denied(*mgr, event_name, {false, "audit check failed"}); + } mgr->release_frames(returned_frames); return result; } diff --git a/tests/slic3rutils/test_plugin_audit.cpp b/tests/slic3rutils/test_plugin_audit.cpp index a257e304ea..4751d77ec4 100644 --- a/tests/slic3rutils/test_plugin_audit.cpp +++ b/tests/slic3rutils/test_plugin_audit.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -32,10 +33,13 @@ #include #include +#include #include #include #include +#include #include +#include #include #include #include @@ -301,6 +305,67 @@ TEST_CASE("Plugin audit denies secret/certificate/config-like paths by keyword", } } +namespace { + +// Converts paths as UTF-8 for the test's lifetime, as the app does from startup +// (boost::nowide::nowide_filesystem() in OrcaSlicer.cpp). +struct Utf8PathConversion +{ + std::locale previous = boost::nowide::nowide_filesystem(); + ~Utf8PathConversion() { fs::path::imbue(previous); } +}; + +// Switches the C library's character classification to a single-byte code page, as the app's UI +// language does on Windows (Russian_Russia.1251), when the system has one. +struct SingleByteCtypeLocale +{ + std::string previous; + bool applied = false; + + SingleByteCtypeLocale() + { + if (const char* current = std::setlocale(LC_CTYPE, nullptr)) + previous = current; + applied = std::setlocale(LC_CTYPE, ".1251") != nullptr; + } + ~SingleByteCtypeLocale() + { + if (applied) + std::setlocale(LC_CTYPE, previous.c_str()); + } +}; + +} // namespace + +TEST_CASE("Plugin audit keyword check keeps non-Latin path names intact", "[audit]") +{ + seed_denied_keywords(); + const PluginAuditManager& mgr = PluginAuditManager::instance(); + + // Lowering these names byte by byte in a single-byte C locale broke their UTF-8, and turning + // the result back into a path threw out of the audit hook: the plugin call never returned. + const Utf8PathConversion utf8_paths; + const SingleByteCtypeLocale ctype; + INFO("single-byte C locale in use: " << (ctype.applied ? "yes" : "no")); + + const std::string folder = "\xD0\x9D\xD0\xBE\xD0\xB2\xD0\xB0\xD1\x8F \xD0\xBF\xD0\xB0\xD0\xBF\xD0\xBA\xD0\xB0"; // "Новая папка" + const std::string part = "\xD0\xB4\xD0\xB5\xD1\x82\xD0\xB0\xD0\xBB\xD1\x8C.stl"; // "деталь.stl" + const std::string settings = "\xD0\xBD\xD0\xB0\xD1\x81\xD1\x82\xD1\x80\xD0\xBE\xD0\xB9\xD0\xBA\xD0\xB8.INI"; // "настройки.INI" + + SECTION("a path with non-Latin names is checked without an error and allowed") + { + bool denied = true; + CHECK_NOTHROW(denied = mgr.is_denied_path_keyword(fs::path("/models/" + folder + "/" + part))); + CHECK_FALSE(denied); + } + + SECTION("keywords and config files next to non-Latin names are still denied, in any case") + { + CHECK(mgr.is_denied_path_keyword(fs::path("/models/" + folder + "/CONFIG/a.stl"))); + CHECK(mgr.is_denied_path_keyword(fs::path("/models/" + folder + "/" + settings))); + } +} + TEST_CASE("Plugin audit checks denied keywords below data_dir, not in the directories above it", "[PluginAudit]") { // Flatpak keeps the app's data folder under /.var/app//config/OrcaSlicer, so @@ -867,6 +932,22 @@ TEST_CASE("A saved request grant covers the request's own connection in a later CHECK(describe(harness.prompts).empty()); } +TEST_CASE("A permission prompt that throws denies the event", "[audit][Python]") +{ + AuditHarness harness; + if (!harness.initialized) + SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error()); + harness.add_plugin(PLUGIN); + py::gil_scoped_acquire gil; + PythonSide python(harness); + harness.approve = [](const Prompt&) -> bool { throw std::runtime_error("prompt failed"); }; + + python.run_as(PLUGIN, "outcome = attempt(lambda: open(PRIVATE).close())"); + + CHECK(python.str("outcome") == "denied"); + CHECK(describe(harness.prompts) == "open: " + python.str("PRIVATE") + "\n"); +} + TEST_CASE("A saved request grant does not cover another URL or a redirect", "[audit][Python]") { AuditHarness harness;