HtmlSanitizerConfigBuilder silently inherits Symfony's 20,000-byte input cap, causing content truncation with no warning
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3607368. -->
Reported by: [webbywe](https://www.drupal.org/user/571194)
Related to !3
>>>
<h2>Problem</h2>
<p>Symfony's <code>HtmlSanitizerConfig</code> ships with a default<br>
<code>maxInputLength</code> of <strong>20,000 bytes</strong>. This cap was<br>
designed to protect public-facing web forms from denial-of-service attacks<br>
via enormous user-submitted strings. <code>HtmlSanitizerConfigBuilder::build()</code><br>
never calls <code>withMaxInputLength()</code>, so every sanitizer instance<br>
silently inherits this 20 kB limit.</p>
<p>When the HTML fragment passed to the sanitizer exceeds 20,000 bytes,<br>
Symfony executes:</p>
<pre><pre>$input = substr($input, 0, $this->config->getMaxInputLength());</pre></pre><p>This is a <strong>raw byte truncation of the input string</strong> before any<br>
parsing occurs. The truncated string is then handed to PHP's <code>DOMDocument</code>,<br>
which parses the broken markup, auto-closes all open tags, and serializes a<br>
structurally valid but content-incomplete HTML fragment. No warning is logged.<br>
No exception is thrown. The pipeline accepts the truncated output and continues.</p>
<h3>Steps to reproduce</h3>
<ol>
<li>Configure a migration using <code>html_processor</code> with a<br>
<code>container</code> selector that returns a large HTML section (e.g. a<br>
page wrapper containing navigation, description, subjects, and a multi-row<br>
editions table). The sanitizer runs at Step 5, before minification at<br>
Step 7, so the cap applies to the raw extracted HTML — which can be<br>
significantly larger than the final minified output.</li>
<li>Enable a sanitizer option such as <code>allowStaticElements: true</code>.</li>
<li>Run the migration against a URL that returns a large page.</li>
<li>Compare the <code>processed-html-*.html</code> debug output file with the<br>
original HTML fetched from the URL.</li>
</ol>
<h3>Expected result</h3>
<p>The sanitized output contains all content that the sanitizer's allowlist permits.</p>
<h3>Actual result</h3>
<p>The output is silently truncated at the 20,000-byte mark. In the observed case<br>
the truncation occurred inside an HTML attribute value<br>
(<code>alt="Cover of: Die Mars-Chronike"</code> instead of<br>
<code>"Die Mars-Chroniken"</code>), followed by DOMDocument auto-closing all open<br>
tags. Several table rows present in the input were absent from the output. No<br>
warning was logged. The migration reported success.</p>
<h2>Root cause</h2>
<p><code>HtmlSanitizerConfig::$maxInputLength</code> defaults to<br>
<code>20_000</code> (Symfony source, line 102). <code>HtmlSanitizer::sanitizeFor()</code><br>
applies this cap via <code>substr()</code> before parsing:</p>
<pre><pre>// Prevent DOS attack induced by extremely long HTML strings<br>if (-1 !== $this->config->getMaxInputLength() &amp;&amp; strlen($input) &gt; $this->config->getMaxInputLength()) {<br> $input = substr($input, 0, $this->config->getMaxInputLength());<br>}</pre></pre><p><code>HtmlSanitizerConfigBuilder::build()</code> never overrides this default.<br>
Since <code>html_processor</code> is a server-side content pipeline — not a<br>
web-form input sanitizer — the DOS protection is inappropriate: content is already<br>
scoped by the <code>container</code> step before reaching the sanitizer, and<br>
the source URLs are administrator-configured, not anonymous-user-supplied.</p>
<h2>Fix</h2>
<p>In <code>HtmlSanitizerConfigBuilder::build()</code>, initialize the config with<br>
the cap disabled:</p>
<pre><pre>// Symfony's default maxInputLength is 20,000 bytes — designed for web-form<br>// DOS protection, not server-side content pipelines. Disable it. Callers can<br>// re-enable via the maxInputLength sanitizer option.<br>$config = (new HtmlSanitizerConfig())-&gt;withMaxInputLength(-1);</pre></pre><p>Additionally, expose <code>maxInputLength</code> as a first-class sanitizer<br>
option so callers operating in untrusted contexts can set an explicit cap:</p>
<pre><pre>sanitizer:<br> allowStaticElements: true<br> maxInputLength: 65536 # bytes; -1 = no cap (module default)</pre></pre><h2>Impact</h2>
<ul>
<li>Content is silently dropped from the pipeline output with no log entry and<br>
no exception. AI migration pipelines receive and process truncated HTML<br>
believing it to be complete.</li>
<li>The bug is invisible at the migration level: the migration reports success,<br>
field values are populated, and nodes are created — but from incomplete<br>
source data.</li>
<li>Truncation is non-deterministic: it depends on the byte size of the live<br>
page fragment at fetch time, making it difficult to reproduce against static<br>
fixtures.</li>
</ul>
<h2>Environment</h2>
<ul>
<li><code>html_processor</code>: ^2.0 (2.0.0-rc1)</li>
<li>Drupal: ^10 || ^11</li>
<li>PHP: 8.2+</li>
<li>Symfony <code>HtmlSanitizer</code>: ^7.0 (<code>HtmlSanitizerConfig::$maxInputLength = 20_000</code> at line 102)</li>
</ul>
issue
GitLab AI Context
Project: project/html_processor
Instance: https://git.drupalcode.org
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://git.drupalcode.org/project/html_processor/-/raw/1.0.x/CONTRIBUTING.md — contribution guidelines
- https://git.drupalcode.org/project/html_processor/-/raw/1.0.x/README.md — project overview and setup
Repository: https://git.drupalcode.org/project/html_processor
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD