Skip to content
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

filter_var to sanitise URL #908

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
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::urlCannotBeParsed($url);
}

$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 urlCannotBeParsed(string $url): static
{
return new static("The given URL `{$url}` is not a valid URL");
}
}
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 malformed file url witht too many slashes', 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
Loading