Skip to content

fix: prevent false positives in recursive delete detection#68

Merged
teknium1 merged 2 commits intoNousResearch:mainfrom
cutepawss:fix/dangerous-cmd-regex-false-positive
Feb 26, 2026
Merged

fix: prevent false positives in recursive delete detection#68
teknium1 merged 2 commits intoNousResearch:mainfrom
cutepawss:fix/dangerous-cmd-regex-false-positive

Conversation

@cutepawss
Copy link
Copy Markdown
Contributor

@cutepawss cutepawss commented Feb 26, 2026

Problem

The regex pattern for detecting recursive delete commands in tools/approval.py produces false positives for any rm command where the filename starts with the letter r.

Root cause: The pattern \brm\s+(-[^\s]*)?r makes the dash-flag group (-[^\s]*)? optional. When the filename doesn't start with -, the group matches nothing, and the trailing r matches the first character of the filename instead of a flag.

Examples of false positives:

Command Expected Actual
rm readme.txt ✅ Safe ❌ Flagged as 'recursive delete'
rm requirements.txt ✅ Safe ❌ Flagged
rm report.csv ✅ Safe ❌ Flagged
rm README.md ✅ Safe ❌ Flagged
rm results.json ✅ Safe ❌ Flagged
rm robots.txt ✅ Safe ❌ Flagged

This causes unnecessary approval prompts for the user when the agent tries to delete files whose names start with 'r'.

Fix

Make the dash mandatory so only actual flags are matched:

- (r'\brm\s+(-[^\s]*)?r', "recursive delete"),
+ (r'\brm\s+-[^\s]*r', "recursive delete"),

The change from (-[^\s]*)? (optional group) to -[^\s]* (required dash) ensures the r is always part of a CLI flag like -r, -rf, -rfv, or -fr — never part of a filename.

Tests Added

Added 15 new tests in tests/tools/test_approval.py across two classes:

TestRmFalsePositiveFix (8 regression tests):

  • Verifies that rm readme.txt, rm requirements.txt, rm report.csv, rm results.json, rm robots.txt, rm run.sh, rm -f readme.txt, rm -v readme.txt are all correctly identified as safe

TestRmRecursiveFlagVariants (7 tests):

  • Verifies that rm -r, rm -rf, rm -rfv, rm -fr, rm -irf, rm --recursive, sudo rm -rf are all still correctly caught as dangerous
29 passed in 0.53s (14 existing + 15 new, 0 failures)

Scope

  • tools/approval.py: 1-line regex fix (line 25)
  • tests/tools/test_approval.py: 15 new regression tests
  • No behavior change for actual recursive delete commands
The regex pattern for detecting recursive delete commands (rm -r, rm -rf,
etc.) incorrectly matched filenames starting with 'r' — e.g., 'rm readme.txt'
was flagged as 'recursive delete' because the dash-flag group was optional.

Fix: make the dash mandatory so only actual flags (-r, -rf, -rfv, -fr)
are matched. This eliminates false approval prompts for innocent commands
like 'rm readme.txt', 'rm requirements.txt', 'rm report.csv', etc.

Before: \brm\s+(-[^\s]*)?r  — matches 'rm readme.txt' (false positive)
After:  \brm\s+-[^\s]*r     — requires '-' prefix, no false positives
Add 15 new tests in two classes:

- TestRmFalsePositiveFix (8 tests): verify filenames starting with 'r'
  (readme.txt, requirements.txt, report.csv, etc.) are NOT falsely
  flagged as 'recursive delete'

- TestRmRecursiveFlagVariants (7 tests): verify all recursive delete
  flag styles (-r, -rf, -rfv, -fr, -irf, --recursive, sudo rm -rf)
  are still correctly caught

All 29 tests pass (14 existing + 15 new).
@teknium1 teknium1 merged commit a8ccaca into NousResearch:main Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants