Skip to content

Commit 35cebc5

Browse files
committed
rewrite the HTML message stuff to fix XSS and improve clarity
1 parent ffb9ce8 commit 35cebc5

2 files changed

Lines changed: 47 additions & 40 deletions

File tree

‎src/skins/vector/views/molecules/MNoticeTile.js‎

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,43 +35,46 @@ module.exports = React.createClass({
3535
// FIXME: this entire class is copy-pasted from MTextTile :(
3636
render: function() {
3737
var content = this.props.mxEvent.getContent();
38-
var body = content.body;
39-
40-
if (content.format === "org.matrix.custom.html") {
41-
body = sanitizeHtml(content.formatted_body, sanitizeHtmlParams);
42-
}
38+
var originalBody = content.body;
4339

4440
if (this.props.searchTerm) {
4541
var lastOffset = 0;
4642
var bodyList = [];
4743
var k = 0;
4844
var offset;
49-
// XXX: this probably doesn't handle stemming very well.
50-
while ((offset = body.indexOf(this.props.searchTerm, lastOffset)) >= 0) {
51-
if (content.format === "org.matrix.custom.html") {
45+
46+
// XXX: rather than searching for the search term in the body,
47+
// we should be looking at the match delimiters returned by the FTS engine
48+
if (content.format === "org.matrix.custom.html") {
49+
var safeBody = sanitizeHtml(content.formatted_body, sanitizeHtmlParams);
50+
var safeSearchTerm = sanitizeHtml(this.props.searchTerm, sanitizeHtmlParams);
51+
while ((offset = safeBody.indexOf(safeSearchTerm, lastOffset)) >= 0) {
5252
// FIXME: we need to apply the search highlighting to only the text elements of HTML, which means
5353
// hooking into the sanitizer parser rather than treating it as a string. Otherwise
5454
// the act of highlighting a <b/> or whatever will break the HTML badly.
55-
bodyList.push(<span key={ k++ } dangerouslySetInnerHTML={{ __html: body.substring(lastOffset, offset) }} />);
56-
bodyList.push(<span key={ k++ } dangerouslySetInnerHTML={{ __html: this.props.searchTerm }} className="mx_MessageTile_searchHighlight" />);
57-
}
58-
else {
59-
bodyList.push(<span key={ k++ } >{ body.substring(lastOffset, offset) }</span>);
60-
bodyList.push(<span key={ k++ } className="mx_MessageTile_searchHighlight">{ this.props.searchTerm }</span>);
55+
bodyList.push(<span key={ k++ } dangerouslySetInnerHTML={{ __html: safeBody.substring(lastOffset, offset) }} />);
56+
bodyList.push(<span key={ k++ } dangerouslySetInnerHTML={{ __html: safeSearchTerm }} className="mx_MessageTile_searchHighlight" />);
57+
lastOffset = offset + safeSearchTerm.length;
6158
}
62-
lastOffset = offset + this.props.searchTerm.length;
63-
}
64-
if (content.format === "org.matrix.custom.html") {
65-
bodyList.push(<span key={ k++ } dangerouslySetInnerHTML={{ __html: body.substring(lastOffset) }} />);
59+
bodyList.push(<span key={ k++ } dangerouslySetInnerHTML={{ __html: safeBody.substring(lastOffset) }} />);
6660
}
6761
else {
68-
bodyList.push(<span key={ k++ }>{ body.substring(lastOffset) }</span>);
62+
while ((offset = originalBody.indexOf(this.props.searchTerm, lastOffset)) >= 0) {
63+
bodyList.push(<span key={ k++ } >{ originalBody.substring(lastOffset, offset) }</span>);
64+
bodyList.push(<span key={ k++ } className="mx_MessageTile_searchHighlight">{ this.props.searchTerm }</span>);
65+
lastOffset = offset + this.props.searchTerm.length;
66+
}
67+
bodyList.push(<span key={ k++ }>{ originalBody.substring(lastOffset) }</span>);
6968
}
7069
body = bodyList;
7170
}
7271
else {
7372
if (content.format === "org.matrix.custom.html") {
74-
body = <span dangerouslySetInnerHTML={{ __html: body }} />;
73+
var safeBody = sanitizeHtml(content.formatted_body, sanitizeHtmlParams);
74+
body = <span dangerouslySetInnerHTML={{ __html: safeBody }} />;
75+
}
76+
else {
77+
body = originalBody;
7578
}
7679
}
7780

‎src/skins/vector/views/molecules/MTextTile.js‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,45 +32,49 @@ module.exports = React.createClass({
3232
displayName: 'MTextTile',
3333
mixins: [MTextTileController],
3434

35+
// FIXME: this entire class is copy-pasted from MTextTile :(
3536
render: function() {
3637
var content = this.props.mxEvent.getContent();
37-
var body = content.body;
38-
39-
if (content.format === "org.matrix.custom.html") {
40-
body = sanitizeHtml(content.formatted_body, sanitizeHtmlParams);
41-
}
38+
var originalBody = content.body;
4239

4340
if (this.props.searchTerm) {
4441
var lastOffset = 0;
4542
var bodyList = [];
4643
var k = 0;
4744
var offset;
48-
// XXX: this probably doesn't handle stemming very well.
49-
while ((offset = body.indexOf(this.props.searchTerm, lastOffset)) >= 0) {
50-
if (content.format === "org.matrix.custom.html") {
45+
46+
// XXX: rather than searching for the search term in the body,
47+
// we should be looking at the match delimiters returned by the FTS engine
48+
if (content.format === "org.matrix.custom.html") {
49+
var safeBody = sanitizeHtml(content.formatted_body, sanitizeHtmlParams);
50+
var safeSearchTerm = sanitizeHtml(this.props.searchTerm, sanitizeHtmlParams);
51+
while ((offset = safeBody.indexOf(safeSearchTerm, lastOffset)) >= 0) {
5152
// FIXME: we need to apply the search highlighting to only the text elements of HTML, which means
5253
// hooking into the sanitizer parser rather than treating it as a string. Otherwise
5354
// the act of highlighting a <b/> or whatever will break the HTML badly.
54-
bodyList.push(<span key={ k++ } dangerouslySetInnerHTML={{ __html: body.substring(lastOffset, offset) }} />);
55-
bodyList.push(<span key={ k++ } dangerouslySetInnerHTML={{ __html: this.props.searchTerm }} className="mx_MessageTile_searchHighlight" />);
56-
}
57-
else {
58-
bodyList.push(<span key={ k++ } >{ body.substring(lastOffset, offset) }</span>);
59-
bodyList.push(<span key={ k++ } className="mx_MessageTile_searchHighlight">{ this.props.searchTerm }</span>);
55+
bodyList.push(<span key={ k++ } dangerouslySetInnerHTML={{ __html: safeBody.substring(lastOffset, offset) }} />);
56+
bodyList.push(<span key={ k++ } dangerouslySetInnerHTML={{ __html: safeSearchTerm }} className="mx_MessageTile_searchHighlight" />);
57+
lastOffset = offset + safeSearchTerm.length;
6058
}
61-
lastOffset = offset + this.props.searchTerm.length;
62-
}
63-
if (content.format === "org.matrix.custom.html") {
64-
bodyList.push(<span key={ k++ } dangerouslySetInnerHTML={{ __html: body.substring(lastOffset) }} />);
59+
bodyList.push(<span key={ k++ } dangerouslySetInnerHTML={{ __html: safeBody.substring(lastOffset) }} />);
6560
}
6661
else {
67-
bodyList.push(<span key={ k++ }>{ body.substring(lastOffset) }</span>);
62+
while ((offset = originalBody.indexOf(this.props.searchTerm, lastOffset)) >= 0) {
63+
bodyList.push(<span key={ k++ } >{ originalBody.substring(lastOffset, offset) }</span>);
64+
bodyList.push(<span key={ k++ } className="mx_MessageTile_searchHighlight">{ this.props.searchTerm }</span>);
65+
lastOffset = offset + this.props.searchTerm.length;
66+
}
67+
bodyList.push(<span key={ k++ }>{ originalBody.substring(lastOffset) }</span>);
6868
}
6969
body = bodyList;
7070
}
7171
else {
7272
if (content.format === "org.matrix.custom.html") {
73-
body = <span dangerouslySetInnerHTML={{ __html: body }} />;
73+
var safeBody = sanitizeHtml(content.formatted_body, sanitizeHtmlParams);
74+
body = <span dangerouslySetInnerHTML={{ __html: safeBody }} />;
75+
}
76+
else {
77+
body = originalBody;
7478
}
7579
}
7680

0 commit comments

Comments
 (0)