This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Gisle noted an unused variable
[perl5.git] / t / op / taint.t
1 #!./perl -T
2 #
3 # Taint tests by Tom Phoenix <rootbeer@teleport.com>.
4 #
5 # I don't claim to know all about tainting. If anyone sees
6 # tests that I've missed here, please add them. But this is
7 # better than having no tests at all, right?
8 #
9
10 BEGIN {
11     chdir 't' if -d 't';
12     @INC = '../lib';
13 }
14
15 use strict;
16 use Config;
17 use File::Spec::Functions;
18
19 BEGIN { require './test.pl'; }
20 plan tests => 238;
21
22
23 $| = 1;
24
25 use vars qw($ipcsysv); # did we manage to load IPC::SysV?
26
27 BEGIN {
28   if ($^O eq 'VMS' && !defined($Config{d_setenv})) {
29       $ENV{PATH} = $ENV{PATH};
30       $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy';
31   }
32   if ($Config{'extensions'} =~ /\bIPC\/SysV\b/
33       && ($Config{d_shm} || $Config{d_msg})) {
34       eval { require IPC::SysV };
35       unless ($@) {
36           $ipcsysv++;
37           IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU IPC_NOWAIT));
38       }
39   }
40 }
41
42 my $Is_MacOS    = $^O eq 'MacOS';
43 my $Is_VMS      = $^O eq 'VMS';
44 my $Is_MSWin32  = $^O eq 'MSWin32';
45 my $Is_NetWare  = $^O eq 'NetWare';
46 my $Is_Dos      = $^O eq 'dos';
47 my $Is_Cygwin   = $^O eq 'cygwin';
48 my $Invoke_Perl = $Is_VMS      ? 'MCR Sys$Disk:[]Perl.' :
49                   $Is_MSWin32  ? '.\perl'               :
50                   $Is_MacOS    ? ':perl'                :
51                   $Is_NetWare  ? 'perl'                 : 
52                                  './perl'               ;
53 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
54
55 if ($Is_VMS) {
56     my (%old, $x);
57     for $x ('DCL$PATH', @MoreEnv) {
58         ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
59     }
60     eval <<EndOfCleanup;
61         END {
62             \$ENV{PATH} = '' if $Config{d_setenv};
63             warn "# Note: logical name 'PATH' may have been deleted\n";
64             \@ENV{keys %old} = values %old;
65         }
66 EndOfCleanup
67 }
68
69 # Sources of taint:
70 #   The empty tainted value, for tainting strings
71 my $TAINT = substr($^X, 0, 0);
72 #   A tainted zero, useful for tainting numbers
73 my $TAINT0;
74 {
75     no warnings;
76     $TAINT0 = 0 + $TAINT;
77 }
78
79 # This taints each argument passed. All must be lvalues.
80 # Side effect: It also stringifies them. :-(
81 sub taint_these (@) {
82     for (@_) { $_ .= $TAINT }
83 }
84
85 # How to identify taint when you see it
86 sub any_tainted (@) {
87     not eval { join("",@_), kill 0; 1 };
88 }
89 sub tainted ($) {
90     any_tainted @_;
91 }
92 sub all_tainted (@) {
93     for (@_) { return 0 unless tainted $_ }
94     1;
95 }
96
97
98 sub test ($;$) {
99     my($ok, $diag) = @_;
100
101     my $curr_test = curr_test();
102
103     if ($ok) {
104         print "ok $curr_test\n";
105     } else {
106         print "not ok $curr_test\n";
107         printf "# Failed test at line %d\n", (caller)[2];
108         for (split m/^/m, $diag) {
109             print "# $_";
110         }
111         print "\n" unless
112             $diag eq ''
113             or substr($diag, -1) eq "\n";
114     }
115
116     next_test();
117
118     return $ok;
119 }
120
121 # We need an external program to call.
122 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : $Is_MacOS ? ":echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$"));
123 END { unlink $ECHO }
124 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
125 print PROG 'print "@ARGV\n"', "\n";
126 close PROG;
127 my $echo = "$Invoke_Perl $ECHO";
128
129 my $TEST = catfile(curdir(), 'TEST');
130
131 # First, let's make sure that Perl is checking the dangerous
132 # environment variables. Maybe they aren't set yet, so we'll
133 # taint them ourselves.
134 {
135     $ENV{'DCL$PATH'} = '' if $Is_VMS;
136
137     $ENV{PATH} = '';
138     delete @ENV{@MoreEnv};
139     $ENV{TERM} = 'dumb';
140
141     if ($Is_Cygwin && ! -f 'cygwin1.dll') {
142         system("/usr/bin/cp /usr/bin/cygwin1.dll .") &&
143             die "$0: failed to cp cygwin1.dll: $!\n";
144         eval q{
145             END { unlink "cygwin1.dll" }
146         };
147     }
148
149     if ($Is_Cygwin && ! -f 'cygcrypt-0.dll' && -f '/usr/bin/cygcrypt-0.dll') {
150         system("/usr/bin/cp /usr/bin/cygcrypt-0.dll .") &&
151             die "$0: failed to cp cygcrypt-0.dll: $!\n";
152         eval q{
153             END { unlink "cygcrypt-0.dll" }
154         };
155     }
156
157     test eval { `$echo 1` } eq "1\n";
158
159     SKIP: {
160         skip "Environment tainting tests skipped", 4
161           if $Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos || $Is_MacOS;
162
163         my @vars = ('PATH', @MoreEnv);
164         while (my $v = $vars[0]) {
165             local $ENV{$v} = $TAINT;
166             last if eval { `$echo 1` };
167             last unless $@ =~ /^Insecure \$ENV{$v}/;
168             shift @vars;
169         }
170         test !@vars, "@vars";
171
172         # tainted $TERM is unsafe only if it contains metachars
173         local $ENV{TERM};
174         $ENV{TERM} = 'e=mc2';
175         test eval { `$echo 1` } eq "1\n";
176         $ENV{TERM} = 'e=mc2' . $TAINT;
177         test !eval { `$echo 1` };
178         test $@ =~ /^Insecure \$ENV{TERM}/, $@;
179     }
180
181     my $tmp;
182     if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) {
183         print "# all directories are writeable\n";
184     }
185     else {
186         $tmp = (grep { defined and -d and (stat _)[2] & 2 }
187                      qw(sys$scratch /tmp /var/tmp /usr/tmp),
188                      @ENV{qw(TMP TEMP)})[0]
189             or print "# can't find world-writeable directory to test PATH\n";
190     }
191
192     SKIP: {
193         skip "all directories are writeable", 2 unless $tmp;
194
195         local $ENV{PATH} = $tmp;
196         test !eval { `$echo 1` };
197         test $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
198     }
199
200     SKIP: {
201         skip "This is not VMS", 4 unless $Is_VMS;
202
203         $ENV{'DCL$PATH'} = $TAINT;
204         test  eval { `$echo 1` } eq '';
205         test $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
206         SKIP: {
207             skip q[can't find world-writeable directory to test DCL$PATH], 2
208               unless $tmp;
209
210             $ENV{'DCL$PATH'} = $tmp;
211             test eval { `$echo 1` } eq '';
212             test $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
213         }
214         $ENV{'DCL$PATH'} = '';
215     }
216 }
217
218 # Let's see that we can taint and untaint as needed.
219 {
220     my $foo = $TAINT;
221     test tainted $foo;
222
223     # That was a sanity check. If it failed, stop the insanity!
224     die "Taint checks don't seem to be enabled" unless tainted $foo;
225
226     $foo = "foo";
227     test not tainted $foo;
228
229     taint_these($foo);
230     test tainted $foo;
231
232     my @list = 1..10;
233     test not any_tainted @list;
234     taint_these @list[1,3,5,7,9];
235     test any_tainted @list;
236     test all_tainted @list[1,3,5,7,9];
237     test not any_tainted @list[0,2,4,6,8];
238
239     ($foo) = $foo =~ /(.+)/;
240     test not tainted $foo;
241
242     $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
243     test not tainted $foo;
244     test $foo eq 'bar';
245
246     {
247       use re 'taint';
248
249       ($foo) = ('bar' . $TAINT) =~ /(.+)/;
250       test tainted $foo;
251       test $foo eq 'bar';
252
253       $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
254       test tainted $foo;
255       test $foo eq 'bar';
256     }
257
258     $foo = $1 if 'bar' =~ /(.+)$TAINT/;
259     test tainted $foo;
260     test $foo eq 'bar';
261
262     my $pi = 4 * atan2(1,1) + $TAINT0;
263     test tainted $pi;
264
265     ($pi) = $pi =~ /(\d+\.\d+)/;
266     test not tainted $pi;
267     test sprintf("%.5f", $pi) eq '3.14159';
268 }
269
270 # How about command-line arguments? The problem is that we don't
271 # always get some, so we'll run another process with some.
272 SKIP: {
273     my $arg = catfile(curdir(), "arg$$");
274     open PROG, "> $arg" or die "Can't create $arg: $!";
275     print PROG q{
276         eval { join('', @ARGV), kill 0 };
277         exit 0 if $@ =~ /^Insecure dependency/;
278         print "# Oops: \$@ was [$@]\n";
279         exit 1;
280     };
281     close PROG;
282     print `$Invoke_Perl "-T" $arg and some suspect arguments`;
283     test !$?, "Exited with status $?";
284     unlink $arg;
285 }
286
287 # Reading from a file should be tainted
288 {
289     test open(FILE, $TEST), "Couldn't open '$TEST': $!";
290
291     my $block;
292     sysread(FILE, $block, 100);
293     my $line = <FILE>;
294     close FILE;
295     test tainted $block;
296     test tainted $line;
297 }
298
299 # Globs should be forbidden, except under VMS,
300 #   which doesn't spawn an external program.
301 SKIP: {
302     skip "globs should be forbidden", 2 if 1 or $Is_VMS;
303
304     my @globs = eval { <*> };
305     test @globs == 0 && $@ =~ /^Insecure dependency/;
306
307     @globs = eval { glob '*' };
308     test @globs == 0 && $@ =~ /^Insecure dependency/;
309 }
310
311 # Output of commands should be tainted
312 {
313     my $foo = `$echo abc`;
314     test tainted $foo;
315 }
316
317 # Certain system variables should be tainted
318 {
319     test all_tainted $^X, $0;
320 }
321
322 # Results of matching should all be untainted
323 {
324     my $foo = "abcdefghi" . $TAINT;
325     test tainted $foo;
326
327     $foo =~ /def/;
328     test not any_tainted $`, $&, $';
329
330     $foo =~ /(...)(...)(...)/;
331     test not any_tainted $1, $2, $3, $+;
332
333     my @bar = $foo =~ /(...)(...)(...)/;
334     test not any_tainted @bar;
335
336     test tainted $foo;  # $foo should still be tainted!
337     test $foo eq "abcdefghi";
338 }
339
340 # Operations which affect files can't use tainted data.
341 {
342     test !eval { chmod 0, $TAINT }, 'chmod';
343     test $@ =~ /^Insecure dependency/, $@;
344
345     # There is no feature test in $Config{} for truncate,
346     #   so we allow for the possibility that it's missing.
347     test !eval { truncate 'NoSuChFiLe', $TAINT0 }, 'truncate';
348     test $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
349
350     test !eval { rename '', $TAINT }, 'rename';
351     test $@ =~ /^Insecure dependency/, $@;
352
353     test !eval { unlink $TAINT }, 'unlink';
354     test $@ =~ /^Insecure dependency/, $@;
355
356     test !eval { utime $TAINT }, 'utime';
357     test $@ =~ /^Insecure dependency/, $@;
358
359     SKIP: {
360         skip "chown() is not available", 2 unless $Config{d_chown};
361
362         test !eval { chown -1, -1, $TAINT }, 'chown';
363         test $@ =~ /^Insecure dependency/, $@;
364     }
365
366     SKIP: {
367         skip "link() is not available", 2 unless $Config{d_link};
368
369         test !eval { link $TAINT, '' }, 'link';
370         test $@ =~ /^Insecure dependency/, $@;
371     }
372
373     SKIP: {
374         skip "symlink() is not available", 2 unless $Config{d_symlink};
375
376         test !eval { symlink $TAINT, '' }, 'symlink';
377         test $@ =~ /^Insecure dependency/, $@;
378     }
379 }
380
381 # Operations which affect directories can't use tainted data.
382 {
383     test !eval { mkdir "foo".$TAINT, 0755.$TAINT0 }, 'mkdir';
384     test $@ =~ /^Insecure dependency/, $@;
385
386     test !eval { rmdir $TAINT }, 'rmdir';
387     test $@ =~ /^Insecure dependency/, $@;
388
389     test !eval { chdir "foo".$TAINT }, 'chdir';
390     test $@ =~ /^Insecure dependency/, $@;
391
392     SKIP: {
393         skip "chroot() is not available", 2 unless $Config{d_chroot};
394
395         test !eval { chroot $TAINT }, 'chroot';
396         test $@ =~ /^Insecure dependency/, $@;
397     }
398 }
399
400 # Some operations using files can't use tainted data.
401 {
402     my $foo = "imaginary library" . $TAINT;
403     test !eval { require $foo }, 'require';
404     test $@ =~ /^Insecure dependency/, $@;
405
406     my $filename = "./taintB$$";        # NB: $filename isn't tainted!
407     END { unlink $filename if defined $filename }
408     $foo = $filename . $TAINT;
409     unlink $filename;   # in any case
410
411     test !eval { open FOO, $foo }, 'open for read';
412     test $@ eq '', $@;          # NB: This should be allowed
413
414     # Try first new style but allow also old style.
415     # We do not want the whole taint.t to fail
416     # just because Errno possibly failing.
417     test eval('$!{ENOENT}') ||
418         $! == 2 || # File not found
419         ($Is_Dos && $! == 22) ||
420         ($^O eq 'mint' && $! == 33);
421
422     test !eval { open FOO, "> $foo" }, 'open for write';
423     test $@ =~ /^Insecure dependency/, $@;
424 }
425
426 # Commands to the system can't use tainted data
427 {
428     my $foo = $TAINT;
429
430     SKIP: {
431         skip "open('|') is not available", 4 if $^O eq 'amigaos';
432
433         test !eval { open FOO, "| x$foo" }, 'popen to';
434         test $@ =~ /^Insecure dependency/, $@;
435
436         test !eval { open FOO, "x$foo |" }, 'popen from';
437         test $@ =~ /^Insecure dependency/, $@;
438     }
439
440     test !eval { exec $TAINT }, 'exec';
441     test $@ =~ /^Insecure dependency/, $@;
442
443     test !eval { system $TAINT }, 'system';
444     test $@ =~ /^Insecure dependency/, $@;
445
446     $foo = "*";
447     taint_these $foo;
448
449     test !eval { `$echo 1$foo` }, 'backticks';
450     test $@ =~ /^Insecure dependency/, $@;
451
452     SKIP: {
453         # wildcard expansion doesn't invoke shell on VMS, so is safe
454         skip "This is not VMS", 2 unless $Is_VMS;
455     
456         test join('', eval { glob $foo } ) ne '', 'globbing';
457         test $@ eq '', $@;
458     }
459 }
460
461 # Operations which affect processes can't use tainted data.
462 {
463     test !eval { kill 0, $TAINT }, 'kill';
464     test $@ =~ /^Insecure dependency/, $@;
465
466     SKIP: {
467         skip "setpgrp() is not available", 2 unless $Config{d_setpgrp};
468
469         test !eval { setpgrp 0, $TAINT0 }, 'setpgrp';
470         test $@ =~ /^Insecure dependency/, $@;
471     }
472
473     SKIP: {
474         skip "setpriority() is not available", 2 unless $Config{d_setprior};
475
476         test !eval { setpriority 0, $TAINT0, $TAINT0 }, 'setpriority';
477         test $@ =~ /^Insecure dependency/, $@;
478     }
479 }
480
481 # Some miscellaneous operations can't use tainted data.
482 {
483     SKIP: {
484         skip "syscall() is not available", 2 unless $Config{d_syscall};
485
486         test !eval { syscall $TAINT }, 'syscall';
487         test $@ =~ /^Insecure dependency/, $@;
488     }
489
490     {
491         my $foo = "x" x 979;
492         taint_these $foo;
493         local *FOO;
494         my $temp = "./taintC$$";
495         END { unlink $temp }
496         test open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
497
498         test !eval { ioctl FOO, $TAINT0, $foo }, 'ioctl';
499         test $@ =~ /^Insecure dependency/, $@;
500
501         SKIP: {
502             skip "fcntl() is not available", 2 unless $Config{d_fcntl};
503
504             test !eval { fcntl FOO, $TAINT0, $foo }, 'fcntl';
505             test $@ =~ /^Insecure dependency/, $@;
506         }
507
508         close FOO;
509     }
510 }
511
512 # Some tests involving references
513 {
514     my $foo = 'abc' . $TAINT;
515     my $fooref = \$foo;
516     test not tainted $fooref;
517     test tainted $$fooref;
518     test tainted $foo;
519 }
520
521 # Some tests involving assignment
522 {
523     my $foo = $TAINT0;
524     my $bar = $foo;
525     test all_tainted $foo, $bar;
526     test tainted($foo = $bar);
527     test tainted($bar = $bar);
528     test tainted($bar += $bar);
529     test tainted($bar -= $bar);
530     test tainted($bar *= $bar);
531     test tainted($bar++);
532     test tainted($bar /= $bar);
533     test tainted($bar += 0);
534     test tainted($bar -= 2);
535     test tainted($bar *= -1);
536     test tainted($bar /= 1);
537     test tainted($bar--);
538     test $bar == 0;
539 }
540
541 # Test assignment and return of lists
542 {
543     my @foo = ("A", "tainted" . $TAINT, "B");
544     test not tainted $foo[0];
545     test     tainted $foo[1];
546     test not tainted $foo[2];
547     my @bar = @foo;
548     test not tainted $bar[0];
549     test     tainted $bar[1];
550     test not tainted $bar[2];
551     my @baz = eval { "A", "tainted" . $TAINT, "B" };
552     test not tainted $baz[0];
553     test     tainted $baz[1];
554     test not tainted $baz[2];
555     my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
556     test not tainted $plugh[0];
557     test     tainted $plugh[1];
558     test not tainted $plugh[2];
559     my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
560     test not tainted ((&$nautilus)[0]);
561     test     tainted ((&$nautilus)[1]);
562     test not tainted ((&$nautilus)[2]);
563     my @xyzzy = &$nautilus;
564     test not tainted $xyzzy[0];
565     test     tainted $xyzzy[1];
566     test not tainted $xyzzy[2];
567     my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
568     test not tainted ((&$red_october)[0]);
569     test     tainted ((&$red_october)[1]);
570     test not tainted ((&$red_october)[2]);
571     my @corge = &$red_october;
572     test not tainted $corge[0];
573     test     tainted $corge[1];
574     test not tainted $corge[2];
575 }
576
577 # Test for system/library calls returning string data of dubious origin.
578 {
579     # No reliable %Config check for getpw*
580     SKIP: {
581         skip "getpwent() is not available", 1 unless 
582           eval { setpwent(); getpwent() };
583
584         setpwent();
585         my @getpwent = getpwent();
586         die "getpwent: $!\n" unless (@getpwent);
587         test (    not tainted $getpwent[0]
588                   and     tainted $getpwent[1]
589                   and not tainted $getpwent[2]
590                   and not tainted $getpwent[3]
591                   and not tainted $getpwent[4]
592                   and not tainted $getpwent[5]
593                   and     tainted $getpwent[6]          # ge?cos
594                   and not tainted $getpwent[7]
595                   and     tainted $getpwent[8]);        # shell
596         endpwent();
597     }
598
599     SKIP: {
600         # pretty hard to imagine not
601         skip "readdir() is not available", 1 unless $Config{d_readdir};
602
603         local(*D);
604         opendir(D, "op") or die "opendir: $!\n";
605         my $readdir = readdir(D);
606         test tainted $readdir;
607         closedir(D);
608     }
609
610     SKIP: {
611         skip "readlink() or symlink() is not available" unless 
612           $Config{d_readlink} && $Config{d_symlink};
613
614         my $symlink = "sl$$";
615         unlink($symlink);
616         my $sl = "/something/naughty";
617         # it has to be a real path on Mac OS
618         $sl = MacPerl::MakePath((MacPerl::Volumes())[0]) if $Is_MacOS;
619         symlink($sl, $symlink) or die "symlink: $!\n";
620         my $readlink = readlink($symlink);
621         test tainted $readlink;
622         unlink($symlink);
623     }
624 }
625
626 # test bitwise ops (regression bug)
627 {
628     my $why = "y";
629     my $j = "x" | $why;
630     test not tainted $j;
631     $why = $TAINT."y";
632     $j = "x" | $why;
633     test     tainted $j;
634 }
635
636 # test target of substitution (regression bug)
637 {
638     my $why = $TAINT."y";
639     $why =~ s/y/z/;
640     test     tainted $why;
641
642     my $z = "[z]";
643     $why =~ s/$z/zee/;
644     test     tainted $why;
645
646     $why =~ s/e/'-'.$$/ge;
647     test     tainted $why;
648 }
649
650
651 SKIP: {
652     skip "no IPC::SysV", 2 unless $ipcsysv;
653
654     # test shmread
655     SKIP: {
656         skip "shm*() not available", 1 unless $Config{d_shm};
657
658         no strict 'subs';
659         my $sent = "foobar";
660         my $rcvd;
661         my $size = 2000;
662         my $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
663
664         if (defined $id) {
665             if (shmwrite($id, $sent, 0, 60)) {
666                 if (shmread($id, $rcvd, 0, 60)) {
667                     substr($rcvd, index($rcvd, "\0")) = '';
668                 } else {
669                     warn "# shmread failed: $!\n";
670                 }
671             } else {
672                 warn "# shmwrite failed: $!\n";
673             }
674             shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
675         } else {
676             warn "# shmget failed: $!\n";
677         }
678
679         skip "SysV shared memory operation failed", 1 unless 
680           $rcvd eq $sent;
681
682         test tainted $rcvd;
683     }
684
685
686     # test msgrcv
687     SKIP: {
688         skip "msg*() not available", 1 unless $Config{d_msg};
689
690         no strict 'subs';
691         my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
692
693         my $sent      = "message";
694         my $type_sent = 1234;
695         my $rcvd;
696         my $type_rcvd;
697
698         if (defined $id) {
699             if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) {
700                 if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) {
701                     ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
702                 } else {
703                     warn "# msgrcv failed: $!\n";
704                 }
705             } else {
706                 warn "# msgsnd failed: $!\n";
707             }
708             msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
709         } else {
710             warn "# msgget failed\n";
711         }
712
713         SKIP: {
714             skip "SysV message queue operation failed", 1
715               unless $rcvd eq $sent && $type_sent == $type_rcvd;
716
717             test tainted $rcvd;
718         }
719     }
720 }
721
722 {
723     # bug id 20001004.006
724
725     open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
726     local $/;
727     my $a = <IN>;
728     my $b = <IN>;
729
730     ok tainted($a) && tainted($b) && !defined($b);
731
732     close IN;
733 }
734
735 {
736     # bug id 20001004.007
737
738     open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
739     my $a = <IN>;
740
741     my $c = { a => 42,
742               b => $a };
743
744     ok !tainted($c->{a}) && tainted($c->{b});
745
746
747     my $d = { a => $a,
748               b => 42 };
749     ok tainted($d->{a}) && !tainted($d->{b});
750
751
752     my $e = { a => 42,
753               b => { c => $a, d => 42 } };
754     ok !tainted($e->{a}) &&
755        !tainted($e->{b}) &&
756         tainted($e->{b}->{c}) &&
757        !tainted($e->{b}->{d});
758
759     close IN;
760 }
761
762 {
763     # bug id 20010519.003
764
765     BEGIN {
766         use vars qw($has_fcntl);
767         eval { require Fcntl; import Fcntl; };
768         unless ($@) {
769             $has_fcntl = 1;
770         }
771     }
772
773     SKIP: {
774         skip "no Fcntl", 18 unless $has_fcntl;
775
776         my $evil = "foo" . $TAINT;
777
778         eval { sysopen(my $ro, $evil, &O_RDONLY) };
779         test $@ !~ /^Insecure dependency/, $@;
780         
781         eval { sysopen(my $wo, $evil, &O_WRONLY) };
782         test $@ =~ /^Insecure dependency/, $@;
783         
784         eval { sysopen(my $rw, $evil, &O_RDWR) };
785         test $@ =~ /^Insecure dependency/, $@;
786         
787         eval { sysopen(my $ap, $evil, &O_APPEND) };
788         test $@ =~ /^Insecure dependency/, $@;
789         
790         eval { sysopen(my $cr, $evil, &O_CREAT) };
791         test $@ =~ /^Insecure dependency/, $@;
792         
793         eval { sysopen(my $tr, $evil, &O_TRUNC) };
794         test $@ =~ /^Insecure dependency/, $@;
795         
796         eval { sysopen(my $ro, "foo", &O_RDONLY | $TAINT0) };
797         test $@ !~ /^Insecure dependency/, $@;
798         
799         eval { sysopen(my $wo, "foo", &O_WRONLY | $TAINT0) };
800         test $@ =~ /^Insecure dependency/, $@;
801
802         eval { sysopen(my $rw, "foo", &O_RDWR | $TAINT0) };
803         test $@ =~ /^Insecure dependency/, $@;
804
805         eval { sysopen(my $ap, "foo", &O_APPEND | $TAINT0) };
806         test $@ =~ /^Insecure dependency/, $@;
807         
808         eval { sysopen(my $cr, "foo", &O_CREAT | $TAINT0) };
809         test $@ =~ /^Insecure dependency/, $@;
810
811         eval { sysopen(my $tr, "foo", &O_TRUNC | $TAINT0) };
812         test $@ =~ /^Insecure dependency/, $@;
813
814         eval { sysopen(my $ro, "foo", &O_RDONLY, $TAINT0) };
815         test $@ !~ /^Insecure dependency/, $@;
816         
817         eval { sysopen(my $wo, "foo", &O_WRONLY, $TAINT0) };
818         test $@ =~ /^Insecure dependency/, $@;
819         
820         eval { sysopen(my $rw, "foo", &O_RDWR, $TAINT0) };
821         test $@ =~ /^Insecure dependency/, $@;
822         
823         eval { sysopen(my $ap, "foo", &O_APPEND, $TAINT0) };
824         test $@ =~ /^Insecure dependency/, $@;
825         
826         eval { sysopen(my $cr, "foo", &O_CREAT, $TAINT0) };
827         test $@ =~ /^Insecure dependency/, $@;
828
829         eval { sysopen(my $tr, "foo", &O_TRUNC, $TAINT0) };
830         test $@ =~ /^Insecure dependency/, $@;
831         
832         unlink("foo"); # not unlink($evil), because that would fail...
833     }
834 }
835
836 {
837     # bug 20010526.004
838
839     use warnings;
840
841     my $saw_warning = 0;
842     local $SIG{__WARN__} = sub { $saw_warning = 1 };
843
844     sub fmi {
845         my $divnum = shift()/1;
846         sprintf("%1.1f\n", $divnum);
847     }
848
849     fmi(21 . $TAINT);
850     fmi(37);
851     fmi(248);
852
853     test !$saw_warning;
854 }
855
856
857 {
858     # Bug ID 20010730.010
859
860     my $i = 0;
861
862     sub Tie::TIESCALAR {
863         my $class =  shift;
864         my $arg   =  shift;
865
866         bless \$arg => $class;
867     }
868
869     sub Tie::FETCH {
870         $i ++;
871         ${$_ [0]}
872     }
873
874  
875     package main;
876  
877     my $bar = "The Big Bright Green Pleasure Machine";
878     taint_these $bar;
879     tie my ($foo), Tie => $bar;
880
881     my $baz = $foo;
882
883     ok $i == 1;
884 }
885
886 {
887     # Check that all environment variables are tainted.
888     my @untainted;
889     while (my ($k, $v) = each %ENV) {
890         if (!tainted($v) &&
891             # These we have explicitly untainted or set earlier.
892             $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) {
893             push @untainted, "# '$k' = '$v'\n";
894         }
895     }
896     test @untainted == 0, "untainted:\n @untainted";
897 }
898
899
900 ok( ${^TAINT} == 1, '$^TAINT is on' );
901
902 eval { ${^TAINT} = 0 };
903 ok( ${^TAINT},  '$^TAINT is not assignable' );
904 ok( $@ =~ /^Modification of a read-only value attempted/,
905                                 'Assigning to ${^TAINT} fails' );
906
907 {
908     # bug 20011111.105
909     
910     my $re1 = qr/x$TAINT/;
911     test tainted $re1;
912     
913     my $re2 = qr/^$re1\z/;
914     test tainted $re2;
915     
916     my $re3 = "$re2";
917     test tainted $re3;
918 }
919
920 SKIP: {
921     skip "system {} has different semantics on Win32", 1 if $Is_MSWin32;
922
923     # bug 20010221.005
924     local $ENV{PATH} .= $TAINT;
925     eval { system { "echo" } "/arg0", "arg1" };
926     test $@ =~ /^Insecure \$ENV/;
927 }
928
929 TODO: {
930     todo_skip 'tainted %ENV warning occludes tainted arguments warning', 22
931       if $Is_VMS;
932
933     # bug 20020208.005 plus some single arg exec/system extras
934     my $err = qr/^Insecure dependency/ ;
935     test !eval { exec $TAINT, $TAINT }, 'exec';
936     test $@ =~ $err, $@;
937     test !eval { exec $TAINT $TAINT }, 'exec';
938     test $@ =~ $err, $@;
939     test !eval { exec $TAINT $TAINT, $TAINT }, 'exec';
940     test $@ =~ $err, $@;
941     test !eval { exec $TAINT 'notaint' }, 'exec';
942     test $@ =~ $err, $@;
943     test !eval { exec {'notaint'} $TAINT }, 'exec';
944     test $@ =~ $err, $@;
945
946     test !eval { system $TAINT, $TAINT }, 'system';
947     test $@ =~ $err, $@;
948     test !eval { system $TAINT $TAINT }, 'system';
949     test $@ =~ $err, $@;
950     test !eval { system $TAINT $TAINT, $TAINT }, 'system';
951     test $@ =~ $err, $@;
952     test !eval { system $TAINT 'notaint' }, 'system';
953     test $@ =~ $err, $@;
954     test !eval { system {'notaint'} $TAINT }, 'system';
955     test $@ =~ $err, $@;
956
957     eval { 
958         no warnings;
959         system("lskdfj does not exist","with","args"); 
960     };
961     test !$@;
962
963     SKIP: {
964         skip "no exec() on MacOS Classic" if $Is_MacOS;
965
966         eval { 
967             no warnings;
968             exec("lskdfj does not exist","with","args"); 
969         };
970         test !$@;
971     }
972
973     # If you add tests here update also the above skip block for VMS.
974 }
975
976 {
977     # [ID 20020704.001] taint propagation failure
978     use re 'taint';
979     $TAINT =~ /(.*)/;
980     test tainted(my $foo = $1);
981 }
982
983 {
984     # [perl #24291] this used to dump core
985     our %nonmagicalenv = ( PATH => "util" );
986     local *ENV = \%nonmagicalenv;
987     eval { system("lskdfj"); };
988     test $@ =~ /^%ENV is aliased to another variable while running with -T switch/;
989     local *ENV = *nonmagicalenv;
990     eval { system("lskdfj"); };
991     test $@ =~ /^%ENV is aliased to %nonmagicalenv while running with -T switch/;
992 }
993 {
994     # [perl #24248]
995     $TAINT =~ /(.*)/;
996     test !tainted($1);
997     my $notaint = $1;
998     test !tainted($notaint);
999
1000     my $l;
1001     $notaint =~ /($notaint)/;
1002     $l = $1;
1003     test !tainted($1);
1004     test !tainted($l);
1005     $notaint =~ /($TAINT)/;
1006     $l = $1;
1007     test tainted($1);
1008     test tainted($l);
1009
1010     $TAINT =~ /($notaint)/;
1011     $l = $1;
1012     test !tainted($1);
1013     test !tainted($l);
1014     $TAINT =~ /($TAINT)/;
1015     $l = $1;
1016     test tainted($1);
1017     test tainted($l);
1018
1019     my $r;
1020     ($r = $TAINT) =~ /($notaint)/;
1021     test !tainted($1);
1022     ($r = $TAINT) =~ /($TAINT)/;
1023     test tainted($1);
1024
1025     #  [perl #24674]
1026     # accessing $^O  shoudn't taint it as a side-effect;
1027     # assigning tainted data to it is now an error
1028
1029     test !tainted($^O);
1030     if (!$^X) { } elsif ($^O eq 'bar') { }
1031     test !tainted($^O);
1032     eval '$^O = $^X';
1033     test $@ =~ /Insecure dependency in/;
1034 }
1035
1036 EFFECTIVELY_CONSTANTS: {
1037     my $tainted_number = 12 + $TAINT0;
1038     test tainted( $tainted_number );
1039
1040     # Even though it's always 0, it's still tainted
1041     my $tainted_product = $tainted_number * 0;
1042     test tainted( $tainted_product );
1043     test $tainted_product == 0;
1044 }
1045
1046 TERNARY_CONDITIONALS: {
1047     my $tainted_true  = $TAINT . "blah blah blah";
1048     my $tainted_false = $TAINT0;
1049     test tainted( $tainted_true );
1050     test tainted( $tainted_false );
1051
1052     my $result = $tainted_true ? "True" : "False";
1053     test $result eq "True";
1054     test !tainted( $result );
1055
1056     $result = $tainted_false ? "True" : "False";
1057     test $result eq "False";
1058     test !tainted( $result );
1059
1060     my $untainted_whatever = "The Fabulous Johnny Cash";
1061     my $tainted_whatever = "Soft Cell" . $TAINT;
1062
1063     $result = $tainted_true ? $tainted_whatever : $untainted_whatever;
1064     test $result eq "Soft Cell";
1065     test tainted( $result );
1066
1067     $result = $tainted_false ? $tainted_whatever : $untainted_whatever;
1068     test $result eq "The Fabulous Johnny Cash";
1069     test !tainted( $result );
1070 }
1071
1072 {
1073     # rt.perl.org 5900  $1 remains tainted if...
1074     # 1) The regular expression contains a scalar variable AND
1075     # 2) The regular expression appears in an elsif clause
1076
1077     my $foo = "abcdefghi" . $TAINT;
1078
1079     my $valid_chars = 'a-z';
1080     if ( $foo eq '' ) {
1081     }
1082     elsif ( $foo =~ /([$valid_chars]+)/o ) {
1083         test not tainted $1;
1084     }
1085
1086     if ( $foo eq '' ) {
1087     }
1088     elsif ( my @bar = $foo =~ /([$valid_chars]+)/o ) {
1089         test not any_tainted @bar;
1090     }
1091 }