Skip to content

Commit b85d210

Browse files
committed
[HtmlSanitizer] Add support for sanitizing unlimited length of HTML document
1 parent 7402279 commit b85d210

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

src/Symfony/Component/HtmlSanitizer/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.4
5+
---
6+
7+
* Add support for sanitizing unlimited length of HTML document
8+
49
6.1
510
---
611

src/Symfony/Component/HtmlSanitizer/HtmlSanitizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private function sanitizeWithContext(string $context, string $input): string
6060
$this->domVisitors[$context] ??= $this->createDomVisitorForContext($context);
6161

6262
// Prevent DOS attack induced by extremely long HTML strings
63-
if (\strlen($input) > $this->config->getMaxInputLength()) {
63+
if (-1 !== $this->config->getMaxInputLength() && \strlen($input) > $this->config->getMaxInputLength()) {
6464
$input = substr($input, 0, $this->config->getMaxInputLength());
6565
}
6666

src/Symfony/Component/HtmlSanitizer/HtmlSanitizerConfig.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,16 @@ public function withoutAttributeSanitizer(AttributeSanitizerInterface $sanitizer
405405
return $clone;
406406
}
407407

408+
/**
409+
* @param int $maxInputLength The maximum length of the input string in bytes
410+
* -1 means no limit
411+
*/
408412
public function withMaxInputLength(int $maxInputLength): static
409413
{
414+
if ($maxInputLength < -1) {
415+
throw new \InvalidArgumentException(sprintf('The maximum input length must be greater than -1, "%d" given.', $maxInputLength));
416+
}
417+
410418
$clone = clone $this;
411419
$clone->maxInputLength = $maxInputLength;
412420

src/Symfony/Component/HtmlSanitizer/Tests/HtmlSanitizerAllTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,4 +561,15 @@ public static function provideSanitizeBody()
561561
yield $case[0] => $case;
562562
}
563563
}
564+
565+
public function testUnlimitedLength()
566+
{
567+
$sanitizer = new HtmlSanitizer((new HtmlSanitizerConfig())->withMaxInputLength(-1));
568+
569+
$input = str_repeat('a', 10_000_000);
570+
571+
$sanitized = $sanitizer->sanitize($input);
572+
573+
$this->assertSame(strlen($input), strlen($sanitized));
574+
}
564575
}

0 commit comments

Comments
 (0)