AI coding agents can edit files, run shell commands, and touch infrastructure. One bad rm -rf, force-push, or docker compose down -v can wipe hours of work or production data.
| 1 | # SPDX-License-Identifier: MIT |
| 2 | from __future__ import annotations |
| 3 | |
| 4 | from agent_guard.redactor import contains_secret_content, find_secret_markers, redact |
| 5 | |
| 6 | |
| 7 | def test_redact_connection_string() -> None: |
| 8 | raw = "postgres://user:secret-password@db:5432/app" |
| 9 | out = redact(raw) |
| 10 | assert "secret-password" not in out |
| 11 | assert "[REDACTED]" in out |
| 12 | assert "postgres://user:" in out |
| 13 | |
| 14 | |
| 15 | def test_redact_bearer() -> None: |
| 16 | raw = "Authorization: Bearer super-secret-token-value" |
| 17 | out = redact(raw) |
| 18 | assert "super-secret-token-value" not in out |
| 19 | assert "[REDACTED]" in out |
| 20 | |
| 21 | |
| 22 | def test_redact_aws_key() -> None: |
| 23 | raw = "AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" |
| 24 | out = redact(raw) |
| 25 | assert "wJalrXUtnFEMI" not in out |
| 26 | |
| 27 | |
| 28 | def test_find_markers() -> None: |
| 29 | text = "export JWT_SECRET=abc DATABASE_URL=postgres://x" |
| 30 | markers = find_secret_markers(text) |
| 31 | assert "JWT_SECRET" in markers |
| 32 | assert "DATABASE_URL" in markers |
| 33 | |
| 34 | |
| 35 | def test_contains_secret_content() -> None: |
| 36 | assert contains_secret_content("OPENAI_API_KEY=sk-test") |
| 37 | assert not contains_secret_content("hello world") |
| 38 | |
| 39 | |
| 40 | def test_private_key_block() -> None: |
| 41 | pem = "-----BEGIN PRIVATE KEY-----\nABCDEF\n-----END PRIVATE KEY-----" |
| 42 | assert "[REDACTED_PRIVATE_KEY]" in redact(pem) |
| 43 | assert "ABCDEF" not in redact(pem) |