| 1 | package Test::Taint; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | ## no critic (Bangs::ProhibitVagueNames) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | ## We're dealing with abstract vars like "$var" in this code. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | Test::Taint - Tools to test taintedness | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | Version 1.08 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 8 | ▶ | use vars qw( $VERSION ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 8 | $VERSION = '1.08'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | taint_checking_ok(); # We have to have taint checking on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | my $id = "deadbeef"; # Dummy session ID | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | taint( $id ); # Simulate it coming in from the web | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | tainted_ok( $id ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | $id = validate_id( $id ); # Your routine to check the $id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | untainted_ok( $id ); # Did it come back clean? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | ok( defined $id ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | Tainted data is data that comes from an unsafe source, such as the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | command line, or, in the case of web apps, any GET or POST transactions. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | Read the L<perlsec> man page for details on why tainted data is bad, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | and how to untaint the data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | When you're writing unit tests for code that deals with tainted data, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | you'll want to have a way to provide tainted data for your routines to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | handle, and easy ways to check and report on the taintedness of your data, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | in standard L<Test::More> style. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 8 | ▶ | use base 'DynaLoader'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 8 | ▶ | use Test::Builder; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 8 | ▶ | use overload; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 8 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 8 | ▶ | use vars qw( $TAINT ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 8 | my $Test = Test::Builder->new; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 8 | ▶ | use vars qw( @EXPORT ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 8 | @EXPORT = qw( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | taint taint_deeply | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | tainted tainted_deeply | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | tainted_ok tainted_ok_deeply | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | untainted_ok untainted_ok_deeply | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | taint_checking | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | taint_checking_ok | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 8 | bootstrap Test::Taint $VERSION; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 7 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 7 | my $caller = caller; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 8 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 7 | for my $sub ( @EXPORT ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 70 | *{$caller.'::'.$sub} = \&{$sub}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 7 | $Test->exported_to($caller); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 7 | $Test->plan(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } # import | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 15 | ▶ | sub _deeply_traverse { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_deeply_traverse: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 15 | my $callback = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 15 | my @stack = \@_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 15 | my %seen; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 15 | ▶ | while(@stack) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 104 | my $node = pop @stack; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | # skip the node if its not a reference | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 104 | ▶ | next unless defined $node; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 94 | ▶ | my($realpack, $realtype, $id) = overload::StrVal($node) =~ /\A(?:(.+)\=)?(HASH|ARRAY|GLOB|SCALAR|REF)\((0x[[:xdigit:]]+)\)\z/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | or next; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # taint the contents of tied objects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 59 | ▶ | if(my $tied = $realtype eq 'HASH' ? tied %{$node} : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 23 | ▶ | $realtype eq 'ARRAY' ? tied @{$node} : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 11 | ▶ | $realtype eq 'SCALAR' ? tied ${$node} : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 4 | $realtype eq 'REF' ? tied ${$node} : undef) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | push @stack, $tied; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 3 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | # prevent circular references from being traversed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 8 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 56 | ▶ | next if $seen{$realpack, $realtype, $id}++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # perform an action on the node, then push them on the stack for traversal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | push @stack, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 15 | ▶ | $realtype eq 'HASH' ? $callback->(values %{$node}) : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 22 | ▶ | $realtype eq 'ARRAY' ? $callback->(@{$node}) : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 10 | ▶ | $realtype eq 'SCALAR' ? $callback->(${$node}) : | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | $realtype eq 'REF' ? $callback->(${$node}) : | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 55 | ▶ | map $callback->(*$node{$_}), qw(SCALAR ARRAY HASH); #must be a GLOB | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 15 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } # _deeply_traverse | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | =head1 C<Test::More>-style Functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | All the C<xxx_ok()> functions work like standard C<Test::More>-style | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | functions, where the last parm is an optional message, it outputs ok or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | not ok, and returns a boolean telling if the test passed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | =head2 taint_checking_ok( [$message] ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | L<Test::More>-style test that taint checking is on. This should probably | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | be the first thing in any F<*.t> file that deals with taintedness. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 5 | ▶ | sub taint_checking_ok { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
taint_checking_ok: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 5 | ▶ | my $msg = @_ ? shift : "Taint checking is on"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 5 | my $ok = taint_checking(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 5 | $Test->ok( $ok, $msg ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 5 | return $ok; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | } # taint_checking_ok | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | =head2 tainted_ok( $var [, $message ] ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | Checks that I<$var> is tainted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | tainted_ok( $ENV{FOO} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 23 | ▶ | sub tainted_ok { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tainted_ok: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 23 | my $var = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 23 | my $msg = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 23 | my $ok = tainted( $var ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 23 | $Test->ok( $ok, $msg ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 23 | return $ok; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | } # tainted_ok | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | =head2 untainted_ok( $var [, $message ] ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | Checks that I<$var> is not tainted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | my $foo = my_validate( $ENV{FOO} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | untainted_ok( $foo ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 49 | ▶ | sub untainted_ok { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
untainted_ok: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 49 | my $var = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 49 | my $msg = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 49 | my $ok = !tainted( $var ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 49 | $Test->ok( $ok, $msg ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 49 | return $ok; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | } # untainted_ok | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | =head2 tainted_ok_deeply( $var [, $message ] ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | Checks that I<$var> is tainted. If I<$var> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | is a reference, it recursively checks every | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | variable to make sure they are all tainted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | tainted_ok_deeply( \%ENV ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 1 | ▶ | sub tainted_ok_deeply { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tainted_ok_deeply: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 1 | my $var = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 1 | my $msg = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 1 | my $ok = tainted_deeply( $var ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 1 | $Test->ok( $ok, $msg ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 1 | return $ok; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | } # tainted_ok_deeply | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | =head2 untainted_ok_deeply( $var [, $message ] ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | Checks that I<$var> is not tainted. If I<$var> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | is a reference, it recursively checks every | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | variable to make sure they are all not tainted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | my %env = my_validate( \%ENV ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | untainted_ok_deeply( \%env ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 1 | ▶ | sub untainted_ok_deeply { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
untainted_ok_deeply: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 1 | my $var = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 1 | my $msg = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 1 | my $ok = !tainted_deeply( $var ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 1 | $Test->ok( $ok, $msg ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 1 | return $ok; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | } # untainted_ok_deeply | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | =head1 Helper Functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | These are all helper functions. Most are wrapped by an C<xxx_ok()> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | counterpart, except for C<taint> which actually does something, instead | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | of just reporting it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | =head2 taint_checking() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | Returns true if taint checking is enabled via the -T flag. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 6 | ▶ | sub taint_checking() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
taint_checking: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 6 | return tainted( $Test::Taint::TAINT ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | } # taint_checking | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | =head2 tainted( I<$var> ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | Returns boolean saying if C<$var> is tainted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 221 | ▶ | sub tainted { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tainted: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 8 | ▶ | no warnings qw(void uninitialized); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 221 | return !eval { local $SIG{__DIE__} = 'DEFAULT'; join('', shift), kill 0; 1 }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | } # tainted | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | =head2 tainted_deeply( I<$var> ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | Returns boolean saying if C<$var> is tainted. If | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | C<$var> is a reference it recursively checks every | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | variable to make sure they are all tainted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 2 | ▶ | sub tainted_deeply { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tainted_deeply: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 2 | my $is_tainted = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | _deeply_traverse( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 22 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 22 | foreach (@_) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | if not defined | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | or ref | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 31 | ▶ | or Scalar::Util::readonly $_ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
not defined $_ or ref $_ or &Scalar::Util::readonly($_) or tainted($_)
not defined $_ or ref $_ or &Scalar::Util::readonly($_)
not defined $_ or ref $_
MC/DC100%4/4
not defined $_ or ref $_ or &Scalar::Util::readonly($_) or tainted($_)
not defined $_
ref $_
&Scalar::Util::readonly($_)
tainted($_)
Truth table100%5/5
not(A) or B or C or D
A= not defined $_B= ref $_C= &Scalar::Util::readonly($_)D= tainted($_)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | or tainted $_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 3 | $is_tainted = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 3 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 22 | return @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | shift, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 2 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 2 | return $is_tainted; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | } # tainted_deeply | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | =head2 taint( @list ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | Marks each (apparently) taintable argument in I<@list> as being tainted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | References can be tainted like any other scalar, but it doesn't make | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | sense to, so they will B<not> be tainted by this function. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | Some C<tie>d and magical variables may fail to be tainted by this routine, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | try as it may. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 45 | ▶ | sub taint { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
taint: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 45 | local $_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 45 | for ( @_ ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 58 | ▶ | _taint($_) unless ref or Scalar::Util::readonly $_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $_ or &Scalar::Util::readonly($_)
MC/DC100%2/2
ref $_ or &Scalar::Util::readonly($_)
ref $_
&Scalar::Util::readonly($_)
Truth table100%3/3
A or B
A= ref $_B= &Scalar::Util::readonly($_)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | } # taint | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | # _taint() is an external function in Taint.xs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | =head2 taint_deeply( @list ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | Similar to C<taint>, except that if any elements in I<@list> are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | references, it walks deeply into the data structure and marks each | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | taintable argument as being tainted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | If any variables are C<tie>d this will taint all the scalars within | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | the tied object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 13 | ▶ | sub taint_deeply { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
taint_deeply: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | _deeply_traverse( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 41 | ▶ | sub { taint @_; @_ }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 13 | @_, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 13 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | } # taint_deeply | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 8 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=9 SCAR=22.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | MAKE_SOME_TAINT: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | # Somehow we need to get some taintedness into $Test::Taint::TAINT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | # Let's try the easy way first. Either of these should be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | # tainted, unless somebody has untainted them, so this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | # will almost always work on the first try. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | # (Unless, of course, taint checking has been turned off!) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 8 | $TAINT = substr("$0$^X", 0, 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 8 | ▶ | last if tainted $TAINT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | # Let's try again. Maybe somebody cleaned those. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 2 | $TAINT = substr(join('', @ARGV, %ENV), 0, 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 2 | ▶ | last if tainted $TAINT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | # If those don't work, go try to open some file from some unsafe | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | # source and get data from them. That data is tainted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | # (Yes, even reading from /dev/null works!) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 1 | local(*FOO); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 1 | for ( qw(/dev/null / . ..), values %INC, $0, $^X ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 126 | ▶ | next unless defined $_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 126 | ▶ | if ( open FOO, $_ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 126 | my $potentially_tainted_data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 126 | ▶ | if ( defined sysread FOO, $potentially_tainted_data, 1 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 123 | $TAINT = substr( $potentially_tainted_data, 0, 0 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 123 | ▶ | last if tainted $TAINT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 1 | close FOO; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | # Sanity check | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 8 | ▶ | die 'Our taintbrush should have zero length!' if length $TAINT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | Written by Andy Lester, C<< <andy@petdance.com> >>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | Copyright 2004-2019, Andy Lester. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | You may use, modify, and distribute this package under the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 8 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||