From 8c3a554641801e013c2c7e48320f3fe866a39b9a Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Fri, 13 Nov 2026 22:07:31 +0100 Subject: [PATCH] =?UTF-8?q?Answer=20security=20round=201:=200=20?= =?UTF-8?q?=F0=9F=94=B4,=202=20=F0=9F=9F=A1=20(0=20misread);=20read=20a=20?= =?UTF-8?q?short-option=20cluster=20with=20a=20number=20as=20recursive,=20?= =?UTF-8?q?and=20search=20the=20directory=20when=20no=20operand=20is=20a?= =?UTF-8?q?=20folder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/rule_hook/env_file.py | 9 ++++----- bin/rule_hook/test_rule_hook.py | 7 ++++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/bin/rule_hook/env_file.py b/bin/rule_hook/env_file.py index 013edd1f..0497c3f5 100644 --- a/bin/rule_hook/env_file.py +++ b/bin/rule_hook/env_file.py @@ -19,9 +19,9 @@ from command_lines.shell_command import launched, lexed, OPERATOR, resolved, sim # Tools that search a folder's files: grep only given an option that makes it recursive, rg and ag always. GREPS = frozenset({"egrep", "fgrep", "grep"}) SEARCHERS = GREPS | {"ag", "rg"} -RECURSIVE_GREP = re.compile(r"-[A-Za-z]*[rR][A-Za-z]*|--(?:dereference-)?recursive|--directories=recurse") +RECURSIVE_GREP = re.compile(r"-[^-]*[rR].*|--(?:dereference-)?recursive|--directories=recurse") # An option that gives the pattern, so the first operand is a folder: -e and -f in a cluster, and --regexp and --file. -PATTERN_OPTION = re.compile(r"-[A-Za-z]*[ef][A-Za-z]*|--(?:regexp|file)(?:=.*)?") +PATTERN_OPTION = re.compile(r"-[^-]*[ef].*|--(?:regexp|file)(?:=.*)?") # rg's --glob and --iglob, and -g in a cluster: group 1 or 2 holds the glob when it is in the same word, else it is the next word. GLOB_OPTION = re.compile(r"--i?glob(?:=(.*))?|-[A-Za-z]*?g(.*)") # What a Grep's glob holds beyond literals, `*`, `?` and `/`: forms globbed() does not translate. @@ -112,13 +112,12 @@ def split(arguments: list[str]) -> tuple[list[str], list[str]]: def folders(directory: str, options: list[str], operands: list[str], home: str) -> list[str]: - """The folders a search reads: its operands that are folders, or the directory it runs in where none names a file or a folder. An option's value is an operand here; one that names no file or folder is dropped like any word that does not.""" + """The folders a search reads: its operands that are folders, or the directory it runs in where none is. An option's value is an operand here; one that is no folder is dropped like any word that is none.""" if not any(PATTERN_OPTION.fullmatch(option) for option in options): # The first operand is the pattern. operands = operands[1:] named = [resolved(directory, home_word(operand), home) for operand in operands] - existing = [word for word in named if os.path.exists(word)] - return [word for word in existing if os.path.isdir(word)] if existing else [directory] + return [word for word in named if os.path.isdir(word)] or [directory] def globs_of(arguments: list[str]) -> list[str]: diff --git a/bin/rule_hook/test_rule_hook.py b/bin/rule_hook/test_rule_hook.py index 3225ac50..2c133669 100644 --- a/bin/rule_hook/test_rule_hook.py +++ b/bin/rule_hook/test_rule_hook.py @@ -466,7 +466,7 @@ class EnvFileRead(unittest.TestCase): "grep -d recurse TOKEN ~/secrets", "rg TOKEN ~/secrets", "rg -n TOKEN ~/secrets", "ag TOKEN ~/secrets", "nice rg TOKEN ~/secrets", "rg -e TOKEN ~/secrets", "rg -A 3 TOKEN ~/secrets", "cat x | rg -g '*.env' TOKEN ~/secrets", "rg -eTOKEN ~/secrets", "rg --regexp=TOKEN ~/secrets", "grep -rf patterns ~/secrets", "rg TOKEN -- ~/secrets", "rg -- TOKEN ~/secrets", "ag -g '*.md' TOKEN ~/secrets", - "grep TOKEN x; rg TOKEN ~/secrets", "echo hi && ag TOKEN ~/secrets", + "grep TOKEN x; rg TOKEN ~/secrets", "grep -rm1 TOKEN ~/secrets", "grep -rC2 TOKEN ~/secrets", "rg -e1 -nA3 TOKEN ~/secrets", "echo hi && ag TOKEN ~/secrets", ) for command in commands: with self.subTest(command): @@ -478,6 +478,11 @@ class EnvFileRead(unittest.TestCase): self.assertEqual(self.breached("Bash", {"command": "cd ~/secrets && grep -rn -A 3 TOKEN"}), ["env-file-read"]) self.assertEqual(self.breached("Bash", {"command": "cd ~/code && rg TOKEN ~/secrets"}), ["env-file-read"]) self.assertEqual(self.breached("Bash", {"command": "ag TOKEN"}, f"{self.home}/secrets"), ["env-file-read"]) + self.assertEqual(self.breached("Bash", {"command": "grep -rnA3 TOKEN"}, f"{self.home}/secrets"), ["env-file-read"]) + # patterns is a file the cwd holds, given as the value of -f. + (self.home / "secrets" / "patterns").write_text("TOKEN\n") + self.assertEqual(self.breached("Bash", {"command": "rg -f patterns"}, f"{self.home}/secrets"), ["env-file-read"]) + self.assertEqual(self.breached("Bash", {"command": "grep -rf patterns"}, f"{self.home}/secrets"), ["env-file-read"]) def test_a_search_of_another_folder_or_a_grep_that_is_not_recursive_passes(self): (self.home / "code").mkdir()