Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): traverse and sanitize content of unsafe elements #28804

Closed

Conversation

gkalpak
Copy link
Member

@gkalpak gkalpak commented Feb 18, 2019

PR Checklist

PR Type

  • Bugfix

What is the current behavior?

In the past, the sanitizer would remove unsafe elements, but still traverse and sanitize (and potentially preserve) their content. This was problematic in the case of <style></style> tags, whose content would be converted to HTML text nodes.

In order to fix this, the sanitizer's behavior was changed in #25879 to ignore the content of all unsafe elements. While this fixed the problem with <style </style> tags, it unnecessarily removed the contents for any unsafe element. This was an unneeded breaking change.

Issue Number: #28427

What is the new behavior?

This commit partially restores the old sanitizer behavior (namely traversing content of unsafe elements), but introduces a list of elements whose content should not be traversed if the elements themselves are considered unsafe. Currently, this list contains style and script.

Does this PR introduce a breaking change?

  • Yes
  • No, but...

...this PR does introduce a change in behavior, but only to fix a previous change in behavior that was a regression.

Other information

Related to #25879 and #26007.
Fixes #28427.

@gkalpak gkalpak requested review from a team as code owners February 18, 2019 16:31
@gkalpak gkalpak added type: bug/fix action: review The PR is still awaiting reviews from at least one requested reviewer regression Indicates than the issue relates to something that worked in a previous version target: patch This PR is targeted for the next patch release risk: medium effort1: hours area: core Issues related to the framework runtime labels Feb 18, 2019
@ngbot ngbot bot added this to the Backlog milestone Feb 18, 2019
@gkalpak
Copy link
Member Author

gkalpak commented Feb 18, 2019

I marked this as PR target: master & patch, because #25879 was merged in 7.x as well, but I am not sure that is the right target.

@gkalpak gkalpak force-pushed the fix-core-sanitizer-regression branch from d52e3fd to 37b6644 Compare February 18, 2019 16:35
// Typically, `<invalid>Some content</invalid>` would traverse (and in this case preserve)
// `Some content`, but strip `invalid-element` opening/closing tags. For some elements, though, we
// don't want to preserve the content, if the elements themselves are going to be removed.
const SKIP_TRAVERSING_CONTENT_IF_INVALID_ELEMENTS = tagSet('script,style');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should template be included here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷‍♂️
Probably, I guess 😁 Added.

@gkalpak gkalpak force-pushed the fix-core-sanitizer-regression branch from 37b6644 to d787173 Compare February 18, 2019 17:03
// Typically, `<invalid>Some content</invalid>` would traverse (and in this case preserve)
// `Some content`, but strip `invalid-element` opening/closing tags. For some elements, though, we
// don't want to preserve the content, if the elements themselves are going to be removed.
const SKIP_TRAVERSING_CONTENT_IF_INVALID_ELEMENTS = tagSet('script,style,template');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you didn't do tagSet, but why not pass an array of elements directly instead of splitting. Surely it would reduce overhead and maybe even help with minification

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to remain consistent with the rest of the file 🤔

@gkalpak gkalpak force-pushed the fix-core-sanitizer-regression branch 2 times, most recently from c3635bf to b66a95e Compare February 18, 2019 17:46
In the past, the sanitizer would remove unsafe elements, but still
traverse and sanitize (and potentially preserve) their content. This was
problematic in the case of `<style></style>` tags, whose content would
be converted to HTML text nodes.

In order to fix this, the sanitizer's behavior was changed in angular#25879 to
ignore the content of _all_ unsafe elements. While this fixed the
problem with `<style></style>` tags, it unnecessarily removed the
contents for _any_ unsafe element. This was an unneeded breaking change.

This commit partially restores the old sanitizer behavior (namely
traversing content of unsafe elements), but introduces a list of
elements whose content should not be traversed if the elements
themselves are considered unsafe. Currently, this list contains `style`,
`script` and `template`.

Related to angular#25879 and angular#26007.

Fixes angular#28427
@mhevery mhevery added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Feb 23, 2019
@mhevery
Copy link
Contributor

mhevery commented Feb 23, 2019

benlesh pushed a commit that referenced this pull request Feb 26, 2019
In the past, the sanitizer would remove unsafe elements, but still
traverse and sanitize (and potentially preserve) their content. This was
problematic in the case of `<style></style>` tags, whose content would
be converted to HTML text nodes.

In order to fix this, the sanitizer's behavior was changed in #25879 to
ignore the content of _all_ unsafe elements. While this fixed the
problem with `<style></style>` tags, it unnecessarily removed the
contents for _any_ unsafe element. This was an unneeded breaking change.

This commit partially restores the old sanitizer behavior (namely
traversing content of unsafe elements), but introduces a list of
elements whose content should not be traversed if the elements
themselves are considered unsafe. Currently, this list contains `style`,
`script` and `template`.

Related to #25879 and #26007.

Fixes #28427

PR Close #28804
@benlesh benlesh closed this in 262ba67 Feb 26, 2019
@gkalpak gkalpak deleted the fix-core-sanitizer-regression branch February 27, 2019 17:06
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime cla: yes effort1: hours regression Indicates than the issue relates to something that worked in a previous version risk: medium target: patch This PR is targeted for the next patch release type: bug/fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpected sanitizer breaking change in 7.1.0-beta.1
5 participants