diff --git a/bin/rule_hook/env_file.py b/bin/rule_hook/env_file.py index 51dec0be..8dee6803 100644 --- a/bin/rule_hook/env_file.py +++ b/bin/rule_hook/env_file.py @@ -22,20 +22,19 @@ def env_file_breaches(call: Call, home: str) -> list[Breach]: def reads(call: Call, path: pathlib.Path, home: str) -> bool: + """Whether a Read, a Grep or else a Bash command reads path.""" if call.tool == "Read": return names(resolved(call.cwd, text(call.given, "file_path"), home), path) if call.tool == "Grep": searched = resolved(call.cwd, text(call.given, "path") or ".", home) return names(searched, path) or holds(searched, path) and not ignored(path) - if call.tool == "Bash": - command = text(call.given, "command") - relative = os.path.relpath(path, home) - spellings = "|".join(re.escape(spelling) for spelling in (str(path), *(f"{prefix}/{relative}" for prefix in ("~", "$HOME", "${HOME}")))) - # Bounded on both sides, so a longer path that holds a spelling, such as ~/.claude/.envrc, is another file. - spelt = re.search(rf"(? bool: diff --git a/bin/rule_hook/main.py b/bin/rule_hook/main.py index b383d458..7d6b5639 100644 --- a/bin/rule_hook/main.py +++ b/bin/rule_hook/main.py @@ -91,7 +91,8 @@ def breaches_of(call: Call, home: str, environ: typing.Mapping[str, str]) -> tup if call.tool == "Read": return "", text(call.given, "file_path"), read_breaches(call, home) + env_file_breaches(call, home) if call.tool == "Grep": - return "", text(call.given, "path"), env_file_breaches(call, home) + # Only env-file-read checks a Grep, and it takes no override, so no marker or log line concerns the call. + return "", "", env_file_breaches(call, home) path = text(call.given, "file_path") if call.tool not in FILE_TOOLS or not path: return None diff --git a/bin/rule_hook/test_rule_hook.py b/bin/rule_hook/test_rule_hook.py index 6eaaeef6..1a8fe9ba 100644 --- a/bin/rule_hook/test_rule_hook.py +++ b/bin/rule_hook/test_rule_hook.py @@ -380,6 +380,14 @@ class EnvFileRead(unittest.TestCase): self.assertEqual(self.breached("Read", {"file_path": f"{self.home}/.claude/.env"}), ["env-file-read"]) self.assertEqual(self.breached("Read", {"file_path": "acme.env"}, f"{self.home}/secrets"), ["env-file-read"]) + def test_a_read_spelt_from_home_is_refused_naming_the_file(self): + found = breaches_of(Call("Read", {"file_path": "~/.claude/.env"}, "/r"), str(self.home), {}) + self.assertEqual([breach.detail for breach in found[2]], [f"the call reads {self.home}/.claude/.env, whose credentials would land in the transcript"]) + + def test_the_log_names_a_read_by_its_file_and_a_command_by_its_first_line(self): + self.assertEqual(breaches_of(Call("Read", {"file_path": "a.txt"}, "/r"), str(self.home), {})[1], "a.txt") + self.assertEqual(breaches_of(Call("Bash", {"command": "ls -l\nls -a\nls"}, "/r"), str(self.home), {})[1], "ls -l") + def test_a_read_through_a_symlink_is_refused(self): (self.home / "link").symlink_to(self.home / "secrets" / "acme.env") self.assertEqual(self.breached("Read", {"file_path": f"{self.home}/link"}), ["env-file-read"]) @@ -391,6 +399,7 @@ class EnvFileRead(unittest.TestCase): self.assertEqual(self.breached("Grep", {"pattern": "TOKEN", "path": f"{self.home}/secrets/acme.env"}), ["env-file-read"]) self.assertEqual(self.breached("Grep", {"pattern": "TOKEN", "path": str(self.home)}), ["env-file-read"]) self.assertEqual(self.breached("Grep", {"pattern": "TOKEN"}, f"{self.home}/secrets"), ["env-file-read"]) + self.assertEqual(self.breached("Grep", {"pattern": "TOKEN", "path": "~/secrets"}), ["env-file-read"]) def test_a_grep_of_a_folder_whose_checkout_ignores_the_file_or_of_another_folder_passes(self): subprocess.run(["git", "init", "--quiet", str(self.home / ".claude")], check=True, env=isolated_env()) @@ -399,12 +408,13 @@ class EnvFileRead(unittest.TestCase): self.assertEqual(self.breached("Grep", {"pattern": "TOKEN", "path": f"{self.home}/code"}), []) def test_a_command_naming_the_file_is_refused(self): - for command in ("cat ~/.claude/.env", f"grep TOKEN {self.home}/secrets/acme.env", "source $HOME/secrets/acme.env", "wc < ${HOME}/.claude/.env", "cd ~/secrets && cat acme.env"): + for command in ("cat ~/.claude/.env", "sh -c 'cat ~/.claude/.env'", f"sh -c 'cat {self.home}/secrets/acme.env'", f"grep TOKEN {self.home}/secrets/acme.env", "source $HOME/secrets/acme.env", "wc < ${HOME}/.claude/.env", "cd ~/secrets && cat acme.env"): with self.subTest(command): self.assertEqual(self.breached("Bash", {"command": command}), ["env-file-read"]) def test_a_command_naming_another_file_passes(self): self.assertEqual(self.breached("Bash", {"command": f"cat ~/.claude/.envrc /backup{self.home}/.claude/.env .env"}, "/r"), []) + self.assertEqual(self.breached("Bash", {"command": "cat 'a\0b'"}, f"{self.home}/.claude"), []) class ReviewerReport(unittest.TestCase):