-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 NEFARIOUSPLAN-CANONICAL-V1 {"body_md":"## The sanitizer extends a parser. The parser has two attribute writers.\n\nThe html-sanitizer's job is XSS safety. To get there, it does not write its own HTML5 serializer. It extends `Masterminds\\HTML5\\Serializer\\OutputRules`, the part of the `masterminds/html5` library that walks a parsed DOM tree and emits HTML5-conformant output. `OutputRules` is in the dependency tree of every TYPO3 installation from 10.4 through 14.3, by way of `typo3/html-sanitizer`.\n\n`OutputRules` ships two methods that write `name=\"value\"` attribute pairs onto the output stream. One is `attrs()`, for regular HTML attributes. The other is `namespaceAttrs()`, for XML namespace declarations like `xmlns:x=\"...\"`.\n\n`attrs()` encodes its values:\n\n```php\nprotected function attrs($ele)\n{\n // FIXME: Needs support for xml, xmlns, xlink, and namespaced elements.\n if (!$ele->hasAttributes()) {\n return $this;\n }\n $map = $ele->attributes;\n $len = $map->length;\n for ($i = 0; $i < $len; ++$i) {\n $node = $map->item($i);\n $val = $this->enc($node->value, true);\n // ...\n $this->wr(' ')->wr($name);\n if ((isset($val) && '' !== $val) || $this->nonBooleanAttribute($node)) {\n $this->wr('=\"')->wr($val)->wr('\"');\n }\n }\n}\n```\n\n`$this->enc($node->value, true)` is the line that turns a literal `\"` in the attribute value into `"` before it reaches the stream.\n\n`namespaceAttrs()`, in the same file, does not:\n\n```php\nprotected function namespaceAttrs($ele)\n{\n if (!$this->xpath || $this->xpath->document !== $ele->ownerDocument) {\n $this->xpath = new \\DOMXPath($ele->ownerDocument);\n }\n\n foreach ($this->xpath->query('namespace::*[not(.=../../namespace::*)]', $ele) as $nsNode) {\n if (!in_array($nsNode->nodeValue, $this->implicitNamespaces)) {\n $this->wr(' ')->wr($nsNode->nodeName)->wr('=\"')->wr($nsNode->nodeValue)->wr('\"');\n }\n }\n}\n```\n\n`$this->wr($nsNode->nodeValue)`. No `enc()`. The decoded value travels to the output stream as-is.\n\nMasterminds writes parsers and serializers, not sanitizers. The library's contract is HTML5 conformance, and `namespaceAttrs()` produces conformant HTML5 even when its value contains a literal `>`. The browser will parse that output differently than the sanitizer intended, but the bytes are valid. The FIXME comment at the top of `attrs()` (`// FIXME: Needs support for xml, xmlns, xlink, and namespaced elements.`) tells you the library knows the two writers diverge on namespaced output. It does not promise the divergence is safe.\n\n## The sanitizer overrode the encoder. It did not override the sister writer.\n\nWhen TYPO3 introduced `src/Serializer/Rules.php` on 2022-11-28 (commit `703dded`, `[!!!][FEATURE] Allow to use custom output rules`), the author identified `enc()` as the safety primitive worth hardening. The override is 20 lines and arrives with a comment that names the choice explicitly:\n\n```php\nprotected function enc($text, $attribute = false): string\n{\n if ($attribute && $this->encodeAttributes && !$this->encode) {\n // In contrast to parent::enc() (when $this->encode is true),\n // we are using htmlspecialchars() instead of htmlentities() as\n // colons and slashes do not need to be aggressively escaped.\n $value = htmlspecialchars(\n $text,\n ENT_HTML5 | ENT_SUBSTITUTE | ENT_QUOTES,\n 'UTF-8',\n // ...\n true\n );\n return $value;\n }\n return parent::enc($text, $attribute);\n}\n```\n\nThe override reaches past `parent::enc()` to install a strictly typed `htmlspecialchars()` call. The comment names UTF-8 substitution, named entity policy, and the disable-automagic flag. The author understood that `enc()` was the safety primitive, and that the parent class's encoder was not the encoder the sanitizer wanted.\n\nThe same file does not override `namespaceAttrs()`. The list of methods marked `#[\\Override]` in `Rules.php` is short and intentional: `text()`, `enc()`, `resolveNodeName()`, `shallAllowInsecureRawText()`, `isRawText()`, `isVoid()`. That list is the threat model. `namespaceAttrs()` is not on it.\n\nSo for the three years and five months between the introduction of the `Rules` subclass and 2026-04-27, every `xmlns:*` declaration the sanitizer saw was written to the output by the parent class's unmodified `namespaceAttrs()`. The carefully hardened encoder lived next door. The sister writer never called it.\n\n## The PoC is the test case the patch shipped with itself.\n\nThere is no separate PoC repository for CVE-2026-47345. The patch commit, `8b5d0be`, adds five lines to `tests/CommonBuilderTest.php` under generator key `#950`:\n\n```php\nyield '#950' => [\n '
text
',\n '
text
',\n];\n```\n\nThe first string is the input. The second string is the expected output after sanitization. Before the patch, the assertion failed because the output was not the second string. It was this:\n\n```html\n
\">text
\n```\n\nWalk through it. The HTML5 parser sees `xmlns:x=\""><img src=x onerror=alert(document.domain)>\"`. It decodes the entities and stores the decoded form on the DOM node as the namespace value: `\">`. The sanitizer's visitor pipeline runs. The `xmlns:x` namespace declaration is not on any deny list because `CommonBuilder` does not filter namespace declarations. The serializer reaches the element, calls `namespaceAttrs()` on the parent class, and emits:\n\n```\n xmlns:x=\"\n```\n\nthen `$this->wr($nsNode->nodeValue)`, which writes the decoded value verbatim. The decoded value is `\">`. The first byte is `\"`, which closes the attribute the writer just opened. The next bytes are HTML the browser parses as a new element. The `` enters the DOM. The `onerror` handler fires. The sanitizer's output, fed into a page that trusted it to be safe, executes attacker-supplied JavaScript in the context of the document the user is reading.\n\nThe encoder that would have turned that `\"` into `"` exists in the same file. The sanitizer hardened it on day one. `namespaceAttrs()` did not reach for it.\n\n## The patch is one method.\n\n`src/Serializer/Rules.php` after the patch:\n\n```diff\n+ /**\n+ * Write the namespace attributes.\n+ *\n+ * @param \\DOMNode $ele The element being written.\n+ */\n+ #[\\Override]\n+ protected function namespaceAttrs($ele)\n+ {\n+ $xpath = new \\DOMXPath($ele->ownerDocument);\n+\n+ foreach ($xpath->query('namespace::*[not(.=../../namespace::*)]', $ele) as $nsNode) {\n+ if (!in_array($nsNode->nodeValue, $this->implicitNamespaces)) {\n+ $this->wr(' ')->wr($nsNode->nodeName)->wr('=\"')->wr($this->enc($nsNode->nodeValue, true))->wr('\"');\n+ }\n+ }\n+ }\n```\n\nSeventeen lines. The XPath query is the parent class's XPath query. The `implicitNamespaces` filter is the parent class's filter. The `$this->wr(' ')->wr($nsNode->nodeName)->wr('=\"')` prefix is the parent class's prefix. The single mechanical change is the value write: `$this->wr($this->enc($nsNode->nodeValue, true))` in place of `$this->wr($nsNode->nodeValue)`. One function call. The function is the override the sanitizer added in 2022.\n\nThe fix is one method override marked `#[\\Override]`, added to the list of methods that needed overriding. The list grew by one.\n\n## The library hands you a class. The unsafe sinks stay in the parent.\n\nThis is [extension-as-mitigation](/patterns/extension-as-mitigation). The library `masterminds/html5` exposes `OutputRules` as a subclass-able extension point and tells the consumer, via the [!!!][FEATURE] commit that introduced the seam in `typo3/html-sanitizer` itself, that the way to customize serializer behavior is to subclass it. The TYPO3 sanitizer authors took the seam. They identified the methods whose behavior they needed to change for safety, and overrode those. The list lives in `src/Serializer/Rules.php`, every method tagged `#[\\Override]`. The list looks complete.\n\nIt was not. Masterminds does not enumerate which methods in `OutputRules` need to be safe-overridden when the class is used as a sanitizer base. Masterminds does not promise that any method in `OutputRules` is safe for that purpose. The contract is HTML5 conformance. The unsafe sinks remain in the parent class, unchanged, because no one wrote them down as \"these need overriding for safety.\" The patch closes one. The next sibling method the override list does not name is the next CVE.\n\nThe same component shipped a second CVE the same day. TYPO3-CORE-SA-2026-006 assigned [CVE-2026-47344](https://nvd.nist.gov/vuln/detail/CVE-2026-47344) against the same `typo3/html-sanitizer` versions, by the same researchers (IPC Labs and Doyensec, with Anthropic Research). CVE-2026-47344 is a browser-parser differential: when `ALLOW_INSECURE_RAW_TEXT` is set on a tag, the sanitizer's tokenizer accepts `` as the closing tag but does not accept ``. The browser does. The fix is a custom `Tokenizer` subclass that overrides Masterminds' `rawText()`, plus a guard in `Rules.php` that denies raw-text passthrough inside `