Skip to content

Commit 9af72e6

Browse files
committed
Fix AssignNameExpression to forbid using names that won't work
1 parent 3eda65e commit 9af72e6

File tree

8 files changed

+62
-8
lines changed

8 files changed

+62
-8
lines changed

src/ExpressionParser.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -784,11 +784,7 @@ public function parseAssignmentExpression()
784784
} else {
785785
$stream->expect(Token::NAME_TYPE, null, 'Only variables can be assigned to');
786786
}
787-
$value = $token->getValue();
788-
if (\in_array(strtolower($value), ['true', 'false', 'none', 'null'])) {
789-
throw new SyntaxError(\sprintf('You cannot assign a value to "%s".', $value), $token->getLine(), $stream->getSourceContext());
790-
}
791-
$targets[] = new AssignNameExpression($value, $token->getLine());
787+
$targets[] = new AssignNameExpression($token->getValue(), $token->getLine());
792788

793789
if (!$stream->nextIf(Token::PUNCTUATION_TYPE, ',')) {
794790
break;

src/Node/Expression/AssignNameExpression.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,20 @@
1313
namespace Twig\Node\Expression;
1414

1515
use Twig\Compiler;
16+
use Twig\Error\SyntaxError;
1617

1718
class AssignNameExpression extends NameExpression
1819
{
20+
public function __construct(string $name, int $lineno)
21+
{
22+
// All names supported by ExpressionParser::parsePrimaryExpression() should be excluded
23+
if (\in_array(strtolower($name), ['true', 'false', 'none', 'null'])) {
24+
throw new SyntaxError(\sprintf('You cannot assign a value to "%s".', $name), $lineno);
25+
}
26+
27+
parent::__construct($name, $lineno);
28+
}
29+
1930
public function compile(Compiler $compiler): void
2031
{
2132
$compiler

src/TokenParser/FromTokenParser.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ public function parse(Token $token): Node
3535
while (true) {
3636
$name = $stream->expect(Token::NAME_TYPE)->getValue();
3737

38-
$alias = $name;
3938
if ($stream->nextIf('as')) {
40-
$alias = $stream->expect(Token::NAME_TYPE)->getValue();
39+
$alias = new AssignNameExpression($stream->expect(Token::NAME_TYPE)->getValue(), $token->getLine());
40+
} else {
41+
$alias = new AssignNameExpression($name, $token->getLine());
4142
}
4243

4344
$targets[$name] = $alias;
@@ -53,7 +54,7 @@ public function parse(Token $token): Node
5354
$node = new ImportNode($macro, $var, $token->getLine(), $this->parser->isMainScope());
5455

5556
foreach ($targets as $name => $alias) {
56-
$this->parser->addImportedSymbol('function', $alias, 'macro_'.$name, $var);
57+
$this->parser->addImportedSymbol('function', $alias->getAttribute('name'), 'macro_'.$name, $var);
5758
}
5859

5960
return $node;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
"map" filter
3+
--TEMPLATE--
4+
{{ [1, 2]|map(true => true * 2)|join(', ') }}
5+
--DATA--
6+
return []
7+
--EXCEPTION--
8+
Twig\Error\SyntaxError: You cannot assign a value to "true" in "index.twig" at line 2.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
"for" tag
3+
--TEMPLATE--
4+
{% for true in [1, 2] %}
5+
{% endfor %}
6+
--DATA--
7+
return []
8+
--EXCEPTION--
9+
Twig\Error\SyntaxError: You cannot assign a value to "true" in "index.twig" at line 2.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
"from" tag
3+
--TEMPLATE--
4+
{% from _self import input as true %}
5+
6+
{{ true('username') }}
7+
8+
{% macro input(name) -%}
9+
{% endmacro %}
10+
--DATA--
11+
return []
12+
--EXCEPTION--
13+
Twig\Error\SyntaxError: You cannot assign a value to "true" in "index.twig" at line 2.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
"import" tag
3+
--TEMPLATE--
4+
{% import _self as true %}
5+
--DATA--
6+
return []
7+
--EXCEPTION--
8+
Twig\Error\SyntaxError: You cannot assign a value to "true" in "index.twig" at line 2.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
"set" tag
3+
--TEMPLATE--
4+
{% set true = 'foo' %}
5+
--DATA--
6+
return []
7+
--EXCEPTION--
8+
Twig\Error\SyntaxError: You cannot assign a value to "true" in "index.twig" at line 2.

0 commit comments

Comments
 (0)