CVE-2019-20922: Uncontrolled Resource Consumption (CWE-400)
value: Handlebars before 4.4.5 allows Regular Expression Denial of Service (ReDoS) because of eager matching. The parser may be forced into an endless loop while processing crafted templates. This may allow attackers to exhaust system resources.
CWE description: The product does not properly control the allocation and maintenance of a limited resource, thereby enabling an actor to influence the amount of resources consumed, eventually leading to the exhaustion of available resources.
CVSS v3 base score: 7.5
Published: 2020-09-30T18:15Z
Repository: https://github.com/handlebars-lang/handlebars.js
Language: JavaScript
Files changed: spec/helpers.js, spec/tokenizer.js, src/handlebars.l
Commit date: 2019-10-20 22:56:24 +0200
Commit message: fix: non-eager matching raw-block-contents

In 4.4.4 the block-contents was matched with an eager match, which means
that with multiple raw-blocks of the same kind, the block was spanned
over the first ending-tag until the last one.
This commit replaces this by a non-eager match.

closes #1579

Diff: diff --git a/spec/helpers.js b/spec/helpers.js
index 0e756ab..f95817c 100644
--- a/spec/helpers.js
+++ b/spec/helpers.js
@@ -45,14 +45,22 @@ describe('helpers', function() {
 
     it('helper for nested raw block works with empty content', function() {
       runWithIdentityHelper('{{{{identity}}}}{{{{/identity}}}}', '');
     });
 
-    it('helper for nested raw block works if nested raw blocks are broken', function() {
+    xit('helper for nested raw block works if nested raw blocks are broken', function() {
+      // This test was introduced in 4.4.4, but it was not the actual problem that lead to the patch release
+      // The test is deactivated, because in 3.x this template cases an exception and it also does not work in 4.4.3
+      // If anyone can make this template work without breaking everything else, then go for it,
+      // but for now, this is just a known bug, that will be documented.
       runWithIdentityHelper('{{{{identity}}}} {{{{a}}}} {{{{ {{{{/ }}}} }}}} {{{{/identity}}}}', ' {{{{a}}}} {{{{ {{{{/ }}}} }}}} ');
     });
 
+    it('helper for nested raw block closes after first matching close', function() {
+      runWithIdentityHelper('{{{{identity}}}}abc{{{{/identity}}}} {{{{identity}}}}abc{{{{/identity}}}}', 'abc abc');
+    });
+
     it('helper for nested raw block throw exception when with missing closing braces', function() {
       var string = '{{{{a}}}} {{{{/a';
       shouldThrow(function() {
         Handlebars.compile(string)();
       });
diff --git a/spec/tokenizer.js b/spec/tokenizer.js
index 1a361b7..ec15d59 100644
--- a/spec/tokenizer.js
+++ b/spec/tokenizer.js
@@ -439,6 +439,11 @@ describe('Tokenizer', function() {
     shouldMatchTokens(result, ['OPEN_BLOCK', 'ID', 'ID', 'OPEN_BLOCK_PARAMS', 'ID', 'ID', 'CLOSE_BLOCK_PARAMS', 'CLOSE']);
 
     result = tokenize('{{else foo as |bar baz|}}');
     shouldMatchTokens(result, ['OPEN_INVERSE_CHAIN', 'ID', 'OPEN_BLOCK_PARAMS', 'ID', 'ID', 'CLOSE_BLOCK_PARAMS', 'CLOSE']);
   });
+
+  it('tokenizes raw blocks', function() {
+    var result = tokenize('{{{{a}}}} abc {{{{/a}}}} aaa {{{{a}}}} abc {{{{/a}}}}');
+    shouldMatchTokens(result, ['OPEN_RAW_BLOCK', 'ID', 'CLOSE_RAW_BLOCK', 'CONTENT', 'END_RAW_BLOCK', 'CONTENT', 'OPEN_RAW_BLOCK', 'ID', 'CLOSE_RAW_BLOCK', 'CONTENT', 'END_RAW_BLOCK']);
+  });
 });
diff --git a/src/handlebars.l b/src/handlebars.l
index 2b27a9f..fbf208b 100644
--- a/src/handlebars.l
+++ b/src/handlebars.l
@@ -61,11 +61,11 @@ ID    [^\s!"#%-,\.\/;->@\[-\^`\{-~]+/{LOOKAHEAD}
                                   } else {
                                     strip(5, 9);
                                     return 'END_RAW_BLOCK';
                                   }
                                  }
-<raw>[^\x00]+/("{{{{")          { return 'CONTENT'; }
+<raw>[^\x00]+?/("{{{{")          { return 'CONTENT'; }
 
 <com>[\s\S]*?"--"{RIGHT_STRIP}?"}}" {
   this.popState();
   return 'COMMENT';
 }

Vulnerable code:     it('helper for nested raw block works if nested raw blocks are broken', function() {
<raw>[^\x00]+/("{{{{")          { return 'CONTENT'; }
Fixed code:     xit('helper for nested raw block works if nested raw blocks are broken', function() {
      // This test was introduced in 4.4.4, but it was not the actual problem that lead to the patch release
      // The test is deactivated, because in 3.x this template cases an exception and it also does not work in 4.4.3
      // If anyone can make this template work without breaking everything else, then go for it,
      // but for now, this is just a known bug, that will be documented.
    it('helper for nested raw block closes after first matching close', function() {
      runWithIdentityHelper('{{{{identity}}}}abc{{{{/identity}}}} {{{{identity}}}}abc{{{{/identity}}}}', 'abc abc');
    });


  it('tokenizes raw blocks', function() {
    var result = tokenize('{{{{a}}}} abc {{{{/a}}}} aaa {{{{a}}}} abc {{{{/a}}}}');
    shouldMatchTokens(result, ['OPEN_RAW_BLOCK', 'ID', 'CLOSE_RAW_BLOCK', 'CONTENT', 'END_RAW_BLOCK', 'CONTENT', 'OPEN_RAW_BLOCK', 'ID', 'CLOSE_RAW_BLOCK', 'CONTENT', 'END_RAW_BLOCK']);
  });
<raw>[^\x00]+?/("{{{{")          { return 'CONTENT'; }