Skip to content

The agent that grows with you

fix(skills): exclude write/append/exclusive open() modes from py_read_secrets_file

chelsealong128c737f4dba
diff --git a/tests/tools/test_skills_guard.py b/tests/tools/test_skills_guard.py
index 1e9c0e8ad6f..97faa6456b3 100644
--- a/tests/tools/test_skills_guard.py
+++ b/tests/tools/test_skills_guard.py
@@ -589,6 +589,23 @@ class TestFalsePositiveReductions:
benign.write_text('def _load():\n return open("config.yaml").read()\n', encoding="utf-8")
assert not any(fi.pattern_id == "py_read_secrets_file" for fi in scan_file(benign, "read_config.py"))
+ def test_python_open_write_of_credential_file_is_not_a_secrets_read(self, tmp_path):
+ # #116950 follow-up: a setup script that WRITES its own .env/credentials/.npmrc
+ # (the same action the `cat >` heredoc exemption above protects for shell) must not
+ # trip py_read_secrets_file — only a READ of a known credential file is exfiltration.
+ for name, content in {
+ "write_mode.py": 'def _setup():\n with open(".env", "w") as fh:\n fh.write("KEY=1")\n',
+ "append_mode.py": 'open(".npmrc", "a").write("registry=x")\n',
+ "write_binary.py": 'open("credentials.json", "wb")\n',
+ "exclusive_mode.py": 'open(".env", "x")\n',
+ "mode_kwarg_write.py": 'open(".env", mode="w")\n',
+ }.items():
+ f = tmp_path / name
+ f.write_text(content, encoding="utf-8")
+ assert not any(
+ fi.pattern_id == "py_read_secrets_file" for fi in scan_file(f, name)
+ ), name
+
def test_allowed_tools_frontmatter_is_low_severity_only(self, tmp_path):
# Required SKILL.md frontmatter per the agent-skill spec.
skill_dir = tmp_path / "ok-skill"
diff --git a/tools/skills_guard.py b/tools/skills_guard.py
index e5cbc3fa880..83d8e11e739 100644
--- a/tools/skills_guard.py
+++ b/tools/skills_guard.py
@@ -156,8 +156,13 @@ THREAT_PATTERNS = [
"js_read_secrets_file", "critical", "exfiltration", "JavaScript reads a known credential file"),
# Python twin of js_read_secrets_file: `open(...)` on a literal credential path, or the
# `Path(...).read_text(...)` chain — the two shapes that read a known secrets file's
- # content in Python without going through the shell `cat` pattern above.
+ # content in Python without going through the shell `cat` pattern above. `open()`, unlike
+ # readFile/read_text, is also how a plugin WRITES its own .env/credentials/.npmrc during
+ # setup, so (mirroring the shell `cat`'s `(?!>)`) exclude a write/append/exclusive mode —
+ # a literal 2nd-arg string containing w/a/x, or a `mode=` kwarg with the same — from the
+ # `open(...)` branch; `Path(...).read_text()` has no mode argument, so needs no exclusion.
(r'\bopen\s*\(\s*["\'][^"\'\n]*(?:\.ssh[/\\]id_(?:rsa|ed25519|ecdsa|dsa)(?!\.pub)|\.env\b|credentials\b|\.netrc\b|\.pgpass\b|\.npmrc\b|\.pypirc\b)[^"\'\n]*["\']'
+ r'(?!\s*,\s*["\'][^"\']*[wax][^"\']*["\'])(?![^\n]*\bmode\s*=\s*["\'][^"\']*[wax])'
r'|\bPath\s*\(\s*["\'][^"\'\n]*(?:\.ssh[/\\]id_(?:rsa|ed25519|ecdsa|dsa)(?!\.pub)|\.env\b|credentials\b|\.netrc\b|\.pgpass\b|\.npmrc\b|\.pypirc\b)[^"\'\n]*["\']\s*\)\.read_text\s*\(',
"py_read_secrets_file", "critical", "exfiltration", "Python reads a known credential file"),
# ── Exfiltration: programmatic env access ──