From 2e02f73bf71aa7c93c3519cb8723745533dd25d8 Mon Sep 17 00:00:00 2001 From: Hugo Hamon Date: Sat, 23 Apr 2016 16:01:10 +0200 Subject: [PATCH] [ExpressionParser] forbids true, false, null and none keywords for variables names. --- lib/Twig/ExpressionParser.php | 7 ++++--- test/Twig/Tests/ExpressionParserTest.php | 5 +++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index 7b2d58f27..ec85a4dbc 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -539,10 +539,11 @@ class Twig_ExpressionParser $targets = array(); while (true) { $token = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE, null, 'Only variables can be assigned to'); - if (in_array($token->getValue(), array('true', 'false', 'none'))) { - throw new Twig_Error_Syntax(sprintf('You cannot assign a value to "%s".', $token->getValue()), $token->getLine(), $this->parser->getFilename()); + $value = $token->getValue(); + if (in_array(strtolower($value), array('true', 'false', 'none', 'null'))) { + throw new Twig_Error_Syntax(sprintf('You cannot assign a value to "%s"', $value), $token->getLine(), $this->parser->getFilename()); } - $targets[] = new Twig_Node_Expression_AssignName($token->getValue(), $token->getLine()); + $targets[] = new Twig_Node_Expression_AssignName($value, $token->getLine()); if (!$this->parser->getStream()->nextIf(Twig_Token::PUNCTUATION_TYPE, ',')) { break; diff --git a/test/Twig/Tests/ExpressionParserTest.php b/test/Twig/Tests/ExpressionParserTest.php index 33bf48be7..fdab7e7b0 100644 --- a/test/Twig/Tests/ExpressionParserTest.php +++ b/test/Twig/Tests/ExpressionParserTest.php @@ -27,8 +27,13 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase { return array( array('{% set false = "foo" %}'), + array('{% set FALSE = "foo" %}'), array('{% set true = "foo" %}'), + array('{% set TRUE = "foo" %}'), array('{% set none = "foo" %}'), + array('{% set NONE = "foo" %}'), + array('{% set null = "foo" %}'), + array('{% set NULL = "foo" %}'), array('{% set 3 = "foo" %}'), array('{% set 1 + 2 = "foo" %}'), array('{% set "bar" = "foo" %}'),