Skip to content

The agent that grows with you

fix(skills): cover expanduser-wrapped paths and Path read variants in py_read_secrets_file

chelsealong9cfea1db5fdc
diff --git a/tests/tools/test_skills_guard.py b/tests/tools/test_skills_guard.py
index 97faa6456b3..82944b0be28 100644
--- a/tests/tools/test_skills_guard.py
+++ b/tests/tools/test_skills_guard.py
@@ -606,6 +606,53 @@ class TestFalsePositiveReductions:
fi.pattern_id == "py_read_secrets_file" for fi in scan_file(f, name)
), name
+ def test_python_expanduser_wrapped_credential_read_is_critical(self, tmp_path):
+ # Follow-up gap: `open(os.path.expanduser(...))` / `Path(os.path.expanduser(...))` were
+ # both clean, since the credential-file alternatives required a quote immediately after
+ # the opening `(`.
+ expanduser_open = tmp_path / "steal_expanduser_open.py"
+ expanduser_open.write_text(
+ "import os\ndef _steal():\n return open(os.path.expanduser('~/.hermes/.env')).read()\n",
+ encoding="utf-8",
+ )
+ assert any(
+ fi.pattern_id == "py_read_secrets_file" and fi.severity == "critical"
+ for fi in scan_file(expanduser_open, "steal_expanduser_open.py")
+ )
+
+ expanduser_path = tmp_path / "steal_expanduser_path.py"
+ expanduser_path.write_text(
+ "import os\nfrom pathlib import Path\n"
+ "def _steal():\n return Path(os.path.expanduser('~/.hermes/.env')).read_text()\n",
+ encoding="utf-8",
+ )
+ assert any(
+ fi.pattern_id == "py_read_secrets_file" and fi.severity == "critical"
+ for fi in scan_file(expanduser_path, "steal_expanduser_path.py")
+ )
+
+ # The expanduser wrapper must not defeat the write-mode exemption either.
+ setup = tmp_path / "setup_expanduser.py"
+ setup.write_text(
+ "import os\nopen(os.path.expanduser('~/.hermes/.env'), 'w')\n", encoding="utf-8"
+ )
+ assert not any(fi.pattern_id == "py_read_secrets_file" for fi in scan_file(setup, "setup_expanduser.py"))
+
+ def test_python_path_read_variants_are_critical(self, tmp_path):
+ # Follow-up gap: the Path branch pinned `read_text`, so `read_bytes`/`readlines`/`readline`
+ # on a known credential file stayed clean.
+ for name, content in {
+ "steal_read_bytes.py": "from pathlib import Path\nPath('~/.ssh/id_rsa').read_bytes()\n",
+ "steal_readlines.py": "from pathlib import Path\nPath('~/.hermes/.env').readlines()\n",
+ "steal_readline.py": "from pathlib import Path\nPath('~/.hermes/.env').readline()\n",
+ }.items():
+ f = tmp_path / name
+ f.write_text(content, encoding="utf-8")
+ assert any(
+ fi.pattern_id == "py_read_secrets_file" and fi.severity == "critical"
+ 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 83d8e11e739..21ffc82cf81 100644
--- a/tools/skills_guard.py
+++ b/tools/skills_guard.py
@@ -154,16 +154,17 @@ THREAT_PATTERNS = [
"read_secrets_file", "critical", "exfiltration", "reads known secrets file"),
(r'\b(?:readFile(?:Sync)?|readTextFile)\s*\(\s*["\'][^"\'\n]*(?:\.ssh[/\\]id_(?:rsa|ed25519|ecdsa|dsa)(?!\.pub)|\.env\b|credentials\b|\.netrc\b|\.pgpass\b|\.npmrc\b|\.pypirc\b)[^"\'\n]*["\']',
"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. `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*\(',
+ # Python twin of js_read_secrets_file: `open(...)` on a literal credential path (optionally
+ # `os.path.expanduser(...)`-wrapped), or the `Path(...).read_text/_bytes/lines/line(...)` chain
+ # — the shapes that read a known secrets file's 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, tolerating the expanduser wrapper's own `)` — from the
+ # `open(...)` branch; `Path(...).read_*()` has no mode argument, so needs no exclusion.
+ (r'\bopen\s*\(\s*(?:os\.path\.expanduser\s*\(\s*)?["\'][^"\'\n]*(?:\.ssh[/\\]id_(?:rsa|ed25519|ecdsa|dsa)(?!\.pub)|\.env\b|credentials\b|\.netrc\b|\.pgpass\b|\.npmrc\b|\.pypirc\b)[^"\'\n]*["\']\s*\)?'
+ r'(?!\s*\)?\s*,\s*["\'][^"\']*[wax][^"\']*["\'])(?![^\n]*\bmode\s*=\s*["\'][^"\']*[wax])'
+ r'|\bPath\s*\(\s*(?:os\.path\.expanduser\s*\(\s*)?["\'][^"\'\n]*(?:\.ssh[/\\]id_(?:rsa|ed25519|ecdsa|dsa)(?!\.pub)|\.env\b|credentials\b|\.netrc\b|\.pgpass\b|\.npmrc\b|\.pypirc\b)[^"\'\n]*["\']\s*\)?\s*\)\.(?:read_text|read_bytes|readlines|readline)\s*\(',
"py_read_secrets_file", "critical", "exfiltration", "Python reads a known credential file"),
# ── Exfiltration: programmatic env access ──
(r'printenv|env\s*\|', "dump_all_env", "high", "exfiltration", "dumps all environment variables"),