| 1 | # This is an implementation of Eval::Safe that uses the Safe module to execute | ||||||||||||||||||||||||||||
| 2 | # the user provided code. | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Eval::Safe::Safe; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use 5.022; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 13 | ▶ | use parent 'Eval::Safe'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 13 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 13 | ▶ | use Eval::Safe::ForkedSafe; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 13 | ▶ | use File::Spec::Functions qw(rel2abs); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 48 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 48 | my ($class, %options) = @_; | |||||||||||||||||||||||||||
| 18 | 48 | my $self = bless \%options, $class; | |||||||||||||||||||||||||||
| 19 | 48 | my $safe = Eval::Safe::ForkedSafe->new($self->{package}); | |||||||||||||||||||||||||||
| 20 | 48 | ▶ | $self->{package} = $safe->root() unless $self->{package}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | # This option is always set if we're building an Eval::Safe::Safe. | ||||||||||||||||||||||||||||
| 22 | 48 | ▶ | if ($self->{safe} > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 0 | $safe->permit_only(qw(:base_core :base_mem :base_loop :base_math :base_orig | |||||||||||||||||||||||||||
| 24 | :load)); | ||||||||||||||||||||||||||||
| 25 | 0 | $safe->deny(qw(tie untie bless)); | |||||||||||||||||||||||||||
| 26 | } else { | ||||||||||||||||||||||||||||
| 27 | 48 | $safe->deny_only(qw(:subprocess :ownprocess :others :dangerous)); | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | 48 | $self->{safe} = $safe; | |||||||||||||||||||||||||||
| 30 | 48 | return $self; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 48 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 48 | local($., $@, $!, $^E, $?); | |||||||||||||||||||||||||||
| 36 | 48 | my ($this) = @_; | |||||||||||||||||||||||||||
| 37 | 48 | delete $this->{safe}; | |||||||||||||||||||||||||||
| 38 | # The package is not entirely deleted by the Safe destructor. | ||||||||||||||||||||||||||||
| 39 | 48 | CORE::eval('undef %'.($this->{package}).'::'); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 43 | ▶ | sub eval { | ||||||||||||||||||||||||||
|
Subroutine
eval: called CC=8 SCAR=21.5
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 43 | my ($this, $code) = @_; | |||||||||||||||||||||||||||
| 45 | 43 | my $eval_str = sprintf "%s; %s; %s", $this->{strict}, $this->{warnings}, $code; | |||||||||||||||||||||||||||
| 46 | 0 | ▶ | print {$this->{debug}} "Evaling (safe): '${eval_str}'\n" if $this->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 43 | my @ret; | |||||||||||||||||||||||||||
| 48 | 43 | ▶ | if (not defined wantarray) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 49 | 14 | $this->{safe}->reval($eval_str); | |||||||||||||||||||||||||||
| 50 | } elsif (wantarray) { | ||||||||||||||||||||||||||||
| 51 | 4 | @ret = $this->{safe}->reval($eval_str); | |||||||||||||||||||||||||||
| 52 | } else { | ||||||||||||||||||||||||||||
| 53 | 25 | @ret = scalar $this->{safe}->reval($eval_str); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | print {$this->{debug}} "Safe returned an error: $@" if $this->{debug} && $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$this->{"debug"} and $@
MC/DC0%0/2
$this->{"debug"} and $@
$this->{"debug"}
$@
Truth table33%1/3
A and B
A= $this->{"debug"}B= $@
| |||||||||||||||||||||||||||||
| 56 | 43 | ▶ | if (defined wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 29 | ▶ | return (wantarray) ? @ret : $ret[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 14 | return; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | ▶ | sub do { | ||||||||||||||||||||||||||
|
Subroutine
do: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 3 | my ($this, $file) = @_; | |||||||||||||||||||||||||||
| 64 | # do can open relative paths, but in that case it looks them up in the @INC | ||||||||||||||||||||||||||||
| 65 | # directory, which we want to avoid. | ||||||||||||||||||||||||||||
| 66 | # We don't use abs_path here to not die (just yet) if the file does not exist. | ||||||||||||||||||||||||||||
| 67 | 3 | my $abs_path = rel2abs($file); | |||||||||||||||||||||||||||
| 68 | 3 | $this->{safe}->rdo($abs_path); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 13 | 1; | |||||||||||||||||||||||||||