8 : (skip_all => "A perl without taint support")
12 my %Expect_File = (); # what we expect for $_
13 my %Expect_Name = (); # what we expect for $File::Find::name/fullname
14 my %Expect_Dir = (); # what we expect for $File::Find::dir
15 my ($cwd, $cwd_untainted);
19 if ($ENV{PERL_CORE}) {
20 # May be doing dynamic loading while @INC is all relative
21 @INC = map { $_ = File::Spec->rel2abs($_); /(.*)/; $1 } @INC;
29 for (keys %ENV) { # untaint ENV
30 ($ENV{$_}) = $ENV{$_} =~ /(.*)/;
34 # Remove insecure directories from PATH
36 my $sep = $Config{path_sep};
37 foreach my $dir (split(/\Q$sep/,$ENV{'PATH'}))
40 ## Match the directory taint tests in mg.c::Perl_magic_setenv()
42 push(@path,$dir) unless (length($dir) >= 256
44 substr($dir,0,1) ne "/"
46 (stat $dir)[2] & 002);
48 $ENV{'PATH'} = join($sep,@path);
51 my $symlink_exists = eval { symlink("",""); 1 };
58 ( my $orig_dir_untainted ) = $orig_dir =~ m|^(.+)$|; # untaint it
63 find({wanted => sub { ++$found if $_ eq 'taint.t' },
64 untaint => 1, untaint_pattern => qr|^(.+)$|}, File::Spec->curdir);
66 is($found, 1, 'taint.t found once');
69 finddepth({wanted => sub { ++$found if $_ eq 'taint.t'; },
70 untaint => 1, untaint_pattern => qr|^(.+)$|}, File::Spec->curdir);
72 is($found, 1, 'taint.t found once again');
75 my $FastFileTests_OK = 0;
78 chdir($orig_dir_untainted);
80 if (-d dir_path('for_find')) {
81 $need_updir = 1 if chdir(dir_path('for_find'));
83 if (-d dir_path('fa')) {
84 unlink file_path('fa', 'fa_ord'),
85 file_path('fa', 'fsl'),
86 file_path('fa', 'faa', 'faa_ord'),
87 file_path('fa', 'fab', 'fab_ord'),
88 file_path('fa', 'fab', 'faba', 'faba_ord'),
89 file_path('fb', 'fb_ord'),
90 file_path('fb', 'fba', 'fba_ord');
91 rmdir dir_path('fa', 'faa');
92 rmdir dir_path('fa', 'fab', 'faba');
93 rmdir dir_path('fa', 'fab');
95 rmdir dir_path('fb', 'fba');
99 my $updir = $^O eq 'VMS' ? File::Spec::VMS->updir() : File::Spec->updir;
102 if (-d dir_path('for_find')) {
103 rmdir dir_path('for_find') or print "# Can't rmdir for_find: $!\n";
112 ok( open(my $T,'>',$_[0]), "Opened $_[0] successfully" );
116 ok( mkdir($_[0],$_[1]), "Created directory $_[0] successfully" );
119 sub wanted_File_Dir {
120 print "# \$File::Find::dir => '$File::Find::dir'\n";
121 print "# \$_ => '$_'\n";
122 s#\.$## if ($^O eq 'VMS' && $_ ne '.');
123 s/(.dir)?$//i if ($^O eq 'VMS' && -d _);
124 ok( $Expect_File{$_}, "Expected and found $File::Find::name" );
125 if ( $FastFileTests_OK ) {
126 delete $Expect_File{ $_}
127 unless ( $Expect_Dir{$_} && ! -d _ );
129 delete $Expect_File{$_}
130 unless ( $Expect_Dir{$_} && ! -d $_ );
134 sub wanted_File_Dir_prune {
136 $File::Find::prune=1 if $_ eq 'faba';
140 print "# \$File::Find::dir => '$File::Find::dir'\n";
141 print "# \$_ => '$_'\n";
145 # Use dir_path() to specify a directory path that's expected for
146 # $File::Find::dir (%Expect_Dir). Also use it in file operations like
149 # dir_path() concatenates directory names to form a *relative*
150 # directory path, independent from the platform it's run on, although
151 # there are limitations. Don't try to create an absolute path,
152 # because that may fail on operating systems that have the concept of
153 # volume names (e.g. Mac OS). As a special case, you can pass it a "."
154 # as first argument, to create a directory path like "./fa/dir". If there's
155 # no second argument this function will return the string "./"
158 my $first_arg = shift @_;
160 if ($first_arg eq '.') {
161 return './' unless @_;
162 my $path = File::Spec->catdir(@_);
166 } else { # $first_arg ne '.'
167 return $first_arg unless @_; # return plain filename
168 my $fname = File::Spec->catdir($first_arg, @_); # relative path
169 $fname = VMS::Filespec::unixpath($fname) if $^O eq 'VMS';
175 # Use topdir() to specify a directory path that you want to pass to
176 # find/finddepth. Historically topdir() differed on Mac OS classic.
178 *topdir = \&dir_path;
181 # Use file_path() to specify a file path that's expected for $_
182 # (%Expect_File). Also suitable for file operations like unlink etc.
184 # file_path() concatenates directory names (if any) and a filename to
185 # form a *relative* file path (the last argument is assumed to be a
186 # file). It's independent from the platform it's run on, although
187 # there are limitations. As a special case, you can pass it a "." as
188 # first argument, to create a file path like "./fa/file". If there's no
189 # second argument, this function will return the string "./" otherwise.
192 my $first_arg = shift @_;
194 if ($first_arg eq '.') {
195 return './' unless @_;
196 my $path = File::Spec->catfile(@_);
200 } else { # $first_arg ne '.'
201 return $first_arg unless @_; # return plain filename
202 my $fname = File::Spec->catfile($first_arg, @_); # relative path
203 $fname = VMS::Filespec::unixify($fname) if $^O eq 'VMS';
209 # Use file_path_name() to specify a file path that's expected for
210 # $File::Find::Name (%Expect_Name). Note: When the no_chdir => 1
211 # option is in effect, $_ is the same as $File::Find::Name. In that
212 # case, also use this function to specify a file path that's expected
215 # Historically file_path_name differed on Mac OS classic.
217 *file_path_name = \&file_path;
220 MkDir( dir_path('for_find'), 0770 );
221 ok( chdir( dir_path('for_find')), 'successful chdir() to for_find' );
223 $cwd = cwd(); # save cwd
224 ( $cwd_untainted ) = $cwd =~ m|^(.+)$|; # untaint it
226 MkDir( dir_path('fa'), 0770 );
227 MkDir( dir_path('fb'), 0770 );
228 touch( file_path('fb', 'fb_ord') );
229 MkDir( dir_path('fb', 'fba'), 0770 );
230 touch( file_path('fb', 'fba', 'fba_ord') );
232 skip "Creating symlink", 1, unless $symlink_exists;
233 ok( symlink('../fb','fa/fsl'), 'Created symbolic link' );
235 touch( file_path('fa', 'fa_ord') );
237 MkDir( dir_path('fa', 'faa'), 0770 );
238 touch( file_path('fa', 'faa', 'faa_ord') );
239 MkDir( dir_path('fa', 'fab'), 0770 );
240 touch( file_path('fa', 'fab', 'fab_ord') );
241 MkDir( dir_path('fa', 'fab', 'faba'), 0770 );
242 touch( file_path('fa', 'fab', 'faba', 'faba_ord') );
244 print "# check untainting (no follow)\n";
246 # untainting here should work correctly
248 %Expect_File = (File::Spec->curdir => 1, file_path('fsl') =>
249 1,file_path('fa_ord') => 1, file_path('fab') => 1,
250 file_path('fab_ord') => 1, file_path('faba') => 1,
251 file_path('faa') => 1, file_path('faa_ord') => 1);
252 delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
255 %Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1,
256 dir_path('fab') => 1, dir_path('faba') => 1,
257 dir_path('fb') => 1, dir_path('fba') => 1);
259 delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists;
261 File::Find::find( {wanted => \&wanted_File_Dir_prune, untaint => 1,
262 untaint_pattern => qr|^(.+)$|}, topdir('fa') );
264 is(scalar keys %Expect_File, 0, 'Found all expected files');
267 # don't untaint at all, should die
272 eval {File::Find::find( {wanted => \&simple_wanted}, topdir('fa') );};
273 like( $@, qr|Insecure dependency|, 'Tainted directory causes death (good)' );
274 chdir($cwd_untainted);
277 # untaint pattern doesn't match, should die
280 eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1,
281 untaint_pattern => qr|^(NO_MATCH)$|},
284 like( $@, qr|is still tainted|, 'Bad untaint pattern causes death (good)' );
285 chdir($cwd_untainted);
288 # untaint pattern doesn't match, should die when we chdir to cwd
289 print "# check untaint_skip (No follow)\n";
292 eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1,
293 untaint_skip => 1, untaint_pattern =>
294 qr|^(NO_MATCH)$|}, topdir('fa') );};
298 like( $@, qr|insecure cwd|, 'Bad untaint pattern causes death in cwd (good)' );
300 chdir($cwd_untainted);
304 skip "Symbolic link tests", 17, unless $symlink_exists;
305 print "# --- symbolic link tests --- \n";
306 $FastFileTests_OK= 1;
308 print "# check untainting (follow)\n";
310 # untainting here should work correctly
311 # no_chdir is in effect, hence we use file_path_name to specify the expected paths for %Expect_File
313 %Expect_File = (file_path_name('fa') => 1,
314 file_path_name('fa','fa_ord') => 1,
315 file_path_name('fa', 'fsl') => 1,
316 file_path_name('fa', 'fsl', 'fb_ord') => 1,
317 file_path_name('fa', 'fsl', 'fba') => 1,
318 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
319 file_path_name('fa', 'fab') => 1,
320 file_path_name('fa', 'fab', 'fab_ord') => 1,
321 file_path_name('fa', 'fab', 'faba') => 1,
322 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
323 file_path_name('fa', 'faa') => 1,
324 file_path_name('fa', 'faa', 'faa_ord') => 1);
328 %Expect_Dir = (dir_path('fa') => 1,
329 dir_path('fa', 'faa') => 1,
330 dir_path('fa', 'fab') => 1,
331 dir_path('fa', 'fab', 'faba') => 1,
333 dir_path('fb', 'fba') => 1);
335 File::Find::find( {wanted => \&wanted_File_Dir, follow_fast => 1,
336 no_chdir => 1, untaint => 1, untaint_pattern =>
337 qr|^(.+)$| }, topdir('fa') );
339 is( scalar(keys %Expect_File), 0, 'Found all files in symlink test' );
342 # don't untaint at all, should die
345 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1},
348 like( $@, qr|Insecure dependency|, 'Not untainting causes death (good)' );
349 chdir($cwd_untainted);
351 # untaint pattern doesn't match, should die
354 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
355 untaint => 1, untaint_pattern =>
356 qr|^(NO_MATCH)$|}, topdir('fa') );};
358 like( $@, qr|is still tainted|, 'Bat untaint pattern causes death (good)' );
359 chdir($cwd_untainted);
361 # untaint pattern doesn't match, should die when we chdir to cwd
362 print "# check untaint_skip (Follow)\n";
365 eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1,
366 untaint_skip => 1, untaint_pattern =>
367 qr|^(NO_MATCH)$|}, topdir('fa') );};
368 like( $@, qr|insecure cwd|, 'Cwd not untainted with bad pattern (good)' );
370 chdir($cwd_untainted);