diff options
author | Robin McCorkell <rmccorkell@karoshi.org.uk> | 2015-07-30 11:29:06 +0100 |
---|---|---|
committer | Robin McCorkell <rmccorkell@karoshi.org.uk> | 2015-07-30 21:02:16 +0100 |
commit | 182bc17aebe19f49c30f50aacabdb1c9824c6f68 (patch) | |
tree | 009dfd226788a7765acfe2baf77cfa4935f957db | |
parent | a07254856ce532bfe5c49c1b53247daf88dbdd4a (diff) | |
download | nextcloud-server-182bc17aebe19f49c30f50aacabdb1c9824c6f68.tar.gz nextcloud-server-182bc17aebe19f49c30f50aacabdb1c9824c6f68.zip |
Sanitize class names before registerService/query
Leading backslashes are removed, so a `registerService('\\OC\\Foo')`
can still be resolved with `query('OC\\Foo')`.
-rw-r--r-- | lib/private/appframework/utility/simplecontainer.php | 10 | ||||
-rw-r--r-- | tests/lib/appframework/utility/SimpleContainerTest.php | 19 |
2 files changed, 29 insertions, 0 deletions
diff --git a/lib/private/appframework/utility/simplecontainer.php b/lib/private/appframework/utility/simplecontainer.php index c1fc96d1975..9458d59014a 100644 --- a/lib/private/appframework/utility/simplecontainer.php +++ b/lib/private/appframework/utility/simplecontainer.php @@ -99,6 +99,7 @@ class SimpleContainer extends Container implements IContainer { * @throws QueryException if the query could not be resolved */ public function query($name) { + $name = $this->sanitizeName($name); if ($this->offsetExists($name)) { return $this->offsetGet($name); } else { @@ -128,6 +129,7 @@ class SimpleContainer extends Container implements IContainer { * @param bool $shared */ public function registerService($name, Closure $closure, $shared = true) { + $name = $this->sanitizeName($name); if (isset($this[$name])) { unset($this[$name]); } @@ -151,4 +153,12 @@ class SimpleContainer extends Container implements IContainer { }); } + /* + * @param string $name + * @return string + */ + protected function sanitizeName($name) { + return ltrim($name, '\\'); + } + } diff --git a/tests/lib/appframework/utility/SimpleContainerTest.php b/tests/lib/appframework/utility/SimpleContainerTest.php index 09857808b9f..d544faf273e 100644 --- a/tests/lib/appframework/utility/SimpleContainerTest.php +++ b/tests/lib/appframework/utility/SimpleContainerTest.php @@ -167,6 +167,25 @@ class SimpleContainerTest extends \Test\TestCase { $this->assertEquals('abc', $this->container->query('test1')); } + public function sanitizeNameProvider() { + return [ + ['ABC\\Foo', 'ABC\\Foo'], + ['\\ABC\\Foo', '\\ABC\\Foo'], + ['\\ABC\\Foo', 'ABC\\Foo'], + ['ABC\\Foo', '\\ABC\\Foo'], + ]; + } + + /** + * @dataProvider sanitizeNameProvider + */ + public function testSanitizeName($register, $query) { + $this->container->registerService($register, function() { + return 'abc'; + }); + $this->assertEquals('abc', $this->container->query($query)); + } + /** * @expectedException \OCP\AppFramework\QueryException */ |