Awesome Reviewers

Reviewer · Security

Harden security-critical validators

When writing security-enforcing logic such as command blocklists, path-sandbox checks, or other allow/deny validators, treat correctness as safety-critical and verify it explicitly rather than by inspection:

Instruction

When writing security-enforcing logic such as command blocklists, path-sandbox checks, or other allow/deny validators, treat correctness as safety-critical and verify it explicitly rather than by inspection:

  1. Test both directions of every boundary. For each anchor/lookahead/regex boundary, add a test that must match and one that must not. A shared trailing boundary (e.g. a single \b) often cannot satisfy two conflicting requirements at once — e.g. su must reject su-exec/su.sh while mkfs must still match mkfs.ext4. Give each word its own anchor when the requirements differ, and run concrete adversarial strings (su-exec nobody ls, mkfs.ext4 /dev/sda, sudoedit /etc/hosts, nice reboot, sh -c 'sudo id') against the compiled pattern before merging, not just the “happy path” examples in the description.

  2. Keep normalization consistent across all checks in the same function. If part of a validator compares against a lower-cased/normalized string and another part compares against the raw input, document why, and prefer compiling with the appropriate flag (e.g. re.IGNORECASE) against one canonical string so all branches see the same input. Divergent normalization is a source of silent bypasses.

  3. Guard against ReDoS on attacker-controlled length. Whenever a pattern contains a repeated group with overlapping alternatives (e.g. (?:\w+=\S*\s+|-\S*\s+|\d\S*\s+)*), check for catastrophic backtracking by benchmarking on long adversarial input, especially after raising any length caps. Make alternatives mutually exclusive (disjoint character classes) rather than bounding repetition count, since a fixed bound can itself be bypassed.

  4. Validate the exact value that will be used. A check that validates one representation of a resource (e.g. os.path.realpath) but then acts on a different representation (the original, unresolved path) leaves a TOCTOU gap — a symlink swapped in between check and use can escape the sandbox. Either act on the validated/resolved value, or explicitly document the residual race.

  5. Give actionable rejection messages. Surface which pattern/word caused a block (e.g. Command blocked: runs a privileged command 'printenv') instead of a generic message, so callers (including an LLM agent) can self-correct.

  6. Record intentional coverage tradeoffs. If tightening a validator to remove false positives narrows true-positive coverage (e.g. moving from “blocked anywhere in string” to “blocked only at a recognized command position”), state this tradeoff explicitly in the PR/description and track follow-up gaps (new wrapper commands, command substitution, filesystem case-insensitivity) as separate, scoped issues rather than silently shipping a weaker guarantee.

Example of per-word anchoring instead of one shared boundary:

_BASH_COMMAND_WORDS = (
    "sudo",
    r"su(?![\w.-])",   # su-exec, su.sh must stay allowed
    "mkfs",            # shared \b already matches mkfs.ext4
    "printenv",
)

Treat any new blocklist, sandbox check, or auth boundary the same way: write the failing case first, confirm it fails on the old code, confirm it passes on the new code, and keep the validated and acted-upon values identical.</description> </invoke>