Skip to content

Commit f8b9766

Browse files
author
David Johnson
committed
fix: add hard enforcement for plan mode restrictions
Fixes anomalyco#10314 Plan mode was relying solely on text reminders, allowing models to bypass restrictions and make file changes via destructive bash commands (sed, rm, etc). Changes: - Add plan-mode.ts validator to detect destructive bash commands - Block destructive commands: rm, mv, sed, awk, tee, chmod, etc. - Block package managers: npm install, yarn add, pip install - Block git write operations: git commit, push, reset - Block dangerous chars: backticks, newline injection - Allow read-only commands: ls, cat, grep, git status/diff/log - Add plan mode checks to edit, write, apply_patch tools Verified by running all tests (969 pass).
1 parent b06afd6 commit f8b9766

6 files changed

Lines changed: 449 additions & 0 deletions

File tree

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
import { describe, test, expect } from "bun:test"
2+
import { PlanMode, validateCommandForPlanMode, isPlanMode } from "./plan-mode"
3+
4+
describe("PlanMode", () => {
5+
describe("isPlanMode", () => {
6+
test("returns true for plan agent", () => {
7+
expect(isPlanMode("plan")).toBe(true)
8+
})
9+
10+
test("returns false for build agent", () => {
11+
expect(isPlanMode("build")).toBe(false)
12+
})
13+
14+
test("returns false for other agents", () => {
15+
expect(isPlanMode("general")).toBe(false)
16+
expect(isPlanMode("explore")).toBe(false)
17+
})
18+
})
19+
20+
describe("validateCommandForPlanMode", () => {
21+
describe("non-plan mode", () => {
22+
test("allows all commands in build mode", () => {
23+
expect(validateCommandForPlanMode("rm -rf /", "build").allowed).toBe(true)
24+
expect(validateCommandForPlanMode("git commit -m 'test'", "build").allowed).toBe(true)
25+
})
26+
})
27+
28+
describe("destructive commands", () => {
29+
test("blocks rm command", () => {
30+
const result = validateCommandForPlanMode("rm file.txt", "plan")
31+
expect(result.allowed).toBe(false)
32+
expect(result.command).toBe("rm")
33+
})
34+
35+
test("blocks rm -rf", () => {
36+
const result = validateCommandForPlanMode("rm -rf /", "plan")
37+
expect(result.allowed).toBe(false)
38+
})
39+
40+
test("blocks sed command", () => {
41+
const result = validateCommandForPlanMode("sed -i 's/old/new/g' file.txt", "plan")
42+
expect(result.allowed).toBe(false)
43+
expect(result.command).toBe("sed")
44+
})
45+
46+
test("blocks mv command", () => {
47+
const result = validateCommandForPlanMode("mv old.txt new.txt", "plan")
48+
expect(result.allowed).toBe(false)
49+
})
50+
51+
test("blocks mkdir command", () => {
52+
const result = validateCommandForPlanMode("mkdir newdir", "plan")
53+
expect(result.allowed).toBe(false)
54+
})
55+
56+
test("blocks chmod command", () => {
57+
const result = validateCommandForPlanMode("chmod +x script.sh", "plan")
58+
expect(result.allowed).toBe(false)
59+
})
60+
})
61+
62+
describe("package manager commands", () => {
63+
test("blocks npm install", () => {
64+
const result = validateCommandForPlanMode("npm install", "plan")
65+
expect(result.allowed).toBe(false)
66+
})
67+
68+
test("blocks npm i shorthand", () => {
69+
const result = validateCommandForPlanMode("npm i lodash", "plan")
70+
expect(result.allowed).toBe(false)
71+
})
72+
73+
test("blocks yarn add", () => {
74+
const result = validateCommandForPlanMode("yarn add react", "plan")
75+
expect(result.allowed).toBe(false)
76+
})
77+
78+
test("blocks pip install", () => {
79+
const result = validateCommandForPlanMode("pip install requests", "plan")
80+
expect(result.allowed).toBe(false)
81+
})
82+
})
83+
84+
describe("git commands", () => {
85+
test("blocks git commit", () => {
86+
const result = validateCommandForPlanMode("git commit -m 'test'", "plan")
87+
expect(result.allowed).toBe(false)
88+
})
89+
90+
test("blocks git push", () => {
91+
const result = validateCommandForPlanMode("git push origin main", "plan")
92+
expect(result.allowed).toBe(false)
93+
})
94+
95+
test("allows git status", () => {
96+
const result = validateCommandForPlanMode("git status", "plan")
97+
expect(result.allowed).toBe(true)
98+
})
99+
100+
test("allows git diff", () => {
101+
const result = validateCommandForPlanMode("git diff", "plan")
102+
expect(result.allowed).toBe(true)
103+
})
104+
105+
test("allows git log", () => {
106+
const result = validateCommandForPlanMode("git log --oneline", "plan")
107+
expect(result.allowed).toBe(true)
108+
})
109+
110+
test("allows git branch", () => {
111+
const result = validateCommandForPlanMode("git branch -a", "plan")
112+
expect(result.allowed).toBe(true)
113+
})
114+
})
115+
116+
describe("read-only commands", () => {
117+
test("allows ls", () => {
118+
expect(validateCommandForPlanMode("ls -la", "plan").allowed).toBe(true)
119+
})
120+
121+
test("allows cat", () => {
122+
expect(validateCommandForPlanMode("cat file.txt", "plan").allowed).toBe(true)
123+
})
124+
125+
test("allows grep", () => {
126+
expect(validateCommandForPlanMode("grep -r 'pattern' src/", "plan").allowed).toBe(true)
127+
})
128+
129+
test("allows find", () => {
130+
expect(validateCommandForPlanMode("find . -name '*.ts'", "plan").allowed).toBe(true)
131+
})
132+
133+
test("allows echo", () => {
134+
expect(validateCommandForPlanMode("echo 'hello world'", "plan").allowed).toBe(true)
135+
})
136+
137+
test("allows pwd", () => {
138+
expect(validateCommandForPlanMode("pwd", "plan").allowed).toBe(true)
139+
})
140+
141+
test("allows which", () => {
142+
expect(validateCommandForPlanMode("which node", "plan").allowed).toBe(true)
143+
})
144+
})
145+
146+
describe("dangerous characters", () => {
147+
test("blocks backtick command substitution", () => {
148+
const result = validateCommandForPlanMode("echo `date`", "plan")
149+
expect(result.allowed).toBe(false)
150+
})
151+
152+
test("blocks newline injection", () => {
153+
const result = validateCommandForPlanMode("echo hello\nrm -rf /", "plan")
154+
expect(result.allowed).toBe(false)
155+
})
156+
})
157+
158+
describe("edge cases", () => {
159+
test("handles empty command", () => {
160+
expect(validateCommandForPlanMode("", "plan").allowed).toBe(true)
161+
})
162+
163+
test("handles whitespace-only command", () => {
164+
expect(validateCommandForPlanMode(" ", "plan").allowed).toBe(true)
165+
})
166+
167+
test("handles commands with leading whitespace", () => {
168+
const result = validateCommandForPlanMode(" rm file.txt", "plan")
169+
expect(result.allowed).toBe(false)
170+
})
171+
172+
test("is case-insensitive for commands", () => {
173+
const result = validateCommandForPlanMode("RM file.txt", "plan")
174+
expect(result.allowed).toBe(false)
175+
})
176+
})
177+
})
178+
})

0 commit comments

Comments
 (0)