Commit | Line | Data |
3fa6e24b |
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 | if ( $symlink_exists ) { print "1..45\n"; } |
21 | else { print "1..27\n"; } |
22 | |
23 | use File::Find; |
24 | use File::Spec; |
25 | use Cwd; |
26 | |
27 | # Remove insecure directories from PATH |
28 | my @path; |
29 | my $sep = ($^O eq 'MSWin32') ? ';' : ':'; |
30 | foreach my $dir (split(/$sep/,$ENV{'PATH'})) |
31 | { |
32 | push(@path,$dir) unless -w $dir; |
33 | } |
34 | $ENV{'PATH'} = join($sep,@path); |
35 | |
36 | cleanup(); |
37 | |
b695f709 |
38 | find({wanted => sub { print "ok 1\n" if $_ eq 'commonsense.t'; }, |
bb7dc48b |
39 | untaint => 1, untaint_pattern => qr|^(.+)$|}, File::Spec->curdir); |
3fa6e24b |
40 | |
b695f709 |
41 | finddepth({wanted => sub { print "ok 2\n" if $_ eq 'commonsense.t'; }, |
bb7dc48b |
42 | untaint => 1, untaint_pattern => qr|^(.+)$|}, |
43 | File::Spec->curdir); |
3fa6e24b |
44 | |
45 | my $case = 2; |
46 | my $FastFileTests_OK = 0; |
47 | |
48 | sub cleanup { |
49 | if (-d dir_path('for_find')) { |
50 | chdir(dir_path('for_find')); |
51 | } |
52 | if (-d dir_path('fa')) { |
53 | unlink file_path('fa', 'fa_ord'), |
54 | file_path('fa', 'fsl'), |
55 | file_path('fa', 'faa', 'faa_ord'), |
56 | file_path('fa', 'fab', 'fab_ord'), |
57 | file_path('fa', 'fab', 'faba', 'faba_ord'), |
58 | file_path('fb', 'fb_ord'), |
59 | file_path('fb', 'fba', 'fba_ord'); |
60 | rmdir dir_path('fa', 'faa'); |
61 | rmdir dir_path('fa', 'fab', 'faba'); |
62 | rmdir dir_path('fa', 'fab'); |
63 | rmdir dir_path('fa'); |
64 | rmdir dir_path('fb', 'fba'); |
65 | rmdir dir_path('fb'); |
66 | chdir File::Spec->updir; |
67 | rmdir dir_path('for_find'); |
68 | } |
69 | } |
70 | |
71 | END { |
72 | cleanup(); |
73 | } |
74 | |
75 | sub Check($) { |
76 | $case++; |
77 | if ($_[0]) { print "ok $case\n"; } |
78 | else { print "not ok $case\n"; } |
79 | } |
80 | |
81 | sub CheckDie($) { |
82 | $case++; |
83 | if ($_[0]) { print "ok $case\n"; } |
84 | else { print "not ok $case\n $!\n"; exit 0; } |
85 | } |
86 | |
87 | sub touch { |
88 | CheckDie( open(my $T,'>',$_[0]) ); |
89 | } |
90 | |
91 | sub MkDir($$) { |
92 | CheckDie( mkdir($_[0],$_[1]) ); |
93 | } |
94 | |
95 | sub wanted_File_Dir { |
96 | print "# \$File::Find::dir => '$File::Find::dir'\n"; |
97 | print "# \$_ => '$_'\n"; |
98 | s#\.$## if ($^O eq 'VMS' && $_ ne '.'); |
99 | Check( $Expect_File{$_} ); |
100 | if ( $FastFileTests_OK ) { |
101 | delete $Expect_File{ $_} |
102 | unless ( $Expect_Dir{$_} && ! -d _ ); |
103 | } else { |
104 | delete $Expect_File{$_} |
105 | unless ( $Expect_Dir{$_} && ! -d $_ ); |
106 | } |
107 | } |
108 | |
109 | sub wanted_File_Dir_prune { |
110 | &wanted_File_Dir; |
111 | $File::Find::prune=1 if $_ eq 'faba'; |
112 | } |
113 | |
114 | |
115 | sub 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 | |
138 | sub 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 | |
171 | sub 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 | |
191 | sub 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 | |
230 | sub file_path_name { |
231 | my $path = file_path(@_); |
232 | $path = ":$path" if (($^O eq 'MacOS') && ($path !~ /:/)); |
233 | return $path; |
234 | } |
235 | |
236 | |
237 | |
238 | MkDir( dir_path('for_find'), 0770 ); |
239 | CheckDie(chdir( dir_path('for_find'))); |
240 | |
241 | $cwd = cwd(); # save cwd |
242 | ( $cwd_untainted ) = $cwd =~ m|^(.+)$|; # untaint it |
243 | |
244 | MkDir( dir_path('fa'), 0770 ); |
245 | MkDir( dir_path('fb'), 0770 ); |
246 | touch( file_path('fb', 'fb_ord') ); |
247 | MkDir( dir_path('fb', 'fba'), 0770 ); |
248 | touch( file_path('fb', 'fba', 'fba_ord') ); |
249 | if ($^O eq 'MacOS') { |
250 | CheckDie( symlink(':fb',':fa:fsl') ) if $symlink_exists; |
251 | } else { |
252 | CheckDie( symlink('../fb','fa/fsl') ) if $symlink_exists; |
253 | } |
254 | touch( file_path('fa', 'fa_ord') ); |
255 | |
256 | MkDir( dir_path('fa', 'faa'), 0770 ); |
257 | touch( file_path('fa', 'faa', 'faa_ord') ); |
258 | MkDir( dir_path('fa', 'fab'), 0770 ); |
259 | touch( file_path('fa', 'fab', 'fab_ord') ); |
260 | MkDir( dir_path('fa', 'fab', 'faba'), 0770 ); |
261 | touch( file_path('fa', 'fab', 'faba', 'faba_ord') ); |
262 | |
3fa6e24b |
263 | print "# check untainting (no follow)\n"; |
264 | |
265 | # untainting here should work correctly |
bb7dc48b |
266 | |
267 | %Expect_File = (File::Spec->curdir => 1, file_path('fsl') => |
268 | 1,file_path('fa_ord') => 1, file_path('fab') => 1, |
269 | file_path('fab_ord') => 1, file_path('faba') => 1, |
3fa6e24b |
270 | file_path('faa') => 1, file_path('faa_ord') => 1); |
271 | delete $Expect_File{ file_path('fsl') } unless $symlink_exists; |
272 | %Expect_Name = (); |
bb7dc48b |
273 | |
274 | %Expect_Dir = ( dir_path('fa') => 1, dir_path('faa') => 1, |
275 | dir_path('fab') => 1, dir_path('faba') => 1, |
3fa6e24b |
276 | dir_path('fb') => 1, dir_path('fba') => 1); |
bb7dc48b |
277 | |
3fa6e24b |
278 | delete @Expect_Dir{ dir_path('fb'), dir_path('fba') } unless $symlink_exists; |
bb7dc48b |
279 | |
280 | File::Find::find( {wanted => \&wanted_File_Dir_prune, untaint => 1, |
281 | untaint_pattern => qr|^(.+)$|}, topdir('fa') ); |
282 | |
3fa6e24b |
283 | Check( scalar(keys %Expect_File) == 0 ); |
284 | |
285 | |
286 | # don't untaint at all, should die |
287 | %Expect_File = (); |
288 | %Expect_Name = (); |
289 | %Expect_Dir = (); |
290 | undef $@; |
291 | eval {File::Find::find( {wanted => \&simple_wanted}, topdir('fa') );}; |
292 | Check( $@ =~ m|Insecure dependency| ); |
293 | chdir($cwd_untainted); |
294 | |
295 | |
296 | # untaint pattern doesn't match, should die |
297 | undef $@; |
bb7dc48b |
298 | |
3fa6e24b |
299 | eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1, |
bb7dc48b |
300 | untaint_pattern => qr|^(NO_MATCH)$|}, |
301 | topdir('fa') );}; |
302 | |
3fa6e24b |
303 | Check( $@ =~ m|is still tainted| ); |
304 | chdir($cwd_untainted); |
305 | |
306 | |
307 | # untaint pattern doesn't match, should die when we chdir to cwd |
308 | print "# check untaint_skip (no follow)\n"; |
309 | undef $@; |
bb7dc48b |
310 | |
311 | eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1, |
312 | untaint_skip => 1, untaint_pattern => |
313 | qr|^(NO_MATCH)$|}, topdir('fa') );}; |
314 | |
3fa6e24b |
315 | Check( $@ =~ m|insecure cwd| ); |
316 | chdir($cwd_untainted); |
317 | |
318 | |
319 | if ( $symlink_exists ) { |
bb7dc48b |
320 | print "# --- symbolic link tests --- \n"; |
3fa6e24b |
321 | $FastFileTests_OK= 1; |
322 | |
323 | print "# check untainting (follow)\n"; |
324 | |
325 | # untainting here should work correctly |
326 | # no_chdir is in effect, hence we use file_path_name to specify the expected paths for %Expect_File |
bb7dc48b |
327 | |
328 | %Expect_File = (file_path_name('fa') => 1, |
329 | file_path_name('fa','fa_ord') => 1, |
330 | file_path_name('fa', 'fsl') => 1, |
331 | file_path_name('fa', 'fsl', 'fb_ord') => 1, |
332 | file_path_name('fa', 'fsl', 'fba') => 1, |
333 | file_path_name('fa', 'fsl', 'fba', 'fba_ord') => 1, |
334 | file_path_name('fa', 'fab') => 1, |
335 | file_path_name('fa', 'fab', 'fab_ord') => 1, |
336 | file_path_name('fa', 'fab', 'faba') => 1, |
337 | file_path_name('fa', 'fab', 'faba', 'faba_ord') => 1, |
338 | file_path_name('fa', 'faa') => 1, |
339 | file_path_name('fa', 'faa', 'faa_ord') => 1); |
340 | |
3fa6e24b |
341 | %Expect_Name = (); |
bb7dc48b |
342 | |
343 | %Expect_Dir = (dir_path('fa') => 1, |
344 | dir_path('fa', 'faa') => 1, |
345 | dir_path('fa', 'fab') => 1, |
346 | dir_path('fa', 'fab', 'faba') => 1, |
347 | dir_path('fb') => 1, |
348 | dir_path('fb', 'fba') => 1); |
349 | |
350 | File::Find::find( {wanted => \&wanted_File_Dir, follow_fast => 1, |
351 | no_chdir => 1, untaint => 1, untaint_pattern => |
352 | qr|^(.+)$| }, topdir('fa') ); |
353 | |
3fa6e24b |
354 | Check( scalar(keys %Expect_File) == 0 ); |
355 | |
356 | |
357 | # don't untaint at all, should die |
358 | undef $@; |
bb7dc48b |
359 | |
360 | eval {File::Find::find( {wanted => \&simple_wanted, follow => 1}, |
361 | topdir('fa') );}; |
362 | |
3fa6e24b |
363 | Check( $@ =~ m|Insecure dependency| ); |
364 | chdir($cwd_untainted); |
365 | |
366 | # untaint pattern doesn't match, should die |
367 | undef $@; |
bb7dc48b |
368 | |
369 | eval {File::Find::find( {wanted => \&simple_wanted, follow => 1, |
370 | untaint => 1, untaint_pattern => |
371 | qr|^(NO_MATCH)$|}, topdir('fa') );}; |
372 | |
3fa6e24b |
373 | Check( $@ =~ m|is still tainted| ); |
374 | chdir($cwd_untainted); |
375 | |
376 | # untaint pattern doesn't match, should die when we chdir to cwd |
377 | print "# check untaint_skip (follow)\n"; |
378 | undef $@; |
bb7dc48b |
379 | |
380 | eval {File::Find::find( {wanted => \&simple_wanted, untaint => 1, |
381 | untaint_skip => 1, untaint_pattern => |
382 | qr|^(NO_MATCH)$|}, topdir('fa') );}; |
383 | |
3fa6e24b |
384 | Check( $@ =~ m|insecure cwd| ); |
385 | chdir($cwd_untainted); |
386 | |
387 | } |
388 | |