Skip to content

Commit

Permalink
bug #2023 [ExpressionParser] forbids true, false, null and none keywo…
Browse files Browse the repository at this point in the history
…rds for variables names. (hhamon)

This PR was merged into the 1.x branch.

Discussion
----------

[ExpressionParser] forbids true, false, null and none keywords for variables names.

Commits
-------

2e02f73 [ExpressionParser] forbids true, false, null and none keywords for variables names.
  • Loading branch information
fabpot committed Apr 24, 2016
2 parents babe6fc + 2e02f73 commit b1748ef
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/Twig/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,11 @@ public function parseAssignmentExpression()
$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;
Expand Down
5 changes: 5 additions & 0 deletions test/Twig/Tests/ExpressionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ public function getFailingTestsForAssignment()
{
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" %}'),
Expand Down

0 comments on commit b1748ef

Please sign in to comment.