strawberry/tests/test_redact.cpp
trvon c452be93d4
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 / docker (push) Has been cancelled
Unify file ids, record uid/comm, redact command lines
File node ids: include/strawberry/node_ids.hpp is now the only place
they are built (file_<sha256> or file_path_<sha256 of path>). The three
modules disagreed: correlation used a 16-digit path hash and behavior
detections the raw path, so path-keyed evidence never met the rest. An
exec detection with a known hash also writes a same_path edge from the
path id to the content id. Source invariant R8 rejects hand-built file
ids in src/ and include/.

Processes carry the real uid and kernel comm: from the eBPF exec event
(which now includes the uid) or /proc. Stored on process nodes and in
process_exec telemetry (fields 14, 15).

Command lines are redacted before any storage (capture_redact_cmdline,
default on): sensitive flags and KEY=value pairs, Authorization/Bearer
credentials, URL passwords, tokens used as URL user names, well-known
token shapes (GitHub, GitLab, Slack, AWS key ids, sk-, JWTs) and mysql
-p<password>. The rules repeat to a fixed point, so redaction is
idempotent by construction. tests/test_redact.cpp covers the rules plus
idempotence, size and planted-secret properties; fuzz_cmdline_redactor
found two idempotence bugs and a leak (--password --token X) that are
fixed here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 17:58:48 -06:00

109 lines
5.3 KiB
C++

// Command-line redaction. A failure prints the seed; re-run with
// STRAWBERRY_PROPERTY_SEED=<seed>.
//
// cases: each redaction rule, and things that must stay (--author, --port)
// R1 idempotent: redact(redact(x)) == redact(x)
// R2 a secret planted behind any sensitive form never survives
// R3 size: output <= input + 80 per token (8 passes x one marker)
#include "strawberry/redact.hpp"
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <random>
#include <string>
#include <vector>
using namespace strawberry;
namespace {
uint64_t seed_from_env() {
if (const char* text = std::getenv("STRAWBERRY_PROPERTY_SEED")) return std::strtoull(text, nullptr, 10);
return std::random_device{}();
}
const uint64_t kSeed = seed_from_env();
#define PROPERTY(cond) \
do { \
if (!(cond)) { \
std::cerr << "PROPERTY FAILED: " #cond " (seed " << kSeed << ", " << __FILE__ << ":" \
<< __LINE__ << ")" << std::endl; \
std::abort(); \
} \
} while (0)
void expect(const std::string& in, const std::string& want) {
const auto got = redact_cmdline(in);
if (got != want) {
std::cerr << "redact(\"" << in << "\")\n got \"" << got << "\"\n want \"" << want << "\"" << std::endl;
std::abort();
}
}
void test_cases() {
expect("/usr/bin/ls -la /tmp", "/usr/bin/ls -la /tmp");
expect("app --password=hunter2 --verbose", "app --password=<redacted> --verbose");
expect("app --password hunter2 --verbose", "app --password <redacted> --verbose");
expect("app --api-key abc123", "app --api-key <redacted>");
expect("AWS_SECRET_ACCESS_KEY=abc/def aws s3 ls", "AWS_SECRET_ACCESS_KEY=<redacted> aws s3 ls");
expect("env GITHUB_TOKEN=x make", "env GITHUB_TOKEN=<redacted> make");
expect("curl -H Authorization: Bearer abc.def https://h", "curl -H Authorization: Bearer <redacted> https://h");
expect("curl -H Authorization: abc https://h", "curl -H Authorization: <redacted> https://h");
expect("git clone https://bob:s3cret@example.com/r.git", "git clone https://bob:<redacted>@example.com/r.git");
expect("git clone https://example.com/r.git", "git clone https://example.com/r.git");
expect("git push https://ghp_0123456789abcdefABCDEF@github.com/x",
"git push https://<redacted>@github.com/x"); // a token as the user name
expect("tool ghp_0123456789abcdefABCDEF", "tool <redacted>");
expect("tool AKIAABCDEFGHIJKLMNOP", "tool <redacted>");
expect("tool eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.sig", "tool <redacted>");
expect("mysql -uroot -pS3cret db", "mysql -uroot -p<redacted> db");
expect("/usr/bin/mysqldump -pS3cret db", "/usr/bin/mysqldump -p<redacted> db");
expect("tar -pcf a.tar dir", "tar -pcf a.tar dir"); // -p is not a password outside mysql
expect("git commit --author=Bob -m x", "git commit --author=Bob -m x");
expect("ssh -p 22 host", "ssh -p 22 host");
expect("sudo --no-password-prompt ls", "sudo --no-password-prompt ls");
expect("a --token x", "a --token <redacted>"); // spaces kept; value after the run
expect("", "");
std::cout << "cases: PASSED" << std::endl;
}
void test_properties() {
std::mt19937_64 rng(kSeed);
const std::vector<std::string> words{
"ls", "mysql", "-p", "-pX", "--password", "--password=", "Authorization:", "Bearer", "basic", "token",
"https://u:p@h/x", "https://u@h", "KEY=v", "SECRET=", "=", "-", "--", "<redacted>", "-p<redacted>",
"ghp_0123456789abcdefXYZ", "--author", "x", "", "a=b=c", "://", "u:<redacted>@h", "PRIVATE"};
const std::string secret = "Zq9SECRETVALUEq9Z";
const std::vector<std::string> sensitive_forms{
"--password " + secret, "--token=" + secret, "API_KEY=" + secret, "Authorization: Bearer " + secret,
"Authorization: " + secret, "https://user:" + secret + "@host/p", "bearer " + secret};
for (int iter = 0; iter < 20000; ++iter) {
std::string in;
const int n = static_cast<int>(rng() % 8);
for (int i = 0; i < n; ++i) {
if (i) in += ' ';
in += words[rng() % words.size()];
}
const auto once = redact_cmdline(in);
PROPERTY(redact_cmdline(once) == once); // R1
const std::size_t tokens = static_cast<std::size_t>(std::count(in.begin(), in.end(), ' ')) + 1;
PROPERTY(once.size() <= in.size() + 80 * tokens); // R3
// R2: plant a secret behind a sensitive form anywhere in the line.
const auto& form = sensitive_forms[rng() % sensitive_forms.size()];
const std::string planted = (rng() % 2 ? in + " " + form : form + " " + in);
PROPERTY(redact_cmdline(planted).find(secret) == std::string::npos);
}
std::cout << "R1-R3 properties: PASSED" << std::endl;
}
} // namespace
int main() {
std::cout << "redact properties, seed " << kSeed << std::endl;
test_cases();
test_properties();
return 0;
}