Skip to content

Track accessCheck() anywhere in a chained entity query statement - #1091

Merged
mglaman merged 3 commits into
mainfrom
bug-613-chained-access-check
Nov 10, 2026
Merged

mglaman merged 3 commits into
mainfrom
bug-613-chained-access-check

Conversation

@mglaman

@mglaman mglaman commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

Fixes #613.

EntityQueryHasAccessCheckRule reported queries whose accessCheck() call was part of a chain that didn't end in execute():

$query = \Drupal::entityTypeManager()->getStorage('node')->getQuery();
$query
  ->condition('field_myfield', TRUE)
  ->accessCheck(FALSE);
$entity_ids = $query->execute();
Relying on entity queries to check access by default is deprecated in drupal:9.2.0 and an error will be thrown from drupal:10.0.0. ...

Why

AccessCheckTypeSpecifyingExtension marks the query as checked when accessCheck() is called. It had two gaps:

  • It only ran when accessCheck() was the outermost call in the statement. In $query->accessCheck(FALSE)->condition(...); the outermost call is condition().
  • It narrowed the expression 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 if accessCheck() 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 without accessCheck() 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, and bug-494.php caught it: PHPStan remembers the type of an identical expression, so a later \Drupal::entityQuery($entity_type)->condition(...)->execute() without accessCheck() stopped being reported. The self-review then caught that my fix for that allowed only variables and properties, which broke $queries['node']->accessCheck(FALSE) that main handled.

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, on main or here:

$ids = array_keys($query->condition('type', 'x')->accessCheck(FALSE)->execute());
return $query->count()->execute();

Covering those needs tracking the call as a side effect when PHPStan processes it, not through the type specifier.

Tests

  • New bug-613.php fixture with every variant from the issue: accessCheck() first and last in the chain, a chain with andConditionGroup(), and several chained conditions. It also covers a property, a static property, an array element, a nullsafe link and a count() chain. None are reported. A chain without accessCheck() is still reported.
  • Before the fix, three of the four issue variants were reported. $query->accessCheck(FALSE)->condition(...) with a single condition already passed.
  • The existing entity query tests pass, including bug-494.php.
  • php vendor/bin/phpunit (922 tests), phpcs and phpstan analyze pass.
  • Core 11.4.4 lib and modules: no reports from this rule before or after. Core always calls accessCheck(), so this only confirms nothing new is reported.

🤖 Generated with Claude Code

mglaman and others added 2 commits November 10, 2026 13:14
$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>
@mglaman
mglaman merged commit fc98586 into main Nov 10, 2026
19 of 20 checks passed
@mglaman
mglaman deleted the bug-613-chained-access-check branch November 10, 2026 20:04
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.

EntityQuery accessCheck() false positive if chaining conditions

1 participant