Skip to content

Commit 62702fb

Browse files
committed
fix: permissions wildcarding so that for ex: 'ls *' includes ls * AND 'ls' to prevent having to double state commands or use 'ls*'
1 parent 22c68a6 commit 62702fb

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

‎packages/opencode/src/util/wildcard.ts‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ import { sortBy, pipe } from "remeda"
22

33
export namespace Wildcard {
44
export function match(str: string, pattern: string) {
5-
const regex = new RegExp(
6-
"^" +
7-
pattern
8-
.replace(/[.+^${}()|[\]\\]/g, "\\$&") // escape special regex chars
9-
.replace(/\*/g, ".*") // * becomes .*
10-
.replace(/\?/g, ".") + // ? becomes .
11-
"$",
12-
"s", // s flag enables multiline matching
13-
)
14-
return regex.test(str)
5+
let escaped = pattern
6+
.replace(/[.+^${}()|[\]\\]/g, "\\$&") // escape special regex chars
7+
.replace(/\*/g, ".*") // * becomes .*
8+
.replace(/\?/g, ".") // ? becomes .
9+
10+
// If pattern ends with " *" (space + wildcard), make the trailing part optional
11+
// This allows "ls *" to match both "ls" and "ls -la"
12+
if (escaped.endsWith(" .*")) {
13+
escaped = escaped.slice(0, -3) + "( .*)?"
14+
}
15+
16+
return new RegExp("^" + escaped + "$", "s").test(str)
1517
}
1618

1719
export function all(input: string, patterns: Record<string, any>) {

‎packages/opencode/test/util/wildcard.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ test("match handles glob tokens", () => {
77
expect(Wildcard.match("foo+bar", "foo+bar")).toBe(true)
88
})
99

10+
test("match with trailing space+wildcard matches command with or without args", () => {
11+
// "ls *" should match "ls" (no args) and "ls -la" (with args)
12+
expect(Wildcard.match("ls", "ls *")).toBe(true)
13+
expect(Wildcard.match("ls -la", "ls *")).toBe(true)
14+
expect(Wildcard.match("ls foo bar", "ls *")).toBe(true)
15+
16+
// "ls*" (no space) should NOT match "ls" alone — wait, it should because .* matches empty
17+
// but it WILL match "lstmeval" which is the dangerous case users should avoid
18+
expect(Wildcard.match("ls", "ls*")).toBe(true)
19+
expect(Wildcard.match("lstmeval", "ls*")).toBe(true)
20+
21+
// "ls *" (with space) should NOT match "lstmeval"
22+
expect(Wildcard.match("lstmeval", "ls *")).toBe(false)
23+
24+
// multi-word commands
25+
expect(Wildcard.match("git status", "git *")).toBe(true)
26+
expect(Wildcard.match("git", "git *")).toBe(true)
27+
expect(Wildcard.match("git commit -m foo", "git *")).toBe(true)
28+
})
29+
1030
test("all picks the most specific pattern", () => {
1131
const rules = {
1232
"*": "deny",

0 commit comments

Comments
 (0)