GHSA-X83R-FR5C-QQVJ
SanitizeDom(string) fails to sanitize attributes merged onto <html>/<head>/<body> wrapper elements
Texto original en inglés · Ver en español
Ciclo de vida de la vulnerabilidad
| Fecha | Fase | Evento | Detalle |
|---|---|---|---|
| Remediación | Parche 10.0.1037-beta | HtmlSanitizer · -67 d desde divulgación | |
| Divulgación | Publicada | GHSA-X83R-FR5C-QQVJ divulgada | |
| Monitoreo | Detectada | Ingresó al índice | |
| Monitoreo | Verificada | Última verificación contra las fuentes |
Impact
`HtmlSanitizer.SanitizeDom(string, string)` builds its working document by prepending a fixed wrapper to the caller's input (`"<!doctype html><html><body>" + html`) and then sanitizes starting from `dom.Body`. Per HTML5 tree-construction rules, an `<html>` or `<body>` start tag appearing in the caller's input does not create a new element — it merges its attributes onto the wrapper element that is already open. Because `DoSanitize` walks `context.QuerySelectorAll("*")`, which returns only descendants of the context element, it never inspects the context element itself (`<body>`) or anything above it (`<html>`, `<head>`). Attributes merged onto those three elements are therefore never passed to `IsAllowedAttribute` and are never stripped.
An attacker-controlled input such as:
var s = new HtmlSanitizer();
var dom = s.SanitizeDom("<body onload=alert(1)><p>hi</p>");survives sanitization with a live `onload` handler on the returned document's `<body>` element:
<!DOCTYPE html><html><head></head><body onload="alert(1)"><p>hi</p></body></html>Any application that calls `SanitizeDom(string)` and later serializes or renders more of the returned `IHtmlDocument` than just `dom.Body.InnerHtml`/`ChildNodes` (for example via `dom.ToHtml()`, `dom.DocumentElement.OuterHtml`, or by returning the whole document to a browser) is exposed to stored/reflected cross-site scripting (XSS): script-capable attributes (`onload`, `onerror`, etc.) and other normally-disallowed attributes can be smuggled onto the `<html>`, `<head>`, or `<body>` elements and reach the browser unsanitized.
Callers who only use `SanitizeDom(string)` output through `dom.Body.InnerHtml`, or who use the string-returning `Sanitize(string, ...)` API, are not affected.
Patches
Fixed in commit ab626e7 https://github.com/mganss/HtmlSanitizer/commit/ab626e7a4f2893f7e9ec0a72779ae19c5ec439a7: before sanitizing, `SanitizeDom(string, string)` now strips every attribute from the wrapper elements (`<html>`, `<head>`, `<body>`) that were not present in the original document, since none of those attributes can legitimately come from anywhere but the caller's input.
Users should upgrade to:
- HtmlSanitizer 9.2.1039
https://www.nuget.org/packages/HtmlSanitizer/9.2.1039or - HtmlSanitizer 10.0.1037-beta
https://www.nuget.org/packages/HtmlSanitizer/10.0.1037-betaor later
Workarounds
- Prefer `Sanitize(string html, ...)` over `SanitizeDom(string html, ...)` where possible.
- If you must use `SanitizeDom(string, string)`, only read `dom.Body.InnerHtml` (or iterate `dom.Body.ChildNodes`) from the returned document, and never serialize or use `dom.ToHtml()`, `dom.DocumentElement`, or `dom.Head`/`dom.Body.OuterHtml`.
- As a defense-in-depth measure, explicitly strip all attributes from `dom.DocumentElement`, `dom.Head`, and `dom.Body` immediately after calling `SanitizeDom` and before further use.
References
- Fix commit: ab626e7a4f2893f7e9ec0a72779ae19c5ec439a7
https://github.com/mganss/HtmlSanitizer/commit/ab626e7a4f2893f7e9ec0a72779ae19c5ec439a7 - Patched package: HtmlSanitizer 9.2.1039 on NuGet
https://www.nuget.org/packages/HtmlSanitizer/9.2.1039 - Patched package: HtmlSanitizer 10.0.1037-beta on NuGet
https://www.nuget.org/packages/HtmlSanitizer/10.0.1037-beta
Rangos afectados
Estado del parche por paquete
| Paquete / producto | Estado | Corregida en | Desde divulgación | Última versión |
|---|---|---|---|---|
nugetHtmlSanitizer |
Corregida | 10.0.1037-beta | -67 d | 11.0.26 no afectada |
10.0.1037-betaArsenal público
Sin exploits, módulos ni PoC públicos conocidos a la fecha de verificación.
https://github.com/mganss/HtmlSanitizer/security/advisories/GHSA-x83r-fr5c-qqvjLas referencias externas se muestran para copiar: cve.llama.ec no lo redirige a otros sitios.
