From 5157cd7f8c2d474022dee1c740ef02e5fe1db42c Mon Sep 17 00:00:00 2001 From: JiaJia Ji Date: Tue, 7 Nov 2023 10:25:31 +0100 Subject: [PATCH] [Bug] Fix csv being allowed as text/html, use Symfony MIME guesser (#374) * task: fix csv being detected as text/html by finfo * Apply php-cs-fixer changes * replace with symfony mime * Update composer.json --------- Co-authored-by: kingjia90 --- composer.json | 1 + src/DataSource/Interpreter/CsvFileInterpreter.php | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 3bdec304..780153ab 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "nesbot/carbon": "^2.27", "pimcore/data-hub": "^1.6", "pimcore/pimcore": "^10.6 || ^11.0", + "symfony/mime": "^5.2 || ^6.2", "webmozarts/console-parallelization": "^1.2.0 || ^2.0.0" }, "require-dev": { diff --git a/src/DataSource/Interpreter/CsvFileInterpreter.php b/src/DataSource/Interpreter/CsvFileInterpreter.php index 90de3f27..6aaba358 100644 --- a/src/DataSource/Interpreter/CsvFileInterpreter.php +++ b/src/DataSource/Interpreter/CsvFileInterpreter.php @@ -16,6 +16,7 @@ namespace Pimcore\Bundle\DataImporterBundle\DataSource\Interpreter; use Pimcore\Bundle\DataImporterBundle\Preview\Model\PreviewData; +use Symfony\Component\Mime\MimeTypes; class CsvFileInterpreter extends AbstractInterpreter { @@ -72,10 +73,10 @@ public function fileValid(string $path, bool $originalFilename = false): bool } } - $csvMimes = ['text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain']; - $finfo = finfo_open(FILEINFO_MIME_TYPE); - $mime = finfo_file($finfo, $path); - finfo_close($finfo); + // csv that has html tags might be recognized as text/html + $csvMimes = ['text/html', 'text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain']; + $mimeTypes = new MimeTypes(); + $mime = $mimeTypes->guessMimeType($path); return in_array($mime, $csvMimes); }