Skip to content

Follow the taints of Twig template values through filters, keys, includes and conditions - #392

Merged
danog merged 7 commits into
psalm:5.xfrom
danog:twig-taint-precision
Oct 5, 2026
Merged

danog merged 7 commits into
psalm:5.xfrom
danog:twig-taint-precision

Conversation

@danog

@danog danog commented Oct 3, 2026 •

Copy link
Copy Markdown
Member

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 }}, with esc calling htmlspecialchars() 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 (int for length). The callable is the one Twig resolves, also for a filter whose name matches a pattern (*_escape): a function, a Class::method string, 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:

  • each variable of a template is given, by key, the parameter of its name of each 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 of render(), which all calls share;
  • an attribute with a constant name (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;
  • a for loop gives its key through an arraykey-fetch path and its value through an arrayvalue-fetch path.

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

  • An included template is given every variable it reads, also those it sets ({% set title = title|default('x') %}), and those read by the templates it gives its context to, however deep.
  • include with a variable (with vars), 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 can keep the value it had, and a loop variable gets back its value after the loop.
  • The nodes of a line of a template are told apart: two expressions of a line no longer share their taints.

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) and psalm --find-dead-code --find-unused-psalm-suppress pass.

@danog
danog force-pushed the twig-taint-precision branch from bbcbef8 to 5a973c5 Compare October 5, 2026 08:44
@danog
danog changed the base branch from master to 5.x October 5, 2026 08:48
…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.
@danog danog changed the title Follow the taints of Twig template values by key, through loops and functions Follow the taints of Twig template values through filters, keys, includes and conditions Oct 5, 2026
@danog danog closed this Oct 5, 2026
@danog danog reopened this Oct 5, 2026
danog added 3 commits October 5, 2026 11:58
…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.
@danog
danog merged commit bee6a8b into psalm:5.x Oct 5, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant