AnyEvent-MCP
view release on metacpan or search on metacpan
lib/AnyEvent/MCP/Server/Permissions.pm view on Meta::CPAN
Continuations are short-lived and bound to the method, parameters, and request,
and are consumed once. Approval retries retain earlier one-time grants
within the same call. Always decisions and Back use additional protected C<input_required>
rounds, capped at 32, without executing the original tool. Messages in
persistent deny rules are returned to subsequent callers, including later
sessions. Client-side elicitation is only appropriate when the connected
client is the trusted approval authority.
=item $closure = $permissions->closure ([$grant...])
Returns a hash reference whose keys contain the supplied sandbox grants
and all grants they imply. Unknown grants or malformed implication lists
die. This expands authority names; it does not grant permission.
=item $permissions->auto_rules ($rules)
Replace the ordered process-local rule chain. Parsing and validation
occur once, at installation. An empty list disables automatic rules.
With an optional true second argument, prepend the new rules before existing
process-local rules instead of replacing them.
=item $rules = $permissions->rules
Returns the ordered, uncompiled in-memory or file-backed rule array reference.
File-backed rules are reread, so changes apply to subsequent checks.
=item $permissions->save ($rules)
Replaces the ordered rules without semantic validation, using atomic file
replacement if backed by a file.
=back
=cut
package AnyEvent::MCP::Server::Permissions;
use common::sense;
use AnyEvent::MCP ();
use AnyEvent::MCP::Config ();
use AnyEvent ();
use AnyEvent::MCP::Permissions::Ask ();
use JSON::XS ();
use Errno qw(ENOENT);
use Scalar::Util qw(weaken);
our @ISA = qw(AnyEvent::MCP::Base);
sub new {
my ($class, %kv) = @_;
my $auto_rules = delete $kv{auto_rules} || [];
my $ask = exists $kv{ask} ? $kv{ask} : $ENV{PERL_ANYEVENT_MCP_PERMISSION_ASK};
$ask = "helper" unless defined $ask && length $ask;
die "Invalid permission ask mode (expected helper, tty, yad, auto, mcp, or none)\n"
unless ref $ask eq "CODE" || !ref $ask && $ask =~ /\A(?:helper|tty|yad|auto|mcp|none)\z/;
my $self = $class->SUPER::new (sandbox_grants => {}, actions => {}, rules => [], ask => $ask, timeout => 300, %kv);
if (!exists $kv{file} && defined $kv{name}) {
my $name = AnyEvent::MCP::utf8enc $kv{name};
$name =~ s/([^A-Za-z0-9_.-])/sprintf "%%%02X", ord $1/ge;
my $dir = $ENV{XDG_CONFIG_HOME} || ($ENV{HOME} || die "HOME is not set\n") . "/.config";
$self->{file} = "$dir/anyevent-mcp/rules/$name.ini";
}
$self->closure ([keys %{$self->{sandbox_grants}}]);
$self->_rule ($_) for @{$self->rules};
$self->auto_rules ($auto_rules);
$self
}
sub auto_rules {
my ($self, $rules, $prepend) = @_;
ref $rules eq "ARRAY" or die "Automatic permission rules must be an array\n";
my @compiled;
for my $entry (@$rules) {
ref $entry eq "ARRAY" && @$entry == 2
&& defined $entry->[0] && !ref $entry->[0] && $entry->[0] =~ /\A(?:allow|deny)\z/
&& defined $entry->[1] && !ref $entry->[1]
or die "Invalid automatic permission rule\n";
my @parts = split /,/, $entry->[1], -1;
@parts && @parts <= 3 && !grep { !length || ($_ ne "*" && /[\s*]/) } @parts
or die "Invalid automatic permission rule '$entry->[1]'\n";
my ($tool, $action, $capability) = (@parts, ("*") x (3 - @parts));
$tool =~ y/_/-/;
push @compiled, {
decision => $entry->[0],
tool => $tool,
action => $action,
capability => $capability,
closure => $capability eq "*" ? undef : $self->closure ([$capability]),
};
}
push @compiled, @{$self->{auto_rules}} if $prepend;
$self->{auto_rules} = \@compiled;
}
sub closure {
my ($self, $sandbox_grants) = @_;
ref $sandbox_grants eq "ARRAY"
or die "Sandbox grants must be an array\n";
my %set;
my @todo = @$sandbox_grants;
while (@todo) {
my $capability = pop @todo;
defined $capability && !ref $capability
&& ref $self->{sandbox_grants}{$capability} eq "ARRAY"
or die "Unknown sandbox grant\n";
next if $set{$capability}++;
push @todo, @{$self->{sandbox_grants}{$capability}};
}
\%set
( run in 1.026 second using v1.01-cache-2.11-cpan-234fa220139 )