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
54 changes: 49 additions & 5 deletions scripts/record_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
contract), then stores stdout, stderr, exit code, status and usage under
``tests/fixtures/agents/<id>/<cli-version>/<scenario>.json``. Before writing
anything it redacts known credential shapes and refuses to save if a key
pattern is still present. JSONL transcripts also have opaque provider IDs
and encrypted reasoning blobs replaced, and bulky tool output truncated,
while every event and the final report are kept. Environment variable values
are never recorded -- only their names, for scenarios that isolate credentials.
pattern is still present. The recording machine's home directory is replaced
with ``~`` in both raw and JSON-escaped form, so local usernames never end up
in a public fixture. JSONL transcripts also have opaque provider IDs and
encrypted reasoning blobs replaced, and bulky tool output truncated, while
every event and the final report are kept. Environment variable values are
never recorded -- only their names, for scenarios that isolate credentials.
"""

from __future__ import annotations
Expand All @@ -39,6 +41,35 @@

REDACTED = "[REDACTED]"

# The recorder runs on the operator's machine, so transcripts can contain the
# local home directory as a raw path, a JSON-escaped path, or the POSIX spelling
# a CLI prints for its own tool paths. None of it is useful evidence in a
# public fixture. Keep every spelling we know how to recognise so a recording
# made on Windows or macOS cannot leak the username.
HOME = Path.home()


def _json_escaped(text: str) -> str:
return json.dumps(text, ensure_ascii=False)[1:-1]


def _home_variants() -> tuple[str, ...]:
spellings = (str(HOME), HOME.as_posix(), f"/Users/{HOME.name}", f"/home/{HOME.name}")
variants: list[str] = []
for spelling in dict.fromkeys(filter(None, spellings)):
escaped = spelling
# stdout/stderr are JSONL inside the outer JSON fixture: a path there
# is escaped twice, while a normal field is escaped once. Cover both
# levels plus the raw string the adapter actually sees.
for _ in range(3):
if escaped not in variants:
variants.append(escaped)
escaped = _json_escaped(escaped)
return tuple(variants)


_HOME_VARIANTS = _home_variants()

# Provider-generated identifiers and encrypted reasoning are neither useful
# evidence nor safe to publish; replacing them keeps the transcript small and
# reviewable without changing the event sequence secfoo parses.
Expand Down Expand Up @@ -83,7 +114,17 @@ class KeyFound(RuntimeError):
"""A credential-shaped string survived redaction; do not write the file."""


def scrub_home(text: str) -> str:
"""Replace the recording machine's home directory variants with ``~``."""
for home in _HOME_VARIANTS:
# Do not match a longer username such as /Users/shrut2. A trailing
# separator is fine: /Users/shrut/repo becomes ~/repo.
text = re.sub(re.escape(home) + r"(?![A-Za-z0-9._-])", "~", text, flags=re.IGNORECASE)
return text


def redact(text: str) -> str:
text = scrub_home(text)
for name, pattern in KEY_PATTERNS.items():
replacement = r"\1=" + REDACTED if name == "assignment" else REDACTED
text = pattern.sub(replacement, text)
Expand Down Expand Up @@ -284,7 +325,10 @@ def main() -> int:
# check secfoo's numbers against the CLI's printed numbers.
"usage_file": getattr(adapter, "last_usage_payload", None),
}
text = json.dumps(payload, indent=2, ensure_ascii=False) + "\n"
# Fields that bypass redact() (for example the adapter's raw usage payload)
# still go through the home scrub here, on the final JSON-escaped text and
# before the credential check.
text = scrub_home(json.dumps(payload, indent=2, ensure_ascii=False) + "\n")
assert_no_keys(text, where="the complete fixture")

fixture = ROOT / "tests" / "fixtures" / "agents" / args.agent / version_slug(version) / f"{args.scenario}.json"
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/agents/copilot/1.0.117/auth_failure.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"json",
"--allow-all-tools",
"--usage-output-file",
"C:\\Users\\shrut\\AppData\\Local\\Temp\\secfoo-copilot-usage-5148-3166705667104.json"
"~\\AppData\\Local\\Temp\\secfoo-copilot-usage-5148-3166705667104.json"
],
"env_names_isolated": [
"COPILOT_GITHUB_TOKEN",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"json",
"--allow-all-tools",
"--usage-output-file",
"C:\\Users\\shrut\\AppData\\Local\\Temp\\secfoo-copilot-usage-20180-2328404806688.json"
"~\\AppData\\Local\\Temp\\secfoo-copilot-usage-20180-2328404806688.json"
],
"env_names_isolated": [],
"prompt_sha256": "f9e09585d5fd5fc8ec0f7e1ae38f438dc24c9f7abbb6ed89abcddf7f3a9fc3f0",
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/agents/copilot/1.0.117/success.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/fixtures/agents/copilot/1.0.117/timeout.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"json",
"--allow-all-tools",
"--usage-output-file",
"C:\\Users\\shrut\\AppData\\Local\\Temp\\secfoo-copilot-usage-20212-2959706999840.json"
"~\\AppData\\Local\\Temp\\secfoo-copilot-usage-20212-2959706999840.json"
],
"env_names_isolated": [],
"prompt_sha256": "f9e09585d5fd5fc8ec0f7e1ae38f438dc24c9f7abbb6ed89abcddf7f3a9fc3f0",
Expand Down
96 changes: 96 additions & 0 deletions tests/test_agents/test_record_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""Regression tests for the shared real-CLI recorder's redaction helpers.

Fixtures are public evidence from the recording machine, not a place for local
usernames. These tests keep the home-directory scrub honest and make sure the
already-recorded Copilot transcripts stay free of those paths.
"""

from __future__ import annotations

import importlib.util
import json
import re
from pathlib import Path

ROOT = Path(__file__).resolve().parents[2]
_spec = importlib.util.spec_from_file_location("record_agent", ROOT / "scripts" / "record_agent.py")
assert _spec is not None and _spec.loader is not None
record_agent = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(record_agent)

# One or two backslashes covers raw and JSON-escaped Windows paths; slash
# paths cover the macOS spelling Copilot prints inside its tool output.
HOME_PATH = re.compile(r"(?:[A-Za-z]:\\{1,4}Users\\{1,4}|/Users/)[A-Za-z0-9._-]+")


def test_scrub_home_replaces_raw_escaped_and_posix_paths(monkeypatch):
# Pin the variants so the assertion does not depend on the CI machine's
# actual home directory. Order matches record_agent._HOME_VARIANTS: the
# drive-qualified slash form must be replaced before its /Users/ suffix.
monkeypatch.setattr(
record_agent,
"_HOME_VARIANTS",
(
r"C:\Users\tester",
"C:/Users/tester",
r"C:\\Users\\tester",
r"C:\\\\Users\\\\tester",
"/Users/tester",
"/home/tester",
),
)
text = (
r"C:\Users\tester\src "
"C:/Users/tester/src "
r"C:\\Users\\tester\\src "
r"C:\\\\Users\\\\tester\\\\src "
"/Users/tester/src "
"/home/tester/src "
r"C:\Users\tester2\src "
"/Users/tester2/src"
)
assert record_agent.scrub_home(text) == (
r"~\src "
"~/src "
r"~\\src "
r"~\\\\src "
"~/src "
"~/src "
r"C:\Users\tester2\src "
"/Users/tester2/src"
)


def test_final_fixture_text_is_scrubbed_before_the_key_check(monkeypatch):
"""Fields that skip redact() (e.g. the CLI usage payload) are covered."""
monkeypatch.setattr(
record_agent,
"_HOME_VARIANTS",
(
r"C:\Users\tester",
"C:/Users/tester",
r"C:\\Users\\tester",
r"C:\\\\Users\\\\tester",
"/Users/tester",
"/home/tester",
),
)
payload = {
"argv": [r"C:\Users\tester\usage.json"],
"stdout": "/Users/tester/reports",
}
text = record_agent.scrub_home(json.dumps(payload, indent=2, ensure_ascii=False) + "\n")

assert "tester" not in text
assert text.count("~") == 2
record_agent.assert_no_keys(text, where="the complete fixture")


def test_copilot_recordings_contain_no_home_paths():
fixtures = ROOT / "tests" / "fixtures" / "agents" / "copilot" / "1.0.117"
paths = sorted(fixtures.glob("*.json"))
assert len(paths) == 4

for path in paths:
text = path.read_text(encoding="utf-8")
assert HOME_PATH.search(text) is None, f"{path.name} still contains a home path"
Loading