Skip to content
Closed
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
5 changes: 5 additions & 0 deletions agent/redact.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@
r"(?<![A-Za-z0-9_-])(" + "|".join(_PREFIX_PATTERNS) + r")(?![A-Za-z0-9_-])"
)

_CONTROL_CHARS_RE = re.compile(r"[\x00-\x1f\x7f-\x9f]")


def mask_secret(
value: str,
Expand Down Expand Up @@ -346,6 +348,9 @@ def mask_secret(
>>> mask_secret("long-token", head=6, tail=4, floor=18)
'***'
"""
if not value:
return empty
value = _CONTROL_CHARS_RE.sub("", value)
if not value:
return empty
if len(value) < floor:
Expand Down
17 changes: 16 additions & 1 deletion tests/agent/test_redact.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from agent.redact import redact_cdp_url, redact_sensitive_text, RedactingFormatter
from agent.redact import mask_secret, redact_cdp_url, redact_sensitive_text, RedactingFormatter


@pytest.fixture(autouse=True)
Expand All @@ -15,6 +15,21 @@ def _ensure_redaction_enabled(monkeypatch):
monkeypatch.setattr("agent.redact._REDACT_ENABLED", True)


class TestMaskSecret:
def test_printable_secret_mask_shape_is_unchanged(self):
assert mask_secret("abcd0123456789zzzz") == "abcd...zzzz"

def test_strips_controls_from_visible_segments(self):
masked = mask_secret("ab\ncd0123456789zz\x85q", empty="missing")

assert "\n" not in masked
assert "\x85" not in masked
assert masked == "abcd...9zzq"

def test_all_control_input_uses_empty_fallback(self):
assert mask_secret("\x00\n\x85", empty="missing") == "missing"


class TestKnownPrefixes:
def test_openai_sk_key(self):
text = "Using key sk-proj-abc123def456ghi789jkl012"
Expand Down
Loading