Repository navigation
Bash: accept only plain commands for marker restore and rule-based approval - #1
Merged
Merged
Conversation
… grammar
Second review: values still executed. Each earlier fix held for the reported
case and was bypassed by a neighbour, always the same way: a value placed
safely into a variable, then used by the rest of the command where bash or
zsh evaluates it (RANDOM=[[M]], printf -v "$x", ${!x}, ${y:x}, ${x@P},
wait -p "$x", [ $o "$y" ], zsh export "$x", set -A "$x").
The posture changes from "refuse what is known dangerous" to "accept only
what is known plain". A command with a marker is restored only if the whole
command fits this grammar (plain.py, new, shared with the permission code):
- simple commands joined by | && || ; or newlines, with redirections to
and from literal paths;
- no $ of any kind outside single quotes, so no variable exists that could
carry a value; no backticks, backslashes, globs, braces, ~user, heredocs,
here-strings, subshells, groups, loops, conditionals, functions,
assignments, comments or background jobs;
- every command name a literal unquoted word that is an external program;
of the shell builtins only echo, true, : and printf with a literal format;
never env, sudo, xargs, a shell, eval, awk, sed, an interpreter, ssh,
make, docker, find -exec and the like;
- a marker only as a whole unquoted argument or inside a single- or
double-quoted argument; never in a command name or redirection target and
never glued to unquoted text;
- a value that would start an argument with "-" only after a literal "--"
word in the same command.
Everything else is refused as a whole and not run. Of the 42 command shapes
the fuzz accepted before, 32 are refused now (substitutions, backticks,
heredocs, here-strings, assignments, export, for, if, groups, subshells,
$'..', comments, markers glued to unquoted text).
Tests: every case from both reviews is refused through the real runner under
bash and zsh with the value that ran in the review; 89 hostile values and
300 random ones in 23 accepted contexts under bash, zsh and dash; 180 refused
placements; the structural fuzz with a vocabulary of what the grammar
accepts, and three deliberately broken grammars that it must catch.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Smu5UBDZuKK1PoTZh8DNKv
…at is not plain Second review: the Read/Edit fix and the Bash deny rules were bypassed by partly quoted globs (cat "s"ecret.en*), heredocs and continuations the splitter misread, cd anywhere but in first position, .. through a symlinked directory, per-command directory options (tar -C, git -C, env --chdir), a marker next to a glob, and combined short options (grep -hfsecret.env). decide() could also be made to run for 49 s. One strict recogniser (plain.py, shared with marker restoration) now answers one question first: is the command PLAIN? Literal words only, joined by | && || ; or newlines, with redirections to literal paths; no expansion of any kind, no globs, braces, ~user, backslashes, heredocs, here-strings, subshells, control flow, assignments, comments, NUL; no shell keyword or builtin as a command name except a few that evaluate nothing; cd, pushd and popd only as the single command of a call. - NOT PLAIN: never allowed. While any deny or ask rule exists (Bash, Read or Edit) the ask is firm: it stays an ask in bypassPermissions and auto and is a deny in dontAsk. A best-effort text scan (bounded, no filesystem) still lets a visible deny or ask rule apply; finding nothing proves nothing. - PLAIN: sub-commands for Bash rules come from the recogniser's parse. Every word, redirection target, what follows = : , @ in a word and every tail of a short-option cluster is checked as a path, relative to the working directory and to every existing directory the command names (which covers -C, --directory, --chdir and the like without a list of options), resolved by the filesystem without normalising first, and normalised as text. - SIMPLE (may be approved by an allow rule) is now: plain, one command, no redirection, no newline. A command with $VAR or a glob is no longer simple. - Paths and patterns are NFC-normalised and case-folded on macOS, also in may_cover_inside. Patterns that name only their anchor (~/, //, ., ./) and patterns with empty or . segments now match. A pattern with a .. segment is unsupported: while one exists nothing is approved without a prompt, and /datafog:doctor says so. - Bounded work: path length and segments are capped, there is no glob expansion on disk any more, one regular-expression match per rule and path. The slowest adversarial input in the tests takes about 0.2 s. Tests: 56 commands from the review against a Read deny rule and 8 against a Bash deny rule in all six modes (none is allow or undecided), -fPATH and option clusters, the project-directory anchor, macOS case and Unicode form, anchor-only and unsupported patterns, 15 adversarial inputs under a one-second limit. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Smu5UBDZuKK1PoTZh8DNKv
A longer or zero-filled key file was accepted as a key. Anything else than 32 non-zero-only bytes now counts as damaged and is replaced by the hook. /datafog:doctor also lists Read/Edit rules whose pattern the plugin cannot interpret. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Smu5UBDZuKK1PoTZh8DNKv
… command; the grammar in the README Live driver: L28, `cat "s"ecret.en*` against the Read deny rule must not be allowed (default mode; headless: denied). L29, `x=<marker>; echo "$x"` with restore_in_bash=allow must be refused by the hook, not restored. 29 runs of at most 30. The existing cases are unchanged: they are all plain commands. README: the positive grammar is now the definition of when a marker is restored (plain commands, no $, external programs, marker as a whole or quoted argument, "--" before a value that starts with "-"). New section "Plain commands": what is plain, that a command which is not plain is never approved by a rule, and that with any deny or ask rule it prompts in every mode, bypassPermissions and auto included, and is denied in headless runs. The same is said next to the recommended deny rules. Unsupported patterns (a ".." segment) and anchor-only patterns are described. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Smu5UBDZuKK1PoTZh8DNKv
The parent of the temp dir is under /var/folders on macOS, which the live driver accepts, so the old choice ran the driver instead of refusing. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Smu5UBDZuKK1PoTZh8DNKv
Third review: "any external program except the ones on a deny-list" still ran a value as code: perl5.99 -e [[M]] (version-suffixed interpreters and shells), git -c "alias.z=!cmd", tar --checkpoint-action=exec=, capsh, rsync -e, scp -S, socat EXEC:, nc -e, man -P, less, fzf --preview. And zsh evaluates the argument of printf %d as arithmetic, so a value shaped like psvar[$(cmd)1] ran cmd. The restore decision is now an allow-list. A marker is restored only when EVERY simple command of the whole command runs a listed program, named by its bare name or by an absolute path in /bin, /usr/bin, /usr/local/bin or /opt/homebrew/bin. No version suffixes, no relative paths, no other directories. - May take markers as arguments: grep egrep fgrep rg cat head tail wc nl tac rev fold cut tr paste join comm cmp column diff ls stat basename dirname file sort printf true : - Listed, but take no marker (they write to or create what an argument names): uniq tee cp mv mkdir touch - Options refused anywhere in a command with a marker, abbreviations included: rg --pre/--pre-glob/--hostname-bin; sort --compress-program, -o/--output, -T, --files0-from, --random-source; file -m/-C/-f. - printf only as printf [--] FORMAT args with a literal format made of text, \n, \t, \\, \0, %% and %s. Every other conversion is refused. - echo is no longer accepted: zsh and dash interpret backslashes in its arguments, so a value was not delivered byte for byte. - curl, wget and every other network tool are not listed: with a marker they are refused, not asked about. The audit of each program is a comment table above the list in shellsub.py. New setting restore_programs_extra (exact program names; the user vouches that the program treats its arguments as data). It is read only from the user's config.json in the plugin data dir and from the environment, not from runtime.json, plugin options or anything in the project; shells, builtins and pure wrappers cannot be added. The hook now refuses Write and Edit on the plugin's config files and Bash commands that name them. Tests: 65 cases from the review refused, through the real runner under bash and zsh with a real zsh array name in the value for the printf ones; the fuzz layers use the list (the test receivers are vouched for through the setting); five deliberately broken variants must be caught, among them a list that matches loosely and a printf that takes any format. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Smu5UBDZuKK1PoTZh8DNKv
…xact rule; one-digit descriptors - decide() took 2-5 s for `ls <7 directories> <19 KB of colon-suffix words>`. Candidate paths per word (12), per command (600), base directories (8) and resolutions (2,500) are capped and realpath results cached; past a cap the command is not cleared and asks in every mode. The reviewer's shape now takes about 0.05 s. - `test -v 'x[$(cmd)]'`, test with an arithmetic operator and `printf -v NAME`, typed literally, make bash evaluate the operand. A wildcard allow rule no longer approves them; an exact rule does. The comment in plain.py that claimed these builtins evaluate nothing is corrected. - `P 12>&1`: bash reads a descriptor, zsh and dash an argument. More than one digit glued to a redirection is not plain. - Tests pin that a command name starting with "-" is not plain. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Smu5UBDZuKK1PoTZh8DNKv
… and its setting in the README Live driver, still 29 runs of at most 30: - L25 (hostile value) no longer uses the local helper ./argdump, which is not a listed program. It runs `grep -cxF -- VALUE nasty.txt`: grep counts the lines that equal its argument taken as a fixed string, the file holds the hostile value on one line, so "1" proves exact single-argument delivery; no PWNED file may appear. - L29 now checks that a marker for a program that is not on the list (./argdump, allowed by a Bash rule) is refused by the hook and the program does not run. The variable case it replaces is covered by unit tests. README: the list of programs that may receive a value, why it is an allow-list, the refused options, the printf format rule, that echo and network programs are off the list, how to use a value with any other program (write it to a file with the Write tool), and the restore_programs_extra setting with what adding a name means. /datafog:status shows the extra programs. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Smu5UBDZuKK1PoTZh8DNKv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replaces block-lists with one strict recogniser (
engine/datafog_guard/plain.py) shared by marker restoration and permission decisions.Follows two independent reviews. Not yet run against real Claude Code at this commit.
Generated by Claude Code