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.exe' :
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" }
153 $ENV{PATH} = ($Is_Cygwin) ? '/usr/bin' : '';
154 delete @ENV{@MoreEnv};
157 test eval { `$echo 1` } eq "1\n";
160 skip "Environment tainting tests skipped", 4
161 if $Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos || $Is_MacOS;
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}/;
170 test !@vars, "@vars";
172 # tainted $TERM is unsafe only if it contains metachars
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}/, $@;
182 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) {
183 print "# all directories are writeable\n";
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";
193 skip "all directories are writeable", 2 unless $tmp;
195 local $ENV{PATH} = $tmp;
196 test !eval { `$echo 1` };
197 test $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
201 skip "This is not VMS", 4 unless $Is_VMS;
203 $ENV{'DCL$PATH'} = $TAINT;
204 test eval { `$echo 1` } eq '';
205 test $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
207 skip q[can't find world-writeable directory to test DCL$PATH], 2
210 $ENV{'DCL$PATH'} = $tmp;
211 test eval { `$echo 1` } eq '';
212 test $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
214 $ENV{'DCL$PATH'} = '';
218 # Let's see that we can taint and untaint as needed.
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;
227 test not tainted $foo;
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];
239 ($foo) = $foo =~ /(.+)/;
240 test not tainted $foo;
242 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
243 test not tainted $foo;
249 ($foo) = ('bar' . $TAINT) =~ /(.+)/;
253 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
258 $foo = $1 if 'bar' =~ /(.+)$TAINT/;
262 my $pi = 4 * atan2(1,1) + $TAINT0;
265 ($pi) = $pi =~ /(\d+\.\d+)/;
266 test not tainted $pi;
267 test sprintf("%.5f", $pi) eq '3.14159';
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.
273 my $arg = catfile(curdir(), "arg$$");
274 open PROG, "> $arg" or die "Can't create $arg: $!";
276 eval { join('', @ARGV), kill 0 };
277 exit 0 if $@ =~ /^Insecure dependency/;
278 print "# Oops: \$@ was [$@]\n";
282 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
283 test !$?, "Exited with status $?";
287 # Reading from a file should be tainted
289 test open(FILE, $TEST), "Couldn't open '$TEST': $!";
292 sysread(FILE, $block, 100);
299 # Globs should be forbidden, except under VMS,
300 # which doesn't spawn an external program.
302 skip "globs should be forbidden", 2 if 1 or $Is_VMS;
304 my @globs = eval { <*> };
305 test @globs == 0 && $@ =~ /^Insecure dependency/;
307 @globs = eval { glob '*' };
308 test @globs == 0 && $@ =~ /^Insecure dependency/;
311 # Output of commands should be tainted
313 my $foo = `$echo abc`;
317 # Certain system variables should be tainted
319 test all_tainted $^X, $0;
322 # Results of matching should all be untainted
324 my $foo = "abcdefghi" . $TAINT;
328 test not any_tainted $`, $&, $';
330 $foo =~ /(...)(...)(...)/;
331 test not any_tainted $1, $2, $3, $+;
333 my @bar = $foo =~ /(...)(...)(...)/;
334 test not any_tainted @bar;
336 test tainted $foo; # $foo should still be tainted!
337 test $foo eq "abcdefghi";
340 # Operations which affect files can't use tainted data.
342 test !eval { chmod 0, $TAINT }, 'chmod';
343 test $@ =~ /^Insecure dependency/, $@;
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)/, $@;
350 test !eval { rename '', $TAINT }, 'rename';
351 test $@ =~ /^Insecure dependency/, $@;
353 test !eval { unlink $TAINT }, 'unlink';
354 test $@ =~ /^Insecure dependency/, $@;
356 test !eval { utime $TAINT }, 'utime';
357 test $@ =~ /^Insecure dependency/, $@;
360 skip "chown() is not available", 2 unless $Config{d_chown};
362 test !eval { chown -1, -1, $TAINT }, 'chown';
363 test $@ =~ /^Insecure dependency/, $@;
367 skip "link() is not available", 2 unless $Config{d_link};
369 test !eval { link $TAINT, '' }, 'link';
370 test $@ =~ /^Insecure dependency/, $@;
374 skip "symlink() is not available", 2 unless $Config{d_symlink};
376 test !eval { symlink $TAINT, '' }, 'symlink';
377 test $@ =~ /^Insecure dependency/, $@;
381 # Operations which affect directories can't use tainted data.
383 test !eval { mkdir "foo".$TAINT, 0755.$TAINT0 }, 'mkdir';
384 test $@ =~ /^Insecure dependency/, $@;
386 test !eval { rmdir $TAINT }, 'rmdir';
387 test $@ =~ /^Insecure dependency/, $@;
389 test !eval { chdir "foo".$TAINT }, 'chdir';
390 test $@ =~ /^Insecure dependency/, $@;
393 skip "chroot() is not available", 2 unless $Config{d_chroot};
395 test !eval { chroot $TAINT }, 'chroot';
396 test $@ =~ /^Insecure dependency/, $@;
400 # Some operations using files can't use tainted data.
402 my $foo = "imaginary library" . $TAINT;
403 test !eval { require $foo }, 'require';
404 test $@ =~ /^Insecure dependency/, $@;
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
411 test !eval { open FOO, $foo }, 'open for read';
412 test $@ eq '', $@; # NB: This should be allowed
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);
422 test !eval { open FOO, "> $foo" }, 'open for write';
423 test $@ =~ /^Insecure dependency/, $@;
426 # Commands to the system can't use tainted data
431 skip "open('|') is not available", 4 if $^O eq 'amigaos';
433 test !eval { open FOO, "| x$foo" }, 'popen to';
434 test $@ =~ /^Insecure dependency/, $@;
436 test !eval { open FOO, "x$foo |" }, 'popen from';
437 test $@ =~ /^Insecure dependency/, $@;
440 test !eval { exec $TAINT }, 'exec';
441 test $@ =~ /^Insecure dependency/, $@;
443 test !eval { system $TAINT }, 'system';
444 test $@ =~ /^Insecure dependency/, $@;
449 test !eval { `$echo 1$foo` }, 'backticks';
450 test $@ =~ /^Insecure dependency/, $@;
453 # wildcard expansion doesn't invoke shell on VMS, so is safe
454 skip "This is not VMS", 2 unless $Is_VMS;
456 test join('', eval { glob $foo } ) ne '', 'globbing';
461 # Operations which affect processes can't use tainted data.
463 test !eval { kill 0, $TAINT }, 'kill';
464 test $@ =~ /^Insecure dependency/, $@;
467 skip "setpgrp() is not available", 2 unless $Config{d_setpgrp};
469 test !eval { setpgrp 0, $TAINT0 }, 'setpgrp';
470 test $@ =~ /^Insecure dependency/, $@;
474 skip "setpriority() is not available", 2 unless $Config{d_setprior};
476 test !eval { setpriority 0, $TAINT0, $TAINT0 }, 'setpriority';
477 test $@ =~ /^Insecure dependency/, $@;
481 # Some miscellaneous operations can't use tainted data.
484 skip "syscall() is not available", 2 unless $Config{d_syscall};
486 test !eval { syscall $TAINT }, 'syscall';
487 test $@ =~ /^Insecure dependency/, $@;
494 my $temp = "./taintC$$";
496 test open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
498 test !eval { ioctl FOO, $TAINT0, $foo }, 'ioctl';
499 test $@ =~ /^Insecure dependency/, $@;
502 skip "fcntl() is not available", 2 unless $Config{d_fcntl};
504 test !eval { fcntl FOO, $TAINT0, $foo }, 'fcntl';
505 test $@ =~ /^Insecure dependency/, $@;
512 # Some tests involving references
514 my $foo = 'abc' . $TAINT;
516 test not tainted $fooref;
517 test tainted $$fooref;
521 # Some tests involving assignment
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--);
541 # Test assignment and return of lists
543 my @foo = ("A", "tainted" . $TAINT, "B");
544 test not tainted $foo[0];
545 test tainted $foo[1];
546 test not tainted $foo[2];
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];
577 # Test for system/library calls returning string data of dubious origin.
579 # No reliable %Config check for getpw*
581 skip "getpwent() is not available", 1 unless
582 eval { setpwent(); getpwent() };
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
600 # pretty hard to imagine not
601 skip "readdir() is not available", 1 unless $Config{d_readdir};
604 opendir(D, "op") or die "opendir: $!\n";
605 my $readdir = readdir(D);
606 test tainted $readdir;
611 skip "readlink() or symlink() is not available" unless
612 $Config{d_readlink} && $Config{d_symlink};
614 my $symlink = "sl$$";
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;
626 # test bitwise ops (regression bug)
636 # test target of substitution (regression bug)
638 my $why = $TAINT."y";
646 $why =~ s/e/'-'.$$/ge;
652 skip "no IPC::SysV", 2 unless $ipcsysv;
656 skip "shm*() not available", 1 unless $Config{d_shm};
662 my $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
665 if (shmwrite($id, $sent, 0, 60)) {
666 if (shmread($id, $rcvd, 0, 60)) {
667 substr($rcvd, index($rcvd, "\0")) = '';
669 warn "# shmread failed: $!\n";
672 warn "# shmwrite failed: $!\n";
674 shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
676 warn "# shmget failed: $!\n";
679 skip "SysV shared memory operation failed", 1 unless
688 skip "msg*() not available", 1 unless $Config{d_msg};
691 my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
693 my $sent = "message";
694 my $type_sent = 1234;
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);
703 warn "# msgrcv failed: $!\n";
706 warn "# msgsnd failed: $!\n";
708 msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
710 warn "# msgget failed\n";
714 skip "SysV message queue operation failed", 1
715 unless $rcvd eq $sent && $type_sent == $type_rcvd;
723 # bug id 20001004.006
725 open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
730 ok tainted($a) && tainted($b) && !defined($b);
736 # bug id 20001004.007
738 open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
744 ok !tainted($c->{a}) && tainted($c->{b});
749 ok tainted($d->{a}) && !tainted($d->{b});
753 b => { c => $a, d => 42 } };
754 ok !tainted($e->{a}) &&
756 tainted($e->{b}->{c}) &&
757 !tainted($e->{b}->{d});
763 # bug id 20010519.003
766 use vars qw($has_fcntl);
767 eval { require Fcntl; import Fcntl; };
774 skip "no Fcntl", 18 unless $has_fcntl;
776 my $evil = "foo" . $TAINT;
778 eval { sysopen(my $ro, $evil, &O_RDONLY) };
779 test $@ !~ /^Insecure dependency/, $@;
781 eval { sysopen(my $wo, $evil, &O_WRONLY) };
782 test $@ =~ /^Insecure dependency/, $@;
784 eval { sysopen(my $rw, $evil, &O_RDWR) };
785 test $@ =~ /^Insecure dependency/, $@;
787 eval { sysopen(my $ap, $evil, &O_APPEND) };
788 test $@ =~ /^Insecure dependency/, $@;
790 eval { sysopen(my $cr, $evil, &O_CREAT) };
791 test $@ =~ /^Insecure dependency/, $@;
793 eval { sysopen(my $tr, $evil, &O_TRUNC) };
794 test $@ =~ /^Insecure dependency/, $@;
796 eval { sysopen(my $ro, "foo", &O_RDONLY | $TAINT0) };
797 test $@ !~ /^Insecure dependency/, $@;
799 eval { sysopen(my $wo, "foo", &O_WRONLY | $TAINT0) };
800 test $@ =~ /^Insecure dependency/, $@;
802 eval { sysopen(my $rw, "foo", &O_RDWR | $TAINT0) };
803 test $@ =~ /^Insecure dependency/, $@;
805 eval { sysopen(my $ap, "foo", &O_APPEND | $TAINT0) };
806 test $@ =~ /^Insecure dependency/, $@;
808 eval { sysopen(my $cr, "foo", &O_CREAT | $TAINT0) };
809 test $@ =~ /^Insecure dependency/, $@;
811 eval { sysopen(my $tr, "foo", &O_TRUNC | $TAINT0) };
812 test $@ =~ /^Insecure dependency/, $@;
814 eval { sysopen(my $ro, "foo", &O_RDONLY, $TAINT0) };
815 test $@ !~ /^Insecure dependency/, $@;
817 eval { sysopen(my $wo, "foo", &O_WRONLY, $TAINT0) };
818 test $@ =~ /^Insecure dependency/, $@;
820 eval { sysopen(my $rw, "foo", &O_RDWR, $TAINT0) };
821 test $@ =~ /^Insecure dependency/, $@;
823 eval { sysopen(my $ap, "foo", &O_APPEND, $TAINT0) };
824 test $@ =~ /^Insecure dependency/, $@;
826 eval { sysopen(my $cr, "foo", &O_CREAT, $TAINT0) };
827 test $@ =~ /^Insecure dependency/, $@;
829 eval { sysopen(my $tr, "foo", &O_TRUNC, $TAINT0) };
830 test $@ =~ /^Insecure dependency/, $@;
832 unlink("foo"); # not unlink($evil), because that would fail...
842 local $SIG{__WARN__} = sub { $saw_warning = 1 };
845 my $divnum = shift()/1;
846 sprintf("%1.1f\n", $divnum);
858 # Bug ID 20010730.010
866 bless \$arg => $class;
877 my $bar = "The Big Bright Green Pleasure Machine";
879 tie my ($foo), Tie => $bar;
887 # Check that all environment variables are tainted.
889 while (my ($k, $v) = each %ENV) {
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";
896 test @untainted == 0, "untainted:\n @untainted";
900 ok( ${^TAINT} == 1, '$^TAINT is on' );
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' );
910 my $re1 = qr/x$TAINT/;
913 my $re2 = qr/^$re1\z/;
921 skip "system {} has different semantics on Win32", 1 if $Is_MSWin32;
924 local $ENV{PATH} .= $TAINT;
925 eval { system { "echo" } "/arg0", "arg1" };
926 test $@ =~ /^Insecure \$ENV/;
930 todo_skip 'tainted %ENV warning occludes tainted arguments warning', 22
933 # bug 20020208.005 plus some single arg exec/system extras
934 my $err = qr/^Insecure dependency/ ;
935 test !eval { exec $TAINT, $TAINT }, 'exec';
937 test !eval { exec $TAINT $TAINT }, 'exec';
939 test !eval { exec $TAINT $TAINT, $TAINT }, 'exec';
941 test !eval { exec $TAINT 'notaint' }, 'exec';
943 test !eval { exec {'notaint'} $TAINT }, 'exec';
946 test !eval { system $TAINT, $TAINT }, 'system';
948 test !eval { system $TAINT $TAINT }, 'system';
950 test !eval { system $TAINT $TAINT, $TAINT }, 'system';
952 test !eval { system $TAINT 'notaint' }, 'system';
954 test !eval { system {'notaint'} $TAINT }, 'system';
959 system("lskdfj does not exist","with","args");
964 skip "no exec() on MacOS Classic" if $Is_MacOS;
968 exec("lskdfj does not exist","with","args");
973 # If you add tests here update also the above skip block for VMS.
977 # [ID 20020704.001] taint propagation failure
980 test tainted(my $foo = $1);
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/;
998 test !tainted($notaint);
1001 $notaint =~ /($notaint)/;
1005 $notaint =~ /($TAINT)/;
1010 $TAINT =~ /($notaint)/;
1014 $TAINT =~ /($TAINT)/;
1020 ($r = $TAINT) =~ /($notaint)/;
1022 ($r = $TAINT) =~ /($TAINT)/;
1026 # accessing $^O shoudn't taint it as a side-effect;
1027 # assigning tainted data to it is now an error
1030 if (!$^X) { } elsif ($^O eq 'bar') { }
1033 test $@ =~ /Insecure dependency in/;
1036 EFFECTIVELY_CONSTANTS: {
1037 my $tainted_number = 12 + $TAINT0;
1038 test tainted( $tainted_number );
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;
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 );
1052 my $result = $tainted_true ? "True" : "False";
1053 test $result eq "True";
1054 test !tainted( $result );
1056 $result = $tainted_false ? "True" : "False";
1057 test $result eq "False";
1058 test !tainted( $result );
1060 my $untainted_whatever = "The Fabulous Johnny Cash";
1061 my $tainted_whatever = "Soft Cell" . $TAINT;
1063 $result = $tainted_true ? $tainted_whatever : $untainted_whatever;
1064 test $result eq "Soft Cell";
1065 test tainted( $result );
1067 $result = $tainted_false ? $tainted_whatever : $untainted_whatever;
1068 test $result eq "The Fabulous Johnny Cash";
1069 test !tainted( $result );
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
1077 my $foo = "abcdefghi" . $TAINT;
1079 my $valid_chars = 'a-z';
1082 elsif ( $foo =~ /([$valid_chars]+)/o ) {
1083 test not tainted $1;
1088 elsif ( my @bar = $foo =~ /([$valid_chars]+)/o ) {
1089 test not any_tainted @bar;
1093 # at scope exit, a restored localised value should have its old
1094 # taint status, not the taint status of the current statement
1101 test not tainted $x99;
1103 my $c = do { local $x99; $^X };
1104 test not tainted $x99;
1110 my $c = do { local $x99; '' };
1114 # an mg_get of a tainted value during localization shouldn't taint the
1118 eval { local $0, eval '1' };
1122 # [perl #8262] //g loops infinitely on tainted data
1129 while($a[0]=~ m/(.)/g ) {
1130 last if $i++ > 10000;
1132 cmp_ok $i, '<', 10000, "infinite m//g";
1138 eval 'use Scalar::Util "dualvar"; $got_dualvar++';
1139 skip "No Scalar::Util::dualvar" unless $got_dualvar;
1140 my $a = Scalar::Util::dualvar(3, $^X);
1142 is ($b, 8, "Arithmetic on tainted dualvars works");
1145 # opening '|-' should not trigger $ENV{PATH} check
1149 skip "fork() is not available", 3 unless $Config{'d_fork'};
1150 skip "opening |- is not stable on threaded OpenBSD with taint", 3
1151 if $Config{useithreads} && $Is_OpenBSD;
1153 $ENV{'PATH'} = $TAINT;
1154 local $SIG{'PIPE'} = 'IGNORE';
1156 my $pid = open my $pipe, '|-';
1157 if (!defined $pid) {
1158 die "open failed: $!";
1161 kill 'KILL', $$; # child suicide
1165 test $@ !~ /Insecure \$ENV/, 'fork triggers %ENV check';
1166 test $@ eq '', 'pipe/fork/open/close failed';
1168 open my $pipe, "|$Invoke_Perl -e 1";
1171 test $@ =~ /Insecure \$ENV/, 'popen neglects %ENV check';
1176 package AUTOLOAD_TAINT;
1179 return if $AUTOLOAD =~ /DESTROY/;
1180 if ($AUTOLOAD =~ /untainted/) {
1181 main::ok(!main::tainted($AUTOLOAD), '$AUTOLOAD can be untainted');
1183 main::ok(main::tainted($AUTOLOAD), '$AUTOLOAD can be tainted');
1188 my $o = bless [], 'AUTOLOAD_TAINT';
1194 # tests for tainted format in s?printf
1195 eval { printf($TAINT . "# %s\n", "foo") };
1196 like($@, qr/^Insecure dependency in printf/, q/printf doesn't like tainted formats/);
1197 eval { printf("# %s\n", $TAINT . "foo") };
1198 ok(!$@, q/printf accepts other tainted args/);
1199 eval { sprintf($TAINT . "# %s\n", "foo") };
1200 like($@, qr/^Insecure dependency in sprintf/, q/sprintf doesn't like tainted formats/);
1201 eval { sprintf("# %s\n", $TAINT . "foo") };
1202 ok(!$@, q/sprintf accepts other tainted args/);
1211 is ($val, '7000000000', 'Assignment to untainted variable');
1214 is ($val, '7000000000', 'Assignment to tainted variable');
1219 my $tainted = '1' . $TAINT;
1220 eval '$val = eval $tainted;';
1221 is ($val, 0, "eval doesn't like tainted strings");
1222 like ($@, qr/^Insecure dependency in eval/);
1224 # Rather nice code to get a tainted undef by from Rick Delaney
1225 open FH, "test.pl" or die $!;
1226 seek FH, 0, 2 or die $!;
1229 eval 'eval $tainted';
1230 like ($@, qr/^Insecure dependency in eval/);
1233 foreach my $ord (78, 163, 256) {
1235 my $line = 'A1' . $TAINT . chr $ord;
1239 local $::TODO = "Bug for UTF-8 not fixed yet" if $ord > 255;
1240 ok(!tainted($1), "\\S match with chr $ord");
1243 # This may bomb out with the alarm signal so keep it last
1245 skip "No alarm()" unless $Config{d_alarm};
1246 # Test from RT #41831]
1247 # [PATCH] Bug & fix: hang when using study + taint mode (perl 5.6.1, 5.8.x)
1249 my $DATA = <<'END' . $TAINT;
1259 ## don't set $SIG{ALRM}, since we'd never get to a user-level handler as
1260 ## perl is stuck in a regexp infinite loop!
1264 if ($DATA =~ /^line2.*line4/m) {
1265 fail("Should not be a match")
1267 pass("Match on tainted multiline data should fail promptly");
1273 # Keep the previous test last