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

filter: Statementstmt 98.6% 355 / 360 Branchbran 83.9% 230 / 274 Conditioncond 64.3% 231 / 359 MC/DCmcdc 45.1% 83 / 184 Subroutinesub 93.7% 30 / 32 Podpod 90.0% 9 / 10 totaltotal 76.9% 938 / 1219 CC 279 SCAR 40.7
CC
279
Cov
82%
check
145.3
resolve
65.0
_rule
41.4
CRAP 58.4
« blib/lib/AnyEvent/MCP/Server.pm ↑ Summary blib/lib/AnyEvent/MCP.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>, C<mcp> or C<none>. When omitted, uses
197 C<PERL_ANYEVENT_MCP_PERMISSION_ASK>, defaulting to C<helper>.
198 C<none> denies only otherwise-unresolved checks without prompting.
199 C<helper> prompts locally using the configured helper. C<tty> and C<yad>
200 run the built-in permission helper with only that interface, ignoring custom
201 helper settings and without interface fallback. C<auto> uses modern MCP form elicitation when
202 advertised for the request, otherwise the helper. C<mcp> requires modern
203 form elicitation and fails when unavailable. A declined prompt never
204 falls back to a different approval mechanism.
205 Choosing C<mcp> also trusts that MCP client to assert the original user
206 whose saved permission rules are evaluated, even for calls whose rules
207 already allow or deny without asking. C<auto> grants that identity trust
208 only where its modern elicitation path was selected. C<helper>, C<tty>
209 and C<yad> do not trust client-supplied user assertions. Avoid C<mcp>
210 and C<auto> with clients not trusted to make permission decisions.
211  
212 =item ask => $callback->($prepared, $done->($success, $decision))
213  
214 Local asynchronous asking callback. C<$prepared> is the prepared request;
215 C<$done> receives a decision hash on success or an error on failure.
216 Callback success means a decision was obtained, not that access was granted.
217  
218 =item helper => $executable
219  
220 =item helper => [$executable, $arg...]
221  
222 External helper, not a shell command. Defaults to
223 C<PERL_ANYEVENT_MCP_PERMISSION_HELPER> or C<anyevent-mcp-askpermission>.
224  
225 =item timeout => $seconds
226  
227 External helper timeout, defaulting to 300 seconds. Also determines the
228 lifetime of each modern permission continuation. Use a positive value;
229 zero makes continuations expire immediately, while the helper treats
230 zero as its default timeout.
231  
232 =item auto_rules => $rules
233  
234 Ordered process-local permission rules, each C<[DECISION, SPEC]> where
235 C<DECISION> is C<allow> or C<deny> and C<SPEC> is
236 C<TOOL[,ACTION[,CAPABILITY]]>. Each field is an exact name or C<*>
237 (any); omitted trailing fields mean C<*>. Tool names treat underscores
238 and hyphens alike. A named capability must be known to this server.
239 Allow matches when all requested grants are implied by the specified
240 capability; deny matches when the requested grants imply that capability.
241 C<*> matches any capability set in either direction. First match wins,
242 before saved rules and prompting. Only tools which call the permission
243 engine are affected. Automatic allowance grants exactly the requested
244 sandbox grants; tool validation and sandbox enforcement still apply.
245  
246 =back
247  
248 =item $grant = $permissions->check ($request, $tool)
249  
250 C<$request> contains C<action>, C<reason>, and C<sandbox_grants> (an array
251 reference; it may be empty). C<tool> in the request is optional display
252 context. The separate optional C<$tool> argument is the trusted dispatched
253 MCP tool name for automatic overrides; Server supplies it from the call.
254 C<dimensions> is an optional hash of independent, named identities. Every
255 dimension has a display C<label>, a nonempty C<value>, and an array of
256 permitted persistent C<choices>, each with C<label> and C<match>. For example:
257  
258 dimensions => {
259 origin => {
260 label => "Origin",
261 value => "https://example.org:8443",
262 default => "https://example.org:8443",
263 choices => [
264 { label => "This origin", match => "https://example.org:8443" },
265 { label => "Any origin", match => "^" },
266 ],
267 },
268 }
269  
270 C<default> optionally selects the initially proposed matcher for persistent
271 rule prompts; it must be one of the offered choices. Without it, the first
272 choice is selected.
273 The engine does not interpret dimension names. Saved rules use
274 C<match.origin>, C<match.subject>, C<match.workdir>, or any other
275 C<match.NAME>. Every selected matcher must match the corresponding request
276 value. A saved matcher for a missing dimension does not match. An omitted
277 C<match.NAME> in a manually written rule matches any value, including absent
278 dimensions; persistent prompts instead require a choice for every dimension.
279 An empty choices array disables permanent approvals, not one-time approvals.
280 Matchers are literal unless beginning with C<^>, which denotes a Perl regex.
281 The caller must supply trustworthy dimension values, not untrusted tool arguments.
282 The former C<subject>, C<subjects>, C<origin>, C<origins>, and C<workdir>
283 request and saved-rule fields are unsupported.
284 The multibroker alias, when supplied by the child's environment, is display-only
285 and does not change rule matching or policy file selection.
286 C<paths> may contain a list of affected filenames for display in permission
287 prompts and for optional B<deny-only> exact-file rules. Such matching does
288 not restrict sandbox authority and is not a substitute for sandboxing.
289 Absent a C<path> matcher, the permission rule is not limited by paths.
290  
291 When prompting for a new rule, each scope defaults to the latest saved
292 matcher for that dimension if it is still among the offered choices.
293 Otherwise the application's first (usually narrowest) choice is selected.
294 Application-specified C<default> values take precedence. This only affects
295 the rule editor; it never grants permission by itself.
296  
297 Returns C<{decision =E<gt> "allow", sandbox_grants =E<gt> [...]}>,
298 C<{decision =E<gt> "deny", sandbox_grants =E<gt> []}>, or
299 C<{decision =E<gt> "ask", request =E<gt> $prepared}>.
300 Denials can also include a saved C<message>. C<check> does not prompt or
301 execute the operation.
302  
303 =item $grant = $permissions->resolve ($request, $approval)
304  
305 C<$request> is the original permission request. C<$approval> is
306 C<{ request =E<gt> $prepared, decision =E<gt> $decision }>, where
307 C<$prepared> is the request returned by C<check> and C<$decision> is the
308 helper's decision hash. Resolves the answer to that prepared request. Approval decisions are
309 C<allow-once>, C<allow-always>, C<deny-once>, and C<deny-always>.
310 Allow-once uses exactly the requested sandbox grants. Allow-always may save
311 a broader or narrower set of grants for future invocations; the present
312 invocation still receives no more than requested. Permanent decisions
313 contain a validated rule restricted to the offered action and dimension scopes.
314 Denial decisions may include an optional human C<message>; allowed tool
315 results are never modified by permission prompting.
316  
317 This is a trusted application interface. Do not pass unauthenticated client
318 metadata directly to it.
319  
320 =item $grant = $permissions->require_permission ($req, { %request })
321  
322 Synchronous interface used by C<< $req->require_permission >>. Returns
323 the granted sandbox-grant array, dies on denial, or throws a permission-needed
324 exception for the server to handle. The worker must enforce the returned grants;
325 never treat merely requested grants as sufficient.
326  
327 =item $permissions->require ($req, $request, $done->($success, $grant))
328  
329 Asynchronous interface. C<$grant> is the allowed sandbox-grant array or an
330 error. Check before starting side effects.
331  
332 With a local prompt, C<$done> runs when permission is resolved. Modern
333 MCP prompting instead completes C<$req> with C<input_required> without
334 calling C<$done>; the client repeats the call with input. Original
335 callbacks are not retained.
336 Continuations are short-lived and bound to the method, parameters, and request,
337 and are consumed once. Approval retries retain earlier one-time grants
338 within the same call. Always decisions and Back use additional protected C<input_required>
339 rounds, capped at 32, without executing the original tool. Messages in
340 persistent deny rules are returned to subsequent callers, including later
341 sessions. Client-side elicitation is only appropriate when the connected
342 client is the trusted approval authority.
343  
344 =item $closure = $permissions->closure ([$grant...])
345  
346 Returns a hash reference whose keys contain the supplied sandbox grants
347 and all grants they imply. Unknown grants or malformed implication lists
348 die. This expands authority names; it does not grant permission.
349  
350 =item $permissions->auto_rules ($rules)
351  
352 Replace the ordered process-local rule chain. Parsing and validation
353 occur once, at installation. An empty list disables automatic rules.
354 With an optional true second argument, prepend the new rules before existing
355 process-local rules instead of replacing them.
356  
357 =item $rules = $permissions->rules
358  
359 Returns the ordered, uncompiled in-memory or file-backed rule array reference.
360 File-backed rules are reread, so changes apply to subsequent checks.
361  
362 =item $permissions->save ($rules)
363  
364 Replaces the ordered rules without semantic validation, using atomic file
365 replacement if backed by a file.
366  
367 =back
368  
369 =cut
370  
371 package AnyEvent::MCP::Server::Permissions;
372  
373 53 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
374 53 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
375 53 ▶use AnyEvent::MCP::Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
376 53 ▶use AnyEvent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
377 53 ▶use AnyEvent::MCP::Permissions::Ask ();
Subroutine BEGIN: called CC=1 SCAR=0.0
378 53 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
379 53 ▶use Errno qw(ENOENT);
Subroutine BEGIN: called CC=1 SCAR=0.0
380 53 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
381  
382 53 our @ISA = qw(AnyEvent::MCP::Base);
383  
384 128 ▶sub new {
Subroutine new: called CC=13 SCAR=25.7
Pod documented
385 128 my ($class, %kv) = @_;
386  
387 128 ▶ my $auto_rules = delete $kv{auto_rules} || [];
Condition100%2/2
delete $kv{'auto_rules'} || []
l!l
9119
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
388 128 ▶ my $ask = exists $kv{ask} ? $kv{ask} : $ENV{PERL_ANYEVENT_MCP_PERMISSION_ASK};
Branch: 1 branch
BranchTrueFalseTotal
exists $kv{'ask'} ? : 28 100 128
389 128 ▶ $ask = "helper" unless defined $ask && length $ask;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ask and length $ask 82 46 128
Condition100%3/3
defined $ask and length $ask
!ll&&!rl&&r
81146
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
390 128 ▶ die "Invalid permission ask mode (expected helper, tty, yad, auto, mcp, or none)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ask eq "CODE" or not ref $ask and $ask =~ /\A(?:helper|tty|yad|auto|mcp|none)\z/u 1 127 128
Condition83%5/6
ref $ask eq "CODE" or not ref $ask and $ask =~ /\A(?:helper|tty|yad|auto|mcp|none)\z/u
l!l&&r!l&&!r
51221
not ref $ask and $ask =~ /\A(?:helper|tty|yad|auto|mcp|none)\z/u
!ll&&!rl&&r
01122
MC/DC66%2/3
ref $ask eq "CODE" or not ref $ask and $ask =~ /\A(?:helper|tty|yad|auto|mcp|none)\z/u
ref $ask eq "CODE" not ref $ask $ask =~ /\A(?:helper|tty|yad|auto|mcp|none)\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|none)\z/u
ABCresult
1XX1
0111
00X0
0100
391 unless ref $ask eq "CODE" || !ref $ask && $ask =~ /\A(?:helper|tty|yad|auto|mcp|none)\z/;
392  
393 127 my $self = $class->SUPER::new (sandbox_grants => {}, actions => {}, rules => [], ask => $ask, timeout => 300, %kv);
394  
395 127 ▶ if (!exists $kv{file} && defined $kv{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $kv{"file"} and defined $kv{"name"}) 38 89 127
Condition100%3/3
not exists $kv{"file"} and defined $kv{"name"}
!ll&&!rl&&r
276238
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
396 38 my $name = AnyEvent::MCP::utf8enc $kv{name};
397 38 $name =~ s/([^A-Za-z0-9_.-])/sprintf "%%%02X", ord $1/ge;
398 38 ▶ 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
371
$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
399 38 $self->{file} = "$dir/anyevent-mcp/rules/$name.ini";
400 }
401  
402 127 $self->closure ([keys %{$self->{sandbox_grants}}]);
403 127 $self->_rule ($_) for @{$self->rules};
404 127 $self->auto_rules ($auto_rules);
405 127 $self
406 }
407  
408 149 ▶sub auto_rules {
Subroutine auto_rules: called CC=17 SCAR=30.9
Pod documented
409 149 my ($self, $rules, $prepend) = @_;
410  
411 149 ▶ 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" 149 0 149
412 149 my @compiled;
413 149 for my $entry (@$rules) {
414 40 ▶ 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" 40 0 40
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
0040
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
0040
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
0040
ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0] and not ref $entry->[0]
!ll&&!rl&&r
0040
ref $entry eq "ARRAY" and @$entry == 2 and defined $entry->[0]
!ll&&!rl&&r
0040
ref $entry eq "ARRAY" and @$entry == 2
!ll&&!rl&&r
0040
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
415 && defined $entry->[0] && !ref $entry->[0] && $entry->[0] =~ /\A(?:allow|deny)\z/
416 && defined $entry->[1] && !ref $entry->[1]
417 or die "Invalid automatic permission rule\n";
418 40 my @parts = split /,/, $entry->[1], -1;
419 40 ▶ @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" 34 6 40
Condition100%12/12
@parts and @parts <= 3 and not grep({not length $_ or $_ ne "*" and /[\s*]/u;} @parts)
!ll&&!rl&&r
2434
@parts and @parts <= 3
!ll&&!rl&&r
1138
!length($_) || $_ ne '*' && /[\s*]/u
l!l&&r!l&&!r
2281
$_ ne "*" and /[\s*]/u
!ll&&!rl&&r
40412
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
420 or die "Invalid automatic permission rule '$entry->[1]'\n";
421 34 my ($tool, $action, $capability) = (@parts, ("*") x (3 - @parts));
422 34 $tool =~ y/_/-/;
423 34 ▶ push @compiled, {
Branch: 1 branch
BranchTrueFalseTotal
$capability eq '*' ? : 25 9 34
424 decision => $entry->[0],
425 tool => $tool,
426 action => $action,
427 capability => $capability,
428 closure => $capability eq "*" ? undef : $self->closure ([$capability]),
429 };
430 }
431 142 ▶ push @compiled, @{$self->{auto_rules}} if $prepend;
Branch: 1 branch
BranchTrueFalseTotal
if $prepend 9 133 142
432 142 $self->{auto_rules} = \@compiled;
433 }
434  
435 1513 ▶sub closure {
Subroutine closure: called CC=7 SCAR=19.7
Pod documented
436 1513 my ($self, $sandbox_grants) = @_;
437  
438 1513 ▶ ref $sandbox_grants eq "ARRAY"
Branch: 1 branch
BranchTrueFalseTotal
ref $sandbox_grants eq "ARRAY" or die "Sandbox grants must be an array\n" 1513 0 1513
439 or die "Sandbox grants must be an array\n";
440  
441 1513 my %set;
442  
443 1513 my @todo = @$sandbox_grants;
444  
445 1513 ▶ while (@todo) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo) 2411 1508 3919
446 2411 my $capability = pop @todo;
447  
448 defined $capability && !ref $capability
449 2411 ▶ && 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" 2406 5 2411
Condition50%3/6
defined $capability and not ref $capability and ref $self->{"sandbox_grants"}{$capability} eq "ARRAY"
!ll&&!rl&&r
052406
defined $capability and not ref $capability
!ll&&!rl&&r
002411
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
450 or die "Unknown sandbox grant\n";
451  
452 2406 ▶ next if $set{$capability}++;
Branch: 1 branch
BranchTrueFalseTotal
if $set{$capability}++ 474 1932 2406
453 1932 push @todo, @{$self->{sandbox_grants}{$capability}};
454 }
455  
456 1508 \%set
457 }
458  
459 1975 ▶sub _regex {
Subroutine _regex: called CC=4 SCAR=14.9
460 1975 my ($pattern) = @_;
461  
462 1975 ▶ defined $pattern && !ref $pattern
Branch: 1 branch
BranchTrueFalseTotal
defined $pattern and not ref $pattern or die "Permission matcher must be a string\n" 1975 0 1975
Condition33%1/3
defined $pattern and not ref $pattern
!ll&&!rl&&r
001975
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
463 or die "Permission matcher must be a string\n";
464  
465 1975 ▶ $pattern =~ /\A\^/ ? qr/$pattern/ : qr/\A\Q$pattern\E\z/
Branch: 1 branch
BranchTrueFalseTotal
$pattern =~ /\A\^/u ? : 632 1343 1975
466 }
467  
468 794 ▶sub _rule {
Subroutine _rule: called CC=27 SCAR=37.2
469 794 my ($self, $rule) = @_;
470  
471 794 ▶ ref $rule eq "HASH"
Branch: 1 branch
BranchTrueFalseTotal
ref $rule eq "HASH" or die "Invalid permission rule\n" 794 0 794
472 or die "Invalid permission rule\n";
473 794 ▶ !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 788 794
474 or die "Unknown permission rule field\n";
475  
476 die "Invalid permission rule expiry\n" if exists $rule->{expires}
477 788 ▶ && (!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 776 788
Condition66%6/9
exists $rule->{"expires"} and !defined($rule->{"expires"}) || ref $rule->{"expires"} || !($rule->{"expires"} =~ /\A[0-9]+\z/u)
!ll&&!rl&&r
7552112
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
478  
479 776 my $decision = $rule->{decision};
480  
481 776 ▶ 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 773 776
Condition66%2/3
defined $decision and $decision =~ /\A(?:allow|deny|ask)\z/u
!ll&&!rl&&r
30773
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
482 die "Only deny rules can have paths or messages\n"
483 773 ▶ 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 773 773
Condition50%3/6
$decision ne "deny" and exists $rule->{"path"} || exists $rule->{"message"}
!ll&&!rl&&r
1795940
exists $rule->{"path"} or exists $rule->{"message"}
l!l&&r!l&&!r
00594
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
484 773 ▶ 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 773 773
Condition66%2/3
$decision eq "allow" and not exists $rule->{"sandbox-grants"}
!ll&&!rl&&r
1865870
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
485  
486 defined $rule->{action}
487 773 ▶ or die "Permission rule needs action\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $rule->{"action"} or die "Permission rule needs action\n" 773 0 773
488  
489 773 my $compiled = { decision => $decision, action => _regex ($rule->{action}) };
490 773 ▶ if (exists $rule->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"path"}) 15 758 773
491 die "Invalid exact denial path\n" if !defined $rule->{path}
492 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
493 15 $compiled->{path} = $rule->{path};
494 }
495 773 my $message = $rule->{message};
496 773 ▶ if (defined $message) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message) 15 758 773
497 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
498 || length $message > 2048 || $message =~ /[\r\n]/;
499 15 $compiled->{message} = $message;
500 }
501  
502 773 for my $key (grep /^match\./, keys %$rule) {
503 351 (my $name = $key) =~ s/^match\.//;
504 351 $compiled->{matches}{$name} = _regex ($rule->{$key});
505 }
506  
507 770 ▶ if (exists $rule->{"sandbox-grants"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"sandbox-grants"}) 676 94 770
508 676 ▶ die "Invalid rule sandbox-grants\n" if ref $rule->{"sandbox-grants"};
Branch: 1 branch
BranchTrueFalseTotal
if ref $rule->{"sandbox-grants"} 0 676 676
509 $compiled->{sandbox_grants} = $self->closure ([length $rule->{"sandbox-grants"}
510 676 ▶ ? split /,/, $rule->{"sandbox-grants"}, -1 : ()]);
Branch: 1 branch
BranchTrueFalseTotal
length $rule->{'sandbox-grants'} ? : 260 416 676
511 }
512  
513 $compiled
514 767 }
515  
516 685 ▶sub rules {
Subroutine rules: called CC=6 SCAR=17.9
Pod documented
517 685 my ($self) = @_;
518  
519 685 my $now = time;
520  
521 [grep {
522 die "Invalid permission rule expiry\n" if exists $_->{expires}
523 453 ▶ && (!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 447 453
Condition100%9/9
exists $_->{"expires"} and !defined($_->{"expires"}) || ref $_->{"expires"} || !($_->{"expires"} =~ /\A[0-9]+\z/u)
!ll&&!rl&&r
421266
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
524 447 ▶ !exists $_->{expires} || $_->{expires} > $now
Condition100%3/3
!exists $_->{'expires'} || $_->{'expires'} > $now
l!l&&r!l&&!r
4211214
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
525 685 } @{$self->_read_rules}]
526 }
527  
528 685 ▶sub _read_rules {
Subroutine _read_rules: called CC=11 SCAR=24.2
529 685 my ($self) = @_;
530  
531 685 ▶ return $self->{rules} unless defined $self->{file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"file"} 346 339 685
532  
533 339 ▶ open my $fh, "<:raw", $self->{file} or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $self->{"file"} or do { return [] if $! == 2; die qq[$self->{"file"}: $!\n] } 261 78 339
534 78 ▶ return [] if $! == ENOENT;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 2 78 0 78
535 0 die "$self->{file}: $!\n";
536 };
537  
538 261 my @rules;
539 261 my $valid = eval {
540 261 for (@{AnyEvent::MCP::Config::parse_ini (AnyEvent::MCP::file_load ($self->{file}, $fh), $self->{file})}) {
541 667 my ($name, $kv) = @$_;
542 667 ▶ if ($name eq "") {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq '') { } 258 409 667
elsif ($name eq 'rule') { } 406 3 409
543 258 ▶ %$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 258 258
544 } elsif ($name eq "rule") {
545 406 push @rules, $kv;
546 } else {
547 3 die "$self->{file}: invalid permission rule section '$name'\n";
548 }
549 }
550 255 $self->_rule ($_) for @rules;
551 234 1
552 };
553 261 ▶ close $fh or die "$self->{file}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die qq[$self->{"file"}: $!\n] 261 0 261
554  
555 261 ▶ $valid ? \@rules : []
Branch: 1 branch
BranchTrueFalseTotal
$valid ? : 234 27 261
556 }
557  
558 96 ▶sub _write {
Subroutine _write: called CC=2 SCAR=6.9
559 96 my ($self, $rules) = @_;
560  
561 96 ▶ unless (defined $self->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"file"}) 38 58 96
562 38 $self->{rules} = $rules;
563 return
564 38 }
565  
566 AnyEvent::MCP::file_save $self->{file},
567 58 AnyEvent::MCP::Config::serialize_ini [map ["rule", $_], @$rules];
568 }
569  
570 165 ▶sub _lock {
Subroutine _lock: called CC=2 SCAR=6.9
571 165 my ($self) = @_;
572  
573 165 ▶ defined $self->{file} ? AnyEvent::MCP::file_lock ($self->{file}) : undef
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'file'} ? : 75 90 165
574 }
575  
576 66 ▶sub save {
Subroutine save: called CC=1 SCAR=0.0
Pod documented
577 66 my ($self, $rules) = @_;
578  
579 66 my $lock = $self->_lock;
580 66 $self->_write ($rules);
581 }
582  
583 537 ▶sub check {
Subroutine check: called CC=81 SCAR=49.8
Pod documented
584 537 my ($self, $request, $tool) = @_;
585  
586 537 ▶ my $need = $self->closure ($request->{sandbox_grants} // []);
Condition50%1/2
$request->{"sandbox_grants"} // []
l!l
5370
MC/DC0%0/1
$request->{"sandbox_grants"} // []
$request->{"sandbox_grants"}
Truth table50%1/2
A
A= $request->{"sandbox_grants"}
Aresult
11
00
587 die "Permission action must be a string\n" unless defined $request->{action}
588 536 ▶ && !ref $request->{action} && length $request->{action};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $request->{"action"} and not ref $request->{"action"} and length $request->{"action"} 1 535 536
Condition50%3/6
defined $request->{"action"} and not ref $request->{"action"} and length $request->{"action"}
!ll&&!rl&&r
01535
defined $request->{"action"} and not ref $request->{"action"}
!ll&&!rl&&r
00536
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
589 535 die "Unknown permission action\n" if keys %{$self->{actions}}
590 535 ▶ && !exists $self->{actions}{$request->{action}};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"actions"};} and not exists $self->{"actions"}{$request->{"action"}} 0 535 535
Condition66%2/3
keys %{$$self{"actions"};} and not exists $self->{"actions"}{$request->{"action"}}
!ll&&!rl&&r
4171180
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
591 die "Obsolete permission request scope\n"
592 535 ▶ 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 534 535
593 534 ▶ my $dimensions = exists $request->{dimensions} ? $request->{dimensions} : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $request->{'dimensions'} ? : 491 43 534
594 534 ▶ ref $dimensions eq "HASH" or die "Invalid permission dimensions\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $dimensions eq "HASH" or die "Invalid permission dimensions\n" 534 0 534
595 534 for my $name (keys %$dimensions) {
596 430 ▶ $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" 430 0 430
597 430 my $dim = $dimensions->{$name};
598 ref $dim eq "HASH" && defined $dim->{value} && !ref $dim->{value} && length $dim->{value}
599 && defined $dim->{label} && !ref $dim->{label} && length $dim->{label}
600 430 ▶ && 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" 430 0 430
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
00430
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
00430
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
00430
ref $dim eq "HASH" and defined $dim->{"value"} and not ref $dim->{"value"} and length $dim->{"value"} and defined $dim->{"label"}
!ll&&!rl&&r
00430
ref $dim eq "HASH" and defined $dim->{"value"} and not ref $dim->{"value"} and length $dim->{"value"}
!ll&&!rl&&r
00430
ref $dim eq "HASH" and defined $dim->{"value"} and not ref $dim->{"value"}
!ll&&!rl&&r
00430
ref $dim eq "HASH" and defined $dim->{"value"}
!ll&&!rl&&r
00430
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
601 or die "Invalid permission dimension '$name'\n";
602 430 for my $choice (@{$dim->{choices}}) {
603 ref $choice eq "HASH" && defined $choice->{label} && !ref $choice->{label}
604 && length $choice->{label} && defined $choice->{match}
605 && $dim->{value} =~ _regex ($choice->{match})
606 849 ▶ 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" 845 4 849
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
04845
ref $choice eq "HASH" and defined $choice->{"label"} and not ref $choice->{"label"} and length $choice->{"label"} and defined $choice->{"match"}
!ll&&!rl&&r
00849
ref $choice eq "HASH" and defined $choice->{"label"} and not ref $choice->{"label"} and length $choice->{"label"}
!ll&&!rl&&r
00849
ref $choice eq "HASH" and defined $choice->{"label"} and not ref $choice->{"label"}
!ll&&!rl&&r
00849
ref $choice eq "HASH" and defined $choice->{"label"}
!ll&&!rl&&r
00849
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
607 }
608 426 ▶ if (exists $dim->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dim->{"default"}) 16 410 426
609 defined $dim->{default} && !ref $dim->{default}
610 16 ▶ && (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" 4 12 16
Condition100%6/6
defined $dim->{"default"} and not ref $dim->{"default"} and grep(($_->{"match"} eq $dim->{"default"}), @{$$dim{"choices"};})
!ll&&!rl&&r
664
defined $dim->{"default"} and not ref $dim->{"default"}
!ll&&!rl&&r
3310
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
611 or die "Permission dimension default was not offered\n";
612 }
613 }
614  
615 518 ▶ if (exists $request->{details}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $request->{"details"}) 7 511 518
616 7 ▶ ref $request->{details} eq "HASH"
Branch: 1 branch
BranchTrueFalseTotal
ref $request->{"details"} eq "HASH" or die "Permission details must be a hash\n" 7 0 7
617 or die "Permission details must be a hash\n";
618 defined && !ref
619 or die "Permission detail must be text\n"
620 7 ▶ for values %{$request->{details}};
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and not ref $_ or die "Permission detail must be text\n" 7 0 7
Condition33%1/3
defined $_ and not ref $_
!ll&&!rl&&r
007
MC/DC0%0/2
defined $_ and not ref $_
defined $_ not ref $_
Truth table33%1/3
A and not(B)
A= defined $_B= not ref $_
ABresult
0X0
100
111
621 }
622  
623 # use the dispatched tool name, never the request's display label.
624 518 ▶ my $tool_name = $tool // "";
Condition100%2/2
$tool // ""
l!l
327191
MC/DC100%1/1
$tool // ""
$tool
Truth table100%2/2
A
A= $tool
Aresult
11
00
625 518 $tool_name =~ y/_/-/;
626 518 for my $rule (@{$self->{auto_rules}}) {
627 89 ▶ 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 75 89
Condition100%3/3
$rule->{"tool"} ne "*" and $rule->{"tool"} ne $tool_name
!ll&&!rl&&r
522314
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
628 75 ▶ next if $rule->{action} ne "*" && $rule->{action} ne $request->{action};
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"action"} ne "*" and $rule->{"action"} ne $request->{"action"} 28 47 75
Condition100%3/3
$rule->{"action"} ne "*" and $rule->{"action"} ne $request->{"action"}
!ll&&!rl&&r
321528
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
629 47 ▶ if ($rule->{capability} ne "*") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"capability"} ne "*") 11 36 47
630 11 ▶ if ($rule->{decision} eq "allow") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{'decision'} eq 'allow') { } 7 4 11
631 7 ▶ next if grep !$rule->{closure}{$_}, keys %$need;
Branch: 1 branch
BranchTrueFalseTotal
if grep((!$rule->{"closure"}{$_}), keys %$need) 3 4 7
632 } else {
633 4 ▶ next unless $need->{$rule->{capability}};
Branch: 1 branch
BranchTrueFalseTotal
unless $$need{$rule->{"capability"}} 1 3 4
634 }
635 }
636 return {
637 decision => $rule->{decision},
638 43 ▶ sandbox_grants => $rule->{decision} eq "allow" ? [@{$request->{sandbox_grants} || []}] : [],
Branch: 1 branch
BranchTrueFalseTotal
$rule->{'decision'} eq 'allow' ? : 33 10 43
Condition50%1/2
$request->{'sandbox_grants'} || []
l!l
330
MC/DC0%0/1
$request->{'sandbox_grants'} || []
$request->{'sandbox_grants'}
Truth table50%1/2
A
A= $request->{'sandbox_grants'}
Aresult
11
00
639 };
640 }
641  
642 475 my $rules = $self->rules;
643 475 my @rules = map { $self->_rule ($_) } @$rules;
644 469 for my $rule (@rules) {
645 324 ▶ next unless $request->{action} =~ $rule->{action};
Branch: 1 branch
BranchTrueFalseTotal
unless $request->{"action"} =~ /$rule->{"action"}/u 148 176 324
646 next if grep {
647 125 ▶ !exists $dimensions->{$_} || $dimensions->{$_}{value} !~ $rule->{matches}{$_}
Condition100%3/3
exists $dimensions->{$_} && $dimensions->{$_}{'value'} =~ /$rule->{'matches'}{$_}/u
!ll&&!rl&&r
15866
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 176 ▶ } keys %{$rule->{matches} || {}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {not exists $dimensions->{$_} && $dimensions->{$_}{"value"} =~ /$rule->{"matches"}{$_}/u;} keys %{$rule->{"matches"} or {};} 59 117 176
Condition100%2/2
$rule->{'matches'} || {}
l!l
9878
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 117 ▶ && 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 114 117
Condition66%8/12
exists $rule->{"path"} and not ref $request->{"paths"} eq "ARRAY" && grep((defined $_ && !ref($_) && $_ eq $rule->{"path"}), @{$$request{"paths"};})
!ll&&!rl&&r
11043
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 4 ? (grep !$need->{$_}, keys %{$rule->{sandbox_grants}})
657 114 ▶ : (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 113 114
$rule->{'decision'} eq 'deny' ? : 4 75 79
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
35781
not exists $rule->{"path"} and exists $rule->{"sandbox_grants"}
!ll&&!rl&&r
43179
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 74 ▶ sandbox_grants => $rule->{decision} eq "allow" ? [@{$request->{sandbox_grants} || []}] : [],
Branch: 1 branch
BranchTrueFalseTotal
$rule->{'decision'} eq 'deny' && defined $rule->{'message'} ? : 4 106 110
Condition80%4/5
$request->{'sandbox_grants'} || []
l!l
740
$rule->{"decision"} eq "deny" and defined $rule->{"message"}
!ll&&!rl&&r
74324
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 113 ▶ } unless $rule->{decision} eq "ask";
Branch: 2 branches
BranchTrueFalseTotal
unless $rule->{"decision"} eq "ask" 110 3 113
$rule->{'decision'} eq 'allow' ? : 74 36 110
664  
665 last
666 3 }
667  
668 359 ▶ 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 359 359
Condition33%1/3
not ref $request->{"reason"} and $request->{"reason"} =~ /\S/u
!ll&&!rl&&r
00359
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 359 my $action_class = $request->{action};
671 359 ▶ $action_class = "^\Q$action_class\E\\z" if $action_class =~ /\A\^/;
Branch: 1 branch
BranchTrueFalseTotal
if $action_class =~ /\A\^/u 0 359 359
672  
673 359 my $descriptions = $self->{sandbox_grant_descriptions};
674 359 ▶ $descriptions = $descriptions->($request) if ref $descriptions eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
if ref $descriptions eq "CODE" 0 359 359
675 359 ▶ die "Invalid sandbox grant descriptions\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $descriptions and ref $descriptions ne "HASH" 0 359 359
Condition33%1/3
defined $descriptions and ref $descriptions ne "HASH"
!ll&&!rl&&r
35900
MC/DC0%0/2
defined $descriptions and ref $descriptions ne "HASH"
defined $descriptions ref $descriptions ne "HASH"
Truth table33%1/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 # Reuse the last saved choice for each dimension if it is still offered.
679 # This changes only the form default, not the authorization check.
680 359 my %prepared_dimensions = %$dimensions;
681 359 for my $name (keys %$dimensions) {
682 275 my $dim = $dimensions->{$name};
683 275 ▶ next if exists $dim->{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $dim->{"default"} 4 271 275
684 my ($selected) = map $_->{"match.$name"},
685 271 grep exists $_->{"match.$name"}, reverse @$rules;
686 $prepared_dimensions{$name} = { %$dim, default => $selected }
687 271 ▶ if defined $selected && grep $_->{match} eq $selected, @{$dim->{choices}};
Branch: 1 branch
BranchTrueFalseTotal
if defined $selected and grep(($_->{"match"} eq $selected), @{$$dim{"choices"};}) 40 231 271
Condition100%3/3
defined $selected and grep(($_->{"match"} eq $selected), @{$$dim{"choices"};})
!ll&&!rl&&r
1785340
MC/DC100%2/2
defined $selected and grep(($_->{"match"} eq $selected), @{$$dim{"choices"};})
defined $selected grep(($_->{"match"} eq $selected), @{$$dim{"choices"};})
Truth table100%3/3
A and B
A= defined $selectedB= grep(($_->{"match"} eq $selected), @{$$dim{"choices"};})
ABresult
0X0
100
111
688 }
689  
690 {
691 decision => "ask",
692 request => {
693 %$request,
694 dimensions => \%prepared_dimensions,
695 sandbox_grants => $request->{sandbox_grants} || [],
696 version => 1,
697 name => $self->{name},
698 (length ($ENV{PERL_ANYEVENT_MCP_MULTIBROKER_ALIAS} // "")
699 ? (multibroker_alias => $ENV{PERL_ANYEVENT_MCP_MULTIBROKER_ALIAS}) : ()),
700 action_matcher => $action_class,
701 359 ▶ sandbox_grant_choices => [sort keys %{$self->{sandbox_grants}}],
Condition50%1/2
$descriptions || {}
l!l
0359
MC/DC0%0/1
$descriptions || {}
$descriptions
Truth table50%1/2
A
A= $descriptions
Aresult
11
00
702 sandbox_grant_descriptions => $descriptions || {},
703 action_description => $self->{actions}{$request->{action}},
704 sandbox_implementation => $self->{sandbox_implementation},
705 }
706 }
707 359 ▶}
Branch: 1 branch
BranchTrueFalseTotal
length($ENV{'PERL_ANYEVENT_MCP_MULTIBROKER_ALIAS'} // '') ? : 16 343 359
Condition75%3/4
$request->{'sandbox_grants'} || []
l!l
3590
$ENV{"PERL_ANYEVENT_MCP_MULTIBROKER_ALIAS"} // ""
l!l
16343
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
708  
709 99 ▶sub resolve {
Subroutine resolve: called CC=41 SCAR=41.7
Pod documented
710 99 my ($self, $request, $approval, $tool) = @_;
711  
712 99 my $lock = $self->_lock;
713 99 my $grant = $self->check ($request, $tool);
714  
715 99 ▶ return $grant unless $grant->{decision} eq "ask";
Branch: 1 branch
BranchTrueFalseTotal
unless $grant->{"decision"} eq "ask" 4 95 99
716  
717 95 my $json = JSON::XS->new->canonical;
718  
719 die "Permission approval does not match request\n" unless ref $approval eq "HASH"
720 95 ▶ && $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 92 95
Condition66%2/3
ref $approval eq "HASH" and $json->encode($approval->{"request"}) eq $json->encode($grant->{"request"})
!ll&&!rl&&r
0392
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
721  
722 92 my $prepared = $grant->{request};
723 92 my $decision = $approval->{decision};
724  
725 die "Invalid permission decision\n" unless ref $decision eq "HASH"
726 92 ▶ && $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 92 92
Condition33%1/3
ref $decision eq "HASH" and $decision->{"decision"} =~ /\A(?:allow|deny)-(?:once|always)\z/u
!ll&&!rl&&r
0092
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
727  
728 92 my $allow = $decision->{decision} =~ /\Aallow-/;
729 92 my $granted = [];
730  
731 92 ▶ if ($allow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow) 66 26 92
732 66 my $have = $self->closure ($prepared->{sandbox_grants});
733 66 my $give = $self->closure ($decision->{sandbox_grants});
734 66 my %choices = map { $_ => 1 } @{$prepared->{sandbox_grant_choices}};
735  
736 66 ▶ die "Unknown selected sandbox grant\n" if grep !$choices{$_}, @{$decision->{sandbox_grants}};
Branch: 1 branch
BranchTrueFalseTotal
if grep((!$choices{$_}), @{$$decision{"sandbox_grants"};}) 0 66 66
737 $granted = [(grep !$give->{$_}, keys %$have)
738 ? (grep $give->{$_}, sort keys %$have)
739 66 ▶ : @{$prepared->{sandbox_grants}}];
Branch: 1 branch
BranchTrueFalseTotal
grep((!$give->{$_}), keys %$have) ? : 16 50 66
740 }
741  
742 92 ▶ if ($decision->{decision} =~ /-always\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decision->{"decision"} =~ /-always\z/u) 33 59 92
743 33 my $rule = $decision->{rule};
744 33 ▶ $rule = { decision => "allow", %$rule } if $allow && ref $rule eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if $allow and ref $rule eq "HASH" 25 8 33
Condition66%2/3
$allow and ref $rule eq "HASH"
!ll&&!rl&&r
8025
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
745 33 ▶ my $action = $allow ? "allow" : "deny";
Branch: 1 branch
BranchTrueFalseTotal
$allow ? : 25 8 33
746  
747 # the rule contains only the selected dimensions, never extra authority.
748 33 my %fields = map { $_ => 1 } qw(decision action sandbox-grants path message expires);
749 33 ▶ $fields{"match.$_"} = 1 for keys %{$prepared->{dimensions} || {}};
Condition50%1/2
$prepared->{'dimensions'} || {}
l!l
330
MC/DC0%0/1
$prepared->{'dimensions'} || {}
$prepared->{'dimensions'}
Truth table50%1/2
A
A= $prepared->{'dimensions'}
Aresult
11
00
750  
751 ref $rule eq "HASH"
752 && $rule->{action} eq $prepared->{action_matcher}
753 && ($rule->{decision} || "allow") eq $action
754 33 ▶ && !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" 30 3 33
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
0330
ref $rule eq "HASH" and $rule->{"action"} eq $prepared->{"action_matcher"} and ($rule->{"decision"} || "allow") eq $action
!ll&&!rl&&r
0033
ref $rule eq "HASH" and $rule->{"action"} eq $prepared->{"action_matcher"}
!ll&&!rl&&r
0033
$rule->{'decision'} || 'allow'
l!l
330
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
755 or die "Invalid persistent permission rule\n";
756  
757 die "Persistent permission sandbox-grants mismatch\n"
758 unless defined $rule->{"sandbox-grants"} && $rule->{"sandbox-grants"} eq
759 30 ▶ (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 30 30
$allow ? : 22 8 30
Condition33%1/3
defined $rule->{"sandbox-grants"} and $rule->{"sandbox-grants"} eq join(",", @{$allow ? $decision->{"sandbox_grants"} : $prepared->{"sandbox_grants"};})
!ll&&!rl&&r
0030
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
760  
761 30 ▶ for my $name (keys %{$prepared->{dimensions} || {}}) {
Condition50%1/2
$prepared->{'dimensions'} || {}
l!l
300
MC/DC0%0/1
$prepared->{'dimensions'} || {}
$prepared->{'dimensions'}
Truth table50%1/2
A
A= $prepared->{'dimensions'}
Aresult
11
00
762 30 my $key = "match.$name";
763 die "Persistent dimension scope was not offered\n"
764 unless defined $rule->{$key}
765 && grep $_->{match} eq $rule->{$key},
766 30 ▶ @{$prepared->{dimensions}{$name}{choices}};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rule->{$key} and grep(($_->{"match"} eq $rule->{$key}), @{$$prepared{"dimensions"}{$name}{"choices"};}) 0 30 30
Condition33%1/3
defined $rule->{$key} and grep(($_->{"match"} eq $rule->{$key}), @{$$prepared{"dimensions"}{$name}{"choices"};})
!ll&&!rl&&r
0030
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
767 }
768 30 ▶ if (exists $rule->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"path"}) 2 28 30
769 die "Invalid persistent denial path\n" unless !$allow
770 && ref $prepared->{paths} eq "ARRAY"
771 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
772 }
773 die "Persistent denial message mismatch\n"
774 unless (!$allow || !defined $decision->{message})
775 && (defined $rule->{message} ? $rule->{message} : "")
776 30 ▶ 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 30 30
defined $rule->{'message'} ? : 2 28 30
defined $decision->{'message'} ? : 2 28 30
Condition50%3/6
not $allow && defined $decision->{"message"} and (defined $rule->{"message"} ? $rule->{"message"} : "") eq (defined $decision->{"message"} ? $decision->{"message"} : "")
!ll&&!rl&&r
0030
$allow && defined $decision->{'message'}
!ll&&!rl&&r
8220
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
777  
778 30 my $rules = $self->rules;
779 30 push @$rules, { %$rule };
780 30 $self->_write ($rules);
781 }
782  
783 {
784 decision => $allow ? "allow" : "deny",
785 sandbox_grants => $granted,
786 89 ▶ (!$allow && defined $decision->{message} ? (message => $decision->{message}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
$allow ? : 63 26 89
!$allow && defined $decision->{'message'} ? : 2 87 89
Condition100%3/3
not $allow and defined $decision->{"message"}
!ll&&!rl&&r
63242
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
787 }
788 }
789  
790 237 ▶sub _call {
Subroutine _call: called CC=1 SCAR=0.0
791 237 my ($req) = @_;
792  
793 237 my %params = %{$req->{params}};
794 237 delete @params{qw(inputResponses requestState _meta)};
795 # _meta is per-request context, not stable operation identity: retries can
796 # carry different client capabilities, progress tokens and clientInfo.
797 # bind the continuation to its authenticated user as well as the call.
798 237 JSON::XS->new->canonical->encode ([$req->{user}, $req->{protocol_version}, $req->{method}, \%params])
799 }
800  
801 244 ▶sub _user_request {
Subroutine _user_request: called CC=4 SCAR=13.9
802 244 my ($req, $request) = @_;
803  
804 244 ▶ my $dimensions = $request->{dimensions} || {};
Condition100%2/2
$request->{'dimensions'} || {}
l!l
16480
MC/DC100%1/1
$request->{'dimensions'} || {}
$request->{'dimensions'}
Truth table100%2/2
A
A= $request->{'dimensions'}
Aresult
11
00
805 244 ▶ ref $dimensions eq "HASH" or die "Invalid permission dimensions\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $dimensions eq "HASH" or die "Invalid permission dimensions\n" 244 0 244
806 244 $dimensions = { %$dimensions };
807 244 delete $dimensions->{user};
808  
809 244 ▶ if (defined (my $user = $req->{user})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $user = $req->{"user"})) 1 243 244
810 $dimensions->{user} = {
811 1 label => "User",
812 value => $user,
813 choices => [
814 { label => "This user", match => $user },
815 { label => "All users", match => "^" },
816 ],
817 default => $user,
818 };
819 }
820  
821 244 +{ %$request, dimensions => $dimensions }
822 }
823  
824 23 ▶sub _remember {
Subroutine _remember: called CC=1 SCAR=0.0
825 23 my ($self, $req, $request, $grant) = @_;
826  
827 23 my $key = JSON::XS->new->canonical->encode ($request);
828 23 $req->{_permission_grants}{$key} = $grant;
829 }
830  
831 37 ▶sub denial_message {
Subroutine denial_message: called CC=3 SCAR=11.1
Pod undocumented
832 37 my ($grant) = @_;
833  
834 "Permission denied" . (defined $grant->{message} && length $grant->{message}
835 37 ▶ ? ": $grant->{message}" : "")
Branch: 1 branch
BranchTrueFalseTotal
defined $grant->{'message'} && length $grant->{'message'} ? : 3 34 37
Condition66%2/3
defined $grant->{"message"} and length $grant->{"message"}
!ll&&!rl&&r
3403
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
836 }
837  
838 59 ▶sub _challenge {
Subroutine _challenge: called CC=10 SCAR=25.7
839 59 my ($self, $req, $request, $prepared, $grants, $stage, $rounds, $selection) = @_;
840  
841 59 ▶ return $req->error ("Too many permission rounds") if $rounds > 32;
Branch: 1 branch
BranchTrueFalseTotal
if $rounds > 32 0 59 59
842  
843 59 AE::log debug => "permission elicitation: challenge created (stage $stage)";
844 59 my $state = "ae-mcp-permission:" . unpack "H*", AnyEvent::MCP::random_bytes 32;
845 $self->{pending}{$state} = {
846 call => $req->{_permission_call} || _call ($req),
847 request => $request,
848 prepared => $prepared,
849 59 ▶ grants => { %{$grants || {}} },
Condition85%6/7
$req->{'_permission_call'} || _call($req)
l!l&&r!l&&!r
43160
$grants || {}
l!l
4514
$selection || {}
l!l
752
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
850 stage => $stage,
851 rounds => $rounds,
852 selection => $selection || {},
853 };
854  
855 59 weaken $self;
856 0 ▶ $self->{pending}{$state}{timer} = AnyEvent->timer (after => $self->{timeout}, cb => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
857 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
858 0 AE::log debug => "permission elicitation: challenge expired (stage $stage)";
859 }
860 59 });
861  
862 $req->reply ({
863 resultType => "input_required",
864 requestState => $state,
865 inputRequests => {
866 permission => {
867 method => "elicitation/create",
868 params => {
869 mode => "form",
870 _meta => {
871 "anyevent-mcp/permission" => {
872 version => 1,
873 request => $prepared,
874 stage => $stage,
875 selection => $selection || {},
876 },
877 },
878 message => $stage eq "rule" ? "Configure the persistent permission rule.\n"
879 . (($selection->{decision} || "") eq "deny-always"
880 59 ▶ ? "Denial reason: " . ($selection->{message} // "(none)") . "\n" : "")
Branch: 2 branches
BranchTrueFalseTotal
$stage eq 'rule' ? : 6 53 59
($selection->{'decision'} || '') eq 'deny-always' ? : 2 4 6
Condition66%4/6
$selection || {}
l!l
752
$selection->{'decision'} || ''
l!l
60
$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
881 . AnyEvent::MCP::Permissions::Ask::message ($prepared, $stage)
882 : AnyEvent::MCP::Permissions::Ask::message ($prepared),
883 requestedSchema => AnyEvent::MCP::Permissions::Ask::schema ($prepared, $stage, $selection),
884 },
885 },
886 },
887 });
888 }
889  
890 50 ▶sub _next_stage {
Subroutine _next_stage: called CC=5 SCAR=16.1
891 50 my ($decision, $stage) = @_;
892  
893 50 ▶ return "rule" if $stage eq "choice" && $decision->{decision} =~ /-always\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $stage eq "choice" and $decision->{"decision"} =~ /-always\z/u 6 44 50
Condition100%3/3
$stage eq "choice" and $decision->{"decision"} =~ /-always\z/u
!ll&&!rl&&r
6386
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
894 44 ▶ return "choice" if $stage eq "rule" && $decision->{decision} eq "back";
Branch: 1 branch
BranchTrueFalseTotal
if $stage eq "rule" and $decision->{"decision"} eq "back" 1 43 44
Condition100%3/3
$stage eq "rule" and $decision->{"decision"} eq "back"
!ll&&!rl&&r
3851
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
895 undef
896 43 }
897  
898 211 ▶sub _resume {
Subroutine _resume: called CC=17 SCAR=28.9
899 211 my ($self, $req) = @_;
900  
901 211 ▶ return if $req->{_permission_resumed}++;
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"_permission_resumed"}++ 6 205 211
902  
903 205 $req->{_permission_call} = _call ($req);
904  
905 205 my $state = $req->{params}{requestState};
906 205 ▶ return unless defined $state && $state =~ /\Aae-mcp-permission:/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state and $state =~ /\Aae-mcp-permission:/u 163 42 205
Condition66%2/3
defined $state and $state =~ /\Aae-mcp-permission:/u
!ll&&!rl&&r
163042
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
907  
908 42 my $pending = delete $self->{pending}{$state};
909 42 ▶ AE::log debug => "permission elicitation: continuation has no pending challenge"
Branch: 1 branch
BranchTrueFalseTotal
unless $pending 3 39 42
910 unless $pending;
911 AE::log debug => "permission elicitation: continuation request mismatch"
912 42 ▶ if $pending && $pending->{call} ne $req->{_permission_call};
Branch: 1 branch
BranchTrueFalseTotal
if $pending and $pending->{"call"} ne $req->{"_permission_call"} 2 40 42
Condition100%3/3
$pending and $pending->{"call"} ne $req->{"_permission_call"}
!ll&&!rl&&r
3372
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
913 die "Invalid or expired permission continuation\n"
914 42 ▶ unless $pending && $pending->{call} eq $req->{_permission_call};
Branch: 1 branch
BranchTrueFalseTotal
unless $pending and $pending->{"call"} eq $req->{"_permission_call"} 5 37 42
Condition100%3/3
$pending and $pending->{"call"} eq $req->{"_permission_call"}
!ll&&!rl&&r
3237
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
915  
916 37 my $response = $req->{params}{inputResponses}{permission};
917 my $action = ref $response eq "HASH" && !ref $response->{action}
918 && ($response->{action} // "") =~ /\A(?:accept|decline|cancel)\z/
919 37 ▶ ? $response->{action} : "invalid";
Branch: 1 branch
BranchTrueFalseTotal
ref $response eq 'HASH' && !ref($response->{'action'}) && ($response->{'action'} // '') =~ /\A(?:accept|decline|cancel)\z/u ? : 37 0 37
Condition37%3/8
ref $response eq "HASH" and not ref $response->{"action"} and ($response->{"action"} // "") =~ /\A(?:accept|decline|cancel)\z/u
!ll&&!rl&&r
0037
ref $response eq "HASH" and not ref $response->{"action"}
!ll&&!rl&&r
0037
$response->{"action"} // ""
l!l
370
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
920 37 AE::log debug => "permission elicitation: continuation received (stage $pending->{stage}, action $action)";
921 my $decision = AnyEvent::MCP::Permissions::Ask::decision
922 37 $pending->{prepared}, $response, $pending->{stage}, $pending->{selection};
923 37 ▶ if (my $next = _next_stage ($decision, $pending->{stage})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = _next_stage($decision, $pending->{"stage"})) 5 32 37
924 $self->_challenge ($req, $pending->{request}, $pending->{prepared}, $pending->{grants},
925 5 ▶ $next, ($pending->{rounds} || 1) + 1, { %{$pending->{selection} || {}}, %$decision });
Condition50%2/4
$pending->{'rounds'} || 1
l!l
50
$pending->{'selection'} || {}
l!l
50
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
926 return
927 5 }
928 my $grant = $self->resolve ($pending->{request}, {
929 request => $pending->{prepared}, decision => $decision,
930 32 }, $req->{tool});
931  
932 32 ▶ die denial_message ($grant) . "\n" unless $grant->{decision} eq "allow";
Branch: 1 branch
BranchTrueFalseTotal
unless $grant->{"decision"} eq "allow" 15 17 32
933  
934 17 $req->{_permission_grants} = $pending->{grants};
935 17 $self->_remember ($req, $pending->{request}, $grant->{sandbox_grants});
936 }
937  
938 142 ▶sub require_permission {
Subroutine require_permission: called CC=4 SCAR=13.9
Pod documented
939 142 my ($self, $req, $request) = @_;
940  
941 142 $request = _user_request ($req, $request);
942 142 my $grant = $self->check ($request, $req->{tool});
943  
944 142 ▶ return $grant->{sandbox_grants} if $grant->{decision} eq "allow";
Branch: 1 branch
BranchTrueFalseTotal
if $grant->{"decision"} eq "allow" 55 87 142
945  
946 87 ▶ if ($grant->{decision} eq "deny") {
Branch: 1 branch
BranchTrueFalseTotal
if ($grant->{"decision"} eq "deny") 14 73 87
947 14 AE::log debug => "permission policy: denied";
948 14 die denial_message ($grant) . "\n";
949 }
950  
951 73 my $key = JSON::XS->new->canonical->encode ($request);
952  
953 return $req->{_permission_grants}{$key}
954 73 ▶ if exists $req->{_permission_grants}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $req->{"_permission_grants"}{$key} 25 48 73
955  
956 48 die bless { request => $request }, "AnyEvent::MCP::Server::Permissions::Required";
957 }
958  
959 100 ▶sub require {
Subroutine require: called CC=33 SCAR=35.7
Pod documented
960 100 my ($self, $req, $request, $done) = @_;
961  
962 100 $request = _user_request ($req, $request);
963 100 my $grant = eval { $self->check ($request, $req->{tool}) };
964  
965 100 ▶ return $done->(0, "$@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 100 100
966  
967 100 ▶ if ($req->{_permission_resumed} && $grant->{decision} eq "ask") {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{"_permission_resumed"} and $grant->{"decision"} eq "ask") 50 50 100
Condition66%2/3
$req->{"_permission_resumed"} and $grant->{"decision"} eq "ask"
!ll&&!rl&&r
50050
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
968 50 my $key = JSON::XS->new->canonical->encode ($request);
969 return $done->(1, $req->{_permission_grants}{$key})
970 50 ▶ if exists $req->{_permission_grants}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $req->{"_permission_grants"}{$key} 1 49 50
971 }
972  
973 99 my $state = $req->{params}{requestState};
974  
975 99 ▶ 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 80 99
Condition83%5/6
not $req->{"_permission_resumed"} and defined $state and $state =~ /\Aae-mcp-permission:/u
!ll&&!rl&&r
80019
not $req->{"_permission_resumed"} and defined $state
!ll&&!rl&&r
493119
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
976 19 my $pending = delete $self->{pending}{$state};
977 return $done->(0, "Invalid or expired permission continuation")
978 unless $pending && $pending->{call} eq _call ($req)
979 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
980 13 my $response = $req->{params}{inputResponses}{permission};
981 13 my $decision = eval {
982 AnyEvent::MCP::Permissions::Ask::decision
983 $pending->{prepared}, $response, $pending->{stage}, $pending->{selection}
984 13 };
985 13 ▶ return $done->(0, "$@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 13 13
986 13 ▶ if (my $next = _next_stage ($decision, $pending->{stage})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = _next_stage($decision, $pending->{"stage"})) 2 11 13
987 $self->_challenge ($req, $pending->{request}, $pending->{prepared}, $pending->{grants},
988 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
989 return
990 2 }
991 11 $grant = eval { $self->resolve ($request, { request => $pending->{prepared}, decision => $decision }, $req->{tool}) };
992 11 ▶ return $done->(0, "$@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 11 11
993 }
994  
995 91 ▶ return $done->(1, $grant->{sandbox_grants}) if $grant->{decision} eq "allow";
Branch: 1 branch
BranchTrueFalseTotal
if $grant->{"decision"} eq "allow" 11 80 91
996 80 ▶ if ($grant->{decision} eq "deny") {
Branch: 1 branch
BranchTrueFalseTotal
if ($grant->{"decision"} eq "deny") 6 74 80
997 6 AE::log debug => "permission policy: denied";
998 6 return $done->(0, denial_message ($grant));
999 }
1000  
1001 74 my $prepared = $grant->{request};
1002 74 my $ask = $self->{ask};
1003 74 ▶ return $done->(0, "Permission denied") if !ref $ask && $ask eq "none";
Branch: 1 branch
BranchTrueFalseTotal
if not ref $ask and $ask eq "none" 3 71 74
Condition100%3/3
not ref $ask and $ask eq "none"
!ll&&!rl&&r
11603
MC/DC100%2/2
not ref $ask and $ask eq "none"
not ref $ask $ask eq "none"
Truth table100%3/3
not(A) and B
A= not ref $askB= $ask eq "none"
ABresult
0X0
100
111
1004  
1005 71 my $client_caps = $req->{params}{_meta}{"io.modelcontextprotocol/clientCapabilities"};
1006 my $mcp = $req->{protocol_version} ge "2026" && ref $client_caps eq "HASH"
1007 && ref $client_caps->{elicitation} eq "HASH"
1008 71 ▶ && (!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
18152
$req->{"protocol_version"} ge 2026 and ref $client_caps eq "HASH" and ref $client_caps->{"elicitation"} eq "HASH"
!ll&&!rl&&r
13553
$req->{"protocol_version"} ge 2026 and ref $client_caps eq "HASH"
!ll&&!rl&&r
13058
not keys %{$$client_caps{"elicitation"};} or ref $client_caps->{"elicitation"}{"form"} eq "HASH"
l!l&&r!l&&!r
2501
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
1009  
1010 71 ▶ 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) 53 18 71
Condition77%7/9
not ref $ask and $ask eq "mcp" || $ask eq "auto" && $mcp
!ll&&!rl&&r
11753
$ask eq "mcp" or $ask eq "auto" and $mcp
l!l&&r!l&&!r
5307
$ask eq "auto" and $mcp
!ll&&!rl&&r
250
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
1011 53 ▶ return $done->(0, "MCP form elicitation is unavailable") unless $mcp;
Branch: 1 branch
BranchTrueFalseTotal
unless $mcp 1 52 53
1012  
1013 $self->_challenge ($req, $request, $prepared, $req->{_permission_grants},
1014 52 "choice", 1);
1015 return
1016 52 }
1017  
1018 18 weaken $self;
1019  
1020 18 my $finished;
1021 17 ▶ my $finish = sub {
Subroutine __ANON__: called CC=7 SCAR=19.9
1022 17 my ($success, $decision) = @_;
1023  
1024 17 ▶ return if !$self || $req->{finished};
Branch: 1 branch
BranchTrueFalseTotal
if not $self or $req->{"finished"} 1 16 17
Condition66%2/3
not $self or $req->{"finished"}
l!l&&r!l&&!r
0116
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
1025 16 ▶ die "Permission prompt already finished\n" if $finished++;
Branch: 1 branch
BranchTrueFalseTotal
if $finished++ 0 16 16
1026 16 ▶ return $done->(0, $decision) unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 16 16
1027  
1028 16 my $grant = eval { $self->resolve ($request, { request => $prepared, decision => $decision }, $req->{tool}) };
1029  
1030 16 ▶ return $done->(0, "$@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 16 16
1031  
1032 $done->($grant->{decision} eq "allow" ? (1, $grant->{sandbox_grants})
1033 16 ▶ : (0, denial_message ($grant)));
Branch: 1 branch
BranchTrueFalseTotal
$grant->{'decision'} eq 'allow' ? : 14 2 16
1034 18 };
1035  
1036 18 ▶ if (ref $ask eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ask eq 'CODE') { } 11 7 18
1037 11 $ask->($prepared, $finish);
1038 } else {
1039 7 ▶ 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 7 7
1040  
1041 my $job = AnyEvent::MCP::Permissions::Ask->new (
1042 helper => $ask eq "tty" || $ask eq "yad"
1043 ? ["anyevent-mcp-askpermission", "--$ask"] : $self->{helper},
1044 timeout => $self->{timeout},
1045 7 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$ask eq 'tty' || $ask eq 'yad' ? : 2 5 7
Condition100%3/3
$ask eq "tty" or $ask eq "yad"
l!l&&r!l&&!r
115
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
1046  
1047 7 my $old_cancel = $req->{on_cancel};
1048 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
1049 7 $job->ask ($prepared, $finish);
1050 }
1051 }
1052  
1053 53 1
« blib/lib/AnyEvent/MCP/Server.pm ↑ Summary blib/lib/AnyEvent/MCP.pm »