Skip to content

Add support for dropElements#37

Merged
0xedward merged 3 commits into
mozilla:mainfrom
0xedward:support-dropElements
Aug 22, 2021
Merged

Add support for dropElements#37
0xedward merged 3 commits into
mozilla:mainfrom
0xedward:support-dropElements

Conversation

@0xedward

@0xedward 0xedward commented Aug 15, 2021

Copy link
Copy Markdown
Collaborator

Resolves #30

Use DOMPurify FORBID_CONTENTS configuration to emulate the behavior we want from dropElements

Example:

const sample = 'Some text <b><i>with</i></b> <blink><i>tags</i></blink>.'
let testSanitizer = new Sanitizer({blockElements: ['blink'], dropElements: ['b']})

testSanitizer.sanitizeFor('p', 'Some text <b><i>with</i></b> <blink><i>tags</i></blink>.') // returns "<p>​Some text <i>​tags​</i>​.</p>"​

DOMPurify.sanitize(sample, { FORBID_CONTENTS: [ "b" ], FORBID_TAGS: ['b', 'blink']}); // returns "<p>Some text  <i>tags</i>.</p>"

Same example with DOMPurify 2.3.1

There might be some subtle bugs when the config contains elements in both dropElements and blockElements (see cure53/DOMPurify#556 (comment)). I'll work on setting up the test infra and come up with sample inputs to test this

@0xedward
0xedward requested a review from mozfreddyb August 15, 2021 07:08
@0xedward
0xedward marked this pull request as ready for review August 15, 2021 07:09
Comment thread src/sanitizer.js
FORBID_ATTR: dropAttrs,
};
// https://github.com/cure53/DOMPurify/issues/556
const isdropElementsSet =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm not sure but maybe deduce this boolean flag easier from within the default-assignment logic above, don't you think?

@0xedward 0xedward Aug 17, 2021

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure but maybe deduce this boolean flag easier from within the default-assignment logic above, don't you think?

I'm probably missing something obvious here. But do you have an example of how to merge the initialization of the booleans with the assignments above?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It seems we are checking if dropElements was set twice. Once in the dropElements = config.dropElements || [] line and then here. I'm wondering if those could be merged, to have the same semantic thing happen just once.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Ah, I think I'm following! I'll try tackling this in #44, since that refactor will touch these lines as well (e.g. DEFAULT_DROP_ELEMENTS here will become a set)

Comment thread src/sanitizer.js Outdated

@mozfreddyb mozfreddyb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think this looks right, but at this point I really want tests 😂

Comment thread src/sanitizer.js
FORBID_ATTR: dropAttrs,
};
// https://github.com/cure53/DOMPurify/issues/556
const isdropElementsSet =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It seems we are checking if dropElements was set twice. Once in the dropElements = config.dropElements || [] line and then here. I'm wondering if those could be merged, to have the same semantic thing happen just once.

@0xedward
0xedward force-pushed the support-dropElements branch from e471942 to 0c8f9d8 Compare August 22, 2021 04:30
@0xedward
0xedward force-pushed the support-dropElements branch from 0c8f9d8 to 38e1959 Compare August 22, 2021 04:39
Resolves mozilla#30

Use DOMPurify `FORBID_CONTENTS` configuration to emulate the behavior we want from `dropElements`

Example:
```javascript
const sample = 'Some text <b><i>with</i></b> <blink><i>tags</i></blink>.'
let testSanitizer = new Sanitizer({blockElements: ['blink'], dropElements: ['b']})

testSanitizer.sanitizeFor('p', 'Some text <b><i>with</i></b> <blink><i>tags</i></blink>.') // returns "<p>​Some text <i>​tags​</i>​.</p>"​

DOMPurify.sanitize(sample, { FORBID_CONTENTS: [ "b" ], FORBID_TAGS: ['b', 'blink']}); // returns "<p>Some text  <i>tags</i>.</p>"

```

There might be some subtle bugs when the config contains elements in both `dropElements` and `blockElements` (see cure53/DOMPurify#556 (comment)). I'll work on setting up the test infra and come up with sample inputs to test this
@0xedward
0xedward force-pushed the support-dropElements branch from 38e1959 to 16a7d36 Compare August 22, 2021 04:56
@0xedward
0xedward merged commit c06bf8d into mozilla:main Aug 22, 2021
@0xedward
0xedward deleted the support-dropElements branch August 22, 2021 04:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for dropElements in _transformConfig

2 participants