CVE-2021-21353: Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection') (CWE-74)
value: Pug is an npm package which is a high-performance template engine. In pug before version 3.0.1, if a remote attacker was able to control the `pretty` option of the pug compiler, e.g. if you spread a user provided object such as the query parameters of a request into the pug template inputs, it was possible for them to achieve remote code execution on the node.js backend. This is fixed in version 3.0.1. This advisory applies to multiple pug packages including "pug", "pug-code-gen". pug-code-gen has a backported fix at version 2.0.3. This advisory is not exploitable if there is no way for un-trusted input to be passed to pug as the `pretty` option, e.g. if you compile templates in advance before applying user input to them, you do not need to upgrade.
CWE description: The product constructs all or part of a command, data structure, or record using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify how it is parsed or interpreted when it is sent to a downstream component.
CVSS v3 base score: 9
Published: 2021-03-03T02:15Z
Repository: https://github.com/pugjs/pug
Language: JavaScript
Files changed: packages/pug-code-gen/index.js
Commit date: 2021-02-28 18:21:18 +0000
Commit message: fix: sanitise and escape the `pretty` option (#3314)


Diff: diff --git a/packages/pug-code-gen/index.js b/packages/pug-code-gen/index.js
index a75929c..de5c70a 100644
--- a/packages/pug-code-gen/index.js
+++ b/packages/pug-code-gen/index.js
@@ -55,10 +55,15 @@ function Compiler(node, options) {
   this.hasCompiledTag = false;
   this.pp = options.pretty || false;
   if (this.pp && typeof this.pp !== 'string') {
     this.pp = '  ';
   }
+  if (this.pp && !/^\s+$/.test(this.pp)) {
+    throw new Error(
+      'The pretty parameter should either be a boolean or whitespace only string'
+    );
+  }
   this.debug = false !== options.compileDebug;
   this.indents = 0;
   this.parentIndents = 0;
   this.terse = false;
   this.mixins = {};
@@ -450,11 +455,13 @@ Compiler.prototype = {
    */
 
   visitMixinBlock: function(block) {
     if (this.pp)
       this.buf.push(
-        "pug_indent.push('" + Array(this.indents + 1).join(this.pp) + "');"
+        'pug_indent.push(' +
+          stringify(Array(this.indents + 1).join(this.pp)) +
+          ');'
       );
     this.buf.push('block && block();');
     if (this.pp) this.buf.push('pug_indent.pop();');
   },
 
@@ -502,11 +509,13 @@ Compiler.prototype = {
     this.mixins[key] = this.mixins[key] || {used: false, instances: []};
     if (mixin.call) {
       this.mixins[key].used = true;
       if (pp)
         this.buf.push(
-          "pug_indent.push('" + Array(this.indents + 1).join(pp) + "');"
+          'pug_indent.push(' +
+            stringify(Array(this.indents + 1).join(pp)) +
+            ');'
         );
       if (block || attrs.length || attrsBlocks.length) {
         this.buf.push(name + '.call({');
 
         if (block) {

Vulnerable code:         "pug_indent.push('" + Array(this.indents + 1).join(this.pp) + "');"
          "pug_indent.push('" + Array(this.indents + 1).join(pp) + "');"
Fixed code:   if (this.pp && !/^\s+$/.test(this.pp)) {
    throw new Error(
      'The pretty parameter should either be a boolean or whitespace only string'
    );
  }
        'pug_indent.push(' +
          stringify(Array(this.indents + 1).join(this.pp)) +
          ');'
          'pug_indent.push(' +
            stringify(Array(this.indents + 1).join(pp)) +
            ');'