A tiny, local-first CLI that finds the files your AI coding agent actually needs — and packs them into a focused context.
| 1 | """Tests for security detection and redaction.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from pathlib import Path |
| 6 | |
| 7 | from agent_context.security import ( |
| 8 | REDACTION_PLACEHOLDER, |
| 9 | contains_likely_secrets, |
| 10 | detect_sensitive_paths, |
| 11 | is_sensitive_path, |
| 12 | redact_secrets, |
| 13 | should_exclude_from_context, |
| 14 | ) |
| 15 | |
| 16 | |
| 17 | def test_sensitive_filenames(): |
| 18 | assert is_sensitive_path(".env") |
| 19 | assert is_sensitive_path(".env.production") |
| 20 | assert is_sensitive_path("credentials.json") |
| 21 | assert is_sensitive_path("secrets.json") |
| 22 | assert is_sensitive_path("service-account.json") |
| 23 | assert is_sensitive_path("id_rsa") |
| 24 | assert is_sensitive_path("id_ed25519") |
| 25 | assert is_sensitive_path("server.pem") |
| 26 | assert is_sensitive_path("tls.key") |
| 27 | assert is_sensitive_path("cert.crt") |
| 28 | assert not is_sensitive_path("middleware.ts") |
| 29 | assert not is_sensitive_path("README.md") |
| 30 | |
| 31 | |
| 32 | def test_should_exclude(): |
| 33 | assert should_exclude_from_context(".env") |
| 34 | assert not should_exclude_from_context("src/auth.ts") |
| 35 | |
| 36 | |
| 37 | def test_detect_sensitive_paths(tmp_path: Path): |
| 38 | env = tmp_path / ".env" |
| 39 | env.write_text("API_KEY=x\n", encoding="utf-8") |
| 40 | ok = tmp_path / "ok.ts" |
| 41 | ok.write_text(" consoles ", encoding="utf-8") |
| 42 | findings = detect_sensitive_paths([env, ok], root=tmp_path) |
| 43 | assert len(findings) == 1 |
| 44 | assert findings[0].path == ".env" |
| 45 | |
| 46 | |
| 47 | def test_redact_assignments(): |
| 48 | text = 'const API_KEY = "sk-live-abc123";\nPASSWORD=hunter2\n' |
| 49 | redacted = redact_secrets(text) |
| 50 | assert "sk-live-abc123" not in redacted |
| 51 | assert "hunter2" not in redacted |
| 52 | assert REDACTION_PLACEHOLDER in redacted |
| 53 | assert contains_likely_secrets(text) |
| 54 | |
| 55 | |
| 56 | def test_redact_pem(): |
| 57 | pem = ( |
| 58 | "-----BEGIN RSA PRIVATE KEY-----\n" |
| 59 | "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC...\n" |
| 60 | "-----END RSA PRIVATE KEY-----\n" |
| 61 | ) |
| 62 | redacted = redact_secrets(pem) |
| 63 | assert "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC" not in redacted |
| 64 | assert REDACTION_PLACEHOLDER in redacted |
| 65 | |
| 66 | |
| 67 | def test_redact_bearer(): |
| 68 | text = "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.abc" |
| 69 | redacted = redact_secrets(text) |
| 70 | assert "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" not in redacted |
| 71 | assert REDACTION_PLACEHOLDER in redacted |
| 72 | |
| 73 | |
| 74 | def test_no_false_positive_on_clean_code(): |
| 75 | text = "export function add(a: number, b: number) { return a + b; }\n" |
| 76 | assert not contains_likely_secrets(text) |
| 77 | assert redact_secrets(text) == text |
| 78 | |
| 79 | |
| 80 | def test_sample_repo_secrets_excluded(sample_repo: Path): |
| 81 | from agent_context.packer import pack_context |
| 82 | |
| 83 | result = pack_context(sample_repo, "authentication middleware", token_budget=8000) |
| 84 | paths = {p.rel_path for p in result.selected} |
| 85 | assert ".env" not in paths |
| 86 | assert "credentials.json" not in paths |
| 87 | assert "super-secret-key-12345" not in result.markdown |
| 88 | assert "hunter2" not in result.markdown |
| 89 | # Inline secret in settings.ts should be redacted if included |
| 90 | if "should-be-redacted" in result.markdown: |
| 91 | raise AssertionError("secret value leaked into context markdown") |