Skip to content

Commit c07716b

Browse files
committed
Switch the static files to using a simplified path_is_subdir method from webwork2.
The problem with using `realpath` is that static assets can use symbolic links. In fact PG does this with static assets that reside in the same directory as the problem file by default. The `realpath` will not be a subdirectory of the temporary directory in that case. So `canonpath` must be used instead. It does not resolve symbolic links, or even things like `../` in paths. Of course the `path_is_subdir` checks those things.
1 parent eb1f1a2 commit c07716b

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

lib/Renderer/Controller/StaticFiles.pm

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
package Renderer::Controller::StaticFiles;
22
use Mojo::Base 'Mojolicious::Controller', -signatures;
33

4-
use Mojo::File qw(path);
4+
use Mojo::File qw(path);
5+
use File::Spec::Functions qw(canonpath);
6+
7+
sub path_is_subdir ($path, $dir) {
8+
return 0 unless $path =~ /^\//;
9+
10+
$path = canonpath($path);
11+
return 0 if $path =~ m#(^\.\.$|^\.\./|/\.\./|/\.\.$)#;
12+
13+
$dir = canonpath($dir);
14+
return 0 unless $path =~ m|^$dir|;
15+
16+
return 1;
17+
}
518

619
sub reply_with_file_if_readable ($c, $directory, $file) {
720
my $filePath = $directory->child($file);
8-
if (-r $filePath && $filePath->realpath =~ /^$directory/) {
21+
if (-r $filePath && path_is_subdir($filePath, $directory)) {
922
return $c->reply->file($filePath);
1023
} else {
1124
return $c->render(data => 'File not found', status => 404);

0 commit comments

Comments
 (0)