Skip to content

Commit

Permalink
improve detection of file urls
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Dec 13, 2024
1 parent fae8396 commit dfc3635
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/Browsershot.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,12 @@ public function setUrl(string $url): static
{
$url = trim($url);

if (str_starts_with(strtolower($url), 'file://') || str_starts_with(strtolower($url), 'file:/')) {
throw FileUrlNotAllowed::make();
$unsupportedProtocols = ['file://', 'file:/', 'file:\\', 'file:\\\\'];

foreach($unsupportedProtocols as $unsupportedProtocol) {
if (str_starts_with(strtolower($url), $unsupportedProtocol)) {
throw FileUrlNotAllowed::make();
}
}

$this->url = $url;
Expand Down
7 changes: 6 additions & 1 deletion tests/BrowsershotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@

it('will not allow a file url', function () {
Browsershot::url('file://test');
})->throws(FileUrlNotAllowed::class);
})->throws(FileUrlNotAllowed::class)->with([
'file://test',
'file:/test',
'file:\test',
'file:\\test',
]);

it('will not allow a file url that has leading spaces', function () {
Browsershot::url(' file://test');
Expand Down

0 comments on commit dfc3635

Please sign in to comment.