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

[GH-692] Fix formatting inside code block in issue created notification #697

Merged
merged 5 commits into from
Nov 2, 2023

Conversation

raghavaggarwal2308
Copy link
Contributor

@raghavaggarwal2308 raghavaggarwal2308 commented Sep 13, 2023

Summary

Fixed the improper formatting of special characters inside the code block of issue-created channel notifications.

What to test?
  • Special characters are properly displayed inside the code block present in the description of the issue-created notification.

Screenshots

Earlier:
image

Now:
image

Ticket Link

Fixes #692

…reated notification (#35)

* [MI-3405] Fix issue: improper formatting inside code block in issue notification

* [MI-3461] Add a comment to clearify the usage of code
@hanzei hanzei added 2: Dev Review Requires review by a core committer 3: QA Review Requires review by a QA tester labels Sep 26, 2023
@hanzei hanzei added this to the v2.2.0 milestone Sep 26, 2023
Comment on lines 462 to 468
// Replacing the unicodes with the respective special characters to have proper rendering in code block.
result := strings.ReplaceAll(policy.Sanitize(description), "'", "'")
result = strings.ReplaceAll(result, """, "\"")
result = strings.ReplaceAll(result, "&", "&")
result = strings.ReplaceAll(result, ">", ">")
result = strings.ReplaceAll(result, "&lt;", "<")
return strings.TrimSpace(result)
Copy link
Member

@mickmister mickmister Sep 27, 2023

Choose a reason for hiding this comment

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

LGTM, though I wonder if there is a more catch-all holistic approach

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mickmister I explored other packages to sanitize the HTML but the one we are using right now was the best I could find. But this converts the special characters to their unicode value which are not formatted inside a code block, so we are replacing them again here. Please feel free to share your opinions if you have a better approach in mind.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm so it's the policy.Sanitize that's causing the issue. We really just want to use it to remove the <details> elements from the description, and not escape those characters.

As this comment microcosm-cc/bluemonday#39 (comment) mentions on a related issue for the bluemonday package, we can call html.UnescapeString from the result to make those characters go back to what we want them to be

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mickmister Updated

@mickmister mickmister removed the 2: Dev Review Requires review by a core committer label Sep 27, 2023
result = strings.ReplaceAll(result, "&gt;", ">")
result = strings.ReplaceAll(result, "&lt;", "<")
result := policy.Sanitize(description)
html.UnescapeString(result)
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to reassign the value right?

Suggested change
html.UnescapeString(result)
result = html.UnescapeString(result)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mickmister Updated

@codecov-commenter
Copy link

codecov-commenter commented Oct 16, 2023

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (b8ac3c3) 15.78% compared to head (74db52f) 15.78%.
Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #697      +/-   ##
==========================================
- Coverage   15.78%   15.78%   -0.01%     
==========================================
  Files          15       15              
  Lines        5574     5575       +1     
==========================================
  Hits          880      880              
- Misses       4652     4653       +1     
  Partials       42       42              
Files Coverage Δ
server/plugin/webhook.go 0.80% <0.00%> (-0.01%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mickmister
Copy link
Member

Does the bluemonday policy remove things other than the <details> tags? I'm thinking it may remove things like <script> because the tool is meant to sanitize the HTML. We would not want it to remove anything but the <details> tags in our case.

@raghavaggarwal2308
Copy link
Contributor Author

@mickmister Yes, it's removing the HTML tags in the string. For some tags, it's just removing the tags and for some, it's removing the tags along with the text inside.
issue_notification

@mickmister
Copy link
Member

@raghavaggarwal2308 Since the main thing we want to sanitize is <details>, we could just only run the sanitize when there exists a <details> tag in the post body. It's not common to have those in the post body, but when it shows up it's usually spammy which is why we remove them

@raghavaggarwal2308
Copy link
Contributor Author

@raghavaggarwal2308 Since the main thing we want to sanitize is <details>, we could just only run the sanitize when there exists a <details> tag in the post body. It's not common to have those in the post body, but when it shows up it's usually spammy which is why we remove them

@mickmister If we do so and there are other tags along with the details tag, then they will be removed as well. Is that fine?

@mickmister
Copy link
Member

@raghavaggarwal2308 Yes that's fine 👍

@raghavaggarwal2308
Copy link
Contributor Author

@mickmister Added logic to sanitize description conditionally.

@mickmister mickmister merged commit b71f32e into mattermost:master Nov 2, 2023
9 checks passed
raghavaggarwal2308 added a commit to Brightscout/mattermost-plugin-github that referenced this pull request Nov 6, 2023
…notification (mattermost#697)

* [MI-3405] Fix issue: improper formatting inside code block in issue created notification (#35)

* [MI-3405] Fix issue: improper formatting inside code block in issue notification

* [MI-3461] Add a comment to clearify the usage of code

* Updated code to support less than and greater than symbols

* Updated code to use html.UnescapeString

* Review fix

* [MI-3633] Updated logic to sanitize description only when a "<details>" tag is present in it.
@avas27JTG avas27JTG mentioned this pull request Nov 8, 2023
mickmister pushed a commit that referenced this pull request Nov 10, 2023
* [GH-692] Fix formatting inside code block in issue created notification (#697)

* [MI-3405] Fix issue: improper formatting inside code block in issue created notification (#35)

* [MI-3405] Fix issue: improper formatting inside code block in issue notification

* [MI-3461] Add a comment to clearify the usage of code

* Updated code to support less than and greater than symbols

* Updated code to use html.UnescapeString

* Review fix

* [MI-3633] Updated logic to sanitize description only when a "<details>" tag is present in it.

* Bump plugin version to v2.1.7

---------

Co-authored-by: Raghav Aggarwal <raghav.aggarwal@brightscout.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3: QA Review Requires review by a QA tester
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Code in Pull Request code blocks is broken on Mattermost
4 participants