Skip to content

The agent that grows with you

fix(approval): anchor uninstall rules at command position, allow option operands

teknium1ce0b10cb2173
diff --git a/tests/tools/test_approval.py b/tests/tools/test_approval.py
index 502c262bc86..b1ed8d09afb 100644
--- a/tests/tools/test_approval.py
+++ b/tests/tools/test_approval.py
@@ -25,6 +25,8 @@ class TestPackageManagerUninstallApproval:
@pytest.mark.parametrize("command", [
"npm uninstall -g left-pad", "npm r left-pad", "pnpm un -g left-pad",
"yarn global remove left-pad", "pip3 uninstall left-pad", "brew rm left-pad",
+ "npm --prefix ./app uninstall left-pad", "pip --proxy http://p:1 uninstall -y requests",
+ "cd app && yarn --cwd ./app remove left-pad",
])
def test_uninstall_requires_approval(self, command):
dangerous, key, _ = detect_dangerous_command(command)
@@ -32,6 +34,7 @@ class TestPackageManagerUninstallApproval:
@pytest.mark.parametrize("command", [
"npm update -g left-pad", "pnpm add left-pad", "yarn install", "pip install left-pad", "brew upgrade left-pad",
+ 'git commit -m "document npm uninstall usage"', 'echo "pip uninstall foo"',
])
def test_install_and_update_stay_unprompted(self, command):
assert detect_dangerous_command(command) == (False, None, None)
diff --git a/tools/approval_detection.py b/tools/approval_detection.py
index 83fbbbb5307..cca7ea59db4 100644
--- a/tools/approval_detection.py
+++ b/tools/approval_detection.py
@@ -82,6 +82,8 @@ _HARDLINE_SYSTEM_DIRS = (r'/home|/home/\*|/root|/root/\*|/etc|/etc/\*|/usr|/usr/
# backslashes in replacement fields are unsupported on the 3.11 floor). _CMDPOS-anchored so `rm`
# must be an actual command word — "rm -rf /" as DATA in `git commit -m "…rm -rf /…"` must not trip the floor.
_RM_FLAG_PREFIX = _CMDPOS + r'rm\s+(-[^\s]*\s+)*'
+# Package-manager global options, each optionally taking ONE non-dash operand.
+_PKG_OPTS = r'(?:-[^\s]+(?:\s+[^-\s][^\s]*)?\s+)*'
HARDLINE_PATTERNS = [
# Root path: any root-anchored path whose components collapse to "/" in the shell ("/", "//",
@@ -397,11 +399,13 @@ DANGEROUS_PATTERNS = [
# the current project (notably `npm uninstall -g`). Treat their destructive
# subcommands like other state-removing operations while leaving installs
# and updates alone.
- (r'\bnpm\s+(?:-[^\s]+\s+)*(?:uninstall|unlink|remove|rm|r|un)\b', "package manager uninstall"),
- (r'\bpnpm\s+(?:-[^\s]+\s+)*(?:uninstall|remove|rm|un)\b', "package manager uninstall"),
- (r'\byarn\s+(?:global\s+)?(?:uninstall|remove)\b', "package manager uninstall"),
- (r'\bpip(?:3)?\s+(?:-[^\s]+\s+)*uninstall\b', "package manager uninstall"),
- (r'\bbrew\s+(?:uninstall|remove|rm)\b', "package manager uninstall"),
+ # _CMDPOS-anchored (quoted prose like `git commit -m "npm uninstall docs"` is data); the
+ # option group also swallows one operand (`--prefix DIR`, `--proxy URL`, `--cwd DIR`).
+ (_CMDPOS + r'npm\s+' + _PKG_OPTS + r'(?:uninstall|unlink|remove|rm|r|un)\b', "package manager uninstall"),
+ (_CMDPOS + r'pnpm\s+' + _PKG_OPTS + r'(?:uninstall|remove|rm|un)\b', "package manager uninstall"),
+ (_CMDPOS + r'yarn\s+' + _PKG_OPTS + r'(?:global\s+)?(?:uninstall|remove)\b', "package manager uninstall"),
+ (_CMDPOS + r'pip(?:3)?\s+' + _PKG_OPTS + r'uninstall\b', "package manager uninstall"),
+ (_CMDPOS + r'brew\s+' + _PKG_OPTS + r'(?:uninstall|remove|rm)\b', "package manager uninstall"),
]