Skip to content

Commit b524241

Browse files
committed
improve the security service
1 parent ddb547d commit b524241

4 files changed

Lines changed: 36 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [5.3.3](https://github.com/spipu/html2pdf/compare/v5.3.2...v5.3.3) - 2025-06-04
6+
7+
* improve the security service
8+
59
## [5.3.2](https://github.com/spipu/html2pdf/compare/v5.3.1...v5.3.2) - 2025-04-25
610

711
* add readonly attribute support for input and textarea - thanks to @kkevinchoo

doc/security.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ You can add a specific host to be allowed for http/https scheme. By default, the
1515
$html2pdf->getSecurityService()->addAllowedHost('www.html2pdf.fr');
1616
```
1717

18+
You can reset the list of allowed hosts for http/https scheme.
19+
20+
```php
21+
$html2pdf->getSecurityService()->resetAllowedHosts();
22+
```
23+
24+
You can disable the check on the allowed hosts for http/https scheme.
25+
26+
```php
27+
$html2pdf->getSecurityService()->disableCheckAllowedHosts();
28+
```
29+
1830
You must ensure that the HTML you want to convert is secure, **especially if it is generated from uncontrolled data contributed by users**.
1931
In such cases, an attacker could send requests to both external servers and restricted-access servers (e.g., within a local network) on host that you have added to the whitelist.
2032

src/Html2Pdf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public function getVersionAsArray()
259259
return array(
260260
'major' => 5,
261261
'minor' => 3,
262-
'revision' => 2,
262+
'revision' => 3,
263263
);
264264
}
265265

src/Security/Security.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class Security implements SecurityInterface
2626
*/
2727
protected $allowedHosts = [];
2828

29+
/**
30+
* @var bool
31+
*/
32+
private $checkAllowedHosts = true;
33+
2934
/**
3035
* @param string $path
3136
* @return void
@@ -38,7 +43,7 @@ public function checkValidPath(string $path): void
3843
throw new HtmlParsingException('Unauthorized path scheme');
3944
}
4045

41-
if (!$this->checkValidPathHost($path)) {
46+
if ($this->checkAllowedHosts && !$this->checkValidPathHost($path)) {
4247
throw new HtmlParsingException('Unauthorized path host on ' . $path . ' => ' . implode('|', $this->allowedHosts));
4348
}
4449
}
@@ -96,4 +101,17 @@ public function addAllowedHost(string $host): void
96101
$this->allowedHosts[] = $host;
97102
}
98103
}
104+
105+
/**
106+
* @return void
107+
*/
108+
public function resetAllowedHosts(): void
109+
{
110+
$this->allowedHosts = [];
111+
}
112+
113+
public function disableCheckAllowedHosts(): void
114+
{
115+
$this->checkAllowedHosts = false;
116+
}
99117
}

0 commit comments

Comments
 (0)