Problem/Motivation

I have a view with aggregation enabled. When I rewrite the output of a field and add HTML markup, it is escaped (i.e. the markup is shown). If I turn aggregation off, the field is rendered as expected.

This only happens if the Aggregation Type is NOT set to group results together.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ExTexan created an issue. See original summary.

cilefen’s picture

Version: 8.0.5 » 8.1.x-dev
Category: Task » Bug report
joelpittet’s picture

Issue summary: View changes
FileSize
42.46 KB
17.31 KB

Thanks for creating this issue @ExTexan. I reproduced the error in 8.2.x:

It seems to only happen if I change the aggregation settings on the field as well (in my case I added SUM or AVG)

Included the export of the config in the yaml file attached.

joelpittet’s picture

Here's a fix and some tests. The tests only patch should fail once or twice and the patch will resolve this.

The last submitted patch, 4: views_html_is_escaped-2714045-4-tests-only.patch, failed testing.

dawehner’s picture

Issue tags: +Novice
+++ b/core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php
@@ -168,6 +168,55 @@ public function testRewrite() {
+    $this->assertSubString($output, '<p>1</p>');
...
+    $this->assertSubString($output, '<p>1</p>');

One could use $this->assertSubString() here.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

suzymasri’s picture

I'm having a look at this @DrupalConDublin.

anouschka42’s picture

I am having at look at this @DrupalCon Dublin

slowflyer’s picture

Issue tags: +Dublin2016
slowflyer’s picture

I did a manual review.
I've been able to reproduce the error.
The patch applied fixed the error.

regards,
slowflyer

anouschka42’s picture

@slowflyer hey how did you reproduce this, we are currently trying to reproduce this error on 8.1 8.2 and 8.3 and can't.

alexpott’s picture

Issue tags: +Triaged core major

Discussed with @timplunkett, @dawehner, @cilefen and @xjm. We agreed that this is a major issue because the views render pipeline should not cause escaping and is core views functionality.

anouschka42’s picture

Issue summary: View changes

ok so I have managed to reproduce this, apologies.. I had not set the aggregation type to SUM. If i do, I can reproduce this. So I am now testing the patch again to see if it fixes the issue.

anouschka42’s picture

Issue summary: View changes

I have managed to reproduce the issue now and have tested the patch views_html_is_escaped-2714045-4.patch against 8.2 and it seems to fix the issue for all Aggregation type settings.

anouschka42’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs manual testing

This looks good to me

anouschka42’s picture

Issue summary: View changes
alexpott’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs subsystem maintainer review

@anouschka42 thanks for reviewing this issue. It'd be great to see before and after screenshots of your manual testing. Also given the security implications of:

+++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php
@@ -1222,6 +1222,9 @@ public function renderText($alter) {
+      $value_is_safe = TRUE;

I think we need a views subsystem maintainer to review this patch before committing.

lomasr’s picture

FileSize
100.55 KB
99.58 KB

I created a simple view with content type name & count of Nid. I enabled aggregation in order to get total number of nid for a content type. The patch at #4 worked for me . Please see before & after screenshots.

anouschka42’s picture

FileSize
102.25 KB
105.94 KB

Manually tested against 8.2. I adjusted the content view to rewrite the title of an article with a h1 tag and enabled aggregation.

See screenshots. without patch

with patch #4 applied

So patch #4 worked for me. Thanks :)

Lendude’s picture

Well lets at least add a test to make sure it is safe. So something like this?

dawehner’s picture

@Lendude
That's a great expansion

+++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php
@@ -1222,6 +1222,9 @@ public function renderText($alter) {
       $value = $this->renderAltered($alter, $tokens);
+      // Altered text is run through Xss::filterAdmin.
+      // @see \Drupal\views\Plugin\views\PluginBase::viewsTokenReplace().
+      $value_is_safe = TRUE;

One thing I am wondering: Should renderAltered mark it as safe somehow? Note: \Drupal\views\Plugin\views\field\FieldPluginBase::tokenizeValue has another call to it. Is there a similar kind of bug in there as well?

Lendude’s picture

@dawehner so maybe something like this? Alters the return value of renderAltered so bit of an API change.... not sure if that's worth it.

Status: Needs review » Needs work

The last submitted patch, 23: aggr_html_is_escaped-2714045-23.patch, failed testing.

Lendude’s picture

Status: Needs work » Needs review
Issue tags: -Novice
FileSize
3.73 KB

Yeah the impact of changing what renderAltered does is way bigger then this issue, so lets not go there. Reuploading #21, I think that is sufficient for this.

@dawehner the call in \Drupal\views\Plugin\views\field\FieldPluginBase::tokenizeValue is:
$value = strip_tags($this->renderAltered($fake_item, $tokens));
pretty sure we don't have to worry about any HTML tags after that :)

joelpittet’s picture

I'd RTBC this if it wasn't so close to my original patch. Need someone from the views subsystem to review this I guess. Thank you @Lendude for improving the tests!

dawehner’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs subsystem maintainer review

I like the test coverage applied here.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 7c43cce to 8.3.x and c30e795 to 8.2.x. Thanks!

+++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php
@@ -1222,6 +1222,9 @@ public function renderText($alter) {
       $value = $this->renderAltered($alter, $tokens);

I've checked the output of $this->renderAltered() and I can't see anyway that the output is not run through XSS filtering. Since the input text is coming from the admin interface I think this is an appropriate tactic for making safe and 'administer views' is restricted access. I've updated the comment to reflect this.

diff --git a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php
index e876ae9..c8e4d7a 100644
--- a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php
+++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php
@@ -1222,8 +1222,11 @@ public function renderText($alter) {
     if (!empty($alter['alter_text']) && $alter['text'] !== '') {
       $tokens = $this->getRenderTokens($alter);
       $value = $this->renderAltered($alter, $tokens);
-      // Altered text is run through Xss::filterAdmin.
-      // @see \Drupal\views\Plugin\views\PluginBase::viewsTokenReplace().
+      // $alter['text'] is entered through the views admin UI and will be safe
+      // because the output of $this->renderAltered() is run through
+      // Xss::filterAdmin().
+      // @see \Drupal\views\Plugin\views\PluginBase::viewsTokenReplace()
+      // @see \Drupal\Component\Utility\Xss::filterAdmin()
       $value_is_safe = TRUE;
     }
 
diff --git a/core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php b/core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php
index f2e9156..be8a4f1 100644
--- a/core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php
+++ b/core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php
@@ -168,9 +168,9 @@ public function testRewrite() {
     $this->assertSubString($output, $random_text);
   }
 
-/**
- * Tests rewriting of the output with HTML.
- */
+  /**
+   * Tests rewriting of the output with HTML.
+   */
   public function testRewriteHtmlWithTokens() {
     /** @var \Drupal\Core\Render\RendererInterface $renderer */
     $renderer = \Drupal::service('renderer');

Fixed on commit.

  • alexpott committed 7c43cce on 8.3.x
    Issue #2714045 by Lendude, joelpittet, anouschka42, lomasr, dawehner:...

  • alexpott committed c30e795 on 8.2.x
    Issue #2714045 by Lendude, joelpittet, anouschka42, lomasr, dawehner:...

Status: Fixed » Closed (fixed)

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