line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SmokeRunner::Multi::SafeRun; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:YANICK'; |
3
|
|
|
|
|
|
|
#ABSTRACT: Run an external command safely in taint mode |
4
|
|
|
|
|
|
|
$SmokeRunner::Multi::SafeRun::VERSION = '0.21'; |
5
|
8
|
|
|
8
|
|
41
|
use strict; |
|
8
|
|
|
|
|
14
|
|
|
8
|
|
|
|
|
211
|
|
6
|
8
|
|
|
8
|
|
40
|
use warnings; |
|
8
|
|
|
|
|
13
|
|
|
8
|
|
|
|
|
255
|
|
7
|
|
|
|
|
|
|
|
8
|
8
|
|
|
8
|
|
45
|
use base 'Exporter'; |
|
8
|
|
|
|
|
21
|
|
|
8
|
|
|
|
|
895
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @EXPORT_OK = 'safe_run'; |
11
|
|
|
|
|
|
|
|
12
|
8
|
|
|
8
|
|
88
|
use Cwd qw( abs_path ); |
|
8
|
|
|
|
|
20
|
|
|
8
|
|
|
|
|
360
|
|
13
|
8
|
|
|
8
|
|
45
|
use File::Spec; |
|
8
|
|
|
|
|
17
|
|
|
8
|
|
|
|
|
201
|
|
14
|
8
|
|
|
8
|
|
780
|
use File::Which qw( which ); |
|
8
|
|
|
|
|
877
|
|
|
8
|
|
|
|
|
424
|
|
15
|
|
|
|
|
|
|
use SmokeRunner::Multi::Validate |
16
|
8
|
|
|
8
|
|
47
|
qw( validate SCALAR_TYPE ARRAYREF_TYPE SCALARREF_TYPE ); |
|
8
|
|
|
|
|
14
|
|
|
8
|
|
|
|
|
79
|
|
17
|
8
|
|
|
8
|
|
6584
|
use IPC::Run3 qw( run3 ); |
|
8
|
|
|
|
|
189928
|
|
|
8
|
|
|
|
|
2090
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
{ |
21
|
|
|
|
|
|
|
my $spec = { command => SCALAR_TYPE, |
22
|
|
|
|
|
|
|
args => ARRAYREF_TYPE( default => [] ), |
23
|
|
|
|
|
|
|
stdout_buffer => SCALARREF_TYPE, |
24
|
|
|
|
|
|
|
stderr_buffer => SCALARREF_TYPE, |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub safe_run |
28
|
|
|
|
|
|
|
{ |
29
|
6
|
|
|
6
|
1
|
2768
|
my %p = validate( @_, $spec ); |
30
|
|
|
|
|
|
|
|
31
|
6
|
|
|
|
|
50
|
my $cmd; |
32
|
6
|
100
|
|
|
|
141
|
if ( File::Spec->file_name_is_absolute( $p{command} ) ) |
33
|
|
|
|
|
|
|
{ |
34
|
4
|
|
|
|
|
13
|
$cmd = $p{command}; |
35
|
|
|
|
|
|
|
|
36
|
4
|
50
|
|
|
|
189
|
die "$cmd is not executable" |
37
|
|
|
|
|
|
|
unless -x $cmd; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
else |
40
|
|
|
|
|
|
|
{ |
41
|
|
|
|
|
|
|
$cmd = which( $p{command} ) |
42
|
2
|
50
|
|
|
|
16
|
or die "Cannot find $p{command} in path"; |
43
|
|
|
|
|
|
|
|
44
|
2
|
|
|
|
|
715
|
$cmd = abs_path($cmd); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# This is a simple way to make the path taint-safe. |
48
|
6
|
|
|
|
|
62
|
local $ENV{PATH} = ''; |
49
|
6
|
|
|
|
|
60
|
run3( [ $cmd, @{ $p{args} } ], |
50
|
|
|
|
|
|
|
undef, |
51
|
|
|
|
|
|
|
$p{stdout_buffer}, $p{stderr_buffer} |
52
|
6
|
|
|
|
|
20
|
); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
1; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
__END__ |