Skip to content

Commit 489d22c

Browse files
hkaltaiclaude
andcommitted
fix(permission): resolve path rules under a wildcard permission too
The resolved pattern of an external_directory request only gave a resolved spelling to rules whose permission is exactly external_directory, so a path grant such as {"*": {"/tmp/link/*": "allow"}} prompted again for the real directory although the path as written was allowed. Every rule that applies to the request now gets one, and only patterns whose directory part is an absolute path are resolved, so command patterns are left alone. The agent-level symlink test creates a junction on Windows, and its name now says it checks a direct read of the real path. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e433449 commit 489d22c

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

‎packages/opencode/src/permission/evaluate.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ function resolvedTwin(pattern: string) {
3131
const wildcard = segments.findIndex((segment) => /[*?]/.test(segment))
3232
if (wildcard <= 0) return
3333
const dir = segments.slice(0, wildcard).join(path.sep) || path.sep
34+
// Only path patterns have a directory to resolve; a pattern such as "rm -rf /tmp/*" does not.
35+
if (!path.isAbsolute(dir)) return
3436
const resolved = (() => {
3537
try {
3638
return Filesystem.realLocation(dir)
@@ -52,7 +54,8 @@ function withResolvedTwins(rules: PermissionV1.Rule[]) {
5254
return twins.get(pattern)
5355
}
5456
return rules.flatMap((rule) => {
55-
const twin = rule.permission === "external_directory" ? twinOf(rule.pattern) : undefined
57+
// Every rule here already applies to external_directory, including wildcard-permission rules.
58+
const twin = twinOf(rule.pattern)
5659
return twin ? [rule, { ...rule, pattern: twin }] : [rule]
5760
})
5861
}

‎packages/opencode/test/agent/analyst-data-explorer.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,12 +625,16 @@ it.instance(
625625
// A global deny on a real folder must survive a per-agent allow written through a link to its parent.
626626
const layered = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "agent-twin-layers-")))
627627
fs.mkdirSync(path.join(layered, "real", "secret"), { recursive: true })
628-
fs.symlinkSync(path.join(layered, "real"), path.join(layered, "link"))
628+
fs.symlinkSync(
629+
path.join(layered, "real"),
630+
path.join(layered, "link"),
631+
process.platform === "win32" ? "junction" : "dir",
632+
)
629633
afterAll(() => fs.rmSync(layered, { recursive: true, force: true }))
630634

631635
for (const name of ["builder", "analyst"] as const) {
632636
it.instance(
633-
`${name} keeps a global deny on a real folder when its agent config allows a link to it`,
637+
`${name} keeps a global deny for a direct read of the real path when its agent config allows a link to its parent`,
634638
() =>
635639
Effect.gen(function* () {
636640
const agent = (yield* Agent.use.get(name))!

‎packages/opencode/test/permission/next.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ test.skipIf(process.platform === "win32")(
194194
const withApproval = Permission.merge(rules({ [glob("real", "secret")]: "deny" }), approved)
195195
expect(decide(withApproval, glob("real", "secret"))).toBe("deny")
196196
expect(decide(withApproval, glob("link", "x"), glob("real", "x"))).toBe("allow")
197+
// A path rule under a wildcard permission resolves too, so it keeps allowing reads through the link.
198+
const wildcardPermission = Permission.fromConfig({ "*": { [glob("link")]: "allow" } })
199+
expect(decide(wildcardPermission, glob("link", "x"), glob("real", "x"))).toBe("allow")
197200
// A deny written for the link also tightens the real spelling.
198201
expect(
199202
decide(

0 commit comments

Comments
 (0)