Problem/Motivation

The problem is, when the "Summary or trimmed" or "Trimmed" field formatters do their thing, they first apply the text filter and then text_summary() will either return the same value (if no trimming is necessary) which is already marked as "safe" or will return a trimmed value, which is just a plain string (and so not marked as safe).

Then in \Drupal\Core\Render\Renderer->ensureMarkupIsSafe(), if it's a plain string (and so not marked as safe) it'll additionally run Xss::filter() on the output, which will remove stuff, even if it's allowed by the text format!

Steps to reproduce

  1. Install Drupal from 11.x HEAD with the Standard profile
  2. Select the Full HTML format when creating the following Article nodes as it doesn't filter by "Limit allowed HTML". The use case are dangerous things like iframes or script tags or inline styles. Not advisable, but there is a use case for it for admin users.
  3. Create an "Article" node with <div style="color: red">red text</div> followed by less than 600 characters of text
  4. Create an "Article" node with <div style="color: red">red text</div> followed by more than 600 characters of text
  5. On the node view page for both nodes, the text at the top will be red -- this is expected, because the text filter allows it
  6. Go to the front page - you'll see that one node (the one with less than 600 characters) shows the text in red, but the other (the one with greater than 600 characters) does not

Proposed resolution

If filtered markup is passed to the `#pre_render` callback, that summarizes the text, return filtered markup instead of a plain string.

Remaining tasks

Review

User interface changes

NA

Introduced terminology

NA

API changes

NA

Data model changes

NA

Release notes snippet

NA

Issue fork drupal-2922569

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

dsnopek created an issue. See original summary.

dsnopek’s picture

Issue summary: View changes
dsnopek’s picture

Issue summary: View changes

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

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should 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.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should 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.

dsnopek’s picture

Status: Active » Needs review
StatusFileSize
new1.25 KB

Here's a patch that works in limited manual testing!

andkar’s picture

Thanks, this works for me.
Drupal version 8.6.13

joe_carvajal’s picture

StatusFileSize
new1.03 KB

Reroll for 8.8.

joe_carvajal’s picture

StatusFileSize
new1.29 KB

Sorry, wrong patch, This is a correct reroll for 8.8 version.

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

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

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

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should 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.

smustgrave’s picture

Status: Needs review » Needs work

This almost sounds like a bug, which would need a test case.

This also should be retested in D10 to see if still an issue and we can go from there.

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

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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.

shmy’s picture

Issue summary: View changes

I've reproduced the issue with current 11.x HEAD and updated the steps in issue summary.

shmy’s picture

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

shmy’s picture

StatusFileSize
new1.19 KB

I've extended the patch (interdiff_9_19) by a safety check to ensure only markup, that is already passed as filtered, is back-casted from string. I'm not sure if the taken approach is sufficient.

shmy’s picture

As the ensureMarkupIsSafe renderer method the treats MarkupInterface as safe and TextTrimmedFormatter::preRenderSummary is a public static method and therefore may be called within a different context by contrib/custom modules (e.g. a render element that doesn't preserve the #pre_render callback preRenderText) iv'e tried to find a more generic implementation but as MarkupInterface doesn't dictate the `::create` method i couldn't find a practical approach.

As test coverage is needed anyway i hope to have some time in the next days and extend an existing ones or add a new one if necessary.

wim leers’s picture

Minor remarks, looking good :)

shmy’s picture

Issue summary: View changes
Status: Needs work » Needs review

As the implemented solution has been approved during review, i belief the proposed solution and remaining task can be updated in the issue summary. Back to review.

smustgrave’s picture

Going to find out if this falls under BC policy as this could change the display on existing sites which might not be desired.

smustgrave credited catch.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Brought this up in slack #needs-review-queue-initative https://drupal.slack.com/archives/C04CHUX484T/p1708719216555289

I asked if this would be a D11 fix only because of the backwards compatibility impact and @catch replied "It wouldn't make it 11.x only but we might have to put it behind a settings.php switch or feature flag"

So maybe a field formatter setting could cover it? Know smart trim has something like that.

shmy’s picture

Thanks for taking care of BC.

I think the required change depends on issue classification.
In my eyes this is a bug fix, not a feature request, because as a user (i.e. site builder) i don't expect that my hard-coded markup is filtered out when summarized.

Since the activity (and probably demand) for a fix seems not very high why not make it 11.x only?

dsnopek’s picture

StatusFileSize
new1.42 KB

Here's a version of the latest patch for Drupal 10.3 without the tests, for folks who want to include this in their composer.json.

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.

smustgrave’s picture

So we definitely missed the D11 boat but maybe this can make D12?

With regards to feature flags, we did something similar with layout builder using a module as a flag.

smustgrave’s picture

Issue summary: View changes

smustgrave’s picture

Status: Needs work » Needs review

Fixed up the MR for main. I broke out the text_with_summary to it's own test so it can be easily removed when we deprecate text_with_summary

shmy’s picture

Thanks for updating the MR.

Regarding the feature flag, i'm assuming thats opt-in? If so i'm worried that the fix will be just a dead end. I can't remember when / if i ever looked up if there is something behind a feature flag and instead chose contrib / custom.
I still thing that is a bug fix, but may change the markup in an unexpected way and therefore opt-in is just the most practical solution.

The field formatter settings would make it at least a reachable option, so i would favor that appraoch.

smustgrave’s picture

This actually needs a summary update but it’s not an opt in feature as it’s fixing a bug.

shmy’s picture

As there is no API for feature flags yet (#3423352) the flag would need to be implemented as a text submodule, like done in layout builder in #3432874?

smustgrave’s picture

That was just a temporary stop gap. The module is being marked obsolete and deleted

shmy’s picture

I see, misread that. Then its just a field formatter setting.

shmy’s picture

I've reviewed the previous commits on the MR, these do look okay.

I'm not sure if that's how its supposed to be, but the behavior of the text_summary() function is not as i would expect. I.e. it doesn't subtract the HTML tag length when determining the output length. On TextFormatterTest::L108 and TextFormatterTest::L149 i would expect that the output is 17 chars (the length of the iframe tag) longer.
A more lengthy tag or heavily nested ones, in combination with a short trim length, may yield an empty output string or just expose the HTML markup without the actual content.

smustgrave’s picture

Status: Needs review » Needs work

Not sure I agree with the field formatter setting but that now changes trajectory of this ticket

smustgrave’s picture

shmy’s picture

So do we continue or replace it with a module setting instead?

A correction on the text_summary() function note above: All of the lines should be longer, not only the most recently added ones.
Found Strip tags from formatted text before trimming, which already had an MR. Added as related.

smustgrave’s picture

Status: Needs work » Closed (duplicate)

Nice find, I do think we are achieving the same thing as the issue mentioned. Lets continue over there. Am giving credit to this issue.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

dsnopek’s picture

StatusFileSize
new1.15 KB

Here's patch rerolled for D11 without tests