Commit bc79bb5d
bc79bb5d3dadMergedon mrmrs-a60a8c
mrmrs · 13 days ago · 4 files · +1 added · ~3 modified
Details
- Commit
- bc79bb5d3dad
- Parent
- 094f0e0f5a0e
cloudflare/agent-runner/server.pymodifiedView file
@@ -8,8 +8,10 @@
88
99
Steps: write the manifest (container config paths, isolation attestation),1010
`agent_campaign.py plan`, containment self-check as the agent user, `run`,11-
`report`. Secrets are passed to the harness environment only; they are never12-
written to disk or echoed in status.11+
`report`. Secrets are passed to the harness environment only. The agent holds12+
the key and could print it, so every file is scrubbed of each secret value13+
before the bundle is packaged, and status output is scrubbed too14+
(redaction.json records counts, never values).1315
"""1416
from __future__ import annotations1517
@@ -117,6 +119,7 @@
117119
manifest["isolation_attestation"] = {"worker_root": str(WORK), "unrestricted_cli_acknowledged": True}118120
(job_dir / "manifest.json").write_text(json.dumps(manifest, indent=2) + "\n")119121
secrets = {k: v for k, v in (request.get("secrets") or {}).items() if k in SECRET_KEYS and isinstance(v, str)}122+
job["redact"] = {k: v for k, v in secrets.items() if len(v) >= 8}120123
env = harness_env(job_dir, secrets)121124
campaign = job_dir / "campaign"122125
script = str(BENCH / "scripts" / "agent_campaign.py")@@ -146,11 +149,36 @@
146149
job["step"] = None147150
148151
152+
def scrub_text(job: dict, text: str) -> str:153+
for name, value in job.get("redact", {}).items():154+
text = text.replace(value, f"[REDACTED:{name}]")155+
return text156+
157+
158+
def scrub_tree(job: dict) -> dict:159+
"""Replace every secret value in every file under the job dir. Returns counts."""160+
counts = {name: 0 for name in job.get("redact", {})}161+
needles = {name: value.encode() for name, value in job.get("redact", {}).items()}162+
for path in job["dir"].rglob("*"):163+
if not path.is_file() or path.is_symlink():164+
continue165+
data = path.read_bytes()166+
changed = data167+
for name, needle in needles.items():168+
hits = changed.count(needle)169+
if hits:170+
counts[name] += hits171+
changed = changed.replace(needle, f"[REDACTED:{name}]".encode())172+
if changed is not data:173+
path.write_bytes(changed)174+
return counts175+
176+
149177
def log_tail(job: dict, lines: int = 30) -> list[str]:150178
logs = sorted((job["dir"] / "logs").glob("*.log"), key=lambda p: p.stat().st_mtime) if (job["dir"] / "logs").exists() else []151179
if not logs:152180
return []153-
return logs[-1].read_text(errors="replace").splitlines()[-lines:]181+
return scrub_text(job, logs[-1].read_text(errors="replace")).splitlines()[-lines:]154182
155183
156184
class Handler(BaseHTTPRequestHandler):@@ -197,13 +225,16 @@
197225
return self.send_json(404, {"error": "unknown job"})198226
if not suffix:199227
return self.send_json(200, {"job_id": job_id, "state": job["state"], "step": job["step"],200-
"exit_codes": job["exit_codes"], "error": job["error"],228+
"exit_codes": job["exit_codes"], "error": scrub_text(job, job["error"] or "") or None,201229
"egress_policy": job.get("egress_policy"),202230
"runner_image": RUNNER_IMAGE_VERSION, "log_tail": log_tail(job)})203231
if job["state"] == "running":204232
return self.send_json(409, {"error": "job still running"})205233
archive = WORK / f"{job_id}.tar.gz"206234
if not archive.exists():235+
counts = scrub_tree(job)236+
(job["dir"] / "redaction.json").write_text(json.dumps(237+
{"scrubbed_secret_names": sorted(counts), "occurrences_replaced": counts}, indent=2) + "\n")207238
with tarfile.open(archive, "w:gz") as tar:208239
tar.add(job["dir"], arcname=job_id,209240
filter=lambda info: None if "/harness-home" in info.name else info)cloudflare/agent-runner/src/index.jsmodifiedView file
Loading diff…
scripts/cloud_campaign.pymodifiedView file
Loading diff…
tests/test_agent_runner_redaction.pyaddedView file
Loading diff…