Skip to content

Commit 22d9ab0

Browse files
committed
fix: Fix unpredictable false positive/negative of missing entity access check in function scope due to cache key conflict #475
1 parent 480245d commit 22d9ab0

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

src/Type/EntityQuery/EntityQueryType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ public function withAccessCheck(): self
2222

2323
return $type;
2424
}
25+
26+
protected function describeAdditionalCacheKey(): string
27+
{
28+
return $this->hasAccessCheck ? 'with-access-check' : '';
29+
}
2530
}

tests/src/Rules/EntityQueryHasAccessCheckRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,15 @@ public function cases(): \Generator
8686
[__DIR__ . '/data/bug-437.php'],
8787
[]
8888
];*/
89+
90+
yield 'bug-475.php' => [
91+
[__DIR__.'/data/bug-475.php'],
92+
[]
93+
];
94+
95+
yield 'bug-475b.php' => [
96+
[__DIR__.'/data/bug-475b.php'],
97+
[]
98+
];
8999
}
90100
}

tests/src/Rules/data/bug-475.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/**
4+
* Cache key differs outside of class so this is separate reproduction
5+
*
6+
* @return void
7+
*/
8+
function bug475Caching(): void
9+
{
10+
// Here condition() return type will be cached as one that has no access check
11+
\Drupal::entityQuery('node')
12+
->condition('field_test', 'foo', '=')
13+
->accessCheck(FALSE)
14+
->execute();
15+
16+
// Cache return on condition() will also be no access check, even though caller did, unless caller type changes cache key
17+
\Drupal::entityQuery('node')
18+
->accessCheck(FALSE)
19+
->condition('field_test', 'foo', '=')
20+
->execute();
21+
}

tests/src/Rules/data/bug-475b.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Bug475CacheKeyExample;
4+
5+
/**
6+
* Cache key differs if in class so this tests that too
7+
*
8+
* @return void
9+
*/
10+
class TestClass
11+
{
12+
public function bug475Caching(): void
13+
{
14+
// Here condition() return type will be cached as one that has no access check
15+
\Drupal::entityQuery('node')
16+
->condition('field_test', 'foo', '=')
17+
->accessCheck(FALSE)
18+
->execute();
19+
20+
// Cache return on condition() will also be no access check, even though caller did, unless caller type changes cache key
21+
\Drupal::entityQuery('node')
22+
->accessCheck(FALSE)
23+
->condition('field_test', 'foo', '=')
24+
->execute();
25+
}
26+
}

0 commit comments

Comments
 (0)