Skip to content

Protect static file routes from directory traversal attacks. #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions lib/RenderApp/Controller/StaticFiles.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,34 @@ use Mojo::Base 'Mojolicious::Controller', -signatures;

use Mojo::File qw(path);

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

# Route requests for pg_files/CAPA_Graphics to render root Contrib/CAPA/CAPA_Graphics
sub CAPA_graphics_file ($c) {
return $c->reply_with_file_if_readable($c->app->home->child('Contrib/CAPA/CAPA_Graphics', $c->stash('static')));
return $c->reply_with_file_if_readable($c->app->home->child('Contrib/CAPA/CAPA_Graphics'), $c->stash('static'));
}

# Route requests for pg_files to the render root tmp. The
# only requests should be for files in the temporary directory.
# FIXME: Perhaps this directory should be configurable.
sub temp_file ($c) {
$c->reply_with_file_if_readable($c->app->home->child('tmp', $c->stash('static')));
return $c->reply_with_file_if_readable($c->app->home->child('tmp'), $c->stash('static'));
}

# Route request to pg_files to lib/PG/htdocs.
sub pg_file ($c) {
$c->reply_with_file_if_readable(path($ENV{PG_ROOT}, 'htdocs', $c->stash('static')));
return $c->reply_with_file_if_readable(path($ENV{PG_ROOT}, 'htdocs'), $c->stash('static'));
}

sub public_file ($c) {
$c->reply_with_file_if_readable($c->app->home->child('public', $c->stash('static')));
$c->reply_with_file_if_readable($c->app->home->child('public'), $c->stash('static'));
}

1;