Skip to content

Taint the array a foreach by reference writes into - #12121

Merged
danog merged 4 commits into
vimeo:masterfrom
danog:taint-foreach-by-ref
Oct 5, 2026
Merged

danog merged 4 commits into
vimeo:masterfrom
danog:taint-foreach-by-ref

Conversation

@danog

@danog danog commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

What a foreach (... as &$value) loop writes through $value now flows into the array it iterates over, so it isn't lost:

$values = ["key" => ""];
foreach ($values as &$value) {
    $value = (string) $_GET["value"];
}
unset($value);
echo $values["key"]; // TaintedHtml

The writes are taken from what $value holds at the end of an iteration and where the loop is left (break), and reach the array through an unkeyed arrayvalue-assignment path. When the array iterated over is a property, the property holds them too, so the taint reaches reads in other methods.

Values overwritten before the iteration ends, and writes in a foreach by value, don't taint the array.

@danog danog added the release:fix The PR will be included in 'Fixes' section of the release notes label Oct 5, 2026
danog added 2 commits October 5, 2026 13:01
# Conflicts:
#	src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php
#	tests/TaintTest.php
@danog
danog merged commit e65f20b into vimeo:master Oct 5, 2026
59 of 60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fix The PR will be included in 'Fixes' section of the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant