Skip to content

Commit

Permalink
Add absolute check to file_path and asset_path
Browse files Browse the repository at this point in the history
  • Loading branch information
rawleyfowler committed Sep 20, 2023
1 parent 2c8d1b6 commit 3239b99
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
19 changes: 17 additions & 2 deletions lib/Mojolicious/Static.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ has 'prefix';

sub asset_path {
my ($self, $asset) = @_;

return $asset if $self->_is_abs($asset);

$asset = "/$asset" unless $asset =~ /^\//;
my $assets = $self->{assets} //= {};
return $self->file_path('/' . $self->asset_dir . ($assets->{$asset} // $asset));
Expand Down Expand Up @@ -64,7 +67,9 @@ sub file {
}

# Search DATA
if (my $asset = $self->_get_data_file($rel)) { return $asset }
if (my $asset = $self->_get_data_file($rel)) {
return $asset;
}

# Search extra files
my $extra = $self->extra;
Expand All @@ -73,6 +78,9 @@ sub file {

sub file_path {
my ($self, $file) = @_;

return $file if $self->_is_abs($file);

$file = "/$file" unless $file =~ /^\//;
return $file unless my $prefix = $self->prefix;
return "$prefix$file";
Expand Down Expand Up @@ -146,7 +154,9 @@ sub warmup {

# DATA sections
my $index = $self->{index} = {};
for my $class (reverse @{$self->classes}) { $index->{$_} = $class for keys %{data_section $class} }
for my $class (reverse @{$self->classes}) {
$index->{$_} = $class for keys %{data_section $class};
}

# Static assets
my $assets = $self->{assets} = {};
Expand Down Expand Up @@ -190,6 +200,11 @@ sub _get_file {
return -f $path && -r _ ? Mojo::Asset::File->new(path => $path) : undef;
}

sub _is_abs {
my ($self, $file) = @_;
return $file =~ m!^(?:[^:/?#]+:|//|#)!;
}

1;

=encoding utf8
Expand Down
5 changes: 3 additions & 2 deletions t/mojolicious/static_prefix_lite_app.t
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ subtest 'UTF-8 encoded inline file' => sub {

subtest 'File' => sub {
my $c = $t->app->build_controller;
is $c->url_for_file('/unknown.css')->path, '/static/unknown.css', 'right file path';
is $c->url_for_file('/foo/bar.css')->path, '/static/foo/bar.css', 'right file path';
is $c->url_for_file('/unknown.css')->path, '/static/unknown.css', 'right file path';
is $c->url_for_file('/foo/bar.css')->path, '/static/foo/bar.css', 'right file path';
is $c->url_for_file('https://somesite.com/file.css'), 'https://somesite.com/file.css', 'right absolute file path?';
};

done_testing();
Expand Down

0 comments on commit 3239b99

Please sign in to comment.