strawberry/tests/test_opensnitch_events.cpp
trvon c77bc79dc7
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / macos (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
Privacy: macOS tracker source via Little Snitch log-traffic (WS2 macOS)
Completes the tracker-attribution transport on macOS. There is no network
sandbox to work around there, so the daemon (root) reads Little Snitch's traffic
stream directly and feeds the same on_opensnitch_connection consumer — no relay
or ingest socket (those are the Linux sandbox answer).

- LittleSnitchParser (cross-platform, unit-tested): parses `littlesnitch
  log-traffic` CSV via a header-name->index map (tolerates column reordering),
  yielding the shared redacted OpenSnitchConnection for outbound rows only
  (program, uid, host/ip/port, protocol 6->tcp/17->udp). Little Snitch's CSV has
  no argv/env, so redaction is structural. split_csv_line handles quoted fields.
- LittleSnitchSource (macOS only): posix_spawns `littlesnitch log-traffic
  --stream`, reads the pipe on a guarded thread, parses and calls back; stop()
  SIGTERMs the CLI (EOF -> loop ends) then reaps it.
- Daemon: start_tracker_attribution gains an __APPLE__ branch that starts the
  source; shutdown/status handle it. New config littlesnitch_cli (default CLI
  path). status privacy.listener source "little-snitch".
- docs: macOS section rewritten (needs Little Snitch "Allow access via Terminal").

Test E10 covers the parser (header, outbound/inbound, deny, quoted exe, no-dest).
Linux suite 51/51; analyzer + clang-tidy clean (Linux view). The __APPLE__ code
compiles/validates on the Mac next (ssh); live spawn needs root + LS Terminal access.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-28 18:54:27 -06:00

304 lines
14 KiB
C++

// OpenSnitch connection events and tracker contacts (docs/tracker-attribution.md).
//
// E1 an event yields pid, uid, path, destination and action only (the
// struct has no field for arguments or environment); non-connection
// objects and bad JSON yield nothing
// E2 the stream splitter cuts concatenated and newline-separated objects,
// keeps a partial one, survives braces and quotes inside strings, and
// flags an oversized object
// E3 the peer's uid is read from a /proc/net/tcp table
// E4 only 127.0.0.1 with a port below 1024 is accepted for listening
// E5 end to end: events split across writes reach the callback
// E6 contacts are recorded on the first contact, then at most hourly with
// the count since; keys are bounded; the record round-trips
// E7 enabling events adds one logger (with a backup), and only once
#include "strawberry/opensnitch.hpp"
#include "strawberry/opensnitch_events.hpp"
#include "strawberry/proc_read.hpp"
#include "strawberry/tracker_set.hpp"
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <atomic>
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <mutex>
#include <thread>
using namespace strawberry;
namespace fs = std::filesystem;
#define CHECK(cond) \
do { \
if (!(cond)) { \
std::cerr << "CHECK FAILED: " #cond " (" << __FILE__ << ":" << __LINE__ << ")" << std::endl; \
std::abort(); \
} \
} while (0)
namespace {
const std::string kEvent =
R"({"Type":0,"Action":"allow","Rule":"r","Event":{"protocol":"tcp","dst_ip":"192.0.2.7","dst_host":"metrics.tracker.example",)"
R"("dst_port":443,"user_id":1000,"process_id":4242,"process_path":"/usr/bin/app",)"
R"("process_args":["/usr/bin/app","--token=secret"],"process_env":{"API_KEY":"secret"}}})";
void test_parse() {
const auto c = parse_opensnitch_event(kEvent);
CHECK(c && c->pid == 4242 && c->uid == 1000 && c->process_path == "/usr/bin/app"); // E1
CHECK(c->dst_host == "metrics.tracker.example" && c->dst_ip == "192.0.2.7" && c->dst_port == 443);
CHECK(c->protocol == "tcp" && c->action == "allow");
const auto bare = parse_opensnitch_event(R"({"dst_ip":"192.0.2.1","dst_port":"80","process_id":"7"})");
CHECK(bare && bare->dst_port == 80 && bare->pid == 7 && bare->dst_host.empty());
CHECK(!parse_opensnitch_event(R"({"Type":1,"Event":{"rule":"x"}})"));
CHECK(!parse_opensnitch_event("{not json"));
CHECK(!parse_opensnitch_event("[1,2]"));
const auto bad_numbers = parse_opensnitch_event(R"({"dst_ip":"192.0.2.1","dst_port":99999,"user_id":-1})");
CHECK(bad_numbers && bad_numbers->dst_port == 0 && bad_numbers->uid == 0);
std::cout << "E1 event fields: PASSED" << std::endl;
}
void test_split() {
bool overflow = false;
std::string buf = R"({"a":1}{"b":"}{\"x"} )" "\n" R"({"c":{"d":2}}{"e":)";
const auto objects = take_json_objects(buf, 1024, overflow);
CHECK(objects.size() == 3 && !overflow); // E2
CHECK(objects[1] == R"({"b":"}{\"x"})" && objects[2] == R"({"c":{"d":2}})");
CHECK(buf == R"({"e":)");
buf += "3}";
CHECK(take_json_objects(buf, 1024, overflow).size() == 1 && buf.empty());
std::string junk = "garbage {\"ok\":1}";
CHECK(take_json_objects(junk, 1024, overflow).size() == 1);
std::string big = "{\"k\":\"" + std::string(5000, 'x');
CHECK(take_json_objects(big, 1024, overflow).empty() && overflow);
std::cout << "E2 stream splitting: PASSED" << std::endl;
}
void test_peer_and_address() {
const std::string table =
" sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode\n"
" 0: 0100007F:03D3 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 1 1 0\n"
" 1: 0100007F:B2C4 0100007F:03D3 01 00000000:00000000 00:00000000 00000000 0 0 2 1 0\n"
" 2: 0100007F:B2C5 0100007F:03D3 01 00000000:00000000 00:00000000 00000000 1000 0 3 1 0\n";
CHECK(tcp_socket_uid(table, "0100007F:B2C4", "0100007F:03D3") == 0u); // E3
CHECK(tcp_socket_uid(table, "0100007f:b2c5", "0100007F:03D3") == 1000u);
CHECK(!tcp_socket_uid(table, "0100007F:FFFF", "0100007F:03D3"));
uint16_t port = 0;
std::string error;
CHECK(parse_events_address("127.0.0.1:979", port, error) && port == 979); // E4
for (const char* bad : {"0.0.0.0:979", "127.0.0.1:8080", "127.0.0.1:0", "127.0.0.1:", "localhost:979", ""}) {
error.clear();
CHECK(!parse_events_address(bad, port, error) && !error.empty());
}
std::cout << "E3 peer uid, E4 listen address: PASSED" << std::endl;
}
void test_listener() {
OpenSnitchEventListener listener;
std::atomic<int> seen{0};
std::string error;
std::string host;
std::mutex mutex;
CHECK(listener.start("127.0.0.1:0", [&](const OpenSnitchConnection& c) {
std::lock_guard lock(mutex);
host = c.dst_host;
++seen;
}, error, /*check_peer=*/false));
const auto address = listener.stats().address;
const auto port = static_cast<uint16_t>(std::stoi(address.substr(address.rfind(':') + 1)));
const int fd = ::socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in sa{};
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
CHECK(::connect(fd, reinterpret_cast<sockaddr*>(&sa), sizeof sa) == 0);
const std::string stream = kEvent + "\n" + kEvent;
const auto half = stream.size() / 2;
CHECK(::write(fd, stream.data(), half) == static_cast<ssize_t>(half));
std::this_thread::sleep_for(std::chrono::milliseconds(50));
CHECK(::write(fd, stream.data() + half, stream.size() - half) == static_cast<ssize_t>(stream.size() - half));
for (int i = 0; i < 200 && seen < 2; ++i) std::this_thread::sleep_for(std::chrono::milliseconds(10));
::close(fd);
CHECK(seen == 2); // E5
{
std::lock_guard lock(mutex);
CHECK(host == "metrics.tracker.example");
}
const auto s = listener.stats();
CHECK(s.listening && s.events == 2 && s.connections == 1 && s.rejected_peers == 0);
listener.stop();
CHECK(!listener.stats().listening);
std::cout << "E5 listener end to end: PASSED" << std::endl;
}
void test_contacts() {
constexpr int64_t kHour = 3600LL * 1'000'000'000;
const int64_t t0 = 1'800'000'000LL * 1'000'000'000;
TrackerContacts contacts(2);
const TrackerMatch m{"tracker.example", "easyprivacy"};
CHECK(contacts.add("/usr/bin/app", 1000, m, 443, t0) == 1); // E6: first contact
CHECK(contacts.add("/usr/bin/app", 1000, m, 443, t0 + 5) == 0);
CHECK(contacts.add("/usr/bin/app", 1000, m, 443, t0 + 10) == 0);
CHECK(contacts.add("/usr/bin/app", 1000, m, 443, t0 + kHour) == 3); // three since the last record
CHECK(contacts.add("/usr/bin/other", 0, m, 80, t0) == 1);
CHECK(contacts.add("/usr/bin/third", 0, m, 80, t0) == 0 && contacts.dropped() == 1); // bounded
const auto snap = contacts.snapshot();
CHECK(snap.size() == 2 && snap[0].program == "/usr/bin/app" && snap[0].count == 4 && snap[0].first_ns == t0);
CHECK(contacts.events() == 6);
TrackerContact rec = snap[0];
const auto payload = encode_tracker_contact(rec, 3, "opensnitch");
TrackerContact back;
std::string source;
CHECK(decode_tracker_contact(payload, back, source));
CHECK(back.program == rec.program && back.domain == rec.domain && back.feed == rec.feed && back.uid == 1000 &&
back.port == 443 && back.count == 3 && back.last_ns == rec.last_ns && source == "opensnitch");
CHECK(!decode_tracker_contact("\x0a\x01", back, source));
std::cout << "E6 contacts and record: PASSED" << std::endl;
}
void test_enable_events() {
const auto dir = fs::temp_directory_path() / ("strawberry-osevents-" + std::to_string(::getpid()));
fs::create_directories(dir);
const auto config = dir / "default-config.json";
{
std::ofstream out(config);
out << R"({"Server":{"Address":"unix:///tmp/osui.sock","LogFile":"/var/log/opensnitchd.log"},"Rules":{}})";
}
bool changed = false;
std::string error;
CHECK(enable_opensnitch_events(config.string(), "127.0.0.1:979", &changed, &error) && changed); // E7
CHECK(fs::exists(dir / "default-config.json.bak"));
const auto text = read_small_file(config.string(), 1 << 20);
CHECK(text && text->find("\"127.0.0.1:979\"") != std::string::npos && text->find("\"remote\"") != std::string::npos);
CHECK(text->find("unix:///tmp/osui.sock") != std::string::npos); // the rest is kept
CHECK(enable_opensnitch_events(config.string(), "127.0.0.1:979", &changed, &error) && !changed);
{
std::ofstream out(config);
out << "{broken";
}
CHECK(!enable_opensnitch_events(config.string(), "127.0.0.1:979", &changed, &error) && !error.empty());
std::error_code ec;
fs::remove_all(dir, ec);
std::cout << "E7 enable events: PASSED" << std::endl;
}
void test_encode_decode() {
OpenSnitchConnection c;
c.pid = 4242;
c.uid = 1000;
c.process_path = "/usr/bin/app";
c.dst_host = "metrics.tracker.example";
c.dst_ip = "192.0.2.7";
c.dst_port = 443;
c.protocol = "tcp";
c.action = "allow";
const auto wire = encode_connection(c); // E8: redacted wire form for the relay
// The wire form carries only the safe fields; never arguments or environment.
CHECK(wire.find("args") == std::string::npos && wire.find("env") == std::string::npos);
const auto back = decode_connection(wire);
CHECK(back && back->pid == 4242 && back->uid == 1000 && back->process_path == "/usr/bin/app");
CHECK(back->dst_host == "metrics.tracker.example" && back->dst_ip == "192.0.2.7" && back->dst_port == 443);
CHECK(back->protocol == "tcp" && back->action == "allow");
CHECK(!decode_connection("{not json"));
CHECK(!decode_connection("[1,2]"));
CHECK(!decode_connection(R"({"pid":1})")); // no destination -> rejected
std::cout << "E8 encode/decode + redaction: PASSED" << std::endl;
}
void test_ingest_server() {
const auto path =
(fs::temp_directory_path() / ("sb-ingest-" + std::to_string(::getpid()) + ".sock")).string();
TrackerIngestServer server;
std::atomic<int> seen{0};
std::string host;
std::mutex mutex;
std::string error;
CHECK(server.start(path, [&](const OpenSnitchConnection& c) { // E9: relay -> socket -> callback
std::lock_guard lock(mutex);
host = c.dst_host;
++seen;
}, error, /*require_root=*/false));
const int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
sockaddr_un sa{};
sa.sun_family = AF_UNIX;
std::strncpy(sa.sun_path, path.c_str(), sizeof(sa.sun_path) - 1);
CHECK(::connect(fd, reinterpret_cast<sockaddr*>(&sa), sizeof sa) == 0);
OpenSnitchConnection c;
c.pid = 7;
c.process_path = "/bin/x";
c.dst_host = "ads.example";
c.dst_port = 443;
const auto wire = encode_connection(c) + encode_connection(c); // two objects, back to back
CHECK(::write(fd, wire.data(), wire.size()) == static_cast<ssize_t>(wire.size()));
for (int i = 0; i < 200 && seen < 2; ++i) std::this_thread::sleep_for(std::chrono::milliseconds(10));
::close(fd);
CHECK(seen == 2);
{
std::lock_guard lock(mutex);
CHECK(host == "ads.example");
}
const auto s = server.stats();
CHECK(s.listening && s.events == 2 && s.connections == 1 && s.rejected_peers == 0);
server.stop();
CHECK(!server.stats().listening);
std::error_code ec;
fs::remove(path, ec);
std::cout << "E9 ingest server end to end: PASSED" << std::endl;
}
void test_littlesnitch_parser() {
// E10: the macOS Little Snitch log-traffic CSV source.
CHECK((split_csv_line(R"(a,"b,c","d""e",f)") == std::vector<std::string>{"a", "b,c", "d\"e", "f"}));
LittleSnitchParser p;
const char* header =
"date,direction,uid,ipAddress,remoteHostname,protocol,port,connectCount,denyCount,byteCountIn,byteCountOut,"
"connectingExecutable,parentAppExecutable";
CHECK(!p.feed_line(header) && p.have_header()); // header yields nothing
// outbound TCP contact
const auto c = p.feed_line(
R"(2026-10-28 12:00:00,out,501,192.0.2.7,metrics.tracker.example,6,443,1,0,0,1234,/usr/bin/curl,)");
CHECK(c && c->process_path == "/usr/bin/curl" && c->dst_host == "metrics.tracker.example");
CHECK(c->dst_ip == "192.0.2.7" && c->dst_port == 443 && c->uid == 501 && c->protocol == "tcp" &&
c->action == "allow");
// a denied UDP row -> action deny, protocol udp
const auto d = p.feed_line(R"(2026-10-28 12:00:01,out,0,,ads.example,17,53,0,3,0,0,/opt/app/x,)");
CHECK(d && d->protocol == "udp" && d->action == "deny" && d->dst_host == "ads.example" && d->dst_ip.empty());
// inbound rows are ignored; so are rows with no destination
CHECK(!p.feed_line(R"(2026-10-28 12:00:02,in,501,192.0.2.9,host.example,6,22,1,0,0,0,/usr/sbin/sshd,)"));
CHECK(!p.feed_line(R"(2026-10-28 12:00:03,out,501,,,6,443,1,0,0,0,/usr/bin/curl,)"));
// a quoted executable description containing a comma
const auto q = p.feed_line(
R"(2026-10-28 12:00:04,out,501,192.0.2.7,t.example,6,443,1,0,0,0,"/Applications/Foo, Bar.app/Contents/MacOS/Foo",)");
CHECK(q && q->process_path == "/Applications/Foo, Bar.app/Contents/MacOS/Foo" && q->dst_host == "t.example");
std::cout << "E10 little snitch parser: PASSED" << std::endl;
}
} // namespace
int main() {
test_parse();
test_split();
test_peer_and_address();
#if defined(__linux__)
test_listener();
#endif
test_contacts();
test_enable_events();
test_encode_decode();
test_littlesnitch_parser();
#if defined(__linux__)
test_ingest_server();
#endif
return 0;
}