Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions agentic_security/logutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@
# httpx errors repeat the full URL. Mask secret-looking values before any
# record reaches a handler:
# ?api_key=... &token=... &client_secret=... &password=...
# Authorization: Bearer ... x-api-key: ...
# scheme://user:pass@host/... scheme://token@host/...
# Authorization: Bearer/Basic/Token/ApiKey ... x-api-key: ...
# sk-..., sk-ant-..., hf_..., AKIA... shaped keys anywhere in the text
_SECRET_PATTERNS = (
re.compile(
r"([?&][^=&\s#\"'<>]*(?:key|token|secret|password)[^=&\s#\"'<>]*=)"
r"[^&\s#\"'<>]+",
re.IGNORECASE,
),
re.compile(
r"\b([a-zA-Z][a-zA-Z0-9+.-]{0,15}://)[^\s/?#\"'<>,]+(?=@[^\s/?#\"'<>,@])"
),
re.compile(r"(\bBearer\s+)[A-Za-z0-9._~+/=-]{8,}", re.IGNORECASE),
re.compile(
r"(\b(?:x-api-key|api-key|authorization)[\"']?\s*[:=]\s*[\"']?)"
r"(?!Bearer\b)[^\s\"',}]{8,}",
r"(\b(?:x-api-key|api-key|authorization)[\"']?\s*[:=]\s*[\"']?"
r"(?!Bearer\b)(?:(?:Basic|Token|ApiKey|Api-Key)[ \t]+)?)"
r"[^\s\"',}]{8,}",
re.IGNORECASE,
),
re.compile(r"()\b(?:sk-(?:ant-|proj-)?|hf_|AKIA)[A-Za-z0-9_-]{12,}"),
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/test_log_redaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,20 @@
("GET https://example.com/data.csv?token=abc123def456", "abc123def456"),
("https://example.com/?client_secret=s3cr3tvalue", "s3cr3tvalue"),
("Authorization: Bearer abcdefgh12345678", "abcdefgh12345678"),
("Authorization: Basic dXNlcjpzZWNyZXQ=", "dXNlcjpzZWNyZXQ="),
("authorization: basic dXNlcjpzZWNyZXQ=", "dXNlcjpzZWNyZXQ="),
("Authorization: Token tok_fake_01234567", "tok_fake_01234567"),
("Authorization: ApiKey key_fake_01234567", "key_fake_01234567"),
("Authorization: Api-Key key_fake_01234567", "key_fake_01234567"),
('{"Authorization": "Basic dXNlcjpzZWNyZXQ="}', "dXNlcjpzZWNyZXQ="),
("{'authorization': 'Token tok_fake_01234567'}", "tok_fake_01234567"),
('{"Authorization": "ApiKey key_fake_01234567"}', "key_fake_01234567"),
("Proxy-Authorization: Basic dXNlcjpzZWNyZXQ=", "dXNlcjpzZWNyZXQ="),
('{"x-api-key": "0123456789abcdef"}', "0123456789abcdef"),
("GET https://user:secretpass@api.example.com/v1/chat", "secretpass"),
("http://token_fake_999@proxy.example.com:8080/v1", "token_fake_999"),
("postgresql://dbuser:dbsecret123@db.example.com:5432/app", "dbsecret123"),
("redis://:cachepass123@cache.example.com:6379/0", "cachepass123"),
(f"loaded key {KEY} from env", KEY),
("anthropic sk-ant-api03-AAAAAAAAAAAAAAAA", "sk-ant-api03-AAAAAAAAAAAAAAAA"),
("hf token hf_ABCDEFGHIJKLMNOPQRST", "hf_ABCDEFGHIJKLMNOPQRST"),
Expand All @@ -36,11 +49,46 @@ def test_redact_masks_secrets(text, secret):
assert MASK in cleaned


@pytest.mark.parametrize(
"text,expected",
[
(
"https://user:secretpass@api.example.com:8443/v1/chat?page=2",
f"https://{MASK}@api.example.com:8443/v1/chat?page=2",
),
(
"https://user:secretpass@api.example.com/v1?api_key=fakekey12345&page=2",
f"https://{MASK}@api.example.com/v1?api_key={MASK}&page=2",
),
(
'{"Authorization": "Basic dXNlcjpzZWNyZXQ="}',
f'{{"Authorization": "Basic {MASK}"}}',
),
(
"{'authorization': 'Token tok_fake_01234567'}",
f"{{'authorization': 'Token {MASK}'}}",
),
(
"Authorization: ApiKey key_fake_01234567",
f"Authorization: ApiKey {MASK}",
),
],
)
def test_redact_preserves_structure(text, expected):
assert redact(text) == expected


@pytest.mark.parametrize(
"text",
[
"Loading CSV from URL: https://example.com/prompts.csv?page=2",
"Authorization: Bearer XXXXX", # the shipped placeholder spec
"Authorization: Basic XXXXX",
"Basic authentication enabled; Token validation passed",
"Contact alice@example.com for access",
"https://example.com/users/alice@example.com/profile",
"https://example.com/api?email=alice@example.com",
"url=https://example.com,email=alice@example.com",
"Scanning module AgenticBackend on port 8718",
"The model said: I cannot help with that.",
],
Expand Down
Loading