diff --git a/bin/rule_hook/folder_search.py b/bin/rule_hook/folder_search.py index c9155616..3f6b62b3 100644 --- a/bin/rule_hook/folder_search.py +++ b/bin/rule_hook/folder_search.py @@ -121,10 +121,7 @@ def globbed(glob: str, searched: str, path: pathlib.Path) -> bool: return False if UNMATCHED.search(glob): return True - if glob.startswith("!"): - # It only excludes files. - return False - anchored = glob.lstrip("/") if "/" in glob else f"**/{glob}" + anchored = glob.removeprefix("/") if "/" in glob else f"**/{glob}" # A `**` that is not a whole folder name matches as `*` in ripgrep; crossing folders there only refuses more. translated = {"**/": "(?:.*/)?", "**": ".*", "*": "[^/]*", "?": "[^/]"} pattern = "".join(translated.get(piece, re.escape(piece)) for piece in re.split(r"(\*\*/|\*\*|\*|\?)", anchored)) diff --git a/bin/rule_hook/test_rule_hook.py b/bin/rule_hook/test_rule_hook.py index b09aea2e..10bc0ac8 100644 --- a/bin/rule_hook/test_rule_hook.py +++ b/bin/rule_hook/test_rule_hook.py @@ -552,6 +552,15 @@ class EnvFileRead(unittest.TestCase): with self.subTest(command): self.assertEqual(self.breached("Bash", {"command": command}), expected) + def test_a_glob_matches_a_file_below_the_searched_folder_by_name_or_by_path(self): + for command, expected in (("rg -g '*.env' TOKEN ~", ["env-file-read"]), ("rg -g 'acme.en?' TOKEN ~/secrets", ["env-file-read"]), ("rg -g '/secrets/acme.env' TOKEN ~", ["env-file-read"]), ("rg -g 'acme.enx' TOKEN ~/secrets", [])): + with self.subTest(command): + self.assertEqual(self.breached("Bash", {"command": command}), expected) + + def test_a_search_s_breach_is_logged_in_the_directory_the_command_runs_in(self): + found = breaches_of(Call("Bash", {"command": "grep -r TOKEN ~/secrets"}, "/r"), str(self.home), {}) + self.assertEqual([breach.directory for breach in found[2] if breach.rule == "env-file-read"], ["/r"]) + def test_a_file_several_configs_name_is_reported_once(self): write_config(self.home, self.home / "code" / "other") config = self.home / ".claude-projects" / "other" / "config.toml"