// Command-line redaction. A failure prints the seed; re-run with // STRAWBERRY_PROPERTY_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 #include #include #include #include #include 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= --verbose"); expect("app --password hunter2 --verbose", "app --password --verbose"); expect("app --api-key abc123", "app --api-key "); expect("AWS_SECRET_ACCESS_KEY=abc/def aws s3 ls", "AWS_SECRET_ACCESS_KEY= aws s3 ls"); expect("env GITHUB_TOKEN=x make", "env GITHUB_TOKEN= make"); expect("curl -H Authorization: Bearer abc.def https://h", "curl -H Authorization: Bearer https://h"); expect("curl -H Authorization: abc https://h", "curl -H Authorization: https://h"); expect("git clone https://bob:s3cret@example.com/r.git", "git clone https://bob:@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://@github.com/x"); // a token as the user name expect("tool ghp_0123456789abcdefABCDEF", "tool "); expect("tool AKIAABCDEFGHIJKLMNOP", "tool "); expect("tool eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.sig", "tool "); expect("mysql -uroot -pS3cret db", "mysql -uroot -p db"); expect("/usr/bin/mysqldump -pS3cret db", "/usr/bin/mysqldump -p 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 "); // spaces kept; value after the run expect("", ""); std::cout << "cases: PASSED" << std::endl; } void test_properties() { std::mt19937_64 rng(kSeed); const std::vector words{ "ls", "mysql", "-p", "-pX", "--password", "--password=", "Authorization:", "Bearer", "basic", "token", "https://u:p@h/x", "https://u@h", "KEY=v", "SECRET=", "=", "-", "--", "", "-p", "ghp_0123456789abcdefXYZ", "--author", "x", "", "a=b=c", "://", "u:@h", "PRIVATE"}; const std::string secret = "Zq9SECRETVALUEq9Z"; const std::vector 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(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::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; }