EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
Improve sanitize CSV
Yuriif209248efd0d
diff --git a/application/Espo/Tools/Export/Format/Csv/Processor.php b/application/Espo/Tools/Export/Format/Csv/Processor.phpindex 585134fb63..8235491ac1 100644--- a/application/Espo/Tools/Export/Format/Csv/Processor.php+++ b/application/Espo/Tools/Export/Format/Csv/Processor.php@@ -37,6 +37,7 @@ use Espo\Tools\Export\Collection;use Espo\Tools\Export\Processor as ProcessorInterface;use Espo\Tools\Export\Processor\Params;+use Espo\Tools\Export\Processor\Util;use Psr\Http\Message\StreamInterface;use GuzzleHttp\Psr7\Stream;@@ -98,26 +99,9 @@ class Processor implements ProcessorInterface$value = (string) $value;- $preparedRow[] = $this->sanitizeCellValue($value);+ $preparedRow[] = Util::sanitizeCellValue($value);}return $preparedRow;}-- private function sanitizeCellValue(string $value): string- {- if ($value === '') {- return $value;- }-- if (is_numeric($value)) {- return $value;- }-- if (in_array($value[0], ['+', '-', '@', '='])) {- return "'" . $value;- }-- return $value;- }}diff --git a/application/Espo/Tools/Export/Format/Xlsx/OpenSpoutProcessor.php b/application/Espo/Tools/Export/Format/Xlsx/OpenSpoutProcessor.phpindex fd35da19b0..8314267c09 100644--- a/application/Espo/Tools/Export/Format/Xlsx/OpenSpoutProcessor.php+++ b/application/Espo/Tools/Export/Format/Xlsx/OpenSpoutProcessor.php@@ -43,6 +43,7 @@ use Espo\Tools\Export\Format\CellValuePreparatorFactory;use Espo\Tools\Export\Processor as ProcessorInterface;use Espo\Tools\Export\Processor\Params;+use Espo\Tools\Export\Processor\Util;use GuzzleHttp\Psr7\Stream;use LogicException;use OpenSpout\Common\Entity\Cell;@@ -174,7 +175,7 @@ class OpenSpoutProcessor implements ProcessorInterface->prepare($entity, $name);if (is_string($value)) {- $value = $this->sanitizeCellValue($value);+ $value = Util::sanitizeCellValue($value);return Cell\StringCell::fromValue($value);}@@ -275,21 +276,4 @@ class OpenSpoutProcessor implements ProcessorInterfacereturn '[$' . $currencySymbol . '-409]#,##0.00;-[$' . $currencySymbol . '-409]#,##0.00';}-- private function sanitizeCellValue(string $value): string- {- if ($value === '') {- return $value;- }-- if (is_numeric($value)) {- return $value;- }-- if (in_array($value[0], ['+', '-', '@', '='])) {- return "'" . $value;- }-- return $value;- }}diff --git a/application/Espo/Tools/Export/Format/Xlsx/PhpSpreadsheetProcessor.php b/application/Espo/Tools/Export/Format/Xlsx/PhpSpreadsheetProcessor.phpindex 773d585e20..84792c74e9 100644--- a/application/Espo/Tools/Export/Format/Xlsx/PhpSpreadsheetProcessor.php+++ b/application/Espo/Tools/Export/Format/Xlsx/PhpSpreadsheetProcessor.php@@ -49,6 +49,7 @@ use Espo\Tools\Export\Format\CellValuePreparatorFactory;use Espo\Tools\Export\Processor as ProcessorInterface;use Espo\Tools\Export\Processor\Params;+use Espo\Tools\Export\Processor\Util;use Psr\Http\Message\StreamInterface;use GuzzleHttp\Psr7\Stream;@@ -625,19 +626,7 @@ class PhpSpreadsheetProcessor implements ProcessorInterfacereturn $value;}- if ($value === '') {- return $value;- }-- if (is_numeric($value)) {- return $value;- }-- if (in_array($value[0], ['+', '-', '@', '='])) {- return "'" . $value;- }-- return $value;+ return Util::sanitizeCellValue($value);}private function sanitizeUrl(string $value): ?stringdiff --git a/application/Espo/Tools/Export/Processor/Util.php b/application/Espo/Tools/Export/Processor/Util.phpnew file mode 100644index 0000000000..dd276cdd04--- /dev/null+++ b/application/Espo/Tools/Export/Processor/Util.php@@ -0,0 +1,55 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2026 EspoCRM, Inc.+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace Espo\Tools\Export\Processor;++/**+ * @internal+ */+class Util+{+ public static function sanitizeCellValue(string $value): string+ {+ if ($value === '') {+ return $value;+ }++ if (is_numeric($value)) {+ return $value;+ }++ $trimmed = ltrim($value, " \t\r\n\v\0");++ if ($trimmed !== '' && in_array($trimmed[0], ['+', '-', '@', '='])) {+ return "'" . $value;+ }++ return $value;+ }+}