From 30feddef1053534065195b9b5a95e0524f169e03 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Wed, 8 Jan 2020 07:20:40 +0000 Subject: [PATCH] Replace strtolower() with strtr() when dealing with method names --- src/ExpressionParser.php | 2 +- src/Parser.php | 4 ++-- src/Sandbox/SecurityPolicy.php | 4 ++-- src/Template.php | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ExpressionParser.php b/src/ExpressionParser.php index 9066ade1695..a3ff72530b8 100644 --- a/src/ExpressionParser.php +++ b/src/ExpressionParser.php @@ -657,7 +657,7 @@ public function parseAssignmentExpression() $stream->expect(Token::NAME_TYPE, null, 'Only variables can be assigned to'); } $value = $token->getValue(); - if (\in_array(strtolower($value), ['true', 'false', 'none', 'null'])) { + if (\in_array(strtr($value, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), ['true', 'false', 'none', 'null'])) { throw new SyntaxError(sprintf('You cannot assign a value to "%s".', $value), $token->getLine(), $stream->getSourceContext()); } $targets[] = new AssignNameExpression($value, $token->getLine()); diff --git a/src/Parser.php b/src/Parser.php index 0ea102cc811..9fb6a83a4e6 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -299,7 +299,7 @@ public function isReservedMacroName($name) $this->reservedMacroNames = []; $r = new \ReflectionClass($this->env->getBaseTemplateClass()); foreach ($r->getMethods() as $method) { - $methodName = strtolower($method->getName()); + $methodName = strtr($method->getName(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); if ('get' === substr($methodName, 0, 3) && isset($methodName[3])) { $this->reservedMacroNames[] = substr($methodName, 3); @@ -307,7 +307,7 @@ public function isReservedMacroName($name) } } - return \in_array(strtolower($name), $this->reservedMacroNames); + return \in_array(strtr($name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), $this->reservedMacroNames); } public function addTrait($trait) diff --git a/src/Sandbox/SecurityPolicy.php b/src/Sandbox/SecurityPolicy.php index 31b6c348332..603843591af 100644 --- a/src/Sandbox/SecurityPolicy.php +++ b/src/Sandbox/SecurityPolicy.php @@ -51,7 +51,7 @@ public function setAllowedMethods(array $methods) { $this->allowedMethods = []; foreach ($methods as $class => $m) { - $this->allowedMethods[$class] = array_map('strtolower', \is_array($m) ? $m : [$m]); + $this->allowedMethods[$class] = array_map(function ($value) { return strtr($value, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); }, \is_array($m) ? $m : [$m]); } } @@ -93,7 +93,7 @@ public function checkMethodAllowed($obj, $method) } $allowed = false; - $method = strtolower($method); + $method = strtr($method, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); foreach ($this->allowedMethods as $class => $methods) { if ($obj instanceof $class) { $allowed = \in_array($method, $methods); diff --git a/src/Template.php b/src/Template.php index 3f7447c126c..704125e5387 100644 --- a/src/Template.php +++ b/src/Template.php @@ -628,7 +628,7 @@ protected function getAttribute($object, $item, array $arguments = [], $type = s foreach ($ref->getMethods(\ReflectionMethod::IS_PUBLIC) as $refMethod) { // Accessing the environment from templates is forbidden to prevent untrusted changes to the environment - if ('getenvironment' !== strtolower($refMethod->name)) { + if ('getenvironment' !== strtr($refMethod->name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')) { $methods[] = $refMethod->name; } } @@ -642,7 +642,7 @@ protected function getAttribute($object, $item, array $arguments = [], $type = s foreach ($methods as $method) { $cache[$method] = $method; - $cache[$lcName = strtolower($method)] = $method; + $cache[$lcName = strtr($method, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')] = $method; if ('g' === $lcName[0] && 0 === strpos($lcName, 'get')) { $name = substr($method, 3); @@ -670,7 +670,7 @@ protected function getAttribute($object, $item, array $arguments = [], $type = s $call = false; if (isset(self::$cache[$class][$item])) { $method = self::$cache[$class][$item]; - } elseif (isset(self::$cache[$class][$lcItem = strtolower($item)])) { + } elseif (isset(self::$cache[$class][$lcItem = strtr($item, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')])) { $method = self::$cache[$class][$lcItem]; } elseif (isset(self::$cache[$class]['__call'])) { $method = $item;