Skip to content

Commit

Permalink
[Bug] Fix csv being allowed as text/html, use Symfony MIME guesser (#374
Browse files Browse the repository at this point in the history
)

* 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 <[email protected]>
  • Loading branch information
kingjia90 and kingjia90 authored Nov 7, 2023
1 parent 6c07f5e commit 5157cd7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
9 changes: 5 additions & 4 deletions src/DataSource/Interpreter/CsvFileInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 5157cd7

Please sign in to comment.