fixed security check in filesystem loader

This commit is contained in:
Fabien Potencier
2010-12-14 14:36:45 +01:00
parent 090270a509
commit efdcb8c6f4
2 changed files with 12 additions and 5 deletions
+11 -5
View File
@@ -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'),
);
}
}