blib/lib/AnyEvent/MCP/Server/Permissions.pm

filter: Statementstmt 98.3% 351 / 357 Branchbran 83.9% 225 / 268 Conditioncond 65.1% 230 / 353 MC/DCmcdc 46.1% 83 / 180 Subroutinesub 93.9% 31 / 33 Podpod 90.0% 9 / 10 totaltotal 77.3% 929 / 1201 CC 272 SCAR 39.5
CC
272
Cov
82%
check
128.3
resolve
60.9
_rule
41.4
CRAP 52.1
« blib/lib/AnyEvent/MCP/Server.pm ↑ Summary lib/AnyEvent/MCP/IMAP/Providers.pm »
1 =head1 NAME
2  
3 AnyEvent::MCP::Server::Permissions - action authorization and sandbox grants
4  
5 =head1 SYNOPSIS
6  
7 my $permissions = AnyEvent::MCP::Server::Permissions->new (
8 name => "example",
9 actions => { read => "Read file contents", write => "Modify files" },
10 sandbox_grants => {
11 "workdir-read" => [],
12 "workdir-write" => ["workdir-read"],
13 },
14 sandbox_grant_descriptions => {
15 "workdir-read" => "Read the configured workdir",
16 },
17 sandbox_implementation => "bubblewrap",
18 );
19  
20 my $grant = $req->require_permission ({
21 tool => $req->{tool},
22 action => "read",
23 dimensions => { subject => {
24 label => "File", value => $path,
25 choices => [{ label => "this file", match => $path }],
26 } },
27 sandbox_grants => ["workdir-read"],
28 reason => "read $path",
29 });
30  
31 =head1 DESCRIPTION
32  
33 This module lets an MCP server check whether a tool operation is allowed
34 before doing the work. It applies ordered permission rules and asks the
35 user when a decision is needed. Users can allow or deny one operation,
36 or save a rule for future requests.
37  
38 Attach a Permissions object to your server, then have each tool call
39 C<< $req->require_permission >> before starting its operation. The tool
40 describes what it wants to do and which worker privileges it needs. An
41 allowed request receives the granted sandbox privileges; a denied request
42 must not proceed.
43  
44 By default, questions go to a local graphical or terminal helper. You can
45 instead configure the server to ask through an MCP client that supports
46 form elicitation. Saved rules can answer later requests without another
47 prompt; see L</RULE FILES>.
48  
49 For HTTP, the transport must authenticate callers before dispatching MCP
50 requests. This module then authorizes operations for the accepted caller. The tool
51 must enforce the returned grants, for example by constructing a restricted
52 sandbox. The permission engine does not construct that sandbox or enforce
53 worker isolation itself.
54  
55 =head2 The three separate questions
56  
57 Authentication establishes who may call the MCP endpoint.
58 The permission engine answers whether a requested B<action> on a
59 B<subject> may proceed and which B<sandbox grants> the user approved.
60 The application's sandbox or other enforcement then determines what
61 the operation can actually do. None of the three substitutes for the others.
62  
63 For example, a user may approve C<write> on a project but only with
64 C<workdir-write> authority. This does not authorize an arbitrary
65 command with network access, and an approval to write a buffer does not
66 imply approval to save it. Ask before side effects: permission prompts
67 can restart the tool callback. See
68 L<AnyEvent::MCP::Server::Toolset/Permission checks and retries>
69 and L<AnyEvent::MCP::Intro> for examples from actual servers.
70  
71 =head1 PERMISSION REQUESTS
72  
73 A request names an C<action> (a server-defined class of authority such as
74 C<inspect>, C<read>, C<write>, C<manage> or C<exec>), optional named
75 C<dimensions>, and the C<sandbox_grants> required by its worker. These are independent:
76 authorization to write does not imply authorization to read, although
77 sandbox-level C<workdir-write> may imply C<workdir-read>. C<tool> is optional
78 human-readable context identifying the actual MCP tool; rules do not match it.
79  
80 An action may have a short human-readable description declared in C<actions>.
81 C<sandbox_grant_descriptions> similarly describes worker privileges, and
82 C<sandbox_implementation> identifies the enforcement environment (for example,
83 C<bubblewrap> or C<NONE - UNSANDBOXED>). These labels are explanatory, not
84 authorization identifiers, and the trusted server supplies them.
85  
86 C<details> optionally maps plain display labels to Unicode text, for example
87 C<< { "View patch" => $diff } >>. These details are display-only and never
88 participate in rule matching or persistence. Supply complete text; the UI
89 chooses presentation, escaping and preview limits. Labels carry no UI
90 punctuation such as an ellipsis. The local YAD helper opens a text viewer;
91 terminal and native MCP forms include bounded previews in the prompt.
92  
93 =head1 RULE FILES
94  
95 Rules are evaluated in order, with the first match deciding. C<decision> is
96 C<allow>, C<deny> or C<ask>; omitted decisions are invalid. C<action> is the
97 server-defined action class, not the decision. A matcher beginning with C<^>
98 is interpreted as a Perl regular expression; otherwise it matches exactly.
99 Omitted C<match.NAME> fields do not restrict that dimension. For example:
100  
101 [rule]
102 decision=allow
103 action=read
104 match.subject=^/home/example/documents/
105 sandbox-grants=workdir-read
106  
107 [rule]
108 decision=deny
109 action=^
110  
111 If any saved rule is invalid (including an obsolete pre-1.0 field), the
112 entire file is silently ignored and permissions are requested normally.
113 Reading does not modify the file; the next ordinary save replaces it,
114 discarding all old rules. Explicit saves do not validate rule semantics;
115 file I/O errors still fail normally.
116  
117 An allow rule requires C<sandbox-grants>, which may be empty for actions
118 that need no worker grants. It matches only requests whose required grants
119 (including implied grants) are contained in the allowed closure. Omitted
120 C<sandbox-grants> on deny and ask rules matches any requested grants.
121 An explicit ask rule takes precedence over any later allow rules.
122 Deny rules may also specify an exact C<path> matched against any entry in
123 the request's C<paths> array and a human-readable C<message> included in
124 subsequent denial errors.
125 Neither field is permitted on allow rules. Exact-path denials apply even
126 when an operation requires additional sandbox grants for other files.
127 File paths are lexical strings, not a sandbox or inode identity.
128 With no matching rule the user is asked.
129  
130 An optional C<expires> field is an unsigned integer Unix timestamp. Rules
131 with C<expires> at or before the current time are removed from the returned
132 list on every read, before matching. Omit it for indefinite rules. Reads do
133 not rewrite the file; the next ordinary save removes expired entries from
134 disk. Permission prompts offer indefinite (the default), 900 seconds or
135 16 hours. Expiry does not interrupt operations already authorized.
136  
137 Decisions received after asynchronous prompting are rechecked against the
138 current policy. Persistent answers are appended. Rule files are private,
139 updates use locking and atomic rename, and failed persistence is reported as
140 a failed grant rather than a successful permanent authorization.
141  
142 =head1 METHODS
143  
144 =over
145  
146 =item $permissions = AnyEvent::MCP::Server::Permissions->new (%config)
147  
148 Creates a permission engine. It does not enforce worker isolation.
149 Options are:
150  
151 =over
152  
153 =item actions => { $action => $description, ... }
154  
155 Maps action names to descriptions, defaulting to an empty hash. When
156 nonempty, unknown actions are rejected.
157  
158 =item sandbox_grants => { $grant => [$implied_grant...], ... }
159  
160 Defines grant names and their implications, defaulting to an empty hash.
161 Every requested or implied grant must be declared.
162  
163 =item sandbox_grant_descriptions => { $grant => $description, ... }
164  
165 =item sandbox_grant_descriptions => $callback->($request)
166  
167 Optional display descriptions. The callback returns a description hash
168 reference. These labels come from the server; request-supplied overrides
169 are ignored.
170  
171 =item sandbox_implementation => $description
172  
173 Optional display label, such as C<bubblewrap>. It does not select or
174 construct an enforcement mechanism.
175  
176 =item rules => [$rule...]
177  
178 Ordered in-memory rule hashes, defaulting to an empty array.
179 See L</RULE FILES> for their fields.
180  
181 =item file => $file
182  
183 UTF-8 rule file. When supplied, rules are read from this file on permission
184 checks. A missing file supplies an empty rule list.
185  
186 =item name => $name
187  
188 Optional name used for display and, when C<file> is omitted, the default
189 filename C<$XDG_CONFIG_HOME/anyevent-mcp/rules/NAME.ini>, or
190 C<$HOME/.config/anyevent-mcp/rules/NAME.ini>. Without a name or file,
191 policy stays in memory. Applications can supply a different C<file>;
192 C<mcp-fs> uses C<anyevent-mcp/rules/fs-NAME.ini>.
193  
194 =item ask => $method
195  
196 C<helper>, C<tty>, C<yad>, C<auto> or C<mcp>. When omitted, uses
197 C<PERL_ANYEVENT_MCP_PERMISSION_ASK>, defaulting to C<helper>.
198 C<helper> prompts locally using the configured helper. C<tty> and C<yad>
199 run the built-in permission helper with only that interface, ignoring custom
200 helper settings and without interface fallback. C<auto> uses modern MCP form elicitation when
201 advertised for the request, otherwise the helper. C<mcp> requires modern
202 form elicitation and fails when unavailable. A declined prompt never
203 falls back to a different approval mechanism.
204 Choosing C<mcp> also trusts that MCP client to assert the original user
205 whose saved permission rules are evaluated, even for calls whose rules
206 already allow or deny without asking. C<auto> grants that identity trust
207 only where its modern elicitation path was selected. C<helper>, C<tty>
208 and C<yad> do not trust client-supplied user assertions. Avoid C<mcp>
209 and C<auto> with clients not trusted to make permission decisions.
210  
211 =item ask => $callback->($prepared, $done->($success, $decision))
212  
213 Local asynchronous asking callback. C<$prepared> is the prepared request;
214 C<$done> receives a decision hash on success or an error on failure.
215 Callback success means a decision was obtained, not that access was granted.
216  
217 =item helper => $executable
218  
219 =item helper => [$executable, $arg...]
220  
221 External helper, not a shell command. Defaults to
222 C<PERL_ANYEVENT_MCP_PERMISSION_HELPER> or C<anyevent-mcp-askpermission>.
223  
224 =item timeout => $seconds
225  
226 External helper timeout, defaulting to 300 seconds. Also determines the
227 lifetime of each modern permission continuation. Use a positive value;
228 zero makes continuations expire immediately, while the helper treats
229 zero as its default timeout.
230  
231 =item auto_rules => $rules
232  
233 Ordered process-local permission rules, each C<[DECISION, SPEC]> where
234 C<DECISION> is C<allow> or C<deny> and C<SPEC> is
235 C<TOOL[,ACTION[,CAPABILITY]]>. Each field is an exact name or C<*>
236 (any); omitted trailing fields mean C<*>. Tool names treat underscores
237 and hyphens alike. A named capability must be known to this server.
238 Allow matches when all requested grants are implied by the specified
239 capability; deny matches when the requested grants imply that capability.
240 C<*> matches any capability set in either direction. First match wins,
241 before saved rules and prompting. Only tools which call the permission
242 engine are affected. Automatic allowance grants exactly the requested
243 sandbox grants; tool validation and sandbox enforcement still apply.
244  
245 =back
246  
247 =item $grant = $permissions->check ($request, $tool)
248  
249 C<$request> contains C<action>, C<reason>, and C<sandbox_grants> (an array
250 reference; it may be empty). C<tool> in the request is optional display
251 context. The separate optional C<$tool> argument is the trusted dispatched
252 MCP tool name for automatic overrides; Server supplies it from the call.
253 C<dimensions> is an optional hash of independent, named identities. Every
254 dimension has a display C<label>, a nonempty C<value>, and an array of
255 permitted persistent C<choices>, each with C<label> and C<match>. For example:
256  
257 dimensions => {
258 origin => {
259 label => "Origin",
260 value => "https://example.org:8443",
261 default => "https://example.org:8443",
262 choices => [
263 { label => "This origin", match => "https://example.org:8443" },
264 { label => "Any origin", match => "^" },
265 ],
266 },
267 }
268  
269 C<default> optionally selects the initially proposed matcher for persistent
270 rule prompts; it must be one of the offered choices. Without it, the first
271 choice is selected.
272 The engine does not interpret dimension names. Saved rules use
273 C<match.origin>, C<match.subject>, C<match.workdir>, or any other
274 C<match.NAME>. Every selected matcher must match the corresponding request
275 value. A saved matcher for a missing dimension does not match. An omitted
276 C<match.NAME> in a manually written rule matches any value, including absent
277 dimensions; persistent prompts instead require a choice for every dimension.
278 An empty choices array disables permanent approvals, not one-time approvals.
279 Matchers are literal unless beginning with C<^>, which denotes a Perl regex.
280 The caller must supply trustworthy dimension values, not untrusted tool arguments.
281 The former C<subject>, C<subjects>, C<origin>, C<origins>, and C<workdir>
282 request and saved-rule fields are unsupported.
283 The multibroker alias, when supplied by the child's environment, is display-only
284 and does not change rule matching or policy file selection.
285 C<paths> may contain a list of affected filenames for display in permission
286 prompts and for optional B<deny-only> exact-file rules. Such matching does
287 not restrict sandbox authority and is not a substitute for sandboxing.
288 Absent a C<path> matcher, the permission rule is not limited by paths.
289  
290 Returns C<{decision =E<gt> "allow", sandbox_grants =E<gt> [...]}>,
291 C<{decision =E<gt> "deny", sandbox_grants =E<gt> []}>, or
292 C<{decision =E<gt> "ask", request =E<gt> $prepared}>.
293 Denials can also include a saved C<message>. C<check> does not prompt or
294 execute the operation.
295  
296 =item $grant = $permissions->resolve ($request, $approval)
297  
298 C<$request> is the original permission request. C<$approval> is
299 C<{ request =E<gt> $prepared, decision =E<gt> $decision }>, where
300 C<$prepared> is the request returned by C<check> and C<$decision> is the
301 helper's decision hash. Resolves the answer to that prepared request. Approval decisions are
302 C<allow-once>, C<allow-always>, C<deny-once>, and C<deny-always>.
303 Allow-once uses exactly the requested sandbox grants. Allow-always may save
304 a broader or narrower set of grants for future invocations; the present
305 invocation still receives no more than requested. Permanent decisions
306 contain a validated rule restricted to the offered action and dimension scopes.
307 Denial decisions may include an optional human C<message>; allowed tool
308 results are never modified by permission prompting.
309  
310 This is a trusted application interface. Do not pass unauthenticated client
311 metadata directly to it.
312  
313 =item $grant = $permissions->require_permission ($req, { %request })
314  
315 Synchronous interface used by C<< $req->require_permission >>. Returns
316 the granted sandbox-grant array, dies on denial, or throws a permission-needed
317 exception for the server to handle. The worker must enforce the returned grants;
318 never treat merely requested grants as sufficient.
319  
320 =item $permissions->require ($req, $request, $done->($success, $grant))
321  
322 Asynchronous interface. C<$grant> is the allowed sandbox-grant array or an
323 error. Check before starting side effects.
324  
325 With a local prompt, C<$done> runs when permission is resolved. Modern
326 MCP prompting instead completes C<$req> with C<input_required> without
327 calling C<$done>; the client repeats the call with input. Original
328 callbacks are not retained.
329 Continuations are short-lived and bound to the method, parameters, and request,
330 and are consumed once. Approval retries retain earlier one-time grants
331 within the same call. Always decisions and Back use additional protected C<input_required>
332 rounds, capped at 32, without executing the original tool. Messages in
333 persistent deny rules are returned to subsequent callers, including later
334 sessions. Client-side elicitation is only appropriate when the connected
335 client is the trusted approval authority.
336  
337 =item $closure = $permissions->closure ([$grant...])
338  
339 Returns a hash reference whose keys contain the supplied sandbox grants
340 and all grants they imply. Unknown grants or malformed implication lists
341 die. This expands authority names; it does not grant permission.
342  
343 =item $permissions->auto_rules ($rules)
344  
345 Replace the ordered process-local rule chain. Parsing and validation
346 occur once, at installation. An empty list disables automatic rules.
347  
348 =item $rules = $permissions->rules
349  
350 Returns the ordered, uncompiled in-memory or file-backed rule array reference.
351 File-backed rules are reread, so changes apply to subsequent checks.
352  
353 =item $permissions->save ($rules)
354  
355 Replaces the ordered rules without semantic validation, using atomic file
356 replacement if backed by a file.
357  
358 =back
359  
360 =cut
361  
362 package AnyEvent::MCP::Server::Permissions;
363  
364 325 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
365 325 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
366 325 ▶use AnyEvent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
367 325 ▶use AnyEvent::MCP::Permissions::Ask ();
Subroutine BEGIN: called CC=1 SCAR=0.0
368 325 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
369 325 ▶use AnyEvent::MCP::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
370 325 ▶use Errno qw(ENOENT);
Subroutine BEGIN: called CC=1 SCAR=0.0
371 325 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
372  
373 325 our @ISA = qw(AnyEvent::MCP::Base);
374  
375 280 ▶sub new {
Subroutine new: called CC=13 SCAR=25.7
Pod documented
376 280 my ($class, %kv) = @_;
377  
378 280 ▶ my $auto_rules = delete $kv{auto_rules} || [];
Condition100%2/2
delete $kv{'auto_rules'} || []
l!l
180100
MC/DC100%1/1
delete $kv{'auto_rules'} || []
delete $kv{'auto_rules'}
Truth table100%2/2
A
A= delete $kv{'auto_rules'}
Aresult
11
00
379 280 ▶ my $ask = exists $kv{ask} ? $kv{ask} : $ENV{PERL_ANYEVENT_MCP_PERMISSION_ASK};
Branch: 1 branch
BranchTrueFalseTotal
exists $kv{'ask'} ? : 157 123 280
380 280 ▶ $ask = "helper" unless defined $ask && length $ask;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ask and length $ask 106 174 280
Condition100%3/3
defined $ask and length $ask
!ll&&!rl&&r
1051174
MC/DC100%2/2
defined $ask and length $ask
defined $ask length $ask
Truth table100%3/3
A and B
A= defined $askB= length $ask
ABresult
0X0
100
111
381 280 ▶ die "Invalid permission ask mode (expected helper, tty, yad, auto, or mcp)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ask eq "CODE" or not ref $ask and $ask =~ /\A(?:helper|tty|yad|auto|mcp)\z/u 1 279 280
Condition83%5/6
ref $ask eq "CODE" or not ref $ask and $ask =~ /\A(?:helper|tty|yad|auto|mcp)\z/u
l!l&&r!l&&!r
52741
not ref $ask and $ask =~ /\A(?:helper|tty|yad|auto|mcp)\z/u
!ll&&!rl&&r
01274
MC/DC66%2/3
ref $ask eq "CODE" or not ref $ask and $ask =~ /\A(?:helper|tty|yad|auto|mcp)\z/u
ref $ask eq "CODE" not ref $ask $ask =~ /\A(?:helper|tty|yad|auto|mcp)\z/u
Truth table75%3/4
A or not(B) and C
A= ref $ask eq "CODE"B= not ref $askC= $ask =~ /\A(?:helper|tty|yad|auto|mcp)\z/u
ABCresult
1XX1
0111
00X0
0100
382 unless ref $ask eq "CODE" || !ref $ask && $ask =~ /\A(?:helper|tty|yad|auto|mcp)\z/;
383  
384 279 my $self = $class->SUPER::new (sandbox_grants => {}, actions => {}, rules => [], ask => $ask, timeout => 300, %kv);
385  
386 279 ▶ if (!exists $kv{file} && defined $kv{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $kv{"file"} and defined $kv{"name"}) 16 263 279
Condition100%3/3
not exists $kv{"file"} and defined $kv{"name"}
!ll&&!rl&&r
1828116
MC/DC100%2/2
not exists $kv{"file"} and defined $kv{"name"}
not exists $kv{"file"} defined $kv{"name"}
Truth table100%3/3
not(A) and B
A= not exists $kv{"file"}B= defined $kv{"name"}
ABresult
0X0
100
111
387 16 my $name = AnyEvent::MCP::utf8enc $kv{name};
388 0 $name =~ s/([^A-Za-z0-9_.-])/sprintf "%%%02X", ord $1/ge;
389 16 ▶ my $dir = $ENV{XDG_CONFIG_HOME} || ($ENV{HOME} || die "HOME is not set\n") . "/.config";
Condition75%3/4
$ENV{'XDG_CONFIG_HOME'} || ($ENV{'HOME'} || die("HOME is not set\n")) . '/.config'
l!l
151
$ENV{'HOME'} || die("HOME is not set\n")
l!l
10
MC/DC100%1/1
$ENV{'XDG_CONFIG_HOME'} || ($ENV{'HOME'} || die("HOME is not set\n")) . '/.config'
$ENV{'XDG_CONFIG_HOME'}
Truth table100%2/2
A
A= $ENV{'XDG_CONFIG_HOME'}
Aresult
11
00
390 16 $self->{file} = "$dir/anyevent-mcp/rules/$name.ini";
391 }
392  
393 279 $self->closure ([keys %{$self->{sandbox_grants}}]);
394 279 $self->_rule ($_) for @{$self->rules};
395 279 $self->auto_rules ($auto_rules);
396 279 $self
397 }
398  
399 322 ▶sub auto_rules {
Subroutine auto_rules: called CC=16 SCAR=30.7
Pod documented
400 322 my ($self, $rules) = @_;
401  
402 322 ▶ ref $rules eq "ARRAY" or die "Automatic permission rules must be an array\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $rules eq "ARRAY" or die "Automatic permission rules must be an array\n" 322 0 322
403 322 my @compiled;
404 322 for my $entry (@$rules) {
405 83 ▶ ref $entry eq "ARRAY" && @$entry == 2
Branch: 1 branch
BranchTrueFalseTotal
ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and $entry->[0] =~ /\A(?:allow|deny)\z/u and defined $entry->[1] and not ref $entry->[1] or die "Invalid automatic permission rule\n" 83 0 83
Condition33%6/18
ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and $entry->[0] =~ /\A(?:allow|deny)\z/u and defined $entry->[1] and not ref $entry->[1]
!ll&&!rl&&r
0083
ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and $entry->[0] =~ /\A(?:allow|deny)\z/u and defined $entry->[1]
!ll&&!rl&&r
0083
ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and $entry->[0] =~ /\A(?:allow|deny)\z/u
!ll&&!rl&&r
0083
ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0]
!ll&&!rl&&r
0083
ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0]
!ll&&!rl&&r
0083
ref $entry eq "ARRAY" and @$entry == 2
!ll&&!rl&&r
0083
MC/DC0%0/7
ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0] and $entry->[0] =~ /\A(?:allow|deny)\z/u and defined $entry->[1] and not ref $entry->[1]
ref $entry eq "ARRAY" @$entry == 2 defined $entry->[0] not ref $entry->[0] $entry->[0] =~ /\A(?:allow|deny)\z/u defined $entry->[1] not ref $entry->[1]
Truth table12%1/8
A and B and C and not(D) and E and F and not(G)
A= ref $entry eq "ARRAY"B= @$entry == 2C= defined $entry->[0]D= not ref $entry->[0]E= $entry->[0] =~ /\A(?:allow|deny)\z/uF= defined $entry->[1]G= not ref $entry->[1]
ABCDEFGresult
0XXXXXX0
10XXXXX0
110XXXX0
1110XXX0
11110XX0
111110X0
11111100
11111111
406 && defined $entry->[0] && !ref $entry->[0] && $entry->[0] =~ /\A(?:allow|deny)\z/
407 && defined $entry->[1] && !ref $entry->[1]
408 or die "Invalid automatic permission rule\n";
409 83 my @parts = split /,/, $entry->[1], -1;
410 83 ▶ @parts && @parts <= 3 && !grep { !length || ($_ ne "*" && /[\s*]/) } @parts
Branch: 1 branch
BranchTrueFalseTotal
@parts and @parts <= 3 and not grep({not length $_ or $_ ne "*" and /[\s*]/u;} @parts) or die "Invalid automatic permission rule '$entry->[1]'\n" 77 6 83
Condition100%12/12
@parts and @parts <= 3 and not grep({not length $_ or $_ ne "*" and /[\s*]/u;} @parts)
!ll&&!rl&&r
2477
@parts and @parts <= 3
!ll&&!rl&&r
1181
!length($_) || $_ ne '*' && /[\s*]/u
l!l&&r!l&&!r
22215
$_ ne "*" and /[\s*]/u
!ll&&!rl&&r
179362
MC/DC100%3/3
@parts and @parts <= 3 and not grep({not length $_ or $_ ne "*" and /[\s*]/u;} @parts)
@parts @parts <= 3 not grep({not length $_ or $_ ne "*" and /[\s*]/u;} @parts)
MC/DC100%3/3
!length($_) || $_ ne '*' && /[\s*]/u
!length($_) $_ ne "*" /[\s*]/u
Truth table100%4/4
A and B and not(C)
A= @partsB= @parts <= 3C= not grep({not length $_ or $_ ne "*" and /[\s*]/u;} @parts)
ABCresult
0XX0
10X0
1100
1111
Truth table100%4/4
not(A) || B and C
A= !length($_)B= $_ ne "*"C= /[\s*]/u
ABCresult
1XX1
0111
00X0
0100
411 or die "Invalid automatic permission rule '$entry->[1]'\n";
412 77 my ($tool, $action, $capability) = (@parts, ("*") x (3 - @parts));
413 77 $tool =~ y/_/-/;
414 77 ▶ push @compiled, {
Branch: 1 branch
BranchTrueFalseTotal
$capability eq '*' ? : 68 9 77
415 decision => $entry->[0],
416 tool => $tool,
417 action => $action,
418 capability => $capability,
419 closure => $capability eq "*" ? undef : $self->closure ([$capability]),
420 };
421 }
422 315 $self->{auto_rules} = \@compiled;
423 }
424  
425 25867 ▶sub closure {
Subroutine closure: called CC=7 SCAR=19.7
Pod documented
426 25867 my ($self, $sandbox_grants) = @_;
427  
428 25867 ▶ ref $sandbox_grants eq "ARRAY"
Branch: 1 branch
BranchTrueFalseTotal
ref $sandbox_grants eq "ARRAY" or die "Sandbox grants must be an array\n" 25867 0 25867
429 or die "Sandbox grants must be an array\n";
430  
431 25867 my %set;
432  
433 25867 my @todo = @$sandbox_grants;
434  
435 25867 ▶ while (@todo) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo) 40108 25862 65970
436 40108 my $capability = pop @todo;
437  
438 defined $capability && !ref $capability
439 40108 ▶ && ref $self->{sandbox_grants}{$capability} eq "ARRAY"
Branch: 1 branch
BranchTrueFalseTotal
defined $capability and not ref $capability and ref $self->{"sandbox_grants"}{$capability} eq "ARRAY" or die "Unknown sandbox grant\n" 40103 5 40108
Condition50%3/6
defined $capability and not ref $capability and ref $self->{"sandbox_grants"}{$capability} eq "ARRAY"
!ll&&!rl&&r
0540103
defined $capability and not ref $capability
!ll&&!rl&&r
0040108
MC/DC33%1/3
defined $capability and not ref $capability and ref $self->{"sandbox_grants"}{$capability} eq "ARRAY"
defined $capability not ref $capability ref $self->{"sandbox_grants"}{$capability} eq "ARRAY"
Truth table50%2/4
A and not(B) and C
A= defined $capabilityB= not ref $capabilityC= ref $self->{"sandbox_grants"}{$capability} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
440 or die "Unknown sandbox grant\n";
441  
442 40103 ▶ next if $set{$capability}++;
Branch: 1 branch
BranchTrueFalseTotal
if $set{$capability}++ 2806 37297 40103
443 37297 push @todo, @{$self->{sandbox_grants}{$capability}};
444 }
445  
446 25862 \%set
447 }
448  
449 54444 ▶sub _regex {
Subroutine _regex: called CC=4 SCAR=14.9
450 54444 my ($pattern) = @_;
451  
452 54444 ▶ defined $pattern && !ref $pattern
Branch: 1 branch
BranchTrueFalseTotal
defined $pattern and not ref $pattern or die "Permission matcher must be a string\n" 54444 0 54444
Condition33%1/3
defined $pattern and not ref $pattern
!ll&&!rl&&r
0054444
MC/DC0%0/2
defined $pattern and not ref $pattern
defined $pattern not ref $pattern
Truth table33%1/3
A and not(B)
A= defined $patternB= not ref $pattern
ABresult
0X0
100
111
453 or die "Permission matcher must be a string\n";
454  
455 54444 ▶ $pattern =~ /\A\^/ ? qr/$pattern/ : qr/\A\Q$pattern\E\z/
Branch: 1 branch
BranchTrueFalseTotal
$pattern =~ /\A\^/u ? : 16278 38166 54444
456 }
457  
458 12047 ▶sub _rule {
Subroutine _rule: called CC=27 SCAR=37.2
459 12047 my ($self, $rule) = @_;
460  
461 12047 ▶ ref $rule eq "HASH"
Branch: 1 branch
BranchTrueFalseTotal
ref $rule eq "HASH" or die "Invalid permission rule\n" 12047 0 12047
462 or die "Invalid permission rule\n";
463 12047 ▶ !grep !/\A(?:decision|action|path|message|sandbox-grants|expires|match\.[a-z][a-z0-9_-]*)\z/, keys %$rule
Branch: 1 branch
BranchTrueFalseTotal
grep((!/\A(?:decision|action|path|message|sandbox-grants|expires|match\.[a-z][a-z0-9_-]*)\z/u), keys %$rule) and die "Unknown permission rule field\n" 6 12041 12047
464 or die "Unknown permission rule field\n";
465  
466 die "Invalid permission rule expiry\n" if exists $rule->{expires}
467 12041 ▶ && (!defined $rule->{expires} || ref $rule->{expires} || $rule->{expires} !~ /\A[0-9]+\z/);
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{"expires"} and !defined($rule->{"expires"}) || ref $rule->{"expires"} || !($rule->{"expires"} =~ /\A[0-9]+\z/u) 12 12029 12041
Condition66%6/9
exists $rule->{"expires"} and !defined($rule->{"expires"}) || ref $rule->{"expires"} || !($rule->{"expires"} =~ /\A[0-9]+\z/u)
!ll&&!rl&&r
120082112
not defined $rule->{"expires"} or ref $rule->{"expires"} or not $rule->{"expires"} =~ /\A[0-9]+\z/u
l!l&&r!l&&!r
01221
not defined $rule->{"expires"} or ref $rule->{"expires"}
l!l&&r!l&&!r
0033
MC/DC50%2/4
exists $rule->{"expires"} and !defined($rule->{"expires"}) || ref $rule->{"expires"} || !($rule->{"expires"} =~ /\A[0-9]+\z/u)
exists $rule->{"expires"} not defined $rule->{"expires"} ref $rule->{"expires"} not $rule->{"expires"} =~ /\A[0-9]+\z/u
Truth table60%3/5
A and not(B) or C or not(D)
A= exists $rule->{"expires"}B= not defined $rule->{"expires"}C= ref $rule->{"expires"}D= not $rule->{"expires"} =~ /\A[0-9]+\z/u
ABCDresult
0XXX0
10000
11XX1
101X1
10011
468  
469 12029 my $decision = $rule->{decision};
470  
471 12029 ▶ die "Invalid permission rule decision\n" unless defined $decision && $decision =~ /\A(?:allow|deny|ask)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $decision and $decision =~ /\A(?:allow|deny|ask)\z/u 3 12026 12029
Condition66%2/3
defined $decision and $decision =~ /\A(?:allow|deny|ask)\z/u
!ll&&!rl&&r
3012026
MC/DC50%1/2
defined $decision and $decision =~ /\A(?:allow|deny|ask)\z/u
defined $decision $decision =~ /\A(?:allow|deny|ask)\z/u
Truth table66%2/3
A and B
A= defined $decisionB= $decision =~ /\A(?:allow|deny|ask)\z/u
ABresult
0X0
100
111
472 die "Only deny rules can have paths or messages\n"
473 12026 ▶ if $decision ne "deny" && (exists $rule->{path} || exists $rule->{message});
Branch: 1 branch
BranchTrueFalseTotal
if $decision ne "deny" and exists $rule->{"path"} || exists $rule->{"message"} 0 12026 12026
Condition50%3/6
$decision ne "deny" and exists $rule->{"path"} || exists $rule->{"message"}
!ll&&!rl&&r
236117900
exists $rule->{"path"} or exists $rule->{"message"}
l!l&&r!l&&!r
0011790
MC/DC0%0/3
$decision ne "deny" and exists $rule->{"path"} || exists $rule->{"message"}
$decision ne "deny" exists $rule->{"path"} exists $rule->{"message"}
Truth table50%2/4
A and B or C
A= $decision ne "deny"B= exists $rule->{"path"}C= exists $rule->{"message"}
ABCresult
0XX0
1000
11X1
1011
474 12026 ▶ die "Allow rule needs sandbox-grants\n" if $decision eq "allow" && !exists $rule->{"sandbox-grants"};
Branch: 1 branch
BranchTrueFalseTotal
if $decision eq "allow" and not exists $rule->{"sandbox-grants"} 0 12026 12026
Condition66%2/3
$decision eq "allow" and not exists $rule->{"sandbox-grants"}
!ll&&!rl&&r
243117830
MC/DC0%0/2
$decision eq "allow" and not exists $rule->{"sandbox-grants"}
$decision eq "allow" not exists $rule->{"sandbox-grants"}
Truth table66%2/3
A and not(B)
A= $decision eq "allow"B= not exists $rule->{"sandbox-grants"}
ABresult
0X0
100
111
475  
476 defined $rule->{action}
477 12026 ▶ or die "Permission rule needs action\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $rule->{"action"} or die "Permission rule needs action\n" 12026 0 12026
478  
479 12026 my $compiled = { decision => $decision, action => _regex ($rule->{action}) };
480 12026 ▶ if (exists $rule->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"path"}) 15 12011 12026
481 die "Invalid exact denial path\n" if !defined $rule->{path}
482 15 ▶ || ref $rule->{path} || !length $rule->{path};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $rule->{"path"} or ref $rule->{"path"} or not length $rule->{"path"} 0 15 15
Condition33%2/6
not defined $rule->{"path"} or ref $rule->{"path"} or not length $rule->{"path"}
l!l&&r!l&&!r
0015
not defined $rule->{"path"} or ref $rule->{"path"}
l!l&&r!l&&!r
0015
MC/DC0%0/3
not defined $rule->{"path"} or ref $rule->{"path"} or not length $rule->{"path"}
not defined $rule->{"path"} ref $rule->{"path"} not length $rule->{"path"}
Truth table25%1/4
not(A) or B or not(C)
A= not defined $rule->{"path"}B= ref $rule->{"path"}C= not length $rule->{"path"}
ABCresult
1XX1
01X1
0011
0000
483 15 $compiled->{path} = $rule->{path};
484 }
485 12026 my $message = $rule->{message};
486 12026 ▶ if (defined $message) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message) 15 12011 12026
487 15 ▶ die "Invalid permission message\n" if ref $message
Branch: 1 branch
BranchTrueFalseTotal
if ref $message or length $message > 2048 or $message =~ /[\r\n]/u 0 15 15
Condition33%2/6
ref $message or length $message > 2048 or $message =~ /[\r\n]/u
l!l&&r!l&&!r
0015
ref $message or length $message > 2048
l!l&&r!l&&!r
0015
MC/DC0%0/3
ref $message or length $message > 2048 or $message =~ /[\r\n]/u
ref $message length $message > 2048 $message =~ /[\r\n]/u
Truth table25%1/4
A or B or C
A= ref $messageB= length $message > 2048C= $message =~ /[\r\n]/u
ABCresult
1XX1
01X1
0011
0000
488 || length $message > 2048 || $message =~ /[\r\n]/;
489 15 $compiled->{message} = $message;
490 }
491  
492 12026 for my $key (grep /^match\./, keys %$rule) {
493 11322 (my $name = $key) =~ s/^match\.//;
494 11322 $compiled->{matches}{$name} = _regex ($rule->{$key});
495 }
496  
497 12023 ▶ if (exists $rule->{"sandbox-grants"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"sandbox-grants"}) 11812 211 12023
498 11812 ▶ die "Invalid rule sandbox-grants\n" if ref $rule->{"sandbox-grants"};
Branch: 1 branch
BranchTrueFalseTotal
if ref $rule->{"sandbox-grants"} 0 11812 11812
499 $compiled->{sandbox_grants} = $self->closure ([length $rule->{"sandbox-grants"}
500 11812 ▶ ? split /,/, $rule->{"sandbox-grants"}, -1 : ()]);
Branch: 1 branch
BranchTrueFalseTotal
length $rule->{'sandbox-grants'} ? : 11179 633 11812
501 }
502  
503 $compiled
504 12020 }
505  
506 6436 ▶sub rules {
Subroutine rules: called CC=6 SCAR=17.9
Pod documented
507 6436 my ($self) = @_;
508  
509 6436 my $now = time;
510  
511 [grep {
512 die "Invalid permission rule expiry\n" if exists $_->{expires}
513 6115 ▶ && (!defined $_->{expires} || ref $_->{expires} || $_->{expires} !~ /\A[0-9]+\z/);
Branch: 1 branch
BranchTrueFalseTotal
if exists $_->{"expires"} and !defined($_->{"expires"}) || ref $_->{"expires"} || !($_->{"expires"} =~ /\A[0-9]+\z/u) 6 6109 6115
Condition100%9/9
exists $_->{"expires"} and !defined($_->{"expires"}) || ref $_->{"expires"} || !($_->{"expires"} =~ /\A[0-9]+\z/u)
!ll&&!rl&&r
6083266
not defined $_->{"expires"} or ref $_->{"expires"} or not $_->{"expires"} =~ /\A[0-9]+\z/u
l!l&&r!l&&!r
2426
not defined $_->{"expires"} or ref $_->{"expires"}
l!l&&r!l&&!r
1130
MC/DC100%4/4
exists $_->{"expires"} and !defined($_->{"expires"}) || ref $_->{"expires"} || !($_->{"expires"} =~ /\A[0-9]+\z/u)
exists $_->{"expires"} not defined $_->{"expires"} ref $_->{"expires"} not $_->{"expires"} =~ /\A[0-9]+\z/u
Truth table100%5/5
A and not(B) or C or not(D)
A= exists $_->{"expires"}B= not defined $_->{"expires"}C= ref $_->{"expires"}D= not $_->{"expires"} =~ /\A[0-9]+\z/u
ABCDresult
0XXX0
10000
11XX1
101X1
10011
514 6109 ▶ !exists $_->{expires} || $_->{expires} > $now
Condition100%3/3
!exists $_->{'expires'} || $_->{'expires'} > $now
l!l&&r!l&&!r
60831214
MC/DC100%2/2
!exists $_->{'expires'} || $_->{'expires'} > $now
!exists $_->{'expires'} $_->{'expires'} > $now
Truth table100%3/3
not(A) || B
A= !exists $_->{'expires'}B= $_->{'expires'} > $now
ABresult
1X1
011
000
515 6436 } @{$self->_read_rules}]
516 }
517  
518 6436 ▶sub _read_rules {
Subroutine _read_rules: called CC=7 SCAR=19.6
519 6436 my ($self) = @_;
520  
521 6436 ▶ return $self->{rules} unless defined $self->{file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"file"} 361 6075 6436
522  
523 6075 ▶ open my $fh, "<", $self->{file} or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->{"file"} or do { return [] if $! == 2; die qq[$self->{"file"}: $!\n] } 5966 109 6075
524 109 ▶ return [] if $! == ENOENT;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 2 109 0 109
525 0 die "$self->{file}: $!\n";
526 };
527  
528 5966 local $/;
529 5966 my $text = scalar (<$fh>);
530 5966 ▶ close $fh or die "$self->{file}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die qq[$self->{"file"}: $!\n] 5966 0 5966
531  
532 5966 my @rules;
533 5966 my $valid = eval {
534 5966 $text = AnyEvent::MCP::utf8dec $text;
535 12043 ▶ AnyEvent::MCP::parse_ini $text, sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
536 12043 my ($name, $kv) = @_;
537 12043 ▶ if ($name eq "") {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq '') { } 5966 6077 12043
elsif ($name eq 'rule') { } 6074 3 6077
538 5966 ▶ %$kv and die "$self->{file}: permission rule outside [rule]\n";
Branch: 1 branch
BranchTrueFalseTotal
%$kv and die qq[$self->{"file"}: permission rule outside [rule]\n] 0 5966 5966
539 } elsif ($name eq "rule") {
540 6074 push @rules, $kv;
541 } else {
542 3 die "$self->{file}: invalid permission rule section '$name'\n";
543 }
544 5966 }, $self->{file};
545 5962 $self->_rule ($_) for @rules;
546 5941 1
547 };
548  
549 5966 ▶ $valid ? \@rules : []
Branch: 1 branch
BranchTrueFalseTotal
$valid ? : 5941 25 5966
550 }
551  
552 90 ▶sub _write {
Subroutine _write: called CC=3 SCAR=11.0
553 90 my ($self, $rules) = @_;
554  
555 90 ▶ unless (defined $self->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"file"}) 33 57 90
556 33 $self->{rules} = $rules;
557 return
558 33 }
559  
560 my $text = join "", map {
561 57 my $rule = $_;
562 "[rule]\n" . (join "", map {
563 63 ▶ die "Multiline permission rule value\n" if $rule->{$_} =~ /[\r\n]/;
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{$_} =~ /[\r\n]/u 0 221 221
564 221 "$_=$rule->{$_}\n"
565 } sort keys %$rule) . "\n"
566 } @$rules;
567  
568 57 AnyEvent::MCP::File::replace $self->{file}, AnyEvent::MCP::utf8enc ($text);
569 }
570  
571 1529 ▶sub _lock {
Subroutine _lock: called CC=2 SCAR=6.9
572 1529 my ($self) = @_;
573  
574 1529 ▶ defined $self->{file} ? AnyEvent::MCP::File::lock ($self->{file}) : undef
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'file'} ? : 1444 85 1529
575 }
576  
577 51 ▶sub save {
Subroutine save: called CC=1 SCAR=0.0
Pod documented
578 51 my ($self, $rules) = @_;
579  
580 51 my $lock = $self->_lock;
581 51 $self->_write ($rules);
582 }
583  
584 6195 ▶sub check {
Subroutine check: called CC=77 SCAR=48.5
Pod documented
585 6195 my ($self, $request, $tool) = @_;
586  
587 6195 ▶ my $need = $self->closure ($request->{sandbox_grants} // []);
Condition50%1/2
$request->{"sandbox_grants"} // []
l!l
61950
MC/DC0%0/1
$request->{"sandbox_grants"} // []
$request->{"sandbox_grants"}
Truth table50%1/2
A
A= $request->{"sandbox_grants"}
Aresult
11
00
588 die "Permission action must be a string\n" unless defined $request->{action}
589 6194 ▶ && !ref $request->{action} && length $request->{action};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $request->{"action"} and not ref $request->{"action"} and length $request->{"action"} 1 6193 6194
Condition50%3/6
defined $request->{"action"} and not ref $request->{"action"} and length $request->{"action"}
!ll&&!rl&&r
016193
defined $request->{"action"} and not ref $request->{"action"}
!ll&&!rl&&r
006194
MC/DC33%1/3
defined $request->{"action"} and not ref $request->{"action"} and length $request->{"action"}
defined $request->{"action"} not ref $request->{"action"} length $request->{"action"}
Truth table50%2/4
A and not(B) and C
A= defined $request->{"action"}B= not ref $request->{"action"}C= length $request->{"action"}
ABCresult
0XX0
10X0
1100
1111
590 6193 die "Unknown permission action\n" if keys %{$self->{actions}}
591 6193 ▶ && !exists $self->{actions}{$request->{action}};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"actions"};} and not exists $self->{"actions"}{$request->{"action"}} 0 6193 6193
Condition66%2/3
keys %{$$self{"actions"};} and not exists $self->{"actions"}{$request->{"action"}}
!ll&&!rl&&r
39557980
MC/DC0%0/2
keys %{$$self{"actions"};} and not exists $self->{"actions"}{$request->{"action"}}
keys %{$$self{"actions"};} not exists $self->{"actions"}{$request->{"action"}}
Truth table66%2/3
A and not(B)
A= keys %{$$self{"actions"};}B= not exists $self->{"actions"}{$request->{"action"}}
ABresult
0X0
100
111
592 die "Obsolete permission request scope\n"
593 6193 ▶ if grep exists $request->{$_}, qw(subject subjects origin origins workdir workdir_scope);
Branch: 1 branch
BranchTrueFalseTotal
if grep exists $request->{$_}, ("subject", "subjects", "origin", "origins", "workdir", "workdir_scope") 1 6192 6193
594 6192 ▶ my $dimensions = exists $request->{dimensions} ? $request->{dimensions} : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $request->{'dimensions'} ? : 6146 46 6192
595 6192 ▶ ref $dimensions eq "HASH" or die "Invalid permission dimensions\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $dimensions eq "HASH" or die "Invalid permission dimensions\n" 6192 0 6192
596 6192 for my $name (keys %$dimensions) {
597 14978 ▶ $name =~ /\A[a-z][a-z0-9_-]*\z/ or die "Invalid permission dimension name\n";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\A[a-z][a-z0-9_-]*\z/u or die "Invalid permission dimension name\n" 14978 0 14978
598 14978 my $dim = $dimensions->{$name};
599 ref $dim eq "HASH" && defined $dim->{value} && !ref $dim->{value} && length $dim->{value}
600 && defined $dim->{label} && !ref $dim->{label} && length $dim->{label}
601 14978 ▶ && ref $dim->{choices} eq "ARRAY"
Branch: 1 branch
BranchTrueFalseTotal
ref $dim eq "HASH" and defined $dim->{"value"} and not ref $dim->{"value"} and length $dim->{"value"} and defined $dim->{"label"} and not ref $dim->{"label"} and length $dim->{"label"} and ref $dim->{"choices"} eq "ARRAY" or die "Invalid permission dimension '${name}'\n" 14978 0 14978
Condition33%7/21
ref $dim eq "HASH" and defined $dim->{"value"} and not ref $dim->{"value"} and length $dim->{"value"} and defined $dim->{"label"} and not ref $dim->{"label"} and length $dim->{"label"} and ref $dim->{"choices"} eq "ARRAY"
!ll&&!rl&&r
0014978
ref $dim eq "HASH" and defined $dim->{"value"} and not ref $dim->{"value"} and length $dim->{"value"} and defined $dim->{"label"} and not ref $dim->{"label"} and length $dim->{"label"}
!ll&&!rl&&r
0014978
ref $dim eq "HASH" and defined $dim->{"value"} and not ref $dim->{"value"} and length $dim->{"value"} and defined $dim->{"label"} and not ref $dim->{"label"}
!ll&&!rl&&r
0014978
ref $dim eq "HASH" and defined $dim->{"value"} and not ref $dim->{"value"} and length $dim->{"value"} and defined $dim->{"label"}
!ll&&!rl&&r
0014978
ref $dim eq "HASH" and defined $dim->{"value"} and not ref $dim->{"value"} and length $dim->{"value"}
!ll&&!rl&&r
0014978
ref $dim eq "HASH" and defined $dim->{"value"} and not ref $dim->{"value"}
!ll&&!rl&&r
0014978
ref $dim eq "HASH" and defined $dim->{"value"}
!ll&&!rl&&r
0014978
MC/DC0%0/8
ref $dim eq "HASH" and defined $dim->{"value"} and not ref $dim->{"value"} and length $dim->{"value"} and defined $dim->{"label"} and not ref $dim->{"label"} and length $dim->{"label"} and ref $dim->{"choices"} eq "ARRAY"
ref $dim eq "HASH" defined $dim->{"value"} not ref $dim->{"value"} length $dim->{"value"} defined $dim->{"label"} not ref $dim->{"label"} length $dim->{"label"} ref $dim->{"choices"} eq "ARRAY"
Truth table11%1/9
A and B and not(C) and D and E and not(F) and G and H
A= ref $dim eq "HASH"B= defined $dim->{"value"}C= not ref $dim->{"value"}D= length $dim->{"value"}E= defined $dim->{"label"}F= not ref $dim->{"label"}G= length $dim->{"label"}H= ref $dim->{"choices"} eq "ARRAY"
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
110XXXXX0
1110XXXX0
11110XXX0
111110XX0
1111110X0
111111100
111111111
602 or die "Invalid permission dimension '$name'\n";
603 14978 for my $choice (@{$dim->{choices}}) {
604 ref $choice eq "HASH" && defined $choice->{label} && !ref $choice->{label}
605 && length $choice->{label} && defined $choice->{match}
606 && $dim->{value} =~ _regex ($choice->{match})
607 31094 ▶ or die "Permission dimension scope does not match request\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $choice eq "HASH" and defined $choice->{"label"} and not ref $choice->{"label"} and length $choice->{"label"} and defined $choice->{"match"} and $dim->{"value"} =~ _regex($choice->{"match"}) or die "Permission dimension scope does not match request\n" 31090 4 31094
Condition40%6/15
ref $choice eq "HASH" and defined $choice->{"label"} and not ref $choice->{"label"} and length $choice->{"label"} and defined $choice->{"match"} and $dim->{"value"} =~ _regex($choice->{"match"})
!ll&&!rl&&r
0431090
ref $choice eq "HASH" and defined $choice->{"label"} and not ref $choice->{"label"} and length $choice->{"label"} and defined $choice->{"match"}
!ll&&!rl&&r
0031094
ref $choice eq "HASH" and defined $choice->{"label"} and not ref $choice->{"label"} and length $choice->{"label"}
!ll&&!rl&&r
0031094
ref $choice eq "HASH" and defined $choice->{"label"} and not ref $choice->{"label"}
!ll&&!rl&&r
0031094
ref $choice eq "HASH" and defined $choice->{"label"}
!ll&&!rl&&r
0031094
MC/DC16%1/6
ref $choice eq "HASH" and defined $choice->{"label"} and not ref $choice->{"label"} and length $choice->{"label"} and defined $choice->{"match"} and $dim->{"value"} =~ _regex($choice->{"match"})
ref $choice eq "HASH" defined $choice->{"label"} not ref $choice->{"label"} length $choice->{"label"} defined $choice->{"match"} $dim->{"value"} =~ _regex($choice->{"match"})
Truth table28%2/7
A and B and not(C) and D and E and F
A= ref $choice eq "HASH"B= defined $choice->{"label"}C= not ref $choice->{"label"}D= length $choice->{"label"}E= defined $choice->{"match"}F= $dim->{"value"} =~ _regex($choice->{"match"})
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
608 }
609 14974 ▶ if (exists $dim->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dim->{"default"}) 34 14940 14974
610 defined $dim->{default} && !ref $dim->{default}
611 34 ▶ && (grep $_->{match} eq $dim->{default}, @{$dim->{choices}})
Branch: 1 branch
BranchTrueFalseTotal
defined $dim->{"default"} and not ref $dim->{"default"} and grep(($_->{"match"} eq $dim->{"default"}), @{$$dim{"choices"};}) or die "Permission dimension default was not offered\n" 22 12 34
Condition100%6/6
defined $dim->{"default"} and not ref $dim->{"default"} and grep(($_->{"match"} eq $dim->{"default"}), @{$$dim{"choices"};})
!ll&&!rl&&r
6622
defined $dim->{"default"} and not ref $dim->{"default"}
!ll&&!rl&&r
3328
MC/DC100%3/3
defined $dim->{"default"} and not ref $dim->{"default"} and grep(($_->{"match"} eq $dim->{"default"}), @{$$dim{"choices"};})
defined $dim->{"default"} not ref $dim->{"default"} grep(($_->{"match"} eq $dim->{"default"}), @{$$dim{"choices"};})
Truth table100%4/4
A and not(B) and C
A= defined $dim->{"default"}B= not ref $dim->{"default"}C= grep(($_->{"match"} eq $dim->{"default"}), @{$$dim{"choices"};})
ABCresult
0XX0
10X0
1100
1111
612 or die "Permission dimension default was not offered\n";
613 }
614 }
615  
616 6176 ▶ if (exists $request->{details}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $request->{"details"}) 347 5829 6176
617 347 ▶ ref $request->{details} eq "HASH"
Branch: 1 branch
BranchTrueFalseTotal
ref $request->{"details"} eq "HASH" or die "Permission details must be a hash\n" 347 0 347
618 or die "Permission details must be a hash\n";
619 defined && !ref
620 or die "Permission detail must be text\n"
621 347 ▶ for values %{$request->{details}};
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and not ref $_ or die "Permission detail must be text\n" 346 1 347
Condition66%2/3
defined $_ and not ref $_
!ll&&!rl&&r
01346
MC/DC50%1/2
defined $_ and not ref $_
defined $_ not ref $_
Truth table66%2/3
A and not(B)
A= defined $_B= not ref $_
ABresult
0X0
100
111
622 }
623  
624 # use the dispatched tool name, never the request's display label.
625 6175 ▶ my $tool_name = $tool // "";
Condition100%2/2
$tool // ""
l!l
5993182
MC/DC100%1/1
$tool // ""
$tool
Truth table100%2/2
A
A= $tool
Aresult
11
00
626 6175 $tool_name =~ y/_/-/;
627 6175 for my $rule (@{$self->{auto_rules}}) {
628 168 ▶ next if $rule->{tool} ne "*" && $rule->{tool} ne $tool_name;
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"tool"} ne "*" and $rule->{"tool"} ne $tool_name 14 154 168
Condition100%3/3
$rule->{"tool"} ne "*" and $rule->{"tool"} ne $tool_name
!ll&&!rl&&r
1312314
MC/DC100%2/2
$rule->{"tool"} ne "*" and $rule->{"tool"} ne $tool_name
$rule->{"tool"} ne "*" $rule->{"tool"} ne $tool_name
Truth table100%3/3
A and B
A= $rule->{"tool"} ne "*"B= $rule->{"tool"} ne $tool_name
ABresult
0X0
100
111
629 154 ▶ next if $rule->{action} ne "*" && $rule->{action} ne $request->{action};
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"action"} ne "*" and $rule->{"action"} ne $request->{"action"} 22 132 154
Condition100%3/3
$rule->{"action"} ne "*" and $rule->{"action"} ne $request->{"action"}
!ll&&!rl&&r
1191322
MC/DC100%2/2
$rule->{"action"} ne "*" and $rule->{"action"} ne $request->{"action"}
$rule->{"action"} ne "*" $rule->{"action"} ne $request->{"action"}
Truth table100%3/3
A and B
A= $rule->{"action"} ne "*"B= $rule->{"action"} ne $request->{"action"}
ABresult
0X0
100
111
630 132 ▶ if ($rule->{capability} ne "*") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"capability"} ne "*") 11 121 132
631 11 ▶ if ($rule->{decision} eq "allow") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{'decision'} eq 'allow') { } 7 4 11
632 7 ▶ next if grep !$rule->{closure}{$_}, keys %$need;
Branch: 1 branch
BranchTrueFalseTotal
if grep((!$rule->{"closure"}{$_}), keys %$need) 3 4 7
633 } else {
634 4 ▶ next unless $need->{$rule->{capability}};
Branch: 1 branch
BranchTrueFalseTotal
unless $$need{$rule->{"capability"}} 1 3 4
635 }
636 }
637 return {
638 decision => $rule->{decision},
639 128 ▶ sandbox_grants => $rule->{decision} eq "allow" ? [@{$request->{sandbox_grants} || []}] : [],
Branch: 1 branch
BranchTrueFalseTotal
$rule->{'decision'} eq 'allow' ? : 119 9 128
Condition50%1/2
$request->{'sandbox_grants'} || []
l!l
1190
MC/DC0%0/1
$request->{'sandbox_grants'} || []
$request->{'sandbox_grants'}
Truth table50%1/2
A
A= $request->{'sandbox_grants'}
Aresult
11
00
640 };
641 }
642  
643 6047 my @rules = map { $self->_rule ($_) } @{$self->rules};
644 6041 for my $rule (@rules) {
645 5744 ▶ next unless $request->{action} =~ $rule->{action};
Branch: 1 branch
BranchTrueFalseTotal
unless $request->{"action"} =~ /$rule->{"action"}/u 5280 464 5744
646 next if grep {
647 374 ▶ !exists $dimensions->{$_} || $dimensions->{$_}{value} !~ $rule->{matches}{$_}
Condition100%3/3
exists $dimensions->{$_} && $dimensions->{$_}{'value'} =~ /$rule->{'matches'}{$_}/u
!ll&&!rl&&r
1179194
MC/DC100%2/2
exists $dimensions->{$_} && $dimensions->{$_}{'value'} =~ /$rule->{'matches'}{$_}/u
exists $dimensions->{$_} $dimensions->{$_}{'value'} =~ /$rule->{'matches'}{$_}/u
Truth table100%3/3
A && B
A= exists $dimensions->{$_}B= $dimensions->{$_}{'value'} =~ /$rule->{'matches'}{$_}/u
ABresult
0X0
100
111
648 464 ▶ } keys %{$rule->{matches} || {}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {not exists $dimensions->{$_} && $dimensions->{$_}{"value"} =~ /$rule->{"matches"}{$_}/u;} keys %{$rule->{"matches"} or {};} 180 284 464
Condition100%2/2
$rule->{'matches'} || {}
l!l
343121
MC/DC100%1/1
$rule->{'matches'} || {}
$rule->{'matches'}
Truth table100%2/2
A
A= $rule->{'matches'}
Aresult
11
00
649  
650 next if exists $rule->{path} && !(ref $request->{paths} eq "ARRAY"
651 284 ▶ && grep defined && !ref && $_ eq $rule->{path}, @{$request->{paths}});
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{"path"} and not ref $request->{"paths"} eq "ARRAY" && grep((defined $_ && !ref($_) && $_ eq $rule->{"path"}), @{$$request{"paths"};}) 3 281 284
Condition66%8/12
exists $rule->{"path"} and not ref $request->{"paths"} eq "ARRAY" && grep((defined $_ && !ref($_) && $_ eq $rule->{"path"}), @{$$request{"paths"};})
!ll&&!rl&&r
27743
ref $request->{'paths'} eq 'ARRAY' && grep((defined $_ && !ref($_) && $_ eq $rule->{'path'}), @{$$request{'paths'};})
!ll&&!rl&&r
034
defined $_ && !ref($_) && $_ eq $rule->{'path'}
!ll&&!rl&&r
044
defined $_ and not ref $_
!ll&&!rl&&r
008
MC/DC66%2/3
exists $rule->{"path"} and not ref $request->{"paths"} eq "ARRAY" && grep((defined $_ && !ref($_) && $_ eq $rule->{"path"}), @{$$request{"paths"};})
exists $rule->{"path"} ref $request->{'paths'} eq 'ARRAY' grep((defined $_ && !ref($_) && $_ eq $rule->{'path'}), @{$$request{'paths'};})
MC/DC33%1/3
defined $_ && !ref($_) && $_ eq $rule->{'path'}
defined $_ not ref $_ $_ eq $rule->{'path'}
Truth table75%3/4
A and not(B && C)
A= exists $rule->{"path"}B= ref $request->{'paths'} eq 'ARRAY'C= grep((defined $_ && !ref($_) && $_ eq $rule->{'path'}), @{$$request{'paths'};})
ABCresult
0XX0
1110
10X1
1101
Truth table50%2/4
A and not(B) && C
A= defined $_B= not ref $_C= $_ eq $rule->{'path'}
ABCresult
0XX0
10X0
1100
1111
652 # an exact-file denial applies even when a multi-path operation requests
653 # additional sandbox authority for its other paths.
654 next if !exists $rule->{path} && exists $rule->{sandbox_grants}
655 && ($rule->{decision} eq "deny"
656 7 ? (grep !$need->{$_}, keys %{$rule->{sandbox_grants}})
657 281 ▶ : (grep !$rule->{sandbox_grants}{$_}, keys %$need));
Branch: 2 branches
BranchTrueFalseTotal
if not exists $rule->{"path"} and exists $rule->{"sandbox_grants"} and $rule->{"decision"} eq "deny" ? grep((!$need->{$_}), keys %{$$rule{"sandbox_grants"};}) : grep((!$rule->{"sandbox_grants"}{$_}), keys %$need) 1 280 281
$rule->{'decision'} eq 'deny' ? : 7 238 245
Condition100%6/6
not exists $rule->{"path"} and exists $rule->{"sandbox_grants"} and $rule->{"decision"} eq "deny" ? grep((!$need->{$_}), keys %{$$rule{"sandbox_grants"};}) : grep((!$rule->{"sandbox_grants"}{$_}), keys %$need)
!ll&&!rl&&r
362441
not exists $rule->{"path"} and exists $rule->{"sandbox_grants"}
!ll&&!rl&&r
432245
MC/DC100%3/3
not exists $rule->{"path"} and exists $rule->{"sandbox_grants"} and $rule->{"decision"} eq "deny" ? grep((!$need->{$_}), keys %{$$rule{"sandbox_grants"};}) : grep((!$rule->{"sandbox_grants"}{$_}), keys %$need)
not exists $rule->{"path"} exists $rule->{"sandbox_grants"} $rule->{"decision"} eq "deny" ? grep((!$need->{$_}), keys %{$$rule{"sandbox_grants"};}) : grep((!$rule->{"sandbox_grants"}{$_}), keys %$need)
Truth table100%4/4
not(A) and B and C
A= not exists $rule->{"path"}B= exists $rule->{"sandbox_grants"}C= $rule->{"decision"} eq "deny" ? grep((!$need->{$_}), keys %{$$rule{"sandbox_grants"};}) : grep((!$rule->{"sandbox_grants"}{$_}), keys %$need)
ABCresult
0XX0
10X0
1100
1111
658  
659 return {
660 decision => $rule->{decision},
661 237 ▶ sandbox_grants => $rule->{decision} eq "allow" ? [@{$request->{sandbox_grants} || []}] : [],
Branch: 1 branch
BranchTrueFalseTotal
$rule->{'decision'} eq 'deny' && defined $rule->{'message'} ? : 4 273 277
Condition80%4/5
$request->{'sandbox_grants'} || []
l!l
2370
$rule->{"decision"} eq "deny" and defined $rule->{"message"}
!ll&&!rl&&r
237364
MC/DC0%0/1
$request->{'sandbox_grants'} || []
$request->{'sandbox_grants'}
MC/DC100%2/2
$rule->{"decision"} eq "deny" and defined $rule->{"message"}
$rule->{"decision"} eq "deny" defined $rule->{"message"}
Truth table50%1/2
A
A= $request->{'sandbox_grants'}
Aresult
11
00
Truth table100%3/3
A and B
A= $rule->{"decision"} eq "deny"B= defined $rule->{"message"}
ABresult
0X0
100
111
662 ($rule->{decision} eq "deny" && defined $rule->{message} ? (message => $rule->{message}) : ()),
663 280 ▶ } unless $rule->{decision} eq "ask";
Branch: 2 branches
BranchTrueFalseTotal
unless $rule->{"decision"} eq "ask" 277 3 280
$rule->{'decision'} eq 'allow' ? : 237 40 277
664  
665 last
666 3 }
667  
668 5764 ▶ die "Permission request needs a reason\n" unless !ref $request->{reason} && $request->{reason} =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $request->{"reason"} and $request->{"reason"} =~ /\S/u 0 5764 5764
Condition33%1/3
not ref $request->{"reason"} and $request->{"reason"} =~ /\S/u
!ll&&!rl&&r
005764
MC/DC0%0/2
not ref $request->{"reason"} and $request->{"reason"} =~ /\S/u
not ref $request->{"reason"} $request->{"reason"} =~ /\S/u
Truth table33%1/3
not(A) and B
A= not ref $request->{"reason"}B= $request->{"reason"} =~ /\S/u
ABresult
0X0
100
111
669  
670 5764 my $action_class = $request->{action};
671 5764 ▶ $action_class = "^\Q$action_class\E\\z" if $action_class =~ /\A\^/;
Branch: 1 branch
BranchTrueFalseTotal
if $action_class =~ /\A\^/u 0 5764 5764
672  
673 5764 my $descriptions = $self->{sandbox_grant_descriptions};
674 5764 ▶ $descriptions = $descriptions->($request) if ref $descriptions eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
if ref $descriptions eq "CODE" 5422 342 5764
675 5764 ▶ die "Invalid sandbox grant descriptions\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $descriptions and ref $descriptions ne "HASH" 0 5764 5764
Condition66%2/3
defined $descriptions and ref $descriptions ne "HASH"
!ll&&!rl&&r
34054240
MC/DC0%0/2
defined $descriptions and ref $descriptions ne "HASH"
defined $descriptions ref $descriptions ne "HASH"
Truth table66%2/3
A and B
A= defined $descriptionsB= ref $descriptions ne "HASH"
ABresult
0X0
100
111
676 if defined $descriptions && ref $descriptions ne "HASH";
677  
678 {
679 decision => "ask",
680 request => {
681 %$request,
682 sandbox_grants => $request->{sandbox_grants} || [],
683 version => 1,
684 name => $self->{name},
685 (length ($ENV{PERL_ANYEVENT_MCP_MULTIBROKER_ALIAS} // "")
686 ? (multibroker_alias => $ENV{PERL_ANYEVENT_MCP_MULTIBROKER_ALIAS}) : ()),
687 action_matcher => $action_class,
688 5764 ▶ sandbox_grant_choices => [sort keys %{$self->{sandbox_grants}}],
Condition100%2/2
$descriptions || {}
l!l
5424340
MC/DC100%1/1
$descriptions || {}
$descriptions
Truth table100%2/2
A
A= $descriptions
Aresult
11
00
689 sandbox_grant_descriptions => $descriptions || {},
690 action_description => $self->{actions}{$request->{action}},
691 sandbox_implementation => $self->{sandbox_implementation},
692 }
693 }
694 5764 ▶}
Branch: 1 branch
BranchTrueFalseTotal
length($ENV{'PERL_ANYEVENT_MCP_MULTIBROKER_ALIAS'} // '') ? : 13 5751 5764
Condition75%3/4
$request->{'sandbox_grants'} || []
l!l
57640
$ENV{"PERL_ANYEVENT_MCP_MULTIBROKER_ALIAS"} // ""
l!l
135751
MC/DC0%0/1
$request->{'sandbox_grants'} || []
$request->{'sandbox_grants'}
MC/DC100%1/1
$ENV{"PERL_ANYEVENT_MCP_MULTIBROKER_ALIAS"} // ""
$ENV{"PERL_ANYEVENT_MCP_MULTIBROKER_ALIAS"}
Truth table50%1/2
A
A= $request->{'sandbox_grants'}
Aresult
11
00
Truth table100%2/2
A
A= $ENV{"PERL_ANYEVENT_MCP_MULTIBROKER_ALIAS"}
Aresult
11
00
695  
696 1466 ▶sub resolve {
Subroutine resolve: called CC=41 SCAR=41.1
Pod documented
697 1466 my ($self, $request, $approval, $tool) = @_;
698  
699 1466 my $lock = $self->_lock;
700 1466 my $grant = $self->check ($request, $tool);
701  
702 1466 ▶ return $grant unless $grant->{decision} eq "ask";
Branch: 1 branch
BranchTrueFalseTotal
unless $grant->{"decision"} eq "ask" 4 1462 1466
703  
704 1462 my $json = JSON::XS->new->canonical;
705  
706 die "Permission approval does not match request\n" unless ref $approval eq "HASH"
707 1462 ▶ && $json->encode ($approval->{request}) eq $json->encode ($grant->{request});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $approval eq "HASH" and $json->encode($approval->{"request"}) eq $json->encode($grant->{"request"}) 3 1459 1462
Condition66%2/3
ref $approval eq "HASH" and $json->encode($approval->{"request"}) eq $json->encode($grant->{"request"})
!ll&&!rl&&r
031459
MC/DC50%1/2
ref $approval eq "HASH" and $json->encode($approval->{"request"}) eq $json->encode($grant->{"request"})
ref $approval eq "HASH" $json->encode($approval->{"request"}) eq $json->encode($grant->{"request"})
Truth table66%2/3
A and B
A= ref $approval eq "HASH"B= $json->encode($approval->{"request"}) eq $json->encode($grant->{"request"})
ABresult
0X0
100
111
708  
709 1459 my $prepared = $grant->{request};
710 1459 my $decision = $approval->{decision};
711  
712 die "Invalid permission decision\n" unless ref $decision eq "HASH"
713 1459 ▶ && $decision->{decision} =~ /\A(?:allow|deny)-(?:once|always)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $decision eq "HASH" and $decision->{"decision"} =~ /\A(?:allow|deny)-(?:once|always)\z/u 0 1459 1459
Condition33%1/3
ref $decision eq "HASH" and $decision->{"decision"} =~ /\A(?:allow|deny)-(?:once|always)\z/u
!ll&&!rl&&r
001459
MC/DC0%0/2
ref $decision eq "HASH" and $decision->{"decision"} =~ /\A(?:allow|deny)-(?:once|always)\z/u
ref $decision eq "HASH" $decision->{"decision"} =~ /\A(?:allow|deny)-(?:once|always)\z/u
Truth table33%1/3
A and B
A= ref $decision eq "HASH"B= $decision->{"decision"} =~ /\A(?:allow|deny)-(?:once|always)\z/u
ABresult
0X0
100
111
714  
715 1459 my $allow = $decision->{decision} =~ /\Aallow-/;
716 1459 my $granted = [];
717  
718 1459 ▶ if ($allow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow) 1392 67 1459
719 1392 my $have = $self->closure ($prepared->{sandbox_grants});
720 1392 my $give = $self->closure ($decision->{sandbox_grants});
721 1392 my %choices = map { $_ => 1 } @{$prepared->{sandbox_grant_choices}};
722  
723 1392 ▶ die "Unknown selected sandbox grant\n" if grep !$choices{$_}, @{$decision->{sandbox_grants}};
Branch: 1 branch
BranchTrueFalseTotal
if grep((!$choices{$_}), @{$$decision{"sandbox_grants"};}) 0 1392 1392
724 $granted = [(grep !$give->{$_}, keys %$have)
725 ? (grep $give->{$_}, sort keys %$have)
726 1392 ▶ : @{$prepared->{sandbox_grants}}];
Branch: 1 branch
BranchTrueFalseTotal
grep((!$give->{$_}), keys %$have) ? : 131 1261 1392
727 }
728  
729 1459 ▶ if ($decision->{decision} =~ /-always\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decision->{"decision"} =~ /-always\z/u) 36 1423 1459
730 36 my $rule = $decision->{rule};
731 36 ▶ $rule = { decision => "allow", %$rule } if $allow && ref $rule eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if $allow and ref $rule eq "HASH" 28 8 36
Condition66%2/3
$allow and ref $rule eq "HASH"
!ll&&!rl&&r
8028
MC/DC50%1/2
$allow and ref $rule eq "HASH"
$allow ref $rule eq "HASH"
Truth table66%2/3
A and B
A= $allowB= ref $rule eq "HASH"
ABresult
0X0
100
111
732 36 ▶ my $action = $allow ? "allow" : "deny";
Branch: 1 branch
BranchTrueFalseTotal
$allow ? : 28 8 36
733  
734 # the rule contains only the selected dimensions, never extra authority.
735 36 my %fields = map { $_ => 1 } qw(decision action sandbox-grants path message expires);
736 36 ▶ $fields{"match.$_"} = 1 for keys %{$prepared->{dimensions} || {}};
Condition100%2/2
$prepared->{'dimensions'} || {}
l!l
324
MC/DC100%1/1
$prepared->{'dimensions'} || {}
$prepared->{'dimensions'}
Truth table100%2/2
A
A= $prepared->{'dimensions'}
Aresult
11
00
737  
738 ref $rule eq "HASH"
739 && $rule->{action} eq $prepared->{action_matcher}
740 && ($rule->{decision} || "allow") eq $action
741 36 ▶ && !grep !$fields{$_}, keys %$rule
Branch: 1 branch
BranchTrueFalseTotal
ref $rule eq "HASH" and $rule->{"action"} eq $prepared->{"action_matcher"} and ($rule->{"decision"} || "allow") eq $action and not grep((!$fields{$_}), keys %$rule) or die "Invalid persistent permission rule\n" 33 3 36
Condition45%5/11
ref $rule eq "HASH" and $rule->{"action"} eq $prepared->{"action_matcher"} and ($rule->{"decision"} || "allow") eq $action and not grep((!$fields{$_}), keys %$rule)
!ll&&!rl&&r
0333
ref $rule eq "HASH" and $rule->{"action"} eq $prepared->{"action_matcher"} and ($rule->{"decision"} || "allow") eq $action
!ll&&!rl&&r
0036
ref $rule eq "HASH" and $rule->{"action"} eq $prepared->{"action_matcher"}
!ll&&!rl&&r
0036
$rule->{'decision'} || 'allow'
l!l
360
MC/DC25%1/4
ref $rule eq "HASH" and $rule->{"action"} eq $prepared->{"action_matcher"} and ($rule->{"decision"} || "allow") eq $action and not grep((!$fields{$_}), keys %$rule)
ref $rule eq "HASH" $rule->{"action"} eq $prepared->{"action_matcher"} $rule->{'decision'} not grep((!$fields{$_}), keys %$rule)
Truth table40%2/5
A and B and C and not(D)
A= ref $rule eq "HASH"B= $rule->{"action"} eq $prepared->{"action_matcher"}C= $rule->{'decision'}D= not grep((!$fields{$_}), keys %$rule)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
742 or die "Invalid persistent permission rule\n";
743  
744 die "Persistent permission sandbox-grants mismatch\n"
745 unless defined $rule->{"sandbox-grants"} && $rule->{"sandbox-grants"} eq
746 33 ▶ (join ",", @{$allow ? $decision->{sandbox_grants} : $prepared->{sandbox_grants}});
Branch: 2 branches
BranchTrueFalseTotal
unless defined $rule->{"sandbox-grants"} and $rule->{"sandbox-grants"} eq join(",", @{$allow ? $decision->{"sandbox_grants"} : $prepared->{"sandbox_grants"};}) 0 33 33
$allow ? : 25 8 33
Condition33%1/3
defined $rule->{"sandbox-grants"} and $rule->{"sandbox-grants"} eq join(",", @{$allow ? $decision->{"sandbox_grants"} : $prepared->{"sandbox_grants"};})
!ll&&!rl&&r
0033
MC/DC0%0/2
defined $rule->{"sandbox-grants"} and $rule->{"sandbox-grants"} eq join(",", @{$allow ? $decision->{"sandbox_grants"} : $prepared->{"sandbox_grants"};})
defined $rule->{"sandbox-grants"} $rule->{"sandbox-grants"} eq join(",", @{$allow ? $decision->{"sandbox_grants"} : $prepared->{"sandbox_grants"};})
Truth table33%1/3
A and B
A= defined $rule->{"sandbox-grants"}B= $rule->{"sandbox-grants"} eq join(",", @{$allow ? $decision->{"sandbox_grants"} : $prepared->{"sandbox_grants"};})
ABresult
0X0
100
111
747  
748 33 ▶ for my $name (keys %{$prepared->{dimensions} || {}}) {
Condition100%2/2
$prepared->{'dimensions'} || {}
l!l
294
MC/DC100%1/1
$prepared->{'dimensions'} || {}
$prepared->{'dimensions'}
Truth table100%2/2
A
A= $prepared->{'dimensions'}
Aresult
11
00
749 34 my $key = "match.$name";
750 die "Persistent dimension scope was not offered\n"
751 unless defined $rule->{$key}
752 && grep $_->{match} eq $rule->{$key},
753 34 ▶ @{$prepared->{dimensions}{$name}{choices}};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rule->{$key} and grep(($_->{"match"} eq $rule->{$key}), @{$$prepared{"dimensions"}{$name}{"choices"};}) 0 34 34
Condition33%1/3
defined $rule->{$key} and grep(($_->{"match"} eq $rule->{$key}), @{$$prepared{"dimensions"}{$name}{"choices"};})
!ll&&!rl&&r
0034
MC/DC0%0/2
defined $rule->{$key} and grep(($_->{"match"} eq $rule->{$key}), @{$$prepared{"dimensions"}{$name}{"choices"};})
defined $rule->{$key} grep(($_->{"match"} eq $rule->{$key}), @{$$prepared{"dimensions"}{$name}{"choices"};})
Truth table33%1/3
A and B
A= defined $rule->{$key}B= grep(($_->{"match"} eq $rule->{$key}), @{$$prepared{"dimensions"}{$name}{"choices"};})
ABresult
0X0
100
111
754 }
755 33 ▶ if (exists $rule->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"path"}) 2 31 33
756 die "Invalid persistent denial path\n" unless !$allow
757 && ref $prepared->{paths} eq "ARRAY"
758 2 ▶ && grep defined && !ref && $_ eq $rule->{path}, @{$prepared->{paths}};
Branch: 1 branch
BranchTrueFalseTotal
unless not $allow and ref $prepared->{"paths"} eq "ARRAY" and grep((defined $_ && !ref($_) && $_ eq $rule->{"path"}), @{$$prepared{"paths"};}) 0 2 2
Condition33%4/12
not $allow and ref $prepared->{"paths"} eq "ARRAY" and grep((defined $_ && !ref($_) && $_ eq $rule->{"path"}), @{$$prepared{"paths"};})
!ll&&!rl&&r
002
not $allow and ref $prepared->{"paths"} eq "ARRAY"
!ll&&!rl&&r
002
defined $_ && !ref($_) && $_ eq $rule->{'path'}
!ll&&!rl&&r
002
defined $_ and not ref $_
!ll&&!rl&&r
002
MC/DC0%0/3
not $allow and ref $prepared->{"paths"} eq "ARRAY" and grep((defined $_ && !ref($_) && $_ eq $rule->{"path"}), @{$$prepared{"paths"};})
not $allow ref $prepared->{"paths"} eq "ARRAY" grep((defined $_ && !ref($_) && $_ eq $rule->{"path"}), @{$$prepared{"paths"};})
MC/DC0%0/3
defined $_ && !ref($_) && $_ eq $rule->{'path'}
defined $_ not ref $_ $_ eq $rule->{'path'}
Truth table25%1/4
not(A) and B and C
A= not $allowB= ref $prepared->{"paths"} eq "ARRAY"C= grep((defined $_ && !ref($_) && $_ eq $rule->{"path"}), @{$$prepared{"paths"};})
ABCresult
0XX0
10X0
1100
1111
Truth table25%1/4
A and not(B) && C
A= defined $_B= not ref $_C= $_ eq $rule->{'path'}
ABCresult
0XX0
10X0
1100
1111
759 }
760 die "Persistent denial message mismatch\n"
761 unless (!$allow || !defined $decision->{message})
762 && (defined $rule->{message} ? $rule->{message} : "")
763 33 ▶ eq (defined $decision->{message} ? $decision->{message} : "");
Branch: 3 branches
BranchTrueFalseTotal
unless not $allow && defined $decision->{"message"} and (defined $rule->{"message"} ? $rule->{"message"} : "") eq (defined $decision->{"message"} ? $decision->{"message"} : "") 0 33 33
defined $rule->{'message'} ? : 2 31 33
defined $decision->{'message'} ? : 2 31 33
Condition50%3/6
not $allow && defined $decision->{"message"} and (defined $rule->{"message"} ? $rule->{"message"} : "") eq (defined $decision->{"message"} ? $decision->{"message"} : "")
!ll&&!rl&&r
0033
$allow && defined $decision->{'message'}
!ll&&!rl&&r
8250
MC/DC0%0/3
not $allow && defined $decision->{"message"} and (defined $rule->{"message"} ? $rule->{"message"} : "") eq (defined $decision->{"message"} ? $decision->{"message"} : "")
$allow defined $decision->{'message'} (defined $rule->{"message"} ? $rule->{"message"} : "") eq (defined $decision->{"message"} ? $decision->{"message"} : "")
Truth table40%2/5
not(A && B) and C
A= $allowB= defined $decision->{'message'}C= (defined $rule->{"message"} ? $rule->{"message"} : "") eq (defined $decision->{"message"} ? $decision->{"message"} : "")
ABCresult
11X0
0X00
1000
0X11
1011
764  
765 33 my $rules = $self->rules;
766 33 push @$rules, { %$rule };
767 33 $self->_write ($rules);
768 }
769  
770 {
771 decision => $allow ? "allow" : "deny",
772 sandbox_grants => $granted,
773 1456 ▶ (!$allow && defined $decision->{message} ? (message => $decision->{message}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
$allow ? : 1389 67 1456
!$allow && defined $decision->{'message'} ? : 2 1454 1456
Condition100%3/3
not $allow and defined $decision->{"message"}
!ll&&!rl&&r
1389652
MC/DC100%2/2
not $allow and defined $decision->{"message"}
not $allow defined $decision->{"message"}
Truth table100%3/3
not(A) and B
A= not $allowB= defined $decision->{"message"}
ABresult
0X0
100
111
774 }
775 }
776  
777 3681 ▶sub _call {
Subroutine _call: called CC=1 SCAR=0.0
778 3681 my ($req) = @_;
779  
780 3681 my %params = %{$req->{params}};
781 3681 delete @params{qw(inputResponses requestState _meta)};
782 # _meta is per-request context, not stable operation identity: retries can
783 # carry different client capabilities, progress tokens and clientInfo.
784 # bind the continuation to its authenticated user as well as the call.
785 3681 JSON::XS->new->canonical->encode ([$req->{user}, $req->{protocol_version}, $req->{method}, \%params])
786 }
787  
788 4548 ▶sub _user_request {
Subroutine _user_request: called CC=4 SCAR=13.9
789 4548 my ($req, $request) = @_;
790  
791 4548 ▶ my $dimensions = $request->{dimensions} || {};
Condition100%2/2
$request->{'dimensions'} || {}
l!l
447771
MC/DC100%1/1
$request->{'dimensions'} || {}
$request->{'dimensions'}
Truth table100%2/2
A
A= $request->{'dimensions'}
Aresult
11
00
792 4548 ▶ ref $dimensions eq "HASH" or die "Invalid permission dimensions\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $dimensions eq "HASH" or die "Invalid permission dimensions\n" 4548 0 4548
793 4548 $dimensions = { %$dimensions };
794 4548 delete $dimensions->{user};
795  
796 4548 ▶ if (defined (my $user = $req->{user})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $user = $req->{"user"})) 1 4547 4548
797 $dimensions->{user} = {
798 1 label => "User",
799 value => $user,
800 choices => [
801 { label => "This user", match => $user },
802 { label => "All users", match => "^" },
803 ],
804 default => $user,
805 };
806 }
807  
808 4548 +{ %$request, dimensions => $dimensions }
809 }
810  
811 1348 ▶sub _remember {
Subroutine _remember: called CC=1 SCAR=0.0
812 1348 my ($self, $req, $request, $grant) = @_;
813  
814 1348 my $key = JSON::XS->new->canonical->encode ($request);
815 1348 $req->{_permission_grants}{$key} = $grant;
816 }
817  
818 82 ▶sub denial_message {
Subroutine denial_message: called CC=3 SCAR=11.1
Pod undocumented
819 82 my ($grant) = @_;
820  
821 "Permission denied" . (defined $grant->{message} && length $grant->{message}
822 82 ▶ ? ": $grant->{message}" : "")
Branch: 1 branch
BranchTrueFalseTotal
defined $grant->{'message'} && length $grant->{'message'} ? : 3 79 82
Condition66%2/3
defined $grant->{"message"} and length $grant->{"message"}
!ll&&!rl&&r
7903
MC/DC50%1/2
defined $grant->{"message"} and length $grant->{"message"}
defined $grant->{"message"} length $grant->{"message"}
Truth table66%2/3
A and B
A= defined $grant->{"message"}B= length $grant->{"message"}
ABresult
0X0
100
111
823 }
824  
825 1423 ▶sub _challenge {
Subroutine _challenge: called CC=10 SCAR=25.7
826 1423 my ($self, $req, $request, $prepared, $grants, $stage, $rounds, $selection) = @_;
827  
828 1423 ▶ return $req->error ("Too many permission rounds") if $rounds > 32;
Branch: 1 branch
BranchTrueFalseTotal
if $rounds > 32 0 1423 1423
829  
830 1423 AE::log debug => "permission elicitation: challenge created (stage $stage)";
831 1423 my $state = "ae-mcp-permission:" . unpack "H*", AnyEvent::MCP::random_bytes 32;
832 $self->{pending}{$state} = {
833 call => $req->{_permission_call} || _call ($req),
834 request => $request,
835 prepared => $prepared,
836 1423 ▶ grants => { %{$grants || {}} },
Condition85%6/7
$req->{'_permission_call'} || _call($req)
l!l&&r!l&&!r
1407160
$grants || {}
l!l
140914
$selection || {}
l!l
91414
MC/DC0%0/2
$req->{'_permission_call'} || _call($req)
$req->{'_permission_call'} _call($req)
MC/DC100%1/1
$grants || {}
$grants
MC/DC100%1/1
$selection || {}
$selection
Truth table66%2/3
A || B
A= $req->{'_permission_call'}B= _call($req)
ABresult
1X1
011
000
Truth table100%2/2
A
A= $grants
Aresult
11
00
Truth table100%2/2
A
A= $selection
Aresult
11
00
837 stage => $stage,
838 rounds => $rounds,
839 selection => $selection || {},
840 };
841  
842 1423 weaken $self;
843 0 ▶ $self->{pending}{$state}{timer} = AnyEvent->timer (after => $self->{timeout}, cb => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
844 0 ▶ if ($self && delete $self->{pending}{$state}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self and delete $self->{"pending"}{$state}) 0 0 0
Condition0%0/3
$self and delete $self->{"pending"}{$state}
!ll&&!rl&&r
000
MC/DC0%0/2
$self and delete $self->{"pending"}{$state}
$self delete $self->{"pending"}{$state}
Truth table0%0/3
A and B
A= $selfB= delete $self->{"pending"}{$state}
ABresult
0X0
100
111
845 0 AE::log debug => "permission elicitation: challenge expired (stage $stage)";
846 }
847 1423 });
848  
849 $req->reply ({
850 resultType => "input_required",
851 requestState => $state,
852 inputRequests => {
853 permission => {
854 method => "elicitation/create",
855 params => {
856 mode => "form",
857 _meta => {
858 "anyevent-mcp/permission" => {
859 version => 1,
860 request => $prepared,
861 stage => $stage,
862 selection => $selection || {},
863 },
864 },
865 message => $stage eq "rule" ? "Configure the persistent permission rule.\n"
866 . (($selection->{decision} || "") eq "deny-always"
867 1423 ▶ ? "Denial reason: " . ($selection->{message} // "(none)") . "\n" : "")
Branch: 2 branches
BranchTrueFalseTotal
$stage eq 'rule' ? : 8 1415 1423
($selection->{'decision'} || '') eq 'deny-always' ? : 2 6 8
Condition66%4/6
$selection || {}
l!l
91414
$selection->{'decision'} || ''
l!l
80
$selection->{"message"} // "(none)"
l!l
20
MC/DC100%1/1
$selection || {}
$selection
MC/DC0%0/1
$selection->{'decision'} || ''
$selection->{'decision'}
MC/DC0%0/1
$selection->{"message"} // "(none)"
$selection->{"message"}
Truth table100%2/2
A
A= $selection
Aresult
11
00
Truth table50%1/2
A
A= $selection->{'decision'}
Aresult
11
00
Truth table50%1/2
A
A= $selection->{"message"}
Aresult
11
00
868 . AnyEvent::MCP::Permissions::Ask::message ($prepared, $stage)
869 : AnyEvent::MCP::Permissions::Ask::message ($prepared),
870 requestedSchema => AnyEvent::MCP::Permissions::Ask::schema ($prepared, $stage, $selection),
871 },
872 },
873 },
874 });
875 }
876  
877 1414 ▶sub _next_stage {
Subroutine _next_stage: called CC=5 SCAR=16.1
878 1414 my ($decision, $stage) = @_;
879  
880 1414 ▶ return "rule" if $stage eq "choice" && $decision->{decision} =~ /-always\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $stage eq "choice" and $decision->{"decision"} =~ /-always\z/u 8 1406 1414
Condition100%3/3
$stage eq "choice" and $decision->{"decision"} =~ /-always\z/u
!ll&&!rl&&r
813988
MC/DC100%2/2
$stage eq "choice" and $decision->{"decision"} =~ /-always\z/u
$stage eq "choice" $decision->{"decision"} =~ /-always\z/u
Truth table100%3/3
A and B
A= $stage eq "choice"B= $decision->{"decision"} =~ /-always\z/u
ABresult
0X0
100
111
881 1406 ▶ return "choice" if $stage eq "rule" && $decision->{decision} eq "back";
Branch: 1 branch
BranchTrueFalseTotal
if $stage eq "rule" and $decision->{"decision"} eq "back" 1 1405 1406
Condition100%3/3
$stage eq "rule" and $decision->{"decision"} eq "back"
!ll&&!rl&&r
139871
MC/DC100%2/2
$stage eq "rule" and $decision->{"decision"} eq "back"
$stage eq "rule" $decision->{"decision"} eq "back"
Truth table100%3/3
A and B
A= $stage eq "rule"B= $decision->{"decision"} eq "back"
ABresult
0X0
100
111
882 undef
883 1405 }
884  
885 3658 ▶sub _resume {
Subroutine _resume: called CC=17 SCAR=28.9
886 3658 my ($self, $req) = @_;
887  
888 3658 ▶ return if $req->{_permission_resumed}++;
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"_permission_resumed"}++ 9 3649 3658
889  
890 3649 $req->{_permission_call} = _call ($req);
891  
892 3649 my $state = $req->{params}{requestState};
893 3649 ▶ return unless defined $state && $state =~ /\Aae-mcp-permission:/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state and $state =~ /\Aae-mcp-permission:/u 2243 1406 3649
Condition66%2/3
defined $state and $state =~ /\Aae-mcp-permission:/u
!ll&&!rl&&r
224301406
MC/DC50%1/2
defined $state and $state =~ /\Aae-mcp-permission:/u
defined $state $state =~ /\Aae-mcp-permission:/u
Truth table66%2/3
A and B
A= defined $stateB= $state =~ /\Aae-mcp-permission:/u
ABresult
0X0
100
111
894  
895 1406 my $pending = delete $self->{pending}{$state};
896 1406 ▶ AE::log debug => "permission elicitation: continuation has no pending challenge"
Branch: 1 branch
BranchTrueFalseTotal
unless $pending 3 1403 1406
897 unless $pending;
898 AE::log debug => "permission elicitation: continuation request mismatch"
899 1406 ▶ if $pending && $pending->{call} ne $req->{_permission_call};
Branch: 1 branch
BranchTrueFalseTotal
if $pending and $pending->{"call"} ne $req->{"_permission_call"} 2 1404 1406
Condition100%3/3
$pending and $pending->{"call"} ne $req->{"_permission_call"}
!ll&&!rl&&r
314012
MC/DC100%2/2
$pending and $pending->{"call"} ne $req->{"_permission_call"}
$pending $pending->{"call"} ne $req->{"_permission_call"}
Truth table100%3/3
A and B
A= $pendingB= $pending->{"call"} ne $req->{"_permission_call"}
ABresult
0X0
100
111
900 die "Invalid or expired permission continuation\n"
901 1406 ▶ unless $pending && $pending->{call} eq $req->{_permission_call};
Branch: 1 branch
BranchTrueFalseTotal
unless $pending and $pending->{"call"} eq $req->{"_permission_call"} 5 1401 1406
Condition100%3/3
$pending and $pending->{"call"} eq $req->{"_permission_call"}
!ll&&!rl&&r
321401
MC/DC100%2/2
$pending and $pending->{"call"} eq $req->{"_permission_call"}
$pending $pending->{"call"} eq $req->{"_permission_call"}
Truth table100%3/3
A and B
A= $pendingB= $pending->{"call"} eq $req->{"_permission_call"}
ABresult
0X0
100
111
902  
903 1401 my $response = $req->{params}{inputResponses}{permission};
904 my $action = ref $response eq "HASH" && !ref $response->{action}
905 && ($response->{action} // "") =~ /\A(?:accept|decline|cancel)\z/
906 1401 ▶ ? $response->{action} : "invalid";
Branch: 1 branch
BranchTrueFalseTotal
ref $response eq 'HASH' && !ref($response->{'action'}) && ($response->{'action'} // '') =~ /\A(?:accept|decline|cancel)\z/u ? : 1401 0 1401
Condition37%3/8
ref $response eq "HASH" and not ref $response->{"action"} and ($response->{"action"} // "") =~ /\A(?:accept|decline|cancel)\z/u
!ll&&!rl&&r
001401
ref $response eq "HASH" and not ref $response->{"action"}
!ll&&!rl&&r
001401
$response->{"action"} // ""
l!l
14010
MC/DC0%0/3
ref $response eq "HASH" and not ref $response->{"action"} and ($response->{"action"} // "") =~ /\A(?:accept|decline|cancel)\z/u
ref $response eq "HASH" not ref $response->{"action"} $response->{"action"}
Truth table25%1/4
A and not(B) and C
A= ref $response eq "HASH"B= not ref $response->{"action"}C= $response->{"action"}
ABCresult
0XX0
10X0
1100
1111
907 1401 AE::log debug => "permission elicitation: continuation received (stage $pending->{stage}, action $action)";
908 my $decision = AnyEvent::MCP::Permissions::Ask::decision
909 1401 $pending->{prepared}, $response, $pending->{stage}, $pending->{selection};
910 1401 ▶ if (my $next = _next_stage ($decision, $pending->{stage})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = _next_stage($decision, $pending->{"stage"})) 7 1394 1401
911 $self->_challenge ($req, $pending->{request}, $pending->{prepared}, $pending->{grants},
912 7 ▶ $next, ($pending->{rounds} || 1) + 1, { %{$pending->{selection} || {}}, %$decision });
Condition50%2/4
$pending->{'rounds'} || 1
l!l
70
$pending->{'selection'} || {}
l!l
70
MC/DC0%0/1
$pending->{'rounds'} || 1
$pending->{'rounds'}
MC/DC0%0/1
$pending->{'selection'} || {}
$pending->{'selection'}
Truth table50%1/2
A
A= $pending->{'rounds'}
Aresult
11
00
Truth table50%1/2
A
A= $pending->{'selection'}
Aresult
11
00
913 return
914 7 }
915 my $grant = $self->resolve ($pending->{request}, {
916 request => $pending->{prepared}, decision => $decision,
917 1394 }, $req->{tool});
918  
919 1394 ▶ die denial_message ($grant) . "\n" unless $grant->{decision} eq "allow";
Branch: 1 branch
BranchTrueFalseTotal
unless $grant->{"decision"} eq "allow" 55 1339 1394
920  
921 1339 $req->{_permission_grants} = $pending->{grants};
922 1339 $self->_remember ($req, $pending->{request}, $grant->{sandbox_grants});
923 }
924  
925 3089 ▶sub require_permission {
Subroutine require_permission: called CC=4 SCAR=13.9
Pod documented
926 3089 my ($self, $req, $request) = @_;
927  
928 3089 $request = _user_request ($req, $request);
929 3089 my $grant = $self->check ($request, $req->{tool});
930  
931 3089 ▶ return $grant->{sandbox_grants} if $grant->{decision} eq "allow";
Branch: 1 branch
BranchTrueFalseTotal
if $grant->{"decision"} eq "allow" 307 2782 3089
932  
933 2782 ▶ if ($grant->{decision} eq "deny") {
Branch: 1 branch
BranchTrueFalseTotal
if ($grant->{"decision"} eq "deny") 21 2761 2782
934 21 AE::log debug => "permission policy: denied";
935 21 die denial_message ($grant) . "\n";
936 }
937  
938 2761 my $key = JSON::XS->new->canonical->encode ($request);
939  
940 return $req->{_permission_grants}{$key}
941 2761 ▶ if exists $req->{_permission_grants}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $req->{"_permission_grants"}{$key} 1348 1413 2761
942  
943 1413 die bless { request => $request }, "AnyEvent::MCP::Server::Permissions::Required";
944 }
945  
946 1457 ▶sub require {
Subroutine require: called CC=31 SCAR=35.1
Pod documented
947 1457 my ($self, $req, $request, $done) = @_;
948  
949 1457 $request = _user_request ($req, $request);
950 1457 my $grant = eval { $self->check ($request, $req->{tool}) };
951  
952 1457 ▶ return $done->(0, "$@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1457 1457
953  
954 1457 ▶ if ($req->{_permission_resumed} && $grant->{decision} eq "ask") {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{"_permission_resumed"} and $grant->{"decision"} eq "ask") 1415 42 1457
Condition66%2/3
$req->{"_permission_resumed"} and $grant->{"decision"} eq "ask"
!ll&&!rl&&r
4201415
MC/DC50%1/2
$req->{"_permission_resumed"} and $grant->{"decision"} eq "ask"
$req->{"_permission_resumed"} $grant->{"decision"} eq "ask"
Truth table66%2/3
A and B
A= $req->{"_permission_resumed"}B= $grant->{"decision"} eq "ask"
ABresult
0X0
100
111
955 1415 my $key = JSON::XS->new->canonical->encode ($request);
956 return $done->(1, $req->{_permission_grants}{$key})
957 1415 ▶ if exists $req->{_permission_grants}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $req->{"_permission_grants"}{$key} 1 1414 1415
958 }
959  
960 1456 my $state = $req->{params}{requestState};
961  
962 1456 ▶ if (!$req->{_permission_resumed} && defined $state && $state =~ /\Aae-mcp-permission:/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $req->{"_permission_resumed"} and defined $state and $state =~ /\Aae-mcp-permission:/u) 19 1437 1456
Condition83%5/6
not $req->{"_permission_resumed"} and defined $state and $state =~ /\Aae-mcp-permission:/u
!ll&&!rl&&r
1437019
not $req->{"_permission_resumed"} and defined $state
!ll&&!rl&&r
14142319
MC/DC66%2/3
not $req->{"_permission_resumed"} and defined $state and $state =~ /\Aae-mcp-permission:/u
not $req->{"_permission_resumed"} defined $state $state =~ /\Aae-mcp-permission:/u
Truth table75%3/4
not(A) and B and C
A= not $req->{"_permission_resumed"}B= defined $stateC= $state =~ /\Aae-mcp-permission:/u
ABCresult
0XX0
10X0
1100
1111
963 19 my $pending = delete $self->{pending}{$state};
964 return $done->(0, "Invalid or expired permission continuation")
965 unless $pending && $pending->{call} eq _call ($req)
966 19 ▶ && JSON::XS->new->canonical->encode ($pending->{request}) eq JSON::XS->new->canonical->encode ($request);
Branch: 1 branch
BranchTrueFalseTotal
unless $pending and $pending->{"call"} eq _call($req) and "JSON::XS"->new->canonical->encode($pending->{"request"}) eq "JSON::XS"->new->canonical->encode($request) 6 13 19
Condition83%5/6
$pending and $pending->{"call"} eq _call($req) and "JSON::XS"->new->canonical->encode($pending->{"request"}) eq "JSON::XS"->new->canonical->encode($request)
!ll&&!rl&&r
6013
$pending and $pending->{"call"} eq _call($req)
!ll&&!rl&&r
3313
MC/DC66%2/3
$pending and $pending->{"call"} eq _call($req) and "JSON::XS"->new->canonical->encode($pending->{"request"}) eq "JSON::XS"->new->canonical->encode($request)
$pending $pending->{"call"} eq _call($req) "JSON::XS"->new->canonical->encode($pending->{"request"}) eq "JSON::XS"->new->canonical->encode($request)
Truth table75%3/4
A and B and C
A= $pendingB= $pending->{"call"} eq _call($req)C= "JSON::XS"->new->canonical->encode($pending->{"request"}) eq "JSON::XS"->new->canonical->encode($request)
ABCresult
0XX0
10X0
1100
1111
967 13 my $response = $req->{params}{inputResponses}{permission};
968 13 my $decision = eval {
969 AnyEvent::MCP::Permissions::Ask::decision
970 $pending->{prepared}, $response, $pending->{stage}, $pending->{selection}
971 13 };
972 13 ▶ return $done->(0, "$@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 13 13
973 13 ▶ if (my $next = _next_stage ($decision, $pending->{stage})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = _next_stage($decision, $pending->{"stage"})) 2 11 13
974 $self->_challenge ($req, $pending->{request}, $pending->{prepared}, $pending->{grants},
975 2 ▶ $next, ($pending->{rounds} || 1) + 1, { %{$pending->{selection} || {}}, %$decision });
Condition50%2/4
$pending->{'rounds'} || 1
l!l
20
$pending->{'selection'} || {}
l!l
20
MC/DC0%0/1
$pending->{'rounds'} || 1
$pending->{'rounds'}
MC/DC0%0/1
$pending->{'selection'} || {}
$pending->{'selection'}
Truth table50%1/2
A
A= $pending->{'rounds'}
Aresult
11
00
Truth table50%1/2
A
A= $pending->{'selection'}
Aresult
11
00
976 return
977 2 }
978 11 $grant = eval { $self->resolve ($request, { request => $pending->{prepared}, decision => $decision }, $req->{tool}) };
979 11 ▶ return $done->(0, "$@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 11 11
980 }
981  
982 1448 ▶ return $done->(1, $grant->{sandbox_grants}) if $grant->{decision} eq "allow";
Branch: 1 branch
BranchTrueFalseTotal
if $grant->{"decision"} eq "allow" 8 1440 1448
983 1440 ▶ if ($grant->{decision} eq "deny") {
Branch: 1 branch
BranchTrueFalseTotal
if ($grant->{"decision"} eq "deny") 3 1437 1440
984 3 AE::log debug => "permission policy: denied";
985 3 return $done->(0, denial_message ($grant));
986 }
987  
988 1437 my $prepared = $grant->{request};
989 1437 my $ask = $self->{ask};
990 1437 my $client_caps = $req->{params}{_meta}{"io.modelcontextprotocol/clientCapabilities"};
991 my $mcp = $req->{protocol_version} ge "2026" && ref $client_caps eq "HASH"
992 && ref $client_caps->{elicitation} eq "HASH"
993 1437 ▶ && (!keys %{$client_caps->{elicitation}} || ref $client_caps->{elicitation}{form} eq "HASH");
Condition91%11/12
$req->{'protocol_version'} ge '2026' && ref $client_caps eq 'HASH' && ref $client_caps->{'elicitation'} eq 'HASH' && (!keys(%{$$client_caps{'elicitation'};}) || ref $client_caps->{'elicitation'}{'form'} eq 'HASH')
!ll&&!rl&&r
2011416
$req->{"protocol_version"} ge 2026 and ref $client_caps eq "HASH" and ref $client_caps->{"elicitation"} eq "HASH"
!ll&&!rl&&r
1551417
$req->{"protocol_version"} ge 2026 and ref $client_caps eq "HASH"
!ll&&!rl&&r
1501422
not keys %{$$client_caps{"elicitation"};} or ref $client_caps->{"elicitation"}{"form"} eq "HASH"
l!l&&r!l&&!r
214141
MC/DC80%4/5
$req->{'protocol_version'} ge '2026' && ref $client_caps eq 'HASH' && ref $client_caps->{'elicitation'} eq 'HASH' && (!keys(%{$$client_caps{'elicitation'};}) || ref $client_caps->{'elicitation'}{'form'} eq 'HASH')
$req->{"protocol_version"} ge 2026 ref $client_caps eq "HASH" ref $client_caps->{"elicitation"} eq "HASH" not keys %{$$client_caps{"elicitation"};} ref $client_caps->{"elicitation"}{"form"} eq "HASH"
Truth table83%5/6
A and B and C && not(D) or E
A= $req->{"protocol_version"} ge 2026B= ref $client_caps eq "HASH"C= ref $client_caps->{"elicitation"} eq "HASH"D= not keys %{$$client_caps{"elicitation"};}E= ref $client_caps->{"elicitation"}{"form"} eq "HASH"
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
994  
995 1437 ▶ if (!ref $ask && ($ask eq "mcp" || $ask eq "auto" && $mcp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $ask and $ask eq "mcp" || $ask eq "auto" && $mcp) 1415 22 1437
Condition77%7/9
not ref $ask and $ask eq "mcp" || $ask eq "auto" && $mcp
!ll&&!rl&&r
11111415
$ask eq "mcp" or $ask eq "auto" and $mcp
l!l&&r!l&&!r
1415011
$ask eq "auto" and $mcp
!ll&&!rl&&r
650
MC/DC50%2/4
not ref $ask and $ask eq "mcp" || $ask eq "auto" && $mcp
not ref $ask $ask eq "mcp" $ask eq "auto" $mcp
Truth table80%4/5
not(A) and B or C and D
A= not ref $askB= $ask eq "mcp"C= $ask eq "auto"D= $mcp
ABCDresult
0XXX0
100X0
10100
11XX1
10111
996 1415 ▶ return $done->(0, "MCP form elicitation is unavailable") unless $mcp;
Branch: 1 branch
BranchTrueFalseTotal
unless $mcp 1 1414 1415
997  
998 $self->_challenge ($req, $request, $prepared, $req->{_permission_grants},
999 1414 "choice", 1);
1000 return
1001 1414 }
1002  
1003 22 weaken $self;
1004  
1005 22 my $finished;
1006 21 ▶ my $finish = sub {
Subroutine __ANON__: called CC=7 SCAR=19.9
1007 21 my ($success, $decision) = @_;
1008  
1009 21 ▶ return if !$self || $req->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if not $self or $req->{"finished"} 1 20 21
Condition66%2/3
not $self or $req->{"finished"}
l!l&&r!l&&!r
0120
MC/DC50%1/2
not $self or $req->{"finished"}
not $self $req->{"finished"}
Truth table66%2/3
not(A) or B
A= not $selfB= $req->{"finished"}
ABresult
1X1
011
000
1010 20 ▶ die "Permission prompt already finished\n" if $finished++;
Branch: 1 branch
BranchTrueFalseTotal
if $finished++ 0 20 20
1011 20 ▶ return $done->(0, $decision) unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 20 20
1012  
1013 20 my $grant = eval { $self->resolve ($request, { request => $prepared, decision => $decision }, $req->{tool}) };
1014  
1015 20 ▶ return $done->(0, "$@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 20 20
1016  
1017 $done->($grant->{decision} eq "allow" ? (1, $grant->{sandbox_grants})
1018 20 ▶ : (0, denial_message ($grant)));
Branch: 1 branch
BranchTrueFalseTotal
$grant->{'decision'} eq 'allow' ? : 17 3 20
1019 22 };
1020  
1021 22 ▶ if (ref $ask eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ask eq 'CODE') { } 11 11 22
1022 11 $ask->($prepared, $finish);
1023 } else {
1024 11 ▶ die "Unknown permission asking method\n" unless $ask =~ /\A(?:auto|helper|tty|yad)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ask =~ /\A(?:auto|helper|tty|yad)\z/u 0 11 11
1025  
1026 my $job = AnyEvent::MCP::Permissions::Ask->new (
1027 helper => $ask eq "tty" || $ask eq "yad"
1028 ? ["anyevent-mcp-askpermission", "--$ask"] : $self->{helper},
1029 timeout => $self->{timeout},
1030 11 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$ask eq 'tty' || $ask eq 'yad' ? : 2 9 11
Condition100%3/3
$ask eq "tty" or $ask eq "yad"
l!l&&r!l&&!r
119
MC/DC100%2/2
$ask eq "tty" or $ask eq "yad"
$ask eq "tty" $ask eq "yad"
Truth table100%3/3
A or B
A= $ask eq "tty"B= $ask eq "yad"
ABresult
1X1
011
000
1031  
1032 11 my $old_cancel = $req->{on_cancel};
1033 0 ▶ $req->{on_cancel} = sub { $job->cancel; $old_cancel->(@_) if $old_cancel };
Subroutine __ANON__: not called CC=2 SCAR=14.0
Branch: 1 branch
BranchTrueFalseTotal
if $old_cancel 0 0 0
1034 11 $job->ask ($prepared, $finish);
1035 }
1036 }
1037  
1038 325 1
« blib/lib/AnyEvent/MCP/Server.pm ↑ Summary lib/AnyEvent/MCP/IMAP/Providers.pm »