mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-25 03:34:22 +01:00
make plan agent whitelist more conservative (#3424)
This commit is contained in:
55
packages/opencode/test/util/wildcard.test.ts
Normal file
55
packages/opencode/test/util/wildcard.test.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { test, expect } from "bun:test"
|
||||
import { Wildcard } from "../../src/util/wildcard"
|
||||
|
||||
test("match handles glob tokens", () => {
|
||||
expect(Wildcard.match("file1.txt", "file?.txt")).toBe(true)
|
||||
expect(Wildcard.match("file12.txt", "file?.txt")).toBe(false)
|
||||
expect(Wildcard.match("foo+bar", "foo+bar")).toBe(true)
|
||||
})
|
||||
|
||||
test("all picks the most specific pattern", () => {
|
||||
const rules = {
|
||||
"*": "deny",
|
||||
"git *": "ask",
|
||||
"git status": "allow",
|
||||
}
|
||||
expect(Wildcard.all("git status", rules)).toBe("allow")
|
||||
expect(Wildcard.all("git log", rules)).toBe("ask")
|
||||
expect(Wildcard.all("echo hi", rules)).toBe("deny")
|
||||
})
|
||||
|
||||
test("allStructured matches command sequences", () => {
|
||||
const rules = {
|
||||
"git *": "ask",
|
||||
"git status*": "allow",
|
||||
}
|
||||
expect(Wildcard.allStructured({ head: "git", tail: ["status", "--short"] }, rules)).toBe("allow")
|
||||
expect(Wildcard.allStructured({ head: "npm", tail: ["run", "build", "--watch"] }, { "npm run *": "allow" })).toBe(
|
||||
"allow",
|
||||
)
|
||||
expect(Wildcard.allStructured({ head: "ls", tail: ["-la"] }, rules)).toBeUndefined()
|
||||
})
|
||||
|
||||
test("allStructured prioritizes flag-specific patterns", () => {
|
||||
const rules = {
|
||||
"find *": "allow",
|
||||
"find * -delete*": "ask",
|
||||
"sort*": "allow",
|
||||
"sort -o *": "ask",
|
||||
}
|
||||
expect(Wildcard.allStructured({ head: "find", tail: ["src", "-delete"] }, rules)).toBe("ask")
|
||||
expect(Wildcard.allStructured({ head: "find", tail: ["src", "-print"] }, rules)).toBe("allow")
|
||||
expect(Wildcard.allStructured({ head: "sort", tail: ["-o", "out.txt"] }, rules)).toBe("ask")
|
||||
expect(Wildcard.allStructured({ head: "sort", tail: ["--reverse"] }, rules)).toBe("allow")
|
||||
})
|
||||
|
||||
test("allStructured handles sed flags", () => {
|
||||
const rules = {
|
||||
"sed * -i*": "ask",
|
||||
"sed -n*": "allow",
|
||||
}
|
||||
expect(Wildcard.allStructured({ head: "sed", tail: ["-i", "file"] }, rules)).toBe("ask")
|
||||
expect(Wildcard.allStructured({ head: "sed", tail: ["-i.bak", "file"] }, rules)).toBe("ask")
|
||||
expect(Wildcard.allStructured({ head: "sed", tail: ["-n", "1p", "file"] }, rules)).toBe("allow")
|
||||
expect(Wildcard.allStructured({ head: "sed", tail: ["-i", "-n", "/./p", "myfile.txt"] }, rules)).toBe("ask")
|
||||
})
|
||||
Reference in New Issue
Block a user