Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -5049,16 +5049,38 @@ def _is_sensitive_task_event_metadata_key(key: str) -> bool:


# Whole-segment secret markers (not substrings of longer words like "tokens").
_PUBLIC_SECRET_SEGMENT_RE = re.compile(
r"(?<![A-Za-z0-9_.-])"
r"(?:"
r"(?:[A-Za-z0-9_.]+[-_.])*(?:secret|raw-ref|broker-ref|pod-)[A-Za-z0-9_.-]*"
r"|"
r"(?:[A-Za-z0-9_.]+[-_.])*token(?:[-_.][A-Za-z0-9_.]+)*"
r")"
r"(?![A-Za-z0-9_.-])",
#
# Redaction is a single linear pass: grab each delimited word, then decide with
# two bounded checks. The previous single-regex form nested
# ``(?:[A-Za-z0-9_.]+[-_.])*`` over a class that also contains the separators
# ``_`` and ``.``, so every dotted/underscored run that never reached
# ``secret``/``token`` had exponentially many ways to split: 37 chars took 23ms,
# 61 chars took 91s. pip output has exactly that shape, and the endpoint that
# replays it is public, so a single log line pinned the event loop at 100% CPU
# and starved every evaluation running on the validator.
_PUBLIC_SECRET_WORD_RE = re.compile(r"(?<![A-Za-z0-9_.-])[A-Za-z0-9_.-]+(?![A-Za-z0-9_.-])")
_PUBLIC_SECRET_MARKER_RE = re.compile(
r"(?:^|[-_.])(?:secret|raw-ref|broker-ref|pod-)",
flags=re.IGNORECASE,
)
_PUBLIC_TOKEN_SEGMENT_RE = re.compile(
r"(?:^|[-_.])token(?:[-_.]|$)",
flags=re.IGNORECASE,
)


def _redact_public_secret_segments(value: str) -> str:
"""Redact whole words that carry a secret marker on a segment boundary."""

def _replace(match: re.Match[str]) -> str:
word = match.group(0)
if _PUBLIC_SECRET_MARKER_RE.search(word):
return "[REDACTED_SECRET]"
if _PUBLIC_TOKEN_SEGMENT_RE.search(word):
return "[REDACTED_SECRET]"
return word

return _PUBLIC_SECRET_WORD_RE.sub(_replace, value)


def _terminal_bench_task_id_shield_pattern() -> re.Pattern[str]:
Expand Down Expand Up @@ -5105,7 +5127,7 @@ def _shield(match: re.Match[str]) -> str:
# Segment-boundary redaction: match secret/token/raw-ref/broker-ref/pod- as
# whole hyphen/underscore/dot-delimited segments, not as substrings of a
# longer word (``tokens`` must not match ``token``).
sanitized = _PUBLIC_SECRET_SEGMENT_RE.sub("[REDACTED_SECRET]", sanitized)
sanitized = _redact_public_secret_segments(sanitized)
for index, original in enumerate(shields):
sanitized = sanitized.replace(f"\x00TBSAFE{index}\x00", original)
return sanitized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,40 @@ def test_public_task_event_text_still_redacts_genuine_secrets() -> None:
for secret in _GENUINE_SECRET_SHAPES:
assert secret not in out, f"secret still visible: {secret!r} in {out!r}"
assert "[REDACTED_SECRET]" in out


def test_secret_redaction_is_linear_on_dotted_runs() -> None:
"""A public log line must never wedge the event loop.

``_PUBLIC_SECRET_SEGMENT_RE`` nested ``(?:[A-Za-z0-9_.]+[-_.])*`` over a class
that also contains the separators, so a dotted/underscored run that never
reaches ``secret``/``token`` backtracked exponentially: 37 chars took 23ms and
61 chars took 91s. pip output carries exactly that shape
("Successfully installed agent-challenge-1.0.1 aiohappyeyeballs-2.7.1 ..."),
and the endpoint serving it is public, so one log line pinned the validator
at 100% CPU and starved every evaluation.
"""
import time

from agent_challenge.api.routes import _public_task_event_text

payload = "a.b_c." * 40 + "!"
started = time.perf_counter()
_public_task_event_text(payload)
elapsed = time.perf_counter() - started
assert elapsed < 1.0, f"redaction took {elapsed:.1f}s on {len(payload)} chars"


def test_realistic_pip_output_is_fast_and_unredacted() -> None:
from agent_challenge.api.routes import _public_task_event_text

line = (
"Successfully installed agent-challenge-1.0.1 aiohappyeyeballs-2.7.1 "
"aiohttp-3.14.3 aiosignal-1.4.0 async-substrate-interface-2.2.1 "
) * 6
import time

started = time.perf_counter()
out = _public_task_event_text(line)
assert time.perf_counter() - started < 1.0
assert "agent-challenge-1.0.1" in out
Loading