Skip to content

Commit

Permalink
Check exact extension in checkFilename utility (#3061)
Browse files Browse the repository at this point in the history
* Fix uploads_dangerous_extensions checking (#3060)
* Remove redundant prefixing of `.` to extension (#3060)
  • Loading branch information
stephan-strate authored and mahagr committed Nov 17, 2020
1 parent 247d1a9 commit ae6f0b5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
10 changes: 3 additions & 7 deletions system/src/Grav/Common/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -928,11 +928,7 @@ public static function getMimeByLocalFile($filename, $default = 'application/oct
public static function checkFilename($filename)
{
$dangerous_extensions = Grav::instance()['config']->get('security.uploads_dangerous_extensions', []);
array_walk($dangerous_extensions, function (&$val) {
$val = '.' . $val;
});

$extension = '.' . pathinfo($filename, PATHINFO_EXTENSION);
$extension = pathinfo($filename, PATHINFO_EXTENSION);

return !(
// Empty filenames are not allowed.
Expand All @@ -941,8 +937,8 @@ public static function checkFilename($filename)
|| strtr($filename, "\t\v\n\r\0\\/", '_______') !== $filename
// Filename should not start or end with dot or space.
|| trim($filename, '. ') !== $filename
// Filename should not contain .php in it.
|| static::contains($extension, $dangerous_extensions)
// File extension should not be part of configured dangerous extensions
|| in_array($extension, $dangerous_extensions)
);
}

Expand Down
16 changes: 16 additions & 0 deletions tests/unit/Grav/Common/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,20 @@ public function testUrlwithExternals()
$this->assertSame('//foo.com', Utils::url('//foo.com'));
$this->assertSame('//foo.com?param=x', Utils::url('//foo.com?param=x'));
}

public function testCheckFilename()
{
// configure extension for consistent results
/** @var \Grav\Common\Config\Config $config */
$config = $this->grav['config'];
$config->set('security.uploads_dangerous_extensions', ['php', 'html', 'htm', 'exe', 'js']);

$this->assertFalse(Utils::checkFilename('foo.php'));
$this->assertFalse(Utils::checkFilename('bar.js'));

$this->assertTrue(Utils::checkFilename('foo.json'));
$this->assertTrue(Utils::checkFilename('foo.xml'));
$this->assertTrue(Utils::checkFilename('foo.yaml'));
$this->assertTrue(Utils::checkFilename('foo.yml'));
}
}

0 comments on commit ae6f0b5

Please sign in to comment.