mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-11-30 17:05:35 -05:00
Merge branch 'main' into feature/mcp
This commit is contained in:
commit
eedd56991c
237 files changed
+29514
-6750
No files matched your search
@@ -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,11 +33,15 @@
|
||||
#include <pybind11/pytypes.h>
|
||||
#include <pybind11/stl.h>
|
||||
|
||||
#include <clocale>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <ios>
|
||||
#include <iterator>
|
||||
#include <locale>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -68,6 +73,12 @@ void seed_denied_keywords()
|
||||
mgr.add_denied_path_keyword(keyword);
|
||||
}
|
||||
|
||||
std::string read_text_file(const fs::path& path)
|
||||
{
|
||||
std::ifstream in(path.string(), std::ios::binary);
|
||||
return std::string(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("Plugin audit denies host-managed filenames anywhere", "[audit]")
|
||||
@@ -302,6 +313,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
|
||||
@@ -689,6 +761,11 @@ def attempt(action):
|
||||
except PermissionError:
|
||||
return "denied"
|
||||
|
||||
def start_and_join_thread():
|
||||
worker = threading.Thread(target=lambda: None)
|
||||
worker.start()
|
||||
worker.join()
|
||||
|
||||
def spawn_checked(argv):
|
||||
parent = os.getpid()
|
||||
try:
|
||||
@@ -868,6 +945,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;
|
||||
@@ -1542,6 +1635,204 @@ inside_link_outcome = attempt(lambda: open(DANGLING_INSIDE_LINK, "w").close())
|
||||
CHECK_FALSE(fs::exists(inside_target));
|
||||
}
|
||||
|
||||
TEST_CASE("A saved exact file grant covers an equivalent spelling of the same file", "[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);
|
||||
|
||||
// The granted file, a file beside it, a lookalike name, the not-yet-existing target of a
|
||||
// dangling link, and the not-yet-existing target of a write.
|
||||
python.exec(R"PY(
|
||||
FILE = os.path.join(OUTSIDE, "approved-file.txt")
|
||||
OTHER = os.path.join(OUTSIDE, "other-file.txt")
|
||||
LOOKALIKE = os.path.join(OUTSIDE, "approved-file.txt.bak")
|
||||
NOT_CREATED = os.path.join(OUTSIDE, "not-created.txt")
|
||||
NEW_FILE = os.path.join(OUTSIDE, "new-file.txt")
|
||||
ESCAPE_PATH = os.path.join(OUTSIDE, "sub", "..", "other-file.txt")
|
||||
os.makedirs(os.path.join(OUTSIDE, "sub"))
|
||||
for path in (FILE, OTHER, LOOKALIKE):
|
||||
with open(path, "w") as f:
|
||||
f.write("x")
|
||||
)PY");
|
||||
|
||||
const fs::path outside = harness.outside.path();
|
||||
const fs::path file = python.str("FILE");
|
||||
const fs::path alias = outside / "alias.txt";
|
||||
const fs::path other_alias = outside / "other-alias.txt";
|
||||
const fs::path dangling = outside / "dangling.txt";
|
||||
boost::system::error_code ec;
|
||||
// A link to the granted file, a link to a different file, and a link whose target does not
|
||||
// exist yet.
|
||||
fs::create_symlink(file, alias, ec);
|
||||
if (ec)
|
||||
SKIP("Cannot create a file symlink here: " + ec.message());
|
||||
fs::create_symlink(outside / "other-file.txt", other_alias, ec);
|
||||
if (ec)
|
||||
SKIP("Cannot create a file symlink here: " + ec.message());
|
||||
fs::create_symlink(outside / "not-created.txt", dangling, ec);
|
||||
if (ec)
|
||||
SKIP("Cannot create a file symlink here: " + ec.message());
|
||||
|
||||
python.globals["ALIAS"] = alias.string();
|
||||
python.globals["OTHER_LINK"] = other_alias.string();
|
||||
python.globals["DANGLING"] = dangling.string();
|
||||
|
||||
PluginPermissions permissions;
|
||||
permissions.fs_read = {python.str("FILE")};
|
||||
permissions.fs_readwrite = {python.str("NOT_CREATED"), python.str("NEW_FILE")};
|
||||
harness.save_permissions(PLUGIN, permissions);
|
||||
|
||||
// A relative target is resolved against the working directory the open resolves in, so the
|
||||
// grant matches the file it names even though the audit target carries no directory.
|
||||
const fs::path original_cwd = fs::current_path();
|
||||
fs::current_path(outside, ec);
|
||||
REQUIRE_FALSE(ec);
|
||||
python.run_as(PLUGIN, R"PY(
|
||||
relative_read_outcome = attempt(lambda: open("approved-file.txt").read())
|
||||
relative_write_outcome = attempt(lambda: open("new-file.txt", "w").close())
|
||||
)PY");
|
||||
fs::current_path(original_cwd, ec);
|
||||
REQUIRE_FALSE(ec);
|
||||
|
||||
python.run_as(PLUGIN, R"PY(
|
||||
verbatim_outcome = attempt(lambda: open(FILE).read())
|
||||
dot_outcome = attempt(lambda: open(os.path.join(OUTSIDE, ".", "approved-file.txt")).read())
|
||||
updown_outcome = attempt(lambda: open(os.path.join(OUTSIDE, "sub", "..", "approved-file.txt")).read())
|
||||
alias_outcome = attempt(lambda: open(ALIAS).read())
|
||||
other_outcome = attempt(lambda: open(OTHER).read())
|
||||
escape_outcome = attempt(lambda: open(ESCAPE_PATH).read())
|
||||
other_link_outcome = attempt(lambda: open(OTHER_LINK).read())
|
||||
lookalike_outcome = attempt(lambda: open(LOOKALIKE).read())
|
||||
dangling_outcome = attempt(lambda: open(DANGLING, "w").close())
|
||||
)PY");
|
||||
|
||||
CHECK(python.str("verbatim_outcome") == "ok");
|
||||
CHECK(python.str("relative_read_outcome") == "ok");
|
||||
CHECK(python.str("relative_write_outcome") == "ok");
|
||||
CHECK(python.str("dot_outcome") == "ok");
|
||||
CHECK(python.str("updown_outcome") == "ok");
|
||||
CHECK(python.str("alias_outcome") == "ok");
|
||||
|
||||
// Full path equality, not containment: a sibling, a lookalike name, a link to another file,
|
||||
// and a ".." spelling that lands on another file are all asked about.
|
||||
CHECK(python.str("other_outcome") == "denied");
|
||||
CHECK(python.str("escape_outcome") == "denied");
|
||||
CHECK(python.str("other_link_outcome") == "denied");
|
||||
CHECK(python.str("lookalike_outcome") == "denied");
|
||||
// Fail closed on a dangling final link: the write would create its target, not the path the
|
||||
// grant names, even though that target itself carries a read/write grant.
|
||||
CHECK(python.str("dangling_outcome") == "denied");
|
||||
CHECK_FALSE(fs::exists(outside / "not-created.txt"));
|
||||
|
||||
const std::string open_prompt = "open: ";
|
||||
const std::string expected_prompts =
|
||||
open_prompt + python.str("OTHER") + "\n" + open_prompt + python.str("ESCAPE_PATH") + "\n" +
|
||||
open_prompt + python.str("OTHER_LINK") + "\n" + open_prompt + python.str("LOOKALIKE") + "\n" +
|
||||
open_prompt + python.str("DANGLING") + "\n";
|
||||
CHECK(describe(harness.prompts) == expected_prompts);
|
||||
|
||||
#ifdef _WIN32
|
||||
// Windows compares path names case-insensitively and accepts either separator, so a different
|
||||
// spelling of the granted file matches without another prompt.
|
||||
std::string forward_slashes;
|
||||
for (const char c : file.string())
|
||||
forward_slashes += c == '\\' ? '/' : c;
|
||||
python.globals["UPPERCASE_FILE"] = (file.parent_path() / "APPROVED-FILE.TXT").string();
|
||||
python.globals["FORWARD_SLASH_FILE"] = forward_slashes;
|
||||
harness.prompts.clear();
|
||||
python.run_as(PLUGIN, R"PY(
|
||||
uppercase_outcome = attempt(lambda: open(UPPERCASE_FILE).read())
|
||||
forward_slash_outcome = attempt(lambda: open(FORWARD_SLASH_FILE).read())
|
||||
)PY");
|
||||
CHECK(python.str("uppercase_outcome") == "ok");
|
||||
CHECK(python.str("forward_slash_outcome") == "ok");
|
||||
CHECK(describe(harness.prompts).empty());
|
||||
#endif
|
||||
|
||||
// A grant saved as a link applies to the file it points at, not just to the link's spelling.
|
||||
permissions.fs_read = {alias.string()};
|
||||
permissions.fs_readwrite.clear();
|
||||
harness.save_permissions(PLUGIN, permissions);
|
||||
harness.prompts.clear();
|
||||
python.run_as(PLUGIN, "grant_via_link_outcome = attempt(lambda: open(FILE).read())");
|
||||
CHECK(python.str("grant_via_link_outcome") == "ok");
|
||||
CHECK(describe(harness.prompts).empty());
|
||||
|
||||
// An exact grant names one file: it does not become a tree grant, so it does not cover what
|
||||
// lives inside the folder it names.
|
||||
permissions.fs_read = {python.str("OUTSIDE")};
|
||||
harness.save_permissions(PLUGIN, permissions);
|
||||
harness.prompts.clear();
|
||||
python.run_as(PLUGIN, "inside_granted_folder_outcome = attempt(lambda: open(FILE).read())");
|
||||
CHECK(python.str("inside_granted_folder_outcome") == "denied");
|
||||
CHECK(describe(harness.prompts) == open_prompt + python.str("FILE") + "\n");
|
||||
|
||||
// A grant that names a dangling link also covers only its verbatim spelling: an equivalent
|
||||
// spelling of the target the write would create is not matched by equivalence.
|
||||
permissions.fs_read.clear();
|
||||
permissions.fs_readwrite = {dangling.string()};
|
||||
harness.save_permissions(PLUGIN, permissions);
|
||||
harness.prompts.clear();
|
||||
python.run_as(PLUGIN, "through_dangling_grant_outcome = attempt(lambda: open(NOT_CREATED, 'w').close())");
|
||||
CHECK(python.str("through_dangling_grant_outcome") == "denied");
|
||||
CHECK(describe(harness.prompts) == open_prompt + python.str("NOT_CREATED") + "\n");
|
||||
CHECK_FALSE(fs::exists(outside / "not-created.txt"));
|
||||
}
|
||||
|
||||
TEST_CASE("A saved exact file grant covers an equivalent spelling in a permission request", "[audit]")
|
||||
{
|
||||
AuditHarness harness;
|
||||
if (!harness.initialized)
|
||||
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
|
||||
harness.add_plugin(PLUGIN);
|
||||
|
||||
const fs::path file = harness.outside.path() / "requested-file.txt";
|
||||
PluginPermissions stored;
|
||||
stored.fs_read = {file.string()};
|
||||
harness.save_permissions(PLUGIN, stored);
|
||||
|
||||
std::vector<std::string> prompted;
|
||||
PluginAuditManager::set_permission_message_prompt_for_testing(
|
||||
[&prompted](const std::string& message) {
|
||||
prompted.push_back(message);
|
||||
return false;
|
||||
});
|
||||
PluginAuditManager& mgr = PluginAuditManager::instance();
|
||||
|
||||
// An absolute declaration that is a different spelling of the granted path is already covered,
|
||||
// so it is neither shown nor appended as a duplicate grant.
|
||||
PluginPermissions dotted;
|
||||
dotted.fs_read = {file.parent_path().string() + "/./" + file.filename().string()};
|
||||
CHECK(mgr.request_permissions(PLUGIN, dotted));
|
||||
CHECK(prompted.empty());
|
||||
CHECK(harness.saved_permissions(PLUGIN).fs_read == std::vector<std::string>{file.string()});
|
||||
|
||||
// A relative declaration resolves against the directory the request is made in.
|
||||
const fs::path original_cwd = fs::current_path();
|
||||
boost::system::error_code ec;
|
||||
fs::current_path(harness.outside.path(), ec);
|
||||
REQUIRE_FALSE(ec);
|
||||
PluginPermissions relative;
|
||||
relative.fs_read = {file.filename().string()};
|
||||
const bool relative_granted = mgr.request_permissions(PLUGIN, relative);
|
||||
fs::current_path(original_cwd, ec);
|
||||
REQUIRE_FALSE(ec);
|
||||
CHECK(relative_granted);
|
||||
CHECK(prompted.empty());
|
||||
|
||||
// A different file still needs its own grant, so the equivalence above did not widen anything.
|
||||
PluginPermissions different;
|
||||
different.fs_read = {(harness.outside.path() / "different-file.txt").string()};
|
||||
CHECK_FALSE(mgr.request_permissions(PLUGIN, different));
|
||||
REQUIRE(prompted.size() == 1);
|
||||
CHECK(prompted.front().find("different-file.txt") != std::string::npos);
|
||||
PluginAuditManager::set_permission_message_prompt_for_testing(nullptr);
|
||||
}
|
||||
|
||||
TEST_CASE("A relative wildcard permission request is shown and stored as an absolute grant", "[audit][Python]")
|
||||
{
|
||||
AuditHarness harness;
|
||||
@@ -1587,6 +1878,524 @@ TEST_CASE("A relative wildcard permission request is shown and stored as an abso
|
||||
CHECK(describe(harness.prompts).empty());
|
||||
}
|
||||
|
||||
TEST_CASE("A threading permission request is prompted once and saved as a boolean", "[audit]")
|
||||
{
|
||||
AuditHarness harness;
|
||||
if (!harness.initialized)
|
||||
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
|
||||
harness.add_plugin(PLUGIN);
|
||||
PluginAuditManager& mgr = PluginAuditManager::instance();
|
||||
|
||||
PluginPermissions requested;
|
||||
requested.threading = true;
|
||||
|
||||
std::string shown;
|
||||
PluginAuditManager::set_permission_message_prompt_for_testing(
|
||||
[&shown](const std::string& message) { shown = message; return true; });
|
||||
const bool granted = mgr.request_permissions(PLUGIN, requested);
|
||||
PluginAuditManager::set_permission_message_prompt_for_testing(nullptr);
|
||||
|
||||
REQUIRE(granted);
|
||||
CHECK(shown.find("create a thread") != std::string::npos);
|
||||
CHECK(harness.saved_permissions(PLUGIN).threading);
|
||||
|
||||
// The saved boolean grant covers a repeated declaration without asking again.
|
||||
bool prompted_again = false;
|
||||
PluginAuditManager::set_permission_message_prompt_for_testing(
|
||||
[&prompted_again](const std::string&) {
|
||||
prompted_again = true;
|
||||
return false;
|
||||
});
|
||||
CHECK(mgr.request_permissions(PLUGIN, requested));
|
||||
PluginAuditManager::set_permission_message_prompt_for_testing(nullptr);
|
||||
CHECK_FALSE(prompted_again);
|
||||
}
|
||||
|
||||
TEST_CASE("A permission request creates a sidecar for a discovered plugin that has none", "[audit]")
|
||||
{
|
||||
AuditHarness harness;
|
||||
if (!harness.initialized)
|
||||
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
|
||||
harness.add_plugin(PLUGIN);
|
||||
|
||||
// A sandbox plugin may be discovered without ever having been given a sidecar. An approval
|
||||
// must still be saved, so the next session does not ask again.
|
||||
const fs::path sidecar = harness.data.plugins_dir() / PLUGIN / INSTALL_STATE_FILE;
|
||||
boost::system::error_code ec;
|
||||
fs::remove(sidecar, ec);
|
||||
REQUIRE_FALSE(ec);
|
||||
|
||||
PluginPermissions requested;
|
||||
requested.fs_read = {"/newly/granted"};
|
||||
|
||||
PluginAuditManager::set_permission_message_prompt_for_testing(
|
||||
[](const std::string&) { return true; });
|
||||
const bool granted = PluginAuditManager::instance().request_permissions(PLUGIN, requested);
|
||||
PluginAuditManager::set_permission_message_prompt_for_testing(nullptr);
|
||||
|
||||
REQUIRE(granted);
|
||||
CHECK(fs::exists(sidecar));
|
||||
CHECK(harness.saved_permissions(PLUGIN).fs_read == std::vector<std::string>{"/newly/granted"});
|
||||
}
|
||||
|
||||
TEST_CASE("A permission request does not recreate a sidecar that disappears while prompting", "[audit]")
|
||||
{
|
||||
AuditHarness harness;
|
||||
if (!harness.initialized)
|
||||
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
|
||||
harness.add_plugin(PLUGIN);
|
||||
|
||||
const fs::path sidecar = harness.data.plugins_dir() / PLUGIN / INSTALL_STATE_FILE;
|
||||
REQUIRE(fs::exists(sidecar));
|
||||
|
||||
bool removed = false;
|
||||
PluginAuditManager::set_permission_message_prompt_for_testing(
|
||||
[&sidecar, &removed](const std::string&) {
|
||||
boost::system::error_code ec;
|
||||
removed = fs::remove(sidecar, ec);
|
||||
return true;
|
||||
});
|
||||
|
||||
PluginPermissions requested;
|
||||
requested.fs_read = {"/new/grant"};
|
||||
const bool granted = PluginAuditManager::instance().request_permissions(PLUGIN, requested);
|
||||
PluginAuditManager::set_permission_message_prompt_for_testing(nullptr);
|
||||
|
||||
// Readable at the first read, absent at the second: that is lost state, not a new install, so
|
||||
// the approval must not create a fresh sidecar.
|
||||
REQUIRE(removed);
|
||||
CHECK_FALSE(granted);
|
||||
CHECK_FALSE(fs::exists(sidecar));
|
||||
}
|
||||
|
||||
TEST_CASE("A permission request leaves an unreadable install-state sidecar untouched", "[audit]")
|
||||
{
|
||||
AuditHarness harness;
|
||||
if (!harness.initialized)
|
||||
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
|
||||
harness.add_plugin(PLUGIN);
|
||||
|
||||
const fs::path sidecar = harness.data.plugins_dir() / PLUGIN / INSTALL_STATE_FILE;
|
||||
const std::string malformed = "{ \"permissions\": {";
|
||||
{
|
||||
std::ofstream out(sidecar.string(), std::ios::binary | std::ios::trunc);
|
||||
out << malformed;
|
||||
}
|
||||
|
||||
bool prompted = false;
|
||||
PluginAuditManager::set_permission_message_prompt_for_testing(
|
||||
[&prompted](const std::string&) {
|
||||
prompted = true;
|
||||
return true;
|
||||
});
|
||||
|
||||
PluginPermissions requested;
|
||||
requested.fs_read = {"/new/grant"};
|
||||
const bool granted = PluginAuditManager::instance().request_permissions(PLUGIN, requested);
|
||||
PluginAuditManager::set_permission_message_prompt_for_testing(nullptr);
|
||||
|
||||
// The file cannot be read, so no grant can be saved safely: fail closed instead of replacing
|
||||
// whatever it holds.
|
||||
CHECK_FALSE(granted);
|
||||
CHECK_FALSE(prompted);
|
||||
CHECK(read_text_file(sidecar) == malformed);
|
||||
}
|
||||
|
||||
TEST_CASE("An approved audited operation creates a missing install-state sidecar", "[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&) { return true; };
|
||||
|
||||
const fs::path sidecar = harness.data.plugins_dir() / PLUGIN / INSTALL_STATE_FILE;
|
||||
boost::system::error_code ec;
|
||||
fs::remove(sidecar, ec);
|
||||
REQUIRE_FALSE(ec);
|
||||
|
||||
// An event grant seeds a sidecar when none exists.
|
||||
python.run_as(PLUGIN, "read_outcome = attempt(lambda: open(PRIVATE).read())");
|
||||
CHECK(python.str("read_outcome") == "ok");
|
||||
REQUIRE(fs::exists(sidecar));
|
||||
CHECK_FALSE(harness.saved_permissions(PLUGIN).fs_read.empty());
|
||||
|
||||
// The threading helper does the same when the sidecar disappears again.
|
||||
fs::remove(sidecar, ec);
|
||||
REQUIRE_FALSE(ec);
|
||||
python.run_as(PLUGIN, "thread_outcome = attempt(start_and_join_thread)");
|
||||
CHECK(python.str("thread_outcome") == "ok");
|
||||
CHECK(fs::exists(sidecar));
|
||||
CHECK(harness.saved_permissions(PLUGIN).threading);
|
||||
}
|
||||
|
||||
TEST_CASE("An approved audited operation does not replace an unreadable install-state sidecar", "[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&) { return true; };
|
||||
|
||||
// Both the event-grant path (persist_permission) and the boolean threading path
|
||||
// (persist_threading_grant) must preserve a sidecar they cannot read.
|
||||
const fs::path sidecar = harness.data.plugins_dir() / PLUGIN / INSTALL_STATE_FILE;
|
||||
const std::string malformed = "not an install state";
|
||||
{
|
||||
std::ofstream out(sidecar.string(), std::ios::binary | std::ios::trunc);
|
||||
out << malformed;
|
||||
}
|
||||
|
||||
// The operation itself is approved for this call; only the persistence is refused.
|
||||
python.run_as(PLUGIN, "read_outcome = attempt(lambda: open(PRIVATE).read())");
|
||||
python.run_as(PLUGIN, "thread_outcome = attempt(start_and_join_thread)");
|
||||
|
||||
CHECK(python.str("read_outcome") == "ok");
|
||||
CHECK(python.str("thread_outcome") == "ok");
|
||||
CHECK(read_text_file(sidecar) == malformed);
|
||||
}
|
||||
|
||||
TEST_CASE("Thread creation is denied without a grant and remembered after approval", "[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);
|
||||
|
||||
// The audit hook's thread event has no user-supplied target; the fixed sentinel is what the
|
||||
// prompt and the persisted grant use. The helper joins the worker so no test thread outlives
|
||||
// the test case.
|
||||
python.run_as(PLUGIN, "thread_outcome = attempt(start_and_join_thread)");
|
||||
CHECK(python.str("thread_outcome") == "denied");
|
||||
CHECK(describe(harness.prompts) == "_thread.start_new_thread: thread\n");
|
||||
CHECK_FALSE(harness.saved_permissions(PLUGIN).threading);
|
||||
|
||||
harness.approve = [](const Prompt&) { return true; };
|
||||
harness.prompts.clear();
|
||||
python.run_as(PLUGIN, "thread_outcome = attempt(start_and_join_thread)");
|
||||
CHECK(python.str("thread_outcome") == "ok");
|
||||
CHECK(describe(harness.prompts) == "_thread.start_new_thread: thread\n");
|
||||
CHECK(harness.saved_permissions(PLUGIN).threading);
|
||||
|
||||
// The saved grant is effective for later events without another prompt.
|
||||
harness.prompts.clear();
|
||||
python.run_as(PLUGIN, "thread_outcome = attempt(start_and_join_thread)");
|
||||
CHECK(python.str("thread_outcome") == "ok");
|
||||
CHECK(describe(harness.prompts).empty());
|
||||
}
|
||||
|
||||
TEST_CASE("Full access allows an audited operation without a grant", "[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);
|
||||
|
||||
PluginPermissions permissions;
|
||||
permissions.full_access = true;
|
||||
harness.save_permissions(PLUGIN, permissions);
|
||||
|
||||
// PRIVATE sits outside every allowed root, so without full access this read would prompt.
|
||||
python.run_as(PLUGIN, "outcome = attempt(lambda: open(PRIVATE).read())");
|
||||
|
||||
CHECK(python.str("outcome") == "ok");
|
||||
CHECK(describe(harness.prompts).empty());
|
||||
}
|
||||
|
||||
TEST_CASE("Without full access an audited operation still prompts", "[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);
|
||||
|
||||
PluginPermissions permissions;
|
||||
permissions.full_access = false;
|
||||
harness.save_permissions(PLUGIN, permissions);
|
||||
|
||||
python.run_as(PLUGIN, "outcome = attempt(lambda: open(PRIVATE).read())");
|
||||
|
||||
CHECK(python.str("outcome") == "denied");
|
||||
CHECK(describe(harness.prompts) == "open: " + python.str("PRIVATE") + "\n");
|
||||
}
|
||||
|
||||
TEST_CASE("Full access does not bypass an unconditional denied path", "[audit][Python]")
|
||||
{
|
||||
AuditHarness harness;
|
||||
if (!harness.initialized)
|
||||
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
|
||||
seed_denied_names();
|
||||
harness.add_plugin(PLUGIN);
|
||||
py::gil_scoped_acquire gil;
|
||||
PythonSide python(harness);
|
||||
|
||||
PluginPermissions permissions;
|
||||
permissions.full_access = true;
|
||||
harness.save_permissions(PLUGIN, permissions);
|
||||
|
||||
// The install-state sidecar stays host-managed: full access only switches off permission
|
||||
// auditing, it does not make the plugin's own state file reachable.
|
||||
python.globals["INSTALL_STATE"] = (harness.data.plugins_dir() / PLUGIN / ".install_state.json").string();
|
||||
python.run_as(PLUGIN, "denied_outcome = attempt(lambda: open(INSTALL_STATE).read())");
|
||||
|
||||
CHECK(python.str("denied_outcome") == "denied");
|
||||
CHECK(describe(harness.prompts).empty());
|
||||
AuditViolation violation;
|
||||
REQUIRE(PluginAuditManager::instance().last_violation(violation));
|
||||
CHECK(violation.reason == "denied path");
|
||||
}
|
||||
|
||||
TEST_CASE("A full access plugin's permission declaration is not prompted", "[audit]")
|
||||
{
|
||||
AuditHarness harness;
|
||||
if (!harness.initialized)
|
||||
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
|
||||
harness.add_plugin(PLUGIN);
|
||||
|
||||
PluginPermissions stored;
|
||||
stored.full_access = true;
|
||||
stored.fs_read = {"/previously/granted"};
|
||||
harness.save_permissions(PLUGIN, stored);
|
||||
|
||||
bool prompted = false;
|
||||
PluginAuditManager::set_permission_message_prompt_for_testing(
|
||||
[&prompted](const std::string&) {
|
||||
prompted = true;
|
||||
return false;
|
||||
});
|
||||
|
||||
PluginPermissions requested;
|
||||
requested.fs_read = {"/new/target"};
|
||||
const bool granted = PluginAuditManager::instance().request_permissions(PLUGIN, requested);
|
||||
|
||||
PluginAuditManager::set_permission_message_prompt_for_testing(nullptr);
|
||||
|
||||
// The declaration is accepted without a prompt and without adding a grant: full access already
|
||||
// covers the event, and the stored list stays as it was.
|
||||
CHECK(granted);
|
||||
CHECK_FALSE(prompted);
|
||||
CHECK(harness.saved_permissions(PLUGIN).fs_read == std::vector<std::string>{"/previously/granted"});
|
||||
}
|
||||
|
||||
TEST_CASE("Toggling full access preserves the other stored grants", "[audit]")
|
||||
{
|
||||
AuditHarness harness;
|
||||
if (!harness.initialized)
|
||||
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
|
||||
harness.add_plugin(PLUGIN);
|
||||
|
||||
PluginPermissions stored;
|
||||
stored.fs_read = {"/kept/read"};
|
||||
stored.network_http = {"https://kept.example"};
|
||||
harness.save_permissions(PLUGIN, stored);
|
||||
|
||||
PluginManager& manager = PluginManager::instance();
|
||||
PluginPermissionMutation toggle;
|
||||
toggle.full_access = true;
|
||||
REQUIRE(manager.mutate_plugin_permissions(PLUGIN, toggle));
|
||||
PluginPermissions after_enable = harness.saved_permissions(PLUGIN);
|
||||
CHECK(after_enable.full_access);
|
||||
CHECK(after_enable.fs_read == std::vector<std::string>{"/kept/read"});
|
||||
CHECK(after_enable.network_http == std::vector<std::string>{"https://kept.example"});
|
||||
|
||||
toggle.full_access = false;
|
||||
REQUIRE(manager.mutate_plugin_permissions(PLUGIN, toggle));
|
||||
PluginPermissions after_disable = harness.saved_permissions(PLUGIN);
|
||||
CHECK_FALSE(after_disable.full_access);
|
||||
CHECK(after_disable.fs_read == std::vector<std::string>{"/kept/read"});
|
||||
|
||||
// An unknown package has no sidecar to modify: the toggle fails closed.
|
||||
toggle.full_access = true;
|
||||
CHECK_FALSE(manager.mutate_plugin_permissions("NoSuchPlugin", toggle));
|
||||
}
|
||||
|
||||
TEST_CASE("A permission mutation changes only the named entries and preserves the rest", "[audit]")
|
||||
{
|
||||
AuditHarness harness;
|
||||
if (!harness.initialized)
|
||||
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
|
||||
harness.add_plugin(PLUGIN);
|
||||
|
||||
const fs::path plugin_dir = harness.data.plugins_dir() / PLUGIN;
|
||||
PluginInstallState initial;
|
||||
initial.installed_from = "local";
|
||||
initial.installed_version = "1.2.3";
|
||||
initial.plugin_name = "Audit Plugin";
|
||||
initial.enabled = false;
|
||||
initial.capabilities = {{"Echo", false}, {"Other", true}};
|
||||
initial.permissions.fs_read = {"/kept/read", "/removed/read"};
|
||||
initial.permissions.fs_readwrite = {"/kept/write"};
|
||||
initial.permissions.network_http = {"https://kept.example"};
|
||||
REQUIRE(write_install_state(plugin_dir, initial));
|
||||
|
||||
PluginPermissionMutation mutation;
|
||||
mutation.add = {
|
||||
{PluginPermissionCategory::FsRead, "/added/read"},
|
||||
{PluginPermissionCategory::FsRead, "/added/read"}, // duplicate is collapsed
|
||||
{PluginPermissionCategory::FsRead, ""}, // empty values are skipped
|
||||
{PluginPermissionCategory::FsRead, "/removed/read"},
|
||||
{PluginPermissionCategory::NetworkSocket, "127.0.0.1:8080"},
|
||||
};
|
||||
mutation.remove = {
|
||||
{PluginPermissionCategory::FsRead, "/removed/read"},
|
||||
{PluginPermissionCategory::FsRead, "/never/granted"},
|
||||
{PluginPermissionCategory::FsRead, ""},
|
||||
{PluginPermissionCategory::Process, "/never/granted"},
|
||||
};
|
||||
|
||||
PluginPermissions applied;
|
||||
REQUIRE(PluginManager::instance().mutate_plugin_permissions(PLUGIN, mutation, &applied));
|
||||
// Removals run first, so a value named in both lists ends present; absent removals are ignored.
|
||||
const std::vector<std::string> expected_fs_read{"/kept/read", "/added/read", "/removed/read"};
|
||||
const std::vector<std::string> expected_sockets{"127.0.0.1:8080"};
|
||||
CHECK(applied.fs_read == expected_fs_read);
|
||||
CHECK(applied.network_socket == expected_sockets);
|
||||
|
||||
PluginInstallState stored;
|
||||
REQUIRE(read_install_state(plugin_dir, stored));
|
||||
CHECK(stored.permissions.fs_read == expected_fs_read);
|
||||
CHECK(stored.permissions.network_socket == expected_sockets);
|
||||
|
||||
// Every grant the mutation did not name stays as it was.
|
||||
CHECK(stored.permissions.fs_readwrite == initial.permissions.fs_readwrite);
|
||||
CHECK(stored.permissions.network_http == initial.permissions.network_http);
|
||||
CHECK(stored.permissions.process.empty());
|
||||
CHECK_FALSE(stored.permissions.threading);
|
||||
CHECK_FALSE(stored.permissions.full_access);
|
||||
|
||||
// The install metadata, enabled flag and capability flags are unrelated state and survive too.
|
||||
CHECK(stored.installed_from == initial.installed_from);
|
||||
CHECK(stored.installed_version == initial.installed_version);
|
||||
CHECK(stored.plugin_name == initial.plugin_name);
|
||||
CHECK_FALSE(stored.enabled);
|
||||
REQUIRE(stored.capabilities.size() == 2);
|
||||
CHECK(stored.capabilities[0].first == "Echo");
|
||||
CHECK_FALSE(stored.capabilities[0].second);
|
||||
CHECK(stored.capabilities[1].first == "Other");
|
||||
CHECK(stored.capabilities[1].second);
|
||||
}
|
||||
|
||||
TEST_CASE("A permission mutation applies to the latest stored contents", "[audit]")
|
||||
{
|
||||
AuditHarness harness;
|
||||
if (!harness.initialized)
|
||||
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
|
||||
harness.add_plugin(PLUGIN);
|
||||
|
||||
PluginPermissions initial;
|
||||
initial.fs_read = {"/caller/saw"};
|
||||
harness.save_permissions(PLUGIN, initial);
|
||||
|
||||
PluginManager& manager = PluginManager::instance();
|
||||
PluginInstallState observed;
|
||||
REQUIRE(manager.get_install_state(PLUGIN, observed));
|
||||
CHECK(observed.permissions.fs_read == std::vector<std::string>{"/caller/saw"});
|
||||
|
||||
// Another writer grants a target after the caller read the state.
|
||||
PluginPermissionMutation concurrent;
|
||||
concurrent.add = {{PluginPermissionCategory::FsRead, "/other/writer"}};
|
||||
REQUIRE(manager.mutate_plugin_permissions(PLUGIN, concurrent));
|
||||
|
||||
// The caller's edit is applied against the sidecar as it stands now, not against `observed`,
|
||||
// so the concurrent grant is not dropped.
|
||||
PluginPermissionMutation mine;
|
||||
mine.add = {{PluginPermissionCategory::FsRead, "/caller/adds"}};
|
||||
REQUIRE(manager.mutate_plugin_permissions(PLUGIN, mine));
|
||||
|
||||
PluginInstallState stored;
|
||||
REQUIRE(read_install_state(harness.data.plugins_dir() / PLUGIN, stored));
|
||||
const std::vector<std::string> expected_fs_read{"/caller/saw", "/other/writer", "/caller/adds"};
|
||||
CHECK(stored.permissions.fs_read == expected_fs_read);
|
||||
}
|
||||
|
||||
TEST_CASE("A permission mutation changes boolean grants only when asked", "[audit]")
|
||||
{
|
||||
AuditHarness harness;
|
||||
if (!harness.initialized)
|
||||
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
|
||||
harness.add_plugin(PLUGIN);
|
||||
|
||||
PluginPermissions initial;
|
||||
initial.fs_read = {"/kept"};
|
||||
initial.full_access = true;
|
||||
harness.save_permissions(PLUGIN, initial);
|
||||
|
||||
PluginManager& manager = PluginManager::instance();
|
||||
|
||||
// A mutation that names no booleans leaves both flags alone.
|
||||
PluginPermissions applied;
|
||||
REQUIRE(manager.mutate_plugin_permissions(PLUGIN, PluginPermissionMutation{}, &applied));
|
||||
CHECK_FALSE(applied.threading);
|
||||
CHECK(applied.full_access);
|
||||
CHECK(applied.fs_read == std::vector<std::string>{"/kept"});
|
||||
|
||||
PluginPermissionMutation booleans;
|
||||
booleans.threading = true;
|
||||
booleans.full_access = false;
|
||||
REQUIRE(manager.mutate_plugin_permissions(PLUGIN, booleans, &applied));
|
||||
CHECK(applied.threading);
|
||||
CHECK_FALSE(applied.full_access);
|
||||
CHECK(applied.fs_read == std::vector<std::string>{"/kept"});
|
||||
|
||||
const PluginPermissions stored = harness.saved_permissions(PLUGIN);
|
||||
CHECK(stored.threading);
|
||||
CHECK_FALSE(stored.full_access);
|
||||
CHECK(stored.fs_read == std::vector<std::string>{"/kept"});
|
||||
}
|
||||
|
||||
TEST_CASE("A permission mutation fails closed for a plugin it cannot update", "[audit]")
|
||||
{
|
||||
AuditHarness harness;
|
||||
if (!harness.initialized)
|
||||
SKIP("Bundled Python interpreter unavailable: " + PythonInterpreter::instance().last_error());
|
||||
harness.add_plugin(PLUGIN);
|
||||
|
||||
PluginPermissionMutation mutation;
|
||||
mutation.add = {{PluginPermissionCategory::FsRead, "/new"}};
|
||||
PluginManager& manager = PluginManager::instance();
|
||||
|
||||
CHECK_FALSE(manager.mutate_plugin_permissions("", mutation));
|
||||
CHECK_FALSE(manager.mutate_plugin_permissions("NoSuchPlugin", mutation));
|
||||
|
||||
// A discovered package whose sidecar is gone has nothing safe to update.
|
||||
const fs::path sidecar = harness.data.plugins_dir() / PLUGIN / INSTALL_STATE_FILE;
|
||||
fs::remove(sidecar);
|
||||
CHECK_FALSE(manager.mutate_plugin_permissions(PLUGIN, mutation));
|
||||
}
|
||||
|
||||
TEST_CASE("A permission edit applies to the next audited operation without a reload", "[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);
|
||||
|
||||
python.run_as(PLUGIN, "first_outcome = attempt(lambda: open(PRIVATE).read())");
|
||||
CHECK(python.str("first_outcome") == "denied");
|
||||
harness.prompts.clear();
|
||||
|
||||
PluginPermissionMutation mutation;
|
||||
mutation.full_access = true;
|
||||
REQUIRE(PluginManager::instance().mutate_plugin_permissions(PLUGIN, mutation));
|
||||
|
||||
// The running audit re-reads the sidecar, so the edit covers the next operation.
|
||||
python.run_as(PLUGIN, "second_outcome = attempt(lambda: open(PRIVATE).read())");
|
||||
CHECK(python.str("second_outcome") == "ok");
|
||||
CHECK(describe(harness.prompts).empty());
|
||||
}
|
||||
|
||||
TEST_CASE("A filesystem wildcard keeps read and write grants separate", "[audit][Python]")
|
||||
{
|
||||
AuditHarness harness;
|
||||
|
||||
Reference in new issue
Block a user