Skip to content

Commit 5301b00

Browse files
feature symfony#52166 [HtmlSanitizer] Add support for sanitizing unlimited length of HTML document (lyrixx)
This PR was merged into the 6.4 branch. Discussion ---------- [HtmlSanitizer] Add support for sanitizing unlimited length of HTML document | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Commits ------- 738450f [HtmlSanitizer] Add support for sanitizing unlimited length of HTML document
2 parents e942c1c + 738450f commit 5301b00

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)