Repository navigation
fix(permissions): match normalized paths for read and bash deny rules #1524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ea55459
fix(permissions): match normalized paths for read and bash deny rules
anandgupta42 6d44159
fix: harden bash and read permission aliases against deny bypasses
anandgupta42 38aa34a
fix: cover mixed operand spellings, symbolic chmod modes and numeric …
anandgupta42 6c17622
fix: close remaining bash permission alias gaps
anandgupta42 26380ee
Merge remote-tracking branch 'origin/main' into fix/permission-relati…
anandgupta42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,66 @@ | ||
| export { evaluate } from "." | ||
| // altimate_change start — upstream_fix: match equivalent path spellings with one rule precedence | ||
| import type { PermissionV1 } from "@opencode-ai/core/v1/permission" | ||
| import { Wildcard } from "@opencode-ai/core/util/wildcard" | ||
|
|
||
| // `pattern` is the spelling the caller authored; `aliases` are equivalent normalized | ||
| // spellings. An alias only ever tightens a decision: the result is the strictest of the | ||
| // authored spelling's rule and the last rule matching it or, for deny/ask rules, an alias. | ||
| // An alias match can never relax a stricter authored match. | ||
| const STRICTNESS = { allow: 0, ask: 1, deny: 2 } as const | ||
|
|
||
| function evaluatePatterns( | ||
| permission: string, | ||
| pattern: string, | ||
| aliases: string[], | ||
| ...rulesets: PermissionV1.Ruleset[] | ||
| ): PermissionV1.Rule { | ||
| const rules = rulesets.flat().filter((rule) => Wildcard.match(permission, rule.permission)) | ||
| const authored = rules.findLast((rule) => Wildcard.match(pattern, rule.pattern)) ?? { | ||
| action: "ask" as const, | ||
| permission, | ||
| pattern: "*", | ||
| } | ||
| // Rule order still decides: an allow rule only applies to the authored spelling. | ||
| const aliased = rules.findLast( | ||
| (rule) => | ||
| Wildcard.match(pattern, rule.pattern) || | ||
| (rule.action !== "allow" && aliases.some((alias) => Wildcard.match(alias, rule.pattern))), | ||
| ) | ||
| return aliased && STRICTNESS[aliased.action] > STRICTNESS[authored.action] ? aliased : authored | ||
| } | ||
|
|
||
| export function evaluate(permission: string, pattern: string, ...rulesets: PermissionV1.Ruleset[]): PermissionV1.Rule { | ||
|
anandgupta42 marked this conversation as resolved.
anandgupta42 marked this conversation as resolved.
|
||
| return evaluatePatterns(permission, pattern, [], ...rulesets) | ||
| } | ||
|
|
||
| export function evaluateRequest( | ||
|
anandgupta42 marked this conversation as resolved.
|
||
| request: Pick<PermissionV1.Request, "permission" | "metadata">, | ||
| pattern: string, | ||
| ...rulesets: PermissionV1.Ruleset[] | ||
| ): PermissionV1.Rule { | ||
| const aliases = request.metadata.permissionAliases | ||
| const values = aliases && typeof aliases === "object" ? (aliases as Record<string, unknown>)[pattern] : undefined | ||
| const groups = Array.isArray(values) | ||
| ? values.filter( | ||
| (group): group is string[] => Array.isArray(group) && group.every((value) => typeof value === "string"), | ||
| ) | ||
| : [] | ||
| if (!groups.length) return evaluate(request.permission, pattern, ...rulesets) | ||
| // Preserve rule order within each access. Distinct invocations must all be allowed, | ||
| // even when they normalize to the same command. | ||
| const rules = groups.map((group) => evaluatePatterns(request.permission, pattern, group, ...rulesets)) | ||
| return rules.find((rule) => rule.action === "deny") ?? rules.find((rule) => rule.action === "ask") ?? rules[0]! | ||
| } | ||
|
|
||
| export function evaluateRequestPatterns( | ||
| request: Pick<PermissionV1.Request, "permission" | "metadata" | "patterns">, | ||
| ...rulesets: PermissionV1.Ruleset[] | ||
| ) { | ||
| // Legacy callers return at the first prompt, so reject all denied accesses first. | ||
| return request.patterns | ||
| .map((pattern) => ({ pattern, rule: evaluateRequest(request, pattern, ...rulesets) })) | ||
| .sort((a, b) => Number(b.rule.action === "deny") - Number(a.rule.action === "deny")) | ||
| } | ||
| // altimate_change end | ||
|
|
||
| export * as PermissionEvaluate from "./evaluate" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.