fix(core): match file permission rules without case on macOS (#64626)

Co-authored-by: neriousy <hejmowskifilip@gmail.com>
This commit is contained in:
knp-sap
2026-11-18 12:05:36 +01:00
committed by GitHub
Unverified
parent d412042beb
commit 79bd2c6f11
6 changed files with 50 additions and 6 deletions
+1 -2
View File
@@ -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) {
+3 -1
View File
@@ -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")
+12 -1
View File
@@ -91,11 +91,22 @@ export class NotFoundError extends Schema.TaggedError<NotFoundError>()("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",
+3 -2
View File
@@ -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)
}
+12
View File
@@ -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
+19
View File
@@ -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([