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'; |
12 | @INC = '../lib' if -d '../lib'; |
13 | } |
14 | |
15 | use strict; |
16 | use Config; |
17 | |
9d116dd7 |
18 | # We do not want the whole taint.t to fail |
19 | # just because Errno possibly failing. |
20 | eval { require Errno; import Errno }; |
21 | |
1e422769 |
22 | my $Is_VMS = $^O eq 'VMS'; |
68dc0745 |
23 | my $Is_MSWin32 = $^O eq 'MSWin32'; |
39e571d4 |
24 | my $Is_Dos = $^O eq 'dos'; |
68dc0745 |
25 | my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.' : |
26 | $Is_MSWin32 ? '.\perl' : './perl'; |
c90c0ff4 |
27 | my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/; |
7bac28a0 |
28 | |
1e422769 |
29 | if ($Is_VMS) { |
7bac28a0 |
30 | my (%old, $x); |
31 | for $x ('DCL$PATH', @MoreEnv) { |
32 | ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x}; |
33 | } |
1e422769 |
34 | eval <<EndOfCleanup; |
35 | END { |
36 | \$ENV{PATH} = ''; |
37 | warn "# Note: logical name 'PATH' may have been deleted\n"; |
7bac28a0 |
38 | @ENV{keys %old} = values %old; |
1e422769 |
39 | } |
40 | EndOfCleanup |
41 | } |
42 | |
43 | # Sources of taint: |
44 | # The empty tainted value, for tainting strings |
45 | my $TAINT = substr($^X, 0, 0); |
46 | # A tainted zero, useful for tainting numbers |
47 | my $TAINT0 = 0 + $TAINT; |
48 | |
49 | # This taints each argument passed. All must be lvalues. |
50 | # Side effect: It also stringifies them. :-( |
51 | sub taint_these (@) { |
52 | for (@_) { $_ .= $TAINT } |
53 | } |
54 | |
55 | # How to identify taint when you see it |
56 | sub any_tainted (@) { |
57 | not eval { join("",@_), kill 0; 1 }; |
58 | } |
59 | sub tainted ($) { |
60 | any_tainted @_; |
61 | } |
62 | sub all_tainted (@) { |
63 | for (@_) { return 0 unless tainted $_ } |
64 | 1; |
65 | } |
66 | |
67 | sub test ($$;$) { |
68 | my($serial, $boolean, $diag) = @_; |
69 | if ($boolean) { |
70 | print "ok $serial\n"; |
71 | } else { |
72 | print "not ok $serial\n"; |
73 | for (split m/^/m, $diag) { |
74 | print "# $_"; |
75 | } |
9607fc9c |
76 | print "\n" unless |
1e422769 |
77 | $diag eq '' |
78 | or substr($diag, -1) eq "\n"; |
79 | } |
80 | } |
81 | |
82 | # We need an external program to call. |
5aabfad6 |
83 | my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : "./echo$$"); |
1e422769 |
84 | END { unlink $ECHO } |
85 | open PROG, "> $ECHO" or die "Can't create $ECHO: $!"; |
86 | print PROG 'print "@ARGV\n"', "\n"; |
87 | close PROG; |
88 | my $echo = "$Invoke_Perl $ECHO"; |
89 | |
b3eb6a9b |
90 | print "1..149\n"; |
1e422769 |
91 | |
92 | # First, let's make sure that Perl is checking the dangerous |
93 | # environment variables. Maybe they aren't set yet, so we'll |
94 | # taint them ourselves. |
95 | { |
96 | $ENV{'DCL$PATH'} = '' if $Is_VMS; |
97 | |
7bac28a0 |
98 | $ENV{PATH} = ''; |
c90c0ff4 |
99 | delete @ENV{@MoreEnv}; |
7bac28a0 |
100 | $ENV{TERM} = 'dumb'; |
101 | |
102 | test 1, eval { `$echo 1` } eq "1\n"; |
103 | |
39e571d4 |
104 | if ($Is_MSWin32 || $Is_VMS || $Is_Dos) { |
7bac28a0 |
105 | print "# Environment tainting tests skipped\n"; |
c90c0ff4 |
106 | for (2..5) { print "ok $_\n" } |
5aabfad6 |
107 | } |
108 | else { |
7bac28a0 |
109 | my @vars = ('PATH', @MoreEnv); |
110 | while (my $v = $vars[0]) { |
111 | local $ENV{$v} = $TAINT; |
112 | last if eval { `$echo 1` }; |
113 | last unless $@ =~ /^Insecure \$ENV{$v}/; |
114 | shift @vars; |
115 | } |
116 | test 2, !@vars, "\$$vars[0]"; |
c90c0ff4 |
117 | |
118 | # tainted $TERM is unsafe only if it contains metachars |
119 | local $ENV{TERM}; |
120 | $ENV{TERM} = 'e=mc2'; |
121 | test 3, eval { `$echo 1` } eq "1\n"; |
122 | $ENV{TERM} = 'e=mc2' . $TAINT; |
123 | test 4, eval { `$echo 1` } eq ''; |
124 | test 5, $@ =~ /^Insecure \$ENV{TERM}/, $@; |
5aabfad6 |
125 | } |
7bac28a0 |
126 | |
9607fc9c |
127 | my $tmp; |
48c036b1 |
128 | if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_Dos) { |
129 | print "# all directories are writeable\n"; |
130 | } |
131 | else { |
9607fc9c |
132 | $tmp = (grep { defined and -d and (stat _)[2] & 2 } |
133 | qw(/tmp /var/tmp /usr/tmp /sys$scratch), |
134 | @ENV{qw(TMP TEMP)})[0] |
135 | or print "# can't find world-writeable directory to test PATH\n"; |
136 | } |
137 | |
385588b3 |
138 | if ($tmp) { |
7bac28a0 |
139 | local $ENV{PATH} = $tmp; |
c90c0ff4 |
140 | test 6, eval { `$echo 1` } eq ''; |
141 | test 7, $@ =~ /^Insecure directory in \$ENV{PATH}/, $@; |
1e422769 |
142 | } |
143 | else { |
fac76ed7 |
144 | for (6..7) { print "ok $_ # Skipped: all directories are writeable\n" } |
1e422769 |
145 | } |
146 | |
1e422769 |
147 | if ($Is_VMS) { |
148 | $ENV{'DCL$PATH'} = $TAINT; |
c90c0ff4 |
149 | test 8, eval { `$echo 1` } eq ''; |
150 | test 9, $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@; |
9607fc9c |
151 | if ($tmp) { |
152 | $ENV{'DCL$PATH'} = $tmp; |
c90c0ff4 |
153 | test 10, eval { `$echo 1` } eq ''; |
154 | test 11, $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@; |
9607fc9c |
155 | } |
156 | else { |
fac76ed7 |
157 | for (10..11) { print "ok $_ # Skipped: can't find world-writeable directory to test DCL\$PATH\n" } |
9607fc9c |
158 | } |
1e422769 |
159 | $ENV{'DCL$PATH'} = ''; |
160 | } |
161 | else { |
fac76ed7 |
162 | for (8..11) { print "ok $_ # Skipped: This is not VMS\n"; } |
1e422769 |
163 | } |
164 | } |
165 | |
166 | # Let's see that we can taint and untaint as needed. |
167 | { |
168 | my $foo = $TAINT; |
c90c0ff4 |
169 | test 12, tainted $foo; |
9607fc9c |
170 | |
171 | # That was a sanity check. If it failed, stop the insanity! |
172 | die "Taint checks don't seem to be enabled" unless tainted $foo; |
1e422769 |
173 | |
174 | $foo = "foo"; |
c90c0ff4 |
175 | test 13, not tainted $foo; |
1e422769 |
176 | |
177 | taint_these($foo); |
c90c0ff4 |
178 | test 14, tainted $foo; |
1e422769 |
179 | |
180 | my @list = 1..10; |
c90c0ff4 |
181 | test 15, not any_tainted @list; |
1e422769 |
182 | taint_these @list[1,3,5,7,9]; |
c90c0ff4 |
183 | test 16, any_tainted @list; |
184 | test 17, all_tainted @list[1,3,5,7,9]; |
185 | test 18, not any_tainted @list[0,2,4,6,8]; |
1e422769 |
186 | |
187 | ($foo) = $foo =~ /(.+)/; |
c90c0ff4 |
188 | test 19, not tainted $foo; |
1e422769 |
189 | |
190 | $foo = $1 if ('bar' . $TAINT) =~ /(.+)/; |
c90c0ff4 |
191 | test 20, not tainted $foo; |
192 | test 21, $foo eq 'bar'; |
1e422769 |
193 | |
b3eb6a9b |
194 | { |
195 | use re 'taint'; |
196 | |
197 | ($foo) = ('bar' . $TAINT) =~ /(.+)/; |
198 | test 22, tainted $foo; |
199 | test 23, $foo eq 'bar'; |
200 | |
201 | $foo = $1 if ('bar' . $TAINT) =~ /(.+)/; |
202 | test 24, tainted $foo; |
203 | test 25, $foo eq 'bar'; |
204 | } |
205 | |
206 | $foo = $1 if 'bar' =~ /(.+)$TAINT/; |
207 | test 26, tainted $foo; |
208 | test 27, $foo eq 'bar'; |
48c036b1 |
209 | |
1e422769 |
210 | my $pi = 4 * atan2(1,1) + $TAINT0; |
b3eb6a9b |
211 | test 28, tainted $pi; |
1e422769 |
212 | |
213 | ($pi) = $pi =~ /(\d+\.\d+)/; |
b3eb6a9b |
214 | test 29, not tainted $pi; |
215 | test 30, sprintf("%.5f", $pi) eq '3.14159'; |
1e422769 |
216 | } |
217 | |
218 | # How about command-line arguments? The problem is that we don't |
219 | # always get some, so we'll run another process with some. |
220 | { |
221 | my $arg = "./arg$$"; |
222 | open PROG, "> $arg" or die "Can't create $arg: $!"; |
223 | print PROG q{ |
224 | eval { join('', @ARGV), kill 0 }; |
225 | exit 0 if $@ =~ /^Insecure dependency/; |
226 | print "# Oops: \$@ was [$@]\n"; |
227 | exit 1; |
228 | }; |
229 | close PROG; |
230 | print `$Invoke_Perl "-T" $arg and some suspect arguments`; |
b3eb6a9b |
231 | test 31, !$?, "Exited with status $?"; |
1e422769 |
232 | unlink $arg; |
233 | } |
234 | |
235 | # Reading from a file should be tainted |
236 | { |
9607fc9c |
237 | my $file = './TEST'; |
b3eb6a9b |
238 | test 32, open(FILE, $file), "Couldn't open '$file': $!"; |
1e422769 |
239 | |
240 | my $block; |
241 | sysread(FILE, $block, 100); |
9607fc9c |
242 | my $line = <FILE>; |
1e422769 |
243 | close FILE; |
b3eb6a9b |
244 | test 33, tainted $block; |
245 | test 34, tainted $line; |
1e422769 |
246 | } |
247 | |
c90c0ff4 |
248 | # Globs should be forbidden, except under VMS, |
249 | # which doesn't spawn an external program. |
250 | if ($Is_VMS) { |
b3eb6a9b |
251 | for (35..36) { print "ok $_\n"; } |
c90c0ff4 |
252 | } |
253 | else { |
7bac28a0 |
254 | my @globs = eval { <*> }; |
b3eb6a9b |
255 | test 35, @globs == 0 && $@ =~ /^Insecure dependency/; |
1e422769 |
256 | |
7bac28a0 |
257 | @globs = eval { glob '*' }; |
b3eb6a9b |
258 | test 36, @globs == 0 && $@ =~ /^Insecure dependency/; |
1e422769 |
259 | } |
260 | |
261 | # Output of commands should be tainted |
262 | { |
263 | my $foo = `$echo abc`; |
b3eb6a9b |
264 | test 37, tainted $foo; |
1e422769 |
265 | } |
266 | |
267 | # Certain system variables should be tainted |
268 | { |
b3eb6a9b |
269 | test 38, all_tainted $^X, $0; |
1e422769 |
270 | } |
271 | |
272 | # Results of matching should all be untainted |
273 | { |
274 | my $foo = "abcdefghi" . $TAINT; |
b3eb6a9b |
275 | test 39, tainted $foo; |
1e422769 |
276 | |
277 | $foo =~ /def/; |
b3eb6a9b |
278 | test 40, not any_tainted $`, $&, $'; |
1e422769 |
279 | |
280 | $foo =~ /(...)(...)(...)/; |
b3eb6a9b |
281 | test 41, not any_tainted $1, $2, $3, $+; |
1e422769 |
282 | |
283 | my @bar = $foo =~ /(...)(...)(...)/; |
b3eb6a9b |
284 | test 42, not any_tainted @bar; |
1e422769 |
285 | |
b3eb6a9b |
286 | test 43, tainted $foo; # $foo should still be tainted! |
287 | test 44, $foo eq "abcdefghi"; |
1e422769 |
288 | } |
289 | |
290 | # Operations which affect files can't use tainted data. |
291 | { |
b3eb6a9b |
292 | test 45, eval { chmod 0, $TAINT } eq '', 'chmod'; |
293 | test 46, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
294 | |
9607fc9c |
295 | # There is no feature test in $Config{} for truncate, |
296 | # so we allow for the possibility that it's missing. |
b3eb6a9b |
297 | test 47, eval { truncate 'NoSuChFiLe', $TAINT0 } eq '', 'truncate'; |
298 | test 48, $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@; |
1e422769 |
299 | |
b3eb6a9b |
300 | test 49, eval { rename '', $TAINT } eq '', 'rename'; |
301 | test 50, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
302 | |
b3eb6a9b |
303 | test 51, eval { unlink $TAINT } eq '', 'unlink'; |
304 | test 52, $@ =~ /^Insecure dependency/, $@; |
9607fc9c |
305 | |
b3eb6a9b |
306 | test 53, eval { utime $TAINT } eq '', 'utime'; |
307 | test 54, $@ =~ /^Insecure dependency/, $@; |
48c036b1 |
308 | |
1e422769 |
309 | if ($Config{d_chown}) { |
b3eb6a9b |
310 | test 55, eval { chown -1, -1, $TAINT } eq '', 'chown'; |
311 | test 56, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
312 | } |
313 | else { |
b3eb6a9b |
314 | for (55..56) { print "ok $_ # Skipped: chown() is not available\n" } |
1e422769 |
315 | } |
316 | |
317 | if ($Config{d_link}) { |
b3eb6a9b |
318 | test 57, eval { link $TAINT, '' } eq '', 'link'; |
319 | test 58, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
320 | } |
321 | else { |
b3eb6a9b |
322 | for (57..58) { print "ok $_ # Skipped: link() is not available\n" } |
1e422769 |
323 | } |
324 | |
325 | if ($Config{d_symlink}) { |
b3eb6a9b |
326 | test 59, eval { symlink $TAINT, '' } eq '', 'symlink'; |
327 | test 60, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
328 | } |
329 | else { |
b3eb6a9b |
330 | for (59..60) { print "ok $_ # Skipped: symlink() is not available\n" } |
1e422769 |
331 | } |
332 | } |
333 | |
334 | # Operations which affect directories can't use tainted data. |
335 | { |
b3eb6a9b |
336 | test 61, eval { mkdir $TAINT0, $TAINT } eq '', 'mkdir'; |
337 | test 62, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
338 | |
b3eb6a9b |
339 | test 63, eval { rmdir $TAINT } eq '', 'rmdir'; |
340 | test 64, $@ =~ /^Insecure dependency/, $@; |
9607fc9c |
341 | |
b3eb6a9b |
342 | test 65, eval { chdir $TAINT } eq '', 'chdir'; |
343 | test 66, $@ =~ /^Insecure dependency/, $@; |
48c036b1 |
344 | |
1e422769 |
345 | if ($Config{d_chroot}) { |
b3eb6a9b |
346 | test 67, eval { chroot $TAINT } eq '', 'chroot'; |
347 | test 68, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
348 | } |
349 | else { |
b3eb6a9b |
350 | for (67..68) { print "ok $_ # Skipped: chroot() is not available\n" } |
1e422769 |
351 | } |
352 | } |
353 | |
354 | # Some operations using files can't use tainted data. |
355 | { |
356 | my $foo = "imaginary library" . $TAINT; |
b3eb6a9b |
357 | test 69, eval { require $foo } eq '', 'require'; |
358 | test 70, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
359 | |
360 | my $filename = "./taintB$$"; # NB: $filename isn't tainted! |
361 | END { unlink $filename if defined $filename } |
362 | $foo = $filename . $TAINT; |
363 | unlink $filename; # in any case |
364 | |
b3eb6a9b |
365 | test 71, eval { open FOO, $foo } eq '', 'open for read'; |
366 | test 72, $@ eq '', $@; # NB: This should be allowed |
9d116dd7 |
367 | |
368 | # Try first new style but allow also old style. |
369 | test 73, $!{ENOENT} || $! == 2 || ($Is_Dos && $! == 22); # File not found |
1e422769 |
370 | |
b3eb6a9b |
371 | test 74, eval { open FOO, "> $foo" } eq '', 'open for write'; |
372 | test 75, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
373 | } |
374 | |
375 | # Commands to the system can't use tainted data |
376 | { |
377 | my $foo = $TAINT; |
378 | |
379 | if ($^O eq 'amigaos') { |
b3eb6a9b |
380 | for (76..79) { print "ok $_ # Skipped: open('|') is not available\n" } |
1e422769 |
381 | } |
382 | else { |
b3eb6a9b |
383 | test 76, eval { open FOO, "| $foo" } eq '', 'popen to'; |
384 | test 77, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
385 | |
b3eb6a9b |
386 | test 78, eval { open FOO, "$foo |" } eq '', 'popen from'; |
387 | test 79, $@ =~ /^Insecure dependency/, $@; |
48c036b1 |
388 | } |
1e422769 |
389 | |
b3eb6a9b |
390 | test 80, eval { exec $TAINT } eq '', 'exec'; |
391 | test 81, $@ =~ /^Insecure dependency/, $@; |
9607fc9c |
392 | |
b3eb6a9b |
393 | test 82, eval { system $TAINT } eq '', 'system'; |
394 | test 83, $@ =~ /^Insecure dependency/, $@; |
48c036b1 |
395 | |
1e422769 |
396 | $foo = "*"; |
397 | taint_these $foo; |
398 | |
b3eb6a9b |
399 | test 84, eval { `$echo 1$foo` } eq '', 'backticks'; |
400 | test 85, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
401 | |
402 | if ($Is_VMS) { # wildcard expansion doesn't invoke shell, so is safe |
b3eb6a9b |
403 | test 86, join('', eval { glob $foo } ) ne '', 'globbing'; |
404 | test 87, $@ eq '', $@; |
1e422769 |
405 | } |
406 | else { |
b3eb6a9b |
407 | for (86..87) { print "ok $_ # Skipped: this is not VMS\n"; } |
1e422769 |
408 | } |
409 | } |
410 | |
411 | # Operations which affect processes can't use tainted data. |
412 | { |
b3eb6a9b |
413 | test 88, eval { kill 0, $TAINT } eq '', 'kill'; |
414 | test 89, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
415 | |
416 | if ($Config{d_setpgrp}) { |
b3eb6a9b |
417 | test 90, eval { setpgrp 0, $TAINT } eq '', 'setpgrp'; |
418 | test 91, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
419 | } |
420 | else { |
b3eb6a9b |
421 | for (90..91) { print "ok $_ # Skipped: setpgrp() is not available\n" } |
1e422769 |
422 | } |
423 | |
424 | if ($Config{d_setprior}) { |
b3eb6a9b |
425 | test 92, eval { setpriority 0, $TAINT, $TAINT } eq '', 'setpriority'; |
426 | test 93, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
427 | } |
428 | else { |
b3eb6a9b |
429 | for (92..93) { print "ok $_ # Skipped: setpriority() is not available\n" } |
1e422769 |
430 | } |
431 | } |
432 | |
433 | # Some miscellaneous operations can't use tainted data. |
434 | { |
435 | if ($Config{d_syscall}) { |
b3eb6a9b |
436 | test 94, eval { syscall $TAINT } eq '', 'syscall'; |
437 | test 95, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
438 | } |
439 | else { |
b3eb6a9b |
440 | for (94..95) { print "ok $_ # Skipped: syscall() is not available\n" } |
1e422769 |
441 | } |
442 | |
443 | { |
444 | my $foo = "x" x 979; |
445 | taint_these $foo; |
446 | local *FOO; |
447 | my $temp = "./taintC$$"; |
448 | END { unlink $temp } |
b3eb6a9b |
449 | test 96, open(FOO, "> $temp"), "Couldn't open $temp for write: $!"; |
1e422769 |
450 | |
b3eb6a9b |
451 | test 97, eval { ioctl FOO, $TAINT, $foo } eq '', 'ioctl'; |
452 | test 98, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
453 | |
454 | if ($Config{d_fcntl}) { |
b3eb6a9b |
455 | test 99, eval { fcntl FOO, $TAINT, $foo } eq '', 'fcntl'; |
456 | test 100, $@ =~ /^Insecure dependency/, $@; |
1e422769 |
457 | } |
458 | else { |
b3eb6a9b |
459 | for (99..100) { print "ok $_ # Skipped: fcntl() is not available\n" } |
1e422769 |
460 | } |
461 | |
462 | close FOO; |
463 | } |
464 | } |
465 | |
9607fc9c |
466 | # Some tests involving references |
1e422769 |
467 | { |
468 | my $foo = 'abc' . $TAINT; |
469 | my $fooref = \$foo; |
b3eb6a9b |
470 | test 101, not tainted $fooref; |
471 | test 102, tainted $$fooref; |
472 | test 103, tainted $foo; |
1e422769 |
473 | } |
54310121 |
474 | |
475 | # Some tests involving assignment |
476 | { |
477 | my $foo = $TAINT0; |
478 | my $bar = $foo; |
b3eb6a9b |
479 | test 104, all_tainted $foo, $bar; |
480 | test 105, tainted($foo = $bar); |
481 | test 106, tainted($bar = $bar); |
482 | test 107, tainted($bar += $bar); |
483 | test 108, tainted($bar -= $bar); |
484 | test 109, tainted($bar *= $bar); |
485 | test 110, tainted($bar++); |
486 | test 111, tainted($bar /= $bar); |
487 | test 112, tainted($bar += 0); |
488 | test 113, tainted($bar -= 2); |
489 | test 114, tainted($bar *= -1); |
490 | test 115, tainted($bar /= 1); |
491 | test 116, tainted($bar--); |
492 | test 117, $bar == 0; |
54310121 |
493 | } |
a1f49e72 |
494 | |
495 | # Test assignment and return of lists |
496 | { |
497 | my @foo = ("A", "tainted" . $TAINT, "B"); |
b3eb6a9b |
498 | test 118, not tainted $foo[0]; |
499 | test 119, tainted $foo[1]; |
500 | test 120, not tainted $foo[2]; |
a1f49e72 |
501 | my @bar = @foo; |
b3eb6a9b |
502 | test 121, not tainted $bar[0]; |
503 | test 122, tainted $bar[1]; |
504 | test 123, not tainted $bar[2]; |
a1f49e72 |
505 | my @baz = eval { "A", "tainted" . $TAINT, "B" }; |
b3eb6a9b |
506 | test 124, not tainted $baz[0]; |
507 | test 125, tainted $baz[1]; |
508 | test 126, not tainted $baz[2]; |
a1f49e72 |
509 | my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ]; |
b3eb6a9b |
510 | test 127, not tainted $plugh[0]; |
511 | test 128, tainted $plugh[1]; |
512 | test 129, not tainted $plugh[2]; |
a1f49e72 |
513 | my $nautilus = sub { "A", "tainted" . $TAINT, "B" }; |
b3eb6a9b |
514 | test 130, not tainted ((&$nautilus)[0]); |
515 | test 131, tainted ((&$nautilus)[1]); |
516 | test 132, not tainted ((&$nautilus)[2]); |
a1f49e72 |
517 | my @xyzzy = &$nautilus; |
b3eb6a9b |
518 | test 133, not tainted $xyzzy[0]; |
519 | test 134, tainted $xyzzy[1]; |
520 | test 135, not tainted $xyzzy[2]; |
a1f49e72 |
521 | my $red_october = sub { return "A", "tainted" . $TAINT, "B" }; |
b3eb6a9b |
522 | test 136, not tainted ((&$red_october)[0]); |
523 | test 137, tainted ((&$red_october)[1]); |
524 | test 138, not tainted ((&$red_october)[2]); |
a1f49e72 |
525 | my @corge = &$red_october; |
b3eb6a9b |
526 | test 139, not tainted $corge[0]; |
527 | test 140, tainted $corge[1]; |
528 | test 141, not tainted $corge[2]; |
a1f49e72 |
529 | } |
fb73857a |
530 | |
531 | # Test for system/library calls returning string data of dubious origin. |
532 | { |
533 | # No reliable %Config check for getpw* |
534 | if (eval { setpwent(); getpwent(); 1 }) { |
535 | setpwent(); |
536 | my @getpwent = getpwent(); |
537 | die "getpwent: $!\n" unless (@getpwent); |
b3eb6a9b |
538 | test 142,( not tainted $getpwent[0] |
fb73857a |
539 | and not tainted $getpwent[1] |
540 | and not tainted $getpwent[2] |
541 | and not tainted $getpwent[3] |
542 | and not tainted $getpwent[4] |
543 | and not tainted $getpwent[5] |
544 | and tainted $getpwent[6] # gecos |
545 | and not tainted $getpwent[7] |
546 | and not tainted $getpwent[8]); |
547 | endpwent(); |
548 | } else { |
b3eb6a9b |
549 | for (142) { print "ok $_ # Skipped: getpwent() is not available\n" } |
fb73857a |
550 | } |
551 | |
552 | if ($Config{d_readdir}) { # pretty hard to imagine not |
553 | local(*D); |
554 | opendir(D, "op") or die "opendir: $!\n"; |
555 | my $readdir = readdir(D); |
b3eb6a9b |
556 | test 143, tainted $readdir; |
fb73857a |
557 | closedir(OP); |
558 | } else { |
b3eb6a9b |
559 | for (143) { print "ok $_ # Skipped: readdir() is not available\n" } |
fb73857a |
560 | } |
561 | |
562 | if ($Config{d_readlink} && $Config{d_symlink}) { |
563 | my $symlink = "sl$$"; |
564 | unlink($symlink); |
565 | symlink("/something/naughty", $symlink) or die "symlink: $!\n"; |
566 | my $readlink = readlink($symlink); |
b3eb6a9b |
567 | test 144, tainted $readlink; |
fb73857a |
568 | unlink($symlink); |
569 | } else { |
b3eb6a9b |
570 | for (144) { print "ok $_ # Skipped: readlink() or symlink() is not available\n"; } |
fb73857a |
571 | } |
572 | } |
573 | |
574 | # test bitwise ops (regression bug) |
575 | { |
576 | my $why = "y"; |
577 | my $j = "x" | $why; |
b3eb6a9b |
578 | test 145, not tainted $j; |
fb73857a |
579 | $why = $TAINT."y"; |
580 | $j = "x" | $why; |
b3eb6a9b |
581 | test 146, tainted $j; |
fb73857a |
582 | } |
583 | |
48c036b1 |
584 | # test target of substitution (regression bug) |
585 | { |
586 | my $why = $TAINT."y"; |
587 | $why =~ s/y/z/; |
b3eb6a9b |
588 | test 147, tainted $why; |
48c036b1 |
589 | |
590 | my $z = "[z]"; |
591 | $why =~ s/$z/zee/; |
b3eb6a9b |
592 | test 148, tainted $why; |
48c036b1 |
593 | |
594 | $why =~ s/e/'-'.$$/ge; |
b3eb6a9b |
595 | test 149, tainted $why; |
48c036b1 |
596 | } |