1N/A#!./perl -T
1N/A
1N/A
1N/Amy %Expect_File = (); # what we expect for $_
1N/Amy %Expect_Name = (); # what we expect for $File::Find::name/fullname
1N/Amy %Expect_Dir = (); # what we expect for $File::Find::dir
1N/Amy ($cwd, $cwd_untainted);
1N/A
1N/A
1N/ABEGIN {
1N/A chdir 't' if -d 't';
1N/A unshift @INC => '../lib';
1N/A}
1N/A
1N/Ause Config;
1N/A
1N/ABEGIN {
1N/A if ($^O ne 'VMS') {
1N/A for (keys %ENV) { # untaint ENV
1N/A ($ENV{$_}) = $ENV{$_} =~ /(.*)/;
1N/A }
1N/A }
1N/A
1N/A # Remove insecure directories from PATH
1N/A my @path;
1N/A my $sep = $Config{path_sep};
1N/A foreach my $dir (split(/\Q$sep/,$ENV{'PATH'}))
1N/A {
1N/A ##
1N/A ## Match the directory taint tests in mg.c::Perl_magic_setenv()
1N/A ##
1N/A push(@path,$dir) unless (length($dir) >= 256
1N/A or
1N/A substr($dir,0,1) ne "/"
1N/A or
1N/A (stat $dir)[2] & 002);
1N/A }
1N/A $ENV{'PATH'} = join($sep,@path);
1N/A}
1N/A
1N/Ause Test::More tests => 45;
1N/A
1N/Amy $symlink_exists = eval { symlink("",""); 1 };
1N/A
1N/Ause File::Find;
1N/Ause File::Spec;
1N/Ause Cwd;
1N/A
1N/Acleanup();
1N/A
1N/Amy $found;
1N/Afind({wanted => sub { $found = 1 if ($_ eq 'commonsense.t') },
1N/A untaint => 1, untaint_pattern => qr|^(.+)$|}, File::Spec->curdir);
1N/A
1N/Aok($found, 'commonsense.t found');
1N/A$found = 0;
1N/A
1N/Afinddepth({wanted => sub { $found = 1 if $_ eq 'commonsense.t'; },
1N/A untaint => 1, untaint_pattern => qr|^(.+)$|}, File::Spec->curdir);
1N/A
1N/Aok($found, 'commonsense.t found again');
1N/A
1N/Amy $case = 2;
1N/Amy $FastFileTests_OK = 0;
1N/A
1N/Asub cleanup {
1N/A if (-d dir_path('for_find')) {
1N/A chdir(dir_path('for_find'));
1N/A }
1N/A if (-d dir_path('fa')) {
1N/A unlink file_path('fa', 'fa_ord'),
1N/A file_path('fa', 'fsl'),
1N/A file_path('fa', 'faa', 'faa_ord'),
1N/A file_path('fa', 'fab', 'fab_ord'),
1N/A file_path('fa', 'fab', 'faba', 'faba_ord'),
1N/A file_path('fb', 'fb_ord'),
1N/A file_path('fb', 'fba', 'fba_ord');
1N/A rmdir dir_path('fa', 'faa');
1N/A rmdir dir_path('fa', 'fab', 'faba');
1N/A rmdir dir_path('fa', 'fab');
1N/A rmdir dir_path('fa');
1N/A rmdir dir_path('fb', 'fba');
1N/A rmdir dir_path('fb');
1N/A }
1N/A chdir File::Spec->updir;
1N/A if (-d dir_path('for_find')) {
1N/A rmdir dir_path('for_find') or print "# Can't rmdir for_find: $!\n";
1N/A }
1N/A}
1N/A
1N/AEND {
1N/A cleanup();
1N/A}
1N/A
1N/Asub touch {
1N/A ok( open(my $T,'>',$_[0]), "Opened $_[0] successfully" );
1N/A}
1N/A
1N/Asub MkDir($$) {
1N/A ok( mkdir($_[0],$_[1]), "Created directory $_[0] successfully" );
1N/A}
1N/A
1N/Asub wanted_File_Dir {
1N/A print "# \$File::Find::dir => '$File::Find::dir'\n";
1N/A print "# \$_ => '$_'\n";
1N/A s#\.$## if ($^O eq 'VMS' && $_ ne '.');
1N/A ok( $Expect_File{$_}, "Expected and found $File::Find::name" );
1N/A if ( $FastFileTests_OK ) {
1N/A delete $Expect_File{ $_}
1N/A unless ( $Expect_Dir{$_} && ! -d _ );
1N/A } else {
1N/A delete $Expect_File{$_}
1N/A unless ( $Expect_Dir{$_} && ! -d $_ );
1N/A }
1N/A}
1N/A
1N/Asub wanted_File_Dir_prune {
1N/A &wanted_File_Dir;
1N/A $File::Find::prune=1 if $_ eq 'faba';
1N/A}
1N/A
1N/Asub simple_wanted {
1N/A print "# \$File::Find::dir => '$File::Find::dir'\n";
1N/A print "# \$_ => '$_'\n";
1N/A}
1N/A
1N/A
1N/A# Use dir_path() to specify a directory path that's expected for
1N/A# $File::Find::dir (%Expect_Dir). Also use it in file operations like
1N/A# chdir, rmdir etc.
1N/A#
1N/A# dir_path() concatenates directory names to form a *relative*
1N/A# directory path, independent from the platform it's run on, although
1N/A# there are limitations. Don't try to create an absolute path,
1N/A# because that may fail on operating systems that have the concept of
1N/A# volume names (e.g. Mac OS). As a special case, you can pass it a "."
1N/A# as first argument, to create a directory path like "./fa/dir" on
1N/A# operating systems other than Mac OS (actually, Mac OS will ignore
1N/A# the ".", if it's the first argument). If there's no second argument,
1N/A# this function will return the empty string on Mac OS and the string
1N/A# "./" otherwise.
1N/A
1N/Asub dir_path {
1N/A my $first_arg = shift @_;
1N/A
1N/A if ($first_arg eq '.') {
1N/A if ($^O eq 'MacOS') {
1N/A return '' unless @_;
1N/A # ignore first argument; return a relative path
1N/A # with leading ":" and with trailing ":"
1N/A return File::Spec->catdir(@_);
1N/A } else { # other OS
1N/A return './' unless @_;
1N/A my $path = File::Spec->catdir(@_);
1N/A # add leading "./"
1N/A $path = "./$path";
1N/A return $path;
1N/A }
1N/A
1N/A } else { # $first_arg ne '.'
1N/A return $first_arg unless @_; # return plain filename
1N/A return File::Spec->catdir($first_arg, @_); # relative path
1N/A }
1N/A}
1N/A
1N/A
1N/A# Use topdir() to specify a directory path that you want to pass to
1N/A# find/finddepth. Basically, topdir() does the same as dir_path() (see
1N/A# above), except that there's no trailing ":" on Mac OS.
1N/A
1N/Asub topdir {
1N/A my $path = dir_path(@_);
1N/A $path =~ s/:$// if ($^O eq 'MacOS');
1N/A return $path;
1N/A}
1N/A
1N/A
1N/A# Use file_path() to specify a file path that's expected for $_
1N/A# (%Expect_File). Also suitable for file operations like unlink etc.
1N/A#
1N/A# file_path() concatenates directory names (if any) and a filename to
1N/A# form a *relative* file path (the last argument is assumed to be a
1N/A# file). It's independent from the platform it's run on, although
1N/A# there are limitations. As a special case, you can pass it a "." as
1N/A# first argument, to create a file path like "./fa/file" on operating
1N/A# systems other than Mac OS (actually, Mac OS will ignore the ".", if
1N/A# it's the first argument). If there's no second argument, this
1N/A# function will return the empty string on Mac OS and the string "./"
1N/A# otherwise.
1N/A
1N/Asub file_path {
1N/A my $first_arg = shift @_;
1N/A
1N/A if ($first_arg eq '.') {
1N/A if ($^O eq 'MacOS') {
1N/A return '' unless @_;
1N/A # ignore first argument; return a relative path
1N/A # with leading ":", but without trailing ":"
1N/A return File::Spec->catfile(@_);
1N/A } else { # other OS
1N/A return './' unless @_;
1N/A my $path = File::Spec->catfile(@_);
1N/A # add leading "./"
1N/A $path = "./$path";
1N/A return $path;
1N/A }
1N/A
1N/A } else { # $first_arg ne '.'
1N/A return $first_arg unless @_; # return plain filename
1N/A return File::Spec->catfile($first_arg, @_); # relative path
1N/A }
1N/A}
1N/A
1N/A
1N/A# Use file_path_name() to specify a file path that's expected for
1N/A# $File::Find::Name (%Expect_Name). Note: When the no_chdir => 1
1N/A# option is in effect, $_ is the same as $File::Find::Name. In that
1N/A# case, also use this function to specify a file path that's expected
1N/A# for $_.
1N/A#
1N/A# Basically, file_path_name() does the same as file_path() (see
1N/A# above), except that there's always a leading ":" on Mac OS, even for
1N/A# plain file/directory names.
1N/A
1N/Asub file_path_name {
1N/A my $path = file_path(@_);
1N/A $path = ":$path" if (($^O eq 'MacOS') && ($path !~ /:/));
1N/A return $path;
1N/A}
1N/A
1N/A
1N/AMkDir( dir_path('for_find'), 0770 );
1N/Aok( chdir( dir_path('for_find')), 'successful chdir() to for_find' );
1N/A
1N/A$cwd = cwd(); # save cwd
1N/A( $cwd_untainted ) = $cwd =~ m|^(.+)$|; # untaint it
1N/A
1N/AMkDir( dir_path('fa'), 0770 );
1N/AMkDir( dir_path('fb'), 0770 );
1N/Atouch( file_path('fb', 'fb_ord') );
1N/AMkDir( dir_path('fb', 'fba'), 0770 );
1N/Atouch( file_path('fb', 'fba', 'fba_ord') );
1N/ASKIP: {
1N/A skip "Creating symlink", 1, unless $symlink_exists;
1N/Aif ($^O eq 'MacOS') {
1N/A ok( symlink(':fb',':fa:fsl'), 'Created symbolic link' );
1N/A} else {
1N/A ok( symlink('../fb','fa/fsl'), 'Created symbolic link' );
1N/A}
1N/A}
1N/Atouch( file_path('fa', 'fa_ord') );
1N/A
1N/AMkDir( dir_path('fa', 'faa'), 0770 );
1N/Atouch( file_path('fa', 'faa', 'faa_ord') );
1N/AMkDir( dir_path('fa', 'fab'), 0770 );
1N/Atouch( file_path('fa', 'fab', 'fab_ord') );
1N/AMkDir( dir_path('fa', 'fab', 'faba'), 0770 );
1N/Atouch( file_path('fa', 'fab', 'faba', 'faba_ord') );
1N/A
1N/Aprint "# check untainting (no follow)\n";
1N/A
1N/A# untainting here should work correctly
1N/A
1N/A%Expect_File = (File::Spec->curdir => 1, file_path('fsl') =>
1N/A 1,file_path('fa_ord') => 1, file_path('fab') => 1,
1N/A file_path('fab_ord') => 1, file_path('faba') => 1,
1N/A file_path('faa') => 1, file_path('faa_ord') => 1);
1N/Adelete $Expect_File{ file_path('fsl') } unless $symlink_exists;
1N/A%Expect_Name = ();
1N/A
1N/A%Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1,
1N/A dir_path('fab') => 1, dir_path('faba') => 1,
1N/A dir_path('fb') => 1, dir_path('fba') => 1);
1N/A
1N/Adelete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists;
1N/A
1N/AFile::Find::find( {wanted => \&wanted_File_Dir_prune, untaint => 1,
1N/A untaint_pattern => qr|^(.+)$|}, topdir('fa') );
1N/A
1N/Ais(scalar keys %Expect_File, 0, 'Found all expected files');
1N/A
1N/A
1N/A# don't untaint at all, should die
1N/A%Expect_File = ();
1N/A%Expect_Name = ();
1N/A%Expect_Dir = ();
1N/Aundef $@;
1N/Aeval {File::Find::find( {wanted => \&simple_wanted}, topdir('fa') );};
1N/Alike( $@, qr|Insecure dependency|, 'Tainted directory causes death (good)' );
1N/Achdir($cwd_untainted);
1N/A
1N/A
1N/A# untaint pattern doesn't match, should die
1N/Aundef $@;
1N/A
1N/Aeval {File::Find::find( {wanted => \&simple_wanted, untaint => 1,
1N/A untaint_pattern => qr|^(NO_MATCH)$|},
1N/A topdir('fa') );};
1N/A
1N/Alike( $@, qr|is still tainted|, 'Bad untaint pattern causes death (good)' );
1N/Achdir($cwd_untainted);
1N/A
1N/A
1N/A# untaint pattern doesn't match, should die when we chdir to cwd
1N/Aprint "# check untaint_skip (No follow)\n";
1N/Aundef $@;
1N/A
1N/Aeval {File::Find::find( {wanted => \&simple_wanted, untaint => 1,
1N/A untaint_skip => 1, untaint_pattern =>
1N/A qr|^(NO_MATCH)$|}, topdir('fa') );};
1N/A
1N/Aprint "# $@" if $@;
1N/A#$^D = 8;
1N/Alike( $@, qr|insecure cwd|, 'Bad untaint pattern causes death in cwd (good)' );
1N/A
1N/Achdir($cwd_untainted);
1N/A
1N/A
1N/ASKIP: {
1N/A skip "Symbolic link tests", 17, unless $symlink_exists;
1N/A print "# --- symbolic link tests --- \n";
1N/A $FastFileTests_OK= 1;
1N/A
1N/A print "# check untainting (follow)\n";
1N/A
1N/A # untainting here should work correctly
1N/A # no_chdir is in effect, hence we use file_path_name to specify the expected paths for %Expect_File
1N/A
1N/A %Expect_File = (file_path_name('fa') => 1,
1N/A file_path_name('fa','fa_ord') => 1,
1N/A file_path_name('fa', 'fsl') => 1,
1N/A file_path_name('fa', 'fsl', 'fb_ord') => 1,
1N/A file_path_name('fa', 'fsl', 'fba') => 1,
1N/A file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1,
1N/A file_path_name('fa', 'fab') => 1,
1N/A file_path_name('fa', 'fab', 'fab_ord') => 1,
1N/A file_path_name('fa', 'fab', 'faba') => 1,
1N/A file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1,
1N/A file_path_name('fa', 'faa') => 1,
1N/A file_path_name('fa', 'faa', 'faa_ord') => 1);
1N/A
1N/A %Expect_Name = ();
1N/A
1N/A %Expect_Dir = (dir_path('fa') => 1,
1N/A dir_path('fa', 'faa') => 1,
1N/A dir_path('fa', 'fab') => 1,
1N/A dir_path('fa', 'fab', 'faba') => 1,
1N/A dir_path('fb') => 1,
1N/A dir_path('fb', 'fba') => 1);
1N/A
1N/A File::Find::find( {wanted => \&wanted_File_Dir, follow_fast => 1,
1N/A no_chdir => 1, untaint => 1, untaint_pattern =>
1N/A qr|^(.+)$| }, topdir('fa') );
1N/A
1N/A is( scalar(keys %Expect_File), 0, 'Found all files in symlink test' );
1N/A
1N/A
1N/A # don't untaint at all, should die
1N/A undef $@;
1N/A
1N/A eval {File::Find::find( {wanted => \&simple_wanted, follow => 1},
1N/A topdir('fa') );};
1N/A
1N/A like( $@, qr|Insecure dependency|, 'Not untainting causes death (good)' );
1N/A chdir($cwd_untainted);
1N/A
1N/A # untaint pattern doesn't match, should die
1N/A undef $@;
1N/A
1N/A eval {File::Find::find( {wanted => \&simple_wanted, follow => 1,
1N/A untaint => 1, untaint_pattern =>
1N/A qr|^(NO_MATCH)$|}, topdir('fa') );};
1N/A
1N/A like( $@, qr|is still tainted|, 'Bat untaint pattern causes death (good)' );
1N/A chdir($cwd_untainted);
1N/A
1N/A # untaint pattern doesn't match, should die when we chdir to cwd
1N/A print "# check untaint_skip (Follow)\n";
1N/A undef $@;
1N/A
1N/A eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1,
1N/A untaint_skip => 1, untaint_pattern =>
1N/A qr|^(NO_MATCH)$|}, topdir('fa') );};
1N/A like( $@, qr|insecure cwd|, 'Cwd not untainted with bad pattern (good)' );
1N/A
1N/A chdir($cwd_untainted);
1N/A}