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

filter: Statementstmt 98.6% 361 / 366 Branchbran 84.2% 236 / 280 Conditioncond 64.9% 239 / 368 MC/DCmcdc 45.7% 86 / 188 Subroutinesub 93.7% 30 / 32 Podpod 90.0% 9 / 10 totaltotal 77.2% 961 / 1244 CC 286 SCAR 41.2
CC
286
Cov
82%
check
150.7
resolve
65.0
_rule
41.4
CRAP 61.9
« bin/anyevent-mcp-form-elicitation ↑ Summary blib/lib/AnyEvent/MCP/Server.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 An optional C<dimension_order> array lists dimension names in the preferred
281 display order; unlisted dimensions follow alphabetically. This affects only
282 the prompts, not rule matching or stored matcher names.
283 Matchers are literal unless beginning with C<^>, which denotes a Perl regex.
284 The caller must supply trustworthy dimension values, not untrusted tool arguments.
285 The former C<subject>, C<subjects>, C<origin>, C<origins>, and C<workdir>
286 request and saved-rule fields are unsupported.
287 The multibroker alias, when supplied by the child's environment, is display-only
288 and does not change rule matching or policy file selection.
289 C<paths> may contain a list of affected filenames for display in permission
290 prompts and for optional B<deny-only> exact-file rules. Such matching does
291 not restrict sandbox authority and is not a substitute for sandboxing.
292 Absent a C<path> matcher, the permission rule is not limited by paths.
293  
294 When prompting for a new rule, each scope defaults to the latest saved
295 matcher for that dimension if it is still among the offered choices.
296 Otherwise the application's first (usually narrowest) choice is selected.
297 Application-specified C<default> values take precedence. This only affects
298 the rule editor; it never grants permission by itself.
299  
300 Returns C<{decision =E<gt> "allow", sandbox_grants =E<gt> [...]}>,
301 C<{decision =E<gt> "deny", sandbox_grants =E<gt> []}>, or
302 C<{decision =E<gt> "ask", request =E<gt> $prepared}>.
303 Denials can also include a saved C<message>. C<check> does not prompt or
304 execute the operation.
305  
306 =item $grant = $permissions->resolve ($request, $approval)
307  
308 C<$request> is the original permission request. C<$approval> is
309 C<{ request =E<gt> $prepared, decision =E<gt> $decision }>, where
310 C<$prepared> is the request returned by C<check> and C<$decision> is the
311 helper's decision hash. Resolves the answer to that prepared request. Approval decisions are
312 C<allow-once>, C<allow-always>, C<deny-once>, and C<deny-always>.
313 Allow-once uses exactly the requested sandbox grants. Allow-always may save
314 a broader or narrower set of grants for future invocations; the present
315 invocation still receives no more than requested. Permanent decisions
316 contain a validated rule restricted to the offered action and dimension scopes.
317 Denial decisions may include an optional human C<message>; allowed tool
318 results are never modified by permission prompting.
319  
320 This is a trusted application interface. Do not pass unauthenticated client
321 metadata directly to it.
322  
323 =item $grant = $permissions->require_permission ($req, { %request })
324  
325 Synchronous interface used by C<< $req->require_permission >>. Returns
326 the granted sandbox-grant array, dies on denial, or throws a permission-needed
327 exception for the server to handle. The worker must enforce the returned grants;
328 never treat merely requested grants as sufficient.
329  
330 =item $permissions->require ($req, $request, $done->($success, $grant))
331  
332 Asynchronous interface. C<$grant> is the allowed sandbox-grant array or an
333 error. Check before starting side effects.
334  
335 With a local prompt, C<$done> runs when permission is resolved. Modern
336 MCP prompting instead completes C<$req> with C<input_required> without
337 calling C<$done>; the client repeats the call with input. Original
338 callbacks are not retained.
339 Continuations are short-lived and bound to the method, parameters, and request,
340 and are consumed once. Approval retries retain earlier one-time grants
341 within the same call. Always decisions and Back use additional protected C<input_required>
342 rounds, capped at 32, without executing the original tool. Messages in
343 persistent deny rules are returned to subsequent callers, including later
344 sessions. Client-side elicitation is only appropriate when the connected
345 client is the trusted approval authority.
346  
347 =item $closure = $permissions->closure ([$grant...])
348  
349 Returns a hash reference whose keys contain the supplied sandbox grants
350 and all grants they imply. Unknown grants or malformed implication lists
351 die. This expands authority names; it does not grant permission.
352  
353 =item $permissions->auto_rules ($rules)
354  
355 Replace the ordered process-local rule chain. Parsing and validation
356 occur once, at installation. An empty list disables automatic rules.
357 With an optional true second argument, prepend the new rules before existing
358 process-local rules instead of replacing them.
359  
360 =item $rules = $permissions->rules
361  
362 Returns the ordered, uncompiled in-memory or file-backed rule array reference.
363 File-backed rules are reread, so changes apply to subsequent checks.
364  
365 =item $permissions->save ($rules)
366  
367 Replaces the ordered rules without semantic validation, using atomic file
368 replacement if backed by a file.
369  
370 =back
371  
372 =cut
373  
374 package AnyEvent::MCP::Server::Permissions;
375  
376 53 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
377 53 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
378 53 ▶use AnyEvent::MCP::Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
379 53 ▶use AnyEvent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
380 53 ▶use AnyEvent::MCP::Permissions::Ask ();
Subroutine BEGIN: called CC=1 SCAR=0.0
381 53 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
382 53 ▶use Errno qw(ENOENT);
Subroutine BEGIN: called CC=1 SCAR=0.0
383 53 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
384  
385 53 our @ISA = qw(AnyEvent::MCP::Base);
386  
387 128 ▶sub new {
Subroutine new: called CC=13 SCAR=25.7
Pod documented
388 128 my ($class, %kv) = @_;
389  
390 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
391 128 ▶ my $ask = exists $kv{ask} ? $kv{ask} : $ENV{PERL_ANYEVENT_MCP_PERMISSION_ASK};
Branch: 1 branch
BranchTrueFalseTotal
exists $kv{'ask'} ? : 28 100 128
392 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
393 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
394 unless ref $ask eq "CODE" || !ref $ask && $ask =~ /\A(?:helper|tty|yad|auto|mcp|none)\z/;
395  
396 127 my $self = $class->SUPER::new (sandbox_grants => {}, actions => {}, rules => [], ask => $ask, timeout => 300, %kv);
397  
398 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
399 38 my $name = AnyEvent::MCP::utf8enc $kv{name};
400 38 $name =~ s/([^A-Za-z0-9_.-])/sprintf "%%%02X", ord $1/ge;
401 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
402 38 $self->{file} = "$dir/anyevent-mcp/rules/$name.ini";
403 }
404  
405 127 $self->closure ([keys %{$self->{sandbox_grants}}]);
406 127 $self->_rule ($_) for @{$self->rules};
407 127 $self->auto_rules ($auto_rules);
408 127 $self
409 }
410  
411 149 ▶sub auto_rules {
Subroutine auto_rules: called CC=17 SCAR=30.9
Pod documented
412 149 my ($self, $rules, $prepend) = @_;
413  
414 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
415 149 my @compiled;
416 149 for my $entry (@$rules) {
417 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
418 && defined $entry->[0] && !ref $entry->[0] && $entry->[0] =~ /\A(?:allow|deny)\z/
419 && defined $entry->[1] && !ref $entry->[1]
420 or die "Invalid automatic permission rule\n";
421 40 my @parts = split /,/, $entry->[1], -1;
422 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
423 or die "Invalid automatic permission rule '$entry->[1]'\n";
424 34 my ($tool, $action, $capability) = (@parts, ("*") x (3 - @parts));
425 34 $tool =~ y/_/-/;
426 34 ▶ push @compiled, {
Branch: 1 branch
BranchTrueFalseTotal
$capability eq '*' ? : 25 9 34
427 decision => $entry->[0],
428 tool => $tool,
429 action => $action,
430 capability => $capability,
431 closure => $capability eq "*" ? undef : $self->closure ([$capability]),
432 };
433 }
434 142 ▶ push @compiled, @{$self->{auto_rules}} if $prepend;
Branch: 1 branch
BranchTrueFalseTotal
if $prepend 9 133 142
435 142 $self->{auto_rules} = \@compiled;
436 }
437  
438 1540 ▶sub closure {
Subroutine closure: called CC=7 SCAR=19.7
Pod documented
439 1540 my ($self, $sandbox_grants) = @_;
440  
441 1540 ▶ ref $sandbox_grants eq "ARRAY"
Branch: 1 branch
BranchTrueFalseTotal
ref $sandbox_grants eq "ARRAY" or die "Sandbox grants must be an array\n" 1540 0 1540
442 or die "Sandbox grants must be an array\n";
443  
444 1540 my %set;
445  
446 1540 my @todo = @$sandbox_grants;
447  
448 1540 ▶ while (@todo) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo) 2441 1535 3976
449 2441 my $capability = pop @todo;
450  
451 defined $capability && !ref $capability
452 2441 ▶ && 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" 2436 5 2441
Condition50%3/6
defined $capability and not ref $capability and ref $self->{"sandbox_grants"}{$capability} eq "ARRAY"
!ll&&!rl&&r
052436
defined $capability and not ref $capability
!ll&&!rl&&r
002441
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
453 or die "Unknown sandbox grant\n";
454  
455 2436 ▶ next if $set{$capability}++;
Branch: 1 branch
BranchTrueFalseTotal
if $set{$capability}++ 474 1962 2436
456 1962 push @todo, @{$self->{sandbox_grants}{$capability}};
457 }
458  
459 1535 \%set
460 }
461  
462 2011 ▶sub _regex {
Subroutine _regex: called CC=4 SCAR=14.9
463 2011 my ($pattern) = @_;
464  
465 2011 ▶ defined $pattern && !ref $pattern
Branch: 1 branch
BranchTrueFalseTotal
defined $pattern and not ref $pattern or die "Permission matcher must be a string\n" 2011 0 2011
Condition33%1/3
defined $pattern and not ref $pattern
!ll&&!rl&&r
002011
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
466 or die "Permission matcher must be a string\n";
467  
468 2011 ▶ $pattern =~ /\A\^/ ? qr/$pattern/ : qr/\A\Q$pattern\E\z/
Branch: 1 branch
BranchTrueFalseTotal
$pattern =~ /\A\^/u ? : 645 1366 2011
469 }
470  
471 802 ▶sub _rule {
Subroutine _rule: called CC=27 SCAR=37.2
472 802 my ($self, $rule) = @_;
473  
474 802 ▶ ref $rule eq "HASH"
Branch: 1 branch
BranchTrueFalseTotal
ref $rule eq "HASH" or die "Invalid permission rule\n" 802 0 802
475 or die "Invalid permission rule\n";
476 802 ▶ !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 796 802
477 or die "Unknown permission rule field\n";
478  
479 die "Invalid permission rule expiry\n" if exists $rule->{expires}
480 796 ▶ && (!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 784 796
Condition66%6/9
exists $rule->{"expires"} and !defined($rule->{"expires"}) || ref $rule->{"expires"} || !($rule->{"expires"} =~ /\A[0-9]+\z/u)
!ll&&!rl&&r
7632112
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
481  
482 784 my $decision = $rule->{decision};
483  
484 784 ▶ 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 781 784
Condition66%2/3
defined $decision and $decision =~ /\A(?:allow|deny|ask)\z/u
!ll&&!rl&&r
30781
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
485 die "Only deny rules can have paths or messages\n"
486 781 ▶ 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 781 781
Condition50%3/6
$decision ne "deny" and exists $rule->{"path"} || exists $rule->{"message"}
!ll&&!rl&&r
1796020
exists $rule->{"path"} or exists $rule->{"message"}
l!l&&r!l&&!r
00602
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
487 781 ▶ 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 781 781
Condition66%2/3
$decision eq "allow" and not exists $rule->{"sandbox-grants"}
!ll&&!rl&&r
1865950
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
488  
489 defined $rule->{action}
490 781 ▶ or die "Permission rule needs action\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $rule->{"action"} or die "Permission rule needs action\n" 781 0 781
491  
492 781 my $compiled = { decision => $decision, action => _regex ($rule->{action}) };
493 781 ▶ if (exists $rule->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"path"}) 15 766 781
494 die "Invalid exact denial path\n" if !defined $rule->{path}
495 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
496 15 $compiled->{path} = $rule->{path};
497 }
498 781 my $message = $rule->{message};
499 781 ▶ if (defined $message) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message) 15 766 781
500 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
501 || length $message > 2048 || $message =~ /[\r\n]/;
502 15 $compiled->{message} = $message;
503 }
504  
505 781 for my $key (grep /^match\./, keys %$rule) {
506 359 (my $name = $key) =~ s/^match\.//;
507 359 $compiled->{matches}{$name} = _regex ($rule->{$key});
508 }
509  
510 778 ▶ if (exists $rule->{"sandbox-grants"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"sandbox-grants"}) 684 94 778
511 684 ▶ die "Invalid rule sandbox-grants\n" if ref $rule->{"sandbox-grants"};
Branch: 1 branch
BranchTrueFalseTotal
if ref $rule->{"sandbox-grants"} 0 684 684
512 $compiled->{sandbox_grants} = $self->closure ([length $rule->{"sandbox-grants"}
513 684 ▶ ? split /,/, $rule->{"sandbox-grants"}, -1 : ()]);
Branch: 1 branch
BranchTrueFalseTotal
length $rule->{'sandbox-grants'} ? : 260 424 684
514 }
515  
516 $compiled
517 775 }
518  
519 690 ▶sub rules {
Subroutine rules: called CC=6 SCAR=17.9
Pod documented
520 690 my ($self) = @_;
521  
522 690 my $now = time;
523  
524 [grep {
525 die "Invalid permission rule expiry\n" if exists $_->{expires}
526 457 ▶ && (!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 451 457
Condition100%9/9
exists $_->{"expires"} and !defined($_->{"expires"}) || ref $_->{"expires"} || !($_->{"expires"} =~ /\A[0-9]+\z/u)
!ll&&!rl&&r
425266
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
527 451 ▶ !exists $_->{expires} || $_->{expires} > $now
Condition100%3/3
!exists $_->{'expires'} || $_->{'expires'} > $now
l!l&&r!l&&!r
4251214
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
528 690 } @{$self->_read_rules}]
529 }
530  
531 690 ▶sub _read_rules {
Subroutine _read_rules: called CC=11 SCAR=24.2
532 690 my ($self) = @_;
533  
534 690 ▶ return $self->{rules} unless defined $self->{file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"file"} 349 341 690
535  
536 341 ▶ 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] } 263 78 341
537 78 ▶ return [] if $! == ENOENT;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 2 78 0 78
538 0 die "$self->{file}: $!\n";
539 };
540  
541 263 my @rules;
542 263 my $valid = eval {
543 263 for (@{AnyEvent::MCP::Config::parse_ini (AnyEvent::MCP::file_load ($self->{file}, $fh), $self->{file})}) {
544 673 my ($name, $kv) = @$_;
545 673 ▶ if ($name eq "") {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq '') { } 260 413 673
elsif ($name eq 'rule') { } 410 3 413
546 260 ▶ %$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 260 260
547 } elsif ($name eq "rule") {
548 410 push @rules, $kv;
549 } else {
550 3 die "$self->{file}: invalid permission rule section '$name'\n";
551 }
552 }
553 257 $self->_rule ($_) for @rules;
554 236 1
555 };
556 263 ▶ close $fh or die "$self->{file}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die qq[$self->{"file"}: $!\n] 263 0 263
557  
558 263 ▶ $valid ? \@rules : []
Branch: 1 branch
BranchTrueFalseTotal
$valid ? : 236 27 263
559 }
560  
561 96 ▶sub _write {
Subroutine _write: called CC=2 SCAR=6.9
562 96 my ($self, $rules) = @_;
563  
564 96 ▶ unless (defined $self->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"file"}) 38 58 96
565 38 $self->{rules} = $rules;
566 return
567 38 }
568  
569 AnyEvent::MCP::file_save $self->{file},
570 58 AnyEvent::MCP::Config::serialize_ini [map ["rule", $_], @$rules];
571 }
572  
573 165 ▶sub _lock {
Subroutine _lock: called CC=2 SCAR=6.9
574 165 my ($self) = @_;
575  
576 165 ▶ defined $self->{file} ? AnyEvent::MCP::file_lock ($self->{file}) : undef
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'file'} ? : 75 90 165
577 }
578  
579 66 ▶sub save {
Subroutine save: called CC=1 SCAR=0.0
Pod documented
580 66 my ($self, $rules) = @_;
581  
582 66 my $lock = $self->_lock;
583 66 $self->_write ($rules);
584 }
585  
586 556 ▶sub check {
Subroutine check: called CC=88 SCAR=50.2
Pod documented
587 556 my ($self, $request, $tool) = @_;
588  
589 556 ▶ my $need = $self->closure ($request->{sandbox_grants} // []);
Condition50%1/2
$request->{"sandbox_grants"} // []
l!l
5560
MC/DC0%0/1
$request->{"sandbox_grants"} // []
$request->{"sandbox_grants"}
Truth table50%1/2
A
A= $request->{"sandbox_grants"}
Aresult
11
00
590 die "Permission action must be a string\n" unless defined $request->{action}
591 555 ▶ && !ref $request->{action} && length $request->{action};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $request->{"action"} and not ref $request->{"action"} and length $request->{"action"} 1 554 555
Condition50%3/6
defined $request->{"action"} and not ref $request->{"action"} and length $request->{"action"}
!ll&&!rl&&r
01554
defined $request->{"action"} and not ref $request->{"action"}
!ll&&!rl&&r
00555
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
592 554 die "Unknown permission action\n" if keys %{$self->{actions}}
593 554 ▶ && !exists $self->{actions}{$request->{action}};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"actions"};} and not exists $self->{"actions"}{$request->{"action"}} 0 554 554
Condition66%2/3
keys %{$$self{"actions"};} and not exists $self->{"actions"}{$request->{"action"}}
!ll&&!rl&&r
4341200
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
594 die "Obsolete permission request scope\n"
595 554 ▶ 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 553 554
596 553 ▶ my $dimensions = exists $request->{dimensions} ? $request->{dimensions} : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $request->{'dimensions'} ? : 510 43 553
597 553 ▶ ref $dimensions eq "HASH" or die "Invalid permission dimensions\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $dimensions eq "HASH" or die "Invalid permission dimensions\n" 553 0 553
598 553 ▶ if (exists $request->{dimension_order}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $request->{"dimension_order"}) 15 538 553
599 15 my $order = $request->{dimension_order};
600 15 ▶ ref $order eq "ARRAY" or die "Invalid permission dimension order\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $order eq "ARRAY" or die "Invalid permission dimension order\n" 12 3 15
601 12 my %seen;
602 12 for my $name (@$order) {
603 18 ▶ defined $name && !ref $name && exists $dimensions->{$name} && !$seen{$name}++
Branch: 1 branch
BranchTrueFalseTotal
defined $name and not ref $name and exists $dimensions->{$name} and not $seen{$name}++ or die "Invalid permission dimension order\n" 9 9 18
Condition88%8/9
defined $name and not ref $name and exists $dimensions->{$name} and not $seen{$name}++
!ll&&!rl&&r
639
defined $name and not ref $name and exists $dimensions->{$name}
!ll&&!rl&&r
3312
defined $name and not ref $name
!ll&&!rl&&r
0315
MC/DC75%3/4
defined $name and not ref $name and exists $dimensions->{$name} and not $seen{$name}++
defined $name not ref $name exists $dimensions->{$name} not $seen{$name}++
Truth table80%4/5
A and not(B) and C and not(D)
A= defined $nameB= not ref $nameC= exists $dimensions->{$name}D= not $seen{$name}++
ABCDresult
0XXX0
10XX0
110X0
11100
11111
604 or die "Invalid permission dimension order\n";
605 }
606 }
607 541 for my $name (keys %$dimensions) {
608 437 ▶ $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" 437 0 437
609 437 my $dim = $dimensions->{$name};
610 ref $dim eq "HASH" && defined $dim->{value} && !ref $dim->{value} && length $dim->{value}
611 && defined $dim->{label} && !ref $dim->{label} && length $dim->{label}
612 437 ▶ && 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" 437 0 437
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
00437
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
00437
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
00437
ref $dim eq "HASH" and defined $dim->{"value"} and not ref $dim->{"value"} and length $dim->{"value"} and defined $dim->{"label"}
!ll&&!rl&&r
00437
ref $dim eq "HASH" and defined $dim->{"value"} and not ref $dim->{"value"} and length $dim->{"value"}
!ll&&!rl&&r
00437
ref $dim eq "HASH" and defined $dim->{"value"} and not ref $dim->{"value"}
!ll&&!rl&&r
00437
ref $dim eq "HASH" and defined $dim->{"value"}
!ll&&!rl&&r
00437
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
613 or die "Invalid permission dimension '$name'\n";
614 437 for my $choice (@{$dim->{choices}}) {
615 ref $choice eq "HASH" && defined $choice->{label} && !ref $choice->{label}
616 && length $choice->{label} && defined $choice->{match}
617 && $dim->{value} =~ _regex ($choice->{match})
618 869 ▶ 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" 865 4 869
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
04865
ref $choice eq "HASH" and defined $choice->{"label"} and not ref $choice->{"label"} and length $choice->{"label"} and defined $choice->{"match"}
!ll&&!rl&&r
00869
ref $choice eq "HASH" and defined $choice->{"label"} and not ref $choice->{"label"} and length $choice->{"label"}
!ll&&!rl&&r
00869
ref $choice eq "HASH" and defined $choice->{"label"} and not ref $choice->{"label"}
!ll&&!rl&&r
00869
ref $choice eq "HASH" and defined $choice->{"label"}
!ll&&!rl&&r
00869
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
619 }
620 433 ▶ if (exists $dim->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dim->{"default"}) 16 417 433
621 defined $dim->{default} && !ref $dim->{default}
622 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
623 or die "Permission dimension default was not offered\n";
624 }
625 }
626  
627 525 ▶ if (exists $request->{details}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $request->{"details"}) 7 518 525
628 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
629 or die "Permission details must be a hash\n";
630 defined && !ref
631 or die "Permission detail must be text\n"
632 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
633 }
634  
635 # use the dispatched tool name, never the request's display label.
636 525 ▶ my $tool_name = $tool // "";
Condition100%2/2
$tool // ""
l!l
331194
MC/DC100%1/1
$tool // ""
$tool
Truth table100%2/2
A
A= $tool
Aresult
11
00
637 525 $tool_name =~ y/_/-/;
638 525 for my $rule (@{$self->{auto_rules}}) {
639 91 ▶ 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 77 91
Condition100%3/3
$rule->{"tool"} ne "*" and $rule->{"tool"} ne $tool_name
!ll&&!rl&&r
522514
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
640 77 ▶ 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 49 77
Condition100%3/3
$rule->{"action"} ne "*" and $rule->{"action"} ne $request->{"action"}
!ll&&!rl&&r
341528
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
641 49 ▶ if ($rule->{capability} ne "*") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"capability"} ne "*") 11 38 49
642 11 ▶ if ($rule->{decision} eq "allow") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{'decision'} eq 'allow') { } 7 4 11
643 7 ▶ next if grep !$rule->{closure}{$_}, keys %$need;
Branch: 1 branch
BranchTrueFalseTotal
if grep((!$rule->{"closure"}{$_}), keys %$need) 3 4 7
644 } else {
645 4 ▶ next unless $need->{$rule->{capability}};
Branch: 1 branch
BranchTrueFalseTotal
unless $$need{$rule->{"capability"}} 1 3 4
646 }
647 }
648 return {
649 decision => $rule->{decision},
650 45 ▶ sandbox_grants => $rule->{decision} eq "allow" ? [@{$request->{sandbox_grants} || []}] : [],
Branch: 1 branch
BranchTrueFalseTotal
$rule->{'decision'} eq 'allow' ? : 35 10 45
Condition50%1/2
$request->{'sandbox_grants'} || []
l!l
350
MC/DC0%0/1
$request->{'sandbox_grants'} || []
$request->{'sandbox_grants'}
Truth table50%1/2
A
A= $request->{'sandbox_grants'}
Aresult
11
00
651 };
652 }
653  
654 480 my $rules = $self->rules;
655 480 my @rules = map { $self->_rule ($_) } @$rules;
656 474 for my $rule (@rules) {
657 328 ▶ next unless $request->{action} =~ $rule->{action};
Branch: 1 branch
BranchTrueFalseTotal
unless $request->{"action"} =~ /$rule->{"action"}/u 148 180 328
658 next if grep {
659 129 ▶ !exists $dimensions->{$_} || $dimensions->{$_}{value} !~ $rule->{matches}{$_}
Condition100%3/3
exists $dimensions->{$_} && $dimensions->{$_}{'value'} =~ /$rule->{'matches'}{$_}/u
!ll&&!rl&&r
16068
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
660 180 ▶ } keys %{$rule->{matches} || {}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {not exists $dimensions->{$_} && $dimensions->{$_}{"value"} =~ /$rule->{"matches"}{$_}/u;} keys %{$rule->{"matches"} or {};} 61 119 180
Condition100%2/2
$rule->{'matches'} || {}
l!l
10278
MC/DC100%1/1
$rule->{'matches'} || {}
$rule->{'matches'}
Truth table100%2/2
A
A= $rule->{'matches'}
Aresult
11
00
661  
662 next if exists $rule->{path} && !(ref $request->{paths} eq "ARRAY"
663 119 ▶ && 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 116 119
Condition66%8/12
exists $rule->{"path"} and not ref $request->{"paths"} eq "ARRAY" && grep((defined $_ && !ref($_) && $_ eq $rule->{"path"}), @{$$request{"paths"};})
!ll&&!rl&&r
11243
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
664 # an exact-file denial applies even when a multi-path operation requests
665 # additional sandbox authority for its other paths.
666 next if !exists $rule->{path} && exists $rule->{sandbox_grants}
667 && ($rule->{decision} eq "deny"
668 4 ? (grep !$need->{$_}, keys %{$rule->{sandbox_grants}})
669 116 ▶ : (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 115 116
$rule->{'decision'} eq 'deny' ? : 4 77 81
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
35801
not exists $rule->{"path"} and exists $rule->{"sandbox_grants"}
!ll&&!rl&&r
43181
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
670  
671 return {
672 decision => $rule->{decision},
673 76 ▶ sandbox_grants => $rule->{decision} eq "allow" ? [@{$request->{sandbox_grants} || []}] : [],
Branch: 1 branch
BranchTrueFalseTotal
$rule->{'decision'} eq 'deny' && defined $rule->{'message'} ? : 4 108 112
Condition80%4/5
$request->{'sandbox_grants'} || []
l!l
760
$rule->{"decision"} eq "deny" and defined $rule->{"message"}
!ll&&!rl&&r
76324
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
674 ($rule->{decision} eq "deny" && defined $rule->{message} ? (message => $rule->{message}) : ()),
675 115 ▶ } unless $rule->{decision} eq "ask";
Branch: 2 branches
BranchTrueFalseTotal
unless $rule->{"decision"} eq "ask" 112 3 115
$rule->{'decision'} eq 'allow' ? : 76 36 112
676  
677 last
678 3 }
679  
680 362 ▶ 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 362 362
Condition33%1/3
not ref $request->{"reason"} and $request->{"reason"} =~ /\S/u
!ll&&!rl&&r
00362
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
681  
682 362 my $action_class = $request->{action};
683 362 ▶ $action_class = "^\Q$action_class\E\\z" if $action_class =~ /\A\^/;
Branch: 1 branch
BranchTrueFalseTotal
if $action_class =~ /\A\^/u 0 362 362
684  
685 362 my $descriptions = $self->{sandbox_grant_descriptions};
686 362 ▶ $descriptions = $descriptions->($request) if ref $descriptions eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
if ref $descriptions eq "CODE" 0 362 362
687 362 ▶ die "Invalid sandbox grant descriptions\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $descriptions and ref $descriptions ne "HASH" 0 362 362
Condition33%1/3
defined $descriptions and ref $descriptions ne "HASH"
!ll&&!rl&&r
36200
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
688 if defined $descriptions && ref $descriptions ne "HASH";
689  
690 # Reuse the last saved choice for each dimension if it is still offered.
691 # This changes only the form default, not the authorization check.
692 362 my %prepared_dimensions = %$dimensions;
693 362 for my $name (keys %$dimensions) {
694 278 my $dim = $dimensions->{$name};
695 278 ▶ next if exists $dim->{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $dim->{"default"} 4 274 278
696 my ($selected) = map $_->{"match.$name"},
697 274 grep exists $_->{"match.$name"}, reverse @$rules;
698 $prepared_dimensions{$name} = { %$dim, default => $selected }
699 274 ▶ if defined $selected && grep $_->{match} eq $selected, @{$dim->{choices}};
Branch: 1 branch
BranchTrueFalseTotal
if defined $selected and grep(($_->{"match"} eq $selected), @{$$dim{"choices"};}) 40 234 274
Condition100%3/3
defined $selected and grep(($_->{"match"} eq $selected), @{$$dim{"choices"};})
!ll&&!rl&&r
1815340
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
700 }
701  
702 {
703 decision => "ask",
704 request => {
705 %$request,
706 dimensions => \%prepared_dimensions,
707 sandbox_grants => $request->{sandbox_grants} || [],
708 version => 1,
709 name => $self->{name},
710 (length ($ENV{PERL_ANYEVENT_MCP_MULTIBROKER_ALIAS} // "")
711 ? (multibroker_alias => $ENV{PERL_ANYEVENT_MCP_MULTIBROKER_ALIAS}) : ()),
712 action_matcher => $action_class,
713 362 ▶ sandbox_grant_choices => [sort keys %{$self->{sandbox_grants}}],
Condition50%1/2
$descriptions || {}
l!l
0362
MC/DC0%0/1
$descriptions || {}
$descriptions
Truth table50%1/2
A
A= $descriptions
Aresult
11
00
714 sandbox_grant_descriptions => $descriptions || {},
715 action_description => $self->{actions}{$request->{action}},
716 sandbox_implementation => $self->{sandbox_implementation},
717 }
718 }
719 362 ▶}
Branch: 1 branch
BranchTrueFalseTotal
length($ENV{'PERL_ANYEVENT_MCP_MULTIBROKER_ALIAS'} // '') ? : 16 346 362
Condition75%3/4
$request->{'sandbox_grants'} || []
l!l
3620
$ENV{"PERL_ANYEVENT_MCP_MULTIBROKER_ALIAS"} // ""
l!l
16346
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
720  
721 99 ▶sub resolve {
Subroutine resolve: called CC=41 SCAR=41.7
Pod documented
722 99 my ($self, $request, $approval, $tool) = @_;
723  
724 99 my $lock = $self->_lock;
725 99 my $grant = $self->check ($request, $tool);
726  
727 99 ▶ return $grant unless $grant->{decision} eq "ask";
Branch: 1 branch
BranchTrueFalseTotal
unless $grant->{"decision"} eq "ask" 4 95 99
728  
729 95 my $json = JSON::XS->new->canonical;
730  
731 die "Permission approval does not match request\n" unless ref $approval eq "HASH"
732 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
733  
734 92 my $prepared = $grant->{request};
735 92 my $decision = $approval->{decision};
736  
737 die "Invalid permission decision\n" unless ref $decision eq "HASH"
738 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
739  
740 92 my $allow = $decision->{decision} =~ /\Aallow-/;
741 92 my $granted = [];
742  
743 92 ▶ if ($allow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow) 66 26 92
744 66 my $have = $self->closure ($prepared->{sandbox_grants});
745 66 my $give = $self->closure ($decision->{sandbox_grants});
746 66 my %choices = map { $_ => 1 } @{$prepared->{sandbox_grant_choices}};
747  
748 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
749 $granted = [(grep !$give->{$_}, keys %$have)
750 ? (grep $give->{$_}, sort keys %$have)
751 66 ▶ : @{$prepared->{sandbox_grants}}];
Branch: 1 branch
BranchTrueFalseTotal
grep((!$give->{$_}), keys %$have) ? : 16 50 66
752 }
753  
754 92 ▶ if ($decision->{decision} =~ /-always\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decision->{"decision"} =~ /-always\z/u) 33 59 92
755 33 my $rule = $decision->{rule};
756 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
757 33 ▶ my $action = $allow ? "allow" : "deny";
Branch: 1 branch
BranchTrueFalseTotal
$allow ? : 25 8 33
758  
759 # the rule contains only the selected dimensions, never extra authority.
760 33 my %fields = map { $_ => 1 } qw(decision action sandbox-grants path message expires);
761 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
762  
763 ref $rule eq "HASH"
764 && $rule->{action} eq $prepared->{action_matcher}
765 && ($rule->{decision} || "allow") eq $action
766 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
767 or die "Invalid persistent permission rule\n";
768  
769 die "Persistent permission sandbox-grants mismatch\n"
770 unless defined $rule->{"sandbox-grants"} && $rule->{"sandbox-grants"} eq
771 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
772  
773 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
774 30 my $key = "match.$name";
775 die "Persistent dimension scope was not offered\n"
776 unless defined $rule->{$key}
777 && grep $_->{match} eq $rule->{$key},
778 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
779 }
780 30 ▶ if (exists $rule->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"path"}) 2 28 30
781 die "Invalid persistent denial path\n" unless !$allow
782 && ref $prepared->{paths} eq "ARRAY"
783 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
784 }
785 die "Persistent denial message mismatch\n"
786 unless (!$allow || !defined $decision->{message})
787 && (defined $rule->{message} ? $rule->{message} : "")
788 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
789  
790 30 my $rules = $self->rules;
791 30 push @$rules, { %$rule };
792 30 $self->_write ($rules);
793 }
794  
795 {
796 decision => $allow ? "allow" : "deny",
797 sandbox_grants => $granted,
798 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
799 }
800 }
801  
802 240 ▶sub _call {
Subroutine _call: called CC=1 SCAR=0.0
803 240 my ($req) = @_;
804  
805 240 my %params = %{$req->{params}};
806 240 delete @params{qw(inputResponses requestState _meta)};
807 # _meta is per-request context, not stable operation identity: retries can
808 # carry different client capabilities, progress tokens and clientInfo.
809 # bind the continuation to its authenticated user as well as the call.
810 240 JSON::XS->new->canonical->encode ([$req->{user}, $req->{protocol_version}, $req->{method}, \%params])
811 }
812  
813 248 ▶sub _user_request {
Subroutine _user_request: called CC=4 SCAR=13.9
814 248 my ($req, $request) = @_;
815  
816 248 ▶ my $dimensions = $request->{dimensions} || {};
Condition100%2/2
$request->{'dimensions'} || {}
l!l
16880
MC/DC100%1/1
$request->{'dimensions'} || {}
$request->{'dimensions'}
Truth table100%2/2
A
A= $request->{'dimensions'}
Aresult
11
00
817 248 ▶ ref $dimensions eq "HASH" or die "Invalid permission dimensions\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $dimensions eq "HASH" or die "Invalid permission dimensions\n" 248 0 248
818 248 $dimensions = { %$dimensions };
819 248 delete $dimensions->{user};
820  
821 248 ▶ if (defined (my $user = $req->{user})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $user = $req->{"user"})) 1 247 248
822 $dimensions->{user} = {
823 1 label => "User",
824 value => $user,
825 choices => [
826 { label => "This user: $user", match => $user },
827 { label => "All users", match => "^" },
828 ],
829 default => $user,
830 };
831 }
832  
833 248 +{ %$request, dimensions => $dimensions }
834 }
835  
836 23 ▶sub _remember {
Subroutine _remember: called CC=1 SCAR=0.0
837 23 my ($self, $req, $request, $grant) = @_;
838  
839 23 my $key = JSON::XS->new->canonical->encode ($request);
840 23 $req->{_permission_grants}{$key} = $grant;
841 }
842  
843 37 ▶sub denial_message {
Subroutine denial_message: called CC=3 SCAR=11.1
Pod undocumented
844 37 my ($grant) = @_;
845  
846 "Permission denied" . (defined $grant->{message} && length $grant->{message}
847 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
848 }
849  
850 59 ▶sub _challenge {
Subroutine _challenge: called CC=10 SCAR=25.7
851 59 my ($self, $req, $request, $prepared, $grants, $stage, $rounds, $selection) = @_;
852  
853 59 ▶ return $req->error ("Too many permission rounds") if $rounds > 32;
Branch: 1 branch
BranchTrueFalseTotal
if $rounds > 32 0 59 59
854  
855 59 AE::log debug => "permission elicitation: challenge created (stage $stage)";
856 59 my $state = "ae-mcp-permission:" . unpack "H*", AnyEvent::MCP::random_bytes 32;
857 $self->{pending}{$state} = {
858 call => $req->{_permission_call} || _call ($req),
859 request => $request,
860 prepared => $prepared,
861 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
862 stage => $stage,
863 rounds => $rounds,
864 selection => $selection || {},
865 };
866  
867 59 weaken $self;
868 0 ▶ $self->{pending}{$state}{timer} = AnyEvent->timer (after => $self->{timeout}, cb => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
869 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
870 0 AE::log debug => "permission elicitation: challenge expired (stage $stage)";
871 }
872 59 });
873  
874 $req->reply ({
875 resultType => "input_required",
876 requestState => $state,
877 inputRequests => {
878 permission => {
879 method => "elicitation/create",
880 params => {
881 mode => "form",
882 _meta => {
883 "anyevent-mcp/permission" => {
884 version => 1,
885 request => $prepared,
886 stage => $stage,
887 selection => $selection || {},
888 },
889 },
890 message => $stage eq "rule" ? "Configure the persistent permission rule.\n"
891 . (($selection->{decision} || "") eq "deny-always"
892 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
893 . AnyEvent::MCP::Permissions::Ask::message ($prepared, $stage)
894 : AnyEvent::MCP::Permissions::Ask::message ($prepared),
895 requestedSchema => AnyEvent::MCP::Permissions::Ask::schema ($prepared, $stage, $selection),
896 },
897 },
898 },
899 });
900 }
901  
902 50 ▶sub _next_stage {
Subroutine _next_stage: called CC=5 SCAR=16.1
903 50 my ($decision, $stage) = @_;
904  
905 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
906 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
907 undef
908 43 }
909  
910 214 ▶sub _resume {
Subroutine _resume: called CC=17 SCAR=28.9
911 214 my ($self, $req) = @_;
912  
913 214 ▶ return if $req->{_permission_resumed}++;
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"_permission_resumed"}++ 6 208 214
914  
915 208 $req->{_permission_call} = _call ($req);
916  
917 208 my $state = $req->{params}{requestState};
918 208 ▶ return unless defined $state && $state =~ /\Aae-mcp-permission:/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state and $state =~ /\Aae-mcp-permission:/u 166 42 208
Condition66%2/3
defined $state and $state =~ /\Aae-mcp-permission:/u
!ll&&!rl&&r
166042
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
919  
920 42 my $pending = delete $self->{pending}{$state};
921 42 ▶ AE::log debug => "permission elicitation: continuation has no pending challenge"
Branch: 1 branch
BranchTrueFalseTotal
unless $pending 3 39 42
922 unless $pending;
923 AE::log debug => "permission elicitation: continuation request mismatch"
924 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
925 die "Invalid or expired permission continuation\n"
926 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
927  
928 37 my $response = $req->{params}{inputResponses}{permission};
929 my $action = ref $response eq "HASH" && !ref $response->{action}
930 && ($response->{action} // "") =~ /\A(?:accept|decline|cancel)\z/
931 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
932 37 AE::log debug => "permission elicitation: continuation received (stage $pending->{stage}, action $action)";
933 my $decision = AnyEvent::MCP::Permissions::Ask::decision
934 37 $pending->{prepared}, $response, $pending->{stage}, $pending->{selection};
935 37 ▶ if (my $next = _next_stage ($decision, $pending->{stage})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = _next_stage($decision, $pending->{"stage"})) 5 32 37
936 $self->_challenge ($req, $pending->{request}, $pending->{prepared}, $pending->{grants},
937 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
938 return
939 5 }
940 my $grant = $self->resolve ($pending->{request}, {
941 request => $pending->{prepared}, decision => $decision,
942 32 }, $req->{tool});
943  
944 32 ▶ die denial_message ($grant) . "\n" unless $grant->{decision} eq "allow";
Branch: 1 branch
BranchTrueFalseTotal
unless $grant->{"decision"} eq "allow" 15 17 32
945  
946 17 $req->{_permission_grants} = $pending->{grants};
947 17 $self->_remember ($req, $pending->{request}, $grant->{sandbox_grants});
948 }
949  
950 146 ▶sub require_permission {
Subroutine require_permission: called CC=4 SCAR=13.9
Pod documented
951 146 my ($self, $req, $request) = @_;
952  
953 146 $request = _user_request ($req, $request);
954 146 my $grant = $self->check ($request, $req->{tool});
955  
956 146 ▶ return $grant->{sandbox_grants} if $grant->{decision} eq "allow";
Branch: 1 branch
BranchTrueFalseTotal
if $grant->{"decision"} eq "allow" 59 87 146
957  
958 87 ▶ if ($grant->{decision} eq "deny") {
Branch: 1 branch
BranchTrueFalseTotal
if ($grant->{"decision"} eq "deny") 14 73 87
959 14 AE::log debug => "permission policy: denied";
960 14 die denial_message ($grant) . "\n";
961 }
962  
963 73 my $key = JSON::XS->new->canonical->encode ($request);
964  
965 return $req->{_permission_grants}{$key}
966 73 ▶ if exists $req->{_permission_grants}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $req->{"_permission_grants"}{$key} 25 48 73
967  
968 48 die bless { request => $request }, "AnyEvent::MCP::Server::Permissions::Required";
969 }
970  
971 100 ▶sub require {
Subroutine require: called CC=33 SCAR=35.7
Pod documented
972 100 my ($self, $req, $request, $done) = @_;
973  
974 100 $request = _user_request ($req, $request);
975 100 my $grant = eval { $self->check ($request, $req->{tool}) };
976  
977 100 ▶ return $done->(0, "$@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 100 100
978  
979 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
980 50 my $key = JSON::XS->new->canonical->encode ($request);
981 return $done->(1, $req->{_permission_grants}{$key})
982 50 ▶ if exists $req->{_permission_grants}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $req->{"_permission_grants"}{$key} 1 49 50
983 }
984  
985 99 my $state = $req->{params}{requestState};
986  
987 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
988 19 my $pending = delete $self->{pending}{$state};
989 return $done->(0, "Invalid or expired permission continuation")
990 unless $pending && $pending->{call} eq _call ($req)
991 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
992 13 my $response = $req->{params}{inputResponses}{permission};
993 13 my $decision = eval {
994 AnyEvent::MCP::Permissions::Ask::decision
995 $pending->{prepared}, $response, $pending->{stage}, $pending->{selection}
996 13 };
997 13 ▶ return $done->(0, "$@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 13 13
998 13 ▶ if (my $next = _next_stage ($decision, $pending->{stage})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = _next_stage($decision, $pending->{"stage"})) 2 11 13
999 $self->_challenge ($req, $pending->{request}, $pending->{prepared}, $pending->{grants},
1000 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
1001 return
1002 2 }
1003 11 $grant = eval { $self->resolve ($request, { request => $pending->{prepared}, decision => $decision }, $req->{tool}) };
1004 11 ▶ return $done->(0, "$@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 11 11
1005 }
1006  
1007 91 ▶ return $done->(1, $grant->{sandbox_grants}) if $grant->{decision} eq "allow";
Branch: 1 branch
BranchTrueFalseTotal
if $grant->{"decision"} eq "allow" 11 80 91
1008 80 ▶ if ($grant->{decision} eq "deny") {
Branch: 1 branch
BranchTrueFalseTotal
if ($grant->{"decision"} eq "deny") 6 74 80
1009 6 AE::log debug => "permission policy: denied";
1010 6 return $done->(0, denial_message ($grant));
1011 }
1012  
1013 74 my $prepared = $grant->{request};
1014 74 my $ask = $self->{ask};
1015 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
1016  
1017 71 my $client_caps = $req->{params}{_meta}{"io.modelcontextprotocol/clientCapabilities"};
1018 my $mcp = $req->{protocol_version} ge "2026" && ref $client_caps eq "HASH"
1019 && ref $client_caps->{elicitation} eq "HASH"
1020 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
1021  
1022 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
1023 53 ▶ return $done->(0, "MCP form elicitation is unavailable") unless $mcp;
Branch: 1 branch
BranchTrueFalseTotal
unless $mcp 1 52 53
1024  
1025 $self->_challenge ($req, $request, $prepared, $req->{_permission_grants},
1026 52 "choice", 1);
1027 return
1028 52 }
1029  
1030 18 weaken $self;
1031  
1032 18 my $finished;
1033 17 ▶ my $finish = sub {
Subroutine __ANON__: called CC=7 SCAR=19.9
1034 17 my ($success, $decision) = @_;
1035  
1036 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
1037 16 ▶ die "Permission prompt already finished\n" if $finished++;
Branch: 1 branch
BranchTrueFalseTotal
if $finished++ 0 16 16
1038 16 ▶ return $done->(0, $decision) unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 16 16
1039  
1040 16 my $grant = eval { $self->resolve ($request, { request => $prepared, decision => $decision }, $req->{tool}) };
1041  
1042 16 ▶ return $done->(0, "$@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 16 16
1043  
1044 $done->($grant->{decision} eq "allow" ? (1, $grant->{sandbox_grants})
1045 16 ▶ : (0, denial_message ($grant)));
Branch: 1 branch
BranchTrueFalseTotal
$grant->{'decision'} eq 'allow' ? : 14 2 16
1046 18 };
1047  
1048 18 ▶ if (ref $ask eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ask eq 'CODE') { } 11 7 18
1049 11 $ask->($prepared, $finish);
1050 } else {
1051 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
1052  
1053 my $job = AnyEvent::MCP::Permissions::Ask->new (
1054 helper => $ask eq "tty" || $ask eq "yad"
1055 ? ["anyevent-mcp-askpermission", "--$ask"] : $self->{helper},
1056 timeout => $self->{timeout},
1057 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
1058  
1059 7 my $old_cancel = $req->{on_cancel};
1060 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
1061 7 $job->ask ($prepared, $finish);
1062 }
1063 }
1064  
1065 53 1
« bin/anyevent-mcp-form-elicitation ↑ Summary blib/lib/AnyEvent/MCP/Server.pm »