Blocked by #2630920: _csrf_token is broken due to cacheability metadata integration, results in rendered links without valid CSRF tokens

---

In RenderElement::preRenderAjaxForm, the following is how the url setting is processed:

      // Convert \Drupal\Core\Url object to string.
      if (isset($settings['url']) && $settings['url'] instanceof Url) {
        $url = $settings['url']->setOptions($settings['options'])->toString(TRUE);
        BubbleableMetadata::createFromRenderArray($element)
          ->merge($url)
          ->applyTo($element);
        $settings['url'] = $url->getGeneratedUrl();
      }
      else {
        $settings['url'] = NULL;
      }

Because of ->toString(TRUE), the bubbled metadata is collected and the returned value is \Drupal\Core\GeneratedUrl.

It then goes into applying said metadata to the element passed. This would be fine if the Url was ultimately rendered on the page (as markup). However, this is the array that is passed into drupalSettings. The placeholder here is never rendered and, thus, the CSRF token never replaced.

I tried simply removing TRUE thinking it would simply return the proper token, but that doesn't work.

Decided to step through this:

Url::toString() invokes MetadataBubblingUrlGenerator::generateFromRoute() which $collect_bubbleable_metadata defaults to FALSE.

However, in that method, it invokes the non-bubbling UrlGenerator::generateFromRoute where $collect_bubbleable_metadata is explicitly set to TRUE:

$generated_url = $this->urlGenerator->generateFromRoute($name, $parameters, $options, TRUE);

This, in turn, causes RouteProcessorCsrf::processOutbound to ALWAYS return a #lazy_builder placeholder and not the actual token.

---

Until I can get some feedback on why/what is going on here, I created a temporary workaround so it would use the correct CSRF token in the Url:

    // Ensure the Url passed is properly generated.
    if (isset($ajax_settings['url'])) {
      /** @var \Drupal\Core\Url $url */
      $url = $ajax_settings['url'];
      $generated_url = $url->toString(TRUE);
      if (($attachments = $generated_url->getAttachments()) && !empty($attachments['placeholders'])) {
        $build['#markup'] = $generated_url->getGeneratedUrl();
        $build['#attached'] = $attachments;
        /** @var \Drupal\Core\Render\Renderer $renderer */
        $renderer = \Drupal::service('renderer');
        foreach (array_keys($build['#attached']['placeholders']) as $placeholder) {
          $build = $renderer->renderPlaceholder($placeholder, $build);
        }
        $ajax_settings['url'] = Url::fromUserInput((string) $build['#markup']);
      }
      // RenderElement::preRenderAjaxForm overrides any token that is already
      // set when it renders the Url object. However, it can be "reset" back to
      // the original query by setting the #ajax options property explicitly.
      $ajax_settings['options']['query'] = $ajax_settings['url']->getOption('query');
    }

Comments

markcarver created an issue. See original summary.

markhalliwell’s picture

Turns out this is part of a larger problem. Postponing any "fixes" to #ajax until that issue can be resolved.

markhalliwell’s picture

Title: Custom #ajax Url with _csrf_token route doesn't generate correct token » _csrf_token in route breaks custom #ajax url
Issue tags: +CSRF, +CSRF Token

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

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.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.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.

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.

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.

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.

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.

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.

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.

catch’s picture

Title: _csrf_token in route breaks custom #ajax url » [PP-1] _csrf_token in route breaks custom #ajax url

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.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.