The vulnerability is a Cross-Site Scripting (XSS) issue within the phpoffice/phpspreadsheet library, specifically in its HTML writing component. The root cause is the improper handling of custom number formats, which allows for the bypass of HTML escaping mechanisms.
The investigation began by analyzing the version history of the package, comparing the last vulnerable version (5.6.0) with the first patched version (5.7.0). The commits between these versions were examined. The key commit, f1eb4e6980d537ec85fc20be5950f9ad65d47ffd, contained the security fix.
The vulnerability description points to a flawed conditional escaping logic in Writer/Html.php. However, the actual patch was applied in a different file, src/PhpSpreadsheet/Style/NumberFormat/Formatter.php, within the toFormattedString function. This function is responsible for applying number formats to cell values.
Prior to the patch, the toFormattedString function had a bug. When processing a number format containing the '@' text placeholder (without any quotes in the format string), it would perform a simple string replacement and return the result immediately. It failed to execute a callback function that the HTML writer passes to it. This callback is intended to perform htmlspecialchars escaping.
An attacker could exploit this by setting a cell's value to an XSS payload (e.g., <img src=x onerror=alert(1)>) and applying a custom number format like @ (with a trailing space). The Formatter::toFormattedString function would then return the payload with a space, but without HTML escaping. The calling function in Writer/Html.php would see that the formatted data is different from the original and would therefore skip its own htmlspecialchars call, leading to the payload being rendered unescaped in the final HTML.
The patch corrects the logic in toFormattedString to ensure that the provided callback is always executed, even for these simple '@' formats. This ensures that the value is properly HTML-escaped before being returned to the writer, thus mitigating the XSS vulnerability. The identified vulnerable function, PhpOffice\PhpSpreadsheet\Style\NumberFormat\Formatter::toFormattedString, is therefore the precise location of the exploitable bug.