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?
14 skip_all_if_miniperl("no dynamic loading on miniperl, no re");
24 use vars qw($ipcsysv); # did we manage to load IPC::SysV?
26 my ($old_env_path, $old_env_dcl_path, $old_env_term);
28 $old_env_path = $ENV{'PATH'};
29 $old_env_dcl_path = $ENV{'DCL$PATH'};
30 $old_env_term = $ENV{'TERM'};
31 if ($^O eq 'VMS' && !defined($Config{d_setenv})) {
32 $ENV{PATH} = $ENV{PATH};
33 $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy';
35 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/
36 && ($Config{d_shm} || $Config{d_msg})) {
37 eval { require IPC::SysV };
40 IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU IPC_NOWAIT));
45 my $Is_VMS = $^O eq 'VMS';
46 my $Is_MSWin32 = $^O eq 'MSWin32';
47 my $Is_NetWare = $^O eq 'NetWare';
48 my $Is_Dos = $^O eq 'dos';
49 my $Is_Cygwin = $^O eq 'cygwin';
50 my $Is_OpenBSD = $^O eq 'openbsd';
51 my $Is_MirBSD = $^O eq 'mirbsd';
52 my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.exe' :
53 $Is_MSWin32 ? '.\perl' :
54 $Is_NetWare ? 'perl' :
56 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
60 for $x ('DCL$PATH', @MoreEnv) {
61 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
63 # VMS note: PATH and TERM are automatically created by the C
64 # library in VMS on reference to the their keys in %ENV.
65 # There is currently no way to determine if they did not exist
66 # before this test was run.
69 \$ENV{PATH} = \$old_env_path;
70 warn "# Note: logical name 'PATH' may have been created\n";
71 \$ENV{'TERM'} = \$old_env_term;
72 warn "# Note: logical name 'TERM' may have been created\n";
73 \@ENV{keys %old} = values %old;
74 if (defined \$old_env_dcl_path) {
75 \$ENV{'DCL\$PATH'} = \$old_env_dcl_path;
77 delete \$ENV{'DCL\$PATH'};
84 # The empty tainted value, for tainting strings
85 my $TAINT = substr($^X, 0, 0);
86 # A tainted zero, useful for tainting numbers
93 # This taints each argument passed. All must be lvalues.
94 # Side effect: It also stringifies them. :-(
96 for (@_) { $_ .= $TAINT }
99 # How to identify taint when you see it
101 local $@; # Don't pollute caller's value.
102 not eval { join("",@_), kill 0; 1 };
107 local $::Level = $::Level + 1;
108 ok(tainted($thing), @_);
113 local $::Level = $::Level + 1;
114 ok(!tainted($thing), @_);
118 my ($code, $what, $desc) = @_;
120 local $::Level = $::Level + 1;
121 is(eval { $code->(); }, undef, $desc);
122 like($@, qr/^Insecure dependency in $what while running with -T switch/);
125 # We need an external program to call.
126 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$"));
128 open my $fh, '>', $ECHO or die "Can't create $ECHO: $!";
129 print $fh 'print "@ARGV\n"', "\n";
131 my $echo = "$Invoke_Perl $ECHO";
135 # First, let's make sure that Perl is checking the dangerous
136 # environment variables. Maybe they aren't set yet, so we'll
137 # taint them ourselves.
139 $ENV{'DCL$PATH'} = '' if $Is_VMS;
141 $ENV{PATH} = ($Is_Cygwin) ? '/usr/bin' : '';
142 delete @ENV{@MoreEnv};
145 is(eval { `$echo 1` }, "1\n");
148 skip "Environment tainting tests skipped", 4
149 if $Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos;
151 my @vars = ('PATH', @MoreEnv);
152 while (my $v = $vars[0]) {
153 local $ENV{$v} = $TAINT;
154 last if eval { `$echo 1` };
155 last unless $@ =~ /^Insecure \$ENV\{$v}/;
160 # tainted $TERM is unsafe only if it contains metachars
162 $ENV{TERM} = 'e=mc2';
163 is(eval { `$echo 1` }, "1\n");
164 $ENV{TERM} = 'e=mc2' . $TAINT;
165 is(eval { `$echo 1` }, undef);
166 like($@, qr/^Insecure \$ENV\{TERM}/);
170 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) {
171 print "# all directories are writeable\n";
174 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
175 qw(sys$scratch /tmp /var/tmp /usr/tmp),
176 @ENV{qw(TMP TEMP)})[0]
177 or print "# can't find world-writeable directory to test PATH\n";
181 skip "all directories are writeable", 2 unless $tmp;
183 local $ENV{PATH} = $tmp;
184 is(eval { `$echo 1` }, undef);
185 like($@, qr/^Insecure directory in \$ENV\{PATH}/);
189 skip "This is not VMS", 4 unless $Is_VMS;
191 $ENV{'DCL$PATH'} = $TAINT;
192 is(eval { `$echo 1` }, undef);
193 like($@, qr/^Insecure \$ENV\{DCL\$PATH}/);
195 skip q[can't find world-writeable directory to test DCL$PATH], 2
198 $ENV{'DCL$PATH'} = $tmp;
199 is(eval { `$echo 1` }, undef);
200 like($@, qr/^Insecure directory in \$ENV\{DCL\$PATH}/);
202 $ENV{'DCL$PATH'} = '';
206 # Let's see that we can taint and untaint as needed.
211 # That was a sanity check. If it failed, stop the insanity!
212 die "Taint checks don't seem to be enabled" unless tainted $foo;
221 isnt_tainted($_) foreach @list;
222 taint_these @list[1,3,5,7,9];
223 is_tainted($_) foreach @list[1,3,5,7,9];
224 isnt_tainted($_) foreach @list[0,2,4,6,8];
226 ($foo) = $foo =~ /(.+)/;
229 my ($desc, $s, $res, $res2, $one);
231 $desc = "match with string tainted";
233 $s = 'abcd' . $TAINT;
236 is_tainted($s, "$desc: s tainted");
237 isnt_tainted($res, "$desc: res not tainted");
238 isnt_tainted($one, "$desc: \$1 not tainted");
239 is($res, 1, "$desc: res value");
240 is($one, 'abcd', "$desc: \$1 value");
242 $desc = "match /g with string tainted";
244 $s = 'abcd' . $TAINT;
247 is_tainted($s, "$desc: s tainted");
248 isnt_tainted($res, "$desc: res not tainted");
249 isnt_tainted($one, "$desc: \$1 not tainted");
250 is($res, 1, "$desc: res value");
251 is($one, 'a', "$desc: \$1 value");
253 $desc = "match with string tainted, list cxt";
255 $s = 'abcd' . $TAINT;
256 ($res) = $s =~ /(.+)/;
258 is_tainted($s, "$desc: s tainted");
259 isnt_tainted($res, "$desc: res not tainted");
260 isnt_tainted($one, "$desc: \$1 not tainted");
261 is($res, 'abcd', "$desc: res value");
262 is($one, 'abcd', "$desc: \$1 value");
264 $desc = "match /g with string tainted, list cxt";
266 $s = 'abcd' . $TAINT;
267 ($res, $res2) = $s =~ /(.)/g;
269 is_tainted($s, "$desc: s tainted");
270 isnt_tainted($res, "$desc: res not tainted");
271 isnt_tainted($res2,"$desc: res2 not tainted");
272 isnt_tainted($one, "$desc: \$1 not tainted");
273 is($res, 'a', "$desc: res value");
274 is($res2,'b', "$desc: res2 value");
275 is($one, 'd', "$desc: \$1 value");
277 $desc = "match with pattern tainted";
280 $res = $s =~ /$TAINT(.+)/;
282 isnt_tainted($s, "$desc: s not tainted");
283 isnt_tainted($res, "$desc: res not tainted");
284 is_tainted($one, "$desc: \$1 tainted");
285 is($res, 1, "$desc: res value");
286 is($one, 'abcd', "$desc: \$1 value");
288 $desc = "match /g with pattern tainted";
291 $res = $s =~ /$TAINT(.)/g;
293 isnt_tainted($s, "$desc: s not tainted");
294 isnt_tainted($res, "$desc: res not tainted");
295 is_tainted($one, "$desc: \$1 tainted");
296 is($res, 1, "$desc: res value");
297 is($one, 'a', "$desc: \$1 value");
299 $desc = "match with pattern tainted via locale";
302 { use locale; $res = $s =~ /(\w+)/; $one = $1; }
303 isnt_tainted($s, "$desc: s not tainted");
304 isnt_tainted($res, "$desc: res not tainted");
305 is_tainted($one, "$desc: \$1 tainted");
306 is($res, 1, "$desc: res value");
307 is($one, 'abcd', "$desc: \$1 value");
309 $desc = "match /g with pattern tainted via locale";
312 { use locale; $res = $s =~ /(\w)/g; $one = $1; }
313 isnt_tainted($s, "$desc: s not tainted");
314 isnt_tainted($res, "$desc: res not tainted");
315 is_tainted($one, "$desc: \$1 tainted");
316 is($res, 1, "$desc: res value");
317 is($one, 'a', "$desc: \$1 value");
319 $desc = "match with pattern tainted, list cxt";
322 ($res) = $s =~ /$TAINT(.+)/;
324 isnt_tainted($s, "$desc: s not tainted");
325 is_tainted($res, "$desc: res tainted");
326 is_tainted($one, "$desc: \$1 tainted");
327 is($res, 'abcd', "$desc: res value");
328 is($one, 'abcd', "$desc: \$1 value");
330 $desc = "match /g with pattern tainted, list cxt";
333 ($res, $res2) = $s =~ /$TAINT(.)/g;
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, 'a', "$desc: res value");
339 is($res2,'b', "$desc: res2 value");
340 is($one, 'd', "$desc: \$1 value");
342 $desc = "match with pattern tainted via locale, list cxt";
345 { use locale; ($res) = $s =~ /(\w+)/; $one = $1; }
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, 'abcd', "$desc: res value");
350 is($one, 'abcd', "$desc: \$1 value");
352 $desc = "match /g with pattern tainted via locale, list cxt";
355 { use locale; ($res, $res2) = $s =~ /(\w)/g; $one = $1; }
356 isnt_tainted($s, "$desc: s not tainted");
357 is_tainted($res, "$desc: res tainted");
358 is_tainted($res2, "$desc: res2 tainted");
359 is_tainted($one, "$desc: \$1 tainted");
360 is($res, 'a', "$desc: res value");
361 is($res2,'b', "$desc: res2 value");
362 is($one, 'd', "$desc: \$1 value");
364 $desc = "substitution with string tainted";
366 $s = 'abcd' . $TAINT;
367 $res = $s =~ s/(.+)/xyz/;
369 is_tainted($s, "$desc: s tainted");
370 isnt_tainted($res, "$desc: res not tainted");
371 isnt_tainted($one, "$desc: \$1 not tainted");
372 is($s, 'xyz', "$desc: s value");
373 is($res, 1, "$desc: res value");
374 is($one, 'abcd', "$desc: \$1 value");
376 $desc = "substitution /g with string tainted";
378 $s = 'abcd' . $TAINT;
379 $res = $s =~ s/(.)/x/g;
381 is_tainted($s, "$desc: s tainted");
382 is_tainted($res, "$desc: res tainted");
383 isnt_tainted($one, "$desc: \$1 not tainted");
384 is($s, 'xxxx', "$desc: s value");
385 is($res, 4, "$desc: res value");
386 is($one, 'd', "$desc: \$1 value");
388 $desc = "substitution /r with string tainted";
390 $s = 'abcd' . $TAINT;
391 $res = $s =~ s/(.+)/xyz/r;
393 is_tainted($s, "$desc: s tainted");
394 is_tainted($res, "$desc: res tainted");
395 isnt_tainted($one, "$desc: \$1 not tainted");
396 is($s, 'abcd', "$desc: s value");
397 is($res, 'xyz', "$desc: res value");
398 is($one, 'abcd', "$desc: \$1 value");
400 $desc = "substitution /e with string tainted";
402 $s = 'abcd' . $TAINT;
404 $res = $s =~ s{(.+)}{
405 $one = $one . "x"; # make sure code not tainted
406 isnt_tainted($one, "$desc: code not tainted within /e");
408 isnt_tainted($one, "$desc: \$1 not tainted within /e");
412 is_tainted($s, "$desc: s tainted");
413 isnt_tainted($res, "$desc: res not tainted");
414 isnt_tainted($one, "$desc: \$1 not tainted");
415 is($s, 'xyz', "$desc: s value");
416 is($res, 1, "$desc: res value");
417 is($one, 'abcd', "$desc: \$1 value");
419 $desc = "substitution with pattern tainted";
422 $res = $s =~ s/$TAINT(.+)/xyz/;
424 is_tainted($s, "$desc: s tainted");
425 isnt_tainted($res, "$desc: res not tainted");
426 is_tainted($one, "$desc: \$1 tainted");
427 is($s, 'xyz', "$desc: s value");
428 is($res, 1, "$desc: res value");
429 is($one, 'abcd', "$desc: \$1 value");
431 $desc = "substitution /g with pattern tainted";
434 $res = $s =~ s/$TAINT(.)/x/g;
436 is_tainted($s, "$desc: s tainted");
437 is_tainted($res, "$desc: res tainted");
438 is_tainted($one, "$desc: \$1 tainted");
439 is($s, 'xxxx', "$desc: s value");
440 is($res, 4, "$desc: res value");
441 is($one, 'd', "$desc: \$1 value");
443 $desc = "substitution /ge with pattern tainted";
449 $res = $s =~ s{(.)$TAINT}{
450 $j = $i; # make sure code not tainted
452 isnt_tainted($j, "$desc: code not tainted within /e");
455 isnt_tainted($s, "$desc: s not tainted loop 1");
458 is_tainted($s, "$desc: s tainted loop $i");
460 is_tainted($one, "$desc: \$1 tainted loop $i");
465 is_tainted($s, "$desc: s tainted");
466 is_tainted($res, "$desc: res tainted");
467 is_tainted($one, "$desc: \$1 tainted");
468 is($s, '123', "$desc: s value");
469 is($res, 3, "$desc: res value");
470 is($one, 'c', "$desc: \$1 value");
472 $desc = "substitution /r with pattern tainted";
475 $res = $s =~ s/$TAINT(.+)/xyz/r;
477 isnt_tainted($s, "$desc: s not tainted");
478 is_tainted($res, "$desc: res tainted");
479 is_tainted($one, "$desc: \$1 tainted");
480 is($s, 'abcd', "$desc: s value");
481 is($res, 'xyz', "$desc: res value");
482 is($one, 'abcd', "$desc: \$1 value");
484 $desc = "substitution with pattern tainted via locale";
487 { use locale; $res = $s =~ s/(\w+)/xyz/; $one = $1; }
488 is_tainted($s, "$desc: s tainted");
489 isnt_tainted($res, "$desc: res not tainted");
490 is_tainted($one, "$desc: \$1 tainted");
491 is($s, 'xyz', "$desc: s value");
492 is($res, 1, "$desc: res value");
493 is($one, 'abcd', "$desc: \$1 value");
495 $desc = "substitution /g with pattern tainted via locale";
498 { use locale; $res = $s =~ s/(\w)/x/g; $one = $1; }
499 is_tainted($s, "$desc: s tainted");
500 is_tainted($res, "$desc: res tainted");
501 is_tainted($one, "$desc: \$1 tainted");
502 is($s, 'xxxx', "$desc: s value");
503 is($res, 4, "$desc: res value");
504 is($one, 'd', "$desc: \$1 value");
506 $desc = "substitution /r with pattern tainted via locale";
509 { use locale; $res = $s =~ s/(\w+)/xyz/r; $one = $1; }
510 isnt_tainted($s, "$desc: s not tainted");
511 is_tainted($res, "$desc: res tainted");
512 is_tainted($one, "$desc: \$1 tainted");
513 is($s, 'abcd', "$desc: s value");
514 is($res, 'xyz', "$desc: res value");
515 is($one, 'abcd', "$desc: \$1 value");
517 $desc = "substitution with replacement tainted";
520 $res = $s =~ s/(.+)/xyz$TAINT/;
522 is_tainted($s, "$desc: s tainted");
523 isnt_tainted($res, "$desc: res not tainted");
524 isnt_tainted($one, "$desc: \$1 not tainted");
525 is($s, 'xyz', "$desc: s value");
526 is($res, 1, "$desc: res value");
527 is($one, 'abcd', "$desc: \$1 value");
529 $desc = "substitution /g with replacement tainted";
532 $res = $s =~ s/(.)/x$TAINT/g;
534 is_tainted($s, "$desc: s tainted");
535 isnt_tainted($res, "$desc: res not tainted");
536 isnt_tainted($one, "$desc: \$1 not tainted");
537 is($s, 'xxxx', "$desc: s value");
538 is($res, 4, "$desc: res value");
539 is($one, 'd', "$desc: \$1 value");
541 $desc = "substitution /ge with replacement tainted";
548 $j = $i; # make sure code not tainted
550 isnt_tainted($j, "$desc: code not tainted within /e");
553 isnt_tainted($s, "$desc: s not tainted loop 1");
556 is_tainted($s, "$desc: s tainted loop $i");
558 isnt_tainted($one, "$desc: \$1 not tainted within /e");
563 is_tainted($s, "$desc: s tainted");
564 is_tainted($res, "$desc: res tainted");
565 isnt_tainted($one, "$desc: \$1 not tainted");
566 is($s, '123', "$desc: s value");
567 is($res, 3, "$desc: res value");
568 is($one, 'c', "$desc: \$1 value");
570 $desc = "substitution /r with replacement tainted";
573 $res = $s =~ s/(.+)/xyz$TAINT/r;
575 isnt_tainted($s, "$desc: s not tainted");
576 is_tainted($res, "$desc: res tainted");
577 isnt_tainted($one, "$desc: \$1 not tainted");
578 is($s, 'abcd', "$desc: s value");
579 is($res, 'xyz', "$desc: res value");
580 is($one, 'abcd', "$desc: \$1 value");
583 # now do them all again with "use re 'taint"
587 $desc = "use re 'taint': match with string tainted";
589 $s = 'abcd' . $TAINT;
592 is_tainted($s, "$desc: s tainted");
593 isnt_tainted($res, "$desc: res not tainted");
594 is_tainted($one, "$desc: \$1 tainted");
595 is($res, 1, "$desc: res value");
596 is($one, 'abcd', "$desc: \$1 value");
598 $desc = "use re 'taint': match /g with string tainted";
600 $s = 'abcd' . $TAINT;
603 is_tainted($s, "$desc: s tainted");
604 isnt_tainted($res, "$desc: res not tainted");
605 is_tainted($one, "$desc: \$1 tainted");
606 is($res, 1, "$desc: res value");
607 is($one, 'a', "$desc: \$1 value");
609 $desc = "use re 'taint': match with string tainted, list cxt";
611 $s = 'abcd' . $TAINT;
612 ($res) = $s =~ /(.+)/;
614 is_tainted($s, "$desc: s tainted");
615 is_tainted($res, "$desc: res tainted");
616 is_tainted($one, "$desc: \$1 tainted");
617 is($res, 'abcd', "$desc: res value");
618 is($one, 'abcd', "$desc: \$1 value");
620 $desc = "use re 'taint': match /g with string tainted, list cxt";
622 $s = 'abcd' . $TAINT;
623 ($res, $res2) = $s =~ /(.)/g;
625 is_tainted($s, "$desc: s tainted");
626 is_tainted($res, "$desc: res tainted");
627 is_tainted($res2, "$desc: res2 tainted");
628 is_tainted($one, "$desc: \$1 not tainted");
629 is($res, 'a', "$desc: res value");
630 is($res2,'b', "$desc: res2 value");
631 is($one, 'd', "$desc: \$1 value");
633 $desc = "use re 'taint': match with pattern tainted";
636 $res = $s =~ /$TAINT(.+)/;
638 isnt_tainted($s, "$desc: s not tainted");
639 isnt_tainted($res, "$desc: res not tainted");
640 is_tainted($one, "$desc: \$1 tainted");
641 is($res, 1, "$desc: res value");
642 is($one, 'abcd', "$desc: \$1 value");
644 $desc = "use re 'taint': match /g with pattern tainted";
647 $res = $s =~ /$TAINT(.)/g;
649 isnt_tainted($s, "$desc: s not tainted");
650 isnt_tainted($res, "$desc: res not tainted");
651 is_tainted($one, "$desc: \$1 tainted");
652 is($res, 1, "$desc: res value");
653 is($one, 'a', "$desc: \$1 value");
655 $desc = "use re 'taint': match with pattern tainted via locale";
658 { use locale; $res = $s =~ /(\w+)/; $one = $1; }
659 isnt_tainted($s, "$desc: s not tainted");
660 isnt_tainted($res, "$desc: res not tainted");
661 is_tainted($one, "$desc: \$1 tainted");
662 is($res, 1, "$desc: res value");
663 is($one, 'abcd', "$desc: \$1 value");
665 $desc = "use re 'taint': match /g with pattern tainted via locale";
668 { use locale; $res = $s =~ /(\w)/g; $one = $1; }
669 isnt_tainted($s, "$desc: s not tainted");
670 isnt_tainted($res, "$desc: res not tainted");
671 is_tainted($one, "$desc: \$1 tainted");
672 is($res, 1, "$desc: res value");
673 is($one, 'a', "$desc: \$1 value");
675 $desc = "use re 'taint': match with pattern tainted, list cxt";
678 ($res) = $s =~ /$TAINT(.+)/;
680 isnt_tainted($s, "$desc: s not tainted");
681 is_tainted($res, "$desc: res tainted");
682 is_tainted($one, "$desc: \$1 tainted");
683 is($res, 'abcd', "$desc: res value");
684 is($one, 'abcd', "$desc: \$1 value");
686 $desc = "use re 'taint': match /g with pattern tainted, list cxt";
689 ($res, $res2) = $s =~ /$TAINT(.)/g;
691 isnt_tainted($s, "$desc: s not tainted");
692 is_tainted($res, "$desc: res tainted");
693 is_tainted($one, "$desc: \$1 tainted");
694 is($res, 'a', "$desc: res value");
695 is($res2,'b', "$desc: res2 value");
696 is($one, 'd', "$desc: \$1 value");
698 $desc = "use re 'taint': match with pattern tainted via locale, list cxt";
701 { use locale; ($res) = $s =~ /(\w+)/; $one = $1; }
702 isnt_tainted($s, "$desc: s not tainted");
703 is_tainted($res, "$desc: res tainted");
704 is_tainted($one, "$desc: \$1 tainted");
705 is($res, 'abcd', "$desc: res value");
706 is($one, 'abcd', "$desc: \$1 value");
708 $desc = "use re 'taint': match /g with pattern tainted via locale, list cxt";
711 { use locale; ($res, $res2) = $s =~ /(\w)/g; $one = $1; }
712 isnt_tainted($s, "$desc: s not tainted");
713 is_tainted($res, "$desc: res tainted");
714 is_tainted($res2, "$desc: res2 tainted");
715 is_tainted($one, "$desc: \$1 tainted");
716 is($res, 'a', "$desc: res value");
717 is($res2,'b', "$desc: res2 value");
718 is($one, 'd', "$desc: \$1 value");
720 $desc = "use re 'taint': substitution with string tainted";
722 $s = 'abcd' . $TAINT;
723 $res = $s =~ s/(.+)/xyz/;
725 is_tainted($s, "$desc: s tainted");
726 isnt_tainted($res, "$desc: res not tainted");
727 is_tainted($one, "$desc: \$1 tainted");
728 is($s, 'xyz', "$desc: s value");
729 is($res, 1, "$desc: res value");
730 is($one, 'abcd', "$desc: \$1 value");
732 $desc = "use re 'taint': substitution /g with string tainted";
734 $s = 'abcd' . $TAINT;
735 $res = $s =~ s/(.)/x/g;
737 is_tainted($s, "$desc: s tainted");
738 is_tainted($res, "$desc: res tainted");
739 is_tainted($one, "$desc: \$1 tainted");
740 is($s, 'xxxx', "$desc: s value");
741 is($res, 4, "$desc: res value");
742 is($one, 'd', "$desc: \$1 value");
744 $desc = "use re 'taint': substitution /r with string tainted";
746 $s = 'abcd' . $TAINT;
747 $res = $s =~ s/(.+)/xyz/r;
749 is_tainted($s, "$desc: s tainted");
750 is_tainted($res, "$desc: res tainted");
751 is_tainted($one, "$desc: \$1 tainted");
752 is($s, 'abcd', "$desc: s value");
753 is($res, 'xyz', "$desc: res value");
754 is($one, 'abcd', "$desc: \$1 value");
756 $desc = "use re 'taint': substitution /e with string tainted";
758 $s = 'abcd' . $TAINT;
760 $res = $s =~ s{(.+)}{
761 $one = $one . "x"; # make sure code not tainted
762 isnt_tainted($one, "$desc: code not tainted within /e");
764 is_tainted($one, "$desc: $1 tainted within /e");
768 is_tainted($s, "$desc: s tainted");
769 isnt_tainted($res, "$desc: res not tainted");
770 is_tainted($one, "$desc: \$1 tainted");
771 is($s, 'xyz', "$desc: s value");
772 is($res, 1, "$desc: res value");
773 is($one, 'abcd', "$desc: \$1 value");
775 $desc = "use re 'taint': substitution with pattern tainted";
778 $res = $s =~ s/$TAINT(.+)/xyz/;
780 is_tainted($s, "$desc: s tainted");
781 isnt_tainted($res, "$desc: res not tainted");
782 is_tainted($one, "$desc: \$1 tainted");
783 is($s, 'xyz', "$desc: s value");
784 is($res, 1, "$desc: res value");
785 is($one, 'abcd', "$desc: \$1 value");
787 $desc = "use re 'taint': substitution /g with pattern tainted";
790 $res = $s =~ s/$TAINT(.)/x/g;
792 is_tainted($s, "$desc: s tainted");
793 is_tainted($res, "$desc: res tainted");
794 is_tainted($one, "$desc: \$1 tainted");
795 is($s, 'xxxx', "$desc: s value");
796 is($res, 4, "$desc: res value");
797 is($one, 'd', "$desc: \$1 value");
799 $desc = "use re 'taint': substitution /ge with pattern tainted";
805 $res = $s =~ s{(.)$TAINT}{
806 $j = $i; # make sure code not tainted
808 isnt_tainted($j, "$desc: code not tainted within /e");
811 isnt_tainted($s, "$desc: s not tainted loop 1");
814 is_tainted($s, "$desc: s tainted loop $i");
816 is_tainted($one, "$desc: \$1 tainted loop $i");
821 is_tainted($s, "$desc: s tainted");
822 is_tainted($res, "$desc: res tainted");
823 is_tainted($one, "$desc: \$1 tainted");
824 is($s, '123', "$desc: s value");
825 is($res, 3, "$desc: res value");
826 is($one, 'c', "$desc: \$1 value");
829 $desc = "use re 'taint': substitution /r with pattern tainted";
832 $res = $s =~ s/$TAINT(.+)/xyz/r;
834 isnt_tainted($s, "$desc: s not tainted");
835 is_tainted($res, "$desc: res tainted");
836 is_tainted($one, "$desc: \$1 tainted");
837 is($s, 'abcd', "$desc: s value");
838 is($res, 'xyz', "$desc: res value");
839 is($one, 'abcd', "$desc: \$1 value");
841 $desc = "use re 'taint': substitution with pattern tainted via locale";
844 { use locale; $res = $s =~ s/(\w+)/xyz/; $one = $1; }
845 is_tainted($s, "$desc: s tainted");
846 isnt_tainted($res, "$desc: res not tainted");
847 is_tainted($one, "$desc: \$1 tainted");
848 is($s, 'xyz', "$desc: s value");
849 is($res, 1, "$desc: res value");
850 is($one, 'abcd', "$desc: \$1 value");
852 $desc = "use re 'taint': substitution /g with pattern tainted via locale";
855 { use locale; $res = $s =~ s/(\w)/x/g; $one = $1; }
856 is_tainted($s, "$desc: s tainted");
857 is_tainted($res, "$desc: res tainted");
858 is_tainted($one, "$desc: \$1 tainted");
859 is($s, 'xxxx', "$desc: s value");
860 is($res, 4, "$desc: res value");
861 is($one, 'd', "$desc: \$1 value");
863 $desc = "use re 'taint': substitution /r with pattern tainted via locale";
866 { use locale; $res = $s =~ s/(\w+)/xyz/r; $one = $1; }
867 isnt_tainted($s, "$desc: s not tainted");
868 is_tainted($res, "$desc: res tainted");
869 is_tainted($one, "$desc: \$1 tainted");
870 is($s, 'abcd', "$desc: s value");
871 is($res, 'xyz', "$desc: res value");
872 is($one, 'abcd', "$desc: \$1 value");
874 $desc = "use re 'taint': substitution with replacement tainted";
877 $res = $s =~ s/(.+)/xyz$TAINT/;
879 is_tainted($s, "$desc: s tainted");
880 isnt_tainted($res, "$desc: res not tainted");
881 isnt_tainted($one, "$desc: \$1 not tainted");
882 is($s, 'xyz', "$desc: s value");
883 is($res, 1, "$desc: res value");
884 is($one, 'abcd', "$desc: \$1 value");
886 $desc = "use re 'taint': substitution /g with replacement tainted";
889 $res = $s =~ s/(.)/x$TAINT/g;
891 is_tainted($s, "$desc: s tainted");
892 isnt_tainted($res, "$desc: res not tainted");
893 isnt_tainted($one, "$desc: \$1 not tainted");
894 is($s, 'xxxx', "$desc: s value");
895 is($res, 4, "$desc: res value");
896 is($one, 'd', "$desc: \$1 value");
898 $desc = "use re 'taint': substitution /ge with replacement tainted";
905 $j = $i; # make sure code not tainted
907 isnt_tainted($j, "$desc: code not tainted within /e");
910 isnt_tainted($s, "$desc: s not tainted loop 1");
913 is_tainted($s, "$desc: s tainted loop $i");
915 isnt_tainted($one, "$desc: \$1 not tainted");
920 is_tainted($s, "$desc: s tainted");
921 is_tainted($res, "$desc: res tainted");
922 isnt_tainted($one, "$desc: \$1 not tainted");
923 is($s, '123', "$desc: s value");
924 is($res, 3, "$desc: res value");
925 is($one, 'c', "$desc: \$1 value");
927 $desc = "use re 'taint': substitution /r with replacement tainted";
930 $res = $s =~ s/(.+)/xyz$TAINT/r;
932 isnt_tainted($s, "$desc: s not tainted");
933 is_tainted($res, "$desc: res tainted");
934 isnt_tainted($one, "$desc: \$1 not tainted");
935 is($s, 'abcd', "$desc: s value");
936 is($res, 'xyz', "$desc: res value");
937 is($one, 'abcd', "$desc: \$1 value");
940 $foo = $1 if 'bar' =~ /(.+)$TAINT/;
944 my $pi = 4 * atan2(1,1) + $TAINT0;
947 ($pi) = $pi =~ /(\d+\.\d+)/;
949 is(sprintf("%.5f", $pi), '3.14159');
952 # How about command-line arguments? The problem is that we don't
953 # always get some, so we'll run another process with some.
955 my $arg = tempfile();
956 open $fh, '>', $arg or die "Can't create $arg: $!";
958 eval { join('', @ARGV), kill 0 };
959 exit 0 if $@ =~ /^Insecure dependency/;
960 print "# Oops: \$@ was [$@]\n";
963 close $fh or die "Can't close $arg: $!";
964 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
965 is($?, 0, "Exited with status $?");
969 # Reading from a file should be tainted
971 ok(open my $fh, '<', $TEST) or diag("Couldn't open '$TEST': $!");
974 sysread($fh, $block, 100);
981 # Output of commands should be tainted
983 my $foo = `$echo abc`;
987 # Certain system variables should be tainted
993 # Results of matching should all be untainted
995 my $foo = "abcdefghi" . $TAINT;
1003 $foo =~ /(...)(...)(...)/;
1009 my @bar = $foo =~ /(...)(...)(...)/;
1010 isnt_tainted($_) foreach @bar;
1012 is_tainted($foo); # $foo should still be tainted!
1013 is($foo, "abcdefghi");
1016 # Operations which affect files can't use tainted data.
1018 violates_taint(sub { chmod 0, $TAINT }, 'chmod');
1021 skip "truncate() is not available", 2 unless $Config{d_truncate};
1023 violates_taint(sub { truncate 'NoSuChFiLe', $TAINT0 }, 'truncate');
1026 violates_taint(sub { rename '', $TAINT }, 'rename');
1027 violates_taint(sub { unlink $TAINT }, 'unlink');
1028 violates_taint(sub { utime $TAINT }, 'utime');
1031 skip "chown() is not available", 2 unless $Config{d_chown};
1033 violates_taint(sub { chown -1, -1, $TAINT }, 'chown');
1037 skip "link() is not available", 2 unless $Config{d_link};
1039 violates_taint(sub { link $TAINT, '' }, 'link');
1043 skip "symlink() is not available", 2 unless $Config{d_symlink};
1045 violates_taint(sub { symlink $TAINT, '' }, 'symlink');
1049 # Operations which affect directories can't use tainted data.
1051 violates_taint(sub { mkdir "foo".$TAINT, 0755 . $TAINT0 }, 'mkdir');
1052 violates_taint(sub { rmdir $TAINT }, 'rmdir');
1053 violates_taint(sub { chdir "foo".$TAINT }, 'chdir');
1056 skip "chroot() is not available", 2 unless $Config{d_chroot};
1058 violates_taint(sub { chroot $TAINT }, 'chroot');
1062 # Some operations using files can't use tainted data.
1064 my $foo = "imaginary library" . $TAINT;
1065 violates_taint(sub { require $foo }, 'require');
1067 my $filename = tempfile(); # NB: $filename isn't tainted!
1068 $foo = $filename . $TAINT;
1069 unlink $filename; # in any case
1071 is(eval { open FOO, $foo }, undef, 'open for read');
1072 is($@, ''); # NB: This should be allowed
1073 is(eval { open my $fh, , '<', $foo }, undef, 'open for read');
1074 is($@, ''); # NB: This should be allowed
1076 # Try first new style but allow also old style.
1077 # We do not want the whole taint.t to fail
1078 # just because Errno possibly failing.
1079 ok(eval('$!{ENOENT}') ||
1080 $! == 2 || # File not found
1081 ($Is_Dos && $! == 22));
1083 violates_taint(sub { open FOO, "> $foo" }, 'open', 'open for write');
1084 violates_taint(sub { open my $fh, '>', $foo }, 'open', 'open for write');
1087 # Commands to the system can't use tainted data
1092 skip "open('|') is not available", 8 if $^O eq 'amigaos';
1094 violates_taint(sub { open FOO, "| x$foo" }, 'piped open', 'popen to');
1095 violates_taint(sub { open FOO, "x$foo |" }, 'piped open', 'popen from');
1096 violates_taint(sub { open my $fh, '|-', "x$foo" }, 'piped open', 'popen to');
1097 violates_taint(sub { open my $fh, '-|', "x$foo" }, 'piped open', 'popen from');
1100 violates_taint(sub { exec $TAINT }, 'exec');
1101 violates_taint(sub { system $TAINT }, 'system');
1106 violates_taint(sub { `$echo 1$foo` }, '``', 'backticks');
1109 # wildcard expansion doesn't invoke shell on VMS, so is safe
1110 skip "This is not VMS", 2 unless $Is_VMS;
1112 isnt(join('', eval { glob $foo } ), '', 'globbing');
1117 # Operations which affect processes can't use tainted data.
1119 violates_taint(sub { kill 0, $TAINT }, 'kill');
1122 skip "setpgrp() is not available", 2 unless $Config{d_setpgrp};
1124 violates_taint(sub { setpgrp 0, $TAINT0 }, 'setpgrp');
1128 skip "setpriority() is not available", 2 unless $Config{d_setprior};
1130 violates_taint(sub { setpriority 0, $TAINT0, $TAINT0 }, 'setpriority');
1134 # Some miscellaneous operations can't use tainted data.
1137 skip "syscall() is not available", 2 unless $Config{d_syscall};
1139 violates_taint(sub { syscall $TAINT }, 'syscall');
1143 my $foo = "x" x 979;
1146 my $temp = tempfile();
1147 ok(open FOO, "> $temp") or diag("Couldn't open $temp for write: $!");
1148 violates_taint(sub { ioctl FOO, $TAINT0, $foo }, 'ioctl');
1150 my $temp2 = tempfile();
1151 ok(open my $fh, '>', $temp2) or diag("Couldn't open $temp2 for write: $!");
1152 violates_taint(sub { ioctl $fh, $TAINT0, $foo }, 'ioctl');
1155 skip "fcntl() is not available", 4 unless $Config{d_fcntl};
1157 violates_taint(sub { fcntl FOO, $TAINT0, $foo }, 'fcntl');
1158 violates_taint(sub { fcntl $fh, $TAINT0, $foo }, 'fcntl');
1165 # Some tests involving references
1167 my $foo = 'abc' . $TAINT;
1169 isnt_tainted($fooref);
1170 is_tainted($$fooref);
1174 # Some tests involving assignment
1180 is_tainted($foo = $bar);
1181 is_tainted($bar = $bar);
1182 is_tainted($bar += $bar);
1183 is_tainted($bar -= $bar);
1184 is_tainted($bar *= $bar);
1186 is_tainted($bar /= $bar);
1187 is_tainted($bar += 0);
1188 is_tainted($bar -= 2);
1189 is_tainted($bar *= -1);
1190 is_tainted($bar /= 1);
1195 # Test assignment and return of lists
1197 my @foo = ("A", "tainted" . $TAINT, "B");
1198 isnt_tainted($foo[0]);
1199 is_tainted( $foo[1]);
1200 isnt_tainted($foo[2]);
1202 isnt_tainted($bar[0]);
1203 is_tainted( $bar[1]);
1204 isnt_tainted($bar[2]);
1205 my @baz = eval { "A", "tainted" . $TAINT, "B" };
1206 isnt_tainted($baz[0]);
1207 is_tainted( $baz[1]);
1208 isnt_tainted($baz[2]);
1209 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
1210 isnt_tainted($plugh[0]);
1211 is_tainted( $plugh[1]);
1212 isnt_tainted($plugh[2]);
1213 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
1214 isnt_tainted(((&$nautilus)[0]));
1215 is_tainted( ((&$nautilus)[1]));
1216 isnt_tainted(((&$nautilus)[2]));
1217 my @xyzzy = &$nautilus;
1218 isnt_tainted($xyzzy[0]);
1219 is_tainted( $xyzzy[1]);
1220 isnt_tainted($xyzzy[2]);
1221 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
1222 isnt_tainted(((&$red_october)[0]));
1223 is_tainted( ((&$red_october)[1]));
1224 isnt_tainted(((&$red_october)[2]));
1225 my @corge = &$red_october;
1226 isnt_tainted($corge[0]);
1227 is_tainted( $corge[1]);
1228 isnt_tainted($corge[2]);
1231 # Test for system/library calls returning string data of dubious origin.
1233 # No reliable %Config check for getpw*
1235 skip "getpwent() is not available", 9 unless
1236 eval { setpwent(); getpwent() };
1239 my @getpwent = getpwent();
1240 die "getpwent: $!\n" unless (@getpwent);
1241 isnt_tainted($getpwent[0]);
1242 is_tainted($getpwent[1]);
1243 isnt_tainted($getpwent[2]);
1244 isnt_tainted($getpwent[3]);
1245 isnt_tainted($getpwent[4]);
1246 isnt_tainted($getpwent[5]);
1247 is_tainted($getpwent[6], 'ge?cos');
1248 isnt_tainted($getpwent[7]);
1249 is_tainted($getpwent[8], 'shell');
1254 # pretty hard to imagine not
1255 skip "readdir() is not available", 1 unless $Config{d_readdir};
1257 opendir my $dh, "op" or die "opendir: $!\n";
1258 my $readdir = readdir $dh;
1259 is_tainted($readdir);
1264 skip "readlink() or symlink() is not available" unless
1265 $Config{d_readlink} && $Config{d_symlink};
1267 my $symlink = "sl$$";
1269 my $sl = "/something/naughty";
1270 # it has to be a real path on Mac OS
1271 symlink($sl, $symlink) or die "symlink: $!\n";
1272 my $readlink = readlink($symlink);
1273 is_tainted($readlink);
1278 # test bitwise ops (regression bug)
1288 # test target of substitution (regression bug)
1290 my $why = $TAINT."y";
1298 $why =~ s/e/'-'.$$/ge;
1304 skip "no IPC::SysV", 2 unless $ipcsysv;
1308 skip "shm*() not available", 1 unless $Config{d_shm};
1311 my $sent = "foobar";
1314 my $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
1317 if (shmwrite($id, $sent, 0, 60)) {
1318 if (shmread($id, $rcvd, 0, 60)) {
1319 substr($rcvd, index($rcvd, "\0")) = '';
1321 warn "# shmread failed: $!\n";
1324 warn "# shmwrite failed: $!\n";
1326 shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
1328 warn "# shmget failed: $!\n";
1331 skip "SysV shared memory operation failed", 1 unless
1340 skip "msg*() not available", 1 unless $Config{d_msg};
1343 my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
1345 my $sent = "message";
1346 my $type_sent = 1234;
1351 if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) {
1352 if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) {
1353 ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
1355 warn "# msgrcv failed: $!\n";
1358 warn "# msgsnd failed: $!\n";
1360 msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
1362 warn "# msgget failed\n";
1366 skip "SysV message queue operation failed", 1
1367 unless $rcvd eq $sent && $type_sent == $type_rcvd;
1375 # bug id 20001004.006
1377 open my $fh, '<', $TEST or warn "$0: cannot read $TEST: $!" ;
1388 # bug id 20001004.007
1390 open my $fh, '<', $TEST or warn "$0: cannot read $TEST: $!" ;
1396 isnt_tainted($c->{a});
1397 is_tainted($c->{b});
1402 is_tainted($d->{a});
1403 isnt_tainted($d->{b});
1407 b => { c => $a, d => 42 } };
1408 isnt_tainted($e->{a});
1409 isnt_tainted($e->{b});
1410 is_tainted($e->{b}->{c});
1411 isnt_tainted($e->{b}->{d});
1415 # bug id 20010519.003
1418 use vars qw($has_fcntl);
1419 eval { require Fcntl; import Fcntl; };
1426 skip "no Fcntl", 18 unless $has_fcntl;
1428 my $foo = tempfile();
1429 my $evil = $foo . $TAINT;
1431 is(eval { sysopen(my $ro, $evil, &O_RDONLY) }, undef);
1434 violates_taint(sub { sysopen(my $wo, $evil, &O_WRONLY) }, 'sysopen');
1435 violates_taint(sub { sysopen(my $rw, $evil, &O_RDWR) }, 'sysopen');
1436 violates_taint(sub { sysopen(my $ap, $evil, &O_APPEND) }, 'sysopen');
1437 violates_taint(sub { sysopen(my $cr, $evil, &O_CREAT) }, 'sysopen');
1438 violates_taint(sub { sysopen(my $tr, $evil, &O_TRUNC) }, 'sysopen');
1440 is(eval { sysopen(my $ro, $foo, &O_RDONLY | $TAINT0) }, undef);
1443 violates_taint(sub { sysopen(my $wo, $foo, &O_WRONLY | $TAINT0) }, 'sysopen');
1444 violates_taint(sub { sysopen(my $rw, $foo, &O_RDWR | $TAINT0) }, 'sysopen');
1445 violates_taint(sub { sysopen(my $ap, $foo, &O_APPEND | $TAINT0) }, 'sysopen');
1446 violates_taint(sub { sysopen(my $cr, $foo, &O_CREAT | $TAINT0) }, 'sysopen');
1447 violates_taint(sub { sysopen(my $tr, $foo, &O_TRUNC | $TAINT0) }, 'sysopen');
1448 is(eval { sysopen(my $ro, $foo, &O_RDONLY, $TAINT0) }, undef);
1451 violates_taint(sub { sysopen(my $wo, $foo, &O_WRONLY, $TAINT0) }, 'sysopen');
1452 violates_taint(sub { sysopen(my $rw, $foo, &O_RDWR, $TAINT0) }, 'sysopen');
1453 violates_taint(sub { sysopen(my $ap, $foo, &O_APPEND, $TAINT0) }, 'sysopen');
1454 violates_taint(sub { sysopen(my $cr, $foo, &O_CREAT, $TAINT0) }, 'sysopen');
1455 violates_taint(sub { sysopen(my $tr, $foo, &O_TRUNC, $TAINT0) }, 'sysopen');
1464 my $saw_warning = 0;
1465 local $SIG{__WARN__} = sub { ++$saw_warning };
1468 my $divnum = shift()/1;
1469 sprintf("%1.1f\n", $divnum);
1476 is($saw_warning, 0);
1481 # Bug ID 20010730.010
1485 sub Tie::TIESCALAR {
1489 bless \$arg => $class;
1500 my $bar = "The Big Bright Green Pleasure Machine";
1502 tie my ($foo), Tie => $bar;
1510 # Check that all environment variables are tainted.
1512 while (my ($k, $v) = each %ENV) {
1514 # These we have explicitly untainted or set earlier.
1515 $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) {
1516 push @untainted, "# '$k' = '$v'\n";
1519 is("@untainted", "");
1523 is(${^TAINT}, 1, '$^TAINT is on');
1525 eval { ${^TAINT} = 0 };
1526 is(${^TAINT}, 1, '$^TAINT is not assignable');
1527 like($@, qr/^Modification of a read-only value attempted/,
1528 'Assigning to ${^TAINT} fails');
1533 my $re1 = qr/x$TAINT/;
1536 my $re2 = qr/^$re1\z/;
1544 skip "system {} has different semantics on Win32", 1 if $Is_MSWin32;
1547 local $ENV{PATH} .= $TAINT;
1548 eval { system { "echo" } "/arg0", "arg1" };
1549 like($@, qr/^Insecure \$ENV/);
1553 todo_skip 'tainted %ENV warning occludes tainted arguments warning', 22
1556 # bug 20020208.005 plus some single arg exec/system extras
1557 violates_taint(sub { exec $TAINT, $TAINT }, 'exec');
1558 violates_taint(sub { exec $TAINT $TAINT }, 'exec');
1559 violates_taint(sub { exec $TAINT $TAINT, $TAINT }, 'exec');
1560 violates_taint(sub { exec $TAINT 'notaint' }, 'exec');
1561 violates_taint(sub { exec {'notaint'} $TAINT }, 'exec');
1563 violates_taint(sub { system $TAINT, $TAINT }, 'system');
1564 violates_taint(sub { system $TAINT $TAINT }, 'system');
1565 violates_taint(sub { system $TAINT $TAINT, $TAINT }, 'system');
1566 violates_taint(sub { system $TAINT 'notaint' }, 'system');
1567 violates_taint(sub { system {'notaint'} $TAINT }, 'system');
1571 system("lskdfj does not exist","with","args");
1577 exec("lskdfj does not exist","with","args");
1581 # If you add tests here update also the above skip block for VMS.
1585 # [ID 20020704.001] taint propagation failure
1588 is_tainted(my $foo = $1);
1592 # [perl #24291] this used to dump core
1593 our %nonmagicalenv = ( PATH => "util" );
1594 local *ENV = \%nonmagicalenv;
1595 eval { system("lskdfj"); };
1596 like($@, qr/^%ENV is aliased to another variable while running with -T switch/);
1597 local *ENV = *nonmagicalenv;
1598 eval { system("lskdfj"); };
1599 like($@, qr/^%ENV is aliased to %nonmagicalenv while running with -T switch/);
1606 isnt_tainted($notaint);
1609 $notaint =~ /($notaint)/;
1613 $notaint =~ /($TAINT)/;
1618 $TAINT =~ /($notaint)/;
1622 $TAINT =~ /($TAINT)/;
1628 ($r = $TAINT) =~ /($notaint)/;
1630 ($r = $TAINT) =~ /($TAINT)/;
1634 use re 'eval'; # this shouldn't make any difference
1635 ($r = $TAINT) =~ /($notaint)/;
1637 ($r = $TAINT) =~ /($TAINT)/;
1642 # accessing $^O shoudn't taint it as a side-effect;
1643 # assigning tainted data to it is now an error
1646 if (!$^X) { } elsif ($^O eq 'bar') { }
1648 local $^O; # We're going to clobber something test infrastructure depends on.
1650 like($@, qr/Insecure dependency in/);
1653 EFFECTIVELY_CONSTANTS: {
1654 my $tainted_number = 12 + $TAINT0;
1655 is_tainted( $tainted_number );
1657 # Even though it's always 0, it's still tainted
1658 my $tainted_product = $tainted_number * 0;
1659 is_tainted( $tainted_product );
1660 is($tainted_product, 0);
1663 TERNARY_CONDITIONALS: {
1664 my $tainted_true = $TAINT . "blah blah blah";
1665 my $tainted_false = $TAINT0;
1666 is_tainted( $tainted_true );
1667 is_tainted( $tainted_false );
1669 my $result = $tainted_true ? "True" : "False";
1670 is($result, "True");
1671 isnt_tainted( $result );
1673 $result = $tainted_false ? "True" : "False";
1674 is($result, "False");
1675 isnt_tainted( $result );
1677 my $untainted_whatever = "The Fabulous Johnny Cash";
1678 my $tainted_whatever = "Soft Cell" . $TAINT;
1680 $result = $tainted_true ? $tainted_whatever : $untainted_whatever;
1681 is($result, "Soft Cell");
1682 is_tainted( $result );
1684 $result = $tainted_false ? $tainted_whatever : $untainted_whatever;
1685 is($result, "The Fabulous Johnny Cash");
1686 isnt_tainted( $result );
1690 # rt.perl.org 5900 $1 remains tainted if...
1691 # 1) The regular expression contains a scalar variable AND
1692 # 2) The regular expression appears in an elsif clause
1694 my $foo = "abcdefghi" . $TAINT;
1696 my $valid_chars = 'a-z';
1699 elsif ( $foo =~ /([$valid_chars]+)/o ) {
1706 elsif ( my @bar = $foo =~ /([$valid_chars]+)/o ) {
1707 isnt_tainted($bar[0]);
1712 # at scope exit, a restored localised value should have its old
1713 # taint status, not the taint status of the current statement
1722 my $c = do { local $x99; $^X };
1729 my $c = do { local $x99; '' };
1733 # an mg_get of a tainted value during localization shouldn't taint the
1737 eval { local $0, eval '1' };
1741 # [perl #8262] //g loops infinitely on tainted data
1747 cmp_ok pos($a[0]), '>', 0, "infinite m//g on arrays (aelemfast)";
1752 cmp_ok pos($a[$i]), '>', 0, "infinite m//g on arrays (aelem)";
1757 cmp_ok pos($h{a}), '>', 0, "infinite m//g on hashes (helem)";
1763 eval 'use Scalar::Util "dualvar"; $got_dualvar++';
1764 skip "No Scalar::Util::dualvar" unless $got_dualvar;
1765 my $a = Scalar::Util::dualvar(3, $^X);
1767 is ($b, 8, "Arithmetic on tainted dualvars works");
1770 # opening '|-' should not trigger $ENV{PATH} check
1774 skip "fork() is not available", 3 unless $Config{'d_fork'};
1775 skip "opening |- is not stable on threaded Open/MirBSD with taint", 3
1776 if $Config{useithreads} and $Is_OpenBSD || $Is_MirBSD;
1778 $ENV{'PATH'} = $TAINT;
1779 local $SIG{'PIPE'} = 'IGNORE';
1781 my $pid = open my $pipe, '|-';
1782 if (!defined $pid) {
1783 die "open failed: $!";
1786 kill 'KILL', $$; # child suicide
1790 unlike($@, qr/Insecure \$ENV/, 'fork triggers %ENV check');
1791 is($@, '', 'pipe/fork/open/close failed');
1793 open my $pipe, "|$Invoke_Perl -e 1";
1796 like($@, qr/Insecure \$ENV/, 'popen neglects %ENV check');
1801 package AUTOLOAD_TAINT;
1804 return if $AUTOLOAD =~ /DESTROY/;
1805 if ($AUTOLOAD =~ /untainted/) {
1806 main::isnt_tainted($AUTOLOAD, '$AUTOLOAD can be untainted');
1807 my $copy = $AUTOLOAD;
1808 main::isnt_tainted($copy, '$AUTOLOAD can be untainted');
1810 main::is_tainted($AUTOLOAD, '$AUTOLOAD can be tainted');
1811 my $copy = $AUTOLOAD;
1812 main::is_tainted($copy, '$AUTOLOAD can be tainted');
1817 my $o = bless [], 'AUTOLOAD_TAINT';
1824 # tests for tainted format in s?printf
1825 my $fmt = $TAINT . "# %s\n";
1826 violates_taint(sub { printf($fmt, "foo") }, 'printf',
1827 q/printf doesn't like tainted formats/);
1828 violates_taint(sub { printf($TAINT . "# %s\n", "foo") }, 'printf',
1829 q/printf doesn't like tainted format expressions/);
1830 eval { printf("# %s\n", $TAINT . "foo") };
1831 is($@, '', q/printf accepts other tainted args/);
1832 violates_taint(sub { sprintf($fmt, "foo") }, 'sprintf',
1833 q/sprintf doesn't like tainted formats/);
1834 violates_taint(sub { sprintf($TAINT . "# %s\n", "foo") }, 'sprintf',
1835 q/sprintf doesn't like tainted format expressions/);
1836 eval { sprintf("# %s\n", $TAINT . "foo") };
1837 is($@, '', q/sprintf accepts other tainted args/);
1846 is ($val, '7000000000', 'Assignment to untainted variable');
1849 is ($val, '7000000000', 'Assignment to tainted variable');
1854 my $tainted = '1' . $TAINT;
1855 eval '$val = eval $tainted;';
1856 is ($val, 0, "eval doesn't like tainted strings");
1857 like ($@, qr/^Insecure dependency in eval/);
1859 # Rather nice code to get a tainted undef by from Rick Delaney
1860 open my $fh, "test.pl" or die $!;
1861 seek $fh, 0, 2 or die $!;
1864 eval 'eval $tainted';
1865 like ($@, qr/^Insecure dependency in eval/);
1868 foreach my $ord (78, 163, 256) {
1870 my $line = 'A1' . $TAINT . chr $ord;
1874 isnt_tainted($1, "\\S match with chr $ord");
1879 sub cr { my $x = crypt($_[0], $_[1]); $x }
1880 sub co { my $x = ~$_[0]; $x }
1882 $a = cr('hello', 'foo' . $TAINT);
1883 $b = cr('hello', 'foo');
1884 is_tainted($a, "tainted crypt");
1885 isnt_tainted($b, "untainted crypt");
1886 $a = co('foo' . $TAINT);
1888 is_tainted($a, "tainted complement");
1889 isnt_tainted($b, "untainted complement");
1893 my @data = qw(bonk zam zlonk qunckkk);
1894 # Clearly some sort of usenet bang-path
1895 my $string = $TAINT . join "!", @data;
1897 is_tainted($string, "tainted data");
1899 my @got = split /!|,/, $string;
1901 # each @got would be useful here, but I want the test for earlier perls
1902 for my $i (0 .. $#data) {
1903 is_tainted($got[$i], "tainted result $i");
1904 is($got[$i], $data[$i], "correct content $i");
1907 is_tainted($string, "still tainted data");
1909 my @got = split /[!,]/, $string;
1911 # each @got would be useful here, but I want the test for earlier perls
1912 for my $i (0 .. $#data) {
1913 is_tainted($got[$i], "tainted result $i");
1914 is($got[$i], $data[$i], "correct content $i");
1917 is_tainted($string, "still tainted data");
1919 my @got = split /!/, $string;
1921 # each @got would be useful here, but I want the test for earlier perls
1922 for my $i (0 .. $#data) {
1923 is_tainted($got[$i], "tainted result $i");
1924 is($got[$i], $data[$i], "correct content $i");
1928 # Bug RT #52552 - broken by change at git commit id f337b08
1930 my $x = $TAINT. q{print "Hello world\n"};
1931 my $y = pack "a*", $x;
1932 is_tainted($y, "pack a* preserves tainting");
1934 my $z = pack "A*", q{print "Hello world\n"}.$TAINT;
1935 is_tainted($z, "pack A* preserves tainting");
1937 my $zz = pack "a*a*", q{print "Hello world\n"}, $TAINT;
1938 is_tainted($zz, "pack a*a* preserves tainting");
1941 # Bug RT #61976 tainted $! would show numeric rather than string value
1944 my $tainted_path = substr($^X,0,0) . "/no/such/file";
1946 # $! is used in a tainted expression, so gets tainted
1947 open my $fh, $tainted_path or $err= "$!";
1948 unlike($err, qr/^\d+$/, 'tainted $!');
1952 # #6758: tainted values become untainted in tied hashes
1953 # (also applies to other value magic such as pos)
1958 sub TIEHASH { bless {} }
1959 sub TIEARRAY { bless {} }
1964 main::is_tainted($_[1], "tied arg1 tainted");
1965 main::is_tainted($_[2], "tied arg2 tainted");
1971 my ($k,$v) = qw(1111 val);
1973 tie my @array, 'P6758';
1974 tie my %hash , 'P6758';
1977 ok $i == 2, "tied STORE called correct number of times";
1980 # Bug RT #45167 the return value of sprintf sometimes wasn't tainted
1981 # when the args were tainted. This only occured on the first use of
1982 # sprintf; after that, its TARG has taint magic attached, so setmagic
1983 # at the end works. That's why there are multiple sprintf's below, rather
1984 # than just one wrapped in an inner loop. Also, any plaintext between
1985 # fprmat entires would correctly cause tainting to get set. so test with
1986 # "%s%s" rather than eg "%s %s".
1989 for my $var1 ($TAINT, "123") {
1990 for my $var2 ($TAINT0, "456") {
1991 is( tainted(sprintf '%s', $var1, $var2), tainted($var1),
1992 "sprintf '%s', '$var1', '$var2'" );
1993 is( tainted(sprintf ' %s', $var1, $var2), tainted($var1),
1994 "sprintf ' %s', '$var1', '$var2'" );
1995 is( tainted(sprintf '%s%s', $var1, $var2),
1996 tainted($var1) || tainted($var2),
1997 "sprintf '%s%s', '$var1', '$var2'" );
2003 # Bug RT #67962: old tainted $1 gets treated as tainted
2004 # in next untainted # match
2008 "abc".$TAINT =~ /(.*)/; # make $1 tainted
2009 is_tainted($1, '$1 should be tainted');
2011 my $untainted = "abcdef";
2012 isnt_tainted($untainted, '$untainted should be untainted');
2013 $untainted =~ s/(abc)/$1/;
2014 isnt_tainted($untainted, '$untainted should still be untainted');
2015 $untainted =~ s/(abc)/x$1/;
2016 isnt_tainted($untainted, '$untainted should yet still be untainted');
2020 # On Windows we can't spawn a fresh Perl interpreter unless at
2021 # least the Windows system directory (usually C:\Windows\System32)
2022 # is still on the PATH. There is however no way to determine the
2023 # actual path on the current system without loading the Win32
2024 # module, so we just restore the original $ENV{PATH} here.
2025 local $ENV{PATH} = $ENV{PATH};
2026 $ENV{PATH} = $old_env_path if $Is_MSWin32;
2028 fresh_perl_is(<<'end', "ok", { switches => [ '-T' ] },
2029 $TAINT = substr($^X, 0, 0);
2030 formline('@'.('<'x("2000".$TAINT)).' | @*', 'hallo', 'welt');
2033 "formline survives a tainted dynamic picture");
2037 isnt_tainted($^A, "format accumulator not tainted yet");
2038 formline('@ | @*', 'hallo' . $TAINT, 'welt');
2039 is_tainted($^A, "tainted formline argument makes a tainted accumulator");
2041 isnt_tainted($^A, "accumulator can be explicitly untainted");
2042 formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2043 isnt_tainted($^A, "accumulator still untainted");
2045 is_tainted($^A, "accumulator can be explicitly tainted");
2046 formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2047 is_tainted($^A, "accumulator still tainted");
2049 isnt_tainted($^A, "accumulator untainted again");
2050 formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2051 isnt_tainted($^A, "accumulator still untainted");
2052 formline('@' .('<'*(5+$TAINT0)) . ' | @*', 'hallo', 'welt');
2053 is_tainted($^A, "the accumulator should be tainted already");
2054 is_tainted($^A, "tainted formline picture makes a tainted accumulator");
2058 "Constant(1)" =~ / ^ ([a-z_]\w*) (?: [(] (.*) [)] )? $ /xi;
2061 isnt_tainted($a, "regex optimization of single char /[]/i doesn't taint");
2062 isnt_tainted($b, "regex optimization of single char /[]/i doesn't taint");
2066 # RT 81230: tainted value during FETCH created extra ref to tied obj
2074 my $x = $^X; # tainted
2077 sub FETCH { my $x = $_[0]; $$x . "" }
2082 local $SIG{__WARN__} = sub { $w .= "@_" };
2084 untie %h if $h{"k"};
2086 ::is($w, "", "RT 81230");
2090 # Compiling a subroutine inside a tainted expression does not make the
2091 # constant folded values tainted.
2092 my $x = sub { "x" . "y" };
2093 my $y = $ENV{PATH} . $x->(); # Compile $x inside a tainted expression
2095 isnt_tainted($z, "Constants folded value not tainted");
2099 # now that regexes are first class SVs, make sure that they themselves
2100 # as well as references to them are tainted
2102 my $rr = qr/(.)$TAINT/;
2103 my $r = $$rr; # bare REGEX
2105 ok($s =~ s/$r/x/, "match bare regex");
2106 is_tainted($s, "match bare regex taint");
2107 is($s, 'xbc', "match bare regex taint value");
2111 # [perl #82616] security Issues with user-defined \p{} properties
2112 # A using a tainted user-defined property should croak
2114 sub IsA { sprintf "%02x", ord("A") }
2117 ok("A" =~ /\p{$prop}/, "user-defined property: non-tainted case");
2118 $prop = "IsA$TAINT";
2119 eval { "A" =~ /\p{$prop}/};
2120 like($@, qr/Insecure user-defined property \\p\{main::IsA}/,
2121 "user-defined property: tainted case");
2125 # [perl #87336] lc/uc(first) failing to taint the returned string
2126 my $source = "foo$TAINT";
2127 my $dest = lc $source;
2128 is_tainted $dest, "lc(tainted) taints its return value";
2129 $dest = lcfirst $source;
2130 is_tainted $dest, "lcfirst(tainted) taints its return value";
2132 is_tainted $dest, "uc(tainted) taints its return value";
2133 $dest = ucfirst $source;
2134 is_tainted $dest, "ucfirst(tainted) taints its return value";
2138 # Taintedness of values returned from given()
2139 use feature 'switch';
2141 my @descriptions = ('when', 'given end', 'default');
2144 my $letter = "$_$TAINT";
2146 my $desc = "tainted value returned from " . shift(@descriptions);
2150 when ('x') { $letter }
2151 when ('y') { goto leavegiven }
2156 is $res, $letter, "$desc is correct";
2157 is_tainted $res, "$desc stays tainted";
2162 # tainted constants and index()
2163 # RT 64804; http://bugs.debian.org/291450
2165 ok(tainted $old_env_path, "initial taintedness");
2166 BEGIN { no strict 'refs'; my $v = $old_env_path; *{"::C"} = sub () { $v }; }
2167 ok(tainted C, "constant is tainted properly");
2168 ok(!tainted "", "tainting not broken yet");
2170 ok(!tainted "", "tainting still works after index() of the constant");
2173 # Tainted values with smartmatch
2174 # [perl #93590] S_do_smartmatch stealing its own string buffers
2175 ok "M$TAINT" ~~ ['m', 'M'], '$tainted ~~ ["whatever", "match"]';
2176 ok !("M$TAINT" ~~ ['m', undef]), '$tainted ~~ ["whatever", undef]';
2178 # Tainted values and ref()
2180 my $x = bless \"M$TAINT", ref(bless[], "main");
2182 pass("no death when TARG of ref is tainted");
2184 # $$ should not be tainted by being read in a tainted expression.
2186 isnt_tainted $$, "PID not tainted initially";
2187 my $x = $ENV{PATH}.$$;
2188 isnt_tainted $$, "PID not tainted when read in tainted expression";
2194 my ($latin1, $utf8) = ("\xDF") x 2;
2195 utf8::downgrade($latin1);
2196 utf8::upgrade($utf8);
2198 is_tainted fc($latin1), "under locale, lc(latin1) taints the result";
2199 is_tainted fc($utf8), "under locale, lc(utf8) taints the result";
2201 is_tainted "\F$latin1", "under locale, \\Flatin1 taints the result";
2202 is_tainted "\F$utf8", "under locale, \\Futf8 taints the result";
2207 eval { die "Test\n".substr($ENV{PATH}, 0, 0); };
2210 like($@, qr/^Test\n\t\.\.\.propagated at /, "error should be propagated");
2213 # tainted run-time (?{}) should die
2216 my $code = '(?{})' . $TAINT;
2218 eval { "a" =~ /$code/ };
2219 like($@, qr/Eval-group in insecure regular expression/, "tainted (?{})");
2222 # This may bomb out with the alarm signal so keep it last
2224 skip "No alarm()" unless $Config{d_alarm};
2225 # Test from RT #41831]
2226 # [PATCH] Bug & fix: hang when using study + taint mode (perl 5.6.1, 5.8.x)
2228 my $DATA = <<'END' . $TAINT;
2238 ## don't set $SIG{ALRM}, since we'd never get to a user-level handler as
2239 ## perl is stuck in a regexp infinite loop!
2243 if ($DATA =~ /^line2.*line4/m) {
2244 fail("Should not be a match")
2246 pass("Match on tainted multiline data should fail promptly");
2252 # Keep the previous test last