STRIPES-953 Only use DOMPurify's output if it changed something; export sanitize logic #81
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There are two pieces of work here:
only use DOMPurify's output if it changed something
ReactQuill
has a funny behavior- if the inputvalue
attribute is different from what it provided to itsonChange
handler, it will reset its cursor state and put it back at the beginning of the string. The HTML for links output byReactQuill
are generally not offensive, but reversing the attribute order during the value lifecycle resets the cursor repeatedly, even for non-offending templates...DOMPurify
also has a funny behavior, it reverses the attributes of any tag that you feed it.This PR checks for any changes that
DOMPurify
would make to the string, and if there are none, it just uses the string...A tempting alternative approach would be to just call
DOMPurify.sanitize
twice 🤢 and be done with it... it may come to that in the future.I also added
target
andrel
tosanitize
settings to prevent unwanted removals from happening.Export the sanitize logic
This makes it possible to sanitize the value at the module level, before it enters the loop/value lifecycle of the form. This allows us to get in front of any malicious/problematic values that were stored via API request (thus bypassing validation logic in the UI that would have prevented such values) and still uphold the UX of the editor.
The
sanitize
function requires thevalue
(a string) and an optional override config forDOMPurify
. The default configuration is adequate for handling the front-end capabilities of the editor.Future work - flat-out remove the built-in sanitization in
TemplateEditor.js
Refs STRIPES-953