{
$this->allowedTags = $allowedTags;
$this->allowedFilters = $allowedFilters;
- $this->allowedMethods = $allowedMethods;
+ $this->setAllowedMethods($allowedMethods);
$this->allowedProperties = $allowedProperties;
$this->allowedFunctions = $allowedFunctions;
}
public function setAllowedMethods(array $methods)
{
- $this->allowedMethods = $methods;
+ $this->allowedMethods = array();
+ foreach ($methods as $class => $m) {
+ $this->allowedMethods[$class] = array_map('strtolower', is_array($m) ? $m : array($m));
+ }
}
public function setAllowedProperties(array $properties)
}
$allowed = false;
+ $method = strtolower($method);
foreach ($this->allowedMethods as $class => $methods) {
if ($obj instanceof $class) {
- $allowed = in_array($method, is_array($methods) ? $methods : array($methods));
+ $allowed = in_array($method, $methods);
break;
}
'1_basic5' => '{{ obj }}',
'1_basic6' => '{{ arr.obj }}',
'1_basic7' => '{{ cycle(["foo","bar"], 1) }}',
+ '1_basic8' => '{{ obj.getfoobar }}{{ obj.getFooBar }}',
'1_basic' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}',
);
}
$twig = $this->getEnvironment(true, array(), self::$templates, array(), array(), array(), array(), array('cycle'));
$this->assertEquals('bar', $twig->loadTemplate('1_basic7')->render(self::$params), 'Sandbox allow some functions');
+ foreach (array('getfoobar', 'getFoobar', 'getFooBar') as $name) {
+ $twig = $this->getEnvironment(true, array(), self::$templates, array(), array(), array('Object' => $name));
+ Object::reset();
+ $this->assertEquals('foobarfoobar', $twig->loadTemplate('1_basic8')->render(self::$params), 'Sandbox allow methods in a case-insensitive way');
+ $this->assertEquals(2, Object::$called['getFooBar'], 'Sandbox only calls method once');
+ }
}
public function testSandboxLocallySetForAnInclude()
class Object
{
- static public $called = array('__toString' => 0, 'foo' => 0);
+ static public $called = array('__toString' => 0, 'foo' => 0, 'getFooBar' => 0);
public $bar = 'bar';
static public function reset()
{
- self::$called = array('__toString' => 0, 'foo' => 0);
+ self::$called = array('__toString' => 0, 'foo' => 0, 'getFooBar' => 0);
}
public function __toString()
return 'foo';
}
+
+ public function getFooBar()
+ {
+ ++self::$called['getFooBar'];
+
+ return 'foobar';
+ }
}