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