Repository navigation
Track accessCheck() anywhere in a chained entity query statement - #1091
Merged
Merged
Conversation
$query->accessCheck(FALSE)->condition('status', 1); left $query
unchecked, because the type specifier only ran when accessCheck() was
the outermost call, and then narrowed the expression it was called on
instead of $query. Walk the chain down to the variable or property that
holds the query and mark it checked.
Calls such as \Drupal::entityQuery('node') are not narrowed: each call
returns a new query.
Fixes #613.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Narrow any non-call starting point, so a query in an array element, like $queries['node'], is marked checked again. Only calls are skipped. - Walk through nullsafe links, like $query?->condition()->accessCheck(). - Check method names before resolving types, so chains without accessCheck() cost nothing. - Cover properties, static properties, array elements, nullsafe links and count() chains in the fixture. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Fixes #613.
EntityQueryHasAccessCheckRulereported queries whoseaccessCheck()call was part of a chain that didn't end inexecute():Why
AccessCheckTypeSpecifyingExtensionmarks the query as checked whenaccessCheck()is called. It had two gaps:accessCheck()was the outermost call in the statement. In$query->accessCheck(FALSE)->condition(...);the outermost call iscondition().accessCheck()was called on. In$query->condition(...)->accessCheck(FALSE);that's$query->condition(...), not$query.Fix
The extension now runs for every query method. It walks the chain down to where it starts, including nullsafe
?->links, and ifaccessCheck()is anywhere in the chain, it marks that starting point as checked. Query methods return the query itself, so this is the same object. It checks method names first, so a chain withoutaccessCheck()resolves no types.It doesn't narrow a chain that starts from a call, like
\Drupal::entityQuery('node'), because each call returns a new query. Variables, properties, static properties and array elements are narrowed. My first version narrowed the call expression, andbug-494.phpcaught it: PHPStan remembers the type of an identical expression, so a later\Drupal::entityQuery($entity_type)->condition(...)->execute()withoutaccessCheck()stopped being reported. The self-review then caught that my fix for that allowed only variables and properties, which broke$queries['node']->accessCheck(FALSE)thatmainhandled.Not covered
PHPStan only runs type-specifying extensions on a statement's outermost call, an assignment's right side, and conditions.
accessCheck()inside a function argument or a comparison still doesn't mark the query, onmainor here:Covering those needs tracking the call as a side effect when PHPStan processes it, not through the type specifier.
Tests
bug-613.phpfixture with every variant from the issue:accessCheck()first and last in the chain, a chain withandConditionGroup(), and several chained conditions. It also covers a property, a static property, an array element, a nullsafe link and acount()chain. None are reported. A chain withoutaccessCheck()is still reported.$query->accessCheck(FALSE)->condition(...)with a single condition already passed.bug-494.php.php vendor/bin/phpunit(922 tests),phpcsandphpstan analyzepass.libandmodules: no reports from this rule before or after. Core always callsaccessCheck(), so this only confirms nothing new is reported.🤖 Generated with Claude Code