PolicyStore (allowlist, protection settings, operator actions), the rule/feed updater with HTTPS-only fetches and a systemd timer, IOC hash/domain/network sets, OpenSnitch blocklist containment, the root control socket (SCM_RIGHTS credentials, verb allowlist) and scanner health snapshots. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
405 lines
17 KiB
C++
405 lines
17 KiB
C++
// Rule updater tests with an injected fetcher (no network). Archives are
|
|
// built in memory with libarchive, including hostile entries.
|
|
|
|
#include "strawberry/hash_utils.hpp"
|
|
#include "strawberry/policy.hpp"
|
|
#include "strawberry/rule_updater.hpp"
|
|
#include "strawberry/yara_scanner.hpp"
|
|
|
|
#include <archive.h>
|
|
#include <archive_entry.h>
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <cassert>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <unistd.h>
|
|
|
|
namespace fs = std::filesystem;
|
|
using json = nlohmann::json;
|
|
using namespace strawberry;
|
|
|
|
namespace {
|
|
|
|
struct Entry {
|
|
std::string path;
|
|
std::string content;
|
|
bool symlink = false;
|
|
};
|
|
|
|
std::string make_zip(const std::vector<Entry>& entries) {
|
|
std::vector<char> buffer(1 << 20);
|
|
size_t used = 0;
|
|
archive* a = archive_write_new();
|
|
archive_write_set_format_zip(a);
|
|
assert(archive_write_open_memory(a, buffer.data(), buffer.size(), &used) == ARCHIVE_OK);
|
|
for (const auto& e : entries) {
|
|
archive_entry* entry = archive_entry_new();
|
|
archive_entry_set_pathname(entry, e.path.c_str());
|
|
if (e.symlink) {
|
|
archive_entry_set_filetype(entry, AE_IFLNK);
|
|
archive_entry_set_symlink(entry, e.content.c_str());
|
|
archive_entry_set_size(entry, 0);
|
|
} else {
|
|
archive_entry_set_filetype(entry, AE_IFREG);
|
|
archive_entry_set_size(entry, static_cast<la_int64_t>(e.content.size()));
|
|
}
|
|
archive_entry_set_perm(entry, 0644);
|
|
archive_write_header(a, entry);
|
|
if (!e.symlink) {
|
|
archive_write_data(a, e.content.data(), e.content.size());
|
|
}
|
|
archive_entry_free(entry);
|
|
}
|
|
archive_write_close(a);
|
|
archive_write_free(a);
|
|
return std::string(buffer.data(), used);
|
|
}
|
|
|
|
std::string sha(const std::string& data) {
|
|
return HashUtils::sha256_data(reinterpret_cast<const unsigned char*>(data.data()), data.size());
|
|
}
|
|
|
|
class FakeFetcher : public Fetcher {
|
|
public:
|
|
std::map<std::string, FetchResult> responses;
|
|
std::map<std::string, int> calls;
|
|
|
|
FetchResult get(const std::string& url, std::size_t max_bytes, const std::vector<std::string>&) override {
|
|
++calls[url];
|
|
auto it = responses.find(url);
|
|
if (it == responses.end()) {
|
|
return {false, 404, "", "HTTP 404", url};
|
|
}
|
|
if (it->second.body.size() > max_bytes) {
|
|
return {false, 200, "", "too large", url};
|
|
}
|
|
auto result = it->second;
|
|
if (result.effective_url.empty()) {
|
|
result.effective_url = url;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void release(const std::string& repo, const std::string& tag, const std::string& asset,
|
|
const std::string& body, const std::string& digest) {
|
|
const std::string download = "https://github.com/" + repo + "/releases/download/" + tag + "/" + asset;
|
|
json release = {{"tag_name", tag},
|
|
{"assets", {{{"name", asset}, {"browser_download_url", download},
|
|
{"size", body.size()}, {"digest", "sha256:" + digest}}}}};
|
|
responses["https://api.github.com/repos/" + repo + "/releases/latest"] = {true, 200, release.dump(), ""};
|
|
responses[download] = {true, 200, body, ""};
|
|
}
|
|
};
|
|
|
|
const std::string kGoodRules =
|
|
"rule Feed_Alpha { strings: $a = \"alpha-marker\" condition: $a }\n"
|
|
"private rule Feed_Helper { condition: true }\n";
|
|
const std::string kOtherRules = "rule Feed_Beta { strings: $b = \"beta-marker\" condition: $b }\n";
|
|
|
|
fs::path make_root() {
|
|
const auto root = fs::temp_directory_path() / ("strawberry_updater_test_" + std::to_string(::getpid()));
|
|
std::error_code ec;
|
|
fs::remove_all(root, ec);
|
|
fs::create_directories(root / "local");
|
|
// Local rule with the same name as a feed rule: namespaces must keep them apart.
|
|
std::ofstream(root / "local" / "local.yar") << "rule Feed_Alpha { condition: false }\n";
|
|
return root;
|
|
}
|
|
|
|
FeedSpec feed() {
|
|
FeedSpec f;
|
|
f.id = "test-feed";
|
|
f.type = "github-release";
|
|
f.repo = "owner/rules";
|
|
f.asset = "rules.zip";
|
|
return f;
|
|
}
|
|
|
|
json state_of(const fs::path& feeds_dir) {
|
|
return json::parse(read_feed_state(feeds_dir.string()))["feeds"][0];
|
|
}
|
|
|
|
void test_install_and_skip() {
|
|
std::cout << "Test install, flattening and unchanged skip:" << std::endl;
|
|
const auto root = make_root();
|
|
UpdaterOptions options;
|
|
options.feeds_dir = (root / "feeds").string();
|
|
options.local_rules_path = (root / "local").string();
|
|
|
|
const auto zip = make_zip({
|
|
{"packages/core/good.yar", kGoodRules},
|
|
{"../../escape.yar", kOtherRules}, // traversal: flattened into the version dir
|
|
{"evil-link.yar", "/etc/passwd", true}, // symlink: ignored
|
|
{"README.txt", "not rules"}, // not a rule file: ignored
|
|
});
|
|
FakeFetcher fetcher;
|
|
fetcher.release("owner/rules", "v1", "rules.zip", zip, sha(zip));
|
|
|
|
auto report = update_feeds({feed()}, options, fetcher);
|
|
assert(report.any_changed);
|
|
assert(report.feeds.size() == 1 && report.feeds[0].ok && report.feeds[0].error.empty());
|
|
const auto current = root / "feeds" / "test-feed" / "current";
|
|
assert(fs::is_symlink(current) && fs::read_symlink(current) == "v1");
|
|
assert(fs::exists(current / "good.yar"));
|
|
assert(fs::exists(current / "escape.yar"));
|
|
assert(!fs::exists(root / "escape.yar") && !fs::exists(root / "feeds" / "escape.yar"));
|
|
assert(!fs::exists(current / "evil-link.yar"));
|
|
assert(!fs::exists(current / "README.txt"));
|
|
assert(report.feeds[0].rules == 3);
|
|
|
|
auto state = state_of(root / "feeds");
|
|
assert(state["version"] == "v1" && state["ok"] == true && state["sha256"] == sha(zip));
|
|
|
|
// Compiles with the local rule of the same name (separate namespaces).
|
|
std::size_t count = 0;
|
|
std::string error;
|
|
assert(YaraScanner::check_rules({{options.local_rules_path, ""}, {current.string(), "test-feed"}}, &error, &count));
|
|
assert(count == 4);
|
|
|
|
// Unchanged digest: no download.
|
|
const auto download = "https://github.com/owner/rules/releases/download/v1/rules.zip";
|
|
assert(fetcher.calls[download] == 1);
|
|
report = update_feeds({feed()}, options, fetcher);
|
|
assert(!report.any_changed && report.feeds[0].ok);
|
|
assert(fetcher.calls[download] == 1);
|
|
|
|
fs::remove_all(root);
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_rejections_keep_previous_version() {
|
|
std::cout << "Test digest mismatch / broken rules keep the previous version:" << std::endl;
|
|
const auto root = make_root();
|
|
UpdaterOptions options;
|
|
options.feeds_dir = (root / "feeds").string();
|
|
options.local_rules_path = (root / "local").string();
|
|
const auto current = root / "feeds" / "test-feed" / "current";
|
|
|
|
FakeFetcher fetcher;
|
|
const auto v1 = make_zip({{"good.yar", kGoodRules}});
|
|
fetcher.release("owner/rules", "v1", "rules.zip", v1, sha(v1));
|
|
assert(update_feeds({feed()}, options, fetcher).feeds[0].ok);
|
|
|
|
// v2 with a digest that does not match the bytes.
|
|
const auto v2 = make_zip({{"good.yar", kOtherRules}});
|
|
fetcher.release("owner/rules", "v2", "rules.zip", v2, std::string(64, '0'));
|
|
auto report = update_feeds({feed()}, options, fetcher);
|
|
assert(report.feeds[0].error.find("sha256 mismatch") != std::string::npos);
|
|
assert(report.feeds[0].ok); // still has a working version
|
|
assert(fs::read_symlink(current) == "v1");
|
|
|
|
// v3 that fails to compile.
|
|
const auto v3 = make_zip({{"broken.yar", "rule Broken { condition: }\n"}});
|
|
fetcher.release("owner/rules", "v3", "rules.zip", v3, sha(v3));
|
|
report = update_feeds({feed()}, options, fetcher);
|
|
assert(report.feeds[0].error.find("failed to compile") != std::string::npos);
|
|
assert(fs::read_symlink(current) == "v1");
|
|
assert(!fs::exists(root / "feeds" / "test-feed" / "v3"));
|
|
|
|
// Release without a published digest is refused.
|
|
fetcher.responses["https://api.github.com/repos/owner/rules/releases/latest"] = {
|
|
true, 200, json({{"tag_name", "v4"}, {"assets", {{{"name", "rules.zip"}, {"browser_download_url", "https://x/r.zip"}}}}}).dump(), ""};
|
|
report = update_feeds({feed()}, options, fetcher);
|
|
assert(report.feeds[0].error.find("no published sha256") != std::string::npos);
|
|
|
|
// GitHub rate limit with no web fallback available: reported clearly.
|
|
fetcher.responses["https://api.github.com/repos/owner/rules/releases/latest"] = {false, 403, "API rate limit exceeded", "HTTP 403"};
|
|
report = update_feeds({feed()}, options, fetcher);
|
|
assert(report.feeds[0].error.find("rate limit") != std::string::npos);
|
|
assert(fs::read_symlink(current) == "v1");
|
|
|
|
// Web fallback: tag via redirect, digest from the expanded assets page.
|
|
const auto v6 = make_zip({{"good.yar", kGoodRules + "// v6\n"}});
|
|
const std::string v6_url = "https://github.com/owner/rules/releases/download/v6/rules.zip";
|
|
fetcher.responses["https://github.com/owner/rules/releases/latest"] = {true, 200, "<html>", "", "https://github.com/owner/rules/releases/tag/v6"};
|
|
fetcher.responses["https://github.com/owner/rules/releases/expanded_assets/v6"] = {true, 200,
|
|
"<a href=\"/owner/rules/releases/download/v6/other.zip\">x</a> sha256:" + std::string(64, '1')
|
|
+ " <a href=\"/owner/rules/releases/download/v6/rules.zip\">rules.zip</a> <span>sha256:" + sha(v6) + "</span>",
|
|
""};
|
|
fetcher.responses[v6_url] = {true, 200, v6, ""};
|
|
report = update_feeds({feed()}, options, fetcher);
|
|
assert(report.feeds[0].error.empty() && report.any_changed);
|
|
assert(fs::read_symlink(current) == "v6");
|
|
|
|
// Web fallback with a tampered asset: digest mismatch, nothing installed.
|
|
const auto v7 = make_zip({{"good.yar", kOtherRules}});
|
|
fetcher.responses["https://github.com/owner/rules/releases/latest"].effective_url = "https://github.com/owner/rules/releases/tag/v7";
|
|
fetcher.responses["https://github.com/owner/rules/releases/expanded_assets/v7"] = {true, 200,
|
|
"<a href=\"/owner/rules/releases/download/v7/rules.zip\">rules.zip</a> sha256:" + std::string(64, '2'), ""};
|
|
fetcher.responses["https://github.com/owner/rules/releases/download/v7/rules.zip"] = {true, 200, v7, ""};
|
|
report = update_feeds({feed()}, options, fetcher);
|
|
assert(report.feeds[0].error.find("sha256 mismatch") != std::string::npos);
|
|
assert(fs::read_symlink(current) == "v6");
|
|
|
|
// A good v5 via the API replaces v6 and keeps v6 as the previous version.
|
|
const auto v5 = make_zip({{"good.yar", kOtherRules}, {"extra.yar", "rule Extra { condition: false }\n"}});
|
|
fetcher.release("owner/rules", "v5", "rules.zip", v5, sha(v5));
|
|
report = update_feeds({feed()}, options, fetcher);
|
|
assert(report.any_changed && fs::read_symlink(current) == "v5");
|
|
assert(fs::exists(root / "feeds" / "test-feed" / "v6"));
|
|
|
|
fs::remove_all(root);
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_url_feed_and_cleanup() {
|
|
std::cout << "Test URL feed (pinned digest), disabled and removed feeds:" << std::endl;
|
|
const auto root = make_root();
|
|
UpdaterOptions options;
|
|
options.feeds_dir = (root / "feeds").string();
|
|
options.local_rules_path = (root / "local").string();
|
|
|
|
FeedSpec url_feed;
|
|
url_feed.id = "plain";
|
|
url_feed.type = "url";
|
|
url_feed.urls = {"https://example.com/my.yar"};
|
|
url_feed.sha256 = sha(kOtherRules);
|
|
|
|
FakeFetcher fetcher;
|
|
fetcher.responses[url_feed.urls.front()] = {true, 200, kOtherRules, ""};
|
|
auto report = update_feeds({url_feed}, options, fetcher);
|
|
assert(report.feeds[0].ok && report.any_changed);
|
|
assert(fs::exists(root / "feeds" / "plain" / "current" / "my.yar"));
|
|
|
|
// Pinned digest mismatch.
|
|
url_feed.sha256 = std::string(64, 'f');
|
|
report = update_feeds({url_feed}, options, fetcher);
|
|
assert(report.feeds[0].error.find("sha256 mismatch") != std::string::npos);
|
|
|
|
// Disabled feeds are not fetched and are not active rule roots.
|
|
url_feed.enabled = false;
|
|
fetcher.calls.clear();
|
|
report = update_feeds({url_feed}, options, fetcher);
|
|
assert(fetcher.calls.empty());
|
|
assert(report.feeds[0].error == "disabled");
|
|
assert(installed_feed_roots({url_feed}, options.feeds_dir).empty());
|
|
|
|
// A feed removed from policy has its directory cleaned up.
|
|
report = update_feeds({}, options, fetcher);
|
|
assert(!fs::exists(root / "feeds" / "plain"));
|
|
|
|
fs::remove_all(root);
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_helpers() {
|
|
std::cout << "Test helpers:" << std::endl;
|
|
assert(sanitize_rule_file_name("../../etc/passwd.yar") == "_.._etc_passwd.yar");
|
|
assert(sanitize_rule_file_name(".hidden.yar") == "hidden.yar");
|
|
assert(sanitize_rule_file_name("a b.yara") == "a_b.yara");
|
|
assert(sanitize_rule_file_name("noext") == "noext.yar");
|
|
assert(count_rule_declarations("rule A { condition: true }\nprivate rule B { condition: true }\n// rule C\n") == 2);
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_ioc_list_parsing() {
|
|
std::cout << "Test IOC list parsing:" << std::endl;
|
|
std::vector<std::string> out;
|
|
const std::string hashes =
|
|
"# comment\n" + std::string(64, 'A') + "\n" + "sha256:" + std::string(64, 'b') + " extra\n"
|
|
"nothex" + std::string(58, 'z') + "\n\n" + std::string(63, 'c') + "\n";
|
|
auto rejected = parse_ioc_list("hashes", hashes, 100, out);
|
|
assert(out.size() == 2 && out[0] == std::string(64, 'a') && out[1] == std::string(64, 'b'));
|
|
assert(rejected == 2);
|
|
|
|
out.clear();
|
|
parse_ioc_list("domains",
|
|
"0.0.0.0 Evil.Example.COM\n127.0.0.1 bad.test.\nplain-domain.org # note\n*.wild.net\n"
|
|
"localhost\n1.2.3.4\n-bad-.com\nfoo..bar.com\nok_under.score.io\n",
|
|
100, out);
|
|
const std::vector<std::string> domains{"evil.example.com", "bad.test", "plain-domain.org", "wild.net", "ok_under.score.io"};
|
|
assert(out == domains);
|
|
|
|
out.clear();
|
|
parse_ioc_list("ips", "1.2.3.4\n 5.6.7.8\t12\n::1\n999.1.1.1\n2001:DB8::1\n", 100, out);
|
|
assert((out == std::vector<std::string>{"1.2.3.4", "5.6.7.8", "::1", "2001:db8::1"}));
|
|
|
|
out.clear();
|
|
// Over-broad prefixes (wider than /8 IPv4, /16 IPv6) are rejected: a feed
|
|
// serving 0.0.0.0/0 must not block or flag the whole internet.
|
|
parse_ioc_list("nets", "1.10.16.0/20 ; SBL256894\n10.0.0.0/33\n2001:db8::/32\n8.8.8.8\nbad/8\n"
|
|
"0.0.0.0/0\n128.0.0.0/1\n12.0.0.0/7\n13.0.0.0/8\n::/0\n2000::/3\n2001::/16\n", 100, out);
|
|
assert((out == std::vector<std::string>{"1.10.16.0/20", "2001:db8::/32", "8.8.8.8/32", "13.0.0.0/8", "2001::/16"}));
|
|
|
|
// Cap.
|
|
out.clear();
|
|
parse_ioc_list("ips", "1.1.1.1\n2.2.2.2\n3.3.3.3\n", 2, out);
|
|
assert(out.size() == 2);
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
void test_list_feed_update_and_shrink_guard() {
|
|
std::cout << "Test list feed install and shrink guard:" << std::endl;
|
|
const auto root = make_root();
|
|
UpdaterOptions options;
|
|
options.feeds_dir = (root / "feeds").string();
|
|
options.local_rules_path = (root / "local").string();
|
|
|
|
FeedSpec feed;
|
|
feed.id = "ips-feed";
|
|
feed.kind = "ips";
|
|
feed.type = "url";
|
|
feed.urls = {"https://example.com/a.txt", "https://example.com/b.txt"};
|
|
|
|
std::string big_a;
|
|
std::string big_b;
|
|
for (int i = 0; i < 150; ++i) {
|
|
big_a += "10.0." + std::to_string(i) + ".1\n";
|
|
big_b += "10.1." + std::to_string(i) + ".1\n";
|
|
}
|
|
big_b += "10.0.0.1\n# duplicate above\n";
|
|
FakeFetcher fetcher;
|
|
fetcher.responses[feed.urls[0]] = {true, 200, big_a, ""};
|
|
fetcher.responses[feed.urls[1]] = {true, 200, big_b, ""};
|
|
|
|
auto report = update_feeds({feed}, options, fetcher);
|
|
assert(report.any_changed && report.feeds[0].ok && report.feeds[0].rules == 300);
|
|
const auto list = root / "feeds" / "ips-feed" / "current" / "list.txt";
|
|
assert(fs::exists(list));
|
|
std::ifstream in(list);
|
|
std::string first;
|
|
std::getline(in, first);
|
|
assert(first == "10.0.0.1"); // sorted, deduped
|
|
|
|
// Suspiciously small replacement is refused; previous list stays.
|
|
fetcher.responses[feed.urls[0]] = {true, 200, "10.9.9.9\n", ""};
|
|
fetcher.responses[feed.urls[1]] = {true, 200, "", ""};
|
|
report = update_feeds({feed}, options, fetcher);
|
|
assert(report.feeds[0].error.find("truncated") != std::string::npos);
|
|
assert(!report.any_changed);
|
|
std::ifstream again(list);
|
|
std::getline(again, first);
|
|
assert(first == "10.0.0.1");
|
|
|
|
// Only-invalid content is an error, nothing installed for a new feed.
|
|
FeedSpec junk = feed;
|
|
junk.id = "junk";
|
|
junk.urls = {"https://example.com/junk"};
|
|
fetcher.responses["https://example.com/junk"] = {true, 200, "<html>error</html>\n", ""};
|
|
report = update_feeds({junk}, options, fetcher);
|
|
assert(report.feeds[0].error.find("no valid") != std::string::npos && !report.feeds[0].ok);
|
|
assert(!fs::exists(root / "feeds" / "junk" / "current"));
|
|
|
|
fs::remove_all(root);
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
std::cout << "Running rule updater tests..." << std::endl << std::endl;
|
|
test_helpers();
|
|
test_install_and_skip();
|
|
test_rejections_keep_previous_version();
|
|
test_url_feed_and_cleanup();
|
|
test_ioc_list_parsing();
|
|
test_list_feed_update_and_shrink_guard();
|
|
std::cout << std::endl << "All tests passed!" << std::endl;
|
|
return 0;
|
|
}
|