Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Fix escaping markdown by rendering plaintext #622

Merged
merged 9 commits into from
Jan 19, 2017

Conversation

kyrias
Copy link
Contributor

@kyrias kyrias commented Jan 17, 2017

We still need to parse "plaintext" messages through the markdown
renderer so that escappes are rendered properly.

@matrixbot
Copy link
Member

Can one of the admins verify this patch?

We still need to parse "plaintext" messages through the markdown
renderer so that escappes are rendered properly.

Fixes element-hq/element-web#2870.

Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
@kyrias
Copy link
Contributor Author

kyrias commented Jan 17, 2017

(This is a WIP at the moment, will take care of the errors)

Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
@kyrias
Copy link
Contributor Author

kyrias commented Jan 17, 2017

Should be ready fro reviewing now!

@kyrias
Copy link
Contributor Author

kyrias commented Jan 17, 2017

Oh, and I just realized that I accidentally got c819b43 in there, but it's not directly related to this PR, it just fixes the old messageinput component to use the new markdown render function. I can split that out into its own PR if you want.

@kyrias
Copy link
Contributor Author

kyrias commented Jan 17, 2017

Okay, this isn't quite ready for merging yet, there are some more functions that need to be nulled-out, eg " will be HTML entity encoded currently.

Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
@kyrias
Copy link
Contributor Author

kyrias commented Jan 17, 2017

And now that is fixed! This should probably be tested out for a little while by more than just me before this is merged, to try to catch any other possible edge cases.

@@ -56,23 +57,47 @@ export default class Markdown {
return is_plain;
}

toHTML() {
render(html) {
Copy link
Member

Choose a reason for hiding this comment

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

Please don't call this render, as it is confusing and can make readers mistake the Markdown class as a React class.

Copy link
Member

Choose a reason for hiding this comment

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

Also, it is not at all clear that html is a boolean. Please document what it is supposed to do.

Copy link
Contributor Author

@kyrias kyrias Jan 18, 2017

Choose a reason for hiding this comment

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

What do you want me to rename it to? toHTML isn't really an apt name anymore with this.

EDIT: I guess I can revert this to be separate functions actually.

Copy link
Member

Choose a reason for hiding this comment

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

Given most of this function is branching based on this conditional, separate functions sounds excellent, as it will also make it clearer at the call site what you're actually doing toHTML or toPlaintext or whatever.

real_paragraph.call(this, node, entering);
} else {
renderer.out = function(s) {
this.lit(s);
Copy link
Member

Choose a reason for hiding this comment

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

Uhh, what is this doing? No comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, seems I only added the comment in the commit message. Will fix that. The normal out function runs the output through a sanitizer that will entity encode things like quotation marks, which is problematic when it happens multiple times.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the explanation. What's lit(s) doing though?

Copy link
Member

@kegsay kegsay left a comment

Choose a reason for hiding this comment

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

This should probably be tested out for a little while by more than just me before this is merged, to try to catch any other possible edge cases.

Please add some unit tests for this. This should be pretty easy to do given how modular the Markdown class is.

@kyrias
Copy link
Contributor Author

kyrias commented Jan 18, 2017

Unit tests for what exactly? Just the issue I found (" being entity encoded), or in general? Because for the latter I could really use some suggestions for what to test.

Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
@kegsay
Copy link
Member

kegsay commented Jan 18, 2017

Unit tests for what exactly? Just the issue I found (" being entity encoded), or in general? Because for the latter I could really use some suggestions for what to test.

The point is to test that the Markdown class actually works. We've already got a good list of stuff we've already found as bugs:

Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
@kyrias
Copy link
Contributor Author

kyrias commented Jan 18, 2017

Split the functions and added some test cases now. It seems like while commonmark doesn't strip <s> tags, riot-web seems to not render them. <del> tags work fine though.

Copy link
Member

@kegsay kegsay left a comment

Choose a reason for hiding this comment

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

Other than 2 things, this LGTM!

@@ -56,12 +59,10 @@ export default class Markdown {
return is_plain;
}

toHTML() {
const parser = new commonmark.Parser();
toHTML(html) {
Copy link
Member

Choose a reason for hiding this comment

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

You don't appear to use html.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aah, relics that I missed in my git add -p!

if (md.isPlainText()) {
contentText = md.toPlaintext();
} else {
contentHTML = md.toHTML(true);
Copy link
Member

Choose a reason for hiding this comment

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

Likewise I don't think you need the true anymore.

@@ -331,6 +331,7 @@ module.exports = React.createClass({
MatrixClientPeg.get().sendHtmlMessage(this.props.room.roomId, contentText, htmlText);
}
else {
const contentText = mdown.toPlaintext(false);
Copy link
Member

Choose a reason for hiding this comment

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

And here.

mci.handleReturn(sinon.stub());

expect(spy.calledOnce).toEqual(true);
expect(spy.args[0][1]).toEqual('Lorem ipsum dolor sit amet, consectetur adipiscing elit.');
Copy link
Member

Choose a reason for hiding this comment

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

Don't you need to assert that spy.args[0][2] is actually blank to be sure it didn't try to send this as HTML?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If it tried to send it as HTML the calledOnce assertion will fail, because then sendHtmlMessage will have been used rather than sendTextMessage

Copy link
Member

Choose a reason for hiding this comment

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

Aha! Gotcha.

mci.handleReturn(sinon.stub());

expect(spy.calledOnce).toEqual(true);
expect(spy.args[0][1]).toEqual('Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n\nFusce congue sapien sed neque molestie volutpat.');
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
@kegsay
Copy link
Member

kegsay commented Jan 19, 2017

LGTM!

@kegsay kegsay merged commit 89fa47d into matrix-org:develop Jan 19, 2017
@kyrias kyrias deleted the commonmark-fix-escaping branch January 19, 2017 14:48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants