Problem/Motivation
The content_access processor filters a query by the searching user's permissions, but adds no cache contexts to the query. ContentAccess::addNodeAccess() checks bypass node access, access content, access comments, view any unpublished content and view own unpublished content.
Query::getCacheContexts() only merges the datasources' list cache contexts. For nodes that is user.node_grants:view. When no module implements hook_node_grants(), NodeAccessGrantsCacheContext::getContext() returns all for every user. Two users who differ only in access content then get the same cache key for different results.
This shows up wherever a query's cacheability is used to cache a response. In JSON:API Search API, an empty page beyond the last result still returns the access-filtered total in meta.count, and no returned entities add their access cacheability: #3612675.
With view own unpublished content, the processor adds a condition on the author field with the user's ID, so results also vary per user.
Steps to reproduce
- Create a node index with the
content_accessprocessor and index one published node. - Create one user with
access contentand one without. Enable no node grants module. - Run the same query as each user and convert
$query->getCacheContexts()to cache keys: the keys match, but the result counts are 1 and 0.
Proposed resolution
In ContentAccess::preprocessSearchQuery(), add the contexts the access conditions depend on:
if ($account instanceof AccountInterface) { $query->addCacheContexts(['user.permissions']); $this->addNodeAccess($query, $account); }
Also add the user context when the account has view own unpublished content and lacks view any unpublished content, because the author condition makes the results per user.
When search_api_access_account names a different account than the current user, the current user's contexts do not describe the results. That case may need no contexts, or a max-age of 0.
I checked the user.permissions part with the Kernel test in jsonapi_search_api !27: its permission test fails without the line above and passes with it.
Remaining tasks
- MR with the change and a Kernel test for both contexts
- Decide how to handle
search_api_access_account
User interface changes
None.
API changes
None. Queries using the content_access processor gain cache contexts.
Data model changes
None.
AI disclosure
Drafted with the help of an LLM and reviewed by me.
Comments