Python
fix(security): scope find's global options so -L/-H/-P cannot bypass the read grant
Shakti Prasad Mohapatra300a0e4da51a
diff --git a/coworker/readonly.py b/coworker/readonly.pyindex 2da3e7e3..1ca764e2 100644--- a/coworker/readonly.py+++ b/coworker/readonly.py@@ -197,6 +197,42 @@ _PATH_VALUE_FLAGS = {"-f", "--file", "--exclude-from", "--include-from"}# worth scoping. Dropping them keeps the target list honest without a per-flag table._NUMERIC = re.compile(r"^[0-9]+([,:.-][0-9]+)*[a-zA-Z]?$")+# find(1)'s global options precede the starting points, so unlike the predicates they+# must not end the path list. -H/-L/-P select symlink handling; -E/-X/-d/-s/-x are BSD+# spellings; -O carries its level attached. -D takes a value (GNU debug options) and+# -f takes the starting point itself.+_FIND_GLOBAL_NO_VALUE = frozenset({"-H", "-L", "-P", "-E", "-X", "-d", "-s", "-x"})+++def _find_targets(args: list[str]) -> list[str]:+ """Starting points of `find`, with its global options skipped.++ `find -L /etc -name shadow` reads /etc exactly like `find /etc -name shadow`, but+ treating every leading `-` token as the start of the predicates returned no targets+ for the prefixed form — so the session grant's root check passed vacuously.+ """+ out: list[str] = []+ i = 0+ while i < len(args):+ tok = args[i]+ if tok in _FIND_GLOBAL_NO_VALUE or (tok.startswith("-O") and tok != "-O"):+ i += 1+ continue+ if tok == "-D" and i + 1 < len(args):+ i += 2 # debugopts, never a path+ continue+ if tok == "-f" and i + 1 < len(args):+ out.append(args[i + 1]) # BSD: -f names the starting point+ i += 2+ continue+ break+ # Starting points end at the first predicate (any other `-` token). With none, find+ # defaults to `.`, which is inside every session root.+ while i < len(args) and not args[i].startswith("-"):+ out.append(args[i])+ i += 1+ return out+def _stage_targets(argv: list[str]) -> list[str]:"""File operands of one accepted pipeline stage."""@@ -222,6 +258,9 @@ def _stage_targets(argv: list[str]) -> list[str]:breakreturn out+ if head == "find":+ return _find_targets(args)+out = []skip_next = Falseseen_operand = False@@ -239,8 +278,6 @@ def _stage_targets(argv: list[str]) -> list[str]:if tok.startswith("-"):if tok in _PATH_VALUE_FLAGS:skip_next = True- elif head == "find":- break # find's predicates start here; paths precede themcontinueif head in _PATTERN_FIRST and not seen_operand:seen_operand = True # the pattern/script/filter, not a filediff --git a/tests/test_readonly_scoping.py b/tests/test_readonly_scoping.pyindex 9495533c..967e1af6 100644--- a/tests/test_readonly_scoping.py+++ b/tests/test_readonly_scoping.py@@ -62,10 +62,24 @@ def test_reads_inside_the_granted_folder_still_run_unprompted(session, command):"grep -r AWS_SECRET /","find / -name id_rsa","head -n 5 ~/.aws/credentials",+ # find's global options must not hide the starting point from the root check:+ # before, these returned no targets and the grant waved them through.+ "find -L /etc -name shadow",+ "find -H ~/.ssh -type f",+ "find -P ~/.aws -name credentials",+ "find -D search ~/.aws",+ "find -O3 /etc -name shadow",],)def test_reads_outside_every_root_now_ask(session, command):- assert not runs(session, command), command+ assert not runs(session, command)+++def test_find_with_a_global_option_inside_the_root_still_runs(session):+ # The scoping must not undo the convenience: the prefixed form over an in-scope path+ # keeps auto-running, exactly like the unprefixed one.+ assert runs(session, "find -L . -name '*.py'")+ assert runs(session, "find -D search src"), commanddef test_openworkers_own_secrets_are_no_longer_readable(session):@@ -150,6 +164,17 @@ def test_without_the_grant_nothing_changes(tmp_path):("jq .foo data.json", ["data.json"]), # the filter is not a file("find /tmp -name x", ["/tmp"]), # predicates end the path list("nl a.txt | sed -n 2p", ["a.txt"]),+ # find's global options precede the starting points and must not end the path+ # list: `find -L /etc …` reads /etc just like the unprefixed form.+ ("find -L /tmp -name x", ["/tmp"]),+ ("find -H /tmp -type f", ["/tmp"]),+ ("find -P /tmp -type f", ["/tmp"]),+ ("find -D search /tmp", ["/tmp"]), # -D takes a value+ ("find -O3 /tmp -name x", ["/tmp"]),+ ("find -E /tmp -regex x", ["/tmp"]), # BSD global options+ ("find -f /tmp -name x", ["/tmp"]), # BSD -f carries the starting point+ ("find -L /tmp /var -name x", ["/tmp", "/var"]),+ ("find -L -name x", []), # no starting point: find defaults to `.`],)def test_read_targets_names_the_files_and_not_the_arguments(command, expected):