This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
startkve.t: Refactor setting of $errpat
[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     require './test.pl';
13     require './loc_tools.pl';
14     set_up_inc('../lib');
15 }
16
17 use strict;
18 use Config;
19
20 plan tests => 808;
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         # Message can be different depending on whether echo
186         # is a builtin or not
187         like($@, qr/^Insecure (?:directory in )?\$ENV\{PATH}/);
188     }
189
190     SKIP: {
191         skip "This is not VMS", 4 unless $Is_VMS;
192
193         $ENV{'DCL$PATH'} = $TAINT;
194         is(eval { `$echo 1` }, undef);
195         like($@, qr/^Insecure \$ENV\{DCL\$PATH}/);
196         SKIP: {
197             skip q[can't find world-writeable directory to test DCL$PATH], 2
198               unless $tmp;
199
200             $ENV{'DCL$PATH'} = $tmp;
201             is(eval { `$echo 1` }, undef);
202             like($@, qr/^Insecure directory in \$ENV\{DCL\$PATH}/);
203         }
204         $ENV{'DCL$PATH'} = '';
205     }
206 }
207
208 # Let's see that we can taint and untaint as needed.
209 {
210     my $foo = $TAINT;
211     is_tainted($foo);
212
213     # That was a sanity check. If it failed, stop the insanity!
214     die "Taint checks don't seem to be enabled" unless tainted $foo;
215
216     $foo = "foo";
217     isnt_tainted($foo);
218
219     taint_these($foo);
220     is_tainted($foo);
221
222     my @list = 1..10;
223     isnt_tainted($_) foreach @list;
224     taint_these @list[1,3,5,7,9];
225     is_tainted($_) foreach @list[1,3,5,7,9];
226     isnt_tainted($_) foreach @list[0,2,4,6,8];
227
228     ($foo) = $foo =~ /(.+)/;
229     isnt_tainted($foo);
230
231     my ($desc, $s, $res, $res2, $one);
232
233     $desc = "match with string tainted";
234
235     $s = 'abcd' . $TAINT;
236     $res = $s =~ /(.+)/;
237     $one = $1;
238     is_tainted($s,     "$desc: s tainted");
239     isnt_tainted($res, "$desc: res not tainted");
240     isnt_tainted($one, "$desc: \$1 not tainted");
241     is($res, 1,        "$desc: res value");
242     is($one, 'abcd',   "$desc: \$1 value");
243
244     $desc = "match /g with string tainted";
245
246     $s = 'abcd' . $TAINT;
247     $res = $s =~ /(.)/g;
248     $one = $1;
249     is_tainted($s,     "$desc: s tainted");
250     isnt_tainted($res, "$desc: res not tainted");
251     isnt_tainted($one, "$desc: \$1 not tainted");
252     is($res, 1,        "$desc: res value");
253     is($one, 'a',      "$desc: \$1 value");
254
255     $desc = "match with string tainted, list cxt";
256
257     $s = 'abcd' . $TAINT;
258     ($res) = $s =~ /(.+)/;
259     $one = $1;
260     is_tainted($s,     "$desc: s tainted");
261     isnt_tainted($res, "$desc: res not tainted");
262     isnt_tainted($one, "$desc: \$1 not tainted");
263     is($res, 'abcd',   "$desc: res value");
264     is($one, 'abcd',   "$desc: \$1 value");
265
266     $desc = "match /g with string tainted, list cxt";
267
268     $s = 'abcd' . $TAINT;
269     ($res, $res2) = $s =~ /(.)/g;
270     $one = $1;
271     is_tainted($s,     "$desc: s tainted");
272     isnt_tainted($res, "$desc: res not tainted");
273     isnt_tainted($res2,"$desc: res2 not tainted");
274     isnt_tainted($one, "$desc: \$1 not tainted");
275     is($res, 'a',      "$desc: res value");
276     is($res2,'b',      "$desc: res2 value");
277     is($one, 'd',      "$desc: \$1 value");
278
279     $desc = "match with pattern tainted";
280
281     $s = 'abcd';
282     $res = $s =~ /$TAINT(.+)/;
283     $one = $1;
284     isnt_tainted($s,   "$desc: s not tainted");
285     isnt_tainted($res, "$desc: res not tainted");
286     is_tainted($one,   "$desc: \$1 tainted");
287     is($res, 1,        "$desc: res value");
288     is($one, 'abcd',   "$desc: \$1 value");
289
290     $desc = "match /g with pattern tainted";
291
292     $s = 'abcd';
293     $res = $s =~ /$TAINT(.)/g;
294     $one = $1;
295     isnt_tainted($s,   "$desc: s not tainted");
296     isnt_tainted($res, "$desc: res not tainted");
297     is_tainted($one,   "$desc: \$1 tainted");
298     is($res, 1,        "$desc: res value");
299     is($one, 'a',      "$desc: \$1 value");
300
301   SKIP: {
302         skip 'Locales not available', 10 unless locales_enabled('LC_CTYPE');
303
304         $desc = "match with pattern tainted via locale";
305
306         $s = 'abcd';
307         {
308             use locale;
309             $res = $s =~ /(\w+)/; $one = $1;
310         }
311         isnt_tainted($s,   "$desc: s not tainted");
312         isnt_tainted($res, "$desc: res not tainted");
313         is_tainted($one,   "$desc: \$1 tainted");
314         is($res, 1,        "$desc: res value");
315         is($one, 'abcd',   "$desc: \$1 value");
316
317         $desc = "match /g with pattern tainted via locale";
318
319         $s = 'abcd';
320         {
321             use locale;
322             $res = $s =~ /(\w)/g; $one = $1;
323         }
324         isnt_tainted($s,   "$desc: s not tainted");
325         isnt_tainted($res, "$desc: res not tainted");
326         is_tainted($one,   "$desc: \$1 tainted");
327         is($res, 1,        "$desc: res value");
328         is($one, 'a',      "$desc: \$1 value");
329     }
330
331     $desc = "match with pattern tainted, list cxt";
332
333     $s = 'abcd';
334     ($res) = $s =~ /$TAINT(.+)/;
335     $one = $1;
336     isnt_tainted($s,   "$desc: s not tainted");
337     is_tainted($res,   "$desc: res tainted");
338     is_tainted($one,   "$desc: \$1 tainted");
339     is($res, 'abcd',   "$desc: res value");
340     is($one, 'abcd',   "$desc: \$1 value");
341
342     $desc = "match /g with pattern tainted, list cxt";
343
344     $s = 'abcd';
345     ($res, $res2) = $s =~ /$TAINT(.)/g;
346     $one = $1;
347     isnt_tainted($s,   "$desc: s not tainted");
348     is_tainted($res,   "$desc: res tainted");
349     is_tainted($one,   "$desc: \$1 tainted");
350     is($res, 'a',      "$desc: res value");
351     is($res2,'b',      "$desc: res2 value");
352     is($one, 'd',      "$desc: \$1 value");
353
354   SKIP: {
355         skip 'Locales not available', 12 unless locales_enabled('LC_CTYPE');
356
357         $desc = "match with pattern tainted via locale, list cxt";
358
359         $s = 'abcd';
360         {
361             use locale;
362             ($res) = $s =~ /(\w+)/; $one = $1;
363         }
364         isnt_tainted($s,   "$desc: s not tainted");
365         is_tainted($res,   "$desc: res tainted");
366         is_tainted($one,   "$desc: \$1 tainted");
367         is($res, 'abcd',   "$desc: res value");
368         is($one, 'abcd',   "$desc: \$1 value");
369
370         $desc = "match /g with pattern tainted via locale, list cxt";
371
372         $s = 'abcd';
373         {
374             use locale;
375             ($res, $res2) = $s =~ /(\w)/g; $one = $1;
376         }
377         isnt_tainted($s,   "$desc: s not tainted");
378         is_tainted($res,   "$desc: res tainted");
379         is_tainted($res2,  "$desc: res2 tainted");
380         is_tainted($one,   "$desc: \$1 tainted");
381         is($res, 'a',      "$desc: res value");
382         is($res2,'b',      "$desc: res2 value");
383         is($one, 'd',      "$desc: \$1 value");
384     }
385
386     $desc = "substitution with string tainted";
387
388     $s = 'abcd' . $TAINT;
389     $res = $s =~ s/(.+)/xyz/;
390     $one = $1;
391     is_tainted($s,     "$desc: s tainted");
392     isnt_tainted($res, "$desc: res not tainted");
393     isnt_tainted($one, "$desc: \$1 not tainted");
394     is($s,   'xyz',    "$desc: s value");
395     is($res, 1,        "$desc: res value");
396     is($one, 'abcd',   "$desc: \$1 value");
397
398     $desc = "substitution /g with string tainted";
399
400     $s = 'abcd' . $TAINT;
401     $res = $s =~ s/(.)/x/g;
402     $one = $1;
403     is_tainted($s,     "$desc: s tainted");
404     is_tainted($res,   "$desc: res tainted");
405     isnt_tainted($one, "$desc: \$1 not tainted");
406     is($s,   'xxxx',   "$desc: s value");
407     is($res, 4,        "$desc: res value");
408     is($one, 'd',      "$desc: \$1 value");
409
410     $desc = "substitution /r with string tainted";
411
412     $s = 'abcd' . $TAINT;
413     $res = $s =~ s/(.+)/xyz/r;
414     $one = $1;
415     is_tainted($s,     "$desc: s tainted");
416     is_tainted($res,   "$desc: res tainted");
417     isnt_tainted($one, "$desc: \$1 not tainted");
418     is($s,   'abcd',   "$desc: s value");
419     is($res, 'xyz',    "$desc: res value");
420     is($one, 'abcd',   "$desc: \$1 value");
421
422     $desc = "substitution /e with string tainted";
423
424     $s = 'abcd' . $TAINT;
425     $one = '';
426     $res = $s =~ s{(.+)}{
427                 $one = $one . "x"; # make sure code not tainted
428                 isnt_tainted($one, "$desc: code not tainted within /e");
429                 $one = $1;
430                 isnt_tainted($one, "$desc: \$1 not tainted within /e");
431                 "xyz";
432             }e;
433     $one = $1;
434     is_tainted($s,     "$desc: s tainted");
435     isnt_tainted($res, "$desc: res not tainted");
436     isnt_tainted($one, "$desc: \$1 not tainted");
437     is($s,   'xyz',    "$desc: s value");
438     is($res, 1,        "$desc: res value");
439     is($one, 'abcd',   "$desc: \$1 value");
440
441     $desc = "substitution with pattern tainted";
442
443     $s = 'abcd';
444     $res = $s =~ s/$TAINT(.+)/xyz/;
445     $one = $1;
446     is_tainted($s,     "$desc: s tainted");
447     isnt_tainted($res, "$desc: res not tainted");
448     is_tainted($one,   "$desc: \$1 tainted");
449     is($s,  'xyz',     "$desc: s value");
450     is($res, 1,        "$desc: res value");
451     is($one, 'abcd',   "$desc: \$1 value");
452
453     $desc = "substitution /g with pattern tainted";
454
455     $s = 'abcd';
456     $res = $s =~ s/$TAINT(.)/x/g;
457     $one = $1;
458     is_tainted($s,     "$desc: s tainted");
459     is_tainted($res,   "$desc: res tainted");
460     is_tainted($one,   "$desc: \$1 tainted");
461     is($s,  'xxxx',    "$desc: s value");
462     is($res, 4,        "$desc: res value");
463     is($one, 'd',      "$desc: \$1 value");
464
465     $desc = "substitution /ge with pattern tainted";
466
467     $s = 'abc';
468     {
469         my $i = 0;
470         my $j;
471         $res = $s =~ s{(.)$TAINT}{
472                     $j = $i; # make sure code not tainted
473                     $one = $1;
474                     isnt_tainted($j, "$desc: code not tainted within /e");
475                     $i++;
476                     if ($i == 1) {
477                         isnt_tainted($s,   "$desc: s not tainted loop 1");
478                     }
479                     else {
480                         is_tainted($s,     "$desc: s tainted loop $i");
481                     }
482                     is_tainted($one,   "$desc: \$1 tainted loop $i");
483                     $i.$TAINT;
484                 }ge;
485         $one = $1;
486     }
487     is_tainted($s,     "$desc: s tainted");
488     is_tainted($res,   "$desc: res tainted");
489     is_tainted($one,   "$desc: \$1 tainted");
490     is($s,  '123',     "$desc: s value");
491     is($res, 3,        "$desc: res value");
492     is($one, 'c',      "$desc: \$1 value");
493
494     $desc = "substitution /r with pattern tainted";
495
496     $s = 'abcd';
497     $res = $s =~ s/$TAINT(.+)/xyz/r;
498     $one = $1;
499     isnt_tainted($s,   "$desc: s not tainted");
500     is_tainted($res,   "$desc: res tainted");
501     is_tainted($one,   "$desc: \$1 tainted");
502     is($s,  'abcd',    "$desc: s value");
503     is($res, 'xyz',    "$desc: res value");
504     is($one, 'abcd',   "$desc: \$1 value");
505
506   SKIP: {
507         skip 'Locales not available', 18 unless locales_enabled('LC_CTYPE');
508
509         $desc = "substitution with pattern tainted via locale";
510
511         $s = 'abcd';
512         {
513             use locale;
514             $res = $s =~ s/(\w+)/xyz/; $one = $1;
515         }
516         is_tainted($s,     "$desc: s tainted");
517         isnt_tainted($res, "$desc: res not tainted");
518         is_tainted($one,   "$desc: \$1 tainted");
519         is($s,  'xyz',     "$desc: s value");
520         is($res, 1,        "$desc: res value");
521         is($one, 'abcd',   "$desc: \$1 value");
522
523         $desc = "substitution /g with pattern tainted via locale";
524
525         $s = 'abcd';
526         {
527             use locale;
528             $res = $s =~ s/(\w)/x/g; $one = $1;
529         }
530         is_tainted($s,     "$desc: s tainted");
531         is_tainted($res,   "$desc: res tainted");
532         is_tainted($one,   "$desc: \$1 tainted");
533         is($s,  'xxxx',    "$desc: s value");
534         is($res, 4,        "$desc: res value");
535         is($one, 'd',      "$desc: \$1 value");
536
537         $desc = "substitution /r with pattern tainted via locale";
538
539         $s = 'abcd';
540         {
541             use locale;
542             $res = $s =~ s/(\w+)/xyz/r; $one = $1;
543         }
544         isnt_tainted($s,   "$desc: s not tainted");
545         is_tainted($res,   "$desc: res tainted");
546         is_tainted($one,   "$desc: \$1 tainted");
547         is($s,  'abcd',    "$desc: s value");
548         is($res, 'xyz',    "$desc: res value");
549         is($one, 'abcd',   "$desc: \$1 value");
550     }
551
552     $desc = "substitution with replacement tainted";
553
554     $s = 'abcd';
555     $res = $s =~ s/(.+)/xyz$TAINT/;
556     $one = $1;
557     is_tainted($s,     "$desc: s tainted");
558     isnt_tainted($res, "$desc: res not tainted");
559     isnt_tainted($one, "$desc: \$1 not tainted");
560     is($s,  'xyz',     "$desc: s value");
561     is($res, 1,        "$desc: res value");
562     is($one, 'abcd',   "$desc: \$1 value");
563
564     $desc = "substitution /g with replacement tainted";
565
566     $s = 'abcd';
567     $res = $s =~ s/(.)/x$TAINT/g;
568     $one = $1;
569     is_tainted($s,     "$desc: s tainted");
570     isnt_tainted($res, "$desc: res not tainted");
571     isnt_tainted($one, "$desc: \$1 not tainted");
572     is($s,  'xxxx',    "$desc: s value");
573     is($res, 4,        "$desc: res value");
574     is($one, 'd',      "$desc: \$1 value");
575
576     $desc = "substitution /ge with replacement tainted";
577
578     $s = 'abc';
579     {
580         my $i = 0;
581         my $j;
582         $res = $s =~ s{(.)}{
583                     $j = $i; # make sure code not tainted
584                     $one = $1;
585                     isnt_tainted($j, "$desc: code not tainted within /e");
586                     $i++;
587                     if ($i == 1) {
588                         isnt_tainted($s,   "$desc: s not tainted loop 1");
589                     }
590                     else {
591                         is_tainted($s,     "$desc: s tainted loop $i");
592                     }
593                     isnt_tainted($one, "$desc: \$1 not tainted within /e");
594                     $i.$TAINT;
595                 }ge;
596         $one = $1;
597     }
598     is_tainted($s,     "$desc: s tainted");
599     isnt_tainted($res, "$desc: res tainted");
600     isnt_tainted($one, "$desc: \$1 not tainted");
601     is($s,  '123',     "$desc: s value");
602     is($res, 3,        "$desc: res value");
603     is($one, 'c',      "$desc: \$1 value");
604
605     $desc = "substitution /r with replacement tainted";
606
607     $s = 'abcd';
608     $res = $s =~ s/(.+)/xyz$TAINT/r;
609     $one = $1;
610     isnt_tainted($s,   "$desc: s not tainted");
611     is_tainted($res,   "$desc: res tainted");
612     isnt_tainted($one, "$desc: \$1 not tainted");
613     is($s,   'abcd',   "$desc: s value");
614     is($res, 'xyz',    "$desc: res value");
615     is($one, 'abcd',   "$desc: \$1 value");
616
617     {
618         # now do them all again with "use re 'taint"
619
620         use re 'taint';
621
622         $desc = "use re 'taint': match with string tainted";
623
624         $s = 'abcd' . $TAINT;
625         $res = $s =~ /(.+)/;
626         $one = $1;
627         is_tainted($s,     "$desc: s tainted");
628         isnt_tainted($res, "$desc: res not tainted");
629         is_tainted($one,   "$desc: \$1 tainted");
630         is($res, 1,        "$desc: res value");
631         is($one, 'abcd',   "$desc: \$1 value");
632
633         $desc = "use re 'taint': match /g with string tainted";
634
635         $s = 'abcd' . $TAINT;
636         $res = $s =~ /(.)/g;
637         $one = $1;
638         is_tainted($s,     "$desc: s 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, 'a',      "$desc: \$1 value");
643
644         $desc = "use re 'taint': match with string tainted, list cxt";
645
646         $s = 'abcd' . $TAINT;
647         ($res) = $s =~ /(.+)/;
648         $one = $1;
649         is_tainted($s,     "$desc: s tainted");
650         is_tainted($res,   "$desc: res tainted");
651         is_tainted($one,   "$desc: \$1 tainted");
652         is($res, 'abcd',   "$desc: res value");
653         is($one, 'abcd',   "$desc: \$1 value");
654
655         $desc = "use re 'taint': match /g with string tainted, list cxt";
656
657         $s = 'abcd' . $TAINT;
658         ($res, $res2) = $s =~ /(.)/g;
659         $one = $1;
660         is_tainted($s,     "$desc: s tainted");
661         is_tainted($res,   "$desc: res tainted");
662         is_tainted($res2,  "$desc: res2 tainted");
663         is_tainted($one,   "$desc: \$1 not tainted");
664         is($res, 'a',      "$desc: res value");
665         is($res2,'b',      "$desc: res2 value");
666         is($one, 'd',      "$desc: \$1 value");
667
668         $desc = "use re 'taint': match with pattern tainted";
669
670         $s = 'abcd';
671         $res = $s =~ /$TAINT(.+)/;
672         $one = $1;
673         isnt_tainted($s,   "$desc: s not tainted");
674         isnt_tainted($res, "$desc: res not tainted");
675         is_tainted($one,   "$desc: \$1 tainted");
676         is($res, 1,        "$desc: res value");
677         is($one, 'abcd',   "$desc: \$1 value");
678
679         $desc = "use re 'taint': match /g with pattern tainted";
680
681         $s = 'abcd';
682         $res = $s =~ /$TAINT(.)/g;
683         $one = $1;
684         isnt_tainted($s,   "$desc: s not tainted");
685         isnt_tainted($res, "$desc: res not tainted");
686         is_tainted($one,   "$desc: \$1 tainted");
687         is($res, 1,        "$desc: res value");
688         is($one, 'a',      "$desc: \$1 value");
689
690   SKIP: {
691         skip 'Locales not available', 10 unless locales_enabled('LC_CTYPE');
692
693         $desc = "use re 'taint': match with pattern tainted via locale";
694
695         $s = 'abcd';
696         {
697             use locale;
698             $res = $s =~ /(\w+)/; $one = $1;
699         }
700         isnt_tainted($s,   "$desc: s not tainted");
701         isnt_tainted($res, "$desc: res not tainted");
702         is_tainted($one,   "$desc: \$1 tainted");
703         is($res, 1,        "$desc: res value");
704         is($one, 'abcd',   "$desc: \$1 value");
705
706         $desc = "use re 'taint': match /g with pattern tainted via locale";
707
708         $s = 'abcd';
709         {
710             use locale;
711             $res = $s =~ /(\w)/g; $one = $1;
712         }
713         isnt_tainted($s,   "$desc: s not tainted");
714         isnt_tainted($res, "$desc: res not tainted");
715         is_tainted($one,   "$desc: \$1 tainted");
716         is($res, 1,        "$desc: res value");
717         is($one, 'a',      "$desc: \$1 value");
718     }
719
720         $desc = "use re 'taint': match with pattern tainted, list cxt";
721
722         $s = 'abcd';
723         ($res) = $s =~ /$TAINT(.+)/;
724         $one = $1;
725         isnt_tainted($s,   "$desc: s not tainted");
726         is_tainted($res,   "$desc: res tainted");
727         is_tainted($one,   "$desc: \$1 tainted");
728         is($res, 'abcd',   "$desc: res value");
729         is($one, 'abcd',   "$desc: \$1 value");
730
731         $desc = "use re 'taint': match /g with pattern tainted, list cxt";
732
733         $s = 'abcd';
734         ($res, $res2) = $s =~ /$TAINT(.)/g;
735         $one = $1;
736         isnt_tainted($s,   "$desc: s not tainted");
737         is_tainted($res,   "$desc: res tainted");
738         is_tainted($one,   "$desc: \$1 tainted");
739         is($res, 'a',      "$desc: res value");
740         is($res2,'b',      "$desc: res2 value");
741         is($one, 'd',      "$desc: \$1 value");
742
743   SKIP: {
744         skip 'Locales not available', 12 unless locales_enabled('LC_CTYPE');
745
746         $desc = "use re 'taint': match with pattern tainted via locale, list cxt";
747
748         $s = 'abcd';
749         {
750             use locale;
751             ($res) = $s =~ /(\w+)/; $one = $1;
752         }
753         isnt_tainted($s,   "$desc: s not tainted");
754         is_tainted($res,   "$desc: res tainted");
755         is_tainted($one,   "$desc: \$1 tainted");
756         is($res, 'abcd',   "$desc: res value");
757         is($one, 'abcd',   "$desc: \$1 value");
758
759         $desc = "use re 'taint': match /g with pattern tainted via locale, list cxt";
760
761         $s = 'abcd';
762         {
763             use locale;
764             ($res, $res2) = $s =~ /(\w)/g; $one = $1;
765         }
766         isnt_tainted($s,   "$desc: s not tainted");
767         is_tainted($res,   "$desc: res tainted");
768         is_tainted($res2,  "$desc: res2 tainted");
769         is_tainted($one,   "$desc: \$1 tainted");
770         is($res, 'a',      "$desc: res value");
771         is($res2,'b',      "$desc: res2 value");
772         is($one, 'd',      "$desc: \$1 value");
773     }
774
775         $desc = "use re 'taint': substitution with string tainted";
776
777         $s = 'abcd' . $TAINT;
778         $res = $s =~ s/(.+)/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 string tainted";
788
789         $s = 'abcd' . $TAINT;
790         $res = $s =~ s/(.)/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 /r with string tainted";
800
801         $s = 'abcd' . $TAINT;
802         $res = $s =~ s/(.+)/xyz/r;
803         $one = $1;
804         is_tainted($s,     "$desc: s tainted");
805         is_tainted($res,   "$desc: res tainted");
806         is_tainted($one,   "$desc: \$1 tainted");
807         is($s,   'abcd',   "$desc: s value");
808         is($res, 'xyz',    "$desc: res value");
809         is($one, 'abcd',   "$desc: \$1 value");
810
811         $desc = "use re 'taint': substitution /e with string tainted";
812
813         $s = 'abcd' . $TAINT;
814         $one = '';
815         $res = $s =~ s{(.+)}{
816                     $one = $one . "x"; # make sure code not tainted
817                     isnt_tainted($one, "$desc: code not tainted within /e");
818                     $one = $1;
819                     is_tainted($one, "$desc: $1 tainted within /e");
820                     "xyz";
821                 }e;
822         $one = $1;
823         is_tainted($s,     "$desc: s tainted");
824         isnt_tainted($res, "$desc: res not tainted");
825         is_tainted($one,   "$desc: \$1 tainted");
826         is($s,   'xyz',    "$desc: s value");
827         is($res, 1,        "$desc: res value");
828         is($one, 'abcd',   "$desc: \$1 value");
829
830         $desc = "use re 'taint': substitution with pattern tainted";
831
832         $s = 'abcd';
833         $res = $s =~ s/$TAINT(.+)/xyz/;
834         $one = $1;
835         is_tainted($s,     "$desc: s tainted");
836         isnt_tainted($res, "$desc: res not tainted");
837         is_tainted($one,   "$desc: \$1 tainted");
838         is($s,  'xyz',     "$desc: s value");
839         is($res, 1,        "$desc: res value");
840         is($one, 'abcd',   "$desc: \$1 value");
841
842         $desc = "use re 'taint': substitution /g with pattern tainted";
843
844         $s = 'abcd';
845         $res = $s =~ s/$TAINT(.)/x/g;
846         $one = $1;
847         is_tainted($s,     "$desc: s tainted");
848         is_tainted($res,   "$desc: res tainted");
849         is_tainted($one,   "$desc: \$1 tainted");
850         is($s,  'xxxx',    "$desc: s value");
851         is($res, 4,        "$desc: res value");
852         is($one, 'd',      "$desc: \$1 value");
853
854         $desc = "use re 'taint': substitution /ge with pattern tainted";
855
856         $s = 'abc';
857         {
858             my $i = 0;
859             my $j;
860             $res = $s =~ s{(.)$TAINT}{
861                         $j = $i; # make sure code not tainted
862                         $one = $1;
863                         isnt_tainted($j, "$desc: code not tainted within /e");
864                         $i++;
865                         if ($i == 1) {
866                             isnt_tainted($s,   "$desc: s not tainted loop 1");
867                         }
868                         else {
869                             is_tainted($s,     "$desc: s tainted loop $i");
870                         }
871                         is_tainted($one,   "$desc: \$1 tainted loop $i");
872                         $i.$TAINT;
873                     }ge;
874             $one = $1;
875         }
876         is_tainted($s,     "$desc: s tainted");
877         is_tainted($res,   "$desc: res tainted");
878         is_tainted($one,   "$desc: \$1 tainted");
879         is($s,  '123',     "$desc: s value");
880         is($res, 3,        "$desc: res value");
881         is($one, 'c',      "$desc: \$1 value");
882
883
884         $desc = "use re 'taint': substitution /r with pattern tainted";
885
886         $s = 'abcd';
887         $res = $s =~ s/$TAINT(.+)/xyz/r;
888         $one = $1;
889         isnt_tainted($s,   "$desc: s not tainted");
890         is_tainted($res,   "$desc: res tainted");
891         is_tainted($one,   "$desc: \$1 tainted");
892         is($s,  'abcd',    "$desc: s value");
893         is($res, 'xyz',    "$desc: res value");
894         is($one, 'abcd',   "$desc: \$1 value");
895
896   SKIP: {
897         skip 'Locales not available', 18 unless locales_enabled('LC_CTYPE');
898
899         $desc = "use re 'taint': substitution with pattern tainted via locale";
900
901         $s = 'abcd';
902         {
903             use locale;
904             $res = $s =~ s/(\w+)/xyz/; $one = $1;
905         }
906         is_tainted($s,     "$desc: s tainted");
907         isnt_tainted($res, "$desc: res not tainted");
908         is_tainted($one,   "$desc: \$1 tainted");
909         is($s,  'xyz',     "$desc: s value");
910         is($res, 1,        "$desc: res value");
911         is($one, 'abcd',   "$desc: \$1 value");
912
913         $desc = "use re 'taint': substitution /g with pattern tainted via locale";
914
915         $s = 'abcd';
916         {
917             use locale;
918             $res = $s =~ s/(\w)/x/g; $one = $1;
919         }
920         is_tainted($s,     "$desc: s tainted");
921         is_tainted($res,   "$desc: res tainted");
922         is_tainted($one,   "$desc: \$1 tainted");
923         is($s,  'xxxx',    "$desc: s value");
924         is($res, 4,        "$desc: res value");
925         is($one, 'd',      "$desc: \$1 value");
926
927         $desc = "use re 'taint': substitution /r with pattern tainted via locale";
928
929         $s = 'abcd';
930         {
931             use locale;
932             $res = $s =~ s/(\w+)/xyz/r; $one = $1;
933         }
934         isnt_tainted($s,   "$desc: s not tainted");
935         is_tainted($res,   "$desc: res tainted");
936         is_tainted($one,   "$desc: \$1 tainted");
937         is($s,  'abcd',    "$desc: s value");
938         is($res, 'xyz',    "$desc: res value");
939         is($one, 'abcd',   "$desc: \$1 value");
940     }
941
942         $desc = "use re 'taint': substitution with replacement tainted";
943
944         $s = 'abcd';
945         $res = $s =~ s/(.+)/xyz$TAINT/;
946         $one = $1;
947         is_tainted($s,     "$desc: s tainted");
948         isnt_tainted($res, "$desc: res not tainted");
949         isnt_tainted($one, "$desc: \$1 not tainted");
950         is($s,  'xyz',     "$desc: s value");
951         is($res, 1,        "$desc: res value");
952         is($one, 'abcd',   "$desc: \$1 value");
953
954         $desc = "use re 'taint': substitution /g with replacement tainted";
955
956         $s = 'abcd';
957         $res = $s =~ s/(.)/x$TAINT/g;
958         $one = $1;
959         is_tainted($s,     "$desc: s tainted");
960         isnt_tainted($res, "$desc: res not tainted");
961         isnt_tainted($one, "$desc: \$1 not tainted");
962         is($s,  'xxxx',    "$desc: s value");
963         is($res, 4,        "$desc: res value");
964         is($one, 'd',      "$desc: \$1 value");
965
966         $desc = "use re 'taint': substitution /ge with replacement tainted";
967
968         $s = 'abc';
969         {
970             my $i = 0;
971             my $j;
972             $res = $s =~ s{(.)}{
973                         $j = $i; # make sure code not tainted
974                         $one = $1;
975                         isnt_tainted($j, "$desc: code not tainted within /e");
976                         $i++;
977                         if ($i == 1) {
978                             isnt_tainted($s,   "$desc: s not tainted loop 1");
979                         }
980                         else {
981                             is_tainted($s,     "$desc: s tainted loop $i");
982                         }
983                             isnt_tainted($one, "$desc: \$1 not tainted");
984                         $i.$TAINT;
985                     }ge;
986             $one = $1;
987         }
988         is_tainted($s,     "$desc: s tainted");
989         isnt_tainted($res, "$desc: res tainted");
990         isnt_tainted($one, "$desc: \$1 not tainted");
991         is($s,  '123',     "$desc: s value");
992         is($res, 3,        "$desc: res value");
993         is($one, 'c',      "$desc: \$1 value");
994
995         $desc = "use re 'taint': substitution /r with replacement tainted";
996
997         $s = 'abcd';
998         $res = $s =~ s/(.+)/xyz$TAINT/r;
999         $one = $1;
1000         isnt_tainted($s,   "$desc: s not tainted");
1001         is_tainted($res,   "$desc: res tainted");
1002         isnt_tainted($one, "$desc: \$1 not tainted");
1003         is($s,   'abcd',   "$desc: s value");
1004         is($res, 'xyz',    "$desc: res value");
1005         is($one, 'abcd',   "$desc: \$1 value");
1006
1007         # [perl #121854] match taintedness became sticky
1008         # when one match has a taintess result, subseqent matches
1009         # using the same pattern shouldn't necessarily be tainted
1010
1011         {
1012             my $f = sub { $_[0] =~ /(.*)/ or die; $1 };
1013             $res = $f->($TAINT);
1014             is_tainted($res,   "121854: res tainted");
1015             $res = $f->("abc");
1016             isnt_tainted($res,   "121854: res not tainted");
1017         }
1018     }
1019
1020     $foo = $1 if 'bar' =~ /(.+)$TAINT/;
1021     is_tainted($foo);
1022     is($foo, 'bar');
1023
1024     my $pi = 4 * atan2(1,1) + $TAINT0;
1025     is_tainted($pi);
1026
1027     ($pi) = $pi =~ /(\d+\.\d+)/;
1028     isnt_tainted($pi);
1029     is(sprintf("%.5f", $pi), '3.14159');
1030 }
1031
1032 # How about command-line arguments? The problem is that we don't
1033 # always get some, so we'll run another process with some.
1034 SKIP: {
1035     my $arg = tempfile();
1036     open $fh, '>', $arg or die "Can't create $arg: $!";
1037     print $fh q{
1038         eval { join('', @ARGV), kill 0 };
1039         exit 0 if $@ =~ /^Insecure dependency/;
1040         print "# Oops: \$@ was [$@]\n";
1041         exit 1;
1042     };
1043     close $fh or die "Can't close $arg: $!";
1044     print `$Invoke_Perl "-T" $arg and some suspect arguments`;
1045     is($?, 0, "Exited with status $?");
1046     unlink $arg;
1047 }
1048
1049 # Reading from a file should be tainted
1050 {
1051     ok(open my $fh, '<', $TEST) or diag("Couldn't open '$TEST': $!");
1052
1053     my $block;
1054     sysread($fh, $block, 100);
1055     my $line = <$fh>;
1056     close $fh;
1057     is_tainted($block);
1058     is_tainted($line);
1059 }
1060
1061 # Output of commands should be tainted
1062 {
1063     my $foo = `$echo abc`;
1064     is_tainted($foo);
1065 }
1066
1067 # Certain system variables should be tainted
1068 {
1069     is_tainted($^X);
1070     is_tainted($0);
1071 }
1072
1073 # Results of matching should all be untainted
1074 {
1075     my $foo = "abcdefghi" . $TAINT;
1076     is_tainted($foo);
1077
1078     $foo =~ /def/;
1079     isnt_tainted($`);
1080     isnt_tainted($&);
1081     isnt_tainted($');
1082
1083     $foo =~ /(...)(...)(...)/;
1084     isnt_tainted($1);
1085     isnt_tainted($2);
1086     isnt_tainted($3);
1087     isnt_tainted($+);
1088
1089     my @bar = $foo =~ /(...)(...)(...)/;
1090     isnt_tainted($_) foreach @bar;
1091
1092     is_tainted($foo);   # $foo should still be tainted!
1093     is($foo, "abcdefghi");
1094 }
1095
1096 # Operations which affect files can't use tainted data.
1097 {
1098     violates_taint(sub { chmod 0, $TAINT }, 'chmod');
1099
1100     SKIP: {
1101         skip "truncate() is not available", 2 unless $Config{d_truncate};
1102
1103         violates_taint(sub { truncate 'NoSuChFiLe', $TAINT0 }, 'truncate');
1104     }
1105
1106     violates_taint(sub { rename '', $TAINT }, 'rename');
1107     violates_taint(sub { unlink $TAINT }, 'unlink');
1108     violates_taint(sub { utime $TAINT }, 'utime');
1109
1110     SKIP: {
1111         skip "chown() is not available", 2 unless $Config{d_chown};
1112
1113         violates_taint(sub { chown -1, -1, $TAINT }, 'chown');
1114     }
1115
1116     SKIP: {
1117         skip "link() is not available", 2 unless $Config{d_link};
1118
1119 violates_taint(sub { link $TAINT, '' }, 'link');
1120     }
1121
1122     SKIP: {
1123         skip "symlink() is not available", 2 unless $Config{d_symlink};
1124
1125         violates_taint(sub { symlink $TAINT, '' }, 'symlink');
1126     }
1127 }
1128
1129 # Operations which affect directories can't use tainted data.
1130 {
1131     violates_taint(sub { mkdir "foo".$TAINT, 0755 . $TAINT0 }, 'mkdir');
1132     violates_taint(sub { rmdir $TAINT }, 'rmdir');
1133     violates_taint(sub { chdir "foo".$TAINT }, 'chdir');
1134
1135     SKIP: {
1136         skip "chroot() is not available", 2 unless $Config{d_chroot};
1137
1138         violates_taint(sub { chroot $TAINT }, 'chroot');
1139     }
1140 }
1141
1142 # Some operations using files can't use tainted data.
1143 {
1144     my $foo = "imaginary library" . $TAINT;
1145     violates_taint(sub { require $foo }, 'require');
1146
1147     my $filename = tempfile();  # NB: $filename isn't tainted!
1148     $foo = $filename . $TAINT;
1149     unlink $filename;   # in any case
1150
1151     is(eval { open FOO, $foo }, undef, 'open for read');
1152     is($@, '');                # NB: This should be allowed
1153     is(eval { open my $fh, , '<', $foo }, undef, 'open for read');
1154     is($@, '');                # NB: This should be allowed
1155
1156     # Try first new style but allow also old style.
1157     # We do not want the whole taint.t to fail
1158     # just because Errno possibly failing.
1159     ok(eval('$!{ENOENT}') ||
1160         $! == 2 || # File not found
1161         ($Is_Dos && $! == 22));
1162
1163     violates_taint(sub { open FOO, "> $foo" }, 'open', 'open for write');
1164     violates_taint(sub { open my $fh, '>', $foo }, 'open', 'open for write');
1165 }
1166
1167 # Commands to the system can't use tainted data
1168 {
1169     my $foo = $TAINT;
1170
1171     SKIP: {
1172         skip "open('|') is not available", 8 if $^O eq 'amigaos';
1173
1174         violates_taint(sub { open FOO, "| x$foo" }, 'piped open', 'popen to');
1175         violates_taint(sub { open FOO, "x$foo |" }, 'piped open', 'popen from');
1176         violates_taint(sub { open my $fh, '|-', "x$foo" }, 'piped open', 'popen to');
1177         violates_taint(sub { open my $fh, '-|', "x$foo" }, 'piped open', 'popen from');
1178     }
1179
1180     violates_taint(sub { exec $TAINT }, 'exec');
1181     violates_taint(sub { system $TAINT }, 'system');
1182
1183     $foo = "*";
1184     taint_these $foo;
1185
1186     violates_taint(sub { `$echo 1$foo` }, '``', 'backticks');
1187
1188     SKIP: {
1189         # wildcard expansion doesn't invoke shell on VMS, so is safe
1190         skip "This is not VMS", 2 unless $Is_VMS;
1191     
1192         isnt(join('', eval { glob $foo } ), '', 'globbing');
1193         is($@, '');
1194     }
1195 }
1196
1197 # Operations which affect processes can't use tainted data.
1198 {
1199     violates_taint(sub { kill 0, $TAINT }, 'kill');
1200
1201     SKIP: {
1202         skip "setpgrp() is not available", 2 unless $Config{d_setpgrp};
1203
1204         violates_taint(sub { setpgrp 0, $TAINT0 }, 'setpgrp');
1205     }
1206
1207     SKIP: {
1208         skip "setpriority() is not available", 2 unless $Config{d_setprior};
1209
1210         violates_taint(sub { setpriority 0, $TAINT0, $TAINT0 }, 'setpriority');
1211     }
1212 }
1213
1214 # Some miscellaneous operations can't use tainted data.
1215 {
1216     SKIP: {
1217         skip "syscall() is not available", 2 unless $Config{d_syscall};
1218
1219         violates_taint(sub { syscall $TAINT }, 'syscall');
1220     }
1221
1222     {
1223         my $foo = "x" x 979;
1224         taint_these $foo;
1225         local *FOO;
1226         my $temp = tempfile();
1227         ok(open FOO, "> $temp") or diag("Couldn't open $temp for write: $!");
1228         violates_taint(sub { ioctl FOO, $TAINT0, $foo }, 'ioctl');
1229
1230         my $temp2 = tempfile();
1231         ok(open my $fh, '>', $temp2) or diag("Couldn't open $temp2 for write: $!");
1232         violates_taint(sub { ioctl $fh, $TAINT0, $foo }, 'ioctl');
1233
1234         SKIP: {
1235             skip "fcntl() is not available", 4 unless $Config{d_fcntl};
1236
1237             violates_taint(sub { fcntl FOO, $TAINT0, $foo }, 'fcntl');
1238             violates_taint(sub { fcntl $fh, $TAINT0, $foo }, 'fcntl');
1239         }
1240
1241         close FOO;
1242     }
1243 }
1244
1245 # Some tests involving references
1246 {
1247     my $foo = 'abc' . $TAINT;
1248     my $fooref = \$foo;
1249     isnt_tainted($fooref);
1250     is_tainted($$fooref);
1251     is_tainted($foo);
1252 }
1253
1254 # Some tests involving assignment
1255 {
1256     my $foo = $TAINT0;
1257     my $bar = $foo;
1258     is_tainted($foo);
1259     is_tainted($bar);
1260     is_tainted($foo = $bar);
1261     is_tainted($bar = $bar);
1262     is_tainted($bar += $bar);
1263     is_tainted($bar -= $bar);
1264     is_tainted($bar *= $bar);
1265     is_tainted($bar++);
1266     is_tainted($bar /= $bar);
1267     is_tainted($bar += 0);
1268     is_tainted($bar -= 2);
1269     is_tainted($bar *= -1);
1270     is_tainted($bar /= 1);
1271     is_tainted($bar--);
1272     is($bar, 0);
1273 }
1274
1275 # Test assignment and return of lists
1276 {
1277     my @foo = ("A", "tainted" . $TAINT, "B");
1278     isnt_tainted($foo[0]);
1279     is_tainted(    $foo[1]);
1280     isnt_tainted($foo[2]);
1281     my @bar = @foo;
1282     isnt_tainted($bar[0]);
1283     is_tainted(    $bar[1]);
1284     isnt_tainted($bar[2]);
1285     my @baz = eval { "A", "tainted" . $TAINT, "B" };
1286     isnt_tainted($baz[0]);
1287     is_tainted(    $baz[1]);
1288     isnt_tainted($baz[2]);
1289     my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
1290     isnt_tainted($plugh[0]);
1291     is_tainted(    $plugh[1]);
1292     isnt_tainted($plugh[2]);
1293     my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
1294     isnt_tainted(((&$nautilus)[0]));
1295     is_tainted(    ((&$nautilus)[1]));
1296     isnt_tainted(((&$nautilus)[2]));
1297     my @xyzzy = &$nautilus;
1298     isnt_tainted($xyzzy[0]);
1299     is_tainted(    $xyzzy[1]);
1300     isnt_tainted($xyzzy[2]);
1301     my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
1302     isnt_tainted(((&$red_october)[0]));
1303     is_tainted(    ((&$red_october)[1]));
1304     isnt_tainted(((&$red_october)[2]));
1305     my @corge = &$red_october;
1306     isnt_tainted($corge[0]);
1307     is_tainted(    $corge[1]);
1308     isnt_tainted($corge[2]);
1309 }
1310
1311 # Test for system/library calls returning string data of dubious origin.
1312 {
1313     # No reliable %Config check for getpw*
1314     SKIP: {
1315         skip "getpwent() is not available", 9 unless 
1316           eval { setpwent(); getpwent() };
1317
1318         setpwent();
1319         my @getpwent = getpwent();
1320         die "getpwent: $!\n" unless (@getpwent);
1321         isnt_tainted($getpwent[0]);
1322         is_tainted($getpwent[1]);
1323         isnt_tainted($getpwent[2]);
1324         isnt_tainted($getpwent[3]);
1325         isnt_tainted($getpwent[4]);
1326         isnt_tainted($getpwent[5]);
1327         is_tainted($getpwent[6], 'ge?cos');
1328         isnt_tainted($getpwent[7]);
1329         is_tainted($getpwent[8], 'shell');
1330         endpwent();
1331     }
1332
1333     SKIP: {
1334         # pretty hard to imagine not
1335         skip "readdir() is not available", 1 unless $Config{d_readdir};
1336
1337         opendir my $dh, "op" or die "opendir: $!\n";
1338         my $readdir = readdir $dh;
1339         is_tainted($readdir);
1340         closedir $dh;
1341     }
1342
1343     SKIP: {
1344         skip "readlink() or symlink() is not available" unless 
1345           $Config{d_readlink} && $Config{d_symlink};
1346
1347         my $symlink = "sl$$";
1348         unlink($symlink);
1349         my $sl = "/something/naughty";
1350         # it has to be a real path on Mac OS
1351         symlink($sl, $symlink) or die "symlink: $!\n";
1352         my $readlink = readlink($symlink);
1353         is_tainted($readlink);
1354         unlink($symlink);
1355     }
1356 }
1357
1358 # test bitwise ops (regression bug)
1359 {
1360     my $why = "y";
1361     my $j = "x" | $why;
1362     isnt_tainted($j);
1363     $why = $TAINT."y";
1364     $j = "x" | $why;
1365     is_tainted(    $j);
1366 }
1367
1368 # test target of substitution (regression bug)
1369 {
1370     my $why = $TAINT."y";
1371     $why =~ s/y/z/;
1372     is_tainted(    $why);
1373
1374     my $z = "[z]";
1375     $why =~ s/$z/zee/;
1376     is_tainted(    $why);
1377
1378     $why =~ s/e/'-'.$$/ge;
1379     is_tainted(    $why);
1380 }
1381
1382
1383 SKIP: {
1384     skip "no IPC::SysV", 2 unless $ipcsysv;
1385
1386     # test shmread
1387     SKIP: {
1388         skip "shm*() not available", 1 unless $Config{d_shm};
1389
1390         no strict 'subs';
1391         my $sent = "foobar";
1392         my $rcvd;
1393         my $size = 2000;
1394         my $id;
1395         eval {
1396             local $SIG{SYS} = sub { die "SIGSYS caught\n" };
1397             $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
1398             1;
1399         } or do { chomp(my $msg = $@); skip "shmget: $msg", 1; };
1400
1401         if (defined $id) {
1402             if (shmwrite($id, $sent, 0, 60)) {
1403                 if (shmread($id, $rcvd, 0, 60)) {
1404                     substr($rcvd, index($rcvd, "\0")) = '';
1405                 } else {
1406                     warn "# shmread failed: $!\n";
1407                 }
1408             } else {
1409                 warn "# shmwrite failed: $!\n";
1410             }
1411             shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
1412         } else {
1413             warn "# shmget failed: $!\n";
1414         }
1415
1416         skip "SysV shared memory operation failed", 1 unless 
1417           $rcvd eq $sent;
1418
1419         is_tainted($rcvd, "shmread");
1420     }
1421
1422
1423     # test msgrcv
1424     SKIP: {
1425         skip "msg*() not available", 1 unless $Config{d_msg};
1426
1427         no strict 'subs';
1428         my $id;
1429         eval {
1430             local $SIG{SYS} = sub { die "SIGSYS caught\n" };
1431             $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
1432             1;
1433         } or do { chomp(my $msg = $@); skip "msgget: $msg", 1; };
1434
1435         my $sent      = "message";
1436         my $type_sent = 1234;
1437         my $rcvd;
1438         my $type_rcvd;
1439
1440         if (defined $id) {
1441             if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) {
1442                 if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) {
1443                     ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
1444                 } else {
1445                     warn "# msgrcv failed: $!\n";
1446                 }
1447             } else {
1448                 warn "# msgsnd failed: $!\n";
1449             }
1450             msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
1451         } else {
1452             warn "# msgget failed\n";
1453         }
1454
1455         SKIP: {
1456             skip "SysV message queue operation failed", 1
1457               unless $rcvd eq $sent && $type_sent == $type_rcvd;
1458
1459             is_tainted($rcvd, "msgrcv");
1460         }
1461     }
1462 }
1463
1464 {
1465     # bug id 20001004.006
1466
1467     open my $fh, '<', $TEST or warn "$0: cannot read $TEST: $!" ;
1468     local $/;
1469     my $a = <$fh>;
1470     my $b = <$fh>;
1471
1472     is_tainted($a);
1473     is_tainted($b);
1474     is($b, undef);
1475 }
1476
1477 {
1478     # bug id 20001004.007
1479
1480     open my $fh, '<', $TEST or warn "$0: cannot read $TEST: $!" ;
1481     my $a = <$fh>;
1482
1483     my $c = { a => 42,
1484               b => $a };
1485
1486     isnt_tainted($c->{a});
1487     is_tainted($c->{b});
1488
1489
1490     my $d = { a => $a,
1491               b => 42 };
1492     is_tainted($d->{a});
1493     isnt_tainted($d->{b});
1494
1495
1496     my $e = { a => 42,
1497               b => { c => $a, d => 42 } };
1498     isnt_tainted($e->{a});
1499     isnt_tainted($e->{b});
1500     is_tainted($e->{b}->{c});
1501     isnt_tainted($e->{b}->{d});
1502 }
1503
1504 {
1505     # bug id 20010519.003
1506
1507     BEGIN {
1508         use vars qw($has_fcntl);
1509         eval { require Fcntl; import Fcntl; };
1510         unless ($@) {
1511             $has_fcntl = 1;
1512         }
1513     }
1514
1515     SKIP: {
1516         skip "no Fcntl", 36 unless $has_fcntl;
1517
1518         my $foo = tempfile();
1519         my $evil = $foo . $TAINT;
1520
1521         is(eval { sysopen(my $ro, $evil, &O_RDONLY) }, undef);
1522         is($@, '');
1523
1524         violates_taint(sub { sysopen(my $wo, $evil, &O_WRONLY) }, 'sysopen');
1525         violates_taint(sub { sysopen(my $rw, $evil, &O_RDWR) }, 'sysopen');
1526         violates_taint(sub { sysopen(my $ap, $evil, &O_APPEND) }, 'sysopen');
1527         violates_taint(sub { sysopen(my $cr, $evil, &O_CREAT) }, 'sysopen');
1528         violates_taint(sub { sysopen(my $tr, $evil, &O_TRUNC) }, 'sysopen');
1529
1530         is(eval { sysopen(my $ro, $foo, &O_RDONLY | $TAINT0) }, undef);
1531         is($@, '');
1532
1533         violates_taint(sub { sysopen(my $wo, $foo, &O_WRONLY | $TAINT0) }, 'sysopen');
1534         violates_taint(sub { sysopen(my $rw, $foo, &O_RDWR | $TAINT0) }, 'sysopen');
1535         violates_taint(sub { sysopen(my $ap, $foo, &O_APPEND | $TAINT0) }, 'sysopen');
1536         violates_taint(sub { sysopen(my $cr, $foo, &O_CREAT | $TAINT0) }, 'sysopen');
1537         violates_taint(sub { sysopen(my $tr, $foo, &O_TRUNC | $TAINT0) }, 'sysopen');
1538         is(eval { sysopen(my $ro, $foo, &O_RDONLY, $TAINT0) }, undef);
1539         is($@, '');
1540
1541         violates_taint(sub { sysopen(my $wo, $foo, &O_WRONLY, $TAINT0) }, 'sysopen');
1542         violates_taint(sub { sysopen(my $rw, $foo, &O_RDWR, $TAINT0) }, 'sysopen');
1543         violates_taint(sub { sysopen(my $ap, $foo, &O_APPEND, $TAINT0) }, 'sysopen');
1544         violates_taint(sub { sysopen(my $cr, $foo, &O_CREAT, $TAINT0) }, 'sysopen');
1545         violates_taint(sub { sysopen(my $tr, $foo, &O_TRUNC, $TAINT0) }, 'sysopen');
1546     }
1547 }
1548
1549 {
1550     # bug 20010526.004
1551
1552     use warnings;
1553
1554     my $saw_warning = 0;
1555     local $SIG{__WARN__} = sub { ++$saw_warning };
1556
1557     sub fmi {
1558         my $divnum = shift()/1;
1559         sprintf("%1.1f\n", $divnum);
1560     }
1561
1562     fmi(21 . $TAINT);
1563     fmi(37);
1564     fmi(248);
1565
1566     is($saw_warning, 0);
1567 }
1568
1569
1570 {
1571     # Bug ID 20010730.010
1572
1573     my $i = 0;
1574
1575     sub Tie::TIESCALAR {
1576         my $class =  shift;
1577         my $arg   =  shift;
1578
1579         bless \$arg => $class;
1580     }
1581
1582     sub Tie::FETCH {
1583         $i ++;
1584         ${$_ [0]}
1585     }
1586
1587  
1588     package main;
1589  
1590     my $bar = "The Big Bright Green Pleasure Machine";
1591     taint_these $bar;
1592     tie my ($foo), Tie => $bar;
1593
1594     my $baz = $foo;
1595
1596     ok $i == 1;
1597 }
1598
1599 {
1600     # Check that all environment variables are tainted.
1601     my @untainted;
1602     while (my ($k, $v) = each %ENV) {
1603         if (!tainted($v) &&
1604             # These we have explicitly untainted or set earlier.
1605             $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) {
1606             push @untainted, "# '$k' = '$v'\n";
1607         }
1608     }
1609     is("@untainted", "");
1610 }
1611
1612
1613 is(${^TAINT}, 1, '$^TAINT is on');
1614
1615 eval { ${^TAINT} = 0 };
1616 is(${^TAINT}, 1, '$^TAINT is not assignable');
1617 like($@, qr/^Modification of a read-only value attempted/,
1618      'Assigning to ${^TAINT} fails');
1619
1620 {
1621     # bug 20011111.105
1622     
1623     my $re1 = qr/x$TAINT/;
1624     is_tainted($re1);
1625     
1626     my $re2 = qr/^$re1\z/;
1627     is_tainted($re2);
1628     
1629     my $re3 = "$re2";
1630     is_tainted($re3);
1631 }
1632
1633 SKIP: {
1634     skip "system {} has different semantics on Win32", 1 if $Is_MSWin32;
1635
1636     # bug 20010221.005
1637     local $ENV{PATH} .= $TAINT;
1638     eval { system { "echo" } "/arg0", "arg1" };
1639     like($@, qr/^Insecure \$ENV/);
1640 }
1641
1642 TODO: {
1643     todo_skip 'tainted %ENV warning occludes tainted arguments warning', 22
1644       if $Is_VMS;
1645
1646     # bug 20020208.005 plus some single arg exec/system extras
1647     violates_taint(sub { exec $TAINT, $TAINT }, 'exec');
1648     violates_taint(sub { exec $TAINT $TAINT }, 'exec');
1649     violates_taint(sub { exec $TAINT $TAINT, $TAINT }, 'exec');
1650     violates_taint(sub { exec $TAINT 'notaint' }, 'exec');
1651     violates_taint(sub { exec {'notaint'} $TAINT }, 'exec');
1652
1653     violates_taint(sub { system $TAINT, $TAINT }, 'system');
1654     violates_taint(sub { system $TAINT $TAINT }, 'system');
1655     violates_taint(sub { system $TAINT $TAINT, $TAINT }, 'system');
1656     violates_taint(sub { system $TAINT 'notaint' }, 'system');
1657     violates_taint(sub { system {'notaint'} $TAINT }, 'system');
1658
1659     eval { 
1660         no warnings;
1661         system("lskdfj does not exist","with","args"); 
1662     };
1663     is($@, "");
1664
1665     eval {
1666         no warnings;
1667         exec("lskdfj does not exist","with","args"); 
1668     };
1669     is($@, "");
1670
1671     # If you add tests here update also the above skip block for VMS.
1672 }
1673
1674 {
1675     # [ID 20020704.001] taint propagation failure
1676     use re 'taint';
1677     $TAINT =~ /(.*)/;
1678     is_tainted(my $foo = $1);
1679 }
1680
1681 {
1682     # [perl #24291] this used to dump core
1683     our %nonmagicalenv = ( PATH => "util" );
1684     local *ENV = \%nonmagicalenv;
1685     eval { system("lskdfj"); };
1686     like($@, qr/^%ENV is aliased to another variable while running with -T switch/);
1687     local *ENV = *nonmagicalenv;
1688     eval { system("lskdfj"); };
1689     like($@, qr/^%ENV is aliased to %nonmagicalenv while running with -T switch/);
1690 }
1691 {
1692     # [perl #24248]
1693     $TAINT =~ /(.*)/;
1694     isnt_tainted($1);
1695     my $notaint = $1;
1696     isnt_tainted($notaint);
1697
1698     my $l;
1699     $notaint =~ /($notaint)/;
1700     $l = $1;
1701     isnt_tainted($1);
1702     isnt_tainted($l);
1703     $notaint =~ /($TAINT)/;
1704     $l = $1;
1705     is_tainted($1);
1706     is_tainted($l);
1707
1708     $TAINT =~ /($notaint)/;
1709     $l = $1;
1710     isnt_tainted($1);
1711     isnt_tainted($l);
1712     $TAINT =~ /($TAINT)/;
1713     $l = $1;
1714     is_tainted($1);
1715     is_tainted($l);
1716
1717     my $r;
1718     ($r = $TAINT) =~ /($notaint)/;
1719     isnt_tainted($1);
1720     ($r = $TAINT) =~ /($TAINT)/;
1721     is_tainted($1);
1722
1723     {
1724         use re 'eval'; # this shouldn't make any difference
1725         ($r = $TAINT) =~ /($notaint)/;
1726         isnt_tainted($1);
1727         ($r = $TAINT) =~ /($TAINT)/;
1728         is_tainted($1);
1729     }
1730
1731     #  [perl #24674]
1732     # accessing $^O  shoudn't taint it as a side-effect;
1733     # assigning tainted data to it is now an error
1734
1735     isnt_tainted($^O);
1736     if (!$^X) { } elsif ($^O eq 'bar') { }
1737     isnt_tainted($^O);
1738     local $^O;  # We're going to clobber something test infrastructure depends on.
1739     eval '$^O = $^X';
1740     like($@, qr/Insecure dependency in/);
1741 }
1742
1743 EFFECTIVELY_CONSTANTS: {
1744     my $tainted_number = 12 + $TAINT0;
1745     is_tainted( $tainted_number );
1746
1747     # Even though it's always 0, it's still tainted
1748     my $tainted_product = $tainted_number * 0;
1749     is_tainted( $tainted_product );
1750     is($tainted_product, 0);
1751 }
1752
1753 TERNARY_CONDITIONALS: {
1754     my $tainted_true  = $TAINT . "blah blah blah";
1755     my $tainted_false = $TAINT0;
1756     is_tainted( $tainted_true );
1757     is_tainted( $tainted_false );
1758
1759     my $result = $tainted_true ? "True" : "False";
1760     is($result, "True");
1761     isnt_tainted( $result );
1762
1763     $result = $tainted_false ? "True" : "False";
1764     is($result, "False");
1765     isnt_tainted( $result );
1766
1767     my $untainted_whatever = "The Fabulous Johnny Cash";
1768     my $tainted_whatever = "Soft Cell" . $TAINT;
1769
1770     $result = $tainted_true ? $tainted_whatever : $untainted_whatever;
1771     is($result, "Soft Cell");
1772     is_tainted( $result );
1773
1774     $result = $tainted_false ? $tainted_whatever : $untainted_whatever;
1775     is($result, "The Fabulous Johnny Cash");
1776     isnt_tainted( $result );
1777 }
1778
1779 {
1780     # rt.perl.org 5900  $1 remains tainted if...
1781     # 1) The regular expression contains a scalar variable AND
1782     # 2) The regular expression appears in an elsif clause
1783
1784     my $foo = "abcdefghi" . $TAINT;
1785
1786     my $valid_chars = 'a-z';
1787     if ( $foo eq '' ) {
1788     }
1789     elsif ( $foo =~ /([$valid_chars]+)/o ) {
1790         isnt_tainted($1);
1791         isnt($1, undef);
1792     }
1793
1794     if ( $foo eq '' ) {
1795     }
1796     elsif ( my @bar = $foo =~ /([$valid_chars]+)/o ) {
1797         isnt_tainted($bar[0]);
1798         is(scalar @bar, 1);
1799     }
1800 }
1801
1802 # at scope exit, a restored localised value should have its old
1803 # taint status, not the taint status of the current statement
1804
1805 {
1806     our $x99 = $^X;
1807     is_tainted($x99);
1808
1809     $x99 = '';
1810     isnt_tainted($x99);
1811
1812     my $c = do { local $x99; $^X };
1813     isnt_tainted($x99);
1814 }
1815 {
1816     our $x99 = $^X;
1817     is_tainted($x99);
1818
1819     my $c = do { local $x99; '' };
1820     is_tainted($x99);
1821 }
1822
1823 # an mg_get of a tainted value during localization shouldn't taint the
1824 # statement
1825
1826 {
1827     eval { local $0, eval '1' };
1828     is($@, '');
1829 }
1830
1831 # [perl #8262] //g loops infinitely on tainted data
1832
1833 {
1834     my @a;
1835     $a[0] = $^X . '-';
1836     $a[0]=~ m/(.)/g;
1837     cmp_ok pos($a[0]), '>', 0, "infinite m//g on arrays (aelemfast)";
1838
1839     my $i = 1;
1840     $a[$i] = $^X . '-';
1841     $a[$i]=~ m/(.)/g;
1842     cmp_ok pos($a[$i]), '>', 0, "infinite m//g on arrays (aelem)";
1843
1844     my %h;
1845     $h{a} = $^X . '-';
1846     $h{a}=~ m/(.)/g;
1847     cmp_ok pos($h{a}), '>', 0, "infinite m//g on hashes (helem)";
1848 }
1849
1850 SKIP:
1851 {
1852     my $got_dualvar;
1853     eval 'use Scalar::Util "dualvar"; $got_dualvar++';
1854     skip "No Scalar::Util::dualvar" unless $got_dualvar;
1855     my $a = Scalar::Util::dualvar(3, $^X);
1856     my $b = $a + 5;
1857     is ($b, 8, "Arithmetic on tainted dualvars works");
1858 }
1859
1860 # opening '|-' should not trigger $ENV{PATH} check
1861
1862 {
1863     SKIP: {
1864         skip "fork() is not available", 3 unless $Config{'d_fork'};
1865         skip "opening |- is not stable on threaded Open/MirBSD with taint", 3
1866             if $Config{useithreads} and $Is_OpenBSD || $Is_MirBSD;
1867
1868         $ENV{'PATH'} = $TAINT;
1869         local $SIG{'PIPE'} = 'IGNORE';
1870         eval {
1871             my $pid = open my $pipe, '|-';
1872             if (!defined $pid) {
1873                 die "open failed: $!";
1874             }
1875             if (!$pid) {
1876                 kill 'KILL', $$;        # child suicide
1877             }
1878             close $pipe;
1879         };
1880         unlike($@, qr/Insecure \$ENV/, 'fork triggers %ENV check');
1881         is($@, '',               'pipe/fork/open/close failed');
1882         eval {
1883             open my $pipe, "|$Invoke_Perl -e 1";
1884             close $pipe;
1885         };
1886         like($@, qr/Insecure \$ENV/, 'popen neglects %ENV check');
1887     }
1888 }
1889
1890 {
1891     package AUTOLOAD_TAINT;
1892     sub AUTOLOAD {
1893         our $AUTOLOAD;
1894         return if $AUTOLOAD =~ /DESTROY/;
1895         if ($AUTOLOAD =~ /untainted/) {
1896             main::isnt_tainted($AUTOLOAD, '$AUTOLOAD can be untainted');
1897             my $copy = $AUTOLOAD;
1898             main::isnt_tainted($copy, '$AUTOLOAD can be untainted');
1899         } else {
1900             main::is_tainted($AUTOLOAD, '$AUTOLOAD can be tainted');
1901             my $copy = $AUTOLOAD;
1902             main::is_tainted($copy, '$AUTOLOAD can be tainted');
1903         }
1904     }
1905
1906     package main;
1907     my $o = bless [], 'AUTOLOAD_TAINT';
1908     $o->untainted;
1909     $o->$TAINT;
1910     $o->untainted;
1911 }
1912
1913 {
1914     # tests for tainted format in s?printf
1915     my $fmt = $TAINT . "# %s\n";
1916     violates_taint(sub { printf($fmt, "foo") }, 'printf',
1917                    q/printf doesn't like tainted formats/);
1918     violates_taint(sub { printf($TAINT . "# %s\n", "foo") }, 'printf',
1919                    q/printf doesn't like tainted format expressions/);
1920     eval { printf("# %s\n", $TAINT . "foo") };
1921     is($@, '', q/printf accepts other tainted args/);
1922     violates_taint(sub { sprintf($fmt, "foo") }, 'sprintf',
1923                    q/sprintf doesn't like tainted formats/);
1924     violates_taint(sub { sprintf($TAINT . "# %s\n", "foo") }, 'sprintf',
1925                    q/sprintf doesn't like tainted format expressions/);
1926     eval { sprintf("# %s\n", $TAINT . "foo") };
1927     is($@, '', q/sprintf accepts other tainted args/);
1928 }
1929
1930 {
1931     # 40708
1932     my $n  = 7e9;
1933     8e9 - $n;
1934
1935     my $val = $n;
1936     is ($val, '7000000000', 'Assignment to untainted variable');
1937     $val = $TAINT;
1938     $val = $n;
1939     is ($val, '7000000000', 'Assignment to tainted variable');
1940 }
1941
1942 {
1943     my $val = 0;
1944     my $tainted = '1' . $TAINT;
1945     eval '$val = eval $tainted;';
1946     is ($val, 0, "eval doesn't like tainted strings");
1947     like ($@, qr/^Insecure dependency in eval/);
1948
1949     # Rather nice code to get a tainted undef by from Rick Delaney
1950     open my $fh, "test.pl" or die $!;
1951     seek $fh, 0, 2 or die $!;
1952     $tainted = <$fh>;
1953
1954     eval 'eval $tainted';
1955     like ($@, qr/^Insecure dependency in eval/);
1956 }
1957
1958 foreach my $ord (78, 163, 256) {
1959     # 47195
1960     my $line = 'A1' . $TAINT . chr $ord;
1961     chop $line;
1962     is($line, 'A1');
1963     $line =~ /(A\S*)/;
1964     isnt_tainted($1, "\\S match with chr $ord");
1965 }
1966
1967 {
1968   SKIP: {
1969       skip 'No crypt function, skipping crypt tests', 4 if(!$Config{d_crypt});
1970       # 59998
1971       sub cr {
1972           # On platforms implementing FIPS mode, using a weak algorithm
1973           # (including the default triple-DES algorithm) causes crypt(3) to
1974           # return a null pointer, which Perl converts into undef. We assume
1975           # for now that all such platforms support glibc-style selection of
1976           # a different hashing algorithm.
1977           # glibc supports MD5, but OpenBSD only supports Blowfish.
1978           my $alg = '';       # Use default algorithm
1979           if ( !defined(crypt("ab", $alg."cd")) ) {
1980               $alg = '$5$';   # Try SHA-256
1981           }
1982           if ( !defined(crypt("ab", $alg."cd")) ) {
1983               $alg = '$2b$12$FPWWO2RJ3CK4FINTw0Hi';  # Try Blowfish
1984           }
1985           if ( !defined(crypt("ab", $alg."cd")) ) {
1986               $alg = ''; # Nothing worked.  Back to default
1987           }
1988           my $x = crypt($_[0], $alg . $_[1]);
1989           $x
1990       }
1991       sub co { my $x = ~$_[0]; $x }
1992       my ($a, $b);
1993       $a = cr('hello', 'foo' . $TAINT);
1994       $b = cr('hello', 'foo');
1995       is_tainted($a,  "tainted crypt");
1996       isnt_tainted($b, "untainted crypt");
1997       $a = co('foo' . $TAINT);
1998       $b = co('foo');
1999       is_tainted($a,  "tainted complement");
2000       isnt_tainted($b, "untainted complement");
2001     }
2002 }
2003
2004 {
2005     my @data = qw(bonk zam zlonk qunckkk);
2006     # Clearly some sort of usenet bang-path
2007     my $string = $TAINT . join "!", @data;
2008
2009     is_tainted($string, "tainted data");
2010
2011     my @got = split /!|,/, $string;
2012
2013     # each @got would be useful here, but I want the test for earlier perls
2014     for my $i (0 .. $#data) {
2015         is_tainted($got[$i], "tainted result $i");
2016         is($got[$i], $data[$i], "correct content $i");
2017     }
2018
2019     is_tainted($string, "still tainted data");
2020
2021     my @got = split /[!,]/, $string;
2022
2023     # each @got would be useful here, but I want the test for earlier perls
2024     for my $i (0 .. $#data) {
2025         is_tainted($got[$i], "tainted result $i");
2026         is($got[$i], $data[$i], "correct content $i");
2027     }
2028
2029     is_tainted($string, "still tainted data");
2030
2031     my @got = split /!/, $string;
2032
2033     # each @got would be useful here, but I want the test for earlier perls
2034     for my $i (0 .. $#data) {
2035         is_tainted($got[$i], "tainted result $i");
2036         is($got[$i], $data[$i], "correct content $i");
2037     }
2038 }
2039
2040 # Bug RT #52552 - broken by change at git commit id f337b08
2041 {
2042     my $x = $TAINT. q{print "Hello world\n"};
2043     my $y = pack "a*", $x;
2044     is_tainted($y, "pack a* preserves tainting");
2045
2046     my $z = pack "A*", q{print "Hello world\n"}.$TAINT;
2047     is_tainted($z, "pack A* preserves tainting");
2048
2049     my $zz = pack "a*a*", q{print "Hello world\n"}, $TAINT;
2050     is_tainted($zz, "pack a*a* preserves tainting");
2051 }
2052
2053 # Bug RT #61976 tainted $! would show numeric rather than string value
2054
2055 {
2056     my $tainted_path = substr($^X,0,0) . "/no/such/file";
2057     my $err;
2058     # $! is used in a tainted expression, so gets tainted
2059     open my $fh, $tainted_path or $err= "$!";
2060     unlike($err, qr/^\d+$/, 'tainted $!');
2061 }
2062
2063 {
2064     # #6758: tainted values become untainted in tied hashes
2065     #         (also applies to other value magic such as pos)
2066
2067
2068     package P6758;
2069
2070     sub TIEHASH { bless {} }
2071     sub TIEARRAY { bless {} }
2072
2073     my $i = 0;
2074
2075     sub STORE {
2076         main::is_tainted($_[1], "tied arg1 tainted");
2077         main::is_tainted($_[2], "tied arg2 tainted");
2078         $i++;
2079     }
2080
2081     package main;
2082
2083     my ($k,$v) = qw(1111 val);
2084     taint_these($k,$v);
2085     tie my @array, 'P6758';
2086     tie my %hash , 'P6758';
2087     $array[$k] = $v;
2088     $hash{$k} = $v;
2089     ok $i == 2, "tied STORE called correct number of times";
2090 }
2091
2092 # Bug RT #45167 the return value of sprintf sometimes wasn't tainted
2093 # when the args were tainted. This only occurred on the first use of
2094 # sprintf; after that, its TARG has taint magic attached, so setmagic
2095 # at the end works.  That's why there are multiple sprintf's below, rather
2096 # than just one wrapped in an inner loop. Also, any plaintext between
2097 # format entries would correctly cause tainting to get set. so test with
2098 # "%s%s" rather than eg "%s %s".
2099
2100 {
2101     for my $var1 ($TAINT, "123") {
2102         for my $var2 ($TAINT0, "456") {
2103             is( tainted(sprintf '%s', $var1, $var2), tainted($var1),
2104                 "sprintf '%s', '$var1', '$var2'" );
2105             is( tainted(sprintf ' %s', $var1, $var2), tainted($var1),
2106                 "sprintf ' %s', '$var1', '$var2'" );
2107             is( tainted(sprintf '%s%s', $var1, $var2),
2108                 tainted($var1) || tainted($var2),
2109                 "sprintf '%s%s', '$var1', '$var2'" );
2110         }
2111     }
2112 }
2113
2114
2115 # Bug RT #67962: old tainted $1 gets treated as tainted
2116 # in next untainted # match
2117
2118 {
2119     use re 'taint';
2120     "abc".$TAINT =~ /(.*)/; # make $1 tainted
2121     is_tainted($1, '$1 should be tainted');
2122
2123     my $untainted = "abcdef";
2124     isnt_tainted($untainted, '$untainted should be untainted');
2125     $untainted =~ s/(abc)/$1/;
2126     isnt_tainted($untainted, '$untainted should still be untainted');
2127     $untainted =~ s/(abc)/x$1/;
2128     isnt_tainted($untainted, '$untainted should yet still be untainted');
2129 }
2130
2131 {
2132     # On Windows we can't spawn a fresh Perl interpreter unless at
2133     # least the Windows system directory (usually C:\Windows\System32)
2134     # is still on the PATH.  There is however no way to determine the
2135     # actual path on the current system without loading the Win32
2136     # module, so we just restore the original $ENV{PATH} here.
2137     local $ENV{PATH} = $ENV{PATH};
2138     $ENV{PATH} = $old_env_path if $Is_MSWin32;
2139
2140     fresh_perl_is(<<'end', "ok", { switches => [ '-T' ] },
2141     $TAINT = substr($^X, 0, 0);
2142     formline('@'.('<'x("2000".$TAINT)).' | @*', 'hallo', 'welt');
2143     print "ok";
2144 end
2145     "formline survives a tainted dynamic picture");
2146 }
2147
2148 {
2149     isnt_tainted($^A, "format accumulator not tainted yet");
2150     formline('@ | @*', 'hallo' . $TAINT, 'welt');
2151     is_tainted($^A, "tainted formline argument makes a tainted accumulator");
2152     $^A = "";
2153     isnt_tainted($^A, "accumulator can be explicitly untainted");
2154     formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2155     isnt_tainted($^A, "accumulator still untainted");
2156     $^A = "" . $TAINT;
2157     is_tainted($^A, "accumulator can be explicitly tainted");
2158     formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2159     is_tainted($^A, "accumulator still tainted");
2160     $^A = "";
2161     isnt_tainted($^A, "accumulator untainted again");
2162     formline('@' .('<'*5) . ' | @*', 'hallo', 'welt');
2163     isnt_tainted($^A, "accumulator still untainted");
2164     formline('@' .('<'*(5+$TAINT0)) . ' | @*', 'hallo', 'welt');
2165     is_tainted($^A, "the accumulator should be tainted already");
2166     is_tainted($^A, "tainted formline picture makes a tainted accumulator");
2167 }
2168
2169 {   # Bug #80610
2170     "Constant(1)" =~ / ^ ([a-z_]\w*) (?: [(] (.*) [)] )? $ /xi;
2171     my $a = $1;
2172     my $b = $2;
2173     isnt_tainted($a, "regex optimization of single char /[]/i doesn't taint");
2174     isnt_tainted($b, "regex optimization of single char /[]/i doesn't taint");
2175 }
2176
2177 {
2178     # RT 81230: tainted value during FETCH created extra ref to tied obj
2179
2180     package P81230;
2181     use warnings;
2182
2183     my %h;
2184
2185     sub TIEHASH {
2186         my $x = $^X; # tainted
2187         bless  \$x;
2188     }
2189     sub FETCH { my $x = $_[0]; $$x . "" }
2190
2191     tie %h, 'P81230';
2192
2193     my $w = "";
2194     local $SIG{__WARN__} = sub { $w .= "@_" };
2195
2196     untie %h if $h{"k"};
2197
2198     ::is($w, "", "RT 81230");
2199 }
2200
2201 {
2202     # Compiling a subroutine inside a tainted expression does not make the
2203     # constant folded values tainted.
2204     my $x = sub { "x" . "y" };
2205     my $y = $ENV{PATH} . $x->(); # Compile $x inside a tainted expression
2206     my $z = $x->();
2207     isnt_tainted($z, "Constants folded value not tainted");
2208 }
2209
2210 {
2211     # now that regexes are first class SVs, make sure that they themselves
2212     # as well as references to them are tainted
2213
2214     my $rr = qr/(.)$TAINT/;
2215     my $r = $$rr; # bare REGEX
2216     my $s ="abc";
2217     ok($s =~ s/$r/x/, "match bare regex");
2218     is_tainted($s, "match bare regex taint");
2219     is($s, 'xbc', "match bare regex taint value");
2220 }
2221
2222 {
2223     # [perl #82616] security Issues with user-defined \p{} properties
2224     # A using a tainted user-defined property should croak
2225
2226     sub IsA { sprintf "%02x", ord("A") }
2227
2228     my $prop = "IsA";
2229     ok("A" =~ /\p{$prop}/, "user-defined property: non-tainted case");
2230     $prop = "IsA$TAINT";
2231     eval { "A" =~ /\p{$prop}/};
2232     like($@, qr/Insecure user-defined property \\p\{main::IsA}/,
2233             "user-defined property: tainted case");
2234 }
2235
2236 {
2237     # [perl #87336] lc/uc(first) failing to taint the returned string
2238     my $source = "foo$TAINT";
2239     my $dest = lc $source;
2240     is_tainted $dest, "lc(tainted) taints its return value";
2241     $dest = lcfirst $source;
2242     is_tainted $dest, "lcfirst(tainted) taints its return value";
2243     $dest = uc $source;
2244     is_tainted $dest, "uc(tainted) taints its return value";
2245     $dest = ucfirst $source;
2246     is_tainted $dest, "ucfirst(tainted) taints its return value";
2247 }
2248
2249 {
2250     # Taintedness of values returned from given()
2251     use feature 'switch';
2252     no warnings 'experimental::smartmatch';
2253
2254     my @descriptions = ('when', 'given end', 'default');
2255
2256     for (qw<x y z>) {
2257         my $letter = "$_$TAINT";
2258
2259         my $desc = "tainted value returned from " . shift(@descriptions);
2260
2261         my $res = do {
2262             given ($_) {
2263                 when ('x') { $letter }
2264                 when ('y') { goto leavegiven }
2265                 default    { $letter }
2266                 leavegiven:  $letter
2267             }
2268         };
2269         is         $res, $letter, "$desc is correct";
2270         is_tainted $res,          "$desc stays tainted";
2271     }
2272 }
2273
2274
2275 # tainted constants and index()
2276 #  RT 64804; http://bugs.debian.org/291450
2277 {
2278     ok(tainted $old_env_path, "initial taintedness");
2279     BEGIN { no strict 'refs'; my $v = $old_env_path; *{"::C"} = sub () { $v }; }
2280     ok(tainted C, "constant is tainted properly");
2281     ok(!tainted "", "tainting not broken yet");
2282     index(undef, C);
2283     ok(!tainted "", "tainting still works after index() of the constant");
2284 }
2285
2286 # Tainted values with smartmatch
2287 # [perl #93590] S_do_smartmatch stealing its own string buffers
2288 {
2289 no warnings 'experimental::smartmatch';
2290 ok "M$TAINT" ~~ ['m', 'M'], '$tainted ~~ ["whatever", "match"]';
2291 ok !("M$TAINT" ~~ ['m', undef]), '$tainted ~~ ["whatever", undef]';
2292 }
2293
2294 # Tainted values and ref()
2295 for(1,2) {
2296   my $x = bless \"M$TAINT", ref(bless[], "main");
2297 }
2298 pass("no death when TARG of ref is tainted");
2299
2300 # $$ should not be tainted by being read in a tainted expression.
2301 {
2302     isnt_tainted $$, "PID not tainted initially";
2303     my $x = $ENV{PATH}.$$;
2304     isnt_tainted $$, "PID not tainted when read in tainted expression";
2305 }
2306
2307 SKIP: {
2308     skip 'Locales not available', 4 unless locales_enabled('LC_CTYPE');
2309
2310     use feature 'fc';
2311     use locale;
2312     my ($latin1, $utf8) = ("\xDF") x 2;
2313     utf8::downgrade($latin1);
2314     utf8::upgrade($utf8);
2315
2316     is_tainted fc($latin1), "under locale, lc(latin1) taints the result";
2317     is_tainted fc($utf8), "under locale, lc(utf8) taints the result";
2318
2319     is_tainted "\F$latin1", "under locale, \\Flatin1 taints the result";
2320     is_tainted "\F$utf8", "under locale, \\Futf8 taints the result";
2321 }
2322
2323 { # 111654
2324   eval {
2325     eval { die "Test\n".substr($ENV{PATH}, 0, 0); };
2326     die;
2327   };
2328   like($@, qr/^Test\n\t\.\.\.propagated at /, "error should be propagated");
2329 }
2330
2331 # tainted run-time (?{}) should die
2332
2333 {
2334     my $code = '(?{})' . $TAINT;
2335     use re 'eval';
2336     eval { "a" =~ /$code/ };
2337     like($@, qr/Eval-group in insecure regular expression/, "tainted (?{})");
2338 }
2339
2340 # reset() and tainted undef (?!)
2341 $::x = "foo";
2342 $_ = "$TAINT".reset "x";
2343 is eval { eval $::x.1 }, 1, 'reset does not taint undef';
2344
2345 # [perl #122669]
2346 {
2347     # See the comment above the first formline test.
2348     local $ENV{PATH} = $ENV{PATH};
2349     $ENV{PATH} = $old_env_path if $Is_MSWin32;
2350     is runperl(
2351        switches => [ '-T' ],
2352        prog => 'use constant K=>$^X; 0 if K; BEGIN{} use strict; '
2353               .'print 122669, qq-\n-',
2354        stderr => 1,
2355      ), "122669\n",
2356         'tainted constant as logop condition should not prevent "use"';
2357 }
2358
2359 # optimised SETi etc need to handle tainting
2360
2361 {
2362     my ($i1, $i2, $i3) = (1, 1, 1);
2363     my ($n1, $n2, $n3) = (1.1, 1.1, 1.1);
2364     my $tn = $TAINT0 + 1.1;
2365
2366     $i1 = $TAINT0 + 2;
2367     is_tainted $i1, "+ SETi";
2368     $i2 = $TAINT0 - 2;
2369     is_tainted $i2, "- SETi";
2370     $i3 = $TAINT0 * 2;
2371     is_tainted $i3, "* SETi";
2372
2373     $n1 = $tn + 2.2;
2374     is_tainted $n1, "+ SETn";
2375     $n2 = $tn - 2.2;
2376     is_tainted $n2, "- SETn";
2377     $n3 = $tn * 2.2;
2378     is_tainted $n3, "* SETn";
2379 }
2380
2381 # check that localizing something with get magic (e.g. taint) doesn't
2382 # upgrade pIOK to IOK
2383
2384 {
2385     local our $x = 1.1 + $TAINT0;  # $x should be NOK
2386     my $ix = int($x);          #          now NOK, pIOK
2387     {
2388         local $x = 0;
2389     }
2390     my $x1 = $x * 1;
2391     isnt($x, 1); # it should be 1.1, not 1
2392 }
2393
2394
2395 # This may bomb out with the alarm signal so keep it last
2396 SKIP: {
2397     skip "No alarm()"  unless $Config{d_alarm};
2398     # Test from RT #41831]
2399     # [PATCH] Bug & fix: hang when using study + taint mode (perl 5.6.1, 5.8.x)
2400
2401     my $DATA = <<'END' . $TAINT;
2402 line1 is here
2403 line2 is here
2404 line3 is here
2405 line4 is here
2406
2407 END
2408
2409     #study $DATA;
2410
2411     ## don't set $SIG{ALRM}, since we'd never get to a user-level handler as
2412     ## perl is stuck in a regexp infinite loop!
2413
2414     alarm(10);
2415
2416     if ($DATA =~ /^line2.*line4/m) {
2417         fail("Should not be a match")
2418     } else {
2419         pass("Match on tainted multiline data should fail promptly");
2420     }
2421
2422     alarm(0);
2423 }
2424 __END__
2425 # Keep the previous test last