Commit | Line | Data |
1e422769 |
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 |
7 | # better than having no tests at all, right? |
8 | # |
9 | |
10 | BEGIN { |
11 | chdir 't' if -d 't'; |
20822f61 |
12 | @INC = '../lib'; |
1e422769 |
13 | } |
14 | |
15 | use strict; |
16 | use Config; |
dc459aad |
17 | use File::Spec::Functions; |
1e422769 |
18 | |
09f04786 |
19 | BEGIN { require './test.pl'; } |
5e500fc8 |
20 | plan tests => 260; |
7c36658b |
21 | |
0ecd3ba2 |
22 | $| = 1; |
23 | |
c9f931b8 |
24 | use vars qw($ipcsysv); # did we manage to load IPC::SysV? |
25 | |
3eeba6fb |
26 | BEGIN { |
27 | if ($^O eq 'VMS' && !defined($Config{d_setenv})) { |
28 | $ENV{PATH} = $ENV{PATH}; |
29 | $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy'; |
30 | } |
be3174d2 |
31 | if ($Config{'extensions'} =~ /\bIPC\/SysV\b/ |
32 | && ($Config{d_shm} || $Config{d_msg})) { |
c9f931b8 |
33 | eval { require IPC::SysV }; |
34 | unless ($@) { |
35 | $ipcsysv++; |
ddc3217d |
36 | IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU IPC_NOWAIT)); |
c9f931b8 |
37 | } |
b9d1c439 |
38 | } |
3eeba6fb |
39 | } |
40 | |
09f04786 |
41 | my $Is_MacOS = $^O eq 'MacOS'; |
42 | my $Is_VMS = $^O eq 'VMS'; |
43 | my $Is_MSWin32 = $^O eq 'MSWin32'; |
44 | my $Is_NetWare = $^O eq 'NetWare'; |
45 | my $Is_Dos = $^O eq 'dos'; |
46 | my $Is_Cygwin = $^O eq 'cygwin'; |
8c2c7bd1 |
47 | my $Is_OpenBSD = $^O eq 'openbsd'; |
09f04786 |
48 | my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' : |
49 | $Is_MSWin32 ? '.\perl' : |
50 | $Is_MacOS ? ':perl' : |
51 | $Is_NetWare ? 'perl' : |
52 | './perl' ; |
c90c0ff4 |
53 | my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/; |
7bac28a0 |
54 | |
1e422769 |
55 | if ($Is_VMS) { |
7bac28a0 |
56 | my (%old, $x); |
57 | for $x ('DCL$PATH', @MoreEnv) { |
58 | ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x}; |
59 | } |
1e422769 |
60 | eval <<EndOfCleanup; |
61 | END { |
3eeba6fb |
62 | \$ENV{PATH} = '' if $Config{d_setenv}; |
1e422769 |
63 | warn "# Note: logical name 'PATH' may have been deleted\n"; |
562a7b0c |
64 | \@ENV{keys %old} = values %old; |
1e422769 |
65 | } |
66 | EndOfCleanup |
67 | } |
68 | |
69 | # Sources of taint: |
70 | # The empty tainted value, for tainting strings |
71 | my $TAINT = substr($^X, 0, 0); |
72 | # A tainted zero, useful for tainting numbers |
09f04786 |
73 | my $TAINT0; |
74 | { |
75 | no warnings; |
76 | $TAINT0 = 0 + $TAINT; |
77 | } |
1e422769 |
78 | |
79 | # This taints each argument passed. All must be lvalues. |
80 | # Side effect: It also stringifies them. :-( |
81 | sub taint_these (@) { |
82 | for (@_) { $_ .= $TAINT } |
83 | } |
84 | |
85 | # How to identify taint when you see it |
86 | sub any_tainted (@) { |
87 | not eval { join("",@_), kill 0; 1 }; |
88 | } |
89 | sub tainted ($) { |
90 | any_tainted @_; |
91 | } |
92 | sub all_tainted (@) { |
93 | for (@_) { return 0 unless tainted $_ } |
94 | 1; |
95 | } |
96 | |
09f04786 |
97 | |
98 | sub test ($;$) { |
99 | my($ok, $diag) = @_; |
100 | |
101 | my $curr_test = curr_test(); |
102 | |
103 | if ($ok) { |
104 | print "ok $curr_test\n"; |
1e422769 |
105 | } else { |
09f04786 |
106 | print "not ok $curr_test\n"; |
107 | printf "# Failed test at line %d\n", (caller)[2]; |
1e422769 |
108 | for (split m/^/m, $diag) { |
109 | print "# $_"; |
110 | } |
9607fc9c |
111 | print "\n" unless |
1e422769 |
112 | $diag eq '' |
113 | or substr($diag, -1) eq "\n"; |
114 | } |
09f04786 |
115 | |
116 | next_test(); |
117 | |
118 | return $ok; |
1e422769 |
119 | } |
120 | |
121 | # We need an external program to call. |
dc459aad |
122 | my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : $Is_MacOS ? ":echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$")); |
1e422769 |
123 | END { unlink $ECHO } |
124 | open PROG, "> $ECHO" or die "Can't create $ECHO: $!"; |
125 | print PROG 'print "@ARGV\n"', "\n"; |
126 | close PROG; |
127 | my $echo = "$Invoke_Perl $ECHO"; |
128 | |
dc459aad |
129 | my $TEST = catfile(curdir(), 'TEST'); |
130 | |
1e422769 |
131 | # First, let's make sure that Perl is checking the dangerous |
132 | # environment variables. Maybe they aren't set yet, so we'll |
133 | # taint them ourselves. |
134 | { |
135 | $ENV{'DCL$PATH'} = '' if $Is_VMS; |
136 | |
f68313a1 |
137 | if ($Is_MSWin32 && $Config{ccname} =~ /bcc32/ && ! -f 'cc3250mt.dll') { |
138 | my $bcc_dir; |
139 | foreach my $dir (split /$Config{path_sep}/, $ENV{PATH}) { |
140 | if (-f "$dir/cc3250mt.dll") { |
141 | $bcc_dir = $dir and last; |
142 | } |
143 | } |
144 | if (defined $bcc_dir) { |
145 | require File::Copy; |
146 | File::Copy::copy("$bcc_dir/cc3250mt.dll", '.') or |
147 | die "$0: failed to copy cc3250mt.dll: $!\n"; |
148 | eval q{ |
149 | END { unlink "cc3250mt.dll" } |
150 | }; |
151 | } |
152 | } |
153 | |
7bac28a0 |
154 | $ENV{PATH} = ''; |
c90c0ff4 |
155 | delete @ENV{@MoreEnv}; |
7bac28a0 |
156 | $ENV{TERM} = 'dumb'; |
157 | |
09f04786 |
158 | test eval { `$echo 1` } eq "1\n"; |
159 | |
160 | SKIP: { |
161 | skip "Environment tainting tests skipped", 4 |
162 | if $Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos || $Is_MacOS; |
7bac28a0 |
163 | |
7bac28a0 |
164 | my @vars = ('PATH', @MoreEnv); |
165 | while (my $v = $vars[0]) { |
166 | local $ENV{$v} = $TAINT; |
167 | last if eval { `$echo 1` }; |
168 | last unless $@ =~ /^Insecure \$ENV{$v}/; |
169 | shift @vars; |
170 | } |
09f04786 |
171 | test !@vars, "@vars"; |
c90c0ff4 |
172 | |
173 | # tainted $TERM is unsafe only if it contains metachars |
174 | local $ENV{TERM}; |
175 | $ENV{TERM} = 'e=mc2'; |
09f04786 |
176 | test eval { `$echo 1` } eq "1\n"; |
c90c0ff4 |
177 | $ENV{TERM} = 'e=mc2' . $TAINT; |
09f04786 |
178 | test !eval { `$echo 1` }; |
179 | test $@ =~ /^Insecure \$ENV{TERM}/, $@; |
5aabfad6 |
180 | } |
7bac28a0 |
181 | |
9607fc9c |
182 | my $tmp; |
2986a63f |
183 | if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) { |
48c036b1 |
184 | print "# all directories are writeable\n"; |
185 | } |
186 | else { |
9607fc9c |
187 | $tmp = (grep { defined and -d and (stat _)[2] & 2 } |
099f76bb |
188 | qw(sys$scratch /tmp /var/tmp /usr/tmp), |
9607fc9c |
189 | @ENV{qw(TMP TEMP)})[0] |
190 | or print "# can't find world-writeable directory to test PATH\n"; |
191 | } |
192 | |
09f04786 |
193 | SKIP: { |
194 | skip "all directories are writeable", 2 unless $tmp; |
195 | |
7bac28a0 |
196 | local $ENV{PATH} = $tmp; |
09f04786 |
197 | test !eval { `$echo 1` }; |
198 | test $@ =~ /^Insecure directory in \$ENV{PATH}/, $@; |
1e422769 |
199 | } |
200 | |
09f04786 |
201 | SKIP: { |
202 | skip "This is not VMS", 4 unless $Is_VMS; |
203 | |
1e422769 |
204 | $ENV{'DCL$PATH'} = $TAINT; |
09f04786 |
205 | test eval { `$echo 1` } eq ''; |
206 | test $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@; |
207 | SKIP: { |
208 | skip q[can't find world-writeable directory to test DCL$PATH], 2 |
25fb98c0 |
209 | unless $tmp; |
09f04786 |
210 | |
9607fc9c |
211 | $ENV{'DCL$PATH'} = $tmp; |
09f04786 |
212 | test eval { `$echo 1` } eq ''; |
213 | test $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@; |
9607fc9c |
214 | } |
1e422769 |
215 | $ENV{'DCL$PATH'} = ''; |
216 | } |
1e422769 |
217 | } |
218 | |
219 | # Let's see that we can taint and untaint as needed. |
220 | { |
221 | my $foo = $TAINT; |
09f04786 |
222 | test tainted $foo; |
9607fc9c |
223 | |
224 | # That was a sanity check. If it failed, stop the insanity! |
225 | die "Taint checks don't seem to be enabled" unless tainted $foo; |
1e422769 |
226 | |
227 | $foo = "foo"; |
09f04786 |
228 | test not tainted $foo; |
1e422769 |
229 | |
230 | taint_these($foo); |
09f04786 |
231 | test tainted $foo; |
1e422769 |
232 | |
233 | my @list = 1..10; |
09f04786 |
234 | test not any_tainted @list; |
1e422769 |
235 | taint_these @list[1,3,5,7,9]; |
09f04786 |
236 | test any_tainted @list; |
237 | test all_tainted @list[1,3,5,7,9]; |
238 | test not any_tainted @list[0,2,4,6,8]; |
1e422769 |
239 | |
240 | ($foo) = $foo =~ /(.+)/; |
09f04786 |
241 | test not tainted $foo; |
1e422769 |
242 | |
243 | $foo = $1 if ('bar' . $TAINT) =~ /(.+)/; |
09f04786 |
244 | test not tainted $foo; |
245 | test $foo eq 'bar'; |
1e422769 |
246 | |
b3eb6a9b |
247 | { |
248 | use re 'taint'; |
249 | |
250 | ($foo) = ('bar' . $TAINT) =~ /(.+)/; |
09f04786 |
251 | test tainted $foo; |
252 | test $foo eq 'bar'; |
b3eb6a9b |
253 | |
254 | $foo = $1 if ('bar' . $TAINT) =~ /(.+)/; |
09f04786 |
255 | test tainted $foo; |
256 | test $foo eq 'bar'; |
b3eb6a9b |
257 | } |
258 | |
259 | $foo = $1 if 'bar' =~ /(.+)$TAINT/; |
09f04786 |
260 | test tainted $foo; |
261 | test $foo eq 'bar'; |
48c036b1 |
262 | |
1e422769 |
263 | my $pi = 4 * atan2(1,1) + $TAINT0; |
09f04786 |
264 | test tainted $pi; |
1e422769 |
265 | |
266 | ($pi) = $pi =~ /(\d+\.\d+)/; |
09f04786 |
267 | test not tainted $pi; |
268 | test sprintf("%.5f", $pi) eq '3.14159'; |
1e422769 |
269 | } |
270 | |
271 | # How about command-line arguments? The problem is that we don't |
272 | # always get some, so we'll run another process with some. |
dc459aad |
273 | SKIP: { |
274 | my $arg = catfile(curdir(), "arg$$"); |
1e422769 |
275 | open PROG, "> $arg" or die "Can't create $arg: $!"; |
276 | print PROG q{ |
277 | eval { join('', @ARGV), kill 0 }; |
278 | exit 0 if $@ =~ /^Insecure dependency/; |
279 | print "# Oops: \$@ was [$@]\n"; |
280 | exit 1; |
281 | }; |
282 | close PROG; |
283 | print `$Invoke_Perl "-T" $arg and some suspect arguments`; |
09f04786 |
284 | test !$?, "Exited with status $?"; |
1e422769 |
285 | unlink $arg; |
286 | } |
287 | |
288 | # Reading from a file should be tainted |
289 | { |
09f04786 |
290 | test open(FILE, $TEST), "Couldn't open '$TEST': $!"; |
1e422769 |
291 | |
292 | my $block; |
293 | sysread(FILE, $block, 100); |
9607fc9c |
294 | my $line = <FILE>; |
1e422769 |
295 | close FILE; |
09f04786 |
296 | test tainted $block; |
297 | test tainted $line; |
1e422769 |
298 | } |
299 | |
c90c0ff4 |
300 | # Globs should be forbidden, except under VMS, |
301 | # which doesn't spawn an external program. |
09f04786 |
302 | SKIP: { |
303 | skip "globs should be forbidden", 2 if 1 or $Is_VMS; |
304 | |
7bac28a0 |
305 | my @globs = eval { <*> }; |
09f04786 |
306 | test @globs == 0 && $@ =~ /^Insecure dependency/; |
1e422769 |
307 | |
7bac28a0 |
308 | @globs = eval { glob '*' }; |
09f04786 |
309 | test @globs == 0 && $@ =~ /^Insecure dependency/; |
1e422769 |
310 | } |
311 | |
312 | # Output of commands should be tainted |
313 | { |
314 | my $foo = `$echo abc`; |
09f04786 |
315 | test tainted $foo; |
1e422769 |
316 | } |
317 | |
318 | # Certain system variables should be tainted |
319 | { |
09f04786 |
320 | test all_tainted $^X, $0; |
1e422769 |
321 | } |
322 | |
323 | # Results of matching should all be untainted |
324 | { |
325 | my $foo = "abcdefghi" . $TAINT; |
09f04786 |
326 | test tainted $foo; |
1e422769 |
327 | |
328 | $foo =~ /def/; |
09f04786 |
329 | test not any_tainted $`, $&, $'; |
1e422769 |
330 | |
331 | $foo =~ /(...)(...)(...)/; |
09f04786 |
332 | test not any_tainted $1, $2, $3, $+; |
1e422769 |
333 | |
334 | my @bar = $foo =~ /(...)(...)(...)/; |
09f04786 |
335 | test not any_tainted @bar; |
1e422769 |
336 | |
09f04786 |
337 | test tainted $foo; # $foo should still be tainted! |
338 | test $foo eq "abcdefghi"; |
1e422769 |
339 | } |
340 | |
341 | # Operations which affect files can't use tainted data. |
342 | { |
09f04786 |
343 | test !eval { chmod 0, $TAINT }, 'chmod'; |
344 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
345 | |
9607fc9c |
346 | # There is no feature test in $Config{} for truncate, |
347 | # so we allow for the possibility that it's missing. |
09f04786 |
348 | test !eval { truncate 'NoSuChFiLe', $TAINT0 }, 'truncate'; |
349 | test $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@; |
1e422769 |
350 | |
09f04786 |
351 | test !eval { rename '', $TAINT }, 'rename'; |
352 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
353 | |
09f04786 |
354 | test !eval { unlink $TAINT }, 'unlink'; |
355 | test $@ =~ /^Insecure dependency/, $@; |
9607fc9c |
356 | |
09f04786 |
357 | test !eval { utime $TAINT }, 'utime'; |
358 | test $@ =~ /^Insecure dependency/, $@; |
48c036b1 |
359 | |
09f04786 |
360 | SKIP: { |
361 | skip "chown() is not available", 2 unless $Config{d_chown}; |
1e422769 |
362 | |
09f04786 |
363 | test !eval { chown -1, -1, $TAINT }, 'chown'; |
364 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
365 | } |
366 | |
09f04786 |
367 | SKIP: { |
368 | skip "link() is not available", 2 unless $Config{d_link}; |
369 | |
370 | test !eval { link $TAINT, '' }, 'link'; |
371 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
372 | } |
09f04786 |
373 | |
374 | SKIP: { |
375 | skip "symlink() is not available", 2 unless $Config{d_symlink}; |
376 | |
377 | test !eval { symlink $TAINT, '' }, 'symlink'; |
378 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
379 | } |
380 | } |
381 | |
382 | # Operations which affect directories can't use tainted data. |
383 | { |
09f04786 |
384 | test !eval { mkdir "foo".$TAINT, 0755.$TAINT0 }, 'mkdir'; |
385 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
386 | |
09f04786 |
387 | test !eval { rmdir $TAINT }, 'rmdir'; |
388 | test $@ =~ /^Insecure dependency/, $@; |
9607fc9c |
389 | |
09f04786 |
390 | test !eval { chdir "foo".$TAINT }, 'chdir'; |
391 | test $@ =~ /^Insecure dependency/, $@; |
48c036b1 |
392 | |
09f04786 |
393 | SKIP: { |
394 | skip "chroot() is not available", 2 unless $Config{d_chroot}; |
395 | |
396 | test !eval { chroot $TAINT }, 'chroot'; |
397 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
398 | } |
399 | } |
400 | |
401 | # Some operations using files can't use tainted data. |
402 | { |
403 | my $foo = "imaginary library" . $TAINT; |
09f04786 |
404 | test !eval { require $foo }, 'require'; |
405 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
406 | |
407 | my $filename = "./taintB$$"; # NB: $filename isn't tainted! |
408 | END { unlink $filename if defined $filename } |
409 | $foo = $filename . $TAINT; |
410 | unlink $filename; # in any case |
411 | |
09f04786 |
412 | test !eval { open FOO, $foo }, 'open for read'; |
413 | test $@ eq '', $@; # NB: This should be allowed |
9d116dd7 |
414 | |
415 | # Try first new style but allow also old style. |
327ccce1 |
416 | # We do not want the whole taint.t to fail |
417 | # just because Errno possibly failing. |
09f04786 |
418 | test eval('$!{ENOENT}') || |
61ae2fbf |
419 | $! == 2 || # File not found |
420 | ($Is_Dos && $! == 22) || |
421 | ($^O eq 'mint' && $! == 33); |
1e422769 |
422 | |
09f04786 |
423 | test !eval { open FOO, "> $foo" }, 'open for write'; |
424 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
425 | } |
426 | |
427 | # Commands to the system can't use tainted data |
428 | { |
429 | my $foo = $TAINT; |
430 | |
09f04786 |
431 | SKIP: { |
432 | skip "open('|') is not available", 4 if $^O eq 'amigaos'; |
433 | |
434 | test !eval { open FOO, "| x$foo" }, 'popen to'; |
435 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
436 | |
09f04786 |
437 | test !eval { open FOO, "x$foo |" }, 'popen from'; |
438 | test $@ =~ /^Insecure dependency/, $@; |
48c036b1 |
439 | } |
1e422769 |
440 | |
09f04786 |
441 | test !eval { exec $TAINT }, 'exec'; |
442 | test $@ =~ /^Insecure dependency/, $@; |
9607fc9c |
443 | |
09f04786 |
444 | test !eval { system $TAINT }, 'system'; |
445 | test $@ =~ /^Insecure dependency/, $@; |
48c036b1 |
446 | |
1e422769 |
447 | $foo = "*"; |
448 | taint_these $foo; |
449 | |
09f04786 |
450 | test !eval { `$echo 1$foo` }, 'backticks'; |
451 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
452 | |
09f04786 |
453 | SKIP: { |
454 | # wildcard expansion doesn't invoke shell on VMS, so is safe |
455 | skip "This is not VMS", 2 unless $Is_VMS; |
456 | |
457 | test join('', eval { glob $foo } ) ne '', 'globbing'; |
458 | test $@ eq '', $@; |
1e422769 |
459 | } |
460 | } |
461 | |
462 | # Operations which affect processes can't use tainted data. |
463 | { |
09f04786 |
464 | test !eval { kill 0, $TAINT }, 'kill'; |
465 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
466 | |
09f04786 |
467 | SKIP: { |
468 | skip "setpgrp() is not available", 2 unless $Config{d_setpgrp}; |
1e422769 |
469 | |
09f04786 |
470 | test !eval { setpgrp 0, $TAINT0 }, 'setpgrp'; |
471 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
472 | } |
09f04786 |
473 | |
474 | SKIP: { |
475 | skip "setpriority() is not available", 2 unless $Config{d_setprior}; |
476 | |
477 | test !eval { setpriority 0, $TAINT0, $TAINT0 }, 'setpriority'; |
478 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
479 | } |
480 | } |
481 | |
482 | # Some miscellaneous operations can't use tainted data. |
483 | { |
09f04786 |
484 | SKIP: { |
485 | skip "syscall() is not available", 2 unless $Config{d_syscall}; |
486 | |
487 | test !eval { syscall $TAINT }, 'syscall'; |
488 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
489 | } |
490 | |
491 | { |
492 | my $foo = "x" x 979; |
493 | taint_these $foo; |
494 | local *FOO; |
495 | my $temp = "./taintC$$"; |
496 | END { unlink $temp } |
09f04786 |
497 | test open(FOO, "> $temp"), "Couldn't open $temp for write: $!"; |
1e422769 |
498 | |
09f04786 |
499 | test !eval { ioctl FOO, $TAINT0, $foo }, 'ioctl'; |
500 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
501 | |
09f04786 |
502 | SKIP: { |
503 | skip "fcntl() is not available", 2 unless $Config{d_fcntl}; |
504 | |
505 | test !eval { fcntl FOO, $TAINT0, $foo }, 'fcntl'; |
506 | test $@ =~ /^Insecure dependency/, $@; |
1e422769 |
507 | } |
508 | |
509 | close FOO; |
510 | } |
511 | } |
512 | |
9607fc9c |
513 | # Some tests involving references |
1e422769 |
514 | { |
515 | my $foo = 'abc' . $TAINT; |
516 | my $fooref = \$foo; |
09f04786 |
517 | test not tainted $fooref; |
518 | test tainted $$fooref; |
519 | test tainted $foo; |
1e422769 |
520 | } |
54310121 |
521 | |
522 | # Some tests involving assignment |
523 | { |
524 | my $foo = $TAINT0; |
525 | my $bar = $foo; |
09f04786 |
526 | test all_tainted $foo, $bar; |
527 | test tainted($foo = $bar); |
528 | test tainted($bar = $bar); |
529 | test tainted($bar += $bar); |
530 | test tainted($bar -= $bar); |
531 | test tainted($bar *= $bar); |
532 | test tainted($bar++); |
533 | test tainted($bar /= $bar); |
534 | test tainted($bar += 0); |
535 | test tainted($bar -= 2); |
536 | test tainted($bar *= -1); |
537 | test tainted($bar /= 1); |
538 | test tainted($bar--); |
539 | test $bar == 0; |
54310121 |
540 | } |
a1f49e72 |
541 | |
542 | # Test assignment and return of lists |
543 | { |
544 | my @foo = ("A", "tainted" . $TAINT, "B"); |
09f04786 |
545 | test not tainted $foo[0]; |
546 | test tainted $foo[1]; |
547 | test not tainted $foo[2]; |
a1f49e72 |
548 | my @bar = @foo; |
09f04786 |
549 | test not tainted $bar[0]; |
550 | test tainted $bar[1]; |
551 | test not tainted $bar[2]; |
a1f49e72 |
552 | my @baz = eval { "A", "tainted" . $TAINT, "B" }; |
09f04786 |
553 | test not tainted $baz[0]; |
554 | test tainted $baz[1]; |
555 | test not tainted $baz[2]; |
a1f49e72 |
556 | my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ]; |
09f04786 |
557 | test not tainted $plugh[0]; |
558 | test tainted $plugh[1]; |
559 | test not tainted $plugh[2]; |
a1f49e72 |
560 | my $nautilus = sub { "A", "tainted" . $TAINT, "B" }; |
09f04786 |
561 | test not tainted ((&$nautilus)[0]); |
562 | test tainted ((&$nautilus)[1]); |
563 | test not tainted ((&$nautilus)[2]); |
a1f49e72 |
564 | my @xyzzy = &$nautilus; |
09f04786 |
565 | test not tainted $xyzzy[0]; |
566 | test tainted $xyzzy[1]; |
567 | test not tainted $xyzzy[2]; |
a1f49e72 |
568 | my $red_october = sub { return "A", "tainted" . $TAINT, "B" }; |
09f04786 |
569 | test not tainted ((&$red_october)[0]); |
570 | test tainted ((&$red_october)[1]); |
571 | test not tainted ((&$red_october)[2]); |
a1f49e72 |
572 | my @corge = &$red_october; |
09f04786 |
573 | test not tainted $corge[0]; |
574 | test tainted $corge[1]; |
575 | test not tainted $corge[2]; |
a1f49e72 |
576 | } |
fb73857a |
577 | |
578 | # Test for system/library calls returning string data of dubious origin. |
579 | { |
580 | # No reliable %Config check for getpw* |
09f04786 |
581 | SKIP: { |
582 | skip "getpwent() is not available", 1 unless |
583 | eval { setpwent(); getpwent() }; |
584 | |
fb73857a |
585 | setpwent(); |
586 | my @getpwent = getpwent(); |
587 | die "getpwent: $!\n" unless (@getpwent); |
09f04786 |
588 | test ( not tainted $getpwent[0] |
2959b6e3 |
589 | and tainted $getpwent[1] |
fb73857a |
590 | and not tainted $getpwent[2] |
591 | and not tainted $getpwent[3] |
592 | and not tainted $getpwent[4] |
593 | and not tainted $getpwent[5] |
3972a534 |
594 | and tainted $getpwent[6] # ge?cos |
fb73857a |
595 | and not tainted $getpwent[7] |
3972a534 |
596 | and tainted $getpwent[8]); # shell |
fb73857a |
597 | endpwent(); |
fb73857a |
598 | } |
599 | |
09f04786 |
600 | SKIP: { |
601 | # pretty hard to imagine not |
602 | skip "readdir() is not available", 1 unless $Config{d_readdir}; |
603 | |
fb73857a |
604 | local(*D); |
605 | opendir(D, "op") or die "opendir: $!\n"; |
606 | my $readdir = readdir(D); |
09f04786 |
607 | test tainted $readdir; |
608 | closedir(D); |
fb73857a |
609 | } |
610 | |
09f04786 |
611 | SKIP: { |
612 | skip "readlink() or symlink() is not available" unless |
613 | $Config{d_readlink} && $Config{d_symlink}; |
614 | |
fb73857a |
615 | my $symlink = "sl$$"; |
616 | unlink($symlink); |
dc459aad |
617 | my $sl = "/something/naughty"; |
618 | # it has to be a real path on Mac OS |
619 | $sl = MacPerl::MakePath((MacPerl::Volumes())[0]) if $Is_MacOS; |
620 | symlink($sl, $symlink) or die "symlink: $!\n"; |
fb73857a |
621 | my $readlink = readlink($symlink); |
09f04786 |
622 | test tainted $readlink; |
fb73857a |
623 | unlink($symlink); |
fb73857a |
624 | } |
625 | } |
626 | |
627 | # test bitwise ops (regression bug) |
628 | { |
629 | my $why = "y"; |
630 | my $j = "x" | $why; |
09f04786 |
631 | test not tainted $j; |
fb73857a |
632 | $why = $TAINT."y"; |
633 | $j = "x" | $why; |
09f04786 |
634 | test tainted $j; |
fb73857a |
635 | } |
636 | |
48c036b1 |
637 | # test target of substitution (regression bug) |
638 | { |
639 | my $why = $TAINT."y"; |
640 | $why =~ s/y/z/; |
09f04786 |
641 | test tainted $why; |
48c036b1 |
642 | |
643 | my $z = "[z]"; |
644 | $why =~ s/$z/zee/; |
09f04786 |
645 | test tainted $why; |
48c036b1 |
646 | |
647 | $why =~ s/e/'-'.$$/ge; |
09f04786 |
648 | test tainted $why; |
48c036b1 |
649 | } |
d929ce6f |
650 | |
09f04786 |
651 | |
652 | SKIP: { |
653 | skip "no IPC::SysV", 2 unless $ipcsysv; |
654 | |
655 | # test shmread |
656 | SKIP: { |
657 | skip "shm*() not available", 1 unless $Config{d_shm}; |
658 | |
659 | no strict 'subs'; |
660 | my $sent = "foobar"; |
661 | my $rcvd; |
662 | my $size = 2000; |
663 | my $id = shmget(IPC_PRIVATE, $size, S_IRWXU); |
664 | |
665 | if (defined $id) { |
666 | if (shmwrite($id, $sent, 0, 60)) { |
667 | if (shmread($id, $rcvd, 0, 60)) { |
668 | substr($rcvd, index($rcvd, "\0")) = ''; |
669 | } else { |
670 | warn "# shmread failed: $!\n"; |
671 | } |
672 | } else { |
673 | warn "# shmwrite failed: $!\n"; |
674 | } |
675 | shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n"; |
676 | } else { |
677 | warn "# shmget failed: $!\n"; |
678 | } |
679 | |
680 | skip "SysV shared memory operation failed", 1 unless |
681 | $rcvd eq $sent; |
682 | |
683 | test tainted $rcvd; |
c9f931b8 |
684 | } |
c2e66d9e |
685 | |
d929ce6f |
686 | |
09f04786 |
687 | # test msgrcv |
688 | SKIP: { |
689 | skip "msg*() not available", 1 unless $Config{d_msg}; |
41d6edb2 |
690 | |
b9d1c439 |
691 | no strict 'subs'; |
41d6edb2 |
692 | my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU); |
693 | |
694 | my $sent = "message"; |
695 | my $type_sent = 1234; |
696 | my $rcvd; |
697 | my $type_rcvd; |
698 | |
699 | if (defined $id) { |
ddc3217d |
700 | if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) { |
701 | if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) { |
41d6edb2 |
702 | ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd); |
703 | } else { |
ddc3217d |
704 | warn "# msgrcv failed: $!\n"; |
41d6edb2 |
705 | } |
706 | } else { |
ddc3217d |
707 | warn "# msgsnd failed: $!\n"; |
41d6edb2 |
708 | } |
c2e66d9e |
709 | msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n"; |
41d6edb2 |
710 | } else { |
711 | warn "# msgget failed\n"; |
712 | } |
713 | |
09f04786 |
714 | SKIP: { |
715 | skip "SysV message queue operation failed", 1 |
716 | unless $rcvd eq $sent && $type_sent == $type_rcvd; |
717 | |
718 | test tainted $rcvd; |
41d6edb2 |
719 | } |
41d6edb2 |
720 | } |
721 | } |
722 | |
3887d568 |
723 | { |
724 | # bug id 20001004.006 |
725 | |
dc459aad |
726 | open IN, $TEST or warn "$0: cannot read $TEST: $!" ; |
3887d568 |
727 | local $/; |
728 | my $a = <IN>; |
729 | my $b = <IN>; |
09f04786 |
730 | |
731 | ok tainted($a) && tainted($b) && !defined($b); |
732 | |
27c9684d |
733 | close IN; |
3887d568 |
734 | } |
27c9684d |
735 | |
736 | { |
737 | # bug id 20001004.007 |
738 | |
dc459aad |
739 | open IN, $TEST or warn "$0: cannot read $TEST: $!" ; |
27c9684d |
740 | my $a = <IN>; |
741 | |
742 | my $c = { a => 42, |
743 | b => $a }; |
09f04786 |
744 | |
745 | ok !tainted($c->{a}) && tainted($c->{b}); |
746 | |
27c9684d |
747 | |
748 | my $d = { a => $a, |
749 | b => 42 }; |
09f04786 |
750 | ok tainted($d->{a}) && !tainted($d->{b}); |
751 | |
27c9684d |
752 | |
753 | my $e = { a => 42, |
754 | b => { c => $a, d => 42 } }; |
09f04786 |
755 | ok !tainted($e->{a}) && |
756 | !tainted($e->{b}) && |
757 | tainted($e->{b}->{c}) && |
758 | !tainted($e->{b}->{d}); |
27c9684d |
759 | |
760 | close IN; |
761 | } |
762 | |
b94c04ac |
763 | { |
764 | # bug id 20010519.003 |
765 | |
248c32c0 |
766 | BEGIN { |
767 | use vars qw($has_fcntl); |
768 | eval { require Fcntl; import Fcntl; }; |
769 | unless ($@) { |
770 | $has_fcntl = 1; |
771 | } |
772 | } |
b94c04ac |
773 | |
09f04786 |
774 | SKIP: { |
775 | skip "no Fcntl", 18 unless $has_fcntl; |
776 | |
248c32c0 |
777 | my $evil = "foo" . $TAINT; |
778 | |
779 | eval { sysopen(my $ro, $evil, &O_RDONLY) }; |
09f04786 |
780 | test $@ !~ /^Insecure dependency/, $@; |
248c32c0 |
781 | |
782 | eval { sysopen(my $wo, $evil, &O_WRONLY) }; |
09f04786 |
783 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
784 | |
785 | eval { sysopen(my $rw, $evil, &O_RDWR) }; |
09f04786 |
786 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
787 | |
788 | eval { sysopen(my $ap, $evil, &O_APPEND) }; |
09f04786 |
789 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
790 | |
791 | eval { sysopen(my $cr, $evil, &O_CREAT) }; |
09f04786 |
792 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
793 | |
794 | eval { sysopen(my $tr, $evil, &O_TRUNC) }; |
09f04786 |
795 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
796 | |
09f04786 |
797 | eval { sysopen(my $ro, "foo", &O_RDONLY | $TAINT0) }; |
798 | test $@ !~ /^Insecure dependency/, $@; |
248c32c0 |
799 | |
09f04786 |
800 | eval { sysopen(my $wo, "foo", &O_WRONLY | $TAINT0) }; |
801 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
802 | |
09f04786 |
803 | eval { sysopen(my $rw, "foo", &O_RDWR | $TAINT0) }; |
804 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
805 | |
09f04786 |
806 | eval { sysopen(my $ap, "foo", &O_APPEND | $TAINT0) }; |
807 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
808 | |
09f04786 |
809 | eval { sysopen(my $cr, "foo", &O_CREAT | $TAINT0) }; |
810 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
811 | |
09f04786 |
812 | eval { sysopen(my $tr, "foo", &O_TRUNC | $TAINT0) }; |
813 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
814 | |
09f04786 |
815 | eval { sysopen(my $ro, "foo", &O_RDONLY, $TAINT0) }; |
816 | test $@ !~ /^Insecure dependency/, $@; |
248c32c0 |
817 | |
09f04786 |
818 | eval { sysopen(my $wo, "foo", &O_WRONLY, $TAINT0) }; |
819 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
820 | |
09f04786 |
821 | eval { sysopen(my $rw, "foo", &O_RDWR, $TAINT0) }; |
822 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
823 | |
09f04786 |
824 | eval { sysopen(my $ap, "foo", &O_APPEND, $TAINT0) }; |
825 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
826 | |
09f04786 |
827 | eval { sysopen(my $cr, "foo", &O_CREAT, $TAINT0) }; |
828 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
829 | |
09f04786 |
830 | eval { sysopen(my $tr, "foo", &O_TRUNC, $TAINT0) }; |
831 | test $@ =~ /^Insecure dependency/, $@; |
248c32c0 |
832 | |
833 | unlink("foo"); # not unlink($evil), because that would fail... |
834 | } |
b94c04ac |
835 | } |
836 | |
65811bc3 |
837 | { |
838 | # bug 20010526.004 |
839 | |
840 | use warnings; |
841 | |
09f04786 |
842 | my $saw_warning = 0; |
843 | local $SIG{__WARN__} = sub { $saw_warning = 1 }; |
65811bc3 |
844 | |
845 | sub fmi { |
846 | my $divnum = shift()/1; |
847 | sprintf("%1.1f\n", $divnum); |
848 | } |
849 | |
850 | fmi(21 . $TAINT); |
851 | fmi(37); |
852 | fmi(248); |
853 | |
09f04786 |
854 | test !$saw_warning; |
65811bc3 |
855 | } |
856 | |
9e1b5a4e |
857 | |
858 | { |
859 | # Bug ID 20010730.010 |
860 | |
861 | my $i = 0; |
862 | |
863 | sub Tie::TIESCALAR { |
864 | my $class = shift; |
865 | my $arg = shift; |
866 | |
867 | bless \$arg => $class; |
868 | } |
869 | |
870 | sub Tie::FETCH { |
871 | $i ++; |
872 | ${$_ [0]} |
873 | } |
874 | |
875 | |
876 | package main; |
877 | |
878 | my $bar = "The Big Bright Green Pleasure Machine"; |
879 | taint_these $bar; |
880 | tie my ($foo), Tie => $bar; |
881 | |
882 | my $baz = $foo; |
883 | |
09f04786 |
884 | ok $i == 1; |
9e1b5a4e |
885 | } |
886 | |
8852b6d2 |
887 | { |
888 | # Check that all environment variables are tainted. |
889 | my @untainted; |
890 | while (my ($k, $v) = each %ENV) { |
891 | if (!tainted($v) && |
eb25aaf6 |
892 | # These we have explicitly untainted or set earlier. |
893 | $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) { |
8852b6d2 |
894 | push @untainted, "# '$k' = '$v'\n"; |
895 | } |
896 | } |
09f04786 |
897 | test @untainted == 0, "untainted:\n @untainted"; |
8852b6d2 |
898 | } |
9e1b5a4e |
899 | |
900 | |
9aa05f58 |
901 | ok( ${^TAINT} == 1, '$^TAINT is on' ); |
7c36658b |
902 | |
903 | eval { ${^TAINT} = 0 }; |
904 | ok( ${^TAINT}, '$^TAINT is not assignable' ); |
905 | ok( $@ =~ /^Modification of a read-only value attempted/, |
c212f17f |
906 | 'Assigning to ${^TAINT} fails' ); |
7c36658b |
907 | |
e08e52cf |
908 | { |
909 | # bug 20011111.105 |
910 | |
911 | my $re1 = qr/x$TAINT/; |
09f04786 |
912 | test tainted $re1; |
e08e52cf |
913 | |
914 | my $re2 = qr/^$re1\z/; |
09f04786 |
915 | test tainted $re2; |
e08e52cf |
916 | |
917 | my $re3 = "$re2"; |
09f04786 |
918 | test tainted $re3; |
e08e52cf |
919 | } |
52a55424 |
920 | |
09f04786 |
921 | SKIP: { |
922 | skip "system {} has different semantics on Win32", 1 if $Is_MSWin32; |
923 | |
52a55424 |
924 | # bug 20010221.005 |
925 | local $ENV{PATH} .= $TAINT; |
926 | eval { system { "echo" } "/arg0", "arg1" }; |
09f04786 |
927 | test $@ =~ /^Insecure \$ENV/; |
52a55424 |
928 | } |
09f04786 |
929 | |
930 | TODO: { |
931 | todo_skip 'tainted %ENV warning occludes tainted arguments warning', 22 |
932 | if $Is_VMS; |
933 | |
934 | # bug 20020208.005 plus some single arg exec/system extras |
06bd1802 |
935 | my $err = qr/^Insecure dependency/ ; |
09f04786 |
936 | test !eval { exec $TAINT, $TAINT }, 'exec'; |
937 | test $@ =~ $err, $@; |
938 | test !eval { exec $TAINT $TAINT }, 'exec'; |
939 | test $@ =~ $err, $@; |
940 | test !eval { exec $TAINT $TAINT, $TAINT }, 'exec'; |
941 | test $@ =~ $err, $@; |
942 | test !eval { exec $TAINT 'notaint' }, 'exec'; |
943 | test $@ =~ $err, $@; |
944 | test !eval { exec {'notaint'} $TAINT }, 'exec'; |
945 | test $@ =~ $err, $@; |
946 | |
947 | test !eval { system $TAINT, $TAINT }, 'system'; |
948 | test $@ =~ $err, $@; |
949 | test !eval { system $TAINT $TAINT }, 'system'; |
950 | test $@ =~ $err, $@; |
951 | test !eval { system $TAINT $TAINT, $TAINT }, 'system'; |
952 | test $@ =~ $err, $@; |
953 | test !eval { system $TAINT 'notaint' }, 'system'; |
954 | test $@ =~ $err, $@; |
955 | test !eval { system {'notaint'} $TAINT }, 'system'; |
956 | test $@ =~ $err, $@; |
957 | |
958 | eval { |
959 | no warnings; |
960 | system("lskdfj does not exist","with","args"); |
961 | }; |
962 | test !$@; |
963 | |
964 | SKIP: { |
965 | skip "no exec() on MacOS Classic" if $Is_MacOS; |
966 | |
967 | eval { |
968 | no warnings; |
969 | exec("lskdfj does not exist","with","args"); |
970 | }; |
971 | test !$@; |
63c6dcc1 |
972 | } |
6c8794e1 |
973 | |
974 | # If you add tests here update also the above skip block for VMS. |
bbd7eb8a |
975 | } |
a8c7c11a |
976 | |
977 | { |
978 | # [ID 20020704.001] taint propagation failure |
979 | use re 'taint'; |
980 | $TAINT =~ /(.*)/; |
09f04786 |
981 | test tainted(my $foo = $1); |
a8c7c11a |
982 | } |
7b756e0a |
983 | |
984 | { |
c038024b |
985 | # [perl #24291] this used to dump core |
986 | our %nonmagicalenv = ( PATH => "util" ); |
7b756e0a |
987 | local *ENV = \%nonmagicalenv; |
988 | eval { system("lskdfj"); }; |
09f04786 |
989 | test $@ =~ /^%ENV is aliased to another variable while running with -T switch/; |
c038024b |
990 | local *ENV = *nonmagicalenv; |
7b756e0a |
991 | eval { system("lskdfj"); }; |
09f04786 |
992 | test $@ =~ /^%ENV is aliased to %nonmagicalenv while running with -T switch/; |
7b756e0a |
993 | } |
0b4182de |
994 | { |
995 | # [perl #24248] |
996 | $TAINT =~ /(.*)/; |
09f04786 |
997 | test !tainted($1); |
0b4182de |
998 | my $notaint = $1; |
09f04786 |
999 | test !tainted($notaint); |
0b4182de |
1000 | |
1001 | my $l; |
1002 | $notaint =~ /($notaint)/; |
1003 | $l = $1; |
09f04786 |
1004 | test !tainted($1); |
1005 | test !tainted($l); |
0b4182de |
1006 | $notaint =~ /($TAINT)/; |
1007 | $l = $1; |
09f04786 |
1008 | test tainted($1); |
1009 | test tainted($l); |
0b4182de |
1010 | |
1011 | $TAINT =~ /($notaint)/; |
1012 | $l = $1; |
09f04786 |
1013 | test !tainted($1); |
1014 | test !tainted($l); |
0b4182de |
1015 | $TAINT =~ /($TAINT)/; |
1016 | $l = $1; |
09f04786 |
1017 | test tainted($1); |
1018 | test tainted($l); |
0b4182de |
1019 | |
1020 | my $r; |
1021 | ($r = $TAINT) =~ /($notaint)/; |
09f04786 |
1022 | test !tainted($1); |
0b4182de |
1023 | ($r = $TAINT) =~ /($TAINT)/; |
09f04786 |
1024 | test tainted($1); |
3511154c |
1025 | |
1026 | # [perl #24674] |
1027 | # accessing $^O shoudn't taint it as a side-effect; |
1028 | # assigning tainted data to it is now an error |
1029 | |
09f04786 |
1030 | test !tainted($^O); |
3511154c |
1031 | if (!$^X) { } elsif ($^O eq 'bar') { } |
09f04786 |
1032 | test !tainted($^O); |
3511154c |
1033 | eval '$^O = $^X'; |
09f04786 |
1034 | test $@ =~ /Insecure dependency in/; |
0b4182de |
1035 | } |
23634c10 |
1036 | |
1037 | EFFECTIVELY_CONSTANTS: { |
1038 | my $tainted_number = 12 + $TAINT0; |
09f04786 |
1039 | test tainted( $tainted_number ); |
23634c10 |
1040 | |
1041 | # Even though it's always 0, it's still tainted |
1042 | my $tainted_product = $tainted_number * 0; |
09f04786 |
1043 | test tainted( $tainted_product ); |
1044 | test $tainted_product == 0; |
23634c10 |
1045 | } |
1046 | |
1047 | TERNARY_CONDITIONALS: { |
1048 | my $tainted_true = $TAINT . "blah blah blah"; |
1049 | my $tainted_false = $TAINT0; |
09f04786 |
1050 | test tainted( $tainted_true ); |
1051 | test tainted( $tainted_false ); |
23634c10 |
1052 | |
1053 | my $result = $tainted_true ? "True" : "False"; |
09f04786 |
1054 | test $result eq "True"; |
1055 | test !tainted( $result ); |
23634c10 |
1056 | |
1057 | $result = $tainted_false ? "True" : "False"; |
09f04786 |
1058 | test $result eq "False"; |
1059 | test !tainted( $result ); |
23634c10 |
1060 | |
1061 | my $untainted_whatever = "The Fabulous Johnny Cash"; |
1062 | my $tainted_whatever = "Soft Cell" . $TAINT; |
1063 | |
1064 | $result = $tainted_true ? $tainted_whatever : $untainted_whatever; |
09f04786 |
1065 | test $result eq "Soft Cell"; |
1066 | test tainted( $result ); |
23634c10 |
1067 | |
1068 | $result = $tainted_false ? $tainted_whatever : $untainted_whatever; |
09f04786 |
1069 | test $result eq "The Fabulous Johnny Cash"; |
1070 | test !tainted( $result ); |
23634c10 |
1071 | } |
65814f21 |
1072 | |
1073 | { |
1074 | # rt.perl.org 5900 $1 remains tainted if... |
1075 | # 1) The regular expression contains a scalar variable AND |
1076 | # 2) The regular expression appears in an elsif clause |
1077 | |
1078 | my $foo = "abcdefghi" . $TAINT; |
1079 | |
1080 | my $valid_chars = 'a-z'; |
1081 | if ( $foo eq '' ) { |
1082 | } |
1083 | elsif ( $foo =~ /([$valid_chars]+)/o ) { |
1084 | test not tainted $1; |
1085 | } |
1086 | |
1087 | if ( $foo eq '' ) { |
1088 | } |
1089 | elsif ( my @bar = $foo =~ /([$valid_chars]+)/o ) { |
1090 | test not any_tainted @bar; |
1091 | } |
1092 | } |
0a9c116b |
1093 | |
1094 | # at scope exit, a restored localised value should have its old |
1095 | # taint status, not the taint status of the current statement |
1096 | |
1097 | { |
1098 | our $x99 = $^X; |
1099 | test tainted $x99; |
1100 | |
1101 | $x99 = ''; |
1102 | test not tainted $x99; |
1103 | |
1104 | my $c = do { local $x99; $^X }; |
1105 | test not tainted $x99; |
1106 | } |
1107 | { |
1108 | our $x99 = $^X; |
1109 | test tainted $x99; |
1110 | |
1111 | my $c = do { local $x99; '' }; |
1112 | test tainted $x99; |
1113 | } |
1114 | |
27cc343c |
1115 | # an mg_get of a tainted value during localization shouldn't taint the |
1116 | # statement |
1117 | |
1118 | { |
1119 | eval { local $0, eval '1' }; |
1120 | test $@ eq ''; |
1121 | } |
e26a4975 |
1122 | |
1123 | # [perl #8262] //g loops infinitely on tainted data |
1124 | |
1125 | { |
1126 | my @a; |
c7dd0d64 |
1127 | local $::TODO = 1; |
e26a4975 |
1128 | $a[0] = $^X; |
1129 | my $i = 0; |
1130 | while($a[0]=~ m/(.)/g ) { |
1131 | last if $i++ > 10000; |
1132 | } |
86f12da2 |
1133 | cmp_ok $i, '<', 10000, "infinite m//g"; |
e26a4975 |
1134 | } |
0aa395f8 |
1135 | |
1136 | SKIP: |
1137 | { |
1138 | my $got_dualvar; |
1139 | eval 'use Scalar::Util "dualvar"; $got_dualvar++'; |
1140 | skip "No Scalar::Util::dualvar" unless $got_dualvar; |
1141 | my $a = Scalar::Util::dualvar(3, $^X); |
1142 | my $b = $a + 5; |
1143 | is ($b, 8, "Arithmetic on tainted dualvars works"); |
1144 | } |
f27977c3 |
1145 | |
1146 | # opening '|-' should not trigger $ENV{PATH} check |
1147 | |
1148 | { |
1149 | SKIP: { |
1150 | skip "fork() is not available", 3 unless $Config{'d_fork'}; |
b4fda7a3 |
1151 | skip "opening |- is not stable on threaded OpenBSD with taint", 3 |
1152 | if $Config{useithreads} && $Is_OpenBSD; |
f27977c3 |
1153 | |
1154 | $ENV{'PATH'} = $TAINT; |
1155 | local $SIG{'PIPE'} = 'IGNORE'; |
1156 | eval { |
1157 | my $pid = open my $pipe, '|-'; |
1158 | if (!defined $pid) { |
1159 | die "open failed: $!"; |
1160 | } |
1161 | if (!$pid) { |
1162 | kill 'KILL', $$; # child suicide |
1163 | } |
1164 | close $pipe; |
1165 | }; |
1166 | test $@ !~ /Insecure \$ENV/, 'fork triggers %ENV check'; |
1167 | test $@ eq '', 'pipe/fork/open/close failed'; |
1168 | eval { |
1169 | open my $pipe, "|$Invoke_Perl -e 1"; |
1170 | close $pipe; |
1171 | }; |
1172 | test $@ =~ /Insecure \$ENV/, 'popen neglects %ENV check'; |
1173 | } |
1174 | } |
5d121f7f |
1175 | |
1176 | { |
1177 | package AUTOLOAD_TAINT; |
1178 | sub AUTOLOAD { |
1179 | our $AUTOLOAD; |
1180 | return if $AUTOLOAD =~ /DESTROY/; |
1181 | if ($AUTOLOAD =~ /untainted/) { |
1182 | main::ok(!main::tainted($AUTOLOAD), '$AUTOLOAD can be untainted'); |
1183 | } else { |
1184 | main::ok(main::tainted($AUTOLOAD), '$AUTOLOAD can be tainted'); |
1185 | } |
1186 | } |
1187 | |
1188 | package main; |
1189 | my $o = bless [], 'AUTOLOAD_TAINT'; |
1190 | $o->$TAINT; |
1191 | $o->untainted; |
1192 | } |
1193 | |
20ee07fb |
1194 | { |
1195 | # tests for tainted format in s?printf |
1196 | eval { printf($TAINT . "# %s\n", "foo") }; |
1197 | like($@, qr/^Insecure dependency in printf/, q/printf doesn't like tainted formats/); |
1198 | eval { printf("# %s\n", $TAINT . "foo") }; |
1199 | ok(!$@, q/printf accepts other tainted args/); |
1200 | eval { sprintf($TAINT . "# %s\n", "foo") }; |
1201 | like($@, qr/^Insecure dependency in sprintf/, q/sprintf doesn't like tainted formats/); |
1202 | eval { sprintf("# %s\n", $TAINT . "foo") }; |
1203 | ok(!$@, q/sprintf accepts other tainted args/); |
1204 | } |
085bde85 |
1205 | |
1206 | { |
1207 | # 40708 |
1208 | my $n = 7e9; |
1209 | 8e9 - $n; |
1210 | |
1211 | my $val = $n; |
1212 | is ($val, '7000000000', 'Assignment to untainted variable'); |
1213 | $val = $TAINT; |
1214 | $val = $n; |
1215 | is ($val, '7000000000', 'Assignment to tainted variable'); |
1216 | } |
5e500fc8 |
1217 | |
1218 | { |
5e500fc8 |
1219 | my $val = 0; |
1220 | my $tainted = '1' . $TAINT; |
1221 | eval '$val = eval $tainted;'; |
1222 | is ($val, 0, "eval doesn't like tainted strings"); |
1223 | like ($@, qr/^Insecure dependency in eval/); |
1224 | |
abb7fb96 |
1225 | # Rather nice code to get a tainted undef by from Rick Delaney |
1226 | open FH, "test.pl" or die $!; |
af2d3def |
1227 | seek FH, 0, 2 or die $!; |
1228 | $tainted = <FH>; |
5e500fc8 |
1229 | |
1230 | eval 'eval $tainted'; |
1231 | like ($@, qr/^Insecure dependency in eval/); |
1232 | } |