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?
23 use vars qw($ipcsysv); # did we manage to load IPC::SysV?
25 my ($old_env_path, $old_env_dcl_path, $old_env_term);
27 $old_env_path = $ENV{'PATH'};
28 $old_env_dcl_path = $ENV{'DCL$PATH'};
29 $old_env_term = $ENV{'TERM'};
30 if ($^O eq 'VMS' && !defined($Config{d_setenv})) {
31 $ENV{PATH} = $ENV{PATH};
32 $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy';
34 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/
35 && ($Config{d_shm} || $Config{d_msg})) {
36 eval { require IPC::SysV };
39 IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU IPC_NOWAIT));
44 my $Is_VMS = $^O eq 'VMS';
45 my $Is_MSWin32 = $^O eq 'MSWin32';
46 my $Is_NetWare = $^O eq 'NetWare';
47 my $Is_Dos = $^O eq 'dos';
48 my $Is_Cygwin = $^O eq 'cygwin';
49 my $Is_OpenBSD = $^O eq 'openbsd';
50 my $Is_MirBSD = $^O eq 'mirbsd';
51 my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.exe' :
52 $Is_MSWin32 ? '.\perl' :
53 $Is_NetWare ? 'perl' :
55 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
59 for $x ('DCL$PATH', @MoreEnv) {
60 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
62 # VMS note: PATH and TERM are automatically created by the C
63 # library in VMS on reference to the their keys in %ENV.
64 # There is currently no way to determine if they did not exist
65 # before this test was run.
68 \$ENV{PATH} = \$old_env_path;
69 warn "# Note: logical name 'PATH' may have been created\n";
70 \$ENV{'TERM'} = \$old_env_term;
71 warn "# Note: logical name 'TERM' may have been created\n";
72 \@ENV{keys %old} = values %old;
73 if (defined \$old_env_dcl_path) {
74 \$ENV{'DCL\$PATH'} = \$old_env_dcl_path;
76 delete \$ENV{'DCL\$PATH'};
83 # The empty tainted value, for tainting strings
84 my $TAINT = substr($^X, 0, 0);
85 # A tainted zero, useful for tainting numbers
92 # This taints each argument passed. All must be lvalues.
93 # Side effect: It also stringifies them. :-(
95 for (@_) { $_ .= $TAINT }
98 # How to identify taint when you see it
100 local $@; # Don't pollute caller's value.
101 not eval { join("",@_), kill 0; 1 };
106 local $::Level = $::Level + 1;
107 ok(tainted($thing), @_);
112 local $::Level = $::Level + 1;
113 ok(!tainted($thing), @_);
117 my ($code, $what, $desc) = @_;
119 local $::Level = $::Level + 1;
120 is(eval { $code->(); }, undef, $desc);
121 like($@, qr/^Insecure dependency in $what while running with -T switch/);
124 # We need an external program to call.
125 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$"));
127 open my $fh, '>', $ECHO or die "Can't create $ECHO: $!";
128 print $fh 'print "@ARGV\n"', "\n";
130 my $echo = "$Invoke_Perl $ECHO";
134 # First, let's make sure that Perl is checking the dangerous
135 # environment variables. Maybe they aren't set yet, so we'll
136 # taint them ourselves.
138 $ENV{'DCL$PATH'} = '' if $Is_VMS;
140 $ENV{PATH} = ($Is_Cygwin) ? '/usr/bin' : '';
141 delete @ENV{@MoreEnv};
144 is(eval { `$echo 1` }, "1\n");
147 skip "Environment tainting tests skipped", 4
148 if $Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos;
150 my @vars = ('PATH', @MoreEnv);
151 while (my $v = $vars[0]) {
152 local $ENV{$v} = $TAINT;
153 last if eval { `$echo 1` };
154 last unless $@ =~ /^Insecure \$ENV\{$v}/;
159 # tainted $TERM is unsafe only if it contains metachars
161 $ENV{TERM} = 'e=mc2';
162 is(eval { `$echo 1` }, "1\n");
163 $ENV{TERM} = 'e=mc2' . $TAINT;
164 is(eval { `$echo 1` }, undef);
165 like($@, qr/^Insecure \$ENV\{TERM}/);
169 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) {
170 print "# all directories are writeable\n";
173 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
174 qw(sys$scratch /tmp /var/tmp /usr/tmp),
175 @ENV{qw(TMP TEMP)})[0]
176 or print "# can't find world-writeable directory to test PATH\n";
180 skip "all directories are writeable", 2 unless $tmp;
182 local $ENV{PATH} = $tmp;
183 is(eval { `$echo 1` }, undef);
184 # Message can be different depending on whether echo
185 # is a builtin or not
186 like($@, qr/^Insecure (?:directory in )?\$ENV\{PATH}/);
190 skip "This is not VMS", 4 unless $Is_VMS;
192 $ENV{'DCL$PATH'} = $TAINT;
193 is(eval { `$echo 1` }, undef);
194 like($@, qr/^Insecure \$ENV\{DCL\$PATH}/);
196 skip q[can't find world-writeable directory to test DCL$PATH], 2
199 $ENV{'DCL$PATH'} = $tmp;
200 is(eval { `$echo 1` }, undef);
201 like($@, qr/^Insecure directory in \$ENV\{DCL\$PATH}/);
203 $ENV{'DCL$PATH'} = '';
207 # Let's see that we can taint and untaint as needed.
212 # That was a sanity check. If it failed, stop the insanity!
213 die "Taint checks don't seem to be enabled" unless tainted $foo;
222 isnt_tainted($_) foreach @list;
223 taint_these @list[1,3,5,7,9];
224 is_tainted($_) foreach @list[1,3,5,7,9];
225 isnt_tainted($_) foreach @list[0,2,4,6,8];
227 ($foo) = $foo =~ /(.+)/;
230 my ($desc, $s, $res, $res2, $one);
232 $desc = "match with string tainted";
234 $s = 'abcd' . $TAINT;
237 is_tainted($s, "$desc: s tainted");
238 isnt_tainted($res, "$desc: res not tainted");
239 isnt_tainted($one, "$desc: \$1 not tainted");
240 is($res, 1, "$desc: res value");
241 is($one, 'abcd', "$desc: \$1 value");
243 $desc = "match /g with string tainted";
245 $s = 'abcd' . $TAINT;
248 is_tainted($s, "$desc: s tainted");
249 isnt_tainted($res, "$desc: res not tainted");
250 isnt_tainted($one, "$desc: \$1 not tainted");
251 is($res, 1, "$desc: res value");
252 is($one, 'a', "$desc: \$1 value");
254 $desc = "match with string tainted, list cxt";
256 $s = 'abcd' . $TAINT;
257 ($res) = $s =~ /(.+)/;
259 is_tainted($s, "$desc: s tainted");
260 isnt_tainted($res, "$desc: res not tainted");
261 isnt_tainted($one, "$desc: \$1 not tainted");
262 is($res, 'abcd', "$desc: res value");
263 is($one, 'abcd', "$desc: \$1 value");
265 $desc = "match /g with string tainted, list cxt";
267 $s = 'abcd' . $TAINT;
268 ($res, $res2) = $s =~ /(.)/g;
270 is_tainted($s, "$desc: s tainted");
271 isnt_tainted($res, "$desc: res not tainted");
272 isnt_tainted($res2,"$desc: res2 not tainted");
273 isnt_tainted($one, "$desc: \$1 not tainted");
274 is($res, 'a', "$desc: res value");
275 is($res2,'b', "$desc: res2 value");
276 is($one, 'd', "$desc: \$1 value");
278 $desc = "match with pattern tainted";
281 $res = $s =~ /$TAINT(.+)/;
283 isnt_tainted($s, "$desc: s not tainted");
284 isnt_tainted($res, "$desc: res not tainted");
285 is_tainted($one, "$desc: \$1 tainted");
286 is($res, 1, "$desc: res value");
287 is($one, 'abcd', "$desc: \$1 value");
289 $desc = "match /g with pattern tainted";
292 $res = $s =~ /$TAINT(.)/g;
294 isnt_tainted($s, "$desc: s not tainted");
295 isnt_tainted($res, "$desc: res not tainted");
296 is_tainted($one, "$desc: \$1 tainted");
297 is($res, 1, "$desc: res value");
298 is($one, 'a', "$desc: \$1 value");
301 skip 'No locale testing without d_setlocale', 10 if(!$Config{d_setlocale});
303 $desc = "match with pattern tainted via locale";
308 $res = $s =~ /(\w+)/; $one = $1;
310 isnt_tainted($s, "$desc: s not tainted");
311 isnt_tainted($res, "$desc: res not tainted");
312 is_tainted($one, "$desc: \$1 tainted");
313 is($res, 1, "$desc: res value");
314 is($one, 'abcd', "$desc: \$1 value");
316 $desc = "match /g with pattern tainted via locale";
321 $res = $s =~ /(\w)/g; $one = $1;
323 isnt_tainted($s, "$desc: s not tainted");
324 isnt_tainted($res, "$desc: res not tainted");
325 is_tainted($one, "$desc: \$1 tainted");
326 is($res, 1, "$desc: res value");
327 is($one, 'a', "$desc: \$1 value");
330 $desc = "match with pattern tainted, list cxt";
333 ($res) = $s =~ /$TAINT(.+)/;
335 isnt_tainted($s, "$desc: s not tainted");
336 is_tainted($res, "$desc: res tainted");
337 is_tainted($one, "$desc: \$1 tainted");
338 is($res, 'abcd', "$desc: res value");
339 is($one, 'abcd', "$desc: \$1 value");
341 $desc = "match /g with pattern tainted, list cxt";
344 ($res, $res2) = $s =~ /$TAINT(.)/g;
346 isnt_tainted($s, "$desc: s not tainted");
347 is_tainted($res, "$desc: res tainted");
348 is_tainted($one, "$desc: \$1 tainted");
349 is($res, 'a', "$desc: res value");
350 is($res2,'b', "$desc: res2 value");
351 is($one, 'd', "$desc: \$1 value");
354 skip 'No locale testing without d_setlocale', 12 if(!$Config{d_setlocale});
356 $desc = "match with pattern tainted via locale, list cxt";
361 ($res) = $s =~ /(\w+)/; $one = $1;
363 isnt_tainted($s, "$desc: s not tainted");
364 is_tainted($res, "$desc: res tainted");
365 is_tainted($one, "$desc: \$1 tainted");
366 is($res, 'abcd', "$desc: res value");
367 is($one, 'abcd', "$desc: \$1 value");
369 $desc = "match /g with pattern tainted via locale, list cxt";
374 ($res, $res2) = $s =~ /(\w)/g; $one = $1;
376 isnt_tainted($s, "$desc: s not tainted");
377 is_tainted($res, "$desc: res tainted");
378 is_tainted($res2, "$desc: res2 tainted");
379 is_tainted($one, "$desc: \$1 tainted");
380 is($res, 'a', "$desc: res value");
381 is($res2,'b', "$desc: res2 value");
382 is($one, 'd', "$desc: \$1 value");
385 $desc = "substitution with string tainted";
387 $s = 'abcd' . $TAINT;
388 $res = $s =~ s/(.+)/xyz/;
390 is_tainted($s, "$desc: s tainted");
391 isnt_tainted($res, "$desc: res not tainted");
392 isnt_tainted($one, "$desc: \$1 not tainted");
393 is($s, 'xyz', "$desc: s value");
394 is($res, 1, "$desc: res value");
395 is($one, 'abcd', "$desc: \$1 value");
397 $desc = "substitution /g with string tainted";
399 $s = 'abcd' . $TAINT;
400 $res = $s =~ s/(.)/x/g;
402 is_tainted($s, "$desc: s tainted");
403 is_tainted($res, "$desc: res tainted");
404 isnt_tainted($one, "$desc: \$1 not tainted");
405 is($s, 'xxxx', "$desc: s value");
406 is($res, 4, "$desc: res value");
407 is($one, 'd', "$desc: \$1 value");
409 $desc = "substitution /r with string tainted";
411 $s = 'abcd' . $TAINT;
412 $res = $s =~ s/(.+)/xyz/r;
414 is_tainted($s, "$desc: s tainted");
415 is_tainted($res, "$desc: res tainted");
416 isnt_tainted($one, "$desc: \$1 not tainted");
417 is($s, 'abcd', "$desc: s value");
418 is($res, 'xyz', "$desc: res value");
419 is($one, 'abcd', "$desc: \$1 value");
421 $desc = "substitution /e with string tainted";
423 $s = 'abcd' . $TAINT;
425 $res = $s =~ s{(.+)}{
426 $one = $one . "x"; # make sure code not tainted
427 isnt_tainted($one, "$desc: code not tainted within /e");
429 isnt_tainted($one, "$desc: \$1 not tainted within /e");
433 is_tainted($s, "$desc: s tainted");
434 isnt_tainted($res, "$desc: res not tainted");
435 isnt_tainted($one, "$desc: \$1 not tainted");
436 is($s, 'xyz', "$desc: s value");
437 is($res, 1, "$desc: res value");
438 is($one, 'abcd', "$desc: \$1 value");
440 $desc = "substitution with pattern tainted";
443 $res = $s =~ s/$TAINT(.+)/xyz/;
445 is_tainted($s, "$desc: s tainted");
446 isnt_tainted($res, "$desc: res not tainted");
447 is_tainted($one, "$desc: \$1 tainted");
448 is($s, 'xyz', "$desc: s value");
449 is($res, 1, "$desc: res value");
450 is($one, 'abcd', "$desc: \$1 value");
452 $desc = "substitution /g with pattern tainted";
455 $res = $s =~ s/$TAINT(.)/x/g;
457 is_tainted($s, "$desc: s tainted");
458 is_tainted($res, "$desc: res tainted");
459 is_tainted($one, "$desc: \$1 tainted");
460 is($s, 'xxxx', "$desc: s value");
461 is($res, 4, "$desc: res value");
462 is($one, 'd', "$desc: \$1 value");
464 $desc = "substitution /ge with pattern tainted";
470 $res = $s =~ s{(.)$TAINT}{
471 $j = $i; # make sure code not tainted
473 isnt_tainted($j, "$desc: code not tainted within /e");
476 isnt_tainted($s, "$desc: s not tainted loop 1");
479 is_tainted($s, "$desc: s tainted loop $i");
481 is_tainted($one, "$desc: \$1 tainted loop $i");
486 is_tainted($s, "$desc: s tainted");
487 is_tainted($res, "$desc: res tainted");
488 is_tainted($one, "$desc: \$1 tainted");
489 is($s, '123', "$desc: s value");
490 is($res, 3, "$desc: res value");
491 is($one, 'c', "$desc: \$1 value");
493 $desc = "substitution /r with pattern tainted";
496 $res = $s =~ s/$TAINT(.+)/xyz/r;
498 isnt_tainted($s, "$desc: s not tainted");
499 is_tainted($res, "$desc: res tainted");
500 is_tainted($one, "$desc: \$1 tainted");
501 is($s, 'abcd', "$desc: s value");
502 is($res, 'xyz', "$desc: res value");
503 is($one, 'abcd', "$desc: \$1 value");
506 skip 'No locale testing without d_setlocale', 18 if(!$Config{d_setlocale});
508 $desc = "substitution with pattern tainted via locale";
513 $res = $s =~ s/(\w+)/xyz/; $one = $1;
515 is_tainted($s, "$desc: s tainted");
516 isnt_tainted($res, "$desc: res not tainted");
517 is_tainted($one, "$desc: \$1 tainted");
518 is($s, 'xyz', "$desc: s value");
519 is($res, 1, "$desc: res value");
520 is($one, 'abcd', "$desc: \$1 value");
522 $desc = "substitution /g with pattern tainted via locale";
527 $res = $s =~ s/(\w)/x/g; $one = $1;
529 is_tainted($s, "$desc: s tainted");
530 is_tainted($res, "$desc: res tainted");
531 is_tainted($one, "$desc: \$1 tainted");
532 is($s, 'xxxx', "$desc: s value");
533 is($res, 4, "$desc: res value");
534 is($one, 'd', "$desc: \$1 value");
536 $desc = "substitution /r with pattern tainted via locale";
541 $res = $s =~ s/(\w+)/xyz/r; $one = $1;
543 isnt_tainted($s, "$desc: s not tainted");
544 is_tainted($res, "$desc: res tainted");
545 is_tainted($one, "$desc: \$1 tainted");
546 is($s, 'abcd', "$desc: s value");
547 is($res, 'xyz', "$desc: res value");
548 is($one, 'abcd', "$desc: \$1 value");
551 $desc = "substitution with replacement tainted";
554 $res = $s =~ s/(.+)/xyz$TAINT/;
556 is_tainted($s, "$desc: s tainted");
557 isnt_tainted($res, "$desc: res not tainted");
558 isnt_tainted($one, "$desc: \$1 not tainted");
559 is($s, 'xyz', "$desc: s value");
560 is($res, 1, "$desc: res value");
561 is($one, 'abcd', "$desc: \$1 value");
563 $desc = "substitution /g with replacement tainted";
566 $res = $s =~ s/(.)/x$TAINT/g;
568 is_tainted($s, "$desc: s tainted");
569 isnt_tainted($res, "$desc: res not tainted");
570 isnt_tainted($one, "$desc: \$1 not tainted");
571 is($s, 'xxxx', "$desc: s value");
572 is($res, 4, "$desc: res value");
573 is($one, 'd', "$desc: \$1 value");
575 $desc = "substitution /ge with replacement tainted";
582 $j = $i; # make sure code not tainted
584 isnt_tainted($j, "$desc: code not tainted within /e");
587 isnt_tainted($s, "$desc: s not tainted loop 1");
590 is_tainted($s, "$desc: s tainted loop $i");
592 isnt_tainted($one, "$desc: \$1 not tainted within /e");
597 is_tainted($s, "$desc: s tainted");
598 isnt_tainted($res, "$desc: res tainted");
599 isnt_tainted($one, "$desc: \$1 not tainted");
600 is($s, '123', "$desc: s value");
601 is($res, 3, "$desc: res value");
602 is($one, 'c', "$desc: \$1 value");
604 $desc = "substitution /r with replacement tainted";
607 $res = $s =~ s/(.+)/xyz$TAINT/r;
609 isnt_tainted($s, "$desc: s not tainted");
610 is_tainted($res, "$desc: res tainted");
611 isnt_tainted($one, "$desc: \$1 not tainted");
612 is($s, 'abcd', "$desc: s value");
613 is($res, 'xyz', "$desc: res value");
614 is($one, 'abcd', "$desc: \$1 value");
617 # now do them all again with "use re 'taint"
621 $desc = "use re 'taint': match with string tainted";
623 $s = 'abcd' . $TAINT;
626 is_tainted($s, "$desc: s tainted");
627 isnt_tainted($res, "$desc: res not tainted");
628 is_tainted($one, "$desc: \$1 tainted");
629 is($res, 1, "$desc: res value");
630 is($one, 'abcd', "$desc: \$1 value");
632 $desc = "use re 'taint': match /g with string tainted";
634 $s = 'abcd' . $TAINT;
637 is_tainted($s, "$desc: s tainted");
638 isnt_tainted($res, "$desc: res not tainted");
639 is_tainted($one, "$desc: \$1 tainted");
640 is($res, 1, "$desc: res value");
641 is($one, 'a', "$desc: \$1 value");
643 $desc = "use re 'taint': match with string tainted, list cxt";
645 $s = 'abcd' . $TAINT;
646 ($res) = $s =~ /(.+)/;
648 is_tainted($s, "$desc: s tainted");
649 is_tainted($res, "$desc: res tainted");
650 is_tainted($one, "$desc: \$1 tainted");
651 is($res, 'abcd', "$desc: res value");
652 is($one, 'abcd', "$desc: \$1 value");
654 $desc = "use re 'taint': match /g with string tainted, list cxt";
656 $s = 'abcd' . $TAINT;
657 ($res, $res2) = $s =~ /(.)/g;
659 is_tainted($s, "$desc: s tainted");
660 is_tainted($res, "$desc: res tainted");
661 is_tainted($res2, "$desc: res2 tainted");
662 is_tainted($one, "$desc: \$1 not tainted");
663 is($res, 'a', "$desc: res value");
664 is($res2,'b', "$desc: res2 value");
665 is($one, 'd', "$desc: \$1 value");
667 $desc = "use re 'taint': match with pattern tainted";
670 $res = $s =~ /$TAINT(.+)/;
672 isnt_tainted($s, "$desc: s not tainted");
673 isnt_tainted($res, "$desc: res not tainted");
674 is_tainted($one, "$desc: \$1 tainted");
675 is($res, 1, "$desc: res value");
676 is($one, 'abcd', "$desc: \$1 value");
678 $desc = "use re 'taint': match /g with pattern tainted";
681 $res = $s =~ /$TAINT(.)/g;
683 isnt_tainted($s, "$desc: s not tainted");
684 isnt_tainted($res, "$desc: res not tainted");
685 is_tainted($one, "$desc: \$1 tainted");
686 is($res, 1, "$desc: res value");
687 is($one, 'a', "$desc: \$1 value");
690 skip 'No locale testing without d_setlocale', 10 if(!$Config{d_setlocale});
692 $desc = "use re 'taint': match with pattern tainted via locale";
697 $res = $s =~ /(\w+)/; $one = $1;
699 isnt_tainted($s, "$desc: s not tainted");
700 isnt_tainted($res, "$desc: res not tainted");
701 is_tainted($one, "$desc: \$1 tainted");
702 is($res, 1, "$desc: res value");
703 is($one, 'abcd', "$desc: \$1 value");
705 $desc = "use re 'taint': match /g with pattern tainted via locale";
710 $res = $s =~ /(\w)/g; $one = $1;
712 isnt_tainted($s, "$desc: s not tainted");
713 isnt_tainted($res, "$desc: res not tainted");
714 is_tainted($one, "$desc: \$1 tainted");
715 is($res, 1, "$desc: res value");
716 is($one, 'a', "$desc: \$1 value");
719 $desc = "use re 'taint': match with pattern tainted, list cxt";
722 ($res) = $s =~ /$TAINT(.+)/;
724 isnt_tainted($s, "$desc: s not tainted");
725 is_tainted($res, "$desc: res tainted");
726 is_tainted($one, "$desc: \$1 tainted");
727 is($res, 'abcd', "$desc: res value");
728 is($one, 'abcd', "$desc: \$1 value");
730 $desc = "use re 'taint': match /g with pattern tainted, list cxt";
733 ($res, $res2) = $s =~ /$TAINT(.)/g;
735 isnt_tainted($s, "$desc: s not tainted");
736 is_tainted($res, "$desc: res tainted");
737 is_tainted($one, "$desc: \$1 tainted");
738 is($res, 'a', "$desc: res value");
739 is($res2,'b', "$desc: res2 value");
740 is($one, 'd', "$desc: \$1 value");
743 skip 'No locale testing without d_setlocale', 12 if(!$Config{d_setlocale});
745 $desc = "use re 'taint': match with pattern tainted via locale, list cxt";
750 ($res) = $s =~ /(\w+)/; $one = $1;
752 isnt_tainted($s, "$desc: s not tainted");
753 is_tainted($res, "$desc: res tainted");
754 is_tainted($one, "$desc: \$1 tainted");
755 is($res, 'abcd', "$desc: res value");
756 is($one, 'abcd', "$desc: \$1 value");
758 $desc = "use re 'taint': match /g with pattern tainted via locale, list cxt";
763 ($res, $res2) = $s =~ /(\w)/g; $one = $1;
765 isnt_tainted($s, "$desc: s not tainted");
766 is_tainted($res, "$desc: res tainted");
767 is_tainted($res2, "$desc: res2 tainted");
768 is_tainted($one, "$desc: \$1 tainted");
769 is($res, 'a', "$desc: res value");
770 is($res2,'b', "$desc: res2 value");
771 is($one, 'd', "$desc: \$1 value");
774 $desc = "use re 'taint': substitution with string tainted";
776 $s = 'abcd' . $TAINT;
777 $res = $s =~ s/(.+)/xyz/;
779 is_tainted($s, "$desc: s tainted");
780 isnt_tainted($res, "$desc: res not tainted");
781 is_tainted($one, "$desc: \$1 tainted");
782 is($s, 'xyz', "$desc: s value");
783 is($res, 1, "$desc: res value");
784 is($one, 'abcd', "$desc: \$1 value");
786 $desc = "use re 'taint': substitution /g with string tainted";
788 $s = 'abcd' . $TAINT;
789 $res = $s =~ s/(.)/x/g;
791 is_tainted($s, "$desc: s tainted");
792 is_tainted($res, "$desc: res tainted");
793 is_tainted($one, "$desc: \$1 tainted");
794 is($s, 'xxxx', "$desc: s value");
795 is($res, 4, "$desc: res value");
796 is($one, 'd', "$desc: \$1 value");
798 $desc = "use re 'taint': substitution /r with string tainted";
800 $s = 'abcd' . $TAINT;
801 $res = $s =~ s/(.+)/xyz/r;
803 is_tainted($s, "$desc: s tainted");
804 is_tainted($res, "$desc: res tainted");
805 is_tainted($one, "$desc: \$1 tainted");
806 is($s, 'abcd', "$desc: s value");
807 is($res, 'xyz', "$desc: res value");
808 is($one, 'abcd', "$desc: \$1 value");
810 $desc = "use re 'taint': substitution /e with string tainted";
812 $s = 'abcd' . $TAINT;
814 $res = $s =~ s{(.+)}{
815 $one = $one . "x"; # make sure code not tainted
816 isnt_tainted($one, "$desc: code not tainted within /e");
818 is_tainted($one, "$desc: $1 tainted within /e");
822 is_tainted($s, "$desc: s tainted");
823 isnt_tainted($res, "$desc: res not tainted");
824 is_tainted($one, "$desc: \$1 tainted");
825 is($s, 'xyz', "$desc: s value");
826 is($res, 1, "$desc: res value");
827 is($one, 'abcd', "$desc: \$1 value");
829 $desc = "use re 'taint': substitution with pattern tainted";
832 $res = $s =~ s/$TAINT(.+)/xyz/;
834 is_tainted($s, "$desc: s tainted");
835 isnt_tainted($res, "$desc: res not tainted");
836 is_tainted($one, "$desc: \$1 tainted");
837 is($s, 'xyz', "$desc: s value");
838 is($res, 1, "$desc: res value");
839 is($one, 'abcd', "$desc: \$1 value");
841 $desc = "use re 'taint': substitution /g with pattern tainted";
844 $res = $s =~ s/$TAINT(.)/x/g;
846 is_tainted($s, "$desc: s tainted");
847 is_tainted($res, "$desc: res tainted");
848 is_tainted($one, "$desc: \$1 tainted");
849 is($s, 'xxxx', "$desc: s value");
850 is($res, 4, "$desc: res value");
851 is($one, 'd', "$desc: \$1 value");
853 $desc = "use re 'taint': substitution /ge with pattern tainted";
859 $res = $s =~ s{(.)$TAINT}{
860 $j = $i; # make sure code not tainted
862 isnt_tainted($j, "$desc: code not tainted within /e");
865 isnt_tainted($s, "$desc: s not tainted loop 1");
868 is_tainted($s, "$desc: s tainted loop $i");
870 is_tainted($one, "$desc: \$1 tainted loop $i");
875 is_tainted($s, "$desc: s tainted");
876 is_tainted($res, "$desc: res tainted");
877 is_tainted($one, "$desc: \$1 tainted");
878 is($s, '123', "$desc: s value");
879 is($res, 3, "$desc: res value");
880 is($one, 'c', "$desc: \$1 value");
883 $desc = "use re 'taint': substitution /r with pattern tainted";
886 $res = $s =~ s/$TAINT(.+)/xyz/r;
888 isnt_tainted($s, "$desc: s not tainted");
889 is_tainted($res, "$desc: res tainted");
890 is_tainted($one, "$desc: \$1 tainted");
891 is($s, 'abcd', "$desc: s value");
892 is($res, 'xyz', "$desc: res value");
893 is($one, 'abcd', "$desc: \$1 value");
896 skip 'No locale testing without d_setlocale', 18 if(!$Config{d_setlocale});
898 $desc = "use re 'taint': substitution with pattern tainted via locale";
903 $res = $s =~ s/(\w+)/xyz/; $one = $1;
905 is_tainted($s, "$desc: s tainted");
906 isnt_tainted($res, "$desc: res not tainted");
907 is_tainted($one, "$desc: \$1 tainted");
908 is($s, 'xyz', "$desc: s value");
909 is($res, 1, "$desc: res value");
910 is($one, 'abcd', "$desc: \$1 value");
912 $desc = "use re 'taint': substitution /g with pattern tainted via locale";
917 $res = $s =~ s/(\w)/x/g; $one = $1;
919 is_tainted($s, "$desc: s tainted");
920 is_tainted($res, "$desc: res tainted");
921 is_tainted($one, "$desc: \$1 tainted");
922 is($s, 'xxxx', "$desc: s value");
923 is($res, 4, "$desc: res value");
924 is($one, 'd', "$desc: \$1 value");
926 $desc = "use re 'taint': substitution /r with pattern tainted via locale";
931 $res = $s =~ s/(\w+)/xyz/r; $one = $1;
933 isnt_tainted($s, "$desc: s not tainted");
934 is_tainted($res, "$desc: res tainted");
935 is_tainted($one, "$desc: \$1 tainted");
936 is($s, 'abcd', "$desc: s value");
937 is($res, 'xyz', "$desc: res value");
938 is($one, 'abcd', "$desc: \$1 value");
941 $desc = "use re 'taint': substitution with replacement tainted";
944 $res = $s =~ s/(.+)/xyz$TAINT/;
946 is_tainted($s, "$desc: s tainted");
947 isnt_tainted($res, "$desc: res not tainted");
948 isnt_tainted($one, "$desc: \$1 not tainted");
949 is($s, 'xyz', "$desc: s value");
950 is($res, 1, "$desc: res value");
951 is($one, 'abcd', "$desc: \$1 value");
953 $desc = "use re 'taint': substitution /g with replacement tainted";
956 $res = $s =~ s/(.)/x$TAINT/g;
958 is_tainted($s, "$desc: s tainted");
959 isnt_tainted($res, "$desc: res not tainted");
960 isnt_tainted($one, "$desc: \$1 not tainted");
961 is($s, 'xxxx', "$desc: s value");
962 is($res, 4, "$desc: res value");
963 is($one, 'd', "$desc: \$1 value");
965 $desc = "use re 'taint': substitution /ge with replacement tainted";
972 $j = $i; # make sure code not tainted
974 isnt_tainted($j, "$desc: code not tainted within /e");
977 isnt_tainted($s, "$desc: s not tainted loop 1");
980 is_tainted($s, "$desc: s tainted loop $i");
982 isnt_tainted($one, "$desc: \$1 not tainted");
987 is_tainted($s, "$desc: s tainted");
988 isnt_tainted($res, "$desc: res tainted");
989 isnt_tainted($one, "$desc: \$1 not tainted");
990 is($s, '123', "$desc: s value");
991 is($res, 3, "$desc: res value");
992 is($one, 'c', "$desc: \$1 value");
994 $desc = "use re 'taint': substitution /r with replacement tainted";
997 $res = $s =~ s/(.+)/xyz$TAINT/r;
999 isnt_tainted($s, "$desc: s not tainted");
1000 is_tainted($res, "$desc: res tainted");
1001 isnt_tainted($one, "$desc: \$1 not tainted");
1002 is($s, 'abcd', "$desc: s value");
1003 is($res, 'xyz', "$desc: res value");
1004 is($one, 'abcd', "$desc: \$1 value");
1006 # [perl #121854] match taintedness became sticky
1007 # when one match has a taintess result, subseqent matches
1008 # using the same pattern shouldn't necessarily be tainted
1011 my $f = sub { $_[0] =~ /(.*)/ or die; $1 };
1012 $res = $f->($TAINT);
1013 is_tainted($res, "121854: res tainted");
1015 isnt_tainted($res, "121854: res not tainted");
1019 $foo = $1 if 'bar' =~ /(.+)$TAINT/;
1023 my $pi = 4 * atan2(1,1) + $TAINT0;
1026 ($pi) = $pi =~ /(\d+\.\d+)/;
1028 is(sprintf("%.5f", $pi), '3.14159');
1031 # How about command-line arguments? The problem is that we don't
1032 # always get some, so we'll run another process with some.
1034 my $arg = tempfile();
1035 open $fh, '>', $arg or die "Can't create $arg: $!";
1037 eval { join('', @ARGV), kill 0 };
1038 exit 0 if $@ =~ /^Insecure dependency/;
1039 print "# Oops: \$@ was [$@]\n";
1042 close $fh or die "Can't close $arg: $!";
1043 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
1044 is($?, 0, "Exited with status $?");
1048 # Reading from a file should be tainted
1050 ok(open my $fh, '<', $TEST) or diag("Couldn't open '$TEST': $!");
1053 sysread($fh, $block, 100);
1060 # Output of commands should be tainted
1062 my $foo = `$echo abc`;
1066 # Certain system variables should be tainted
1072 # Results of matching should all be untainted
1074 my $foo = "abcdefghi" . $TAINT;
1082 $foo =~ /(...)(...)(...)/;
1088 my @bar = $foo =~ /(...)(...)(...)/;
1089 isnt_tainted($_) foreach @bar;
1091 is_tainted($foo); # $foo should still be tainted!
1092 is($foo, "abcdefghi");
1095 # Operations which affect files can't use tainted data.
1097 violates_taint(sub { chmod 0, $TAINT }, 'chmod');
1100 skip "truncate() is not available", 2 unless $Config{d_truncate};
1102 violates_taint(sub { truncate 'NoSuChFiLe', $TAINT0 }, 'truncate');
1105 violates_taint(sub { rename '', $TAINT }, 'rename');
1106 violates_taint(sub { unlink $TAINT }, 'unlink');
1107 violates_taint(sub { utime $TAINT }, 'utime');
1110 skip "chown() is not available", 2 unless $Config{d_chown};
1112 violates_taint(sub { chown -1, -1, $TAINT }, 'chown');
1116 skip "link() is not available", 2 unless $Config{d_link};
1118 violates_taint(sub { link $TAINT, '' }, 'link');
1122 skip "symlink() is not available", 2 unless $Config{d_symlink};
1124 violates_taint(sub { symlink $TAINT, '' }, 'symlink');
1128 # Operations which affect directories can't use tainted data.
1130 violates_taint(sub { mkdir "foo".$TAINT, 0755 . $TAINT0 }, 'mkdir');
1131 violates_taint(sub { rmdir $TAINT }, 'rmdir');
1132 violates_taint(sub { chdir "foo".$TAINT }, 'chdir');
1135 skip "chroot() is not available", 2 unless $Config{d_chroot};
1137 violates_taint(sub { chroot $TAINT }, 'chroot');
1141 # Some operations using files can't use tainted data.
1143 my $foo = "imaginary library" . $TAINT;
1144 violates_taint(sub { require $foo }, 'require');
1146 my $filename = tempfile(); # NB: $filename isn't tainted!
1147 $foo = $filename . $TAINT;
1148 unlink $filename; # in any case
1150 is(eval { open FOO, $foo }, undef, 'open for read');
1151 is($@, ''); # NB: This should be allowed
1152 is(eval { open my $fh, , '<', $foo }, undef, 'open for read');
1153 is($@, ''); # NB: This should be allowed
1155 # Try first new style but allow also old style.
1156 # We do not want the whole taint.t to fail
1157 # just because Errno possibly failing.
1158 ok(eval('$!{ENOENT}') ||
1159 $! == 2 || # File not found
1160 ($Is_Dos && $! == 22));
1162 violates_taint(sub { open FOO, "> $foo" }, 'open', 'open for write');
1163 violates_taint(sub { open my $fh, '>', $foo }, 'open', 'open for write');
1166 # Commands to the system can't use tainted data
1171 skip "open('|') is not available", 8 if $^O eq 'amigaos';
1173 violates_taint(sub { open FOO, "| x$foo" }, 'piped open', 'popen to');
1174 violates_taint(sub { open FOO, "x$foo |" }, 'piped open', 'popen from');
1175 violates_taint(sub { open my $fh, '|-', "x$foo" }, 'piped open', 'popen to');
1176 violates_taint(sub { open my $fh, '-|', "x$foo" }, 'piped open', 'popen from');
1179 violates_taint(sub { exec $TAINT }, 'exec');
1180 violates_taint(sub { system $TAINT }, 'system');
1185 violates_taint(sub { `$echo 1$foo` }, '``', 'backticks');
1188 # wildcard expansion doesn't invoke shell on VMS, so is safe
1189 skip "This is not VMS", 2 unless $Is_VMS;
1191 isnt(join('', eval { glob $foo } ), '', 'globbing');
1196 # Operations which affect processes can't use tainted data.
1198 violates_taint(sub { kill 0, $TAINT }, 'kill');
1201 skip "setpgrp() is not available", 2 unless $Config{d_setpgrp};
1203 violates_taint(sub { setpgrp 0, $TAINT0 }, 'setpgrp');
1207 skip "setpriority() is not available", 2 unless $Config{d_setprior};
1209 violates_taint(sub { setpriority 0, $TAINT0, $TAINT0 }, 'setpriority');
1213 # Some miscellaneous operations can't use tainted data.
1216 skip "syscall() is not available", 2 unless $Config{d_syscall};
1218 violates_taint(sub { syscall $TAINT }, 'syscall');
1222 my $foo = "x" x 979;
1225 my $temp = tempfile();
1226 ok(open FOO, "> $temp") or diag("Couldn't open $temp for write: $!");
1227 violates_taint(sub { ioctl FOO, $TAINT0, $foo }, 'ioctl');
1229 my $temp2 = tempfile();
1230 ok(open my $fh, '>', $temp2) or diag("Couldn't open $temp2 for write: $!");
1231 violates_taint(sub { ioctl $fh, $TAINT0, $foo }, 'ioctl');
1234 skip "fcntl() is not available", 4 unless $Config{d_fcntl};
1236 violates_taint(sub { fcntl FOO, $TAINT0, $foo }, 'fcntl');
1237 violates_taint(sub { fcntl $fh, $TAINT0, $foo }, 'fcntl');
1244 # Some tests involving references
1246 my $foo = 'abc' . $TAINT;
1248 isnt_tainted($fooref);
1249 is_tainted($$fooref);
1253 # Some tests involving assignment
1259 is_tainted($foo = $bar);
1260 is_tainted($bar = $bar);
1261 is_tainted($bar += $bar);
1262 is_tainted($bar -= $bar);
1263 is_tainted($bar *= $bar);
1265 is_tainted($bar /= $bar);
1266 is_tainted($bar += 0);
1267 is_tainted($bar -= 2);
1268 is_tainted($bar *= -1);
1269 is_tainted($bar /= 1);
1274 # Test assignment and return of lists
1276 my @foo = ("A", "tainted" . $TAINT, "B");
1277 isnt_tainted($foo[0]);
1278 is_tainted( $foo[1]);
1279 isnt_tainted($foo[2]);
1281 isnt_tainted($bar[0]);
1282 is_tainted( $bar[1]);
1283 isnt_tainted($bar[2]);
1284 my @baz = eval { "A", "tainted" . $TAINT, "B" };
1285 isnt_tainted($baz[0]);
1286 is_tainted( $baz[1]);
1287 isnt_tainted($baz[2]);
1288 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
1289 isnt_tainted($plugh[0]);
1290 is_tainted( $plugh[1]);
1291 isnt_tainted($plugh[2]);
1292 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
1293 isnt_tainted(((&$nautilus)[0]));
1294 is_tainted( ((&$nautilus)[1]));
1295 isnt_tainted(((&$nautilus)[2]));
1296 my @xyzzy = &$nautilus;
1297 isnt_tainted($xyzzy[0]);
1298 is_tainted( $xyzzy[1]);
1299 isnt_tainted($xyzzy[2]);
1300 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
1301 isnt_tainted(((&$red_october)[0]));
1302 is_tainted( ((&$red_october)[1]));
1303 isnt_tainted(((&$red_october)[2]));
1304 my @corge = &$red_october;
1305 isnt_tainted($corge[0]);
1306 is_tainted( $corge[1]);
1307 isnt_tainted($corge[2]);
1310 # Test for system/library calls returning string data of dubious origin.
1312 # No reliable %Config check for getpw*
1314 skip "getpwent() is not available", 9 unless
1315 eval { setpwent(); getpwent() };
1318 my @getpwent = getpwent();
1319 die "getpwent: $!\n" unless (@getpwent);
1320 isnt_tainted($getpwent[0]);
1321 is_tainted($getpwent[1]);
1322 isnt_tainted($getpwent[2]);
1323 isnt_tainted($getpwent[3]);
1324 isnt_tainted($getpwent[4]);
1325 isnt_tainted($getpwent[5]);
1326 is_tainted($getpwent[6], 'ge?cos');
1327 isnt_tainted($getpwent[7]);
1328 is_tainted($getpwent[8], 'shell');
1333 # pretty hard to imagine not
1334 skip "readdir() is not available", 1 unless $Config{d_readdir};
1336 opendir my $dh, "op" or die "opendir: $!\n";
1337 my $readdir = readdir $dh;
1338 is_tainted($readdir);
1343 skip "readlink() or symlink() is not available" unless
1344 $Config{d_readlink} && $Config{d_symlink};
1346 my $symlink = "sl$$";
1348 my $sl = "/something/naughty";
1349 # it has to be a real path on Mac OS
1350 symlink($sl, $symlink) or die "symlink: $!\n";
1351 my $readlink = readlink($symlink);
1352 is_tainted($readlink);
1357 # test bitwise ops (regression bug)
1367 # test target of substitution (regression bug)
1369 my $why = $TAINT."y";
1377 $why =~ s/e/'-'.$$/ge;
1383 skip "no IPC::SysV", 2 unless $ipcsysv;
1387 skip "shm*() not available", 1 unless $Config{d_shm};
1390 my $sent = "foobar";
1395 local $SIG{SYS} = sub { die "SIGSYS caught\n" };
1396 $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
1398 } or do { chomp(my $msg = $@); skip "shmget: $msg", 1; };
1401 if (shmwrite($id, $sent, 0, 60)) {
1402 if (shmread($id, $rcvd, 0, 60)) {
1403 substr($rcvd, index($rcvd, "\0")) = '';
1405 warn "# shmread failed: $!\n";
1408 warn "# shmwrite failed: $!\n";
1410 shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
1412 warn "# shmget failed: $!\n";
1415 skip "SysV shared memory operation failed", 1 unless
1418 is_tainted($rcvd, "shmread");
1424 skip "msg*() not available", 1 unless $Config{d_msg};
1429 local $SIG{SYS} = sub { die "SIGSYS caught\n" };
1430 $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
1432 } or do { chomp(my $msg = $@); skip "msgget: $msg", 1; };
1434 my $sent = "message";
1435 my $type_sent = 1234;
1440 if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) {
1441 if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) {
1442 ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
1444 warn "# msgrcv failed: $!\n";
1447 warn "# msgsnd failed: $!\n";
1449 msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
1451 warn "# msgget failed\n";
1455 skip "SysV message queue operation failed", 1
1456 unless $rcvd eq $sent && $type_sent == $type_rcvd;
1458 is_tainted($rcvd, "msgrcv");
1464 # bug id 20001004.006
1466 open my $fh, '<', $TEST or warn "$0: cannot read $TEST: $!" ;
1477 # bug id 20001004.007
1479 open my $fh, '<', $TEST or warn "$0: cannot read $TEST: $!" ;
1485 isnt_tainted($c->{a});
1486 is_tainted($c->{b});
1491 is_tainted($d->{a});
1492 isnt_tainted($d->{b});
1496 b => { c => $a, d => 42 } };
1497 isnt_tainted($e->{a});
1498 isnt_tainted($e->{b});
1499 is_tainted($e->{b}->{c});
1500 isnt_tainted($e->{b}->{d});
1504 # bug id 20010519.003
1507 use vars qw($has_fcntl);
1508 eval { require Fcntl; import Fcntl; };
1515 skip "no Fcntl", 36 unless $has_fcntl;
1517 my $foo = tempfile();
1518 my $evil = $foo . $TAINT;
1520 is(eval { sysopen(my $ro, $evil, &O_RDONLY) }, undef);
1523 violates_taint(sub { sysopen(my $wo, $evil, &O_WRONLY) }, 'sysopen');
1524 violates_taint(sub { sysopen(my $rw, $evil, &O_RDWR) }, 'sysopen');
1525 violates_taint(sub { sysopen(my $ap, $evil, &O_APPEND) }, 'sysopen');
1526 violates_taint(sub { sysopen(my $cr, $evil, &O_CREAT) }, 'sysopen');
1527 violates_taint(sub { sysopen(my $tr, $evil, &O_TRUNC) }, 'sysopen');
1529 is(eval { sysopen(my $ro, $foo, &O_RDONLY | $TAINT0) }, undef);
1532 violates_taint(sub { sysopen(my $wo, $foo, &O_WRONLY | $TAINT0) }, 'sysopen');
1533 violates_taint(sub { sysopen(my $rw, $foo, &O_RDWR | $TAINT0) }, 'sysopen');
1534 violates_taint(sub { sysopen(my $ap, $foo, &O_APPEND | $TAINT0) }, 'sysopen');
1535 violates_taint(sub { sysopen(my $cr, $foo, &O_CREAT | $TAINT0) }, 'sysopen');
1536 violates_taint(sub { sysopen(my $tr, $foo, &O_TRUNC | $TAINT0) }, 'sysopen');
1537 is(eval { sysopen(my $ro, $foo, &O_RDONLY, $TAINT0) }, undef);
1540 violates_taint(sub { sysopen(my $wo, $foo, &O_WRONLY, $TAINT0) }, 'sysopen');
1541 violates_taint(sub { sysopen(my $rw, $foo, &O_RDWR, $TAINT0) }, 'sysopen');
1542 violates_taint(sub { sysopen(my $ap, $foo, &O_APPEND, $TAINT0) }, 'sysopen');
1543 violates_taint(sub { sysopen(my $cr, $foo, &O_CREAT, $TAINT0) }, 'sysopen');
1544 violates_taint(sub { sysopen(my $tr, $foo, &O_TRUNC, $TAINT0) }, 'sysopen');
1553 my $saw_warning = 0;
1554 local $SIG{__WARN__} = sub { ++$saw_warning };
1557 my $divnum = shift()/1;
1558 sprintf("%1.1f\n", $divnum);
1565 is($saw_warning, 0);
1570 # Bug ID 20010730.010
1574 sub Tie::TIESCALAR {
1578 bless \$arg => $class;
1589 my $bar = "The Big Bright Green Pleasure Machine";
1591 tie my ($foo), Tie => $bar;
1599 # Check that all environment variables are tainted.
1601 while (my ($k, $v) = each %ENV) {
1603 # These we have explicitly untainted or set earlier.
1604 $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) {
1605 push @untainted, "# '$k' = '$v'\n";
1608 is("@untainted", "");
1612 is(${^TAINT}, 1, '$^TAINT is on');
1614 eval { ${^TAINT} = 0 };
1615 is(${^TAINT}, 1, '$^TAINT is not assignable');
1616 like($@, qr/^Modification of a read-only value attempted/,
1617 'Assigning to ${^TAINT} fails');
1622 my $re1 = qr/x$TAINT/;
1625 my $re2 = qr/^$re1\z/;
1633 skip "system {} has different semantics on Win32", 1 if $Is_MSWin32;
1636 local $ENV{PATH} .= $TAINT;
1637 eval { system { "echo" } "/arg0", "arg1" };
1638 like($@, qr/^Insecure \$ENV/);
1642 todo_skip 'tainted %ENV warning occludes tainted arguments warning', 22
1645 # bug 20020208.005 plus some single arg exec/system extras
1646 violates_taint(sub { exec $TAINT, $TAINT }, 'exec');
1647 violates_taint(sub { exec $TAINT $TAINT }, 'exec');
1648 violates_taint(sub { exec $TAINT $TAINT, $TAINT }, 'exec');
1649 violates_taint(sub { exec $TAINT 'notaint' }, 'exec');
1650 violates_taint(sub { exec {'notaint'} $TAINT }, 'exec');
1652 violates_taint(sub { system $TAINT, $TAINT }, 'system');
1653 violates_taint(sub { system $TAINT $TAINT }, 'system');
1654 violates_taint(sub { system $TAINT $TAINT, $TAINT }, 'system');
1655 violates_taint(sub { system $TAINT 'notaint' }, 'system');
1656 violates_taint(sub { system {'notaint'} $TAINT }, 'system');
1660 system("lskdfj does not exist","with","args");
1666 exec("lskdfj does not exist","with","args");
1670 # If you add tests here update also the above skip block for VMS.
1674 # [ID 20020704.001] taint propagation failure
1677 is_tainted(my $foo = $1);
1681 # [perl #24291] this used to dump core
1682 our %nonmagicalenv = ( PATH => "util" );
1683 local *ENV = \%nonmagicalenv;
1684 eval { system("lskdfj"); };
1685 like($@, qr/^%ENV is aliased to another variable while running with -T switch/);
1686 local *ENV = *nonmagicalenv;
1687 eval { system("lskdfj"); };
1688 like($@, qr/^%ENV is aliased to %nonmagicalenv while running with -T switch/);
1695 isnt_tainted($notaint);
1698 $notaint =~ /($notaint)/;
1702 $notaint =~ /($TAINT)/;
1707 $TAINT =~ /($notaint)/;
1711 $TAINT =~ /($TAINT)/;
1717 ($r = $TAINT) =~ /($notaint)/;
1719 ($r = $TAINT) =~ /($TAINT)/;
1723 use re 'eval'; # this shouldn't make any difference
1724 ($r = $TAINT) =~ /($notaint)/;
1726 ($r = $TAINT) =~ /($TAINT)/;
1731 # accessing $^O shoudn't taint it as a side-effect;
1732 # assigning tainted data to it is now an error
1735 if (!$^X) { } elsif ($^O eq 'bar') { }
1737 local $^O; # We're going to clobber something test infrastructure depends on.
1739 like($@, qr/Insecure dependency in/);
1742 EFFECTIVELY_CONSTANTS: {
1743 my $tainted_number = 12 + $TAINT0;
1744 is_tainted( $tainted_number );
1746 # Even though it's always 0, it's still tainted
1747 my $tainted_product = $tainted_number * 0;
1748 is_tainted( $tainted_product );
1749 is($tainted_product, 0);
1752 TERNARY_CONDITIONALS: {
1753 my $tainted_true = $TAINT . "blah blah blah";
1754 my $tainted_false = $TAINT0;
1755 is_tainted( $tainted_true );
1756 is_tainted( $tainted_false );
1758 my $result = $tainted_true ? "True" : "False";
1759 is($result, "True");
1760 isnt_tainted( $result );
1762 $result = $tainted_false ? "True" : "False";
1763 is($result, "False");
1764 isnt_tainted( $result );
1766 my $untainted_whatever = "The Fabulous Johnny Cash";
1767 my $tainted_whatever = "Soft Cell" . $TAINT;
1769 $result = $tainted_true ? $tainted_whatever : $untainted_whatever;
1770 is($result, "Soft Cell");
1771 is_tainted( $result );
1773 $result = $tainted_false ? $tainted_whatever : $untainted_whatever;
1774 is($result, "The Fabulous Johnny Cash");
1775 isnt_tainted( $result );
1779 # rt.perl.org 5900 $1 remains tainted if...
1780 # 1) The regular expression contains a scalar variable AND
1781 # 2) The regular expression appears in an elsif clause
1783 my $foo = "abcdefghi" . $TAINT;
1785 my $valid_chars = 'a-z';
1788 elsif ( $foo =~ /([$valid_chars]+)/o ) {
1795 elsif ( my @bar = $foo =~ /([$valid_chars]+)/o ) {
1796 isnt_tainted($bar[0]);
1801 # at scope exit, a restored localised value should have its old
1802 # taint status, not the taint status of the current statement
1811 my $c = do { local $x99; $^X };
1818 my $c = do { local $x99; '' };
1822 # an mg_get of a tainted value during localization shouldn't taint the
1826 eval { local $0, eval '1' };
1830 # [perl #8262] //g loops infinitely on tainted data
1836 cmp_ok pos($a[0]), '>', 0, "infinite m//g on arrays (aelemfast)";
1841 cmp_ok pos($a[$i]), '>', 0, "infinite m//g on arrays (aelem)";
1846 cmp_ok pos($h{a}), '>', 0, "infinite m//g on hashes (helem)";
1852 eval 'use Scalar::Util "dualvar"; $got_dualvar++';
1853 skip "No Scalar::Util::dualvar" unless $got_dualvar;
1854 my $a = Scalar::Util::dualvar(3, $^X);
1856 is ($b, 8, "Arithmetic on tainted dualvars works");
1859 # opening '|-' should not trigger $ENV{PATH} check
1863 skip "fork() is not available", 3 unless $Config{'d_fork'};
1864 skip "opening |- is not stable on threaded Open/MirBSD with taint", 3
1865 if $Config{useithreads} and $Is_OpenBSD || $Is_MirBSD;
1867 $ENV{'PATH'} = $TAINT;
1868 local $SIG{'PIPE'} = 'IGNORE';
1870 my $pid = open my $pipe, '|-';
1871 if (!defined $pid) {
1872 die "open failed: $!";
1875 kill 'KILL', $$; # child suicide
1879 unlike($@, qr/Insecure \$ENV/, 'fork triggers %ENV check');
1880 is($@, '', 'pipe/fork/open/close failed');
1882 open my $pipe, "|$Invoke_Perl -e 1";
1885 like($@, qr/Insecure \$ENV/, 'popen neglects %ENV check');
1890 package AUTOLOAD_TAINT;
1893 return if $AUTOLOAD =~ /DESTROY/;
1894 if ($AUTOLOAD =~ /untainted/) {
1895 main::isnt_tainted($AUTOLOAD, '$AUTOLOAD can be untainted');
1896 my $copy = $AUTOLOAD;
1897 main::isnt_tainted($copy, '$AUTOLOAD can be untainted');
1899 main::is_tainted($AUTOLOAD, '$AUTOLOAD can be tainted');
1900 my $copy = $AUTOLOAD;
1901 main::is_tainted($copy, '$AUTOLOAD can be tainted');
1906 my $o = bless [], 'AUTOLOAD_TAINT';
1913 # tests for tainted format in s?printf
1914 my $fmt = $TAINT . "# %s\n";
1915 violates_taint(sub { printf($fmt, "foo") }, 'printf',
1916 q/printf doesn't like tainted formats/);
1917 violates_taint(sub { printf($TAINT . "# %s\n", "foo") }, 'printf',
1918 q/printf doesn't like tainted format expressions/);
1919 eval { printf("# %s\n", $TAINT . "foo") };
1920 is($@, '', q/printf accepts other tainted args/);
1921 violates_taint(sub { sprintf($fmt, "foo") }, 'sprintf',
1922 q/sprintf doesn't like tainted formats/);
1923 violates_taint(sub { sprintf($TAINT . "# %s\n", "foo") }, 'sprintf',
1924 q/sprintf doesn't like tainted format expressions/);
1925 eval { sprintf("# %s\n", $TAINT . "foo") };
1926 is($@, '', q/sprintf accepts other tainted args/);
1935 is ($val, '7000000000', 'Assignment to untainted variable');
1938 is ($val, '7000000000', 'Assignment to tainted variable');
1943 my $tainted = '1' . $TAINT;
1944 eval '$val = eval $tainted;';
1945 is ($val, 0, "eval doesn't like tainted strings");
1946 like ($@, qr/^Insecure dependency in eval/);
1948 # Rather nice code to get a tainted undef by from Rick Delaney
1949 open my $fh, "test.pl" or die $!;
1950 seek $fh, 0, 2 or die $!;
1953 eval 'eval $tainted';
1954 like ($@, qr/^Insecure dependency in eval/);
1957 foreach my $ord (78, 163, 256) {
1959 my $line = 'A1' . $TAINT . chr $ord;
1963 isnt_tainted($1, "\\S match with chr $ord");
1968 skip 'No crypt function, skipping crypt tests', 4 if(!$Config{d_crypt});
1971 # On platforms implementing FIPS mode, using a weak algorithm
1972 # (including the default triple-DES algorithm) causes crypt(3) to
1973 # return a null pointer, which Perl converts into undef. We assume
1974 # for now that all such platforms support glibc-style selection of
1975 # a different hashing algorithm.
1976 my $alg = ''; # Use default algorithm
1977 if ( !defined(crypt("ab", "cd")) ) {
1978 $alg = '$5$'; # Use SHA-256
1980 my $x = crypt($_[0], $alg . $_[1]);
1983 sub co { my $x = ~$_[0]; $x }
1985 $a = cr('hello', 'foo' . $TAINT);
1986 $b = cr('hello', 'foo');
1987 is_tainted($a, "tainted crypt");
1988 isnt_tainted($b, "untainted crypt");
1989 $a = co('foo' . $TAINT);
1991 is_tainted($a, "tainted complement");
1992 isnt_tainted($b, "untainted complement");
1997 my @data = qw(bonk zam zlonk qunckkk);
1998 # Clearly some sort of usenet bang-path
1999 my $string = $TAINT . join "!", @data;
2001 is_tainted($string, "tainted data");
2003 my @got = split /!|,/, $string;
2005 # each @got would be useful here, but I want the test for earlier perls
2006 for my $i (0 .. $#data) {
2007 is_tainted($got[$i], "tainted result $i");
2008 is($got[$i], $data[$i], "correct content $i");
2011 is_tainted($string, "still tainted data");
2013 my @got = split /[!,]/, $string;
2015 # each @got would be useful here, but I want the test for earlier perls
2016 for my $i (0 .. $#data) {
2017 is_tainted($got[$i], "tainted result $i");
2018 is($got[$i], $data[$i], "correct content $i");
2021 is_tainted($string, "still tainted data");
2023 my @got = split /!/, $string;
2025 # each @got would be useful here, but I want the test for earlier perls
2026 for my $i (0 .. $#data) {
2027 is_tainted($got[$i], "tainted result $i");
2028 is($got[$i], $data[$i], "correct content $i");
2032 # Bug RT #52552 - broken by change at git commit id f337b08
2034 my $x = $TAINT. q{print "Hello world\n"};
2035 my $y = pack "a*", $x;
2036 is_tainted($y, "pack a* preserves tainting");
2038 my $z = pack "A*", q{print "Hello world\n"}.$TAINT;
2039 is_tainted($z, "pack A* preserves tainting");
2041 my $zz = pack "a*a*", q{print "Hello world\n"}, $TAINT;
2042 is_tainted($zz, "pack a*a* preserves tainting");
2045 # Bug RT #61976 tainted $! would show numeric rather than string value
2048 my $tainted_path = substr($^X,0,0) . "/no/such/file";
2050 # $! is used in a tainted expression, so gets tainted
2051 open my $fh, $tainted_path or $err= "$!";
2052 unlike($err, qr/^\d+$/, 'tainted $!');
2056 # #6758: tainted values become untainted in tied hashes
2057 # (also applies to other value magic such as pos)
2062 sub TIEHASH { bless {} }
2063 sub TIEARRAY { bless {} }
2068 main::is_tainted($_[1], "tied arg1 tainted");
2069 main::is_tainted($_[2], "tied arg2 tainted");
2075 my ($k,$v) = qw(1111 val);
2077 tie my @array, 'P6758';
2078 tie my %hash , 'P6758';
2081 ok $i == 2, "tied STORE called correct number of times";
2084 # Bug RT #45167 the return value of sprintf sometimes wasn't tainted
2085 # when the args were tainted. This only occurred on the first use of
2086 # sprintf; after that, its TARG has taint magic attached, so setmagic
2087 # at the end works. That's why there are multiple sprintf's below, rather
2088 # than just one wrapped in an inner loop. Also, any plaintext between
2089 # fprmat entries would correctly cause tainting to get set. so test with
2090 # "%s%s" rather than eg "%s %s".
2093 for my $var1 ($TAINT, "123") {
2094 for my $var2 ($TAINT0, "456") {
2095 is( tainted(sprintf '%s', $var1, $var2), tainted($var1),
2096 "sprintf '%s', '$var1', '$var2'" );
2097 is( tainted(sprintf ' %s', $var1, $var2), tainted($var1),
2098 "sprintf ' %s', '$var1', '$var2'" );
2099 is( tainted(sprintf '%s%s', $var1, $var2),
2100 tainted($var1) || tainted($var2),
2101 "sprintf '%s%s', '$var1', '$var2'" );
2107 # Bug RT #67962: old tainted $1 gets treated as tainted
2108 # in next untainted # match
2112 "abc".$TAINT =~ /(.*)/; # make $1 tainted
2113 is_tainted($1, '$1 should be tainted');
2115 my $untainted = "abcdef";
2116 isnt_tainted($untainted, '$untainted should be untainted');
2117 $untainted =~ s/(abc)/$1/;
2118 isnt_tainted($untainted, '$untainted should still be untainted');
2119 $untainted =~ s/(abc)/x$1/;
2120 isnt_tainted($untainted, '$untainted should yet still be untainted');
2124 # On Windows we can't spawn a fresh Perl interpreter unless at
2125 # least the Windows system directory (usually C:\Windows\System32)
2126 # is still on the PATH. There is however no way to determine the
2127 # actual path on the current system without loading the Win32
2128 # module, so we just restore the original $ENV{PATH} here.
2129 local $ENV{PATH} = $ENV{PATH};
2130 $ENV{PATH} = $old_env_path if $Is_MSWin32;
2132 fresh_perl_is(<<'end', "ok", { switches => [ '-T' ] },
2133 $TAINT = substr($^X, 0, 0);
2134 formline('@'.('<'x("2000".$TAINT)).' | @*', 'hallo', 'welt');
2137 "formline survives a tainted dynamic picture");
2141 isnt_tainted($^A, "format accumulator not tainted yet");
2142 formline('@ | @*', 'hallo' . $TAINT, 'welt');
2143 is_tainted($^A, "tainted formline argument makes a tainted accumulator");
2145 isnt_tainted($^A, "accumulator can be explicitly untainted");
2146 formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2147 isnt_tainted($^A, "accumulator still untainted");
2149 is_tainted($^A, "accumulator can be explicitly tainted");
2150 formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2151 is_tainted($^A, "accumulator still tainted");
2153 isnt_tainted($^A, "accumulator untainted again");
2154 formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2155 isnt_tainted($^A, "accumulator still untainted");
2156 formline('@' .('<'*(5+$TAINT0)) . ' | @*', 'hallo', 'welt');
2157 is_tainted($^A, "the accumulator should be tainted already");
2158 is_tainted($^A, "tainted formline picture makes a tainted accumulator");
2162 "Constant(1)" =~ / ^ ([a-z_]\w*) (?: [(] (.*) [)] )? $ /xi;
2165 isnt_tainted($a, "regex optimization of single char /[]/i doesn't taint");
2166 isnt_tainted($b, "regex optimization of single char /[]/i doesn't taint");
2170 # RT 81230: tainted value during FETCH created extra ref to tied obj
2178 my $x = $^X; # tainted
2181 sub FETCH { my $x = $_[0]; $$x . "" }
2186 local $SIG{__WARN__} = sub { $w .= "@_" };
2188 untie %h if $h{"k"};
2190 ::is($w, "", "RT 81230");
2194 # Compiling a subroutine inside a tainted expression does not make the
2195 # constant folded values tainted.
2196 my $x = sub { "x" . "y" };
2197 my $y = $ENV{PATH} . $x->(); # Compile $x inside a tainted expression
2199 isnt_tainted($z, "Constants folded value not tainted");
2203 # now that regexes are first class SVs, make sure that they themselves
2204 # as well as references to them are tainted
2206 my $rr = qr/(.)$TAINT/;
2207 my $r = $$rr; # bare REGEX
2209 ok($s =~ s/$r/x/, "match bare regex");
2210 is_tainted($s, "match bare regex taint");
2211 is($s, 'xbc', "match bare regex taint value");
2215 # [perl #82616] security Issues with user-defined \p{} properties
2216 # A using a tainted user-defined property should croak
2218 sub IsA { sprintf "%02x", ord("A") }
2221 ok("A" =~ /\p{$prop}/, "user-defined property: non-tainted case");
2222 $prop = "IsA$TAINT";
2223 eval { "A" =~ /\p{$prop}/};
2224 like($@, qr/Insecure user-defined property \\p\{main::IsA}/,
2225 "user-defined property: tainted case");
2229 # [perl #87336] lc/uc(first) failing to taint the returned string
2230 my $source = "foo$TAINT";
2231 my $dest = lc $source;
2232 is_tainted $dest, "lc(tainted) taints its return value";
2233 $dest = lcfirst $source;
2234 is_tainted $dest, "lcfirst(tainted) taints its return value";
2236 is_tainted $dest, "uc(tainted) taints its return value";
2237 $dest = ucfirst $source;
2238 is_tainted $dest, "ucfirst(tainted) taints its return value";
2242 # Taintedness of values returned from given()
2243 use feature 'switch';
2244 no warnings 'experimental::smartmatch';
2246 my @descriptions = ('when', 'given end', 'default');
2249 my $letter = "$_$TAINT";
2251 my $desc = "tainted value returned from " . shift(@descriptions);
2255 when ('x') { $letter }
2256 when ('y') { goto leavegiven }
2261 is $res, $letter, "$desc is correct";
2262 is_tainted $res, "$desc stays tainted";
2267 # tainted constants and index()
2268 # RT 64804; http://bugs.debian.org/291450
2270 ok(tainted $old_env_path, "initial taintedness");
2271 BEGIN { no strict 'refs'; my $v = $old_env_path; *{"::C"} = sub () { $v }; }
2272 ok(tainted C, "constant is tainted properly");
2273 ok(!tainted "", "tainting not broken yet");
2275 ok(!tainted "", "tainting still works after index() of the constant");
2278 # Tainted values with smartmatch
2279 # [perl #93590] S_do_smartmatch stealing its own string buffers
2281 no warnings 'experimental::smartmatch';
2282 ok "M$TAINT" ~~ ['m', 'M'], '$tainted ~~ ["whatever", "match"]';
2283 ok !("M$TAINT" ~~ ['m', undef]), '$tainted ~~ ["whatever", undef]';
2286 # Tainted values and ref()
2288 my $x = bless \"M$TAINT", ref(bless[], "main");
2290 pass("no death when TARG of ref is tainted");
2292 # $$ should not be tainted by being read in a tainted expression.
2294 isnt_tainted $$, "PID not tainted initially";
2295 my $x = $ENV{PATH}.$$;
2296 isnt_tainted $$, "PID not tainted when read in tainted expression";
2300 skip 'No locale testing without d_setlocale', 4 if(!$Config{d_setlocale});
2304 my ($latin1, $utf8) = ("\xDF") x 2;
2305 utf8::downgrade($latin1);
2306 utf8::upgrade($utf8);
2308 is_tainted fc($latin1), "under locale, lc(latin1) taints the result";
2309 is_tainted fc($utf8), "under locale, lc(utf8) taints the result";
2311 is_tainted "\F$latin1", "under locale, \\Flatin1 taints the result";
2312 is_tainted "\F$utf8", "under locale, \\Futf8 taints the result";
2317 eval { die "Test\n".substr($ENV{PATH}, 0, 0); };
2320 like($@, qr/^Test\n\t\.\.\.propagated at /, "error should be propagated");
2323 # tainted run-time (?{}) should die
2326 my $code = '(?{})' . $TAINT;
2328 eval { "a" =~ /$code/ };
2329 like($@, qr/Eval-group in insecure regular expression/, "tainted (?{})");
2332 # reset() and tainted undef (?!)
2334 $_ = "$TAINT".reset "x";
2335 is eval { eval $::x.1 }, 1, 'reset does not taint undef';
2339 # See the comment above the first formline test.
2340 local $ENV{PATH} = $ENV{PATH};
2341 $ENV{PATH} = $old_env_path if $Is_MSWin32;
2343 switches => [ '-T' ],
2344 prog => 'use constant K=>$^X; 0 if K; BEGIN{} use strict; '
2345 .'print 122669, qq-\n-',
2348 'tainted constant as logop condition should not prevent "use"';
2351 # This may bomb out with the alarm signal so keep it last
2353 skip "No alarm()" unless $Config{d_alarm};
2354 # Test from RT #41831]
2355 # [PATCH] Bug & fix: hang when using study + taint mode (perl 5.6.1, 5.8.x)
2357 my $DATA = <<'END' . $TAINT;
2367 ## don't set $SIG{ALRM}, since we'd never get to a user-level handler as
2368 ## perl is stuck in a regexp infinite loop!
2372 if ($DATA =~ /^line2.*line4/m) {
2373 fail("Should not be a match")
2375 pass("Match on tainted multiline data should fail promptly");
2381 # Keep the previous test last