Fix caching routes by users with an active session #56921
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 #56789, which is a regression introduced in #52793
When a user has an active session only the apps that are enabled for the user are initially loaded*. In order to cache the routes the routes for all apps are loaded, but routes defined in routes.php are taken into account only if the app was already loaded. Therefore, when the routes were cached in a request by a user with an active session only the routes for apps enabled for that user were cached, and those routes were used by any other user, independently of which apps they had access to. To solve that now all the enabled apps are explicitly loaded before caching the routes.
Note that this did not affect routes defined using annotations on the controller files; in that case the loaded routes do not depend on the previously loaded apps, as it explicitly checks all the enabled apps.
*As soon as the session is initialized, which happens when loading base.php, the legacy
OC_APP::getEnabledAppswill return only the apps enabled for the user. That method is used byAppManager::loadApps, so once the session is initialized any load of (several) apps will be restricted to those enabled for the user (explicitly loading a single app still works as expected). Therefore, when$appManager->loadApps()is called from the OCS handler or from the index.php handler (throughhandleRequestin base.php) only the apps for the user are loaded.Steps to reproduce
'memcache.local' => '\\OC\\Memcache\\APCu', to config.php) if not enabled alreadyweather_statusapp only for a specific group (for simplicity admin is used here)Clear the APCu cache (call
apcu_clear_cache()somehow, for example using https://github.com/krakjoe/apcu/blob/master/apc.php or the helper apps/testing/clean_apcu_cache.php added in this pull request; restarting the web server would reset the APCu cache, but it might also kill the user session and make the test invalid)In the same Bash terminal as before, do a request with the logged in user, for example:
weather_statusapp by a user member of the group that it is enabled for (again for simplicity admin is used here):Result with this pull request
The query succeeded
Result without this pull request
Invalid query returned; if the APCu cache is cleared again and the request repeated then it will now succeed (as the routes will be regenerated by the admin, which has access to the app)