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: Unescape HTML entities for alternate DOMPurify check #2630

Merged
merged 4 commits into from
Jan 5, 2024

Conversation

DafyddLlyr
Copy link
Contributor

@DafyddLlyr DafyddLlyr commented Jan 3, 2024

What's the problem?

Originally picked up here - https://opensystemslab.slack.com/archives/C01E3AC0C03/p1703175603781519 (OSL Slack)

A permission error is thrown when attempting to copy some flows from the "Templates" team, despite the users in question having the requisite permission level.

What's the cause?

The logged error is misleading, it's actually an input validation error that's being thrown - the try/catch block just didn't account for this.

What's happening here is that the simple length === length check for DOMPurify is not accounting for HTML entities (see screenshot below).

image

There isn't a simple alternative way of checking that DOMPurify has modified content unfortunately. We can't check string equality directly as the order of HTML attributes in sanitised content can differ from the original (and there's not a method of preserving this). Please see comment from DOMPurify readme below on checking for removed attributes directly -

After sanitizing your markup, you can also have a look at the property DOMPurify.removed and find out, what elements and attributes were thrown out. Please do not use this property for making any security critical decisions. This is just a little helper for curious minds.

Source: https://github.com/cure53/DOMPurify?tab=readme-ov-file#okay-makes-sense-lets-move-on

What's the solution?

Check length, and then unescape known HTML entities which are converted, and re-check. I'll be honest this doesn't feel ideal but it's the best / simplest solution I've reached.

I'm throwing noisy errors here so that we can more easily spot any more issues going forward.

Copy link

github-actions bot commented Jan 3, 2024

Removed vultr server and associated DNS entries

@DafyddLlyr DafyddLlyr force-pushed the dp/dompurify-character-conversion branch from 07715d5 to 18d0f05 Compare January 4, 2024 14:07
@DafyddLlyr DafyddLlyr changed the title fix: Unescape quotations before DOMPurify check fix: Unescape HTML entities for alternate DOMPurify check Jan 4, 2024
const logUncleanHTMLError = (input: string, cleanHTML: string) => {
const userId = userContext.getStore()?.user.sub;

console.error({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this should be fine, I'll check on staging. We might need to use the logger.notify() method Ben wrote if this doesn't get caught by Airbrake.

});
};

const unescapeHTML = (input: string): string =>
Copy link
Contributor Author

@DafyddLlyr DafyddLlyr Jan 4, 2024

Choose a reason for hiding this comment

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

I tested a number of other HTML entities and these were the only one which were getting caught.

Tiptap is converting the quotation marks, and DOMPurify appears to be converting all HTML entities apart from  .

@DafyddLlyr DafyddLlyr requested a review from a team January 4, 2024 16:48
@DafyddLlyr DafyddLlyr marked this pull request as ready for review January 4, 2024 16:49
Copy link
Member

@jessicamcinchak jessicamcinchak left a comment

Choose a reason for hiding this comment

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

Thanks for getting to the bottom of this one!

@DafyddLlyr DafyddLlyr merged commit 59d016a into main Jan 5, 2024
12 checks passed
@DafyddLlyr DafyddLlyr deleted the dp/dompurify-character-conversion branch January 5, 2024 09:41
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.

2 participants