Preserve independent support, typed rule namespaces and stable process lifetimes. Share authoritative managed-set reconciliation across daemon and offline correlation, load offline policy without seeding/migration, and release resources after shutdown-flush exceptions. Keep retirement history, transaction ownership and fault/allocation regressions; no whole-set crash-atomicity or live acceptance claim.
355 lines
18 KiB
C++
355 lines
18 KiB
C++
#include "strawberry/config.hpp"
|
|
#include "strawberry/policy.hpp"
|
|
#include "strawberry/proc_read.hpp"
|
|
#include "strawberry/scanner_health.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
#include <unistd.h>
|
|
|
|
namespace fs = std::filesystem;
|
|
using strawberry::AllowEntry;
|
|
using strawberry::PolicyStore;
|
|
|
|
namespace {
|
|
|
|
fs::path make_root() {
|
|
const auto root = fs::temp_directory_path() / ("strawberry_policy_test_" + std::to_string(::getpid()));
|
|
std::error_code ec;
|
|
fs::remove_all(root, ec);
|
|
fs::create_directories(root);
|
|
return root;
|
|
}
|
|
|
|
bool ok(PolicyStore& store, const std::string& verb, const std::string& payload, std::string* reply = nullptr) {
|
|
std::string error;
|
|
const auto result = store.apply(verb, payload, 1000, &error);
|
|
if (reply) *reply = result ? *result : error;
|
|
return result.has_value();
|
|
}
|
|
|
|
std::string err(PolicyStore& store, const std::string& verb, const std::string& payload) {
|
|
std::string error;
|
|
const auto result = store.apply(verb, payload, 1000, &error);
|
|
assert(!result);
|
|
return error;
|
|
}
|
|
|
|
const std::string kHash(64, 'a');
|
|
|
|
void test_seed_and_round_trip() {
|
|
std::cout << "Test seed and round trip:" << std::endl;
|
|
const auto root = make_root();
|
|
const auto path = (root / "policy.json").string();
|
|
|
|
PolicyStore store(path);
|
|
assert(store.load());
|
|
assert(fs::exists(path));
|
|
auto snapshot = store.snapshot();
|
|
assert(snapshot.feeds.size() == strawberry::default_feeds().size());
|
|
assert(snapshot.feeds[0].id == "yara-forge-core" && snapshot.feeds[0].kind == "yara");
|
|
assert(snapshot.feeds[0].repo == "YARAHQ/yara-forge" && snapshot.feeds[0].asset == "yara-forge-rules-core.zip");
|
|
std::size_t lists = 0;
|
|
for (const auto& feed : snapshot.feeds) {
|
|
lists += strawberry::is_list_kind(feed.kind) ? 1 : 0;
|
|
if (feed.type == "url") {
|
|
assert(!feed.urls.empty() && feed.urls.front().starts_with("https://"));
|
|
}
|
|
}
|
|
assert(lists == 5); // four threat-intel lists plus EasyPrivacy (trackers)
|
|
|
|
assert(ok(store, "allowlist-add", R"({"kind":"sha256","value":")" + std::string(64, 'A') + R"(","note":"tool"})"));
|
|
assert(ok(store, "feed-add", R"({"id":"extra","type":"url","url":"https://example.com/r.yar"})"));
|
|
|
|
PolicyStore reloaded(path);
|
|
assert(reloaded.load());
|
|
snapshot = reloaded.snapshot();
|
|
assert(snapshot.allowlist.size() == 1);
|
|
assert(snapshot.allowlist[0].value == kHash); // normalized to lowercase
|
|
assert(snapshot.allowlist[0].added_by == 1000);
|
|
assert(snapshot.feeds.size() == strawberry::default_feeds().size() + 1);
|
|
assert(snapshot.history.size() == 3); // init + 2 changes
|
|
assert(snapshot.feeds.back().kind == "yara" && snapshot.feeds.back().urls.size() == 1);
|
|
|
|
// Corrupt file: load fails, nothing is overwritten.
|
|
{ std::ofstream(path) << "{not json"; }
|
|
PolicyStore corrupt(path);
|
|
std::string error;
|
|
assert(!corrupt.load(&error) && !error.empty());
|
|
|
|
fs::remove_all(root);
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_validation() {
|
|
std::cout << "Test validation:" << std::endl;
|
|
const auto root = make_root();
|
|
PolicyStore store((root / "policy.json").string());
|
|
assert(store.load());
|
|
|
|
assert(err(store, "allowlist-add", "not json").find("not valid JSON") != std::string::npos);
|
|
assert(err(store, "allowlist-add", "[]").find("object") != std::string::npos);
|
|
assert(err(store, "allowlist-add", R"({"kind":"sha256","value":"abc"})").find("64") != std::string::npos);
|
|
assert(err(store, "allowlist-add", R"({"kind":"path","value":"relative"})").find("absolute") != std::string::npos);
|
|
assert(err(store, "allowlist-add", R"({"kind":"path","value":"/usr/../etc/shadow"})").find("normalized") != std::string::npos);
|
|
assert(err(store, "allowlist-add", R"({"kind":"rule","value":"bad rule!"})").find("rule") != std::string::npos);
|
|
assert(err(store, "allowlist-add", R"({"kind":"rule","value":"X","rule":"Y"})").find("rule") != std::string::npos);
|
|
assert(err(store, "allowlist-add", R"({"kind":"other","value":"x"})").find("kind") != std::string::npos);
|
|
assert(err(store, "allowlist-add", R"({"kind":"rule","value":"X","extra":1})").find("unknown field") != std::string::npos);
|
|
assert(err(store, "allowlist-add", R"({"kind":"path","value":"/a\nb"})").find("control") != std::string::npos);
|
|
|
|
assert(err(store, "feed-add", R"({"id":"Bad_ID","type":"url","url":"https://x/y.yar"})").find("id") != std::string::npos);
|
|
assert(err(store, "feed-add", R"({"id":"x","type":"url","url":"http://x/y.yar"})").find("https") != std::string::npos);
|
|
assert(err(store, "feed-add", R"({"id":"x","type":"url","url":"file:///etc/passwd"})").find("https") != std::string::npos);
|
|
assert(err(store, "feed-add", R"({"id":"x","type":"github-release","repo":"../x","asset":"a.zip"})").find("repo") != std::string::npos);
|
|
assert(err(store, "feed-add", R"({"id":"x","type":"github-release","repo":"o/r","asset":"../a.zip"})").find("asset") != std::string::npos);
|
|
assert(err(store, "feed-add", R"({"id":"x","type":"github-release","repo":"o/r","asset":"a.zip","url":"https://x"})").find("only valid") != std::string::npos);
|
|
assert(err(store, "feed-add", R"({"id":"x","type":"ftp"})").find("type") != std::string::npos);
|
|
assert(err(store, "feed-add", R"({"id":"yara-forge-core","type":"url","url":"https://x/y.yar"})").find("already exists") != std::string::npos);
|
|
assert(err(store, "feed-add", R"({"id":"bad-sev","kind":"hashes","type":"url","url":"https://x/h.txt","max_severity":"urgent"})").find("max_severity") != std::string::npos);
|
|
// IOC feed trust: community hash feeds resolve to advisory "high"; unknown feeds too.
|
|
assert(store.ioc_feed_severity("malware-hashes") == "high");
|
|
assert(store.ioc_feed_severity("no-such-feed") == "high");
|
|
assert(err(store, "feed-set-enabled", R"({"id":"yara-forge-core","enabled":"no"})").find("boolean") != std::string::npos);
|
|
assert(err(store, "feed-remove", R"({"id":"missing"})").find("no feed") != std::string::npos);
|
|
assert(err(store, "allowlist-remove", R"({"id":"missing"})").find("no allowlist") != std::string::npos);
|
|
|
|
// Duplicates are rejected.
|
|
assert(ok(store, "allowlist-add", R"({"kind":"rule","value":"Noisy"})"));
|
|
assert(err(store, "allowlist-add", R"({"kind":"rule","value":"Noisy"})").find("identical") != std::string::npos);
|
|
|
|
// Nothing invalid was persisted.
|
|
PolicyStore reloaded((root / "policy.json").string());
|
|
assert(reloaded.load());
|
|
assert(reloaded.snapshot().allowlist.size() == 1);
|
|
assert(reloaded.snapshot().feeds.size() == strawberry::default_feeds().size());
|
|
|
|
fs::remove_all(root);
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_feed_trust_severity() {
|
|
std::cout << "Test feed trust severity:" << std::endl;
|
|
const auto root = make_root();
|
|
PolicyStore store((root / "policy.json").string());
|
|
assert(store.load());
|
|
// A curated hash feed opts in to "critical" (forces prevention).
|
|
assert(ok(store, "feed-add",
|
|
R"({"id":"curated","kind":"hashes","type":"url","url":"https://example.com/h.txt","max_severity":"critical"})"));
|
|
assert(store.ioc_feed_severity("curated") == "critical");
|
|
// Persists and round-trips across reload.
|
|
PolicyStore reloaded((root / "policy.json").string());
|
|
assert(reloaded.load());
|
|
assert(reloaded.ioc_feed_severity("curated") == "critical");
|
|
bool found = false;
|
|
for (const auto& f : reloaded.snapshot().feeds) {
|
|
if (f.id == "curated") {
|
|
found = true;
|
|
assert(f.max_severity == "critical");
|
|
}
|
|
}
|
|
assert(found);
|
|
fs::remove_all(root);
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_matching_and_mutations() {
|
|
std::cout << "Test matching and mutations:" << std::endl;
|
|
const auto root = make_root();
|
|
PolicyStore store((root / "policy.json").string());
|
|
assert(store.load());
|
|
|
|
std::string reply;
|
|
assert(ok(store, "allowlist-add", R"({"kind":"sha256","value":")" + kHash + R"("})", &reply));
|
|
assert(ok(store, "allowlist-add", R"({"kind":"path","value":"/usr/bin/socat","rule":"Hacktool_Socat"})"));
|
|
assert(ok(store, "allowlist-add", R"({"kind":"path","value":"/opt/vendor/"})"));
|
|
assert(ok(store, "allowlist-add", R"({"kind":"rule","value":"NoisyRule"})"));
|
|
|
|
assert(store.match(kHash, "/anywhere", "AnyRule")); // hash: all rules
|
|
assert(!store.match(std::string(64, 'b'), "/anywhere", "AnyRule"));
|
|
assert(store.match("", "/usr/bin/socat", "Hacktool_Socat")); // path + rule
|
|
assert(!store.match("", "/usr/bin/socat", "OtherRule"));
|
|
assert(!store.match("", "/usr/bin/socat2", "Hacktool_Socat")); // exact path only
|
|
assert(store.match("", "/opt/vendor/bin/tool", "Anything")); // directory prefix
|
|
assert(!store.match("", "/opt/vendor2/tool", "Anything"));
|
|
assert(store.match("", "/tmp/x", "NoisyRule")); // rule everywhere
|
|
assert(!store.match("", "", "Unrelated"));
|
|
|
|
// Remove by id.
|
|
const auto id = store.snapshot().allowlist[0].id;
|
|
assert(ok(store, "allowlist-remove", R"({"id":")" + id + R"("})"));
|
|
assert(!store.match(kHash, "/anywhere", "AnyRule"));
|
|
|
|
// Feed toggling.
|
|
assert(ok(store, "feed-set-enabled", R"({"id":"yara-forge-core","enabled":false})"));
|
|
assert(!store.snapshot().feeds[0].enabled);
|
|
for (const auto& feed : strawberry::default_feeds()) {
|
|
assert(ok(store, "feed-remove", R"({"id":")" + feed.id + R"("})"));
|
|
}
|
|
assert(store.snapshot().feeds.empty());
|
|
assert(store.snapshot().removed_defaults.size() == strawberry::default_feeds().size());
|
|
|
|
// Limits.
|
|
for (std::size_t i = 0; i < PolicyStore::kMaxFeeds; ++i) {
|
|
assert(ok(store, "feed-add", R"({"id":"f)" + std::to_string(i) + R"(","type":"url","url":"https://e.com/r.yar"})"));
|
|
}
|
|
assert(err(store, "feed-add", R"({"id":"one-more","type":"url","url":"https://e.com/r.yar"})").find("too many") != std::string::npos);
|
|
assert(store.snapshot().history.size() <= PolicyStore::kMaxHistory);
|
|
|
|
fs::remove_all(root);
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_v2_migration_and_list_feeds() {
|
|
std::cout << "Test v1 -> v2 migration and list feed validation:" << std::endl;
|
|
const auto root = make_root();
|
|
const auto path = (root / "policy.json").string();
|
|
// A v1 policy: core feed only, single "url" field style, user removed nothing.
|
|
{
|
|
std::ofstream(path) << R"({"version":1,"feeds":[{"id":"yara-forge-core","type":"github-release",)"
|
|
R"("repo":"YARAHQ/yara-forge","asset":"yara-forge-rules-core.zip","enabled":true},)"
|
|
R"({"id":"mine","type":"url","url":"https://example.com/a.yar","enabled":true}],"allowlist":[],"history":[]})";
|
|
}
|
|
PolicyStore store(path);
|
|
assert(store.load());
|
|
auto snapshot = store.snapshot();
|
|
assert(snapshot.version == 2);
|
|
assert(snapshot.feeds.size() == strawberry::default_feeds().size() + 1);
|
|
assert(snapshot.feeds[1].id == "mine" && snapshot.feeds[1].urls.size() == 1 && snapshot.feeds[1].kind == "yara");
|
|
|
|
// Removing a default is remembered; a re-migration would not re-add it.
|
|
assert(ok(store, "feed-remove", R"({"id":"cert-pl-domains"})"));
|
|
PolicyStore reloaded(path);
|
|
assert(reloaded.load());
|
|
for (const auto& feed : reloaded.snapshot().feeds) {
|
|
assert(feed.id != "cert-pl-domains");
|
|
}
|
|
|
|
// List feed payloads.
|
|
assert(ok(store, "feed-add", R"({"id":"my-ips","kind":"ips","type":"url","urls":["https://a.example/1.txt","https://a.example/2.txt"]})"));
|
|
assert(err(store, "feed-add", R"({"id":"x1","kind":"urls","type":"url","url":"https://a/b"})").find("kind") != std::string::npos);
|
|
assert(err(store, "feed-add", R"({"id":"x2","kind":"ips","type":"url","url":"https://a/b","urls":["https://a/c"]})").find("exactly one") != std::string::npos);
|
|
assert(err(store, "feed-add", R"({"id":"x3","kind":"ips","type":"url","urls":["https://a/b","https://a/c"],"sha256":")" + kHash + R"("})").find("exactly one url") != std::string::npos);
|
|
assert(err(store, "feed-add", R"({"id":"x4","kind":"ips","type":"url","urls":["https://a/b c"]})").find("https") != std::string::npos);
|
|
assert(err(store, "feed-add", R"({"id":"x5","kind":"hashes","type":"url","url":"https://a/b","auth_header_file":"key.txt"})").find("absolute") != std::string::npos);
|
|
assert(ok(store, "feed-add", R"({"id":"keyed","kind":"hashes","type":"url","url":"https://a/b","auth_header_file":"/etc/strawberry/abuse.key"})"));
|
|
|
|
// Only yara feeds are rule roots.
|
|
fs::create_directories(root / "feeds" / "my-ips" / "current");
|
|
fs::create_directories(root / "feeds" / "mine" / "current");
|
|
const auto roots = strawberry::installed_feed_roots(store.snapshot().feeds, (root / "feeds").string());
|
|
assert(roots.size() == 1 && roots[0].first == "mine");
|
|
assert(strawberry::installed_feed_roots(store.snapshot().feeds, (root / "feeds").string(), "ips").size() == 1);
|
|
|
|
fs::remove_all(root);
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_paths_follow_database() {
|
|
std::cout << "Test policy/feeds paths default next to the database:" << std::endl;
|
|
strawberry::Config config;
|
|
config.db_path = "/var/lib/strawberry/database.db";
|
|
assert(strawberry::policy_path_for(config) == "/var/lib/strawberry/policy.json");
|
|
assert(strawberry::feeds_dir_for(config) == "/var/lib/strawberry/feeds");
|
|
config.policy_path = "/srv/p.json";
|
|
config.feeds_dir = "/srv/feeds";
|
|
assert(strawberry::policy_path_for(config) == "/srv/p.json");
|
|
assert(strawberry::feeds_dir_for(config) == "/srv/feeds");
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_status_parsing() {
|
|
std::cout << "Test status parsing into scanner health:" << std::endl;
|
|
const auto snapshot = strawberry::snapshot_from_status_json(R"({
|
|
"now_ns": 1000,
|
|
"monitor": {"enabled": true, "backend": "bpf", "execveat": true, "events_dropped": 2},
|
|
"rules": {"loaded": true, "last_error": ""},
|
|
"pipeline": {"scan_errors": 0},
|
|
"feeds": {"configured": [{"id": "core", "enabled": true}, {"id": "new", "enabled": true}, {"id": "off", "enabled": false}],
|
|
"state": {"feeds": [{"id": "core", "version": "v1", "updated_at": 900, "error": "HTTP 500"}]}}
|
|
})");
|
|
assert(snapshot.reachable && snapshot.backend == "bpf" && snapshot.events_dropped == 2);
|
|
assert(snapshot.enabled_feeds.size() == 2);
|
|
assert(snapshot.feeds_not_installed.size() == 1 && snapshot.feeds_not_installed[0] == "new");
|
|
assert(snapshot.feed_errors.size() == 1 && snapshot.feed_errors[0] == "core: HTTP 500");
|
|
assert(snapshot.newest_feed_update_ns == 900);
|
|
assert(!strawberry::snapshot_from_status_json("garbage").reachable);
|
|
// Wrong-typed fields fall back to defaults instead of throwing (fuzzing).
|
|
const auto odd = strawberry::snapshot_from_status_json(
|
|
R"({"monitor":{"enabled":"yes","backend":5,"events_dropped":-3},"resources":{"level":1,"deferring":"x"},)"
|
|
R"("timings":[],"feeds":{"configured":[{"id":2,"enabled":1}],"state":"none"}})");
|
|
assert(odd.reachable && !odd.monitor_enabled && odd.backend.empty() && odd.events_dropped == 0);
|
|
assert(odd.resource_level.empty() && odd.deferring.empty() && odd.enabled_feeds.empty());
|
|
|
|
// Resource governor problems.
|
|
const auto busy = strawberry::snapshot_from_status_json(R"({
|
|
"monitor": {"enabled": true, "backend": "bpf", "execveat": true},
|
|
"rules": {"loaded": true},
|
|
"resources": {"profile": "balanced", "level": "critical", "host_busy": true,
|
|
"deferring": ["inventory", "memory_scan"], "longest_deferral_s": 420},
|
|
"timings": {"exec_decision": {"count": 50, "p95_ms": 511.0}}
|
|
})");
|
|
assert(busy.scan_profile == "balanced" && busy.resource_level == "critical" && busy.host_busy);
|
|
assert(busy.deferring.size() == 2 && busy.exec_decisions == 50);
|
|
const auto problems = strawberry::scanner_problems(busy, 0);
|
|
const auto has = [&](const char* text) {
|
|
return std::any_of(problems.begin(), problems.end(), [&](const std::string& p) { return p.find(text) != std::string::npos; });
|
|
};
|
|
assert(has("over its resource budget"));
|
|
assert(has("inventory, memory_scan deferred for 7 min"));
|
|
assert(has("exec checks (p95 511 ms)"));
|
|
const auto calm = strawberry::snapshot_from_status_json(R"({
|
|
"monitor": {"enabled": true, "backend": "bpf", "execveat": true}, "rules": {"loaded": true},
|
|
"resources": {"level": "normal", "deferring": ["inventory"], "longest_deferral_s": 30},
|
|
"timings": {"exec_decision": {"count": 5, "p95_ms": 900.0}}
|
|
})");
|
|
assert(strawberry::scanner_problems(calm, 0).empty()); // short deferral, too few samples
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_read_only_load_never_seeds_or_migrates() {
|
|
const auto root = make_root();
|
|
const auto path = root / "policy.json";
|
|
PolicyStore store(path.string());
|
|
std::string error;
|
|
assert(!store.load_read_only(&error) && !error.empty());
|
|
assert(!fs::exists(path));
|
|
const std::string legacy = R"({"version":1,"feeds":[],"allowlist":[{"id":"fixture","kind":"rule","value":"SyntheticDemoRule"}]})";
|
|
{ std::ofstream output(path); output << legacy; assert(output.good()); }
|
|
assert(store.load_read_only(&error));
|
|
const auto snapshot = store.snapshot();
|
|
assert(snapshot.version == 1 && snapshot.feeds.empty() && snapshot.history.empty());
|
|
assert(snapshot.allowlist.size() == 1 && store.match("", "", "SyntheticDemoRule").has_value());
|
|
assert(strawberry::read_small_file(path.string()).value() == legacy);
|
|
{ std::ofstream output(path); output << "{invalid"; assert(output.good()); }
|
|
assert(!store.load_read_only(&error));
|
|
assert(strawberry::read_small_file(path.string()).value() == "{invalid");
|
|
assert(store.snapshot().allowlist.size() == 1); // failed load does not discard last valid in-memory policy
|
|
const auto directory = root / "directory";
|
|
fs::create_directory(directory);
|
|
PolicyStore not_a_file(directory.string());
|
|
assert(!not_a_file.load_read_only(&error));
|
|
std::error_code ec; fs::remove_all(root, ec);
|
|
std::cout << "Test read_only_load_never_seeds_or_migrates: PASSED" << std::endl;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
std::cout << "Running policy tests..." << std::endl << std::endl;
|
|
test_read_only_load_never_seeds_or_migrates();
|
|
test_seed_and_round_trip();
|
|
test_validation();
|
|
test_feed_trust_severity();
|
|
test_matching_and_mutations();
|
|
test_v2_migration_and_list_feeds();
|
|
test_paths_follow_database();
|
|
test_status_parsing();
|
|
std::cout << std::endl << "All tests passed!" << std::endl;
|
|
return 0;
|
|
}
|