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 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 like($@, qr/^Insecure directory in \$ENV{PATH}/);
188 skip "This is not VMS", 4 unless $Is_VMS;
190 $ENV{'DCL$PATH'} = $TAINT;
191 is(eval { `$echo 1` }, undef);
192 like($@, qr/^Insecure \$ENV{DCL\$PATH}/);
194 skip q[can't find world-writeable directory to test DCL$PATH], 2
197 $ENV{'DCL$PATH'} = $tmp;
198 is(eval { `$echo 1` }, undef);
199 like($@, qr/^Insecure directory in \$ENV{DCL\$PATH}/);
201 $ENV{'DCL$PATH'} = '';
205 # Let's see that we can taint and untaint as needed.
210 # That was a sanity check. If it failed, stop the insanity!
211 die "Taint checks don't seem to be enabled" unless tainted $foo;
220 isnt_tainted($_) foreach @list;
221 taint_these @list[1,3,5,7,9];
222 is_tainted($_) foreach @list[1,3,5,7,9];
223 isnt_tainted($_) foreach @list[0,2,4,6,8];
225 ($foo) = $foo =~ /(.+)/;
228 my ($desc, $s, $res, $res2, $one);
230 $desc = "match with string tainted";
232 $s = 'abcd' . $TAINT;
235 is_tainted($s, "$desc: s tainted");
236 isnt_tainted($res, "$desc: res not tainted");
237 isnt_tainted($one, "$desc: \$1 not tainted");
238 is($res, 1, "$desc: res value");
239 is($one, 'abcd', "$desc: \$1 value");
241 $desc = "match /g with string tainted";
243 $s = 'abcd' . $TAINT;
246 is_tainted($s, "$desc: s tainted");
247 isnt_tainted($res, "$desc: res not tainted");
248 isnt_tainted($one, "$desc: \$1 not tainted");
249 is($res, 1, "$desc: res value");
250 is($one, 'a', "$desc: \$1 value");
252 $desc = "match with string tainted, list cxt";
254 $s = 'abcd' . $TAINT;
255 ($res) = $s =~ /(.+)/;
257 is_tainted($s, "$desc: s tainted");
258 isnt_tainted($res, "$desc: res not tainted");
259 isnt_tainted($one, "$desc: \$1 not tainted");
260 is($res, 'abcd', "$desc: res value");
261 is($one, 'abcd', "$desc: \$1 value");
263 $desc = "match /g with string tainted, list cxt";
265 $s = 'abcd' . $TAINT;
266 ($res, $res2) = $s =~ /(.)/g;
268 is_tainted($s, "$desc: s tainted");
269 isnt_tainted($res, "$desc: res not tainted");
270 isnt_tainted($res2,"$desc: res2 not tainted");
271 isnt_tainted($one, "$desc: \$1 not tainted");
272 is($res, 'a', "$desc: res value");
273 is($res2,'b', "$desc: res2 value");
274 is($one, 'd', "$desc: \$1 value");
276 $desc = "match with pattern tainted";
279 $res = $s =~ /$TAINT(.+)/;
281 isnt_tainted($s, "$desc: s not tainted");
282 isnt_tainted($res, "$desc: res not tainted");
283 is_tainted($one, "$desc: \$1 tainted");
284 is($res, 1, "$desc: res value");
285 is($one, 'abcd', "$desc: \$1 value");
287 $desc = "match /g with pattern tainted";
290 $res = $s =~ /$TAINT(.)/g;
292 isnt_tainted($s, "$desc: s not tainted");
293 isnt_tainted($res, "$desc: res not tainted");
294 is_tainted($one, "$desc: \$1 tainted");
295 is($res, 1, "$desc: res value");
296 is($one, 'a', "$desc: \$1 value");
298 $desc = "match with pattern tainted via locale";
301 { use locale; $res = $s =~ /(\w+)/; $one = $1; }
302 isnt_tainted($s, "$desc: s not tainted");
303 isnt_tainted($res, "$desc: res not tainted");
304 is_tainted($one, "$desc: \$1 tainted");
305 is($res, 1, "$desc: res value");
306 is($one, 'abcd', "$desc: \$1 value");
308 $desc = "match /g with pattern tainted via locale";
311 { use locale; $res = $s =~ /(\w)/g; $one = $1; }
312 isnt_tainted($s, "$desc: s not tainted");
313 isnt_tainted($res, "$desc: res not tainted");
314 is_tainted($one, "$desc: \$1 tainted");
315 is($res, 1, "$desc: res value");
316 is($one, 'a', "$desc: \$1 value");
318 $desc = "match with pattern tainted, list cxt";
321 ($res) = $s =~ /$TAINT(.+)/;
323 isnt_tainted($s, "$desc: s not tainted");
324 is_tainted($res, "$desc: res tainted");
325 is_tainted($one, "$desc: \$1 tainted");
326 is($res, 'abcd', "$desc: res value");
327 is($one, 'abcd', "$desc: \$1 value");
329 $desc = "match /g with pattern tainted, list cxt";
332 ($res, $res2) = $s =~ /$TAINT(.)/g;
334 isnt_tainted($s, "$desc: s not tainted");
335 is_tainted($res, "$desc: res tainted");
336 is_tainted($one, "$desc: \$1 tainted");
337 is($res, 'a', "$desc: res value");
338 is($res2,'b', "$desc: res2 value");
339 is($one, 'd', "$desc: \$1 value");
341 $desc = "match with pattern tainted via locale, list cxt";
344 { use locale; ($res) = $s =~ /(\w+)/; $one = $1; }
345 isnt_tainted($s, "$desc: s not tainted");
346 is_tainted($res, "$desc: res tainted");
347 is_tainted($one, "$desc: \$1 tainted");
348 is($res, 'abcd', "$desc: res value");
349 is($one, 'abcd', "$desc: \$1 value");
351 $desc = "match /g with pattern tainted via locale, list cxt";
354 { use locale; ($res, $res2) = $s =~ /(\w)/g; $one = $1; }
355 isnt_tainted($s, "$desc: s not tainted");
356 is_tainted($res, "$desc: res tainted");
357 is_tainted($res2, "$desc: res2 tainted");
358 is_tainted($one, "$desc: \$1 tainted");
359 is($res, 'a', "$desc: res value");
360 is($res2,'b', "$desc: res2 value");
361 is($one, 'd', "$desc: \$1 value");
363 $desc = "substitution with string tainted";
365 $s = 'abcd' . $TAINT;
366 $res = $s =~ s/(.+)/xyz/;
368 is_tainted($s, "$desc: s tainted");
369 isnt_tainted($res, "$desc: res not tainted");
370 isnt_tainted($one, "$desc: \$1 not tainted");
371 is($s, 'xyz', "$desc: s value");
372 is($res, 1, "$desc: res value");
373 is($one, 'abcd', "$desc: \$1 value");
375 $desc = "substitution /g with string tainted";
377 $s = 'abcd' . $TAINT;
378 $res = $s =~ s/(.)/x/g;
380 is_tainted($s, "$desc: s tainted");
381 is_tainted($res, "$desc: res tainted");
382 isnt_tainted($one, "$desc: \$1 not tainted");
383 is($s, 'xxxx', "$desc: s value");
384 is($res, 4, "$desc: res value");
385 is($one, 'd', "$desc: \$1 value");
387 $desc = "substitution /r with string tainted";
389 $s = 'abcd' . $TAINT;
390 $res = $s =~ s/(.+)/xyz/r;
392 is_tainted($s, "$desc: s tainted");
393 is_tainted($res, "$desc: res tainted");
394 isnt_tainted($one, "$desc: \$1 not tainted");
395 is($s, 'abcd', "$desc: s value");
396 is($res, 'xyz', "$desc: res value");
397 is($one, 'abcd', "$desc: \$1 value");
399 $desc = "substitution /e with string tainted";
401 $s = 'abcd' . $TAINT;
403 $res = $s =~ s{(.+)}{
404 $one = $one . "x"; # make sure code not tainted
405 isnt_tainted($one, "$desc: code not tainted within /e");
407 isnt_tainted($one, "$desc: \$1 not tainted within /e");
411 is_tainted($s, "$desc: s tainted");
412 isnt_tainted($res, "$desc: res not tainted");
413 isnt_tainted($one, "$desc: \$1 not tainted");
414 is($s, 'xyz', "$desc: s value");
415 is($res, 1, "$desc: res value");
416 is($one, 'abcd', "$desc: \$1 value");
418 $desc = "substitution with pattern tainted";
421 $res = $s =~ s/$TAINT(.+)/xyz/;
423 is_tainted($s, "$desc: s tainted");
424 isnt_tainted($res, "$desc: res not tainted");
425 is_tainted($one, "$desc: \$1 tainted");
426 is($s, 'xyz', "$desc: s value");
427 is($res, 1, "$desc: res value");
428 is($one, 'abcd', "$desc: \$1 value");
430 $desc = "substitution /g with pattern tainted";
433 $res = $s =~ s/$TAINT(.)/x/g;
435 is_tainted($s, "$desc: s tainted");
436 is_tainted($res, "$desc: res tainted");
437 is_tainted($one, "$desc: \$1 tainted");
438 is($s, 'xxxx', "$desc: s value");
439 is($res, 4, "$desc: res value");
440 is($one, 'd', "$desc: \$1 value");
442 $desc = "substitution /ge with pattern tainted";
448 $res = $s =~ s{(.)$TAINT}{
449 $j = $i; # make sure code not tainted
451 isnt_tainted($j, "$desc: code not tainted within /e");
454 isnt_tainted($s, "$desc: s not tainted loop 1");
457 is_tainted($s, "$desc: s tainted loop $i");
459 is_tainted($one, "$desc: \$1 tainted loop $i");
464 is_tainted($s, "$desc: s tainted");
465 is_tainted($res, "$desc: res tainted");
466 is_tainted($one, "$desc: \$1 tainted");
467 is($s, '123', "$desc: s value");
468 is($res, 3, "$desc: res value");
469 is($one, 'c', "$desc: \$1 value");
471 $desc = "substitution /r with pattern tainted";
474 $res = $s =~ s/$TAINT(.+)/xyz/r;
476 isnt_tainted($s, "$desc: s not tainted");
477 is_tainted($res, "$desc: res tainted");
478 is_tainted($one, "$desc: \$1 tainted");
479 is($s, 'abcd', "$desc: s value");
480 is($res, 'xyz', "$desc: res value");
481 is($one, 'abcd', "$desc: \$1 value");
483 $desc = "substitution with pattern tainted via locale";
486 { use locale; $res = $s =~ s/(\w+)/xyz/; $one = $1; }
487 is_tainted($s, "$desc: s tainted");
488 isnt_tainted($res, "$desc: res not tainted");
489 is_tainted($one, "$desc: \$1 tainted");
490 is($s, 'xyz', "$desc: s value");
491 is($res, 1, "$desc: res value");
492 is($one, 'abcd', "$desc: \$1 value");
494 $desc = "substitution /g with pattern tainted via locale";
497 { use locale; $res = $s =~ s/(\w)/x/g; $one = $1; }
498 is_tainted($s, "$desc: s tainted");
499 is_tainted($res, "$desc: res tainted");
500 is_tainted($one, "$desc: \$1 tainted");
501 is($s, 'xxxx', "$desc: s value");
502 is($res, 4, "$desc: res value");
503 is($one, 'd', "$desc: \$1 value");
505 $desc = "substitution /r with pattern tainted via locale";
508 { use locale; $res = $s =~ s/(\w+)/xyz/r; $one = $1; }
509 isnt_tainted($s, "$desc: s not tainted");
510 is_tainted($res, "$desc: res tainted");
511 is_tainted($one, "$desc: \$1 tainted");
512 is($s, 'abcd', "$desc: s value");
513 is($res, 'xyz', "$desc: res value");
514 is($one, 'abcd', "$desc: \$1 value");
516 $desc = "substitution with replacement tainted";
519 $res = $s =~ s/(.+)/xyz$TAINT/;
521 is_tainted($s, "$desc: s tainted");
522 isnt_tainted($res, "$desc: res not tainted");
523 isnt_tainted($one, "$desc: \$1 not tainted");
524 is($s, 'xyz', "$desc: s value");
525 is($res, 1, "$desc: res value");
526 is($one, 'abcd', "$desc: \$1 value");
528 $desc = "substitution /g with replacement tainted";
531 $res = $s =~ s/(.)/x$TAINT/g;
533 is_tainted($s, "$desc: s tainted");
534 isnt_tainted($res, "$desc: res not tainted");
535 isnt_tainted($one, "$desc: \$1 not tainted");
536 is($s, 'xxxx', "$desc: s value");
537 is($res, 4, "$desc: res value");
538 is($one, 'd', "$desc: \$1 value");
540 $desc = "substitution /ge with replacement tainted";
547 $j = $i; # make sure code not tainted
549 isnt_tainted($j, "$desc: code not tainted within /e");
552 isnt_tainted($s, "$desc: s not tainted loop 1");
555 is_tainted($s, "$desc: s tainted loop $i");
557 isnt_tainted($one, "$desc: \$1 not tainted within /e");
562 is_tainted($s, "$desc: s tainted");
563 is_tainted($res, "$desc: res tainted");
564 isnt_tainted($one, "$desc: \$1 not tainted");
565 is($s, '123', "$desc: s value");
566 is($res, 3, "$desc: res value");
567 is($one, 'c', "$desc: \$1 value");
569 $desc = "substitution /r with replacement tainted";
572 $res = $s =~ s/(.+)/xyz$TAINT/r;
574 isnt_tainted($s, "$desc: s not tainted");
575 is_tainted($res, "$desc: res tainted");
576 isnt_tainted($one, "$desc: \$1 not tainted");
577 is($s, 'abcd', "$desc: s value");
578 is($res, 'xyz', "$desc: res value");
579 is($one, 'abcd', "$desc: \$1 value");
582 # now do them all again with "use re 'taint"
586 $desc = "use re 'taint': match with string tainted";
588 $s = 'abcd' . $TAINT;
591 is_tainted($s, "$desc: s tainted");
592 isnt_tainted($res, "$desc: res not tainted");
593 is_tainted($one, "$desc: \$1 tainted");
594 is($res, 1, "$desc: res value");
595 is($one, 'abcd', "$desc: \$1 value");
597 $desc = "use re 'taint': match /g with string tainted";
599 $s = 'abcd' . $TAINT;
602 is_tainted($s, "$desc: s tainted");
603 isnt_tainted($res, "$desc: res not tainted");
604 is_tainted($one, "$desc: \$1 tainted");
605 is($res, 1, "$desc: res value");
606 is($one, 'a', "$desc: \$1 value");
608 $desc = "use re 'taint': match with string tainted, list cxt";
610 $s = 'abcd' . $TAINT;
611 ($res) = $s =~ /(.+)/;
613 is_tainted($s, "$desc: s tainted");
614 is_tainted($res, "$desc: res tainted");
615 is_tainted($one, "$desc: \$1 tainted");
616 is($res, 'abcd', "$desc: res value");
617 is($one, 'abcd', "$desc: \$1 value");
619 $desc = "use re 'taint': match /g with string tainted, list cxt";
621 $s = 'abcd' . $TAINT;
622 ($res, $res2) = $s =~ /(.)/g;
624 is_tainted($s, "$desc: s tainted");
625 is_tainted($res, "$desc: res tainted");
626 is_tainted($res2, "$desc: res2 tainted");
627 is_tainted($one, "$desc: \$1 not tainted");
628 is($res, 'a', "$desc: res value");
629 is($res2,'b', "$desc: res2 value");
630 is($one, 'd', "$desc: \$1 value");
632 $desc = "use re 'taint': match with pattern tainted";
635 $res = $s =~ /$TAINT(.+)/;
637 isnt_tainted($s, "$desc: s not 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, 'abcd', "$desc: \$1 value");
643 $desc = "use re 'taint': match /g with pattern tainted";
646 $res = $s =~ /$TAINT(.)/g;
648 isnt_tainted($s, "$desc: s not tainted");
649 isnt_tainted($res, "$desc: res not tainted");
650 is_tainted($one, "$desc: \$1 tainted");
651 is($res, 1, "$desc: res value");
652 is($one, 'a', "$desc: \$1 value");
654 $desc = "use re 'taint': match with pattern tainted via locale";
657 { use locale; $res = $s =~ /(\w+)/; $one = $1; }
658 isnt_tainted($s, "$desc: s not tainted");
659 isnt_tainted($res, "$desc: res not tainted");
660 is_tainted($one, "$desc: \$1 tainted");
661 is($res, 1, "$desc: res value");
662 is($one, 'abcd', "$desc: \$1 value");
664 $desc = "use re 'taint': match /g with pattern tainted via locale";
667 { use locale; $res = $s =~ /(\w)/g; $one = $1; }
668 isnt_tainted($s, "$desc: s not tainted");
669 isnt_tainted($res, "$desc: res not tainted");
670 is_tainted($one, "$desc: \$1 tainted");
671 is($res, 1, "$desc: res value");
672 is($one, 'a', "$desc: \$1 value");
674 $desc = "use re 'taint': match with pattern tainted, list cxt";
677 ($res) = $s =~ /$TAINT(.+)/;
679 isnt_tainted($s, "$desc: s not tainted");
680 is_tainted($res, "$desc: res tainted");
681 is_tainted($one, "$desc: \$1 tainted");
682 is($res, 'abcd', "$desc: res value");
683 is($one, 'abcd', "$desc: \$1 value");
685 $desc = "use re 'taint': match /g with pattern tainted, list cxt";
688 ($res, $res2) = $s =~ /$TAINT(.)/g;
690 isnt_tainted($s, "$desc: s not tainted");
691 is_tainted($res, "$desc: res tainted");
692 is_tainted($one, "$desc: \$1 tainted");
693 is($res, 'a', "$desc: res value");
694 is($res2,'b', "$desc: res2 value");
695 is($one, 'd', "$desc: \$1 value");
697 $desc = "use re 'taint': match with pattern tainted via locale, list cxt";
700 { use locale; ($res) = $s =~ /(\w+)/; $one = $1; }
701 isnt_tainted($s, "$desc: s not tainted");
702 is_tainted($res, "$desc: res tainted");
703 is_tainted($one, "$desc: \$1 tainted");
704 is($res, 'abcd', "$desc: res value");
705 is($one, 'abcd', "$desc: \$1 value");
707 $desc = "use re 'taint': match /g with pattern tainted via locale, list cxt";
710 { use locale; ($res, $res2) = $s =~ /(\w)/g; $one = $1; }
711 isnt_tainted($s, "$desc: s not tainted");
712 is_tainted($res, "$desc: res tainted");
713 is_tainted($res2, "$desc: res2 tainted");
714 is_tainted($one, "$desc: \$1 tainted");
715 is($res, 'a', "$desc: res value");
716 is($res2,'b', "$desc: res2 value");
717 is($one, 'd', "$desc: \$1 value");
719 $desc = "use re 'taint': substitution with string tainted";
721 $s = 'abcd' . $TAINT;
722 $res = $s =~ s/(.+)/xyz/;
724 is_tainted($s, "$desc: s tainted");
725 isnt_tainted($res, "$desc: res not tainted");
726 is_tainted($one, "$desc: \$1 tainted");
727 is($s, 'xyz', "$desc: s value");
728 is($res, 1, "$desc: res value");
729 is($one, 'abcd', "$desc: \$1 value");
731 $desc = "use re 'taint': substitution /g with string tainted";
733 $s = 'abcd' . $TAINT;
734 $res = $s =~ s/(.)/x/g;
736 is_tainted($s, "$desc: s tainted");
737 is_tainted($res, "$desc: res tainted");
738 is_tainted($one, "$desc: \$1 tainted");
739 is($s, 'xxxx', "$desc: s value");
740 is($res, 4, "$desc: res value");
741 is($one, 'd', "$desc: \$1 value");
743 $desc = "use re 'taint': substitution /r with string tainted";
745 $s = 'abcd' . $TAINT;
746 $res = $s =~ s/(.+)/xyz/r;
748 is_tainted($s, "$desc: s tainted");
749 is_tainted($res, "$desc: res tainted");
750 is_tainted($one, "$desc: \$1 tainted");
751 is($s, 'abcd', "$desc: s value");
752 is($res, 'xyz', "$desc: res value");
753 is($one, 'abcd', "$desc: \$1 value");
755 $desc = "use re 'taint': substitution /e with string tainted";
757 $s = 'abcd' . $TAINT;
759 $res = $s =~ s{(.+)}{
760 $one = $one . "x"; # make sure code not tainted
761 isnt_tainted($one, "$desc: code not tainted within /e");
763 is_tainted($one, "$desc: $1 tainted within /e");
767 is_tainted($s, "$desc: s tainted");
768 isnt_tainted($res, "$desc: res not tainted");
769 is_tainted($one, "$desc: \$1 tainted");
770 is($s, 'xyz', "$desc: s value");
771 is($res, 1, "$desc: res value");
772 is($one, 'abcd', "$desc: \$1 value");
774 $desc = "use re 'taint': substitution with pattern tainted";
777 $res = $s =~ s/$TAINT(.+)/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 pattern tainted";
789 $res = $s =~ s/$TAINT(.)/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 /ge with pattern tainted";
804 $res = $s =~ s{(.)$TAINT}{
805 $j = $i; # make sure code not tainted
807 isnt_tainted($j, "$desc: code not tainted within /e");
810 isnt_tainted($s, "$desc: s not tainted loop 1");
813 is_tainted($s, "$desc: s tainted loop $i");
815 is_tainted($one, "$desc: \$1 tainted loop $i");
820 is_tainted($s, "$desc: s tainted");
821 is_tainted($res, "$desc: res tainted");
822 is_tainted($one, "$desc: \$1 tainted");
823 is($s, '123', "$desc: s value");
824 is($res, 3, "$desc: res value");
825 is($one, 'c', "$desc: \$1 value");
828 $desc = "use re 'taint': substitution /r with pattern tainted";
831 $res = $s =~ s/$TAINT(.+)/xyz/r;
833 isnt_tainted($s, "$desc: s not tainted");
834 is_tainted($res, "$desc: res tainted");
835 is_tainted($one, "$desc: \$1 tainted");
836 is($s, 'abcd', "$desc: s value");
837 is($res, 'xyz', "$desc: res value");
838 is($one, 'abcd', "$desc: \$1 value");
840 $desc = "use re 'taint': substitution with pattern tainted via locale";
843 { use locale; $res = $s =~ s/(\w+)/xyz/; $one = $1; }
844 is_tainted($s, "$desc: s tainted");
845 isnt_tainted($res, "$desc: res not tainted");
846 is_tainted($one, "$desc: \$1 tainted");
847 is($s, 'xyz', "$desc: s value");
848 is($res, 1, "$desc: res value");
849 is($one, 'abcd', "$desc: \$1 value");
851 $desc = "use re 'taint': substitution /g with pattern tainted via locale";
854 { use locale; $res = $s =~ s/(\w)/x/g; $one = $1; }
855 is_tainted($s, "$desc: s tainted");
856 is_tainted($res, "$desc: res tainted");
857 is_tainted($one, "$desc: \$1 tainted");
858 is($s, 'xxxx', "$desc: s value");
859 is($res, 4, "$desc: res value");
860 is($one, 'd', "$desc: \$1 value");
862 $desc = "use re 'taint': substitution /r with pattern tainted via locale";
865 { use locale; $res = $s =~ s/(\w+)/xyz/r; $one = $1; }
866 isnt_tainted($s, "$desc: s not tainted");
867 is_tainted($res, "$desc: res tainted");
868 is_tainted($one, "$desc: \$1 tainted");
869 is($s, 'abcd', "$desc: s value");
870 is($res, 'xyz', "$desc: res value");
871 is($one, 'abcd', "$desc: \$1 value");
873 $desc = "use re 'taint': substitution with replacement tainted";
876 $res = $s =~ s/(.+)/xyz$TAINT/;
878 is_tainted($s, "$desc: s tainted");
879 isnt_tainted($res, "$desc: res not tainted");
880 isnt_tainted($one, "$desc: \$1 not tainted");
881 is($s, 'xyz', "$desc: s value");
882 is($res, 1, "$desc: res value");
883 is($one, 'abcd', "$desc: \$1 value");
885 $desc = "use re 'taint': substitution /g with replacement tainted";
888 $res = $s =~ s/(.)/x$TAINT/g;
890 is_tainted($s, "$desc: s tainted");
891 isnt_tainted($res, "$desc: res not tainted");
892 isnt_tainted($one, "$desc: \$1 not tainted");
893 is($s, 'xxxx', "$desc: s value");
894 is($res, 4, "$desc: res value");
895 is($one, 'd', "$desc: \$1 value");
897 $desc = "use re 'taint': substitution /ge with replacement tainted";
904 $j = $i; # make sure code not tainted
906 isnt_tainted($j, "$desc: code not tainted within /e");
909 isnt_tainted($s, "$desc: s not tainted loop 1");
912 is_tainted($s, "$desc: s tainted loop $i");
914 isnt_tainted($one, "$desc: \$1 not tainted");
919 is_tainted($s, "$desc: s tainted");
920 is_tainted($res, "$desc: res tainted");
921 isnt_tainted($one, "$desc: \$1 not tainted");
922 is($s, '123', "$desc: s value");
923 is($res, 3, "$desc: res value");
924 is($one, 'c', "$desc: \$1 value");
926 $desc = "use re 'taint': substitution /r with replacement tainted";
929 $res = $s =~ s/(.+)/xyz$TAINT/r;
931 isnt_tainted($s, "$desc: s not tainted");
932 is_tainted($res, "$desc: res tainted");
933 isnt_tainted($one, "$desc: \$1 not tainted");
934 is($s, 'abcd', "$desc: s value");
935 is($res, 'xyz', "$desc: res value");
936 is($one, 'abcd', "$desc: \$1 value");
939 $foo = $1 if 'bar' =~ /(.+)$TAINT/;
943 my $pi = 4 * atan2(1,1) + $TAINT0;
946 ($pi) = $pi =~ /(\d+\.\d+)/;
948 is(sprintf("%.5f", $pi), '3.14159');
951 # How about command-line arguments? The problem is that we don't
952 # always get some, so we'll run another process with some.
954 my $arg = tempfile();
955 open $fh, '>', $arg or die "Can't create $arg: $!";
957 eval { join('', @ARGV), kill 0 };
958 exit 0 if $@ =~ /^Insecure dependency/;
959 print "# Oops: \$@ was [$@]\n";
962 close $fh or die "Can't close $arg: $!";
963 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
964 is($?, 0, "Exited with status $?");
968 # Reading from a file should be tainted
970 ok(open my $fh, '<', $TEST) or diag("Couldn't open '$TEST': $!");
973 sysread($fh, $block, 100);
980 # Output of commands should be tainted
982 my $foo = `$echo abc`;
986 # Certain system variables should be tainted
992 # Results of matching should all be untainted
994 my $foo = "abcdefghi" . $TAINT;
1002 $foo =~ /(...)(...)(...)/;
1008 my @bar = $foo =~ /(...)(...)(...)/;
1009 isnt_tainted($_) foreach @bar;
1011 is_tainted($foo); # $foo should still be tainted!
1012 is($foo, "abcdefghi");
1015 # Operations which affect files can't use tainted data.
1017 violates_taint(sub { chmod 0, $TAINT }, 'chmod');
1020 skip "truncate() is not available", 2 unless $Config{d_truncate};
1022 violates_taint(sub { truncate 'NoSuChFiLe', $TAINT0 }, 'truncate');
1025 violates_taint(sub { rename '', $TAINT }, 'rename');
1026 violates_taint(sub { unlink $TAINT }, 'unlink');
1027 violates_taint(sub { utime $TAINT }, 'utime');
1030 skip "chown() is not available", 2 unless $Config{d_chown};
1032 violates_taint(sub { chown -1, -1, $TAINT }, 'chown');
1036 skip "link() is not available", 2 unless $Config{d_link};
1038 violates_taint(sub { link $TAINT, '' }, 'link');
1042 skip "symlink() is not available", 2 unless $Config{d_symlink};
1044 violates_taint(sub { symlink $TAINT, '' }, 'symlink');
1048 # Operations which affect directories can't use tainted data.
1050 violates_taint(sub { mkdir "foo".$TAINT, 0755 . $TAINT0 }, 'mkdir');
1051 violates_taint(sub { rmdir $TAINT }, 'rmdir');
1052 violates_taint(sub { chdir "foo".$TAINT }, 'chdir');
1055 skip "chroot() is not available", 2 unless $Config{d_chroot};
1057 violates_taint(sub { chroot $TAINT }, 'chroot');
1061 # Some operations using files can't use tainted data.
1063 my $foo = "imaginary library" . $TAINT;
1064 violates_taint(sub { require $foo }, 'require');
1066 my $filename = tempfile(); # NB: $filename isn't tainted!
1067 $foo = $filename . $TAINT;
1068 unlink $filename; # in any case
1070 is(eval { open FOO, $foo }, undef, 'open for read');
1071 is($@, ''); # NB: This should be allowed
1072 is(eval { open my $fh, , '<', $foo }, undef, 'open for read');
1073 is($@, ''); # NB: This should be allowed
1075 # Try first new style but allow also old style.
1076 # We do not want the whole taint.t to fail
1077 # just because Errno possibly failing.
1078 ok(eval('$!{ENOENT}') ||
1079 $! == 2 || # File not found
1080 ($Is_Dos && $! == 22));
1082 violates_taint(sub { open FOO, "> $foo" }, 'open', 'open for write');
1083 violates_taint(sub { open my $fh, '>', $foo }, 'open', 'open for write');
1086 # Commands to the system can't use tainted data
1091 skip "open('|') is not available", 8 if $^O eq 'amigaos';
1093 violates_taint(sub { open FOO, "| x$foo" }, 'piped open', 'popen to');
1094 violates_taint(sub { open FOO, "x$foo |" }, 'piped open', 'popen from');
1095 violates_taint(sub { open my $fh, '|-', "x$foo" }, 'piped open', 'popen to');
1096 violates_taint(sub { open my $fh, '-|', "x$foo" }, 'piped open', 'popen from');
1099 violates_taint(sub { exec $TAINT }, 'exec');
1100 violates_taint(sub { system $TAINT }, 'system');
1105 violates_taint(sub { `$echo 1$foo` }, '``', 'backticks');
1108 # wildcard expansion doesn't invoke shell on VMS, so is safe
1109 skip "This is not VMS", 2 unless $Is_VMS;
1111 isnt(join('', eval { glob $foo } ), '', 'globbing');
1116 # Operations which affect processes can't use tainted data.
1118 violates_taint(sub { kill 0, $TAINT }, 'kill');
1121 skip "setpgrp() is not available", 2 unless $Config{d_setpgrp};
1123 violates_taint(sub { setpgrp 0, $TAINT0 }, 'setpgrp');
1127 skip "setpriority() is not available", 2 unless $Config{d_setprior};
1129 violates_taint(sub { setpriority 0, $TAINT0, $TAINT0 }, 'setpriority');
1133 # Some miscellaneous operations can't use tainted data.
1136 skip "syscall() is not available", 2 unless $Config{d_syscall};
1138 violates_taint(sub { syscall $TAINT }, 'syscall');
1142 my $foo = "x" x 979;
1145 my $temp = tempfile();
1146 ok(open FOO, "> $temp") or diag("Couldn't open $temp for write: $!");
1147 violates_taint(sub { ioctl FOO, $TAINT0, $foo }, 'ioctl');
1149 my $temp2 = tempfile();
1150 ok(open my $fh, '>', $temp2) or diag("Couldn't open $temp2 for write: $!");
1151 violates_taint(sub { ioctl $fh, $TAINT0, $foo }, 'ioctl');
1154 skip "fcntl() is not available", 4 unless $Config{d_fcntl};
1156 violates_taint(sub { fcntl FOO, $TAINT0, $foo }, 'fcntl');
1157 violates_taint(sub { fcntl $fh, $TAINT0, $foo }, 'fcntl');
1164 # Some tests involving references
1166 my $foo = 'abc' . $TAINT;
1168 isnt_tainted($fooref);
1169 is_tainted($$fooref);
1173 # Some tests involving assignment
1179 is_tainted($foo = $bar);
1180 is_tainted($bar = $bar);
1181 is_tainted($bar += $bar);
1182 is_tainted($bar -= $bar);
1183 is_tainted($bar *= $bar);
1185 is_tainted($bar /= $bar);
1186 is_tainted($bar += 0);
1187 is_tainted($bar -= 2);
1188 is_tainted($bar *= -1);
1189 is_tainted($bar /= 1);
1194 # Test assignment and return of lists
1196 my @foo = ("A", "tainted" . $TAINT, "B");
1197 isnt_tainted($foo[0]);
1198 is_tainted( $foo[1]);
1199 isnt_tainted($foo[2]);
1201 isnt_tainted($bar[0]);
1202 is_tainted( $bar[1]);
1203 isnt_tainted($bar[2]);
1204 my @baz = eval { "A", "tainted" . $TAINT, "B" };
1205 isnt_tainted($baz[0]);
1206 is_tainted( $baz[1]);
1207 isnt_tainted($baz[2]);
1208 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
1209 isnt_tainted($plugh[0]);
1210 is_tainted( $plugh[1]);
1211 isnt_tainted($plugh[2]);
1212 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
1213 isnt_tainted(((&$nautilus)[0]));
1214 is_tainted( ((&$nautilus)[1]));
1215 isnt_tainted(((&$nautilus)[2]));
1216 my @xyzzy = &$nautilus;
1217 isnt_tainted($xyzzy[0]);
1218 is_tainted( $xyzzy[1]);
1219 isnt_tainted($xyzzy[2]);
1220 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
1221 isnt_tainted(((&$red_october)[0]));
1222 is_tainted( ((&$red_october)[1]));
1223 isnt_tainted(((&$red_october)[2]));
1224 my @corge = &$red_october;
1225 isnt_tainted($corge[0]);
1226 is_tainted( $corge[1]);
1227 isnt_tainted($corge[2]);
1230 # Test for system/library calls returning string data of dubious origin.
1232 # No reliable %Config check for getpw*
1234 skip "getpwent() is not available", 9 unless
1235 eval { setpwent(); getpwent() };
1238 my @getpwent = getpwent();
1239 die "getpwent: $!\n" unless (@getpwent);
1240 isnt_tainted($getpwent[0]);
1241 is_tainted($getpwent[1]);
1242 isnt_tainted($getpwent[2]);
1243 isnt_tainted($getpwent[3]);
1244 isnt_tainted($getpwent[4]);
1245 isnt_tainted($getpwent[5]);
1246 is_tainted($getpwent[6], 'ge?cos');
1247 isnt_tainted($getpwent[7]);
1248 is_tainted($getpwent[8], 'shell');
1253 # pretty hard to imagine not
1254 skip "readdir() is not available", 1 unless $Config{d_readdir};
1256 opendir my $dh, "op" or die "opendir: $!\n";
1257 my $readdir = readdir $dh;
1258 is_tainted($readdir);
1263 skip "readlink() or symlink() is not available" unless
1264 $Config{d_readlink} && $Config{d_symlink};
1266 my $symlink = "sl$$";
1268 my $sl = "/something/naughty";
1269 # it has to be a real path on Mac OS
1270 symlink($sl, $symlink) or die "symlink: $!\n";
1271 my $readlink = readlink($symlink);
1272 is_tainted($readlink);
1277 # test bitwise ops (regression bug)
1287 # test target of substitution (regression bug)
1289 my $why = $TAINT."y";
1297 $why =~ s/e/'-'.$$/ge;
1303 skip "no IPC::SysV", 2 unless $ipcsysv;
1307 skip "shm*() not available", 1 unless $Config{d_shm};
1310 my $sent = "foobar";
1313 my $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
1316 if (shmwrite($id, $sent, 0, 60)) {
1317 if (shmread($id, $rcvd, 0, 60)) {
1318 substr($rcvd, index($rcvd, "\0")) = '';
1320 warn "# shmread failed: $!\n";
1323 warn "# shmwrite failed: $!\n";
1325 shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
1327 warn "# shmget failed: $!\n";
1330 skip "SysV shared memory operation failed", 1 unless
1339 skip "msg*() not available", 1 unless $Config{d_msg};
1342 my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
1344 my $sent = "message";
1345 my $type_sent = 1234;
1350 if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) {
1351 if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) {
1352 ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
1354 warn "# msgrcv failed: $!\n";
1357 warn "# msgsnd failed: $!\n";
1359 msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
1361 warn "# msgget failed\n";
1365 skip "SysV message queue operation failed", 1
1366 unless $rcvd eq $sent && $type_sent == $type_rcvd;
1374 # bug id 20001004.006
1376 open my $fh, '<', $TEST or warn "$0: cannot read $TEST: $!" ;
1387 # bug id 20001004.007
1389 open my $fh, '<', $TEST or warn "$0: cannot read $TEST: $!" ;
1395 isnt_tainted($c->{a});
1396 is_tainted($c->{b});
1401 is_tainted($d->{a});
1402 isnt_tainted($d->{b});
1406 b => { c => $a, d => 42 } };
1407 isnt_tainted($e->{a});
1408 isnt_tainted($e->{b});
1409 is_tainted($e->{b}->{c});
1410 isnt_tainted($e->{b}->{d});
1414 # bug id 20010519.003
1417 use vars qw($has_fcntl);
1418 eval { require Fcntl; import Fcntl; };
1425 skip "no Fcntl", 18 unless $has_fcntl;
1427 my $foo = tempfile();
1428 my $evil = $foo . $TAINT;
1430 is(eval { sysopen(my $ro, $evil, &O_RDONLY) }, undef);
1433 violates_taint(sub { sysopen(my $wo, $evil, &O_WRONLY) }, 'sysopen');
1434 violates_taint(sub { sysopen(my $rw, $evil, &O_RDWR) }, 'sysopen');
1435 violates_taint(sub { sysopen(my $ap, $evil, &O_APPEND) }, 'sysopen');
1436 violates_taint(sub { sysopen(my $cr, $evil, &O_CREAT) }, 'sysopen');
1437 violates_taint(sub { sysopen(my $tr, $evil, &O_TRUNC) }, 'sysopen');
1439 is(eval { sysopen(my $ro, $foo, &O_RDONLY | $TAINT0) }, undef);
1442 violates_taint(sub { sysopen(my $wo, $foo, &O_WRONLY | $TAINT0) }, 'sysopen');
1443 violates_taint(sub { sysopen(my $rw, $foo, &O_RDWR | $TAINT0) }, 'sysopen');
1444 violates_taint(sub { sysopen(my $ap, $foo, &O_APPEND | $TAINT0) }, 'sysopen');
1445 violates_taint(sub { sysopen(my $cr, $foo, &O_CREAT | $TAINT0) }, 'sysopen');
1446 violates_taint(sub { sysopen(my $tr, $foo, &O_TRUNC | $TAINT0) }, 'sysopen');
1447 is(eval { sysopen(my $ro, $foo, &O_RDONLY, $TAINT0) }, undef);
1450 violates_taint(sub { sysopen(my $wo, $foo, &O_WRONLY, $TAINT0) }, 'sysopen');
1451 violates_taint(sub { sysopen(my $rw, $foo, &O_RDWR, $TAINT0) }, 'sysopen');
1452 violates_taint(sub { sysopen(my $ap, $foo, &O_APPEND, $TAINT0) }, 'sysopen');
1453 violates_taint(sub { sysopen(my $cr, $foo, &O_CREAT, $TAINT0) }, 'sysopen');
1454 violates_taint(sub { sysopen(my $tr, $foo, &O_TRUNC, $TAINT0) }, 'sysopen');
1463 my $saw_warning = 0;
1464 local $SIG{__WARN__} = sub { ++$saw_warning };
1467 my $divnum = shift()/1;
1468 sprintf("%1.1f\n", $divnum);
1475 is($saw_warning, 0);
1480 # Bug ID 20010730.010
1484 sub Tie::TIESCALAR {
1488 bless \$arg => $class;
1499 my $bar = "The Big Bright Green Pleasure Machine";
1501 tie my ($foo), Tie => $bar;
1509 # Check that all environment variables are tainted.
1511 while (my ($k, $v) = each %ENV) {
1513 # These we have explicitly untainted or set earlier.
1514 $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) {
1515 push @untainted, "# '$k' = '$v'\n";
1518 is("@untainted", "");
1522 is(${^TAINT}, 1, '$^TAINT is on');
1524 eval { ${^TAINT} = 0 };
1525 is(${^TAINT}, 1, '$^TAINT is not assignable');
1526 like($@, qr/^Modification of a read-only value attempted/,
1527 'Assigning to ${^TAINT} fails');
1532 my $re1 = qr/x$TAINT/;
1535 my $re2 = qr/^$re1\z/;
1543 skip "system {} has different semantics on Win32", 1 if $Is_MSWin32;
1546 local $ENV{PATH} .= $TAINT;
1547 eval { system { "echo" } "/arg0", "arg1" };
1548 like($@, qr/^Insecure \$ENV/);
1552 todo_skip 'tainted %ENV warning occludes tainted arguments warning', 22
1555 # bug 20020208.005 plus some single arg exec/system extras
1556 violates_taint(sub { exec $TAINT, $TAINT }, 'exec');
1557 violates_taint(sub { exec $TAINT $TAINT }, 'exec');
1558 violates_taint(sub { exec $TAINT $TAINT, $TAINT }, 'exec');
1559 violates_taint(sub { exec $TAINT 'notaint' }, 'exec');
1560 violates_taint(sub { exec {'notaint'} $TAINT }, 'exec');
1562 violates_taint(sub { system $TAINT, $TAINT }, 'system');
1563 violates_taint(sub { system $TAINT $TAINT }, 'system');
1564 violates_taint(sub { system $TAINT $TAINT, $TAINT }, 'system');
1565 violates_taint(sub { system $TAINT 'notaint' }, 'system');
1566 violates_taint(sub { system {'notaint'} $TAINT }, 'system');
1570 system("lskdfj does not exist","with","args");
1576 exec("lskdfj does not exist","with","args");
1580 # If you add tests here update also the above skip block for VMS.
1584 # [ID 20020704.001] taint propagation failure
1587 is_tainted(my $foo = $1);
1591 # [perl #24291] this used to dump core
1592 our %nonmagicalenv = ( PATH => "util" );
1593 local *ENV = \%nonmagicalenv;
1594 eval { system("lskdfj"); };
1595 like($@, qr/^%ENV is aliased to another variable while running with -T switch/);
1596 local *ENV = *nonmagicalenv;
1597 eval { system("lskdfj"); };
1598 like($@, qr/^%ENV is aliased to %nonmagicalenv while running with -T switch/);
1605 isnt_tainted($notaint);
1608 $notaint =~ /($notaint)/;
1612 $notaint =~ /($TAINT)/;
1617 $TAINT =~ /($notaint)/;
1621 $TAINT =~ /($TAINT)/;
1627 ($r = $TAINT) =~ /($notaint)/;
1629 ($r = $TAINT) =~ /($TAINT)/;
1633 # accessing $^O shoudn't taint it as a side-effect;
1634 # assigning tainted data to it is now an error
1637 if (!$^X) { } elsif ($^O eq 'bar') { }
1639 local $^O; # We're going to clobber something test infrastructure depends on.
1641 like($@, qr/Insecure dependency in/);
1644 EFFECTIVELY_CONSTANTS: {
1645 my $tainted_number = 12 + $TAINT0;
1646 is_tainted( $tainted_number );
1648 # Even though it's always 0, it's still tainted
1649 my $tainted_product = $tainted_number * 0;
1650 is_tainted( $tainted_product );
1651 is($tainted_product, 0);
1654 TERNARY_CONDITIONALS: {
1655 my $tainted_true = $TAINT . "blah blah blah";
1656 my $tainted_false = $TAINT0;
1657 is_tainted( $tainted_true );
1658 is_tainted( $tainted_false );
1660 my $result = $tainted_true ? "True" : "False";
1661 is($result, "True");
1662 isnt_tainted( $result );
1664 $result = $tainted_false ? "True" : "False";
1665 is($result, "False");
1666 isnt_tainted( $result );
1668 my $untainted_whatever = "The Fabulous Johnny Cash";
1669 my $tainted_whatever = "Soft Cell" . $TAINT;
1671 $result = $tainted_true ? $tainted_whatever : $untainted_whatever;
1672 is($result, "Soft Cell");
1673 is_tainted( $result );
1675 $result = $tainted_false ? $tainted_whatever : $untainted_whatever;
1676 is($result, "The Fabulous Johnny Cash");
1677 isnt_tainted( $result );
1681 # rt.perl.org 5900 $1 remains tainted if...
1682 # 1) The regular expression contains a scalar variable AND
1683 # 2) The regular expression appears in an elsif clause
1685 my $foo = "abcdefghi" . $TAINT;
1687 my $valid_chars = 'a-z';
1690 elsif ( $foo =~ /([$valid_chars]+)/o ) {
1697 elsif ( my @bar = $foo =~ /([$valid_chars]+)/o ) {
1698 isnt_tainted($bar[0]);
1703 # at scope exit, a restored localised value should have its old
1704 # taint status, not the taint status of the current statement
1713 my $c = do { local $x99; $^X };
1720 my $c = do { local $x99; '' };
1724 # an mg_get of a tainted value during localization shouldn't taint the
1728 eval { local $0, eval '1' };
1732 # [perl #8262] //g loops infinitely on tainted data
1738 cmp_ok pos($a[0]), '>', 0, "infinite m//g on arrays (aelemfast)";
1743 cmp_ok pos($a[$i]), '>', 0, "infinite m//g on arrays (aelem)";
1748 cmp_ok pos($h{a}), '>', 0, "infinite m//g on hashes (helem)";
1754 eval 'use Scalar::Util "dualvar"; $got_dualvar++';
1755 skip "No Scalar::Util::dualvar" unless $got_dualvar;
1756 my $a = Scalar::Util::dualvar(3, $^X);
1758 is ($b, 8, "Arithmetic on tainted dualvars works");
1761 # opening '|-' should not trigger $ENV{PATH} check
1765 skip "fork() is not available", 3 unless $Config{'d_fork'};
1766 skip "opening |- is not stable on threaded Open/MirBSD with taint", 3
1767 if $Config{useithreads} and $Is_OpenBSD || $Is_MirBSD;
1769 $ENV{'PATH'} = $TAINT;
1770 local $SIG{'PIPE'} = 'IGNORE';
1772 my $pid = open my $pipe, '|-';
1773 if (!defined $pid) {
1774 die "open failed: $!";
1777 kill 'KILL', $$; # child suicide
1781 unlike($@, qr/Insecure \$ENV/, 'fork triggers %ENV check');
1782 is($@, '', 'pipe/fork/open/close failed');
1784 open my $pipe, "|$Invoke_Perl -e 1";
1787 like($@, qr/Insecure \$ENV/, 'popen neglects %ENV check');
1792 package AUTOLOAD_TAINT;
1795 return if $AUTOLOAD =~ /DESTROY/;
1796 if ($AUTOLOAD =~ /untainted/) {
1797 main::isnt_tainted($AUTOLOAD, '$AUTOLOAD can be untainted');
1798 my $copy = $AUTOLOAD;
1799 main::isnt_tainted($copy, '$AUTOLOAD can be untainted');
1801 main::is_tainted($AUTOLOAD, '$AUTOLOAD can be tainted');
1802 my $copy = $AUTOLOAD;
1803 main::is_tainted($copy, '$AUTOLOAD can be tainted');
1808 my $o = bless [], 'AUTOLOAD_TAINT';
1815 # tests for tainted format in s?printf
1816 my $fmt = $TAINT . "# %s\n";
1817 violates_taint(sub { printf($fmt, "foo") }, 'printf',
1818 q/printf doesn't like tainted formats/);
1819 violates_taint(sub { printf($TAINT . "# %s\n", "foo") }, 'printf',
1820 q/printf doesn't like tainted format expressions/);
1821 eval { printf("# %s\n", $TAINT . "foo") };
1822 is($@, '', q/printf accepts other tainted args/);
1823 violates_taint(sub { sprintf($fmt, "foo") }, 'sprintf',
1824 q/sprintf doesn't like tainted formats/);
1825 violates_taint(sub { sprintf($TAINT . "# %s\n", "foo") }, 'sprintf',
1826 q/sprintf doesn't like tainted format expressions/);
1827 eval { sprintf("# %s\n", $TAINT . "foo") };
1828 is($@, '', q/sprintf accepts other tainted args/);
1837 is ($val, '7000000000', 'Assignment to untainted variable');
1840 is ($val, '7000000000', 'Assignment to tainted variable');
1845 my $tainted = '1' . $TAINT;
1846 eval '$val = eval $tainted;';
1847 is ($val, 0, "eval doesn't like tainted strings");
1848 like ($@, qr/^Insecure dependency in eval/);
1850 # Rather nice code to get a tainted undef by from Rick Delaney
1851 open my $fh, "test.pl" or die $!;
1852 seek $fh, 0, 2 or die $!;
1855 eval 'eval $tainted';
1856 like ($@, qr/^Insecure dependency in eval/);
1859 foreach my $ord (78, 163, 256) {
1861 my $line = 'A1' . $TAINT . chr $ord;
1865 isnt_tainted($1, "\\S match with chr $ord");
1870 sub cr { my $x = crypt($_[0], $_[1]); $x }
1871 sub co { my $x = ~$_[0]; $x }
1873 $a = cr('hello', 'foo' . $TAINT);
1874 $b = cr('hello', 'foo');
1875 is_tainted($a, "tainted crypt");
1876 isnt_tainted($b, "untainted crypt");
1877 $a = co('foo' . $TAINT);
1879 is_tainted($a, "tainted complement");
1880 isnt_tainted($b, "untainted complement");
1884 my @data = qw(bonk zam zlonk qunckkk);
1885 # Clearly some sort of usenet bang-path
1886 my $string = $TAINT . join "!", @data;
1888 is_tainted($string, "tainted data");
1890 my @got = split /!|,/, $string;
1892 # each @got would be useful here, but I want the test for earlier perls
1893 for my $i (0 .. $#data) {
1894 is_tainted($got[$i], "tainted result $i");
1895 is($got[$i], $data[$i], "correct content $i");
1898 is_tainted($string, "still tainted data");
1900 my @got = split /[!,]/, $string;
1902 # each @got would be useful here, but I want the test for earlier perls
1903 for my $i (0 .. $#data) {
1904 is_tainted($got[$i], "tainted result $i");
1905 is($got[$i], $data[$i], "correct content $i");
1908 is_tainted($string, "still tainted data");
1910 my @got = split /!/, $string;
1912 # each @got would be useful here, but I want the test for earlier perls
1913 for my $i (0 .. $#data) {
1914 is_tainted($got[$i], "tainted result $i");
1915 is($got[$i], $data[$i], "correct content $i");
1919 # Bug RT #52552 - broken by change at git commit id f337b08
1921 my $x = $TAINT. q{print "Hello world\n"};
1922 my $y = pack "a*", $x;
1923 is_tainted($y, "pack a* preserves tainting");
1925 my $z = pack "A*", q{print "Hello world\n"}.$TAINT;
1926 is_tainted($z, "pack A* preserves tainting");
1928 my $zz = pack "a*a*", q{print "Hello world\n"}, $TAINT;
1929 is_tainted($zz, "pack a*a* preserves tainting");
1932 # Bug RT #61976 tainted $! would show numeric rather than string value
1935 my $tainted_path = substr($^X,0,0) . "/no/such/file";
1937 # $! is used in a tainted expression, so gets tainted
1938 open my $fh, $tainted_path or $err= "$!";
1939 unlike($err, qr/^\d+$/, 'tainted $!');
1943 # #6758: tainted values become untainted in tied hashes
1944 # (also applies to other value magic such as pos)
1949 sub TIEHASH { bless {} }
1950 sub TIEARRAY { bless {} }
1955 main::is_tainted($_[1], "tied arg1 tainted");
1956 main::is_tainted($_[2], "tied arg2 tainted");
1962 my ($k,$v) = qw(1111 val);
1964 tie my @array, 'P6758';
1965 tie my %hash , 'P6758';
1968 ok $i == 2, "tied STORE called correct number of times";
1971 # Bug RT #45167 the return value of sprintf sometimes wasn't tainted
1972 # when the args were tainted. This only occured on the first use of
1973 # sprintf; after that, its TARG has taint magic attached, so setmagic
1974 # at the end works. That's why there are multiple sprintf's below, rather
1975 # than just one wrapped in an inner loop. Also, any plaintext between
1976 # fprmat entires would correctly cause tainting to get set. so test with
1977 # "%s%s" rather than eg "%s %s".
1980 for my $var1 ($TAINT, "123") {
1981 for my $var2 ($TAINT0, "456") {
1982 is( tainted(sprintf '%s', $var1, $var2), tainted($var1),
1983 "sprintf '%s', '$var1', '$var2'" );
1984 is( tainted(sprintf ' %s', $var1, $var2), tainted($var1),
1985 "sprintf ' %s', '$var1', '$var2'" );
1986 is( tainted(sprintf '%s%s', $var1, $var2),
1987 tainted($var1) || tainted($var2),
1988 "sprintf '%s%s', '$var1', '$var2'" );
1994 # Bug RT #67962: old tainted $1 gets treated as tainted
1995 # in next untainted # match
1999 "abc".$TAINT =~ /(.*)/; # make $1 tainted
2000 is_tainted($1, '$1 should be tainted');
2002 my $untainted = "abcdef";
2003 isnt_tainted($untainted, '$untainted should be untainted');
2004 $untainted =~ s/(abc)/$1/;
2005 isnt_tainted($untainted, '$untainted should still be untainted');
2006 $untainted =~ s/(abc)/x$1/;
2007 isnt_tainted($untainted, '$untainted should yet still be untainted');
2011 # On Windows we can't spawn a fresh Perl interpreter unless at
2012 # least the Windows system directory (usually C:\Windows\System32)
2013 # is still on the PATH. There is however no way to determine the
2014 # actual path on the current system without loading the Win32
2015 # module, so we just restore the original $ENV{PATH} here.
2016 local $ENV{PATH} = $ENV{PATH};
2017 $ENV{PATH} = $old_env_path if $Is_MSWin32;
2019 fresh_perl_is(<<'end', "ok", { switches => [ '-T' ] },
2020 $TAINT = substr($^X, 0, 0);
2021 formline('@'.('<'x("2000".$TAINT)).' | @*', 'hallo', 'welt');
2024 "formline survives a tainted dynamic picture");
2028 isnt_tainted($^A, "format accumulator not tainted yet");
2029 formline('@ | @*', 'hallo' . $TAINT, 'welt');
2030 is_tainted($^A, "tainted formline argument makes a tainted accumulator");
2032 isnt_tainted($^A, "accumulator can be explicitly untainted");
2033 formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2034 isnt_tainted($^A, "accumulator still untainted");
2036 is_tainted($^A, "accumulator can be explicitly tainted");
2037 formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2038 is_tainted($^A, "accumulator still tainted");
2040 isnt_tainted($^A, "accumulator untainted again");
2041 formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2042 isnt_tainted($^A, "accumulator still untainted");
2043 formline('@' .('<'*(5+$TAINT0)) . ' | @*', 'hallo', 'welt');
2045 local $::TODO = "get magic handled too late?";
2046 is_tainted($^A, "the accumulator should be tainted already");
2048 is_tainted($^A, "tainted formline picture makes a tainted accumulator");
2052 "Constant(1)" =~ / ^ ([a-z_]\w*) (?: [(] (.*) [)] )? $ /xi;
2055 isnt_tainted($a, "regex optimization of single char /[]/i doesn't taint");
2056 isnt_tainted($b, "regex optimization of single char /[]/i doesn't taint");
2060 # RT 81230: tainted value during FETCH created extra ref to tied obj
2068 my $x = $^X; # tainted
2071 sub FETCH { my $x = $_[0]; $$x . "" }
2076 local $SIG{__WARN__} = sub { $w .= "@_" };
2078 untie %h if $h{"k"};
2080 ::is($w, "", "RT 81230");
2084 # Compiling a subroutine inside a tainted expression does not make the
2085 # constant folded values tainted.
2086 my $x = sub { "x" . "y" };
2087 my $y = $ENV{PATH} . $x->(); # Compile $x inside a tainted expression
2089 isnt_tainted($z, "Constants folded value not tainted");
2093 # now that regexes are first class SVs, make sure that they themselves
2094 # as well as references to them are tainted
2096 my $rr = qr/(.)$TAINT/;
2097 my $r = $$rr; # bare REGEX
2099 ok($s =~ s/$r/x/, "match bare regex");
2100 is_tainted($s, "match bare regex taint");
2101 is($s, 'xbc', "match bare regex taint value");
2105 # [perl #82616] security Issues with user-defined \p{} properties
2106 # A using a tainted user-defined property should croak
2108 sub IsA { sprintf "%02x", ord("A") }
2111 ok("A" =~ /\p{$prop}/, "user-defined property: non-tainted case");
2112 $prop = "IsA$TAINT";
2113 eval { "A" =~ /\p{$prop}/};
2114 like($@, qr/Insecure user-defined property \\p{main::IsA}/,
2115 "user-defined property: tainted case");
2119 # [perl #87336] lc/uc(first) failing to taint the returned string
2120 my $source = "foo$TAINT";
2121 my $dest = lc $source;
2122 is_tainted $dest, "lc(tainted) taints its return value";
2123 $dest = lcfirst $source;
2124 is_tainted $dest, "lcfirst(tainted) taints its return value";
2126 is_tainted $dest, "uc(tainted) taints its return value";
2127 $dest = ucfirst $source;
2128 is_tainted $dest, "ucfirst(tainted) taints its return value";
2132 # Taintedness of values returned from given()
2133 use feature 'switch';
2135 my @descriptions = ('when', 'given end', 'default');
2138 my $letter = "$_$TAINT";
2140 my $desc = "tainted value returned from " . shift(@descriptions);
2144 when ('x') { $letter }
2145 when ('y') { goto leavegiven }
2150 is $res, $letter, "$desc is correct";
2151 is_tainted $res, "$desc stays tainted";
2156 # tainted constants and index()
2157 # RT 64804; http://bugs.debian.org/291450
2159 ok(tainted $old_env_path, "initial taintedness");
2160 BEGIN { no strict 'refs'; my $v = $old_env_path; *{"::C"} = sub () { $v }; }
2161 ok(tainted C, "constant is tainted properly");
2162 ok(!tainted "", "tainting not broken yet");
2164 ok(!tainted "", "tainting still works after index() of the constant");
2167 # Tainted values with smartmatch
2168 # [perl #93590] S_do_smartmatch stealing its own string buffers
2169 ok "M$TAINT" ~~ ['m', 'M'], '$tainted ~~ ["whatever", "match"]';
2170 ok !("M$TAINT" ~~ ['m', undef]), '$tainted ~~ ["whatever", undef]';
2173 # This may bomb out with the alarm signal so keep it last
2175 skip "No alarm()" unless $Config{d_alarm};
2176 # Test from RT #41831]
2177 # [PATCH] Bug & fix: hang when using study + taint mode (perl 5.6.1, 5.8.x)
2179 my $DATA = <<'END' . $TAINT;
2189 ## don't set $SIG{ALRM}, since we'd never get to a user-level handler as
2190 ## perl is stuck in a regexp infinite loop!
2194 if ($DATA =~ /^line2.*line4/m) {
2195 fail("Should not be a match")
2197 pass("Match on tainted multiline data should fail promptly");
2203 # Keep the previous test last