4 my %Expect_File = (); # what we expect for $_
5 my %Expect_Name = (); # what we expect for $File::Find::name/fullname
6 my %Expect_Dir = (); # what we expect for $File::Find::dir
7 my $symlink_exists = eval { symlink("",""); 1 };
15 unshift @INC => '../lib';
17 for (keys %ENV) { # untaint ENV
18 ($ENV{$_}) = $ENV{$_} =~ /(.*)/;
21 # Remove insecure directories from PATH
23 my $sep = $Config{path_sep};
24 foreach my $dir (split(/\Q$sep/,$ENV{'PATH'}))
27 ## Match the directory taint tests in mg.c::Perl_magic_setenv()
29 push(@path,$dir) unless (length($dir) >= 256
31 substr($dir,0,1) ne "/"
33 (stat $dir)[2] & 002);
35 $ENV{'PATH'} = join($sep,@path);
39 if ( $symlink_exists ) { print "1..45\n"; }
40 else { print "1..27\n"; }
47 my $NonTaintedCwd = $^O eq 'MSWin32' || $^O eq 'cygwin';
51 find({wanted => sub { print "ok 1\n" if $_ eq 'commonsense.t'; },
52 untaint => 1, untaint_pattern => qr|^(.+)$|}, File::Spec->curdir);
54 finddepth({wanted => sub { print "ok 2\n" if $_ eq 'commonsense.t'; },
55 untaint => 1, untaint_pattern => qr|^(.+)$|},
59 my $FastFileTests_OK = 0;
62 if (-d dir_path('for_find')) {
63 chdir(dir_path('for_find'));
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');
77 rmdir dir_path('fb', 'fba');
79 chdir File::Spec->updir;
80 rmdir dir_path('for_find');
90 if ($_[0]) { print "ok $case\n"; }
91 else { print "not ok $case\n"; }
97 if ($_[0]) { print "ok $case\n"; }
98 else { print "not ok $case\n"; exit 0; }
103 print "ok $case # skipped: ",$_[0],"\n";
107 CheckDie( open(my $T,'>',$_[0]) );
111 CheckDie( mkdir($_[0],$_[1]) );
114 sub wanted_File_Dir {
115 print "# \$File::Find::dir => '$File::Find::dir'\n";
116 print "# \$_ => '$_'\n";
117 s#\.$## if ($^O eq 'VMS' && $_ ne '.');
118 Check( $Expect_File{$_} );
119 if ( $FastFileTests_OK ) {
120 delete $Expect_File{ $_}
121 unless ( $Expect_Dir{$_} && ! -d _ );
123 delete $Expect_File{$_}
124 unless ( $Expect_Dir{$_} && ! -d $_ );
128 sub wanted_File_Dir_prune {
130 $File::Find::prune=1 if $_ eq 'faba';
135 print "# \$File::Find::dir => '$File::Find::dir'\n";
136 print "# \$_ => '$_'\n";
140 # Use dir_path() to specify a directory path that's expected for
141 # $File::Find::dir (%Expect_Dir). Also use it in file operations like
144 # dir_path() concatenates directory names to form a _relative_
145 # directory path, independant from the platform it's run on, although
146 # there are limitations. Don't try to create an absolute path,
147 # because that may fail on operating systems that have the concept of
148 # volume names (e.g. Mac OS). Be careful when you want to create an
149 # updir path like ../fa (Unix) or ::fa: (Mac OS). Plain directory
150 # names will work best. As a special case, you can pass it a "." as
151 # first argument, to create a directory path like "./fa/dir" on
152 # operating systems other than Mac OS (actually, Mac OS will ignore
153 # the ".", if it's the first argument). If there's no second argument,
154 # this function will return the empty string on Mac OS and the string
158 my $first_item = shift @_;
160 if ($first_item eq '.') {
161 if ($^O eq 'MacOS') {
163 # ignore first argument; return a relative path
164 # with leading ":" and with trailing ":"
165 return File::Spec->catdir("", @_);
167 return './' unless @_;
168 my $path = File::Spec->catdir(@_);
174 } else { # $first_item ne '.'
175 return $first_item unless @_; # return plain filename
176 if ($^O eq 'MacOS') {
177 # relative path with leading ":" and with trailing ":"
178 return File::Spec->catdir("", $first_item, @_);
180 return File::Spec->catdir($first_item, @_);
186 # Use topdir() to specify a directory path that you want to pass to
187 #find/finddepth Basically, topdir() does the same as dir_path() (see
188 #above), except that there's no trailing ":" on Mac OS.
191 my $path = dir_path(@_);
192 $path =~ s/:$// if ($^O eq 'MacOS');
197 # Use file_path() to specify a file path that's expected for $_ (%Expect_File).
198 # Also suitable for file operations like unlink etc.
200 # file_path() concatenates directory names (if any) and a filename to
201 # form a _relative_ file path (the last argument is assumed to be a
202 # file). It's independant from the platform it's run on, although
203 # there are limitations (see the warnings for dir_path() above). As a
204 # special case, you can pass it a "." as first argument, to create a
205 # file path like "./fa/file" on operating systems other than Mac OS
206 # (actually, Mac OS will ignore the ".", if it's the first
207 # argument). If there's no second argument, this function will return
208 # the empty string on Mac OS and the string "./" otherwise.
211 my $first_item = shift @_;
213 if ($first_item eq '.') {
214 if ($^O eq 'MacOS') {
216 # ignore first argument; return a relative path
217 # with leading ":", but without trailing ":"
218 return File::Spec->catfile("", @_);
220 return './' unless @_;
221 my $path = File::Spec->catfile(@_);
227 } else { # $first_item ne '.'
228 return $first_item unless @_; # return plain filename
229 if ($^O eq 'MacOS') {
230 # relative path with leading ":", but without trailing ":"
231 return File::Spec->catfile("", $first_item, @_);
233 return File::Spec->catfile($first_item, @_);
239 # Use file_path_name() to specify a file path that's expected for
240 # $File::Find::Name (%Expect_Name). Note: When the no_chdir => 1
241 # option is in effect, $_ is the same as $File::Find::Name. In that
242 # case, also use this function to specify a file path that's expected
245 # Basically, file_path_name() does the same as file_path() (see
246 # above), except that there's always a leading ":" on Mac OS, even for
247 # plain file/directory names.
250 my $path = file_path(@_);
251 $path = ":$path" if (($^O eq 'MacOS') && ($path !~ /:/));
257 MkDir( dir_path('for_find'), 0770 );
258 CheckDie(chdir( dir_path('for_find')));
260 $cwd = cwd(); # save cwd
261 ( $cwd_untainted ) = $cwd =~ m|^(.+)$|; # untaint it
263 MkDir( dir_path('fa'), 0770 );
264 MkDir( dir_path('fb'), 0770 );
265 touch( file_path('fb', 'fb_ord') );
266 MkDir( dir_path('fb', 'fba'), 0770 );
267 touch( file_path('fb', 'fba', 'fba_ord') );
268 if ($^O eq 'MacOS') {
269 CheckDie( symlink(':fb',':fa:fsl') ) if $symlink_exists;
271 CheckDie( symlink('../fb','fa/fsl') ) if $symlink_exists;
273 touch( file_path('fa', 'fa_ord') );
275 MkDir( dir_path('fa', 'faa'), 0770 );
276 touch( file_path('fa', 'faa', 'faa_ord') );
277 MkDir( dir_path('fa', 'fab'), 0770 );
278 touch( file_path('fa', 'fab', 'fab_ord') );
279 MkDir( dir_path('fa', 'fab', 'faba'), 0770 );
280 touch( file_path('fa', 'fab', 'faba', 'faba_ord') );
282 print "# check untainting (no follow)\n";
284 # untainting here should work correctly
286 %Expect_File = (File::Spec->curdir => 1, file_path('fsl') =>
287 1,file_path('fa_ord') => 1, file_path('fab') => 1,
288 file_path('fab_ord') => 1, file_path('faba') => 1,
289 file_path('faa') => 1, file_path('faa_ord') => 1);
290 delete $Expect_File{ file_path('fsl') } unless $symlink_exists;
293 %Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1,
294 dir_path('fab') => 1, dir_path('faba') => 1,
295 dir_path('fb') => 1, dir_path('fba') => 1);
297 delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists;
299 File::Find::find( {wanted => \&wanted_File_Dir_prune, untaint => 1,
300 untaint_pattern => qr|^(.+)$|}, topdir('fa') );
302 Check( scalar(keys %Expect_File) == 0 );
305 # don't untaint at all, should die
310 eval {File::Find::find( {wanted => \&simple_wanted}, topdir('fa') );};
311 Check( $@ =~ m|Insecure dependency| );
312 chdir($cwd_untainted);
315 # untaint pattern doesn't match, should die
318 eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1,
319 untaint_pattern => qr|^(NO_MATCH)$|},
322 Check( $@ =~ m|is still tainted| );
323 chdir($cwd_untainted);
326 # untaint pattern doesn't match, should die when we chdir to cwd
327 print "# check untaint_skip (No follow)\n";
330 eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1,
331 untaint_skip => 1, untaint_pattern =>
332 qr|^(NO_MATCH)$|}, topdir('fa') );};
336 if ($NonTaintedCwd) {
337 Skip("$^O does not taint cwd");
340 Check( $@ =~ m|insecure cwd| );
342 chdir($cwd_untainted);
345 if ( $symlink_exists ) {
346 print "# --- symbolic link tests --- \n";
347 $FastFileTests_OK= 1;
349 print "# check untainting (follow)\n";
351 # untainting here should work correctly
352 # no_chdir is in effect, hence we use file_path_name to specify the expected paths for %Expect_File
354 %Expect_File = (file_path_name('fa') => 1,
355 file_path_name('fa','fa_ord') => 1,
356 file_path_name('fa', 'fsl') => 1,
357 file_path_name('fa', 'fsl', 'fb_ord') => 1,
358 file_path_name('fa', 'fsl', 'fba') => 1,
359 file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
360 file_path_name('fa', 'fab') => 1,
361 file_path_name('fa', 'fab', 'fab_ord') => 1,
362 file_path_name('fa', 'fab', 'faba') => 1,
363 file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
364 file_path_name('fa', 'faa') => 1,
365 file_path_name('fa', 'faa', 'faa_ord') => 1);
369 %Expect_Dir = (dir_path('fa') => 1,
370 dir_path('fa', 'faa') => 1,
371 dir_path('fa', 'fab') => 1,
372 dir_path('fa', 'fab', 'faba') => 1,
374 dir_path('fb', 'fba') => 1);
376 File::Find::find( {wanted => \&wanted_File_Dir, follow_fast => 1,
377 no_chdir => 1, untaint => 1, untaint_pattern =>
378 qr|^(.+)$| }, topdir('fa') );
380 Check( scalar(keys %Expect_File) == 0 );
383 # don't untaint at all, should die
386 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1},
389 Check( $@ =~ m|Insecure dependency| );
390 chdir($cwd_untainted);
392 # untaint pattern doesn't match, should die
395 eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
396 untaint => 1, untaint_pattern =>
397 qr|^(NO_MATCH)$|}, topdir('fa') );};
399 Check( $@ =~ m|is still tainted| );
400 chdir($cwd_untainted);
402 # untaint pattern doesn't match, should die when we chdir to cwd
403 print "# check untaint_skip (Follow)\n";
406 eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1,
407 untaint_skip => 1, untaint_pattern =>
408 qr|^(NO_MATCH)$|}, topdir('fa') );};
409 if ($NonTaintedCwd) {
410 Skip("$^O does not taint cwd");
413 Check( $@ =~ m|insecure cwd| );
415 chdir($cwd_untainted);