|
| 1 | +# Tencent is pleased to support the open source community by making tRPC-Agent-Python available. |
| 2 | +# |
| 3 | +# Copyright (C) 2026 Tencent. All rights reserved. |
| 4 | +# |
| 5 | +# tRPC-Agent-Python is licensed under Apache-2.0. |
| 6 | +"""Tests for secret detection and redaction (>=95% detection target).""" |
| 7 | +SAMPLE_SECRETS = [ |
| 8 | + 'API_KEY = "sk-abcdefghijklmnopqrstuvwxyz123456"', |
| 9 | + 'key = "sk-ant-api03-abcdefghijklmnopqrst"', |
| 10 | + "aws AKIAIOSFODNN7EXAMPLE", |
| 11 | + 'token = "ghp_abcdefghijklmnopqrstuvwxyz0123456789"', |
| 12 | + "Authorization: Bearer abc123def456ghi789jkl", |
| 13 | + "jwt eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.abc123def456", |
| 14 | + "-----BEGIN RSA PRIVATE KEY-----", |
| 15 | + 'password = "hunter2secret"', |
| 16 | + 'PASSWD: "topsecretvalue"', |
| 17 | + 'secret = "do-not-leak-me"', |
| 18 | + 'api_key="deadbeefcafe1234"', |
| 19 | + "https://user:p4ssw0rd@db.example.com/prod", |
| 20 | + 'TOKEN = "9f8e7d6c5b4a39281706f5e4d3c2b1a0"', |
| 21 | + 'apikey: "0123456789abcdefffff"', |
| 22 | + "Bearer xoxb-1234567890-abcdefghij", |
| 23 | + 'DB_PASSWORD = "prod-db-pass-2026"', |
| 24 | + 'passwd = "another$ecret!"', |
| 25 | + "AKIA0123456789ABCDEF", |
| 26 | + 'secret_token = "veryhiddenvalue1"', |
| 27 | + 'ghs_abcdefghijklmnopqrstuvwxyz0123456789', |
| 28 | +] |
| 29 | + |
| 30 | + |
| 31 | +def test_detection_rate_at_least_95_percent(): |
| 32 | + from review.redaction import contains_secret |
| 33 | + detected = sum(1 for s in SAMPLE_SECRETS if contains_secret(s)) |
| 34 | + assert detected / len(SAMPLE_SECRETS) >= 0.95 |
| 35 | + |
| 36 | + |
| 37 | +def test_redaction_removes_secret_values(): |
| 38 | + from review.redaction import redact_text |
| 39 | + for sample in SAMPLE_SECRETS: |
| 40 | + out = redact_text(sample) |
| 41 | + assert "***REDACTED-" in out or sample == out, sample |
| 42 | + assert "hunter2secret" not in redact_text('password = "hunter2secret"') |
| 43 | + assert "AKIAIOSFODNN7EXAMPLE" not in redact_text("aws AKIAIOSFODNN7EXAMPLE") |
| 44 | + |
| 45 | + |
| 46 | +def test_redaction_is_stable_fingerprint(): |
| 47 | + from review.redaction import redact_text |
| 48 | + a = redact_text("Bearer abc123def456ghi789jkl") |
| 49 | + b = redact_text("Bearer abc123def456ghi789jkl") |
| 50 | + assert a == b |
| 51 | + |
| 52 | + |
| 53 | +def test_plain_text_untouched(): |
| 54 | + from review.redaction import redact_text |
| 55 | + text = "def add(a, b):\n return a + b" |
| 56 | + assert redact_text(text) == text |
0 commit comments