Review all usages of Xss::filter(), Xss::filterAdmin(), and Html::escape()

Created on 6 July 2015, almost 10 years ago
Updated 9 April 2025, 15 days ago

#2506195: Remove SafeMarkup::set() from Xss::filter() β†’ has revealed that we should be relying on the render system's auto-escape and auto-filtering more. If a string is not marked safe then the render system will automatically escape HTML. Therefore calls to SafeMarkup::checkPlain() like:

    $form['admin_label'] = array(
      '#type' => 'item',
      '#title' => $this->t('Block description'),
      '#markup' => SafeMarkup::checkPlain($definition['admin_label']),
    );

Are unnecessary. Here we could just change #markup to #value and rely on auto-escaping.

The calls to Xss::filterAdmin can be replaced by converting to a render array. For example:

      $variables['types'][$type->id()] = array(
        'type' => $type->id(),
        'add_link' => \Drupal::l($type->label(), new Url('node.add', array('node_type' => $type->id()))),
        'description' => Xss::filterAdmin($type->getDescription()),
      );

We can just do 'description' => array('#markup' => $type->getDescription()), instead because all #markup is auto-filtering for the admin tag list by the render system.

πŸ“Œ Task
Status

Needs work

Version

11.0 πŸ”₯

Component

theme system

Created by

πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡«πŸ‡·France duaelfr Montpellier, France

    This is quite old!
    I was digging into an issue with inline templating within Views and found this piece of code from \Drupal\views\Plugin\views\PluginBase::viewsTokenReplace():

          // Use the unfiltered text for the Twig template, then filter the output.
          // Otherwise, Xss::filterAdmin could remove valid Twig syntax before the
          // template is parsed.
    
          $build = [
            '#type' => 'inline_template',
            '#template' => $text,
            '#context' => $twig_tokens,
            '#post_render' => [
              function ($children, $elements) {
                return Xss::filterAdmin($children);
              },
            ],
          ];
    
          // Currently you cannot attach assets to tokens with
          // Renderer::renderInIsolation(). This may be unnecessarily limiting. Consider
          // using Renderer::executeInRenderContext() instead.
          // @todo https://www.drupal.org/node/2566621
          return (string) $this->getRenderer()->renderInIsolation($build);
    

    Here, even if we are using Twig's render engine, filterAdmin is called on post_process without any way to bypass it. In my case, I have an inline template that includes an image field rendered with a responsive style. The <picture> and <srcset> tags are dropped by this post process so I must use an external template instead of the builtin feature of views.

Production build 0.71.5 2024