From 79bd2c6f11fd5ad011dbe5a1c31343c46e1e5294 Mon Sep 17 00:00:00 2001 From: knp-sap <140621443+knp-sap@users.noreply.github.com> Date: Wed, 18 Nov 2026 12:05:36 +0100 Subject: [PATCH] fix(core): match file permission rules without case on macOS (#64626) Co-authored-by: neriousy --- packages/core/src/config/plugin/agent.ts | 3 +-- packages/core/src/config/plugin/policy.ts | 4 +++- packages/core/src/permission.ts | 13 ++++++++++++- packages/core/src/util/wildcard.ts | 5 +++-- packages/core/test/config/policy.test.ts | 12 ++++++++++++ packages/core/test/permission.test.ts | 19 +++++++++++++++++++ 6 files changed, 50 insertions(+), 6 deletions(-) diff --git a/packages/core/src/config/plugin/agent.ts b/packages/core/src/config/plugin/agent.ts index 526e7df319a..6ddfb711f43 100644 --- a/packages/core/src/config/plugin/agent.ts +++ b/packages/core/src/config/plugin/agent.ts @@ -29,7 +29,6 @@ const decodeAgent = SchemaParser.decodeUnknownResult(ConfigAgent.Info) const decodeLegacyAgent = SchemaParser.decodeUnknownResult(ConfigAgentV1.Info) const formatIssue = SchemaIssue.makeFormatterDefault() type PathAction = FileAccess.ExternalDirectoryAuthorization["action"] | typeof ReadTool.name | typeof EditTool.name -const pathActions = ["external_directory", "read", "edit"] as const satisfies readonly PathAction[] const agentKeys = new Set(["variant", ...Object.keys(ConfigAgent.Info.fields)]) // Fields only V2 understands. A file that uses any of them is read as V2, whatever else it contains. const nativeKeys = ["permissions", "request", "system", "disabled"] @@ -118,7 +117,7 @@ function expandPermissions(rules: Permission.Ruleset, home: string): Permission. } function isPathAction(action: string): action is PathAction { - return pathActions.some((item) => item === action) + return (Permission.pathActions satisfies readonly PathAction[]).some((item) => item === action) } function expandHome(resource: string, home: string) { diff --git a/packages/core/src/config/plugin/policy.ts b/packages/core/src/config/plugin/policy.ts index 2e740628a7f..768200c7433 100644 --- a/packages/core/src/config/plugin/policy.ts +++ b/packages/core/src/config/plugin/policy.ts @@ -6,6 +6,7 @@ import { Bus } from "../../bus.js" import { Config } from "../../config.js" import { Credential } from "../../credential.js" import { ManagedPolicy } from "../../managed-policy.js" +import { Permission } from "../../permission.js" import { State } from "../../state.js" import { Wildcard } from "../../util/wildcard.js" import { WebSearch } from "../../websearch.js" @@ -102,7 +103,8 @@ export const Plugin = define({ .map((resource) => statements.findLast( (policy) => - policy.action === "tool.use" && Wildcard.match(`${event.action}:${resource}`, policy.resource), + policy.action === "tool.use" && + Wildcard.match(`${event.action}:${resource}`, policy.resource, Permission.ignoresCase(event.action)), ), ) .find((policy) => policy?.effect === "deny") diff --git a/packages/core/src/permission.ts b/packages/core/src/permission.ts index 3314c1b9188..d81da926f9f 100644 --- a/packages/core/src/permission.ts +++ b/packages/core/src/permission.ts @@ -91,11 +91,22 @@ export class NotFoundError extends Schema.TaggedError()("Permissi export type Error = BlockedError | CorrectedError +/** Actions whose resources are file paths. */ +export const pathActions = ["external_directory", "read", "edit"] as const + +/** macOS volumes ignore case by default, so `.OPENCODE/x` opens `.opencode/x`; rules for file paths must match it too. */ +export function ignoresCase(action: string) { + return process.platform === "darwin" && pathActions.some((item) => item === action) +} + export function evaluate(action: string, resource: string, ...rulesets: Permission.Ruleset[]): Permission.Rule { + const ignoreCase = ignoresCase(action) return ( rulesets .flat() - .findLast((rule) => Wildcard.match(action, rule.action) && Wildcard.match(resource, rule.resource)) ?? { + .findLast( + (rule) => Wildcard.match(action, rule.action) && Wildcard.match(resource, rule.resource, ignoreCase), + ) ?? { action, resource: "*", effect: "ask", diff --git a/packages/core/src/util/wildcard.ts b/packages/core/src/util/wildcard.ts index 65608026f9b..ad1760758d8 100644 --- a/packages/core/src/util/wildcard.ts +++ b/packages/core/src/util/wildcard.ts @@ -1,6 +1,7 @@ export * as Wildcard from "./wildcard.js" -export function match(input: string, pattern: string) { +/** Windows always matches without case; `ignoreCase` extends that to other callers, such as file rules on macOS. */ +export function match(input: string, pattern: string, ignoreCase = false) { const normalized = input.replaceAll("\\", "/") let escaped = pattern .replaceAll("\\", "/") @@ -10,5 +11,5 @@ export function match(input: string, pattern: string) { if (escaped.endsWith(" .*")) escaped = escaped.slice(0, -3) + "( .*)?" - return new RegExp("^" + escaped + "$", process.platform === "win32" ? "si" : "s").test(normalized) + return new RegExp("^" + escaped + "$", process.platform === "win32" || ignoreCase ? "si" : "s").test(normalized) } diff --git a/packages/core/test/config/policy.test.ts b/packages/core/test/config/policy.test.ts index a9f9cc9d16a..4543c0f75db 100644 --- a/packages/core/test/config/policy.test.ts +++ b/packages/core/test/config/policy.test.ts @@ -101,6 +101,18 @@ describe("ConfigPolicyPlugin.Plugin", () => { }), ) + it.effect("matches tool.use file policies without case where file systems ignore it by default", () => + Effect.gen(function* () { + yield* addPlugin([organizationPolicy([tool("deny", "read:*.env"), tool("deny", "shell:git push*")], "Acme")]) + const files = process.platform === "darwin" || process.platform === "win32" + expect((yield* evaluate("read", ["secrets.ENV"])).effect).toBe(files ? "deny" : "allow") + // Commands keep their case outside Windows. + expect((yield* evaluate("shell", ["GIT PUSH origin dev"])).effect).toBe( + process.platform === "win32" ? "deny" : "allow", + ) + }), + ) + it.effect("filters plugin-provided providers with ordered wildcard policies", () => Effect.gen(function* () { const catalog = yield* Provider.Service diff --git a/packages/core/test/permission.test.ts b/packages/core/test/permission.test.ts index d2a8f3b5790..2447954ca0f 100644 --- a/packages/core/test/permission.test.ts +++ b/packages/core/test/permission.test.ts @@ -17,6 +17,7 @@ import { SessionTable } from "@opencode/core/session/sql" import { SessionStore } from "@opencode/core/session/store" import { ShellParse } from "@opencode/core/shell/parse" import { Tool } from "@opencode/core/tool" +import { Wildcard } from "@opencode/core/util/wildcard" import { eq } from "drizzle-orm" import { location } from "./fixture/location" import { testEffect } from "./lib/effect" @@ -181,6 +182,24 @@ describe("Permission", () => { expect(Permission.evaluate("external_directory", "C:/Users/x/other/*", rules).effect).toBe("ask") }) + test("matches file rules without case where file systems ignore it by default", () => { + const rules: Permission.Ruleset = [ + { action: "*", resource: "*", effect: "allow" }, + { action: "edit", resource: ".opencode/*", effect: "ask" }, + { action: "read", resource: "*.env", effect: "deny" }, + { action: "shell", resource: "git push*", effect: "ask" }, + ] + const files = process.platform === "darwin" || process.platform === "win32" + expect(Permission.evaluate("edit", ".OPENCODE/plugins/guard.ts", rules).effect).toBe(files ? "ask" : "allow") + expect(Permission.evaluate("read", "Secrets.ENV", rules).effect).toBe(files ? "deny" : "allow") + // Commands and other resources keep their case outside Windows. + expect(Permission.evaluate("shell", "GIT PUSH origin dev", rules).effect).toBe( + process.platform === "win32" ? "ask" : "allow", + ) + expect(Wildcard.match(".OPENCODE/x", ".opencode/*", true)).toBe(true) + expect(Wildcard.match(".OPENCODE/x", ".opencode/*")).toBe(process.platform === "win32") + }) + it.effect("allows managed output reads without granting external directory access", () => Effect.gen(function* () { yield* setup([