mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-16 20:36:33 +00:00
fixed security check in filesystem loader
This commit is contained in:
@@ -104,12 +104,18 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface
|
||||
// normalize name
|
||||
$name = str_replace('\\', '/', $name);
|
||||
|
||||
// remove ./
|
||||
$name = preg_replace('#(^|/)\./(\./)*#', '$1', $name);
|
||||
$parts = explode('/', $name);
|
||||
$level = 0;
|
||||
foreach ($parts as $part) {
|
||||
if ('..' === $part) {
|
||||
--$level;
|
||||
} elseif ('.' !== $part) {
|
||||
++$level;
|
||||
}
|
||||
|
||||
// security check (a name cannot start with ../)
|
||||
if ('..' === substr($name, 0, 2)) {
|
||||
throw new Twig_Error_Loader('Looks like you try to load a template outside configured directories.');
|
||||
if ($level < 0) {
|
||||
throw new Twig_Error_Loader('Looks like you try to load a template outside configured directories.');
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($this->cache[$name])) {
|
||||
|
||||
@@ -34,6 +34,7 @@ class Twig_Tests_Loader_FilesystemTest extends PHPUnit_Framework_TestCase
|
||||
array('foo\\..\\..\\AutoloaderTest.php'),
|
||||
array('foo/../bar/../../AutoloaderTest.php'),
|
||||
array('foo/bar/../../../AutoloaderTest.php'),
|
||||
array('filters/../../AutoloaderTest.php'),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user