Kaynağa Gözat

fix(HTML Parser): fix nasty bug where malformed HTML would hang showdown

When feeding malformed HTML to showdown, the library would enter an infinite loop,
effectively halting showdown's execution.

Closes #393
Estevao Soares dos Santos 8 yıl önce
ebeveyn
işleme
6566c72cc1

+ 12 - 5
dist/showdown.js

@@ -1,4 +1,4 @@
-;/*! showdown 01-06-2017 */
+;/*! showdown 02-06-2017 */
 (function(){
 /**
  * Created by Tivie on 13-07-2015.
@@ -1942,17 +1942,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

Dosya farkı çok büyük olduğundan ihmal edildi
+ 0 - 0
dist/showdown.js.map


Dosya farkı çok büyük olduğundan ihmal edildi
+ 1 - 1
dist/showdown.min.js


Dosya farkı çok büyük olduğundan ihmal edildi
+ 0 - 0
dist/showdown.min.js.map


+ 11 - 4
src/subParsers/hashHTMLBlocks.js

@@ -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

+ 1 - 0
test/issues/#393.showdown-hangs-with-malformed-html.html

@@ -0,0 +1 @@
+<p><p>malformed<p></p>

+ 1 - 0
test/issues/#393.showdown-hangs-with-malformed-html.md

@@ -0,0 +1 @@
+<p>malformed<p>

Bu fark içinde çok fazla dosya değişikliği olduğu için bazı dosyalar gösterilmiyor