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

LPD-33910 Add sanitization for codeMirror editor #212

Merged
merged 1 commit into from
Aug 29, 2024

Conversation

fortunatomaldonado
Copy link
Member

https://liferay.atlassian.net/browse/LPD-33910
https://liferay.atlassian.net/browse/LPP-55225

CodeMirror editor was not being sanitized and so adding sensitization prevents any unexpected behavior.
I tried several examples and was not able to get any unexpected behavior.

Please let me know if there are any questions or comments about this.
Thank you!

Comment on lines +8 to +14
var ALERT_REGEX = /alert\((.*?)\)/;
var INNER_HTML_REGEX = /innerHTML\s*=\s*.*?/;
var PHP_CODE_REGEX = /<\?[\s\S]*?\?>/g;
var ASP_CODE_REGEX = /<%[\s\S]*?%>/g;
var ASP_NET_CODE_REGEX = /(<asp:[^]+>[\s|\S]*?<\/asp:[^]+>)|(<asp:[^]+\/>)/gi;
var HTML_TAG_WITH_ON_ATTRIBUTE_REGEX = /<[^>]+?(\s+\bon\w+=(?:'[^']*'|"[^"]*"|[^'"\s>]+))*\s*\/?>/gi;
var ON_ATTRIBUTE_REGEX = /(\s+\bon\w+=(?:'[^']*'|"[^"]*"|[^'"\s>]+))/gi;
Copy link
Member

Choose a reason for hiding this comment

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

Where did you get these from? Do we already do something similar in liferay-portal somewhere?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hello @bryceosterhaus

I took this from here: https://liferay.atlassian.net/browse/LPD-2161.
This is done for Rich Text Fields, and I asked the team to see if there component could cover this particular issue, but they came back saying it couldn't.
https://liferay.atlassian.net/browse/LPP-55225?focusedCommentId=2721890

Let me know if there are any further questions or comments about this.

Copy link
Member

Choose a reason for hiding this comment

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

Okay good! Just wanted to make sure these regexes were at least being used in some other place so that we are testing so many new ones at once.

Copy link
Member

@bryceosterhaus bryceosterhaus left a comment

Choose a reason for hiding this comment

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

LGTM!

@fortunatomaldonado
Copy link
Member Author

Thank you @bryceosterhaus!
@markocikos, can we get this pull and #213 merged?
Also if we can get the version upgraded, I wasn't able to do it before.

Thank you!

Copy link
Member

@markocikos markocikos left a comment

Choose a reason for hiding this comment

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

@fortunatomaldonado The sanitation looks good, but a question inline on code removal in PR.

Comment on lines +196 to +207
_sanitizeHTML: function (html) {
var sanitizedHtml = html
.replace(HTML_TAG_WITH_ON_ATTRIBUTE_REGEX, function (match) {
return match.replace(ON_ATTRIBUTE_REGEX, '');
})
.replace(ALERT_REGEX, '')
.replace(INNER_HTML_REGEX, '')
.replace(PHP_CODE_REGEX, '')
.replace(ASP_CODE_REGEX, '')
.replace(ASP_NET_CODE_REGEX, '');

return sanitizedHtml;
Copy link
Member

Choose a reason for hiding this comment

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

Are we intentionally removing _handleCodeMirrorChange? It seems to update preview iframe. Won't this change result in preview never updating?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hello @markocikos,
Are you referring to this: 799b5af#diff-8570f7b77fa9c9666a3f866cf502ba43abeb941c958aaf14683e3177eb60ecfeL186-L197

I found there were two _handleCodeMirrorChange methods that were exactly the same in the file. So I decided to remove one of them.
Let me know if I am misunderstanding.

Copy link
Member

Choose a reason for hiding this comment

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

Oh wow, that's a nice cleanup then 😄

@markocikos
Copy link
Member

markocikos commented Aug 29, 2024

@fortunatomaldonado you don't need to generate artifacts on each PR, that's what caused the conflicts after I merged #213. We only need to generate them once before release. Historically there was rarely more then one PR per release, so it was custom to add them immediately in PR. But it's not necessary, especially if you expect multiple fixes soon.

@fortunatomaldonado
Copy link
Member Author

@markocikos, oh I see. I'll remove the artifacts here and resend.
Thank you!

Copy link
Member

@markocikos markocikos left a comment

Choose a reason for hiding this comment

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

@markocikos markocikos merged commit 1fa3717 into liferay:master Aug 29, 2024
1 check passed
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.

3 participants