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