Skip to content

Commit

Permalink
fix(HTML Parser): fix nasty bug where malformed HTML would hang showdown
Browse files Browse the repository at this point in the history
When feeding malformed HTML to showdown, the library would enter an infinite loop,
effectively halting showdown's execution.

Closes #393
  • Loading branch information
tivie committed Jun 2, 2017
1 parent 1eee6af commit 6566c72
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 13 deletions.
17 changes: 12 additions & 5 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

15 changes: 11 additions & 4 deletions src/subParsers/hashHTMLBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,24 @@ showdown.subParser('hashHTMLBlocks', function (text, options, globals) {

for (var i = 0; i < blockTags.length; ++i) {

var opTagPos,
var opTagPos, ii = 0,
rgx1 = new RegExp('^ {0,3}<' + blockTags[i] + '\\b[^>]*>', 'im'),
patLeft = '<' + blockTags[i] + '\\b[^>]*>',
patRight = '</' + blockTags[i] + '>';
// 1. Look for the first position of the first opening HTML tag in the text
while ((opTagPos = showdown.helper.regexIndexOf(text, rgx1)) !== -1) {
//2. Split the text in that position
var subTexts = showdown.helper.splitAtIndex(text, opTagPos);
var subTexts = showdown.helper.splitAtIndex(text, opTagPos),
//3. Match recursively
subTexts[1] = showdown.helper.replaceRecursiveRegExp(subTexts[1], repFunc, patLeft, patRight, 'im');
text = subTexts[0].concat(subTexts[1]);
newSubText1 = showdown.helper.replaceRecursiveRegExp(subTexts[1], repFunc, patLeft, patRight, 'im');

// prevent an infinite loop
if (newSubText1 === subTexts[1]) {
break;
}

text = subTexts[0].concat(newSubText1);
ii++;
}
}
// HR SPECIAL CASE
Expand Down
1 change: 1 addition & 0 deletions test/issues/#393.showdown-hangs-with-malformed-html.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><p>malformed<p></p>
1 change: 1 addition & 0 deletions test/issues/#393.showdown-hangs-with-malformed-html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>malformed<p>

0 comments on commit 6566c72

Please sign in to comment.