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