Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 3.31.0 (2026-XX-XX)

* Fix a variable not being escaped when it has the name of a macro import alias of another block or macro
* Fix `html_cva()` ignoring a boolean default variant
* Fix a PHP 8.5 deprecation in `html_cva()` when a recipe value is `null`
* Add the `collate` function to `twig/intl-extra` to compare strings in the alphabetical order of a locale
Expand Down
8 changes: 0 additions & 8 deletions src/NodeVisitor/EscaperNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Twig\Node\Expression\Filter\EscapeFilter;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Expression\OperatorEscapeInterface;
use Twig\Node\ImportNode;
use Twig\Node\ModuleNode;
use Twig\Node\Node;
use Twig\Node\Nodes;
Expand All @@ -40,7 +39,6 @@ final class EscaperNodeVisitor implements NodeVisitorInterface
private $safeAnalysis;
private $traverser;
private $defaultStrategy = false;
private $safeVars = [];
private bool $usesEscaper = false;

public function __construct()
Expand All @@ -56,16 +54,13 @@ public function enterNode(Node $node, Environment $env): Node
$this->defaultStrategy = $defaultStrategy;
}
$node->setAttribute('strategy', \is_string($this->defaultStrategy) ? $this->defaultStrategy : false);
$this->safeVars = [];
$this->blocks = [];
$this->statusStack = [];
$this->usesEscaper = false;
} elseif ($node instanceof AutoEscapeNode) {
$this->statusStack[] = $node->getAttribute('value');
} elseif ($node instanceof BlockNode) {
$this->statusStack[] = $this->blocks[$node->getAttribute('name')] ?? $this->needEscaping();
} elseif ($node instanceof ImportNode) {
$this->safeVars[] = $node->getNode('var')->getNode('var')->getAttribute('name');
}

return $node;
Expand All @@ -76,7 +71,6 @@ public function leaveNode(Node $node, Environment $env): ?Node
if ($node instanceof ModuleNode) {
$node->setAttribute('escaper', $this->usesEscaper);
$this->defaultStrategy = false;
$this->safeVars = [];
$this->blocks = [];
} elseif ($node instanceof FilterExpression) {
if ($node instanceof EscapeFilter) {
Expand Down Expand Up @@ -159,8 +153,6 @@ private function isSafeFor(string $type, AbstractExpression $expression, Environ
$this->traverser = new NodeTraverser($env, [$this->safeAnalysis]);
}

$this->safeAnalysis->setSafeVars($this->safeVars);

$this->traverser->traverse($expression);
$safe = $this->safeAnalysis->getSafe($expression);
}
Expand Down
13 changes: 0 additions & 13 deletions src/NodeVisitor/SafeAnalysisNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Expression\FunctionExpression;
use Twig\Node\Expression\GetAttrExpression;
use Twig\Node\Expression\MacroReferenceExpression;
use Twig\Node\Expression\MethodCallExpression;
use Twig\Node\Expression\OperatorEscapeInterface;
use Twig\Node\Expression\ParentExpression;
use Twig\Node\Expression\Variable\ContextVariable;
use Twig\Node\Node;

/**
Expand All @@ -33,18 +31,12 @@ final class SafeAnalysisNodeVisitor implements NodeVisitorInterface
* @var \WeakMap<Node, array>
*/
private \WeakMap $data;
private $safeVars = [];

public function __construct()
{
$this->data = new \WeakMap();
}

public function setSafeVars(array $safeVars): void
{
$this->safeVars = $safeVars;
}

/**
* @return array
*/
Expand Down Expand Up @@ -135,11 +127,6 @@ public function leaveNode(Node $node, Environment $env): ?Node
} elseif ($node instanceof MethodCallExpression || $node instanceof MacroReferenceExpression) {
// all macro calls are safe
$this->setSafe($node, ['all']);
} elseif ($node instanceof GetAttrExpression && $node->getNode('node') instanceof ContextVariable) {
$name = $node->getNode('node')->getAttribute('name');
if (\in_array($name, $this->safeVars, true)) {
$this->setSafe($node, ['all']);
}
}

return $node;
Expand Down
27 changes: 27 additions & 0 deletions tests/Fixtures/tags/autoescape/import_alias_name_reused.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
"autoescape" tag escapes a variable that has the name of a macro import alias of another scope
--TEMPLATE--
{{ include('block.html.twig') }}
{{ include('macro.html.twig') }}
{{ include('late.html.twig') }}
--TEMPLATE(block.html.twig)--
{% extends 'base.html.twig' %}
{% block header %}{% import _self as item %}{{ item.hello() }}{% endblock %}
{% block content %}{% for item in items %}{{ item.body }}{{ item['body'] }}{% endfor %}{% endblock %}
{% macro hello() %}<b>hello</b>{% endmacro %}
--TEMPLATE(macro.html.twig)--
{%- macro hello() %}{% import _self as item %}{{ item.bold() }}{% endmacro %}
{% macro bold() %}<b>hello</b>{% endmacro %}
{% macro show(item) %}{{ item.body }}{% endmacro %}
{{ _self.hello() }}|{{ _self.show(items[0]) -}}
--TEMPLATE(late.html.twig)--
{%- block content %}{% for item in items %}{{ item.body ?? '' }}{% endfor %}{% endblock %}
{% import _self as item %}
--TEMPLATE(base.html.twig)--
{%- block header %}{% endblock %}|{% block content %}{% endblock %}
--DATA--
return ['items' => [['body' => '<br />']]]
--EXPECT--
<b>hello</b>|&lt;br /&gt;&lt;br /&gt;
<b>hello</b>|&lt;br /&gt;
&lt;br /&gt;
Loading