From c8f9f916b4b93f5676fad46c664980935c7757ae Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 1 Sep 2017 09:13:50 +0200 Subject: [PATCH] prevent bundle readers from breaking out of paths --- .../Intl/Data/Bundle/Reader/JsonBundleReader.php | 5 +++++ .../Intl/Data/Bundle/Reader/PhpBundleReader.php | 5 +++++ .../Reader/Fixtures/invalid_directory/en.json | 1 + .../Reader/Fixtures/invalid_directory/en.php | 14 ++++++++++++++ .../Data/Bundle/Reader/JsonBundleReaderTest.php | 8 ++++++++ .../Data/Bundle/Reader/PhpBundleReaderTest.php | 8 ++++++++ 6 files changed, 41 insertions(+) create mode 100644 src/Symfony/Component/Intl/Tests/Data/Bundle/Reader/Fixtures/invalid_directory/en.json create mode 100644 src/Symfony/Component/Intl/Tests/Data/Bundle/Reader/Fixtures/invalid_directory/en.php diff --git a/src/Symfony/Component/Intl/Data/Bundle/Reader/JsonBundleReader.php b/src/Symfony/Component/Intl/Data/Bundle/Reader/JsonBundleReader.php index 90012a7ad6..d102a21802 100644 --- a/src/Symfony/Component/Intl/Data/Bundle/Reader/JsonBundleReader.php +++ b/src/Symfony/Component/Intl/Data/Bundle/Reader/JsonBundleReader.php @@ -30,6 +30,11 @@ class JsonBundleReader implements BundleReaderInterface { $fileName = $path.'/'.$locale.'.json'; + // prevent directory traversal attacks + if (dirname($fileName) !== $path) { + throw new ResourceBundleNotFoundException(sprintf('The resource bundle "%s" does not exist.', $fileName)); + } + if (!file_exists($fileName)) { throw new ResourceBundleNotFoundException(sprintf( 'The resource bundle "%s" does not exist.', diff --git a/src/Symfony/Component/Intl/Data/Bundle/Reader/PhpBundleReader.php b/src/Symfony/Component/Intl/Data/Bundle/Reader/PhpBundleReader.php index 57391ce010..0b66bb1a55 100644 --- a/src/Symfony/Component/Intl/Data/Bundle/Reader/PhpBundleReader.php +++ b/src/Symfony/Component/Intl/Data/Bundle/Reader/PhpBundleReader.php @@ -30,6 +30,11 @@ class PhpBundleReader implements BundleReaderInterface { $fileName = $path.'/'.$locale.'.php'; + // prevent directory traversal attacks + if (dirname($fileName) !== $path) { + throw new ResourceBundleNotFoundException(sprintf('The resource bundle "%s" does not exist.', $fileName)); + } + if (!file_exists($fileName)) { throw new ResourceBundleNotFoundException(sprintf( 'The resource bundle "%s/%s.php" does not exist.', diff --git a/src/Symfony/Component/Intl/Tests/Data/Bundle/Reader/Fixtures/invalid_directory/en.json b/src/Symfony/Component/Intl/Tests/Data/Bundle/Reader/Fixtures/invalid_directory/en.json new file mode 100644 index 0000000000..16ea32adf7 --- /dev/null +++ b/src/Symfony/Component/Intl/Tests/Data/Bundle/Reader/Fixtures/invalid_directory/en.json @@ -0,0 +1 @@ +{"Foo":"Bar"} diff --git a/src/Symfony/Component/Intl/Tests/Data/Bundle/Reader/Fixtures/invalid_directory/en.php b/src/Symfony/Component/Intl/Tests/Data/Bundle/Reader/Fixtures/invalid_directory/en.php new file mode 100644 index 0000000000..f2b06a91ad --- /dev/null +++ b/src/Symfony/Component/Intl/Tests/Data/Bundle/Reader/Fixtures/invalid_directory/en.php @@ -0,0 +1,14 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +return array( + 'Foo' => 'Bar', +); diff --git a/src/Symfony/Component/Intl/Tests/Data/Bundle/Reader/JsonBundleReaderTest.php b/src/Symfony/Component/Intl/Tests/Data/Bundle/Reader/JsonBundleReaderTest.php index a8ccabe07b..2b6e6c4169 100644 --- a/src/Symfony/Component/Intl/Tests/Data/Bundle/Reader/JsonBundleReaderTest.php +++ b/src/Symfony/Component/Intl/Tests/Data/Bundle/Reader/JsonBundleReaderTest.php @@ -69,4 +69,12 @@ class JsonBundleReaderTest extends TestCase { $this->reader->read(__DIR__.'/Fixtures/json', 'en_Invalid'); } + + /** + * @expectedException \Symfony\Component\Intl\Exception\ResourceBundleNotFoundException + */ + public function testReaderDoesNotBreakOutOfGivenPath() + { + $this->reader->read(__DIR__.'/Fixtures/json', '../invalid_directory/en'); + } } diff --git a/src/Symfony/Component/Intl/Tests/Data/Bundle/Reader/PhpBundleReaderTest.php b/src/Symfony/Component/Intl/Tests/Data/Bundle/Reader/PhpBundleReaderTest.php index 51898cb2be..954e2f0423 100644 --- a/src/Symfony/Component/Intl/Tests/Data/Bundle/Reader/PhpBundleReaderTest.php +++ b/src/Symfony/Component/Intl/Tests/Data/Bundle/Reader/PhpBundleReaderTest.php @@ -61,4 +61,12 @@ class PhpBundleReaderTest extends TestCase { $this->reader->read(__DIR__.'/Fixtures/NotAFile', 'en'); } + + /** + * @expectedException \Symfony\Component\Intl\Exception\ResourceBundleNotFoundException + */ + public function testReaderDoesNotBreakOutOfGivenPath() + { + $this->reader->read(__DIR__.'/Fixtures/php', '../invalid_directory/en'); + } }