3 # Taint tests by Tom Phoenix <rootbeer@teleport.com>.
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?
17 use File::Spec::Functions;
19 BEGIN { require './test.pl'; }
24 use vars qw($ipcsysv); # did we manage to load IPC::SysV?
27 if ($^O eq 'VMS' && !defined($Config{d_setenv})) {
28 $ENV{PATH} = $ENV{PATH};
29 $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy';
31 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/
32 && ($Config{d_shm} || $Config{d_msg})) {
33 eval { require IPC::SysV };
36 IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU IPC_NOWAIT));
41 my $Is_MacOS = $^O eq 'MacOS';
42 my $Is_VMS = $^O eq 'VMS';
43 my $Is_MSWin32 = $^O eq 'MSWin32';
44 my $Is_NetWare = $^O eq 'NetWare';
45 my $Is_Dos = $^O eq 'dos';
46 my $Is_Cygwin = $^O eq 'cygwin';
47 my $Is_OpenBSD = $^O eq 'openbsd';
48 my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' :
49 $Is_MSWin32 ? '.\perl' :
51 $Is_NetWare ? 'perl' :
53 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
57 for $x ('DCL$PATH', @MoreEnv) {
58 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
62 \$ENV{PATH} = '' if $Config{d_setenv};
63 warn "# Note: logical name 'PATH' may have been deleted\n";
64 \@ENV{keys %old} = values %old;
70 # The empty tainted value, for tainting strings
71 my $TAINT = substr($^X, 0, 0);
72 # A tainted zero, useful for tainting numbers
79 # This taints each argument passed. All must be lvalues.
80 # Side effect: It also stringifies them. :-(
82 for (@_) { $_ .= $TAINT }
85 # How to identify taint when you see it
87 not eval { join("",@_), kill 0; 1 };
93 for (@_) { return 0 unless tainted $_ }
101 my $curr_test = curr_test();
104 print "ok $curr_test\n";
106 print "not ok $curr_test\n";
107 printf "# Failed test at line %d\n", (caller)[2];
108 for (split m/^/m, $diag) {
113 or substr($diag, -1) eq "\n";
121 # We need an external program to call.
122 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : $Is_MacOS ? ":echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$"));
124 open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
125 print PROG 'print "@ARGV\n"', "\n";
127 my $echo = "$Invoke_Perl $ECHO";
129 my $TEST = catfile(curdir(), 'TEST');
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.
135 $ENV{'DCL$PATH'} = '' if $Is_VMS;
137 if ($Is_MSWin32 && $Config{ccname} =~ /bcc32/ && ! -f 'cc3250mt.dll') {
139 foreach my $dir (split /$Config{path_sep}/, $ENV{PATH}) {
140 if (-f "$dir/cc3250mt.dll") {
141 $bcc_dir = $dir and last;
144 if (defined $bcc_dir) {
146 File::Copy::copy("$bcc_dir/cc3250mt.dll", '.') or
147 die "$0: failed to copy cc3250mt.dll: $!\n";
149 END { unlink "cc3250mt.dll" }
155 delete @ENV{@MoreEnv};
158 test eval { `$echo 1` } eq "1\n";
161 skip "Environment tainting tests skipped", 4
162 if $Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos || $Is_MacOS;
164 my @vars = ('PATH', @MoreEnv);
165 while (my $v = $vars[0]) {
166 local $ENV{$v} = $TAINT;
167 last if eval { `$echo 1` };
168 last unless $@ =~ /^Insecure \$ENV{$v}/;
171 test !@vars, "@vars";
173 # tainted $TERM is unsafe only if it contains metachars
175 $ENV{TERM} = 'e=mc2';
176 test eval { `$echo 1` } eq "1\n";
177 $ENV{TERM} = 'e=mc2' . $TAINT;
178 test !eval { `$echo 1` };
179 test $@ =~ /^Insecure \$ENV{TERM}/, $@;
183 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) {
184 print "# all directories are writeable\n";
187 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
188 qw(sys$scratch /tmp /var/tmp /usr/tmp),
189 @ENV{qw(TMP TEMP)})[0]
190 or print "# can't find world-writeable directory to test PATH\n";
194 skip "all directories are writeable", 2 unless $tmp;
196 local $ENV{PATH} = $tmp;
197 test !eval { `$echo 1` };
198 test $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
202 skip "This is not VMS", 4 unless $Is_VMS;
204 $ENV{'DCL$PATH'} = $TAINT;
205 test eval { `$echo 1` } eq '';
206 test $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
208 skip q[can't find world-writeable directory to test DCL$PATH], 2
211 $ENV{'DCL$PATH'} = $tmp;
212 test eval { `$echo 1` } eq '';
213 test $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
215 $ENV{'DCL$PATH'} = '';
219 # Let's see that we can taint and untaint as needed.
224 # That was a sanity check. If it failed, stop the insanity!
225 die "Taint checks don't seem to be enabled" unless tainted $foo;
228 test not tainted $foo;
234 test not any_tainted @list;
235 taint_these @list[1,3,5,7,9];
236 test any_tainted @list;
237 test all_tainted @list[1,3,5,7,9];
238 test not any_tainted @list[0,2,4,6,8];
240 ($foo) = $foo =~ /(.+)/;
241 test not tainted $foo;
243 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
244 test not tainted $foo;
250 ($foo) = ('bar' . $TAINT) =~ /(.+)/;
254 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
259 $foo = $1 if 'bar' =~ /(.+)$TAINT/;
263 my $pi = 4 * atan2(1,1) + $TAINT0;
266 ($pi) = $pi =~ /(\d+\.\d+)/;
267 test not tainted $pi;
268 test sprintf("%.5f", $pi) eq '3.14159';
271 # How about command-line arguments? The problem is that we don't
272 # always get some, so we'll run another process with some.
274 my $arg = catfile(curdir(), "arg$$");
275 open PROG, "> $arg" or die "Can't create $arg: $!";
277 eval { join('', @ARGV), kill 0 };
278 exit 0 if $@ =~ /^Insecure dependency/;
279 print "# Oops: \$@ was [$@]\n";
283 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
284 test !$?, "Exited with status $?";
288 # Reading from a file should be tainted
290 test open(FILE, $TEST), "Couldn't open '$TEST': $!";
293 sysread(FILE, $block, 100);
300 # Globs should be forbidden, except under VMS,
301 # which doesn't spawn an external program.
303 skip "globs should be forbidden", 2 if 1 or $Is_VMS;
305 my @globs = eval { <*> };
306 test @globs == 0 && $@ =~ /^Insecure dependency/;
308 @globs = eval { glob '*' };
309 test @globs == 0 && $@ =~ /^Insecure dependency/;
312 # Output of commands should be tainted
314 my $foo = `$echo abc`;
318 # Certain system variables should be tainted
320 test all_tainted $^X, $0;
323 # Results of matching should all be untainted
325 my $foo = "abcdefghi" . $TAINT;
329 test not any_tainted $`, $&, $';
331 $foo =~ /(...)(...)(...)/;
332 test not any_tainted $1, $2, $3, $+;
334 my @bar = $foo =~ /(...)(...)(...)/;
335 test not any_tainted @bar;
337 test tainted $foo; # $foo should still be tainted!
338 test $foo eq "abcdefghi";
341 # Operations which affect files can't use tainted data.
343 test !eval { chmod 0, $TAINT }, 'chmod';
344 test $@ =~ /^Insecure dependency/, $@;
346 # There is no feature test in $Config{} for truncate,
347 # so we allow for the possibility that it's missing.
348 test !eval { truncate 'NoSuChFiLe', $TAINT0 }, 'truncate';
349 test $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
351 test !eval { rename '', $TAINT }, 'rename';
352 test $@ =~ /^Insecure dependency/, $@;
354 test !eval { unlink $TAINT }, 'unlink';
355 test $@ =~ /^Insecure dependency/, $@;
357 test !eval { utime $TAINT }, 'utime';
358 test $@ =~ /^Insecure dependency/, $@;
361 skip "chown() is not available", 2 unless $Config{d_chown};
363 test !eval { chown -1, -1, $TAINT }, 'chown';
364 test $@ =~ /^Insecure dependency/, $@;
368 skip "link() is not available", 2 unless $Config{d_link};
370 test !eval { link $TAINT, '' }, 'link';
371 test $@ =~ /^Insecure dependency/, $@;
375 skip "symlink() is not available", 2 unless $Config{d_symlink};
377 test !eval { symlink $TAINT, '' }, 'symlink';
378 test $@ =~ /^Insecure dependency/, $@;
382 # Operations which affect directories can't use tainted data.
384 test !eval { mkdir "foo".$TAINT, 0755.$TAINT0 }, 'mkdir';
385 test $@ =~ /^Insecure dependency/, $@;
387 test !eval { rmdir $TAINT }, 'rmdir';
388 test $@ =~ /^Insecure dependency/, $@;
390 test !eval { chdir "foo".$TAINT }, 'chdir';
391 test $@ =~ /^Insecure dependency/, $@;
394 skip "chroot() is not available", 2 unless $Config{d_chroot};
396 test !eval { chroot $TAINT }, 'chroot';
397 test $@ =~ /^Insecure dependency/, $@;
401 # Some operations using files can't use tainted data.
403 my $foo = "imaginary library" . $TAINT;
404 test !eval { require $foo }, 'require';
405 test $@ =~ /^Insecure dependency/, $@;
407 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
408 END { unlink $filename if defined $filename }
409 $foo = $filename . $TAINT;
410 unlink $filename; # in any case
412 test !eval { open FOO, $foo }, 'open for read';
413 test $@ eq '', $@; # NB: This should be allowed
415 # Try first new style but allow also old style.
416 # We do not want the whole taint.t to fail
417 # just because Errno possibly failing.
418 test eval('$!{ENOENT}') ||
419 $! == 2 || # File not found
420 ($Is_Dos && $! == 22) ||
421 ($^O eq 'mint' && $! == 33);
423 test !eval { open FOO, "> $foo" }, 'open for write';
424 test $@ =~ /^Insecure dependency/, $@;
427 # Commands to the system can't use tainted data
432 skip "open('|') is not available", 4 if $^O eq 'amigaos';
434 test !eval { open FOO, "| x$foo" }, 'popen to';
435 test $@ =~ /^Insecure dependency/, $@;
437 test !eval { open FOO, "x$foo |" }, 'popen from';
438 test $@ =~ /^Insecure dependency/, $@;
441 test !eval { exec $TAINT }, 'exec';
442 test $@ =~ /^Insecure dependency/, $@;
444 test !eval { system $TAINT }, 'system';
445 test $@ =~ /^Insecure dependency/, $@;
450 test !eval { `$echo 1$foo` }, 'backticks';
451 test $@ =~ /^Insecure dependency/, $@;
454 # wildcard expansion doesn't invoke shell on VMS, so is safe
455 skip "This is not VMS", 2 unless $Is_VMS;
457 test join('', eval { glob $foo } ) ne '', 'globbing';
462 # Operations which affect processes can't use tainted data.
464 test !eval { kill 0, $TAINT }, 'kill';
465 test $@ =~ /^Insecure dependency/, $@;
468 skip "setpgrp() is not available", 2 unless $Config{d_setpgrp};
470 test !eval { setpgrp 0, $TAINT0 }, 'setpgrp';
471 test $@ =~ /^Insecure dependency/, $@;
475 skip "setpriority() is not available", 2 unless $Config{d_setprior};
477 test !eval { setpriority 0, $TAINT0, $TAINT0 }, 'setpriority';
478 test $@ =~ /^Insecure dependency/, $@;
482 # Some miscellaneous operations can't use tainted data.
485 skip "syscall() is not available", 2 unless $Config{d_syscall};
487 test !eval { syscall $TAINT }, 'syscall';
488 test $@ =~ /^Insecure dependency/, $@;
495 my $temp = "./taintC$$";
497 test open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
499 test !eval { ioctl FOO, $TAINT0, $foo }, 'ioctl';
500 test $@ =~ /^Insecure dependency/, $@;
503 skip "fcntl() is not available", 2 unless $Config{d_fcntl};
505 test !eval { fcntl FOO, $TAINT0, $foo }, 'fcntl';
506 test $@ =~ /^Insecure dependency/, $@;
513 # Some tests involving references
515 my $foo = 'abc' . $TAINT;
517 test not tainted $fooref;
518 test tainted $$fooref;
522 # Some tests involving assignment
526 test all_tainted $foo, $bar;
527 test tainted($foo = $bar);
528 test tainted($bar = $bar);
529 test tainted($bar += $bar);
530 test tainted($bar -= $bar);
531 test tainted($bar *= $bar);
532 test tainted($bar++);
533 test tainted($bar /= $bar);
534 test tainted($bar += 0);
535 test tainted($bar -= 2);
536 test tainted($bar *= -1);
537 test tainted($bar /= 1);
538 test tainted($bar--);
542 # Test assignment and return of lists
544 my @foo = ("A", "tainted" . $TAINT, "B");
545 test not tainted $foo[0];
546 test tainted $foo[1];
547 test not tainted $foo[2];
549 test not tainted $bar[0];
550 test tainted $bar[1];
551 test not tainted $bar[2];
552 my @baz = eval { "A", "tainted" . $TAINT, "B" };
553 test not tainted $baz[0];
554 test tainted $baz[1];
555 test not tainted $baz[2];
556 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
557 test not tainted $plugh[0];
558 test tainted $plugh[1];
559 test not tainted $plugh[2];
560 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
561 test not tainted ((&$nautilus)[0]);
562 test tainted ((&$nautilus)[1]);
563 test not tainted ((&$nautilus)[2]);
564 my @xyzzy = &$nautilus;
565 test not tainted $xyzzy[0];
566 test tainted $xyzzy[1];
567 test not tainted $xyzzy[2];
568 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
569 test not tainted ((&$red_october)[0]);
570 test tainted ((&$red_october)[1]);
571 test not tainted ((&$red_october)[2]);
572 my @corge = &$red_october;
573 test not tainted $corge[0];
574 test tainted $corge[1];
575 test not tainted $corge[2];
578 # Test for system/library calls returning string data of dubious origin.
580 # No reliable %Config check for getpw*
582 skip "getpwent() is not available", 1 unless
583 eval { setpwent(); getpwent() };
586 my @getpwent = getpwent();
587 die "getpwent: $!\n" unless (@getpwent);
588 test ( not tainted $getpwent[0]
589 and tainted $getpwent[1]
590 and not tainted $getpwent[2]
591 and not tainted $getpwent[3]
592 and not tainted $getpwent[4]
593 and not tainted $getpwent[5]
594 and tainted $getpwent[6] # ge?cos
595 and not tainted $getpwent[7]
596 and tainted $getpwent[8]); # shell
601 # pretty hard to imagine not
602 skip "readdir() is not available", 1 unless $Config{d_readdir};
605 opendir(D, "op") or die "opendir: $!\n";
606 my $readdir = readdir(D);
607 test tainted $readdir;
612 skip "readlink() or symlink() is not available" unless
613 $Config{d_readlink} && $Config{d_symlink};
615 my $symlink = "sl$$";
617 my $sl = "/something/naughty";
618 # it has to be a real path on Mac OS
619 $sl = MacPerl::MakePath((MacPerl::Volumes())[0]) if $Is_MacOS;
620 symlink($sl, $symlink) or die "symlink: $!\n";
621 my $readlink = readlink($symlink);
622 test tainted $readlink;
627 # test bitwise ops (regression bug)
637 # test target of substitution (regression bug)
639 my $why = $TAINT."y";
647 $why =~ s/e/'-'.$$/ge;
653 skip "no IPC::SysV", 2 unless $ipcsysv;
657 skip "shm*() not available", 1 unless $Config{d_shm};
663 my $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
666 if (shmwrite($id, $sent, 0, 60)) {
667 if (shmread($id, $rcvd, 0, 60)) {
668 substr($rcvd, index($rcvd, "\0")) = '';
670 warn "# shmread failed: $!\n";
673 warn "# shmwrite failed: $!\n";
675 shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
677 warn "# shmget failed: $!\n";
680 skip "SysV shared memory operation failed", 1 unless
689 skip "msg*() not available", 1 unless $Config{d_msg};
692 my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
694 my $sent = "message";
695 my $type_sent = 1234;
700 if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) {
701 if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) {
702 ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
704 warn "# msgrcv failed: $!\n";
707 warn "# msgsnd failed: $!\n";
709 msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
711 warn "# msgget failed\n";
715 skip "SysV message queue operation failed", 1
716 unless $rcvd eq $sent && $type_sent == $type_rcvd;
724 # bug id 20001004.006
726 open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
731 ok tainted($a) && tainted($b) && !defined($b);
737 # bug id 20001004.007
739 open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
745 ok !tainted($c->{a}) && tainted($c->{b});
750 ok tainted($d->{a}) && !tainted($d->{b});
754 b => { c => $a, d => 42 } };
755 ok !tainted($e->{a}) &&
757 tainted($e->{b}->{c}) &&
758 !tainted($e->{b}->{d});
764 # bug id 20010519.003
767 use vars qw($has_fcntl);
768 eval { require Fcntl; import Fcntl; };
775 skip "no Fcntl", 18 unless $has_fcntl;
777 my $evil = "foo" . $TAINT;
779 eval { sysopen(my $ro, $evil, &O_RDONLY) };
780 test $@ !~ /^Insecure dependency/, $@;
782 eval { sysopen(my $wo, $evil, &O_WRONLY) };
783 test $@ =~ /^Insecure dependency/, $@;
785 eval { sysopen(my $rw, $evil, &O_RDWR) };
786 test $@ =~ /^Insecure dependency/, $@;
788 eval { sysopen(my $ap, $evil, &O_APPEND) };
789 test $@ =~ /^Insecure dependency/, $@;
791 eval { sysopen(my $cr, $evil, &O_CREAT) };
792 test $@ =~ /^Insecure dependency/, $@;
794 eval { sysopen(my $tr, $evil, &O_TRUNC) };
795 test $@ =~ /^Insecure dependency/, $@;
797 eval { sysopen(my $ro, "foo", &O_RDONLY | $TAINT0) };
798 test $@ !~ /^Insecure dependency/, $@;
800 eval { sysopen(my $wo, "foo", &O_WRONLY | $TAINT0) };
801 test $@ =~ /^Insecure dependency/, $@;
803 eval { sysopen(my $rw, "foo", &O_RDWR | $TAINT0) };
804 test $@ =~ /^Insecure dependency/, $@;
806 eval { sysopen(my $ap, "foo", &O_APPEND | $TAINT0) };
807 test $@ =~ /^Insecure dependency/, $@;
809 eval { sysopen(my $cr, "foo", &O_CREAT | $TAINT0) };
810 test $@ =~ /^Insecure dependency/, $@;
812 eval { sysopen(my $tr, "foo", &O_TRUNC | $TAINT0) };
813 test $@ =~ /^Insecure dependency/, $@;
815 eval { sysopen(my $ro, "foo", &O_RDONLY, $TAINT0) };
816 test $@ !~ /^Insecure dependency/, $@;
818 eval { sysopen(my $wo, "foo", &O_WRONLY, $TAINT0) };
819 test $@ =~ /^Insecure dependency/, $@;
821 eval { sysopen(my $rw, "foo", &O_RDWR, $TAINT0) };
822 test $@ =~ /^Insecure dependency/, $@;
824 eval { sysopen(my $ap, "foo", &O_APPEND, $TAINT0) };
825 test $@ =~ /^Insecure dependency/, $@;
827 eval { sysopen(my $cr, "foo", &O_CREAT, $TAINT0) };
828 test $@ =~ /^Insecure dependency/, $@;
830 eval { sysopen(my $tr, "foo", &O_TRUNC, $TAINT0) };
831 test $@ =~ /^Insecure dependency/, $@;
833 unlink("foo"); # not unlink($evil), because that would fail...
843 local $SIG{__WARN__} = sub { $saw_warning = 1 };
846 my $divnum = shift()/1;
847 sprintf("%1.1f\n", $divnum);
859 # Bug ID 20010730.010
867 bless \$arg => $class;
878 my $bar = "The Big Bright Green Pleasure Machine";
880 tie my ($foo), Tie => $bar;
888 # Check that all environment variables are tainted.
890 while (my ($k, $v) = each %ENV) {
892 # These we have explicitly untainted or set earlier.
893 $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) {
894 push @untainted, "# '$k' = '$v'\n";
897 test @untainted == 0, "untainted:\n @untainted";
901 ok( ${^TAINT} == 1, '$^TAINT is on' );
903 eval { ${^TAINT} = 0 };
904 ok( ${^TAINT}, '$^TAINT is not assignable' );
905 ok( $@ =~ /^Modification of a read-only value attempted/,
906 'Assigning to ${^TAINT} fails' );
911 my $re1 = qr/x$TAINT/;
914 my $re2 = qr/^$re1\z/;
922 skip "system {} has different semantics on Win32", 1 if $Is_MSWin32;
925 local $ENV{PATH} .= $TAINT;
926 eval { system { "echo" } "/arg0", "arg1" };
927 test $@ =~ /^Insecure \$ENV/;
931 todo_skip 'tainted %ENV warning occludes tainted arguments warning', 22
934 # bug 20020208.005 plus some single arg exec/system extras
935 my $err = qr/^Insecure dependency/ ;
936 test !eval { exec $TAINT, $TAINT }, 'exec';
938 test !eval { exec $TAINT $TAINT }, 'exec';
940 test !eval { exec $TAINT $TAINT, $TAINT }, 'exec';
942 test !eval { exec $TAINT 'notaint' }, 'exec';
944 test !eval { exec {'notaint'} $TAINT }, 'exec';
947 test !eval { system $TAINT, $TAINT }, 'system';
949 test !eval { system $TAINT $TAINT }, 'system';
951 test !eval { system $TAINT $TAINT, $TAINT }, 'system';
953 test !eval { system $TAINT 'notaint' }, 'system';
955 test !eval { system {'notaint'} $TAINT }, 'system';
960 system("lskdfj does not exist","with","args");
965 skip "no exec() on MacOS Classic" if $Is_MacOS;
969 exec("lskdfj does not exist","with","args");
974 # If you add tests here update also the above skip block for VMS.
978 # [ID 20020704.001] taint propagation failure
981 test tainted(my $foo = $1);
985 # [perl #24291] this used to dump core
986 our %nonmagicalenv = ( PATH => "util" );
987 local *ENV = \%nonmagicalenv;
988 eval { system("lskdfj"); };
989 test $@ =~ /^%ENV is aliased to another variable while running with -T switch/;
990 local *ENV = *nonmagicalenv;
991 eval { system("lskdfj"); };
992 test $@ =~ /^%ENV is aliased to %nonmagicalenv while running with -T switch/;
999 test !tainted($notaint);
1002 $notaint =~ /($notaint)/;
1006 $notaint =~ /($TAINT)/;
1011 $TAINT =~ /($notaint)/;
1015 $TAINT =~ /($TAINT)/;
1021 ($r = $TAINT) =~ /($notaint)/;
1023 ($r = $TAINT) =~ /($TAINT)/;
1027 # accessing $^O shoudn't taint it as a side-effect;
1028 # assigning tainted data to it is now an error
1031 if (!$^X) { } elsif ($^O eq 'bar') { }
1034 test $@ =~ /Insecure dependency in/;
1037 EFFECTIVELY_CONSTANTS: {
1038 my $tainted_number = 12 + $TAINT0;
1039 test tainted( $tainted_number );
1041 # Even though it's always 0, it's still tainted
1042 my $tainted_product = $tainted_number * 0;
1043 test tainted( $tainted_product );
1044 test $tainted_product == 0;
1047 TERNARY_CONDITIONALS: {
1048 my $tainted_true = $TAINT . "blah blah blah";
1049 my $tainted_false = $TAINT0;
1050 test tainted( $tainted_true );
1051 test tainted( $tainted_false );
1053 my $result = $tainted_true ? "True" : "False";
1054 test $result eq "True";
1055 test !tainted( $result );
1057 $result = $tainted_false ? "True" : "False";
1058 test $result eq "False";
1059 test !tainted( $result );
1061 my $untainted_whatever = "The Fabulous Johnny Cash";
1062 my $tainted_whatever = "Soft Cell" . $TAINT;
1064 $result = $tainted_true ? $tainted_whatever : $untainted_whatever;
1065 test $result eq "Soft Cell";
1066 test tainted( $result );
1068 $result = $tainted_false ? $tainted_whatever : $untainted_whatever;
1069 test $result eq "The Fabulous Johnny Cash";
1070 test !tainted( $result );
1074 # rt.perl.org 5900 $1 remains tainted if...
1075 # 1) The regular expression contains a scalar variable AND
1076 # 2) The regular expression appears in an elsif clause
1078 my $foo = "abcdefghi" . $TAINT;
1080 my $valid_chars = 'a-z';
1083 elsif ( $foo =~ /([$valid_chars]+)/o ) {
1084 test not tainted $1;
1089 elsif ( my @bar = $foo =~ /([$valid_chars]+)/o ) {
1090 test not any_tainted @bar;
1094 # at scope exit, a restored localised value should have its old
1095 # taint status, not the taint status of the current statement
1102 test not tainted $x99;
1104 my $c = do { local $x99; $^X };
1105 test not tainted $x99;
1111 my $c = do { local $x99; '' };
1115 # an mg_get of a tainted value during localization shouldn't taint the
1119 eval { local $0, eval '1' };
1123 # [perl #8262] //g loops infinitely on tainted data
1130 while($a[0]=~ m/(.)/g ) {
1131 last if $i++ > 10000;
1133 cmp_ok $i, '<', 10000, "infinite m//g";
1139 eval 'use Scalar::Util "dualvar"; $got_dualvar++';
1140 skip "No Scalar::Util::dualvar" unless $got_dualvar;
1141 my $a = Scalar::Util::dualvar(3, $^X);
1143 is ($b, 8, "Arithmetic on tainted dualvars works");
1146 # opening '|-' should not trigger $ENV{PATH} check
1150 skip "fork() is not available", 3 unless $Config{'d_fork'};
1151 skip "opening |- is not stable on threaded OpenBSD with taint", 3
1152 if $Config{useithreads} && $Is_OpenBSD;
1154 $ENV{'PATH'} = $TAINT;
1155 local $SIG{'PIPE'} = 'IGNORE';
1157 my $pid = open my $pipe, '|-';
1158 if (!defined $pid) {
1159 die "open failed: $!";
1162 kill 'KILL', $$; # child suicide
1166 test $@ !~ /Insecure \$ENV/, 'fork triggers %ENV check';
1167 test $@ eq '', 'pipe/fork/open/close failed';
1169 open my $pipe, "|$Invoke_Perl -e 1";
1172 test $@ =~ /Insecure \$ENV/, 'popen neglects %ENV check';
1177 package AUTOLOAD_TAINT;
1180 return if $AUTOLOAD =~ /DESTROY/;
1181 if ($AUTOLOAD =~ /untainted/) {
1182 main::ok(!main::tainted($AUTOLOAD), '$AUTOLOAD can be untainted');
1184 main::ok(main::tainted($AUTOLOAD), '$AUTOLOAD can be tainted');
1189 my $o = bless [], 'AUTOLOAD_TAINT';
1195 # tests for tainted format in s?printf
1196 eval { printf($TAINT . "# %s\n", "foo") };
1197 like($@, qr/^Insecure dependency in printf/, q/printf doesn't like tainted formats/);
1198 eval { printf("# %s\n", $TAINT . "foo") };
1199 ok(!$@, q/printf accepts other tainted args/);
1200 eval { sprintf($TAINT . "# %s\n", "foo") };
1201 like($@, qr/^Insecure dependency in sprintf/, q/sprintf doesn't like tainted formats/);
1202 eval { sprintf("# %s\n", $TAINT . "foo") };
1203 ok(!$@, q/sprintf accepts other tainted args/);
1212 is ($val, '7000000000', 'Assignment to untainted variable');
1215 is ($val, '7000000000', 'Assignment to tainted variable');
1219 local $::TODO = "eval currently ignores tainting";
1221 my $tainted = '1' . $TAINT;
1222 eval '$val = eval $tainted;';
1223 is ($val, 0, "eval doesn't like tainted strings");
1224 like ($@, qr/^Insecure dependency in eval/);
1226 # Rather nice code to get a tainted undef by from Rick Delaney
1227 open FH, "test.pl" or die $!;
1228 $tainted=(<FH>,<FH>);
1230 eval 'eval $tainted';
1231 like ($@, qr/^Insecure dependency in eval/);