Details
Recommended Fix
Neutralise formula leaders at write time inside CSVExport::writeData() so the export pipeline itself enforces the protection regardless of upstream sanitisation.
private const FORMULA_TRIGGERS = ['=', '+', '-', '@', "\t", "\r"];
public function writeData(array $data, array $fields = [])
{
if (!empty($fields)) {
$this->writeHeader($fields);
}
foreach ($data as $row) {
$line = [];
foreach ($row as $cell) {
if (is_string($cell) && $cell !== '' && in_array($cell[0], self::FORMULA_TRIGGERS, true)) {
$cell = "'" . $cell; // force-as-text per OWASP CSV-injection prevention
}
$line[] = is_string($cell)
? $this->getDelimiter() . str_replace($this->getDelimiter(), $this->getDelimiter() . $this->getDelimiter(), $cell) . $this->getDelimiter()
: $cell;
}
$this->csv[] = implode($this->separator, $line);
}
}
Apply the same neutralisation in writeHeader(), in XLSExport::getCursorRawData() (lines 226-236 - the Tools::fixHtml helper does not address formulas), and in any other future exporter that emits spreadsheet-loadable formats.
A regression test should:
- Insert a customer with
nombre = '=SUM(1+1)'. - Render the list to CSV via
CSVExport->writeData(). - Assert the resulting line begins with
"'=SUM(1+1)", not"=SUM(1+1)". - Assert
nombrecontaining an embedded delimiter (a"b) is correctly doubled to"a""b".
Defence in depth: emit Content-Type: text/csv; charset=utf-8 plus a Content-Disposition filename that the operator's MUA/browser cannot trivially confuse for a trusted XLSX (e.g. always *.csv, never *.xls or *.xlsx), and document in SECURITY.md that exports must be opened with the spreadsheet's "Import Text" / "From Text/CSV" wizard until the application is upgraded.