Skip to content

[HtmlSanitizer] Add HtmlSanitizerConfig::withMaxInputLength() #46062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2243,6 +2243,10 @@ private function addHtmlSanitizerSection(ArrayNodeDefinition $rootNode, callable
->info('Unregisters custom attribute sanitizers.')
->scalarPrototype()->end()
->end()
->integerNode('max_input_length')
->info('The maximum length allowed for the sanitized input.')
->defaultValue(0)
->end()
->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2734,6 +2734,10 @@ private function registerHtmlSanitizerConfiguration(array $config, ContainerBuil
$def->addMethodCall('withoutAttributeSanitizer', [new Reference($serviceName)], true);
}

if ($sanitizerConfig['max_input_length']) {
$def->addMethodCall('withMaxInputLength', [$sanitizerConfig['max_input_length']], true);
}

// Create the sanitizer and link its config
$sanitizerId = 'html_sanitizer.sanitizer.'.$sanitizerName;
$container->register($sanitizerId, HtmlSanitizer::class)->addArgument(new Reference($configId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,7 @@
<xsd:attribute name="force-https-urls" type="xsd:boolean" />
<xsd:attribute name="allow-relative-links" type="xsd:boolean" />
<xsd:attribute name="allow-relative-medias" type="xsd:boolean" />
<xsd:attribute name="max-input-length" type="xsd:positiveInteger" />
</xsd:complexType>

<xsd:complexType name="element-option">
Expand Down
8 changes: 3 additions & 5 deletions src/Symfony/Component/HtmlSanitizer/HtmlSanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,16 @@
final class HtmlSanitizer implements HtmlSanitizerInterface
{
private HtmlSanitizerConfig $config;
private int $maxInputLength;
private ParserInterface $parser;

/**
* @var array<string, DomVisitor>
*/
private array $domVisitors = [];

public function __construct(HtmlSanitizerConfig $config, int $maxInputLength = 20000, ParserInterface $parser = null)
public function __construct(HtmlSanitizerConfig $config, ParserInterface $parser = null)
{
$this->config = $config;
$this->maxInputLength = $maxInputLength;
$this->parser = $parser ?? new MastermindsParser();
}

Expand Down Expand Up @@ -64,8 +62,8 @@ private function sanitizeWithContext(string $context, string $input): string
$this->domVisitors[$context] ??= $this->createDomVisitorForContext($context);

// Prevent DOS attack induced by extremely long HTML strings
if (\strlen($input) > $this->maxInputLength) {
$input = substr($input, 0, $this->maxInputLength);
if (\strlen($input) > $this->config->getMaxInputLength()) {
$input = substr($input, 0, $this->config->getMaxInputLength());
}

// Only operate on valid UTF-8 strings. This is necessary to prevent cross
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/HtmlSanitizer/HtmlSanitizerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class HtmlSanitizerConfig
*/
private array $attributeSanitizers;

private int $maxInputLength = 20_000;

public function __construct()
{
$this->attributeSanitizers = [
Expand Down Expand Up @@ -405,6 +407,19 @@ public function withoutAttributeSanitizer(AttributeSanitizerInterface $sanitizer
return $clone;
}

public function withMaxInputLength(int $maxInputLength): static
{
$clone = clone $this;
$clone->maxInputLength = $maxInputLength;

return $clone;
}

public function getMaxInputLength(): int
{
return $this->maxInputLength;
}

/**
* @return array<string, array<string, true>>
*/
Expand Down