Problem/Motivation

When a field containing certain HTML elements like video or iframe is passed through the a token rewrite, then the iframe element is stripped out breaking the output.

Steps to reproduce

1. Create a field which outputs a video or iframe tag. This can be a simple body field.
2. Select this field on a view page.
3. Rewrite the ouput of this field with it's twig placeholder.
4. The video or iframe element is stripped out.

Proposed resolution

Add the video and iframe elements to the XSS whitelist in views. As well as refactor all the existing XSS:filterAdmin() calls into a single function so that contrib modules may override the behaviour as necessary.

The argument now is what happens if the content has rare tags like audio or embed that it wants to keep in rewrites.

We can also revisit how the sanitization should work. Whether it should:
1. Sanitize the individual tokens first rather than the final output.
2. A UI option to opt out of XSS sanitization for the field.

Remaining tasks

N/A

User interface changes

N/A

API changes

Introduce a new optional $additional_tags argument to \Drupal\Component\Utility\Xss::filterAdmin() so that specific modules can choose which extra elements they want to provide support for.

Data model changes

N/A

Release notes snippet

TBD

Issue fork drupal-2942327

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

bewilled created an issue. See original summary.

cilefen’s picture

Issue tags: -views, -video, -template

Any way we can get a look at the custom code so we can reproduce?

bewilled’s picture

Looked it up and there's a built in video formatter under core/modules/file/src/Plugin/Field/FieldFormatter/FileVideoFormatter.php. If you use that formatter with a video field and then rewrite that field, then you can reproduce.

Please be sure to check "Use field template"

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.

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.

someshver’s picture

StatusFileSize
new2.08 KB

I have created a patch to support video tag in the global custom text field in views.

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

jitumiet’s picture

Adding patch file for:
Replacement Pattern for Video (Rendered Entity) is not woking in Views "Global: Custom text" field.

In any content type create Entity Reference for Media and select Video.
Then create node inside content type and upload any video.
Go to Views create views and add video field with formatter "Rendered Entity". Exclude field from display.
Add "Global: Custom text" field and use replacement pattern for Video field. Video will not render as "Global: Custom text" strips video and source tag.

jitumiet’s picture

Status: Active » Needs review

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.

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.

smustgrave’s picture

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

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

Did not test on D10
But this will need a test case to show the issue.

jitumiet’s picture

Version: 9.5.x-dev » 11.x-dev
Status: Needs work » Needs review
StatusFileSize
new2.12 KB

Applying patch for Views Global Text area field to allow extra HTML tags. As video, source and iframe tag is not rendering. Due to which Media embedded video and remote-video not rendering in Views Global Text area field.

Status: Needs review » Needs work
jitumiet’s picture

Status: Needs work » Needs review
StatusFileSize
new1.68 KB
smustgrave’s picture

Status: Needs review » Needs work

Was previously tagged for tests which will see need to happen. Thanks!

codebymikey changed the visibility of the branch 2942327-views-strips-out to hidden.

codebymikey’s picture

Title: Views strips out video tags in custom field formatter » Views strips out video and iframe tags when replacing tokens
Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new3.96 KB

Made the change so that it only affects the views module rather than globally allow iframes in the admin.

Some further thought is probably necessary for whether this exposes any new potential security vectors.

e.g. if the site builder is using a user supplied token like a query string as a filter. But I'd assume as long as the value isn't rendered as {{ token|raw }}, it should be properly escaped or better yet, they should be using an appropriate escape filter in the first place like {{ token|escape('uri') }}.

edit: This is a non-issue, as the token variables are escaped by default unless specifically rendered using the raw filter.

codebymikey’s picture

StatusFileSize
new3.95 KB

Reuploaded an updated static patch of the latest MR.

smustgrave’s picture

Issue summary appears to be missing standard templates so tagging for such

have not reviewed

smustgrave’s picture

Status: Needs review » Needs work

Wrong status sorry

codebymikey’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs issue summary update

Updated the issue summary

manibharathi ezhimalai ravi’s picture

StatusFileSize
new163.12 KB
new138.54 KB

Hi,
Able to reproduce the issue in the Drupal11.x. After creating the view with specific html element data is not coming for the iframe tag.

MR !4595 has been verified in the Drupal 11.x.

Before Patch:
before_patch

After Patch:
After_patch

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record

Thanks @codebymikey summary looks good!

Left some small comments on the MR.

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

oily’s picture

Resolved PHPSTAN recommendations in the MR. Tests are passing. Looks like a change record can close this.

codebymikey’s picture

The more I think back on this, the more I think the current approach wouldn't actually solve the long-term issue (we'll forever be needing to add new elements as necessary to fit the site's use-case).

I think we can keep the API changes to \Drupal\Component\Utility\Xss::filterAdmin() as it's helpful for modules.

Options to consider:
1. Provide a UI option to skip XSS filtering entirely on the specific field rewrite (and let the user sanitize their data themselves using twig filters/functions).
2. Provide a UI option to allow the user to specify the additional HTML elements which are exempt from XSS for view fields (not sure if this should be a global setting, or on an individual field-level basis - seems a bit counter intuitive if we already have "Strip HTML tags" which does the inverse).
3. Remove the XSS filtering from the fields and leave it to the user to sanitize, since the same content is rendered as is when rewrites aren't enabled.

I'm still trying to understand why Views automatically does the XSS filtering on behalf of the user in the first place since the source data should ideally already be sanitized/trusted by Drupal or Twig, and if it is untrusted data, then I assume that's what the "Strip HTML tags" option is for (and you provide a whitelist of allowed elements).

More thoughts on this from a core maintainer are more than welcome.

oily’s picture

@codebymikey I think your plan at #36 should be a new child issue. It seems a feature request whereas this issue is a bug. The current MR fixes the bug.

codebymikey’s picture

@oily, I get your point, I think it just depends whether the XSS filtering is necessary in the first place given how the rendering supposedly works and views being configured by the site builders with admin permissions rather than arbitrary users.

If XSS filtering is not necessary, then removing it solves the actual issue rather than fixing it for this specific video/iframe situation - since the real issue is that the same supposedly "dangerous" HTML elements are rendered on the page when rewrites aren't turned on.

However if the core team determines that filtering is still necessary, then we can make a follow-up issue with direction for how they propose we go about opting out of it. But I think it's worth raising those questions here before this lands.

oily’s picture

@codebymikey That sounds good but a CR is required all the same then can change to 'Needs review' and get it reviewed. The CR should reflect what is in the MR. Could update the description to state what has been actually done in the MR as distinguished from several possible approaches.

codebymikey’s picture

Yeah, that's completely fine, feel free to update the issue summary/CR and issue tags as necessary.

oily changed the visibility of the branch 2942327-views-global-text-allow-extra-html-tags to hidden.

oily changed the visibility of the branch 2942327-views-global-text-allow-extra-html-tags to active.

oily’s picture

The pipeline is all green and test coverage works: a failing test is confirmed.

oily’s picture

Rebased, pipeline successful.

Changing to 'Needs review'.

Also needs change record.

oily’s picture

Status: Needs work » Needs review
oily’s picture

Status: Needs review » Needs work

Back to Needs review. MR !4595.

Change record added.

oily’s picture

Status: Needs work » Needs review
oily’s picture

Issue tags: -Needs change record
catch’s picture

Status: Needs review » Needs work

If we want to change the API of Xss::filterAdmin(), that would need to happen in its own issue, this one could be postponed on it. I'm not sure that we should do that though at all.

#38 is a good question, given Twig auto-escapes and the views UI requires a 'restrict access' permission, is this actually necessary?

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.