Problem/Motivation

Views block descriptions are double escaped in the block configuration page if the views block has a custom display name.

Steps to reproduce

  1. Install Drupal
  2. Create a views block with a character such as & in the block title and set a custom 'Display name'. Note that the character can be in the block title or display name, the double encoding will occur as long as the display name is set.
  3. Go to Structure > Block layout and place the block
  4. See that the block description is double escaped
    Screenshot

Proposed resolution

Don't double escape the block description

Remaining tasks

  • Write a patch with tests
  • Review

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

CommentFileSizeAuthor
#43 interdiff-3199730-27-42.txt5.02 KBmohit_aghera
#43 3199730-42--test-only.patch8.91 KBmohit_aghera
#43 3199730-42.patch10.18 KBmohit_aghera
#42 3199730-42--test-only.patch8.91 KBmohit_aghera
#42 interdiff-3199730-27-42.txt5.02 KBmohit_aghera
#42 3199730-42.patch10.18 KBmohit_aghera
#31 0fgFodt.png43.1 KBilgnerfagundes
#27 interdiff-3199730-18-27.txt2.54 KBmohit_aghera
#27 3199730-27-test-only.patch5.78 KBmohit_aghera
#27 3199730-27.patch6.99 KBmohit_aghera
#21 After--patch.jpg35.93 KBranjith_kumar_k_u
#21 Before--patch.jpg40.04 KBranjith_kumar_k_u
#20 block-with-amps.png18.53 KBroman-yrv
#19 block-with-amps.png28.74 KBroman-yrv
#18 interdiff-3199730-17-18.txt927 bytesmohit_aghera
#18 3199730-18.patch7.3 KBmohit_aghera
#17 interdiff-3199730-12-17.txt6.68 KBmohit_aghera
#17 3199730-17.patch7.29 KBmohit_aghera
#15 Screenshot 2021-03-02 at 13.29.52.png27.46 KBgauravvvv
#14 3199730-after-12.png23.6 KBshriaas
#14 3199730-view-block.png10.15 KBshriaas
#12 3199730-after.png16.84 KBabhijith s
#12 3199730-before.png21.13 KBabhijith s
#12 3199730-12.patch621 bytesabhijith s
#11 Screenshot 2021-03-02 at 11.19.35.png43.06 KBgauravvvv
#8 Screenshot 2021-03-02 at 10.07.55.png66.42 KBgauravvvv
#8 Screenshot 2021-03-02 at 10.07.11.png113.7 KBgauravvvv
#7 Screen Shot 2021-03-02 at 1.48.43 pm.png17.19 KBpameeela
#7 Screen Shot 2021-03-02 at 1.46.58 pm.png53.79 KBpameeela
#6 3199730-block-description.png64.25 KBshriaas
#5 Screenshot 2021-02-24 at 18.31.57.png71.74 KBgauravvvv
Screenshot_2.png22.44 KBkumar ashutosh

Issue fork drupal-3199730

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Kumar Ashutosh created an issue. See original summary.

cilefen’s picture

Title: Html decode is not working for block title » Block description is double-escaped
Issue tags: -html, -Seven theme, -Drupal 8.x
larowlan’s picture

Issue tags: +Novice, +Needs tests

AJV009 made their first commit to this issue’s fork.

gauravvvv’s picture

StatusFileSize
new71.74 KB

Working fine on 9.1 and 9.2
Attached a screenshot for reference.

shriaas’s picture

StatusFileSize
new64.25 KB

Working fine on 8.9.x-dev attaching screenshot for same.

pameeela’s picture

Title: Block description is double-escaped » Views block description is double-escaped
Version: 8.9.x-dev » 9.2.x-dev
Issue summary: View changes
Issue tags: +Bug Smash Initiative
StatusFileSize
new53.79 KB
new17.19 KB

This was very easy to reproduce using the steps provided (i.e. a views block, not a custom block) but I have updated the summary to be a bit clearer.

gauravvvv’s picture

Hello @pameeela, I am using view block, and still not reproducible to me on a fresh install. Adding screenshot for reference.

pameeela’s picture

@Gauravmahlawat your screenshot shows that you have set the block title, not the display name. Please see the screenshot referenced in the issue summary.

pameeela’s picture

Title: Views block description is double-escaped » Views block description is double-escaped if display name is set
Issue summary: View changes

Updated STR to clarify.

gauravvvv’s picture

StatusFileSize
new43.06 KB

@pameeela, thank you for clarifying. This issue reproducible now.

abhijith s’s picture

StatusFileSize
new621 bytes
new21.13 KB
new16.84 KB

Before patch:
before

After patch:
after

Please review this.

abhijith s’s picture

Status: Active » Needs review
shriaas’s picture

StatusFileSize
new10.15 KB
new23.6 KB

Patch applied successfully on 8.9.x-dev. Issue is resolved after applying the patch, attaching screenshots for same:

gauravvvv’s picture

StatusFileSize
new27.46 KB

#12 looks good. Moving to RTBC +1

larowlan’s picture

Status: Needs review » Needs work

Still needs tests - thanks

mohit_aghera’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new7.29 KB
new6.68 KB

Adding new test cases with sample view.
Fixed existing failures.

mohit_aghera’s picture

StatusFileSize
new7.3 KB
new927 bytes

Fixing phpcs issues in the patch.

roman-yrv’s picture

Issue summary: View changes
Status: Needs review » Needs work
StatusFileSize
new28.74 KB

I've just tested it.
Unfortunately, it isn't fixed now.
Block with amps

roman-yrv’s picture

Status: Needs work » Needs review
StatusFileSize
new18.53 KB

Sorry, I've made a mistake in my previous comment.
I've tested it again with this patch (https://www.drupal.org/files/issues/2021-03-03/3199730-18.patch), it works!
block with amps

ranjith_kumar_k_u’s picture

StatusFileSize
new40.04 KB
new35.93 KB

The last patch works fine
Before patch
before patch
After patch
after patch
RTBC.

gauravvvv’s picture

Don't forget to change the status when moving to RTBC.

gauravvvv’s picture

Status: Needs review » Reviewed & tested by the community
larowlan’s picture

Status: Reviewed & tested by the community » Needs work

Thanks everyone, can we get a test-only patch here that fails, to demonstrate the new test-coverage resolves the issue.

+++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.articles_and_pages.yml
--- a/core/modules/views/tests/src/Functional/Wizard/BasicTest.php
+++ b/core/modules/views/tests/src/Functional/Wizard/BasicTest.php

+++ b/core/modules/views/tests/src/Functional/Wizard/BasicTest.php
@@ -220,4 +227,14 @@ public function testWizardDefaultValues() {
+  public function testEscapedBlockDescription() {
+    // Visit the block placement URL directly and validate block description.

Any reason we're putting this in one of the Wizard tests and not in a general Views UI test?

mohit_aghera’s picture

Thanks, @larowlan. I'll add the test-only patch.

My main intention to keep in Wizard was that, while placing the block it follows the block placement flow. i.e. visit the admin/structure/block page etc. So I thought of to keep in Wizard/BasicTest.php and views module.

Later I realized that we can test it directly by visiting the page admin/structure/block/add/views_block%3Aarticles_and_pages-block_1/

Should I move it along with views_ui module's test cases?

larowlan’s picture

\Drupal\Tests\views_ui\Functional\XssTest would be a good spot I guess, because it already has testNoDoubleEscaping

mohit_aghera’s picture

Component: Seven theme » Olivero theme
Status: Needs work » Needs review
StatusFileSize
new6.99 KB
new5.78 KB
new2.54 KB

Refactored test cases and moved to relevant place.

Status: Needs review » Needs work

The last submitted patch, 27: 3199730-27-test-only.patch, failed testing. View results

mherchel’s picture

Component: Olivero theme » block.module

This is is not related to Olivero.

mohit_aghera’s picture

Status: Needs work » Needs review

@mherchel I think it got updated by mistake. Sorry for the confusion.

Moving it to needs review as failures is related to test-only patch.

ilgnerfagundes’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new43.1 KB

I did the test locally and the title is getting correct, and the test that failed that was previously requested seems to be working correctly as well.

  • larowlan committed fd9f708 on 9.2.x
    Issue #3199730 by mohit_aghera, Abhijith S, AJV009, Gauravmahlawat,...

  • larowlan committed cc98b68 on 9.1.x
    Issue #3199730 by mohit_aghera, Abhijith S, AJV009, Gauravmahlawat,...
larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Committed fd9f708 and pushed to 9.2.x. Thanks!

Backported to 9.1.x

For future reference folks, one set of before/after screenshots is enough.

If you come to an issue and find someone else has added them, move on.

larowlan’s picture

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

  • larowlan committed c8f77b5 on 9.1.x
    Revert "Issue #3199730 by mohit_aghera, Abhijith S, AJV009,...

  • larowlan committed cc0730d on 9.2.x
    Revert "Issue #3199730 by mohit_aghera, Abhijith S, AJV009,...
larowlan’s picture

Status: Fixed » Needs work

Was discussing this with @alexpott who pointed out we should be limiting the allowed tags via

'#allowed_tags' => Xss::getHtmlTagList()

So I've reverted this.

Can we add that function in folks.

larowlan credited alexpott.

larowlan’s picture

alexpott’s picture

This has been plain text since D7... see the following code from D7:

    $form['blocks'][$key]['info'] = array(
      '#markup' => check_plain($block['info']),
    );

so changing this to support html needs a big security think...

My original thought was to change to use a more restrictive tag list. This has the advantage or supporting some tags but being way more restrictive than the admin list that's the default for #markup.

The other option would be to do

    $form['admin_label'] = [
      '#type' => 'item',
      '#title' => $this->t('Block description'),
      '#plain_text' => PlainTextOutput::renderFromHtml($definition['admin_label']),
    ];

This would have the advantage of not changing the behaviour. However, I think restricting the taglist is probably best because then admin labels that have been translated can use tags like bdo which are specifically for translations.

mohit_aghera’s picture

Version: 9.1.x-dev » 9.2.x-dev
Status: Needs work » Needs review
StatusFileSize
new10.18 KB
new5.02 KB
new8.91 KB

Hi @larowlan @alexpott

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

I tried to add #allowed_tags on the label field, somehow it is not working as expected.
Can it be because it is specifically for render elements?
To confirm this, I've added test-only patch.

For the implementation, I took different approach and added code inside ViewsBlock derivative plugins.
We can filter tags there and then render markup in BlockPluginTrait class.
I've updated patch accordingly.
Can you please confirm if this is the right approach.

mohit_aghera’s picture

StatusFileSize
new10.18 KB
new8.91 KB
new5.02 KB

Uploading patch by resolving phpcs issues.

alexpott’s picture

Status: Needs review » Needs work

@mohit_aghera '#allowed_tags' => Xss::getHtmlTagList(), works as I would expect it.

One thing that is apparent is that we need to match what we do in \Drupal\Core\Block\BlockPluginTrait::buildConfigurationForm() in \Drupal\block\BlockListBuilder::buildBlocksForm()

          $form[$entity_id]['info'] = [
            '#plain_text' => $info['status'] ? $info['label'] : $this->t('@label (disabled)', ['@label' => $info['label']]),
            '#wrapper_attributes' => [
              'class' => ['block'],
            ],
          ];

This needs the same change.

alexpott’s picture

However a problem with using Xss::getHtmlTagList() is that it doesn't allow bdo or any of the other translation related tags and it does allow ul and li which have no business in the form or the listing page. So perhaps the PlainTextOutput::renderFromHtml() is the best solution for now. We should consider opening an issue to add a list of tags for titles that only includes inline html tags.

+++ b/core/lib/Drupal/Core/Block/BlockPluginTrait.php
@@ -161,7 +162,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
-      '#plain_text' => $definition['admin_label'],
+      '#markup' => PlainTextOutput::renderFromHtml($definition['admin_label']),

If we're going to use PlainTextOutput::renderFromHtml() then we can leave this as #plain_text.

alexpott’s picture

This issue is a sort of duplicate of #2568045: Make it impossible to double escape with #plain_text - which is about the wider problem. Also this issue gives me an idea about a good fix for that.

alexpott’s picture

#2568045: Make it impossible to double escape with #plain_text now has an alternate fix in the heart of the render system that will help all cases of #plain_text => MarkupInterface object - it includes some of the test developed here so if we decide to go forward with that we should transfer credit for people who developed the test to that issue and mark this one as a duplicate.

mohit_aghera’s picture

@alexpott
I think that makes sense.
We can close this one and continue work in #2568045
I think that patch seems not getting applied on 8.9.x branch. I can take care of that if we plan to close this one.

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.

kumar ashutosh’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.