Browse Source

Merge pull request #17982 from owncloud/appframework-sanitize-name

Sanitize class names before registerService/query
remotes/origin/handlebars-approach
Thomas Müller 10 years ago
parent
commit
abd3d5c6a5
  1. 10
      lib/private/appframework/utility/simplecontainer.php
  2. 19
      tests/lib/appframework/utility/SimpleContainerTest.php

10
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 {
}, false);
}
/*
* @param string $name
* @return string
*/
protected function sanitizeName($name) {
return ltrim($name, '\\');
}
}

19
tests/lib/appframework/utility/SimpleContainerTest.php

@ -170,6 +170,25 @@ class SimpleContainerTest extends \Test\TestCase {
$this->container->query('test'), $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
*/

Loading…
Cancel
Save