Answer security round 1: 0 🔴, 2 🟡 (0 misread); read a short-option cluster with a number as recursive, and search the directory when no operand is a folder
This commit is contained in:
@@ -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]:
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user