1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! Glob matching, subject extraction, and precedence for a compiled
//! `PermissionMap` (#7948).
//!
//! Why: a permission map is written as an unordered YAML table, so "which rule
//! applies" cannot be "the first one that matches" — an operator writing
//! `bash: { "git status*": allow, "*": ask }` expects the narrow rule to win
//! regardless of key order. This module owns the total order that makes that
//! mechanical.
//! What: `PermissionMap::evaluate` (one subject), `PermissionMap::evaluate_call`
//! (every subject of one call), and `subjects_for`. Precedence, highest first:
//! 1. an argument-level rule beats a tool-level rule;
//! 2. at the same level, the longest LITERAL PREFIX wins;
//! 3. at equal specificity, `deny` > `ask` > `allow`.
//!
//! A call that matches no rule returns `None`, which every caller reads as
//! `allow` — the pre-#7948 behaviour.
//!
//! A `bash` rule matches the command TEXT, never the program that ends up
//! running: `sh -c 'rm x'`, `/bin/rm x`, and `true; rm x` all miss a `rm *`
//! deny. Treat a bash rule as a guard against an accident, not against an
//! adversary — an agent that wants to get around it can.
//! Test: `permissions::tests::matcher_tests` — the whole module.
use Path;
use Value;
use ;
/// The rule that decided one call, and what it decided.
///
/// What: `rule` is the pattern as written, in `tool` form for a tool-level
/// rule and `tool[arg]` form for an argument-level one.
/// Test: `arg_level_rule_beats_tool_level_rule`, `rule_text_names_the_arg_pattern`.
/// `(level, literal-prefix, decision rank)` — every field ordered "higher wins".
type Specificity = ;
/// Keep the candidate when it is strictly more specific than `best`.
///
/// Why: strictly-greater keeps the result independent of iteration order — two
/// candidates tying on all three axes carry the same decision.
/// The argument(s) a tool call is discriminated by.
///
/// Why: a map says "bash may run `git status*`", not "bash may receive this
/// JSON object", so rules match against the string that names what the tool
/// acts on. Probing by argument NAME rather than a tool-name table keeps a
/// renamed or new file tool covered instead of silently dropping it to
/// tool-level matching.
/// What: MCP tools (`mcp__<server>__<tool>`) have no subject — their argument
/// shape belongs to a third-party server. A `files` array (`write_files`)
/// yields every entry's string `path`. Otherwise the first string among
/// `command` (bash), `path` (file tools), and `pattern` (glob/grep).
///
/// Every PATH subject (a `files[].path` or a `path`) yields its raw form and,
/// when different, its [`normalise_path_subject`] form. `evaluate_call` takes
/// the most restrictive decision across subjects, so `./secrets/x` meets the
/// same `secrets/**` deny as `secrets/x`, and a rewrite never loosens the raw
/// form's decision.
/// Test: `subject_for_bash_is_the_command`, `subject_for_read_file_is_the_path`,
/// `subject_for_grep_is_the_pattern`, `mcp_tools_have_no_subject`,
/// `subjects_for_write_files_are_every_path`,
/// `subject_is_none_when_no_known_argument_is_present`,
/// `path_deny_holds_for_every_lexical_spelling`.
/// [`subjects_for`] with the run's working root supplied.
///
/// Why (#7948): `tools::fs::scoped_path` accepts an ABSOLUTE path that resolves
/// inside the working root (`scoped_path_accepts_absolute_inside_dir`), so the
/// same file has two spellings the model may pick between. An operator writes a
/// path rule relative — `secrets/**` — and a purely lexical subject keeps
/// `/<root>/secrets/token` absolute, so the deny never matched it.
/// What: on top of every form [`subjects_for`] yields, a path subject that
/// normalises to an absolute path UNDER `root` also yields its root-relative
/// spelling. `evaluate_call` folds every subject to the safest decision, so the
/// extra form can only add restriction. `root` of `None` is exactly
/// [`subjects_for`].
/// Test: `absolute_in_root_path_also_yields_its_root_relative_form`,
/// `path_deny_holds_for_the_absolute_in_root_spelling`,
/// `absolute_path_outside_the_root_yields_no_relative_form`.
/// Push `raw`, its normalised form when that differs, and its root-relative
/// form when it names a file under `root`.
// #7948: every form is decided, so a rewrite can only add restriction.
/// The root-relative spelling of an absolute path subject under `root`, or
/// `None` when either side is relative or the subject is outside the root.
///
/// Why (#7948): see [`subjects_for_in_root`].
/// What: pure string work, comparing the [`normalise_path_subject`] form of
/// both sides at a path-SEGMENT boundary — `/a/bc/x` is not under `/a/b`. The
/// root itself yields `None`; a rule names a file, not the tree's own entry.
///
/// Known limit (#7948): lexical only, matching this module's other path work —
/// a root reached through a symlink (macOS `/var` → `/private/var`) is not
/// recognised in its other spelling. `scoped_path` still confines the call.
/// Test: `absolute_in_root_path_also_yields_its_root_relative_form`,
/// `absolute_path_outside_the_root_yields_no_relative_form`.
/// Lexically normalise a path subject: drop `.` segments and empty segments
/// from repeated `/`, fold `name/..`, and strip a trailing `/`.
///
/// Why (#7948): rules glob-match the path the model wrote, while
/// `tools::fs::scoped_path` resolves it only later. Without this,
/// `secrets/**: deny` blocks `secrets/x` but not `./secrets/x`,
/// `secrets/./x`, `a/../secrets/x`, or `secrets//x`.
/// What: pure string work with no filesystem I/O. A relative `..` with nothing
/// left to fold is KEPT (`a/../../x` becomes `../x`), so a path that climbs
/// above the root never normalises into an in-tree path; an absolute path's
/// `..` at `/` stays at `/`. An empty relative result is `.`. The caller still
/// decides the raw form too, so normalisation never widens a decision.
///
/// Known limits (#7948): no case-folding, so `Secrets/x` misses a `secrets/**`
/// rule on a case-insensitive filesystem; no symlink resolution, so a symlink
/// into a denied tree, or a `link/..` fold that differs from the physical
/// path, is not caught here. `scoped_path` still confines the call to the tree.
/// Test: `normalise_path_subject_folds_lexical_spellings`,
/// `path_deny_holds_for_every_lexical_spelling`,
/// `path_climbing_above_the_root_is_never_more_permissive`.