Clean up 1_compile.t; move tests to more consistent
[p5sagit/p5-mst-13.2.git] / lib / File / Find / t / taint.t
CommitLineData
3fa6e24b 1#!./perl -T
2
3
4my %Expect_File = (); # what we expect for $_
5my %Expect_Name = (); # what we expect for $File::Find::name/fullname
6my %Expect_Dir = (); # what we expect for $File::Find::dir
87ba1cf0 7my ($cwd, $cwd_untainted);
3fa6e24b 8
8ddbe0db 9use Config;
10
3fa6e24b 11BEGIN {
12 chdir 't' if -d 't';
13 unshift @INC => '../lib';
14
15 for (keys %ENV) { # untaint ENV
8ddbe0db 16 ($ENV{$_}) = $ENV{$_} =~ /(.*)/;
3fa6e24b 17 }
8ddbe0db 18
19 # Remove insecure directories from PATH
20 my @path;
21 my $sep = $Config{path_sep};
22 foreach my $dir (split(/\Q$sep/,$ENV{'PATH'}))
23 {
24 ##
25 ## Match the directory taint tests in mg.c::Perl_magic_setenv()
26 ##
27 push(@path,$dir) unless (length($dir) >= 256
28 or
29 substr($dir,0,1) ne "/"
30 or
31 (stat $dir)[2] & 002);
32 }
33 $ENV{'PATH'} = join($sep,@path);
3fa6e24b 34}
35
87ba1cf0 36use Test::More tests => 45;
004cc508 37
87ba1cf0 38my $symlink_exists = eval { symlink("",""); 1 };
3fa6e24b 39
40use File::Find;
41use File::Spec;
42use Cwd;
a1b073b7 43
3fa6e24b 44cleanup();
45
87ba1cf0 46my $found;
47find({wanted => sub { $found = 1 if ($_ eq 'commonsense.t') },
48 untaint => 1, untaint_pattern => qr|^(.+)$|}, File::Spec->curdir);
49
50ok($found, 'commonsense.t found');
51$found = 0;
52
53finddepth({wanted => sub { $found = 1 if $_ eq 'commonsense.t'; },
54 untaint => 1, untaint_pattern => qr|^(.+)$|}, File::Spec->curdir);
55
56ok($found, 'commonsense.t found again');
3fa6e24b 57
58my $case = 2;
59my $FastFileTests_OK = 0;
60
61sub cleanup {
62 if (-d dir_path('for_find')) {
63 chdir(dir_path('for_find'));
64 }
65 if (-d dir_path('fa')) {
66 unlink file_path('fa', 'fa_ord'),
67 file_path('fa', 'fsl'),
68 file_path('fa', 'faa', 'faa_ord'),
69 file_path('fa', 'fab', 'fab_ord'),
70 file_path('fa', 'fab', 'faba', 'faba_ord'),
71 file_path('fb', 'fb_ord'),
72 file_path('fb', 'fba', 'fba_ord');
73 rmdir dir_path('fa', 'faa');
74 rmdir dir_path('fa', 'fab', 'faba');
75 rmdir dir_path('fa', 'fab');
76 rmdir dir_path('fa');
77 rmdir dir_path('fb', 'fba');
78 rmdir dir_path('fb');
79 chdir File::Spec->updir;
80 rmdir dir_path('for_find');
81 }
82}
83
84END {
85 cleanup();
86}
87
3fa6e24b 88sub touch {
87ba1cf0 89 ok( open(my $T,'>',$_[0]), "Opened $_[0] successfully" );
3fa6e24b 90}
91
92sub MkDir($$) {
87ba1cf0 93 ok( mkdir($_[0],$_[1]), "Created directory $_[0] successfully" );
3fa6e24b 94}
95
96sub wanted_File_Dir {
97 print "# \$File::Find::dir => '$File::Find::dir'\n";
98 print "# \$_ => '$_'\n";
99 s#\.$## if ($^O eq 'VMS' && $_ ne '.');
87ba1cf0 100 ok( $Expect_File{$_}, "Expected and found $File::Find::name" );
3fa6e24b 101 if ( $FastFileTests_OK ) {
102 delete $Expect_File{ $_}
103 unless ( $Expect_Dir{$_} && ! -d _ );
104 } else {
105 delete $Expect_File{$_}
106 unless ( $Expect_Dir{$_} && ! -d $_ );
107 }
108}
109
110sub wanted_File_Dir_prune {
111 &wanted_File_Dir;
112 $File::Find::prune=1 if $_ eq 'faba';
113}
114
3fa6e24b 115sub simple_wanted {
116 print "# \$File::Find::dir => '$File::Find::dir'\n";
117 print "# \$_ => '$_'\n";
118}
119
120
bb7dc48b 121# Use dir_path() to specify a directory path that's expected for
122# $File::Find::dir (%Expect_Dir). Also use it in file operations like
123# chdir, rmdir etc.
3fa6e24b 124#
bb7dc48b 125# dir_path() concatenates directory names to form a _relative_
126# directory path, independant from the platform it's run on, although
127# there are limitations. Don't try to create an absolute path,
128# because that may fail on operating systems that have the concept of
129# volume names (e.g. Mac OS). Be careful when you want to create an
130# updir path like ../fa (Unix) or ::fa: (Mac OS). Plain directory
131# names will work best. As a special case, you can pass it a "." as
132# first argument, to create a directory path like "./fa/dir" on
133# operating systems other than Mac OS (actually, Mac OS will ignore
134# the ".", if it's the first argument). If there's no second argument,
135# this function will return the empty string on Mac OS and the string
136# "./" otherwise.
3fa6e24b 137
138sub dir_path {
139 my $first_item = shift @_;
140
141 if ($first_item eq '.') {
142 if ($^O eq 'MacOS') {
143 return '' unless @_;
144 # ignore first argument; return a relative path
145 # with leading ":" and with trailing ":"
146 return File::Spec->catdir("", @_);
147 } else { # other OS
148 return './' unless @_;
149 my $path = File::Spec->catdir(@_);
150 # add leading "./"
151 $path = "./$path";
152 return $path;
153 }
154
155 } else { # $first_item ne '.'
156 return $first_item unless @_; # return plain filename
157 if ($^O eq 'MacOS') {
158 # relative path with leading ":" and with trailing ":"
159 return File::Spec->catdir("", $first_item, @_);
160 } else { # other OS
161 return File::Spec->catdir($first_item, @_);
162 }
163 }
164}
165
166
bb7dc48b 167# Use topdir() to specify a directory path that you want to pass to
168#find/finddepth Basically, topdir() does the same as dir_path() (see
169#above), except that there's no trailing ":" on Mac OS.
3fa6e24b 170
171sub topdir {
172 my $path = dir_path(@_);
173 $path =~ s/:$// if ($^O eq 'MacOS');
174 return $path;
175}
176
177
178# Use file_path() to specify a file path that's expected for $_ (%Expect_File).
179# Also suitable for file operations like unlink etc.
bb7dc48b 180
181# file_path() concatenates directory names (if any) and a filename to
182# form a _relative_ file path (the last argument is assumed to be a
183# file). It's independant from the platform it's run on, although
184# there are limitations (see the warnings for dir_path() above). As a
185# special case, you can pass it a "." as first argument, to create a
186# file path like "./fa/file" on operating systems other than Mac OS
187# (actually, Mac OS will ignore the ".", if it's the first
188# argument). If there's no second argument, this function will return
189# the empty string on Mac OS and the string "./" otherwise.
3fa6e24b 190
191sub file_path {
192 my $first_item = shift @_;
193
194 if ($first_item eq '.') {
195 if ($^O eq 'MacOS') {
196 return '' unless @_;
197 # ignore first argument; return a relative path
198 # with leading ":", but without trailing ":"
199 return File::Spec->catfile("", @_);
200 } else { # other OS
201 return './' unless @_;
202 my $path = File::Spec->catfile(@_);
203 # add leading "./"
204 $path = "./$path";
205 return $path;
206 }
207
208 } else { # $first_item ne '.'
209 return $first_item unless @_; # return plain filename
210 if ($^O eq 'MacOS') {
211 # relative path with leading ":", but without trailing ":"
212 return File::Spec->catfile("", $first_item, @_);
213 } else { # other OS
214 return File::Spec->catfile($first_item, @_);
215 }
216 }
217}
218
219
bb7dc48b 220# Use file_path_name() to specify a file path that's expected for
221# $File::Find::Name (%Expect_Name). Note: When the no_chdir => 1
222# option is in effect, $_ is the same as $File::Find::Name. In that
223# case, also use this function to specify a file path that's expected
224# for $_.
3fa6e24b 225#
bb7dc48b 226# Basically, file_path_name() does the same as file_path() (see
227# above), except that there's always a leading ":" on Mac OS, even for
228# plain file/directory names.
3fa6e24b 229
230sub file_path_name {
231 my $path = file_path(@_);
232 $path = ":$path" if (($^O eq 'MacOS') && ($path !~ /:/));
233 return $path;
234}
235
236
36841713 237MkDir( dir_path('for_find'), 0770 );
87ba1cf0 238ok( chdir( dir_path('for_find')), 'successful chdir() to for_find' );
3fa6e24b 239
240$cwd = cwd(); # save cwd
241( $cwd_untainted ) = $cwd =~ m|^(.+)$|; # untaint it
242
243MkDir( dir_path('fa'), 0770 );
244MkDir( dir_path('fb'), 0770 );
245touch( file_path('fb', 'fb_ord') );
246MkDir( dir_path('fb', 'fba'), 0770 );
247touch( file_path('fb', 'fba', 'fba_ord') );
87ba1cf0 248SKIP: {
249 skip "Creating symlink", 1, unless $symlink_exists;
3fa6e24b 250if ($^O eq 'MacOS') {
87ba1cf0 251 ok( symlink(':fb',':fa:fsl'), 'Created symbolic link' );
3fa6e24b 252} else {
87ba1cf0 253 ok( symlink('../fb','fa/fsl'), 'Created symbolic link' );
254}
3fa6e24b 255}
256touch( file_path('fa', 'fa_ord') );
257
258MkDir( dir_path('fa', 'faa'), 0770 );
259touch( file_path('fa', 'faa', 'faa_ord') );
260MkDir( dir_path('fa', 'fab'), 0770 );
261touch( file_path('fa', 'fab', 'fab_ord') );
262MkDir( dir_path('fa', 'fab', 'faba'), 0770 );
263touch( file_path('fa', 'fab', 'faba', 'faba_ord') );
264
3fa6e24b 265print "# check untainting (no follow)\n";
266
267# untainting here should work correctly
bb7dc48b 268
269%Expect_File = (File::Spec->curdir => 1, file_path('fsl') =>
270 1,file_path('fa_ord') => 1, file_path('fab') => 1,
271 file_path('fab_ord') => 1, file_path('faba') => 1,
3fa6e24b 272 file_path('faa') => 1, file_path('faa_ord') => 1);
273delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
274%Expect_Name = ();
bb7dc48b 275
276%Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1,
277 dir_path('fab') => 1, dir_path('faba') => 1,
3fa6e24b 278 dir_path('fb') => 1, dir_path('fba') => 1);
bb7dc48b 279
3fa6e24b 280delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists;
bb7dc48b 281
282File::Find::find( {wanted => \&wanted_File_Dir_prune, untaint => 1,
283 untaint_pattern => qr|^(.+)$|}, topdir('fa') );
284
87ba1cf0 285is(scalar keys %Expect_File, 0, 'Found all expected files');
3fa6e24b 286
287
288# don't untaint at all, should die
289%Expect_File = ();
290%Expect_Name = ();
291%Expect_Dir = ();
292undef $@;
293eval {File::Find::find( {wanted => \&simple_wanted}, topdir('fa') );};
87ba1cf0 294like( $@, qr|Insecure dependency|, 'Tainted directory causes death (good)' );
3fa6e24b 295chdir($cwd_untainted);
296
297
298# untaint pattern doesn't match, should die
299undef $@;
bb7dc48b 300
3fa6e24b 301eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1,
bb7dc48b 302 untaint_pattern => qr|^(NO_MATCH)$|},
303 topdir('fa') );};
304
87ba1cf0 305like( $@, qr|is still tainted|, 'Bad untaint pattern causes death (good)' );
3fa6e24b 306chdir($cwd_untainted);
307
308
309# untaint pattern doesn't match, should die when we chdir to cwd
004cc508 310print "# check untaint_skip (No follow)\n";
3fa6e24b 311undef $@;
bb7dc48b 312
313eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1,
314 untaint_skip => 1, untaint_pattern =>
315 qr|^(NO_MATCH)$|}, topdir('fa') );};
316
004cc508 317print "# $@" if $@;
318#$^D = 8;
87ba1cf0 319like( $@, qr|insecure cwd|, 'Bad untaint pattern causes death in cwd (good)' );
320
3fa6e24b 321chdir($cwd_untainted);
322
323
87ba1cf0 324SKIP: {
325 skip "Symbolic link tests", 17, unless $symlink_exists;
bb7dc48b 326 print "# --- symbolic link tests --- \n";
3fa6e24b 327 $FastFileTests_OK= 1;
328
329 print "# check untainting (follow)\n";
330
331 # untainting here should work correctly
332 # no_chdir is in effect, hence we use file_path_name to specify the expected paths for %Expect_File
bb7dc48b 333
334 %Expect_File = (file_path_name('fa') => 1,
335 file_path_name('fa','fa_ord') => 1,
336 file_path_name('fa', 'fsl') => 1,
337 file_path_name('fa', 'fsl', 'fb_ord') => 1,
338 file_path_name('fa', 'fsl', 'fba') => 1,
339 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
340 file_path_name('fa', 'fab') => 1,
341 file_path_name('fa', 'fab', 'fab_ord') => 1,
342 file_path_name('fa', 'fab', 'faba') => 1,
343 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
344 file_path_name('fa', 'faa') => 1,
345 file_path_name('fa', 'faa', 'faa_ord') => 1);
346
3fa6e24b 347 %Expect_Name = ();
bb7dc48b 348
349 %Expect_Dir = (dir_path('fa') => 1,
350 dir_path('fa', 'faa') => 1,
351 dir_path('fa', 'fab') => 1,
352 dir_path('fa', 'fab', 'faba') => 1,
353 dir_path('fb') => 1,
354 dir_path('fb', 'fba') => 1);
355
356 File::Find::find( {wanted => \&wanted_File_Dir, follow_fast => 1,
357 no_chdir => 1, untaint => 1, untaint_pattern =>
358 qr|^(.+)$| }, topdir('fa') );
359
87ba1cf0 360 is( scalar(keys %Expect_File), 0, 'Found all files in symlink test' );
3fa6e24b 361
362
363 # don't untaint at all, should die
364 undef $@;
bb7dc48b 365
366 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1},
367 topdir('fa') );};
368
87ba1cf0 369 like( $@, qr|Insecure dependency|, 'Not untainting causes death (good)' );
3fa6e24b 370 chdir($cwd_untainted);
371
372 # untaint pattern doesn't match, should die
373 undef $@;
bb7dc48b 374
375 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
376 untaint => 1, untaint_pattern =>
377 qr|^(NO_MATCH)$|}, topdir('fa') );};
378
87ba1cf0 379 like( $@, qr|is still tainted|, 'Bat untaint pattern causes death (good)' );
3fa6e24b 380 chdir($cwd_untainted);
381
382 # untaint pattern doesn't match, should die when we chdir to cwd
004cc508 383 print "# check untaint_skip (Follow)\n";
3fa6e24b 384 undef $@;
bb7dc48b 385
386 eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1,
387 untaint_skip => 1, untaint_pattern =>
388 qr|^(NO_MATCH)$|}, topdir('fa') );};
87ba1cf0 389 like( $@, qr|insecure cwd|, 'Cwd not untainted with bad pattern (good)' );
30c4d17d 390
3fa6e24b 391 chdir($cwd_untainted);
3fa6e24b 392}
393