File: Taint.pm

package info (click to toggle)
libtest-taint-perl 1.06-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 168 kB
  • ctags: 126
  • sloc: perl: 499; makefile: 3
file content (367 lines) | stat: -rw-r--r-- 8,816 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package Test::Taint;

## no critic (Bangs::ProhibitVagueNames)
## We're dealing with abstract vars like "$var" in this code.

=head1 NAME

Test::Taint - Tools to test taintedness

=head1 VERSION

Version 1.06

=cut

use vars qw( $VERSION );
$VERSION = "1.06";

=head1 SYNOPSIS

    taint_checking_ok();        # We have to have taint checking on
    my $id = "deadbeef";        # Dummy session ID
    taint( $id );               # Simulate it coming in from the web
    tainted_ok( $id );
    $id = validate_id( $id );   # Your routine to check the $id
    untainted_ok( $id );        # Did it come back clean?
    ok( defined $id );

=head1 DESCRIPTION

Tainted data is data that comes from an unsafe source, such as the
command line, or, in the case of web apps, any GET or POST transactions.
Read the L<perlsec> man page for details on why tainted data is bad,
and how to untaint the data.

When you're writing unit tests for code that deals with tainted data,
you'll want to have a way to provide tainted data for your routines to
handle, and easy ways to check and report on the taintedness of your data,
in standard L<Test::More> style.

=cut

use strict;
use warnings;

use base 'DynaLoader';
use Test::Builder;
use overload;
use Scalar::Util;
use vars qw( $TAINT );

my $Test = Test::Builder->new;

use vars qw( @EXPORT );
@EXPORT = qw(
    taint             taint_deeply
    tainted           tainted_deeply
    tainted_ok        tainted_ok_deeply
    untainted_ok      untainted_ok_deeply
    taint_checking
    taint_checking_ok
);

bootstrap Test::Taint $VERSION;

sub import {
    my $self = shift;
    my $caller = caller;
    no strict 'refs';
    for my $sub ( @EXPORT ) {
        *{$caller.'::'.$sub} = \&{$sub};
    }
    $Test->exported_to($caller);
    $Test->plan(@_);
} # import

sub _deeply_traverse {
    my $callback = shift;
    my @stack    = \@_;

    my %seen;

    while(@stack) {
        my $node = pop @stack;

        # skip the node if its not a reference
        next unless defined $node;

        my($realpack, $realtype, $id) = overload::StrVal($node) =~ /\A(?:(.+)\=)?(HASH|ARRAY|GLOB|SCALAR|REF)\((0x[[:xdigit:]]+)\)\z/
            or next;

        # taint the contents of tied objects
        if(my $tied = $realtype eq 'HASH'   ? tied %{$node} :
                      $realtype eq 'ARRAY'  ? tied @{$node} :
                      $realtype eq 'SCALAR' ? tied ${$node} :
                      $realtype eq 'REF'    ? tied ${$node} : undef) {
            push @stack, $tied;
            next;
        }

        # prevent circular references from being traversed
        no warnings 'uninitialized';
        next if $seen{$realpack, $realtype, $id}++;

        # perform an action on the node, then push them on the stack for traversal
        push @stack,
            $realtype eq 'HASH'   ? $callback->(values %{$node}) :
            $realtype eq 'ARRAY'  ? $callback->(@{$node})        :
            $realtype eq 'SCALAR' ? $callback->(${$node})        :
            $realtype eq 'REF'    ? $callback->(${$node})        :
            map $callback->(*$node{$_}), qw(SCALAR ARRAY HASH);   #must be a GLOB
    }

    return;
} # _deeply_traverse

=head1 C<Test::More>-style Functions

All the C<xxx_ok()> functions work like standard C<Test::More>-style
functions, where the last parm is an optional message, it outputs ok or
not ok, and returns a boolean telling if the test passed.

=head2 taint_checking_ok( [$message] )

L<Test::More>-style test that taint checking is on.  This should probably
be the first thing in any F<*.t> file that deals with taintedness.

=cut

sub taint_checking_ok {
    my $msg = @_ ? shift : "Taint checking is on";

    my $ok = taint_checking();
    $Test->ok( $ok, $msg );

    return $ok;
} # taint_checking_ok

=head2 tainted_ok( $var [, $message ] )

Checks that I<$var> is tainted.

    tainted_ok( $ENV{FOO} );

=cut

sub tainted_ok {
    my $var = shift;
    my $msg = shift;
    my $ok = tainted( $var );
    $Test->ok( $ok, $msg );

    return $ok;
} # tainted_ok

=head2 untainted_ok( $var [, $message ] )

Checks that I<$var> is not tainted.

    my $foo = my_validate( $ENV{FOO} );
    untainted_ok( $foo );

=cut

sub untainted_ok {
    my $var = shift;
    my $msg = shift;

    my $ok = !tainted( $var );
    $Test->ok( $ok, $msg );

    return $ok;
} # untainted_ok

=head2 tainted_ok_deeply( $var [, $message ] )

Checks that I<$var> is tainted.  If I<$var>
is a reference, it recursively checks every
variable to make sure they are all tainted.

    tainted_ok_deeply( \%ENV );

=cut

sub tainted_ok_deeply {
    my $var = shift;
    my $msg = shift;

    my $ok = tainted_deeply( $var );
    $Test->ok( $ok, $msg );

    return $ok;
} # tainted_ok_deeply

=head2 untainted_ok_deeply( $var [, $message ] )

Checks that I<$var> is not tainted.  If I<$var>
is a reference, it recursively checks every
variable to make sure they are all not tainted.

    my %env = my_validate( \%ENV );
    untainted_ok_deeply( \%env );

=cut

sub untainted_ok_deeply {
    my $var = shift;
    my $msg = shift;

    my $ok = !tainted_deeply( $var );
    $Test->ok( $ok, $msg );

    return $ok;
} # untainted_ok_deeply

=head1 Helper Functions

These are all helper functions.  Most are wrapped by an C<xxx_ok()>
counterpart, except for C<taint> which actually does something, instead
of just reporting it.

=head2 taint_checking()

Returns true if taint checking is enabled via the -T flag.

=cut

sub taint_checking() {
    return tainted( $Test::Taint::TAINT );
} # taint_checking

=head2 tainted( I<$var> )

Returns boolean saying if C<$var> is tainted.

=cut

sub tainted {
    no warnings qw(void uninitialized);

    return !eval { local $SIG{__DIE__} = 'DEFAULT'; join('', shift), kill 0; 1 };
} # tainted

=head2 tainted_deeply( I<$var> )

Returns boolean saying if C<$var> is tainted.  If
C<$var> is a reference it recursively checks every
variable to make sure they are all tainted.

=cut

sub tainted_deeply {
  my $is_tainted = 1;

  _deeply_traverse(
      sub {
          foreach (@_) {
            next
              if not defined
              or ref
              or Scalar::Util::readonly $_
              or tainted $_;

            $is_tainted = 0;
            last;
          }

          return @_;
      },
      shift,
  );

  return $is_tainted;
} # tainted_deeply

=head2 taint( @list )

Marks each (apparently) taintable argument in I<@list> as being tainted.

References can be tainted like any other scalar, but it doesn't make
sense to, so they will B<not> be tainted by this function.

Some C<tie>d and magical variables may fail to be tainted by this routine,
try as it may.)

=cut

sub taint {
    local $_;

    for ( @_ ) {
        _taint($_) unless ref or Scalar::Util::readonly $_;
    }
} # taint

# _taint() is an external function in Taint.xs

=head2 taint_deeply( @list )

Similar to C<taint>, except that if any elements in I<@list> are
references, it walks deeply into the data structure and marks each
taintable argument as being tainted.

If any variables are C<tie>d this will taint all the scalars within
the tied object.

=cut

sub taint_deeply {
    _deeply_traverse(
        sub { taint @_; @_ },
        @_,
    );

    return;
} # taint_deeply

BEGIN {
    MAKE_SOME_TAINT: {
        # Somehow we need to get some taintedness into $Test::Taint::TAINT
        # Let's try the easy way first. Either of these should be
        # tainted, unless somebody has untainted them, so this
        # will almost always work on the first try.
        # (Unless, of course, taint checking has been turned off!)
        $TAINT = substr("$0$^X", 0, 0);
        last if tainted $TAINT;

        # Let's try again. Maybe somebody cleaned those.
        $TAINT = substr(join('', @ARGV, %ENV), 0, 0);
        last if tainted $TAINT;

        # If those don't work, go try to open some file from some unsafe
        # source and get data from them.  That data is tainted.
        # (Yes, even reading from /dev/null works!)
        local(*FOO);
        for ( qw(/dev/null / . ..), values %INC, $0, $^X ) {
            next unless defined $_;
            if ( open FOO, $_ ) {
                my $potentially_tainted_data;
                if ( defined sysread FOO, $potentially_tainted_data, 1 ) {
                    $TAINT = substr( $potentially_tainted_data, 0, 0 );
                    last if tainted $TAINT;
                }
            }
        }
        close FOO;
    }

    # Sanity check
    die 'Our taintbrush should have zero length!' if length $TAINT;
}


=head1 AUTHOR

Written by Andy Lester, C<< <andy@petdance.com> >>.

=head1 COPYRIGHT

Copyright 2004, Andy Lester, All Rights Reserved.

You may use, modify, and distribute this package under the
same terms as Perl itself.

=cut

1;