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

Adjust SAFE_URL_PATTERN regex for use with test method of regexes. #33136

Merged
merged 3 commits into from
Feb 19, 2021

Conversation

nikonthethird
Copy link
Contributor

@nikonthethird nikonthethird commented Feb 17, 2021

Recently, the method used to check attributes in the sanitizer has been switched from match to test.

The test method on regexes however behaves different than the match method on strings in the presence of the global modifier (g).
See here for an example where the same input returns true, then false.

This modifier causes issues when the same template is sanitized multiple times, which happens when hovering over tooltips containing an <img src> tag for example.

This PR also adds a unit test for sanitizing the same template twice.

This fixes #33124.

The test method on regexes behaves different than the match method on strings in the presence of the global modifier.
See here for an example where the same input returns true, then false:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
Adds escapes for slashes and a unit test for sanitizing the same template twice.
@nikonthethird nikonthethird requested a review from a team as a code owner February 17, 2021 23:06
@XhmikosR
Copy link
Member

Thank you for catching this and for adding a test case too!

That's a recent regression, and must also apply to v4-dev, so I'll backport it manually after this lands.

I'll take a closer look later today.

@XhmikosR
Copy link
Member

XhmikosR commented Feb 18, 2021

@nikonthethird your patch totally makes sense. But just to be safe, I don't suppose we could have any other issues? Not sure why the regex had the global flag specified in the first place...

BTW I feel like we should report this to eslint-plugin-unicorn. It shouldn't have auto-fixed the regex since it had the global flag.

Edit: just saw you reported it already, thank you!

@nikonthethird
Copy link
Contributor Author

nikonthethird commented Feb 18, 2021

@XhmikosR, yes, the global flag should not have been on the regex from the start, I agree.

The regex was copied from Angular 7 according to the source comment above it, and Angular still has the regex with the global flag on it. But they are using the match method.

Edit: And their source comment mentions the Closure library, they have updated the regex and removed the global flag it seems. Correction: They have not updated the regex, they never had the global flag on it, from the first publicly accessible commit! So it seems someone at the Angular team added it.

As far as I understood the documentation of the test method, it should behave the same as match once the global flag has been removed.

@XhmikosR
Copy link
Member

Yeah, agreed. Thanks for the clarification and especially for the test case.

I'll merge it and backport it to v4-dev in the next days. 4.6.0 isn't affected since I landed this after releasing it.

@XhmikosR XhmikosR changed the title Adjust regex SAFE_URL_PATTERN for use with test method of regexes. Adjust SAFE_URL_PATTERN regex for use with test method of regexes. Feb 19, 2021
@XhmikosR XhmikosR merged commit e8f08d1 into twbs:main Feb 19, 2021
@XhmikosR
Copy link
Member

Ah, it seems that we don't fully cover the sanitizer in the v4-dev branch.

@nikonthethird could you have a look at backporting this manually and adding a test there too?

@nikonthethird
Copy link
Contributor Author

@XhmikosR ok, I'm on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

<img> inside HTML tooltip is only displayed every other hover.
2 participants