This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Test perl #4760
[perl5.git] / t / op / taint.t
1 #!./perl -T
2 #
3 # Taint tests by Tom Phoenix <rootbeer@teleport.com>.
4 #
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?
8 #
9
10 BEGIN {
11     chdir 't' if -d 't';
12     @INC = '../lib';
13     require './test.pl';
14     skip_all_if_miniperl("no dynamic loading on miniperl, no re");
15 }
16
17 use strict;
18 use Config;
19
20 plan tests => 797;
21
22 $| = 1;
23
24 use vars qw($ipcsysv); # did we manage to load IPC::SysV?
25
26 my ($old_env_path, $old_env_dcl_path, $old_env_term);
27 BEGIN {
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';
34   }
35   if ($Config{'extensions'} =~ /\bIPC\/SysV\b/
36       && ($Config{d_shm} || $Config{d_msg})) {
37       eval { require IPC::SysV };
38       unless ($@) {
39           $ipcsysv++;
40           IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU IPC_NOWAIT));
41       }
42   }
43 }
44
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'                 :
55                                  './perl'               ;
56 my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
57
58 if ($Is_VMS) {
59     my (%old, $x);
60     for $x ('DCL$PATH', @MoreEnv) {
61         ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
62     }
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.
67     eval <<EndOfCleanup;
68         END {
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;
76             } else {
77                 delete \$ENV{'DCL\$PATH'};
78             }
79         }
80 EndOfCleanup
81 }
82
83 # Sources of taint:
84 #   The empty tainted value, for tainting strings
85 my $TAINT = substr($^X, 0, 0);
86 #   A tainted zero, useful for tainting numbers
87 my $TAINT0;
88 {
89     no warnings;
90     $TAINT0 = 0 + $TAINT;
91 }
92
93 # This taints each argument passed. All must be lvalues.
94 # Side effect: It also stringifies them. :-(
95 sub taint_these (@) {
96     for (@_) { $_ .= $TAINT }
97 }
98
99 # How to identify taint when you see it
100 sub tainted ($) {
101     local $@;   # Don't pollute caller's value.
102     not eval { join("",@_), kill 0; 1 };
103 }
104
105 sub is_tainted {
106     my $thing = shift;
107     local $::Level = $::Level + 1;
108     ok(tainted($thing), @_);
109 }
110
111 sub isnt_tainted {
112     my $thing = shift;
113     local $::Level = $::Level + 1;
114     ok(!tainted($thing), @_);
115 }
116
117 sub violates_taint {
118     my ($code, $what, $desc) = @_;
119     $desc //= $what;
120     local $::Level = $::Level + 1;
121     is(eval { $code->(); }, undef, $desc);
122     like($@, qr/^Insecure dependency in $what while running with -T switch/);
123 }
124
125 # We need an external program to call.
126 my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$"));
127 END { unlink $ECHO }
128 open my $fh, '>', $ECHO or die "Can't create $ECHO: $!";
129 print $fh 'print "@ARGV\n"', "\n";
130 close $fh;
131 my $echo = "$Invoke_Perl $ECHO";
132
133 my $TEST = 'TEST';
134
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.
138 {
139     $ENV{'DCL$PATH'} = '' if $Is_VMS;
140
141     $ENV{PATH} = ($Is_Cygwin) ? '/usr/bin' : '';
142     delete @ENV{@MoreEnv};
143     $ENV{TERM} = 'dumb';
144
145     is(eval { `$echo 1` }, "1\n");
146
147     SKIP: {
148         skip "Environment tainting tests skipped", 4
149           if $Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos;
150
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}/;
156             shift @vars;
157         }
158         is("@vars", "");
159
160         # tainted $TERM is unsafe only if it contains metachars
161         local $ENV{TERM};
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}/);
167     }
168
169     my $tmp;
170     if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) {
171         print "# all directories are writeable\n";
172     }
173     else {
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";
178     }
179
180     SKIP: {
181         skip "all directories are writeable", 2 unless $tmp;
182
183         local $ENV{PATH} = $tmp;
184         is(eval { `$echo 1` }, undef);
185         like($@, qr/^Insecure directory in \$ENV\{PATH}/);
186     }
187
188     SKIP: {
189         skip "This is not VMS", 4 unless $Is_VMS;
190
191         $ENV{'DCL$PATH'} = $TAINT;
192         is(eval { `$echo 1` }, undef);
193         like($@, qr/^Insecure \$ENV\{DCL\$PATH}/);
194         SKIP: {
195             skip q[can't find world-writeable directory to test DCL$PATH], 2
196               unless $tmp;
197
198             $ENV{'DCL$PATH'} = $tmp;
199             is(eval { `$echo 1` }, undef);
200             like($@, qr/^Insecure directory in \$ENV\{DCL\$PATH}/);
201         }
202         $ENV{'DCL$PATH'} = '';
203     }
204 }
205
206 # Let's see that we can taint and untaint as needed.
207 {
208     my $foo = $TAINT;
209     is_tainted($foo);
210
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;
213
214     $foo = "foo";
215     isnt_tainted($foo);
216
217     taint_these($foo);
218     is_tainted($foo);
219
220     my @list = 1..10;
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];
225
226     ($foo) = $foo =~ /(.+)/;
227     isnt_tainted($foo);
228
229     my ($desc, $s, $res, $res2, $one);
230
231     $desc = "match with string tainted";
232
233     $s = 'abcd' . $TAINT;
234     $res = $s =~ /(.+)/;
235     $one = $1;
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");
241
242     $desc = "match /g with string tainted";
243
244     $s = 'abcd' . $TAINT;
245     $res = $s =~ /(.)/g;
246     $one = $1;
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");
252
253     $desc = "match with string tainted, list cxt";
254
255     $s = 'abcd' . $TAINT;
256     ($res) = $s =~ /(.+)/;
257     $one = $1;
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");
263
264     $desc = "match /g with string tainted, list cxt";
265
266     $s = 'abcd' . $TAINT;
267     ($res, $res2) = $s =~ /(.)/g;
268     $one = $1;
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");
276
277     $desc = "match with pattern tainted";
278
279     $s = 'abcd';
280     $res = $s =~ /$TAINT(.+)/;
281     $one = $1;
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");
287
288     $desc = "match /g with pattern tainted";
289
290     $s = 'abcd';
291     $res = $s =~ /$TAINT(.)/g;
292     $one = $1;
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");
298
299     $desc = "match with pattern tainted via locale";
300
301     $s = 'abcd';
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");
308
309     $desc = "match /g with pattern tainted via locale";
310
311     $s = 'abcd';
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");
318
319     $desc = "match with pattern tainted, list cxt";
320
321     $s = 'abcd';
322     ($res) = $s =~ /$TAINT(.+)/;
323     $one = $1;
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");
329
330     $desc = "match /g with pattern tainted, list cxt";
331
332     $s = 'abcd';
333     ($res, $res2) = $s =~ /$TAINT(.)/g;
334     $one = $1;
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");
341
342     $desc = "match with pattern tainted via locale, list cxt";
343
344     $s = 'abcd';
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");
351
352     $desc = "match /g with pattern tainted via locale, list cxt";
353
354     $s = 'abcd';
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");
363
364     $desc = "substitution with string tainted";
365
366     $s = 'abcd' . $TAINT;
367     $res = $s =~ s/(.+)/xyz/;
368     $one = $1;
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");
375
376     $desc = "substitution /g with string tainted";
377
378     $s = 'abcd' . $TAINT;
379     $res = $s =~ s/(.)/x/g;
380     $one = $1;
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");
387
388     $desc = "substitution /r with string tainted";
389
390     $s = 'abcd' . $TAINT;
391     $res = $s =~ s/(.+)/xyz/r;
392     $one = $1;
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");
399
400     $desc = "substitution /e with string tainted";
401
402     $s = 'abcd' . $TAINT;
403     $one = '';
404     $res = $s =~ s{(.+)}{
405                 $one = $one . "x"; # make sure code not tainted
406                 isnt_tainted($one, "$desc: code not tainted within /e");
407                 $one = $1;
408                 isnt_tainted($one, "$desc: \$1 not tainted within /e");
409                 "xyz";
410             }e;
411     $one = $1;
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");
418
419     $desc = "substitution with pattern tainted";
420
421     $s = 'abcd';
422     $res = $s =~ s/$TAINT(.+)/xyz/;
423     $one = $1;
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");
430
431     $desc = "substitution /g with pattern tainted";
432
433     $s = 'abcd';
434     $res = $s =~ s/$TAINT(.)/x/g;
435     $one = $1;
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");
442
443     $desc = "substitution /ge with pattern tainted";
444
445     $s = 'abc';
446     {
447         my $i = 0;
448         my $j;
449         $res = $s =~ s{(.)$TAINT}{
450                     $j = $i; # make sure code not tainted
451                     $one = $1;
452                     isnt_tainted($j, "$desc: code not tainted within /e");
453                     $i++;
454                     if ($i == 1) {
455                         isnt_tainted($s,   "$desc: s not tainted loop 1");
456                     }
457                     else {
458                         is_tainted($s,     "$desc: s tainted loop $i");
459                     }
460                     is_tainted($one,   "$desc: \$1 tainted loop $i");
461                     $i.$TAINT;
462                 }ge;
463         $one = $1;
464     }
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");
471
472     $desc = "substitution /r with pattern tainted";
473
474     $s = 'abcd';
475     $res = $s =~ s/$TAINT(.+)/xyz/r;
476     $one = $1;
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");
483
484     $desc = "substitution with pattern tainted via locale";
485
486     $s = 'abcd';
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");
494
495     $desc = "substitution /g with pattern tainted via locale";
496
497     $s = 'abcd';
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");
505
506     $desc = "substitution /r with pattern tainted via locale";
507
508     $s = 'abcd';
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");
516
517     $desc = "substitution with replacement tainted";
518
519     $s = 'abcd';
520     $res = $s =~ s/(.+)/xyz$TAINT/;
521     $one = $1;
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");
528
529     $desc = "substitution /g with replacement tainted";
530
531     $s = 'abcd';
532     $res = $s =~ s/(.)/x$TAINT/g;
533     $one = $1;
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");
540
541     $desc = "substitution /ge with replacement tainted";
542
543     $s = 'abc';
544     {
545         my $i = 0;
546         my $j;
547         $res = $s =~ s{(.)}{
548                     $j = $i; # make sure code not tainted
549                     $one = $1;
550                     isnt_tainted($j, "$desc: code not tainted within /e");
551                     $i++;
552                     if ($i == 1) {
553                         isnt_tainted($s,   "$desc: s not tainted loop 1");
554                     }
555                     else {
556                         is_tainted($s,     "$desc: s tainted loop $i");
557                     }
558                     isnt_tainted($one, "$desc: \$1 not tainted within /e");
559                     $i.$TAINT;
560                 }ge;
561         $one = $1;
562     }
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");
569
570     $desc = "substitution /r with replacement tainted";
571
572     $s = 'abcd';
573     $res = $s =~ s/(.+)/xyz$TAINT/r;
574     $one = $1;
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");
581
582     {
583         # now do them all again with "use re 'taint"
584
585         use re 'taint';
586
587         $desc = "use re 'taint': match with string tainted";
588
589         $s = 'abcd' . $TAINT;
590         $res = $s =~ /(.+)/;
591         $one = $1;
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");
597
598         $desc = "use re 'taint': match /g with string tainted";
599
600         $s = 'abcd' . $TAINT;
601         $res = $s =~ /(.)/g;
602         $one = $1;
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");
608
609         $desc = "use re 'taint': match with string tainted, list cxt";
610
611         $s = 'abcd' . $TAINT;
612         ($res) = $s =~ /(.+)/;
613         $one = $1;
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");
619
620         $desc = "use re 'taint': match /g with string tainted, list cxt";
621
622         $s = 'abcd' . $TAINT;
623         ($res, $res2) = $s =~ /(.)/g;
624         $one = $1;
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");
632
633         $desc = "use re 'taint': match with pattern tainted";
634
635         $s = 'abcd';
636         $res = $s =~ /$TAINT(.+)/;
637         $one = $1;
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");
643
644         $desc = "use re 'taint': match /g with pattern tainted";
645
646         $s = 'abcd';
647         $res = $s =~ /$TAINT(.)/g;
648         $one = $1;
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");
654
655         $desc = "use re 'taint': match with pattern tainted via locale";
656
657         $s = 'abcd';
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");
664
665         $desc = "use re 'taint': match /g with pattern tainted via locale";
666
667         $s = 'abcd';
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");
674
675         $desc = "use re 'taint': match with pattern tainted, list cxt";
676
677         $s = 'abcd';
678         ($res) = $s =~ /$TAINT(.+)/;
679         $one = $1;
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");
685
686         $desc = "use re 'taint': match /g with pattern tainted, list cxt";
687
688         $s = 'abcd';
689         ($res, $res2) = $s =~ /$TAINT(.)/g;
690         $one = $1;
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");
697
698         $desc = "use re 'taint': match with pattern tainted via locale, list cxt";
699
700         $s = 'abcd';
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");
707
708         $desc = "use re 'taint': match /g with pattern tainted via locale, list cxt";
709
710         $s = 'abcd';
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");
719
720         $desc = "use re 'taint': substitution with string tainted";
721
722         $s = 'abcd' . $TAINT;
723         $res = $s =~ s/(.+)/xyz/;
724         $one = $1;
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");
731
732         $desc = "use re 'taint': substitution /g with string tainted";
733
734         $s = 'abcd' . $TAINT;
735         $res = $s =~ s/(.)/x/g;
736         $one = $1;
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");
743
744         $desc = "use re 'taint': substitution /r with string tainted";
745
746         $s = 'abcd' . $TAINT;
747         $res = $s =~ s/(.+)/xyz/r;
748         $one = $1;
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");
755
756         $desc = "use re 'taint': substitution /e with string tainted";
757
758         $s = 'abcd' . $TAINT;
759         $one = '';
760         $res = $s =~ s{(.+)}{
761                     $one = $one . "x"; # make sure code not tainted
762                     isnt_tainted($one, "$desc: code not tainted within /e");
763                     $one = $1;
764                     is_tainted($one, "$desc: $1 tainted within /e");
765                     "xyz";
766                 }e;
767         $one = $1;
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");
774
775         $desc = "use re 'taint': substitution with pattern tainted";
776
777         $s = 'abcd';
778         $res = $s =~ s/$TAINT(.+)/xyz/;
779         $one = $1;
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");
786
787         $desc = "use re 'taint': substitution /g with pattern tainted";
788
789         $s = 'abcd';
790         $res = $s =~ s/$TAINT(.)/x/g;
791         $one = $1;
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");
798
799         $desc = "use re 'taint': substitution /ge with pattern tainted";
800
801         $s = 'abc';
802         {
803             my $i = 0;
804             my $j;
805             $res = $s =~ s{(.)$TAINT}{
806                         $j = $i; # make sure code not tainted
807                         $one = $1;
808                         isnt_tainted($j, "$desc: code not tainted within /e");
809                         $i++;
810                         if ($i == 1) {
811                             isnt_tainted($s,   "$desc: s not tainted loop 1");
812                         }
813                         else {
814                             is_tainted($s,     "$desc: s tainted loop $i");
815                         }
816                         is_tainted($one,   "$desc: \$1 tainted loop $i");
817                         $i.$TAINT;
818                     }ge;
819             $one = $1;
820         }
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");
827
828
829         $desc = "use re 'taint': substitution /r with pattern tainted";
830
831         $s = 'abcd';
832         $res = $s =~ s/$TAINT(.+)/xyz/r;
833         $one = $1;
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");
840
841         $desc = "use re 'taint': substitution with pattern tainted via locale";
842
843         $s = 'abcd';
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");
851
852         $desc = "use re 'taint': substitution /g with pattern tainted via locale";
853
854         $s = 'abcd';
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");
862
863         $desc = "use re 'taint': substitution /r with pattern tainted via locale";
864
865         $s = 'abcd';
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");
873
874         $desc = "use re 'taint': substitution with replacement tainted";
875
876         $s = 'abcd';
877         $res = $s =~ s/(.+)/xyz$TAINT/;
878         $one = $1;
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");
885
886         $desc = "use re 'taint': substitution /g with replacement tainted";
887
888         $s = 'abcd';
889         $res = $s =~ s/(.)/x$TAINT/g;
890         $one = $1;
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");
897
898         $desc = "use re 'taint': substitution /ge with replacement tainted";
899
900         $s = 'abc';
901         {
902             my $i = 0;
903             my $j;
904             $res = $s =~ s{(.)}{
905                         $j = $i; # make sure code not tainted
906                         $one = $1;
907                         isnt_tainted($j, "$desc: code not tainted within /e");
908                         $i++;
909                         if ($i == 1) {
910                             isnt_tainted($s,   "$desc: s not tainted loop 1");
911                         }
912                         else {
913                             is_tainted($s,     "$desc: s tainted loop $i");
914                         }
915                             isnt_tainted($one, "$desc: \$1 not tainted");
916                         $i.$TAINT;
917                     }ge;
918             $one = $1;
919         }
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");
926
927         $desc = "use re 'taint': substitution /r with replacement tainted";
928
929         $s = 'abcd';
930         $res = $s =~ s/(.+)/xyz$TAINT/r;
931         $one = $1;
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");
938     }
939
940     $foo = $1 if 'bar' =~ /(.+)$TAINT/;
941     is_tainted($foo);
942     is($foo, 'bar');
943
944     my $pi = 4 * atan2(1,1) + $TAINT0;
945     is_tainted($pi);
946
947     ($pi) = $pi =~ /(\d+\.\d+)/;
948     isnt_tainted($pi);
949     is(sprintf("%.5f", $pi), '3.14159');
950 }
951
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.
954 SKIP: {
955     my $arg = tempfile();
956     open $fh, '>', $arg or die "Can't create $arg: $!";
957     print $fh q{
958         eval { join('', @ARGV), kill 0 };
959         exit 0 if $@ =~ /^Insecure dependency/;
960         print "# Oops: \$@ was [$@]\n";
961         exit 1;
962     };
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 $?");
966     unlink $arg;
967 }
968
969 # Reading from a file should be tainted
970 {
971     ok(open my $fh, '<', $TEST) or diag("Couldn't open '$TEST': $!");
972
973     my $block;
974     sysread($fh, $block, 100);
975     my $line = <$fh>;
976     close $fh;
977     is_tainted($block);
978     is_tainted($line);
979 }
980
981 # Output of commands should be tainted
982 {
983     my $foo = `$echo abc`;
984     is_tainted($foo);
985 }
986
987 # Certain system variables should be tainted
988 {
989     is_tainted($^X);
990     is_tainted($0);
991 }
992
993 # Results of matching should all be untainted
994 {
995     my $foo = "abcdefghi" . $TAINT;
996     is_tainted($foo);
997
998     $foo =~ /def/;
999     isnt_tainted($`);
1000     isnt_tainted($&);
1001     isnt_tainted($');
1002
1003     $foo =~ /(...)(...)(...)/;
1004     isnt_tainted($1);
1005     isnt_tainted($2);
1006     isnt_tainted($3);
1007     isnt_tainted($+);
1008
1009     my @bar = $foo =~ /(...)(...)(...)/;
1010     isnt_tainted($_) foreach @bar;
1011
1012     is_tainted($foo);   # $foo should still be tainted!
1013     is($foo, "abcdefghi");
1014 }
1015
1016 # Operations which affect files can't use tainted data.
1017 {
1018     violates_taint(sub { chmod 0, $TAINT }, 'chmod');
1019
1020     SKIP: {
1021         skip "truncate() is not available", 2 unless $Config{d_truncate};
1022
1023         violates_taint(sub { truncate 'NoSuChFiLe', $TAINT0 }, 'truncate');
1024     }
1025
1026     violates_taint(sub { rename '', $TAINT }, 'rename');
1027     violates_taint(sub { unlink $TAINT }, 'unlink');
1028     violates_taint(sub { utime $TAINT }, 'utime');
1029
1030     SKIP: {
1031         skip "chown() is not available", 2 unless $Config{d_chown};
1032
1033         violates_taint(sub { chown -1, -1, $TAINT }, 'chown');
1034     }
1035
1036     SKIP: {
1037         skip "link() is not available", 2 unless $Config{d_link};
1038
1039 violates_taint(sub { link $TAINT, '' }, 'link');
1040     }
1041
1042     SKIP: {
1043         skip "symlink() is not available", 2 unless $Config{d_symlink};
1044
1045         violates_taint(sub { symlink $TAINT, '' }, 'symlink');
1046     }
1047 }
1048
1049 # Operations which affect directories can't use tainted data.
1050 {
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');
1054
1055     SKIP: {
1056         skip "chroot() is not available", 2 unless $Config{d_chroot};
1057
1058         violates_taint(sub { chroot $TAINT }, 'chroot');
1059     }
1060 }
1061
1062 # Some operations using files can't use tainted data.
1063 {
1064     my $foo = "imaginary library" . $TAINT;
1065     violates_taint(sub { require $foo }, 'require');
1066
1067     my $filename = tempfile();  # NB: $filename isn't tainted!
1068     $foo = $filename . $TAINT;
1069     unlink $filename;   # in any case
1070
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
1075
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));
1082
1083     violates_taint(sub { open FOO, "> $foo" }, 'open', 'open for write');
1084     violates_taint(sub { open my $fh, '>', $foo }, 'open', 'open for write');
1085 }
1086
1087 # Commands to the system can't use tainted data
1088 {
1089     my $foo = $TAINT;
1090
1091     SKIP: {
1092         skip "open('|') is not available", 8 if $^O eq 'amigaos';
1093
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');
1098     }
1099
1100     violates_taint(sub { exec $TAINT }, 'exec');
1101     violates_taint(sub { system $TAINT }, 'system');
1102
1103     $foo = "*";
1104     taint_these $foo;
1105
1106     violates_taint(sub { `$echo 1$foo` }, '``', 'backticks');
1107
1108     SKIP: {
1109         # wildcard expansion doesn't invoke shell on VMS, so is safe
1110         skip "This is not VMS", 2 unless $Is_VMS;
1111     
1112         isnt(join('', eval { glob $foo } ), '', 'globbing');
1113         is($@, '');
1114     }
1115 }
1116
1117 # Operations which affect processes can't use tainted data.
1118 {
1119     violates_taint(sub { kill 0, $TAINT }, 'kill');
1120
1121     SKIP: {
1122         skip "setpgrp() is not available", 2 unless $Config{d_setpgrp};
1123
1124         violates_taint(sub { setpgrp 0, $TAINT0 }, 'setpgrp');
1125     }
1126
1127     SKIP: {
1128         skip "setpriority() is not available", 2 unless $Config{d_setprior};
1129
1130         violates_taint(sub { setpriority 0, $TAINT0, $TAINT0 }, 'setpriority');
1131     }
1132 }
1133
1134 # Some miscellaneous operations can't use tainted data.
1135 {
1136     SKIP: {
1137         skip "syscall() is not available", 2 unless $Config{d_syscall};
1138
1139         violates_taint(sub { syscall $TAINT }, 'syscall');
1140     }
1141
1142     {
1143         my $foo = "x" x 979;
1144         taint_these $foo;
1145         local *FOO;
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');
1149
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');
1153
1154         SKIP: {
1155             skip "fcntl() is not available", 4 unless $Config{d_fcntl};
1156
1157             violates_taint(sub { fcntl FOO, $TAINT0, $foo }, 'fcntl');
1158             violates_taint(sub { fcntl $fh, $TAINT0, $foo }, 'fcntl');
1159         }
1160
1161         close FOO;
1162     }
1163 }
1164
1165 # Some tests involving references
1166 {
1167     my $foo = 'abc' . $TAINT;
1168     my $fooref = \$foo;
1169     isnt_tainted($fooref);
1170     is_tainted($$fooref);
1171     is_tainted($foo);
1172 }
1173
1174 # Some tests involving assignment
1175 {
1176     my $foo = $TAINT0;
1177     my $bar = $foo;
1178     is_tainted($foo);
1179     is_tainted($bar);
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);
1185     is_tainted($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);
1191     is_tainted($bar--);
1192     is($bar, 0);
1193 }
1194
1195 # Test assignment and return of lists
1196 {
1197     my @foo = ("A", "tainted" . $TAINT, "B");
1198     isnt_tainted($foo[0]);
1199     is_tainted(    $foo[1]);
1200     isnt_tainted($foo[2]);
1201     my @bar = @foo;
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]);
1229 }
1230
1231 # Test for system/library calls returning string data of dubious origin.
1232 {
1233     # No reliable %Config check for getpw*
1234     SKIP: {
1235         skip "getpwent() is not available", 9 unless 
1236           eval { setpwent(); getpwent() };
1237
1238         setpwent();
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');
1250         endpwent();
1251     }
1252
1253     SKIP: {
1254         # pretty hard to imagine not
1255         skip "readdir() is not available", 1 unless $Config{d_readdir};
1256
1257         opendir my $dh, "op" or die "opendir: $!\n";
1258         my $readdir = readdir $dh;
1259         is_tainted($readdir);
1260         closedir $dh;
1261     }
1262
1263     SKIP: {
1264         skip "readlink() or symlink() is not available" unless 
1265           $Config{d_readlink} && $Config{d_symlink};
1266
1267         my $symlink = "sl$$";
1268         unlink($symlink);
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);
1274         unlink($symlink);
1275     }
1276 }
1277
1278 # test bitwise ops (regression bug)
1279 {
1280     my $why = "y";
1281     my $j = "x" | $why;
1282     isnt_tainted($j);
1283     $why = $TAINT."y";
1284     $j = "x" | $why;
1285     is_tainted(    $j);
1286 }
1287
1288 # test target of substitution (regression bug)
1289 {
1290     my $why = $TAINT."y";
1291     $why =~ s/y/z/;
1292     is_tainted(    $why);
1293
1294     my $z = "[z]";
1295     $why =~ s/$z/zee/;
1296     is_tainted(    $why);
1297
1298     $why =~ s/e/'-'.$$/ge;
1299     is_tainted(    $why);
1300 }
1301
1302
1303 SKIP: {
1304     skip "no IPC::SysV", 2 unless $ipcsysv;
1305
1306     # test shmread
1307     SKIP: {
1308         skip "shm*() not available", 1 unless $Config{d_shm};
1309
1310         no strict 'subs';
1311         my $sent = "foobar";
1312         my $rcvd;
1313         my $size = 2000;
1314         my $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
1315
1316         if (defined $id) {
1317             if (shmwrite($id, $sent, 0, 60)) {
1318                 if (shmread($id, $rcvd, 0, 60)) {
1319                     substr($rcvd, index($rcvd, "\0")) = '';
1320                 } else {
1321                     warn "# shmread failed: $!\n";
1322                 }
1323             } else {
1324                 warn "# shmwrite failed: $!\n";
1325             }
1326             shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
1327         } else {
1328             warn "# shmget failed: $!\n";
1329         }
1330
1331         skip "SysV shared memory operation failed", 1 unless 
1332           $rcvd eq $sent;
1333
1334         is_tainted($rcvd);
1335     }
1336
1337
1338     # test msgrcv
1339     SKIP: {
1340         skip "msg*() not available", 1 unless $Config{d_msg};
1341
1342         no strict 'subs';
1343         my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
1344
1345         my $sent      = "message";
1346         my $type_sent = 1234;
1347         my $rcvd;
1348         my $type_rcvd;
1349
1350         if (defined $id) {
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);
1354                 } else {
1355                     warn "# msgrcv failed: $!\n";
1356                 }
1357             } else {
1358                 warn "# msgsnd failed: $!\n";
1359             }
1360             msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
1361         } else {
1362             warn "# msgget failed\n";
1363         }
1364
1365         SKIP: {
1366             skip "SysV message queue operation failed", 1
1367               unless $rcvd eq $sent && $type_sent == $type_rcvd;
1368
1369             is_tainted($rcvd);
1370         }
1371     }
1372 }
1373
1374 {
1375     # bug id 20001004.006
1376
1377     open my $fh, '<', $TEST or warn "$0: cannot read $TEST: $!" ;
1378     local $/;
1379     my $a = <$fh>;
1380     my $b = <$fh>;
1381
1382     is_tainted($a);
1383     is_tainted($b);
1384     is($b, undef);
1385 }
1386
1387 {
1388     # bug id 20001004.007
1389
1390     open my $fh, '<', $TEST or warn "$0: cannot read $TEST: $!" ;
1391     my $a = <$fh>;
1392
1393     my $c = { a => 42,
1394               b => $a };
1395
1396     isnt_tainted($c->{a});
1397     is_tainted($c->{b});
1398
1399
1400     my $d = { a => $a,
1401               b => 42 };
1402     is_tainted($d->{a});
1403     isnt_tainted($d->{b});
1404
1405
1406     my $e = { a => 42,
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});
1412 }
1413
1414 {
1415     # bug id 20010519.003
1416
1417     BEGIN {
1418         use vars qw($has_fcntl);
1419         eval { require Fcntl; import Fcntl; };
1420         unless ($@) {
1421             $has_fcntl = 1;
1422         }
1423     }
1424
1425     SKIP: {
1426         skip "no Fcntl", 18 unless $has_fcntl;
1427
1428         my $foo = tempfile();
1429         my $evil = $foo . $TAINT;
1430
1431         is(eval { sysopen(my $ro, $evil, &O_RDONLY) }, undef);
1432         is($@, '');
1433
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');
1439
1440         is(eval { sysopen(my $ro, $foo, &O_RDONLY | $TAINT0) }, undef);
1441         is($@, '');
1442
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);
1449         is($@, '');
1450
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');
1456     }
1457 }
1458
1459 {
1460     # bug 20010526.004
1461
1462     use warnings;
1463
1464     my $saw_warning = 0;
1465     local $SIG{__WARN__} = sub { ++$saw_warning };
1466
1467     sub fmi {
1468         my $divnum = shift()/1;
1469         sprintf("%1.1f\n", $divnum);
1470     }
1471
1472     fmi(21 . $TAINT);
1473     fmi(37);
1474     fmi(248);
1475
1476     is($saw_warning, 0);
1477 }
1478
1479
1480 {
1481     # Bug ID 20010730.010
1482
1483     my $i = 0;
1484
1485     sub Tie::TIESCALAR {
1486         my $class =  shift;
1487         my $arg   =  shift;
1488
1489         bless \$arg => $class;
1490     }
1491
1492     sub Tie::FETCH {
1493         $i ++;
1494         ${$_ [0]}
1495     }
1496
1497  
1498     package main;
1499  
1500     my $bar = "The Big Bright Green Pleasure Machine";
1501     taint_these $bar;
1502     tie my ($foo), Tie => $bar;
1503
1504     my $baz = $foo;
1505
1506     ok $i == 1;
1507 }
1508
1509 {
1510     # Check that all environment variables are tainted.
1511     my @untainted;
1512     while (my ($k, $v) = each %ENV) {
1513         if (!tainted($v) &&
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";
1517         }
1518     }
1519     is("@untainted", "");
1520 }
1521
1522
1523 is(${^TAINT}, 1, '$^TAINT is on');
1524
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');
1529
1530 {
1531     # bug 20011111.105
1532     
1533     my $re1 = qr/x$TAINT/;
1534     is_tainted($re1);
1535     
1536     my $re2 = qr/^$re1\z/;
1537     is_tainted($re2);
1538     
1539     my $re3 = "$re2";
1540     is_tainted($re3);
1541 }
1542
1543 SKIP: {
1544     skip "system {} has different semantics on Win32", 1 if $Is_MSWin32;
1545
1546     # bug 20010221.005
1547     local $ENV{PATH} .= $TAINT;
1548     eval { system { "echo" } "/arg0", "arg1" };
1549     like($@, qr/^Insecure \$ENV/);
1550 }
1551
1552 TODO: {
1553     todo_skip 'tainted %ENV warning occludes tainted arguments warning', 22
1554       if $Is_VMS;
1555
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');
1562
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');
1568
1569     eval { 
1570         no warnings;
1571         system("lskdfj does not exist","with","args"); 
1572     };
1573     is($@, "");
1574
1575     eval {
1576         no warnings;
1577         exec("lskdfj does not exist","with","args"); 
1578     };
1579     is($@, "");
1580
1581     # If you add tests here update also the above skip block for VMS.
1582 }
1583
1584 {
1585     # [ID 20020704.001] taint propagation failure
1586     use re 'taint';
1587     $TAINT =~ /(.*)/;
1588     is_tainted(my $foo = $1);
1589 }
1590
1591 {
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/);
1600 }
1601 {
1602     # [perl #24248]
1603     $TAINT =~ /(.*)/;
1604     isnt_tainted($1);
1605     my $notaint = $1;
1606     isnt_tainted($notaint);
1607
1608     my $l;
1609     $notaint =~ /($notaint)/;
1610     $l = $1;
1611     isnt_tainted($1);
1612     isnt_tainted($l);
1613     $notaint =~ /($TAINT)/;
1614     $l = $1;
1615     is_tainted($1);
1616     is_tainted($l);
1617
1618     $TAINT =~ /($notaint)/;
1619     $l = $1;
1620     isnt_tainted($1);
1621     isnt_tainted($l);
1622     $TAINT =~ /($TAINT)/;
1623     $l = $1;
1624     is_tainted($1);
1625     is_tainted($l);
1626
1627     my $r;
1628     ($r = $TAINT) =~ /($notaint)/;
1629     isnt_tainted($1);
1630     ($r = $TAINT) =~ /($TAINT)/;
1631     is_tainted($1);
1632
1633     {
1634         use re 'eval'; # this shouldn't make any difference
1635         ($r = $TAINT) =~ /($notaint)/;
1636         isnt_tainted($1);
1637         ($r = $TAINT) =~ /($TAINT)/;
1638         is_tainted($1);
1639     }
1640
1641     #  [perl #24674]
1642     # accessing $^O  shoudn't taint it as a side-effect;
1643     # assigning tainted data to it is now an error
1644
1645     isnt_tainted($^O);
1646     if (!$^X) { } elsif ($^O eq 'bar') { }
1647     isnt_tainted($^O);
1648     local $^O;  # We're going to clobber something test infrastructure depends on.
1649     eval '$^O = $^X';
1650     like($@, qr/Insecure dependency in/);
1651 }
1652
1653 EFFECTIVELY_CONSTANTS: {
1654     my $tainted_number = 12 + $TAINT0;
1655     is_tainted( $tainted_number );
1656
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);
1661 }
1662
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 );
1668
1669     my $result = $tainted_true ? "True" : "False";
1670     is($result, "True");
1671     isnt_tainted( $result );
1672
1673     $result = $tainted_false ? "True" : "False";
1674     is($result, "False");
1675     isnt_tainted( $result );
1676
1677     my $untainted_whatever = "The Fabulous Johnny Cash";
1678     my $tainted_whatever = "Soft Cell" . $TAINT;
1679
1680     $result = $tainted_true ? $tainted_whatever : $untainted_whatever;
1681     is($result, "Soft Cell");
1682     is_tainted( $result );
1683
1684     $result = $tainted_false ? $tainted_whatever : $untainted_whatever;
1685     is($result, "The Fabulous Johnny Cash");
1686     isnt_tainted( $result );
1687 }
1688
1689 {
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
1693
1694     my $foo = "abcdefghi" . $TAINT;
1695
1696     my $valid_chars = 'a-z';
1697     if ( $foo eq '' ) {
1698     }
1699     elsif ( $foo =~ /([$valid_chars]+)/o ) {
1700         isnt_tainted($1);
1701         isnt($1, undef);
1702     }
1703
1704     if ( $foo eq '' ) {
1705     }
1706     elsif ( my @bar = $foo =~ /([$valid_chars]+)/o ) {
1707         isnt_tainted($bar[0]);
1708         is(scalar @bar, 1);
1709     }
1710 }
1711
1712 # at scope exit, a restored localised value should have its old
1713 # taint status, not the taint status of the current statement
1714
1715 {
1716     our $x99 = $^X;
1717     is_tainted($x99);
1718
1719     $x99 = '';
1720     isnt_tainted($x99);
1721
1722     my $c = do { local $x99; $^X };
1723     isnt_tainted($x99);
1724 }
1725 {
1726     our $x99 = $^X;
1727     is_tainted($x99);
1728
1729     my $c = do { local $x99; '' };
1730     is_tainted($x99);
1731 }
1732
1733 # an mg_get of a tainted value during localization shouldn't taint the
1734 # statement
1735
1736 {
1737     eval { local $0, eval '1' };
1738     is($@, '');
1739 }
1740
1741 # [perl #8262] //g loops infinitely on tainted data
1742
1743 {
1744     my @a;
1745     $a[0] = $^X . '-';
1746     $a[0]=~ m/(.)/g;
1747     cmp_ok pos($a[0]), '>', 0, "infinite m//g on arrays (aelemfast)";
1748
1749     my $i = 1;
1750     $a[$i] = $^X . '-';
1751     $a[$i]=~ m/(.)/g;
1752     cmp_ok pos($a[$i]), '>', 0, "infinite m//g on arrays (aelem)";
1753
1754     my %h;
1755     $h{a} = $^X . '-';
1756     $h{a}=~ m/(.)/g;
1757     cmp_ok pos($h{a}), '>', 0, "infinite m//g on hashes (helem)";
1758 }
1759
1760 SKIP:
1761 {
1762     my $got_dualvar;
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);
1766     my $b = $a + 5;
1767     is ($b, 8, "Arithmetic on tainted dualvars works");
1768 }
1769
1770 # opening '|-' should not trigger $ENV{PATH} check
1771
1772 {
1773     SKIP: {
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;
1777
1778         $ENV{'PATH'} = $TAINT;
1779         local $SIG{'PIPE'} = 'IGNORE';
1780         eval {
1781             my $pid = open my $pipe, '|-';
1782             if (!defined $pid) {
1783                 die "open failed: $!";
1784             }
1785             if (!$pid) {
1786                 kill 'KILL', $$;        # child suicide
1787             }
1788             close $pipe;
1789         };
1790         unlike($@, qr/Insecure \$ENV/, 'fork triggers %ENV check');
1791         is($@, '',               'pipe/fork/open/close failed');
1792         eval {
1793             open my $pipe, "|$Invoke_Perl -e 1";
1794             close $pipe;
1795         };
1796         like($@, qr/Insecure \$ENV/, 'popen neglects %ENV check');
1797     }
1798 }
1799
1800 {
1801     package AUTOLOAD_TAINT;
1802     sub AUTOLOAD {
1803         our $AUTOLOAD;
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');
1809         } else {
1810             main::is_tainted($AUTOLOAD, '$AUTOLOAD can be tainted');
1811             my $copy = $AUTOLOAD;
1812             main::is_tainted($copy, '$AUTOLOAD can be tainted');
1813         }
1814     }
1815
1816     package main;
1817     my $o = bless [], 'AUTOLOAD_TAINT';
1818     $o->untainted;
1819     $o->$TAINT;
1820     $o->untainted;
1821 }
1822
1823 {
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/);
1838 }
1839
1840 {
1841     # 40708
1842     my $n  = 7e9;
1843     8e9 - $n;
1844
1845     my $val = $n;
1846     is ($val, '7000000000', 'Assignment to untainted variable');
1847     $val = $TAINT;
1848     $val = $n;
1849     is ($val, '7000000000', 'Assignment to tainted variable');
1850 }
1851
1852 {
1853     my $val = 0;
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/);
1858
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 $!;
1862     $tainted = <$fh>;
1863
1864     eval 'eval $tainted';
1865     like ($@, qr/^Insecure dependency in eval/);
1866 }
1867
1868 foreach my $ord (78, 163, 256) {
1869     # 47195
1870     my $line = 'A1' . $TAINT . chr $ord;
1871     chop $line;
1872     is($line, 'A1');
1873     $line =~ /(A\S*)/;
1874     isnt_tainted($1, "\\S match with chr $ord");
1875 }
1876
1877 {
1878     # 59998
1879     sub cr { my $x = crypt($_[0], $_[1]); $x }
1880     sub co { my $x = ~$_[0]; $x }
1881     my ($a, $b);
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);
1887     $b = co('foo');
1888     is_tainted($a,  "tainted complement");
1889     isnt_tainted($b, "untainted complement");
1890 }
1891
1892 {
1893     my @data = qw(bonk zam zlonk qunckkk);
1894     # Clearly some sort of usenet bang-path
1895     my $string = $TAINT . join "!", @data;
1896
1897     is_tainted($string, "tainted data");
1898
1899     my @got = split /!|,/, $string;
1900
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");
1905     }
1906
1907     is_tainted($string, "still tainted data");
1908
1909     my @got = split /[!,]/, $string;
1910
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");
1915     }
1916
1917     is_tainted($string, "still tainted data");
1918
1919     my @got = split /!/, $string;
1920
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");
1925     }
1926 }
1927
1928 # Bug RT #52552 - broken by change at git commit id f337b08
1929 {
1930     my $x = $TAINT. q{print "Hello world\n"};
1931     my $y = pack "a*", $x;
1932     is_tainted($y, "pack a* preserves tainting");
1933
1934     my $z = pack "A*", q{print "Hello world\n"}.$TAINT;
1935     is_tainted($z, "pack A* preserves tainting");
1936
1937     my $zz = pack "a*a*", q{print "Hello world\n"}, $TAINT;
1938     is_tainted($zz, "pack a*a* preserves tainting");
1939 }
1940
1941 # Bug RT #61976 tainted $! would show numeric rather than string value
1942
1943 {
1944     my $tainted_path = substr($^X,0,0) . "/no/such/file";
1945     my $err;
1946     # $! is used in a tainted expression, so gets tainted
1947     open my $fh, $tainted_path or $err= "$!";
1948     unlike($err, qr/^\d+$/, 'tainted $!');
1949 }
1950
1951 {
1952     # #6758: tainted values become untainted in tied hashes
1953     #         (also applies to other value magic such as pos)
1954
1955
1956     package P6758;
1957
1958     sub TIEHASH { bless {} }
1959     sub TIEARRAY { bless {} }
1960
1961     my $i = 0;
1962
1963     sub STORE {
1964         main::is_tainted($_[1], "tied arg1 tainted");
1965         main::is_tainted($_[2], "tied arg2 tainted");
1966         $i++;
1967     }
1968
1969     package main;
1970
1971     my ($k,$v) = qw(1111 val);
1972     taint_these($k,$v);
1973     tie my @array, 'P6758';
1974     tie my %hash , 'P6758';
1975     $array[$k] = $v;
1976     $hash{$k} = $v;
1977     ok $i == 2, "tied STORE called correct number of times";
1978 }
1979
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".
1987
1988 {
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'" );
1998         }
1999     }
2000 }
2001
2002
2003 # Bug RT #67962: old tainted $1 gets treated as tainted
2004 # in next untainted # match
2005
2006 {
2007     use re 'taint';
2008     "abc".$TAINT =~ /(.*)/; # make $1 tainted
2009     is_tainted($1, '$1 should be tainted');
2010
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');
2017 }
2018
2019 {
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;
2027
2028     fresh_perl_is(<<'end', "ok", { switches => [ '-T' ] },
2029     $TAINT = substr($^X, 0, 0);
2030     formline('@'.('<'x("2000".$TAINT)).' | @*', 'hallo', 'welt');
2031     print "ok";
2032 end
2033     "formline survives a tainted dynamic picture");
2034 }
2035
2036 {
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");
2040     $^A = "";
2041     isnt_tainted($^A, "accumulator can be explicitly untainted");
2042     formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2043     isnt_tainted($^A, "accumulator still untainted");
2044     $^A = "" . $TAINT;
2045     is_tainted($^A, "accumulator can be explicitly tainted");
2046     formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2047     is_tainted($^A, "accumulator still tainted");
2048     $^A = "";
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");
2055 }
2056
2057 {   # Bug #80610
2058     "Constant(1)" =~ / ^ ([a-z_]\w*) (?: [(] (.*) [)] )? $ /xi;
2059     my $a = $1;
2060     my $b = $2;
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");
2063 }
2064
2065 {
2066     # RT 81230: tainted value during FETCH created extra ref to tied obj
2067
2068     package P81230;
2069     use warnings;
2070
2071     my %h;
2072
2073     sub TIEHASH {
2074         my $x = $^X; # tainted
2075         bless  \$x;
2076     }
2077     sub FETCH { my $x = $_[0]; $$x . "" }
2078
2079     tie %h, 'P81230';
2080
2081     my $w = "";
2082     local $SIG{__WARN__} = sub { $w .= "@_" };
2083
2084     untie %h if $h{"k"};
2085
2086     ::is($w, "", "RT 81230");
2087 }
2088
2089 {
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
2094     my $z = $x->();
2095     isnt_tainted($z, "Constants folded value not tainted");
2096 }
2097
2098 {
2099     # now that regexes are first class SVs, make sure that they themselves
2100     # as well as references to them are tainted
2101
2102     my $rr = qr/(.)$TAINT/;
2103     my $r = $$rr; # bare REGEX
2104     my $s ="abc";
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");
2108 }
2109
2110 {
2111     # [perl #82616] security Issues with user-defined \p{} properties
2112     # A using a tainted user-defined property should croak
2113
2114     sub IsA { sprintf "%02x", ord("A") }
2115
2116     my $prop = "IsA";
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");
2122 }
2123
2124 {
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";
2131     $dest = uc $source;
2132     is_tainted $dest, "uc(tainted) taints its return value";
2133     $dest = ucfirst $source;
2134     is_tainted $dest, "ucfirst(tainted) taints its return value";
2135 }
2136
2137 {
2138     # Taintedness of values returned from given()
2139     use feature 'switch';
2140
2141     my @descriptions = ('when', 'given end', 'default');
2142
2143     for (qw<x y z>) {
2144         my $letter = "$_$TAINT";
2145
2146         my $desc = "tainted value returned from " . shift(@descriptions);
2147
2148         my $res = do {
2149             given ($_) {
2150                 when ('x') { $letter }
2151                 when ('y') { goto leavegiven }
2152                 default    { $letter }
2153                 leavegiven:  $letter
2154             }
2155         };
2156         is         $res, $letter, "$desc is correct";
2157         is_tainted $res,          "$desc stays tainted";
2158     }
2159 }
2160
2161
2162 # tainted constants and index()
2163 #  RT 64804; http://bugs.debian.org/291450
2164 {
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");
2169     index(undef, C);
2170     ok(!tainted "", "tainting still works after index() of the constant");
2171 }
2172
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]';
2177
2178 # Tainted values and ref()
2179 for(1,2) {
2180   my $x = bless \"M$TAINT", ref(bless[], "main");
2181 }
2182 pass("no death when TARG of ref is tainted");
2183
2184 # $$ should not be tainted by being read in a tainted expression.
2185 {
2186     isnt_tainted $$, "PID not tainted initially";
2187     my $x = $ENV{PATH}.$$;
2188     isnt_tainted $$, "PID not tainted when read in tainted expression";
2189 }
2190
2191 {
2192     use feature 'fc';
2193     use locale;
2194     my ($latin1, $utf8) = ("\xDF") x 2;
2195     utf8::downgrade($latin1);
2196     utf8::upgrade($utf8);
2197
2198     is_tainted fc($latin1), "under locale, lc(latin1) taints the result";
2199     is_tainted fc($utf8), "under locale, lc(utf8) taints the result";
2200
2201     is_tainted "\F$latin1", "under locale, \\Flatin1 taints the result";
2202     is_tainted "\F$utf8", "under locale, \\Futf8 taints the result";
2203 }
2204
2205 { # 111654
2206   eval {
2207     eval { die "Test\n".substr($ENV{PATH}, 0, 0); };
2208     die;
2209   };
2210   like($@, qr/^Test\n\t\.\.\.propagated at /, "error should be propagated");
2211 }
2212
2213 # tainted run-time (?{}) should die
2214
2215 {
2216     my $code = '(?{})' . $TAINT;
2217     use re 'eval';
2218     eval { "a" =~ /$code/ };
2219     like($@, qr/Eval-group in insecure regular expression/, "tainted (?{})");
2220 }
2221
2222 # This may bomb out with the alarm signal so keep it last
2223 SKIP: {
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)
2227
2228     my $DATA = <<'END' . $TAINT;
2229 line1 is here
2230 line2 is here
2231 line3 is here
2232 line4 is here
2233
2234 END
2235
2236     #study $DATA;
2237
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!
2240
2241     alarm(10);
2242
2243     if ($DATA =~ /^line2.*line4/m) {
2244         fail("Should not be a match")
2245     } else {
2246         pass("Match on tainted multiline data should fail promptly");
2247     }
2248
2249     alarm(0);
2250 }
2251 __END__
2252 # Keep the previous test last