Problem/Motivation

When enabling the 'Account Administration Pages' language negotiator for interface language, the Toolbar module throws an 'Access Denied' when trying to load the cached subtrees.

Steps to reproduce

  1. Install a standard installation (drush si)
  2. Do NOT disable caching in for example settings.local.php
  3. Enable content translation and interface translation
  4. enable Spanish and English
  5. Set language negotiation as follows:
  6. Set preferred administration language of user 1 to english.
  7. Create an article and view the full node
  8. Navigate to the same page but in 'Spanish' using the language switcher or just adding 'es' to the url. Should be [localhost]/es/node/1

The toolbar breaks, because of a 403 on an ajax request to load the subtrees. The browser will show the following error:
POST http://localhost/devdays/toolbar/subtrees/nAMBTIOqWe13vT5ZIhj_JroJ1d-FcDGfVKyhzq6wpFA?_wrapper_format=drupal_ajax 403 (Forbidden)

The problem has to do with the fact that the hashes for the cached subtree and the requested subtree don't coincide and therefore return a 403. See
ToolbarController::checkSubTreeAccess()

We have to find out whether this problem lies with the language negotiator for Account administration pages (and possibly other ones too, haven't tested...) or with the Toolbar module.

Proposed resolution

-

Remaining tasks

-

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nuez created an issue. See original summary.

nuez’s picture

Version: 8.3.x-dev » 8.4.x-dev
tar_inet’s picture

Status: Active » Needs work

I have followed all the steps and I can´t reproduce it. May I missing something? How do you get this error? Just loading the page? I can't see any error on the toolbar.

nuez’s picture

Issue summary: View changes
FileSize
21.51 KB
130.96 KB
nuez’s picture

Issue summary: View changes

I've checked it again and the issue still appears with the latest 8.4.x.

I've updated the steps with images to make it clearer. Please let us know if you can reproduce it.

kjauslin’s picture

I can also see this problem, using 8.3.0. Having "Account administration pages" activated at the language detection page at /admin/config/regional/language/detection. When de-activating the "Account administration pages" setting, the ajax seems to work again. I also have the primary language setting at something different than English.

rferguson’s picture

I'm also having this issue when that setting is enabled. Logged in or not, I can't visit my french content pages. I just get page not found.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

casey’s picture

Component: content_translation.module » toolbar.module
casey’s picture

Status: Needs work » Needs review
FileSize
865 bytes
nuez’s picture

I'm not sure if disabling the router access checks is the right approach, I think it would be better to find out the exact root of the problem by writing a test.

nuez’s picture

Status: Needs review » Needs work
nuez’s picture

The problem seems to be related to fact that the subtree rendered in two different places and checked if they’re still the exact same subtree with a hash. Once the subtree is generated with the page request, and another via ajax calling ‘/toolbar/subtrees/{hash}’.

The hash of both instances of the subtree needs to be compared, and if they don’t coincide there is a 403 on ‘toolbar/subtrees/{hash}’. I’ve found incorrect 403 statuses in at least 2 occasions:

The preferred language for administration pages of the user is english, the content language of a non-admin page is say Spanish. Then the toolbar subtree will render Spanish in the DOM but in english in /toolbar/subtrees/{hash}, causing a 403.
It also seems to occur when cache is switched on and the user switches to a language for which no previous cached subtree has been rendered. (I still have to further investigate this problem).

Solution: Make sure that language of the /toolbar/subtree/{hash} path is negotiated in the same way as the page where the toolbar is coming from.

Next step: Write a test to reproduce the issue.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

candelas’s picture

Having the same problem.
@nuez would you have a moment to make the test, please? Thanks

rwam’s picture

Priority: Normal » Major

Patch in #10 has no effects and doesn't solve the problem for our sites. We're using content and interface translation too, but we get always an 403.

For now, I remove the check if the hashes are equals to provide a working toolbar menu on ToolbarController::checkSubTreeAccess():

diff --git a/core/modules/toolbar/src/Controller/ToolbarController.php b/core/modules/toolbar/src/Controller/ToolbarController.php
index b4abf27c2..b1672e226 100644
--- a/core/modules/toolbar/src/Controller/ToolbarController.php
+++ b/core/modules/toolbar/src/Controller/ToolbarController.php
@@ -49,7 +49,7 @@ public function subtreesAjax() {
    */
   public function checkSubTreeAccess($hash) {
     $expected_hash = _toolbar_get_subtrees_hash()[0];
-    return AccessResult::allowedIf($this->currentUser()->hasPermission('access toolbar') && Crypt::hashEquals($expected_hash, $hash))->cachePerPermissions();
+    return AccessResult::allowedIf($this->currentUser()->hasPermission('access toolbar'))->cachePerPermissions();
   }

 }

May be it relates to this bug, but on the horizontal bar the active trail isn't working.

And I would change priority to Major because the backend is unusable with vertical toolbar.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

idflood’s picture

While it's not the correct solution the patch was still a good workaround for our use case. The patch didn't apply anymore on 8.9.* so here is a reroll.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

smulvih2’s picture

#22 works for me on 8.8.10

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Rar9’s picture

hi i applied patch 22, but I still get above issue sometimes

D9.1.10
Lang DE as default + eng + Account administration pages

Path: /en/toolbar/subtrees/kEIgVbrAnk8tVlQ2I7zujQnIh9zFpVDT4RPSO9jovOs?_wrapper_format=drupal_ajax. Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException: in Drupal\Core\Routing\AccessAwareRouter->checkAccess() (line 120 of /var/www/vhosts/drupal9/web/core/lib/Drupal/Core/Routing/AccessAwareRouter.php).

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

sonneworks’s picture

tried patch 22, did not work.
Found out that the _toolbar_get_subtrees_hash() produces a different hash because the initial toolbar_get_rendered_subtrees() contained the "scheduled content" from the scheduler module and the menu generated when doing the ajax request did not contain the scheduled content item. Not sure why, i've disabled the view and the problem was no longer present

not a solution but at least i can continue for now

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jordan.jamous’s picture

Problem still exists in Drupal 10.1.4, has anyone found a solution for this matter? #22 did't work for me. My setup is similar to the setup explained in the issue description. Thanks

tvalimaa’s picture

I have the same problem with same kind of settings and Drupal 10.1.4. #22 patch didn't work.

What I debug toolbar modules code I find that ToolbarController.php checkSubTreeAccess() function $hash and $expected_hash are not match so hash_equals() returns false.

candelas’s picture

I have the same problem with same kind of settings and Drupal 10.2.1 .
I have Catalan as main language and I as user English (I am user 1).
My solution is not to use admin language in the language negotiation and set first user and then url as the only Detection methods.