[HttpFoundation] fix guessing mime-types of files with leading dash

This commit is contained in:
Nicolas Grekas 2019-04-19 14:48:43 +02:00
parent 4463791d0e
commit 6be5cc75a4
3 changed files with 12 additions and 3 deletions

View File

@ -31,7 +31,7 @@ class FileBinaryMimeTypeGuesser implements MimeTypeGuesserInterface
* *
* @param string $cmd The command to run to get the mime type of a file * @param string $cmd The command to run to get the mime type of a file
*/ */
public function __construct($cmd = 'file -b --mime %s 2>/dev/null') public function __construct($cmd = 'file -b --mime -- %s 2>/dev/null')
{ {
$this->cmd = $cmd; $this->cmd = $cmd;
} }
@ -80,7 +80,7 @@ class FileBinaryMimeTypeGuesser implements MimeTypeGuesserInterface
ob_start(); ob_start();
// need to use --mime instead of -i. see #6641 // need to use --mime instead of -i. see #6641
passthru(sprintf($this->cmd, escapeshellarg($path)), $return); passthru(sprintf($this->cmd, escapeshellarg((0 === strpos($path, '-') ? './' : '').$path)), $return);
if ($return > 0) { if ($return > 0) {
ob_end_clean(); ob_end_clean();

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 B

View File

@ -20,7 +20,16 @@ use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
*/ */
class MimeTypeTest extends TestCase class MimeTypeTest extends TestCase
{ {
protected $path; public function testGuessWithLeadingDash()
{
$cwd = getcwd();
chdir(__DIR__.'/../Fixtures');
try {
$this->assertEquals('image/gif', MimeTypeGuesser::getInstance()->guess('-test'));
} finally {
chdir($cwd);
}
}
public function testGuessImageWithoutExtension() public function testGuessImageWithoutExtension()
{ {