Repository navigation
fix: match permission rule glob subjects as opaque text #2747
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": patch | ||
| --- | ||
|
|
||
| Fix permission rule argument patterns such as Bash(rm -rf*) not matching commands, URLs, or search text that contain slashes or dot segments. Patterns negated with `!` now exclude those subjects as well. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,8 @@ | |
| * and the rule-subject helpers (`literalRulePattern`, | ||
| * `escapeRuleSubjectLiteral`, `matchesGlobRuleSubject`, | ||
| * `matchesPathRuleSubject`) that tool implementations use to build their | ||
| * `matchesRule` closures and canonical rule strings. Path matching compares | ||
| * `matchesRule` closures and canonical rule strings. Glob matching treats | ||
| * subjects as opaque text, so `*` crosses `/`. Path matching compares | ||
| * normalized path variants, so `./a`, `dir/../a`, and Windows separator or | ||
| * case variants can match the same rule. Pure functions; no scoped service. | ||
| */ | ||
|
|
@@ -27,19 +28,51 @@ interface PathMatchSemantics { | |
| readonly pathClass: PathClass; | ||
| } | ||
|
|
||
| const SLASH_PLACEHOLDER = '\0'; | ||
|
|
||
| export function globMatch(value: string, pattern: string, options?: { nocase?: boolean }): boolean { | ||
| if (picomatch.isMatch(value, pattern, options)) return true; | ||
| // Try the historical path-semantics match first so rules that matched | ||
| // before keep matching (e.g. `a/**/b` still matches `a/b`). | ||
| if (pathSegmentGlobMatch(value, pattern, options)) return true; | ||
|
|
||
| // Then match the subject as opaque text: picomatch gives wildcards path | ||
| // semantics (`*` stops at `/` and refuses dot segments), so rewrite `/` to | ||
| // a placeholder and allow dots instead. | ||
| const opaqueOptions = { ...options, dot: true }; | ||
| if (picomatch.isMatch(asOpaqueText(value), asOpaqueText(pattern), opaqueOptions)) return true; | ||
|
|
||
| const normalizedValue = stripLeadingDotSlash(value); | ||
| const normalizedPattern = stripLeadingDotSlash(pattern); | ||
| if (normalizedValue === value && normalizedPattern === pattern) return false; | ||
| return picomatch.isMatch(normalizedValue, normalizedPattern, options); | ||
| return picomatch.isMatch( | ||
| asOpaqueText(normalizedValue), | ||
| asOpaqueText(normalizedPattern), | ||
| opaqueOptions, | ||
| ); | ||
| } | ||
|
|
||
| function asOpaqueText(value: string): string { | ||
| // Strip real NUL bytes first so one cannot be mistaken for a rewritten `/`. | ||
| return value.replaceAll(SLASH_PLACEHOLDER, '').replaceAll('/', SLASH_PLACEHOLDER); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a rule subject can contain Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| function stripLeadingDotSlash(value: string): string { | ||
| return value.startsWith('./') ? value.slice(2) : value; | ||
| } | ||
|
|
||
| function pathSegmentGlobMatch( | ||
| value: string, | ||
| pattern: string, | ||
| options?: { nocase?: boolean }, | ||
| ): boolean { | ||
| if (picomatch.isMatch(value, pattern, options)) return true; | ||
|
|
||
| const normalizedValue = stripLeadingDotSlash(value); | ||
| const normalizedPattern = stripLeadingDotSlash(pattern); | ||
| if (normalizedValue === value && normalizedPattern === pattern) return false; | ||
| return picomatch.isMatch(normalizedValue, normalizedPattern, options); | ||
| } | ||
|
|
||
| export function pathGlobMatch( | ||
| value: string, | ||
| pattern: string, | ||
|
|
@@ -48,11 +81,11 @@ export function pathGlobMatch( | |
| const semantics = pathMatchSemantics(value, pattern, pathOptions); | ||
| const nocase = pathOptions?.caseInsensitivePaths ?? true; | ||
|
|
||
| if (globMatch(value, pattern, { nocase })) return true; | ||
| if (pathSegmentGlobMatch(value, pattern, { nocase })) return true; | ||
|
|
||
| for (const valueVariant of pathVariants(value, semantics, pathOptions)) { | ||
| for (const patternVariant of pathVariants(pattern, semantics, pathOptions)) { | ||
| if (globMatch(valueVariant, patternVariant, { nocase })) return true; | ||
| if (pathSegmentGlobMatch(valueVariant, patternVariant, { nocase })) return true; | ||
| } | ||
| } | ||
| return false; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove these newly added implementation-narration comments, including the similar comment inside
asOpaqueText, or fold only the externally relevant responsibility into the top-of-file header. This package explicitly requires comments to live solely in that header and forbids comments beside functions or statements.AGENTS.md reference: packages/agent-core-v2/AGENTS.md:L36-L38
Useful? React with 👍 / 👎.