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
Use mojo.js esq absolute check in controller
  • Loading branch information
rawleyfowler committed Sep 20, 2023
1 parent 2c8d1b6 commit 412bef0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/Mojolicious/Controller.pm
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,12 @@ sub url_for {

sub url_for_asset {
my ($self, $asset) = @_;
return $self->url_for($self->app->static->asset_path($asset));
return $asset =~ m!^(?:[^:/?#]+:|//|#)! ? $asset : $self->url_for($self->app->static->asset_path($asset));
}

sub url_for_file {
my ($self, $file) = @_;
return $self->url_for($self->app->static->file_path($file));
return $file =~ m!^(?:[^:/?#]+:|//|#)! ? $file : $self->url_for($self->app->static->file_path($file));
}

sub write {
Expand Down
8 changes: 6 additions & 2 deletions lib/Mojolicious/Static.pm
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,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 Down Expand Up @@ -146,7 +148,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
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 412bef0

Please sign in to comment.