Skip to content

Commit e327397

Browse files
committed
validate url
1 parent bcfd608 commit e327397

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

Diff for: src/Browsershot.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Browsershot
6969
protected ImageManipulations $imageManipulations;
7070

7171
protected array $unsafeProtocols = [
72-
'file:,',
72+
'file:',
7373
'file:/',
7474
'file://',
7575
'file:\\',
@@ -268,6 +268,10 @@ public function setUrl(string $url): static
268268
{
269269
$url = trim($url);
270270

271+
if (filter_var($url, FILTER_VALIDATE_URL) === false ){
272+
throw FileUrlNotAllowed::urlCannotBeParsed($url);
273+
}
274+
271275
foreach ($this->unsafeProtocols as $unsupportedProtocol) {
272276
if (str_starts_with(strtolower($url), $unsupportedProtocol)) {
273277
throw FileUrlNotAllowed::make();

Diff for: src/Exceptions/FileUrlNotAllowed.php

+5
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,9 @@ public static function make(): static
1010
{
1111
return new static('An URL is not allow to start with file:// or file:/');
1212
}
13+
14+
public static function urlCannotBeParsed(string $url): static
15+
{
16+
return new static("The given URL `{$url}` is not a valid URL");
17+
}
1318
}

Diff for: tests/BrowsershotTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,17 @@
5959
'File://test',
6060
'file:/test',
6161
'file:\test',
62+
'file:',
6263
'file:\\test',
6364
'view-source',
6465
'View-Source',
6566
]);
6667

68+
it('will not allow a malformed file url with too many slashes', function () {
69+
Browsershot::url('fil
70+
e:///test');
71+
})->throws(FileUrlNotAllowed::class);
72+
6773
it('will not allow a file url that has leading spaces', function () {
6874
Browsershot::url(' file://test');
6975
})->throws(FileUrlNotAllowed::class);

0 commit comments

Comments
 (0)