Follow the taints of Twig template values through filters, keys, includes and conditions - #392
Merged
Merged
Conversation
danog
force-pushed
the
twig-taint-precision
branch
from
October 5, 2026 08:44
bbcbef8 to
5a973c5
Compare
…n review - A variable of a template has its own node, given by key the parameter of its name of each render() call, rather than of every render() call: the taints of one call no longer reach the templates other calls render, and those of a second parameter are no longer lost (Psalm follows a single flow of given taints through a node, so the keys of a shared node left by one of them). - An included template is given every variable it, or a template it gives its context to, reads: also those it sets, and those read two includes down. - `include` with a variable, the `include()` function, `embed` (and the blocks it overrides) and `extends` give their context and display what the template outputs. - A variable set in an `if` or a `for` keeps the value it had, and a loop variable gets it back after the loop. - A filter takes the taints of its arguments. - A filter or function is found as Twig resolves it, also by a pattern. - The nodes of a line are told apart, and the template isn't read again for each.
…rollers of the tainting tests final The lowest dependencies installed 7.0.0-beta1, which lacks @psalm-capabilities and the taint API the plugin uses; and Psalm 7 reports a class never extended that isn't final.
…to Psalm 7 - With a beta minimum stability and no prefer-stable, the lowest dependencies were betas older than Psalm 7 works with (amphp 3.0.0-beta.10, ...); fidry/cpu-core-counter < 1.2 lacks the method Psalm 7 calls. - Psalm 7 reports a flow into a sink once, and Request::get() is internal since Symfony 6.4. The values given to Response are cast to string, which the tests aren't about.
Psalm 7.0.0-beta24 reports it twice, later commits once.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follows #390. Replaces #391, whose commit comes first here. Found while enabling taint analysis on a large codebase, where most template variables are arrays or objects with a few tainted keys.
What a filter or function returns (was #391)
A Twig filter is PHP code, but the taint analysis passed what it is given through to what it returns, whatever it does. A custom escaping filter (
{{ name|esc|raw }}, withesccallinghtmlspecialchars()and declaring@psalm-taint-escape html) was reported like{{ name|raw }}, and so was a filter returning a number ({{ items|length|raw }}).What a filter or function returns now takes the taints of what it is given (its arguments too:
{{ 'Hello %s'|format(name)|raw }}), but those its PHP callable removes: those its storage says it escapes (@psalm-taint-escape), and those its native return type cannot hold (intforlength). The callable is the one Twig resolves, also for a filter whose name matches a pattern (*_escape): a function, aClass::methodstring, a[class, method]array, or a first-class callable. A closure, or a callable Psalm does not know, removes nothing.Values followed by key
The taints of a template value were followed as a whole. Now they follow the array paths Psalm uses in PHP, so a key only has the taints put in that key:
render()call. Before, every variable had the taints of all of the template's parameters; and the parameters of a call reached the templates of other calls, as they went through the parameter node ofrender(), which all calls share;item.name,item['name']) is fetched by key. A method call (item.name()) or an attribute with a computed name still takes the taints of the whole value and of the arguments;forloop gives its key through anarraykey-fetchpath and its value through anarrayvalue-fetchpath.Each variable has its own node: Psalm follows a single flow of given taints through a node, so if the parameters of a template shared one, they would all leave by the key of the first. That is also why
$arr['y']isn't reported for$arr = ['x' => $_GET['a'], 'y' => $_GET['b']]; echo $arr['y'];in plain PHP: an issue of Psalm, not of this plugin.Includes, control flow
{% set title = title|default('x') %}), and those read by the templates it gives its context to, however deep.includewith a variable (with vars), theinclude()function,embed(and the blocks it overrides) andextendsgive their context and display what the template outputs.ifor aforcan keep the value it had, and a loop variable gets back its value after the loop.Tests: 17 new scenarios in
TwigTaintingWithAnalyzer.feature. The 2 of #391, the 3 of the keyed values, and 14 of the 15 for the rest (all but "An embedded template only given its own variables does not display a tainted parameter") fail without this change.phpunit(45 tests),codecept run -g symfony-common(129 tests) andpsalm --find-dead-code --find-unused-psalm-suppresspass.