Plugin audit: keep non-Latin path names intact in the keyword check (#16925)

* 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 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

---------

Co-authored-by: GNUGiminot <215263196+GNUGiminot@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: SoftFever <103989404+SoftFever@users.noreply.github.com>
(cherry picked from commit 2ec1a0732f)
This commit is contained in:
GNUGiminot authored and SoftFever committed 2026-11-07 02:14:51 +08:00
1 parent 69f66d781e
commit 84a2400a90
2 files changed
+100 -8

No files matched your search

+81
View File
@@ -1,5 +1,6 @@
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/nowide/filesystem.hpp>
#include <boost/system/error_code.hpp>
#include <catch2/catch_all.hpp>
@@ -32,10 +33,13 @@
#include <pybind11/pytypes.h>
#include <pybind11/stl.h>
#include <clocale>
#include <fstream>
#include <functional>
#include <ios>
#include <locale>
#include <memory>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
@@ -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 <home>/.var/app/<app-id>/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;