Skip to content

Commit a5813ca

Browse files
committed
Allow http(s) URLs to localhost in HTML
The protocol-relative URL check was too broad - it matched `//` inside `http://[::1]:5173` and similar Vite dev server URLs. Add a negative lookbehind for `:` so `http://` and `https://` URLs are not caught while still blocking bare `//localhost` protocol-relative URLs. Fixes spatie/laravel-pdf#309
1 parent 2390401 commit a5813ca

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/Browsershot.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ public function setHtml(string $html): static
341341
}
342342
}
343343

344-
if (preg_match('#//\s*(localhost[/:\s]|127\.|0\.0\.0\.0[/:\s]|\[::1][/:\s]|::1[/:\s])#i', $content)) {
344+
if (preg_match('#(?<!:)//\s*(localhost[/:\s]|127\.|0\.0\.0\.0[/:\s]|\[::1][/:\s]|::1[/:\s])#i', $content)) {
345345
throw HtmlIsNotAllowedToContainFile::make();
346346
}
347347

tests/BrowsershotTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@
9999
expect($browsershot)->toBeInstanceOf(Browsershot::class);
100100
});
101101

102+
it('will allow html with http urls to localhost', function (string $html) {
103+
$browsershot = Browsershot::html($html);
104+
105+
expect($browsershot)->toBeInstanceOf(Browsershot::class);
106+
})->with([
107+
'<script type="module" src="http://[::1]:5173/@vite/client"></script>',
108+
'<link rel="stylesheet" href="http://localhost:5173/resources/css/app.css">',
109+
'<script type="module" src="http://127.0.0.1:5173/resources/js/app.js"></script>',
110+
'<script type="module" src="https://localhost:5173/@vite/client"></script>',
111+
]);
112+
102113
it('will allow html containing backslashes in css', function (string $html) {
103114
$browsershot = Browsershot::html($html);
104115

0 commit comments

Comments
 (0)