Skip to content

Commit

Permalink
filter_var to sanitise URL
Browse files Browse the repository at this point in the history
  • Loading branch information
chuajianshen committed Dec 18, 2024
1 parent 445f6c8 commit a328033
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Browsershot.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,10 @@ public function waitForSelector(string $selector, array $options = []): static

public function setUrl(string $url): static
{
$url = trim($url);

if (filter_var($url, FILTER_VALIDATE_URL) === false ){
throw FileUrlNotAllowed::parseError();
}

$unsupportedProtocols = [
'file://',
'file:/',
Expand Down
5 changes: 5 additions & 0 deletions src/Exceptions/FileUrlNotAllowed.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ public static function make(): static
{
return new static('An URL is not allow to start with file:// or file:/');
}

public static function parseError(): static
{
return new static('URL parse error');
}
}
5 changes: 5 additions & 0 deletions tests/BrowsershotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@
Browsershot::url('file:/test');
})->throws(FileUrlNotAllowed::class);

it('will not allow a slightly malformed file url', function () {
Browsershot::url('fil
e:///test');
})->throws(FileUrlNotAllowed::class);

it('will not allow html to contain file:/', function () {
Browsershot::html('<h1><img src="file:/" /></h1>');
})->throws(HtmlIsNotAllowedToContainFile::class);
Expand Down

0 comments on commit a328033

Please sign in to comment.