CVE-2022-21169 (published 2022-09-26T05:15Z)

The package express-xss-sanitizer before 1.1.3 are vulnerable to Prototype Pollution via the allowedTags attribute, allowing the attacker to bypass xss sanitization.

CVSS v3 base score: 6.1, MEDIUM (CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:C/C:L/I:L/A:N)

Weakness information:
cwe_id: CWE-1321
cwe_name: Improperly Controlled Modification of Object Prototype Attributes ('Prototype Pollution')
description: The product receives input from an upstream component that specifies attributes that are to be initialized or updated in an object, but it does not properly control modifications of attributes of the object prototype.
extended_description: 
				By adding or modifying attributes of an object prototype, it is possible to create attributes that exist on every object, or replace critical attributes with malicious ones. This can be problematic if the product depends on existence or non-existence of certain attributes, or uses pre-defined attributes of object prototype (such as hasOwnProperty, toString or valueOf).
				This weakness is usually exploited by using a special attribute of objects called proto,  constructor or prototype. Such attributes give access to the object prototype. This weakness is often found in code that assigns object attributes based on user input, or merges or clones objects recursively.
			
			

Fix information:
repo_url: https://github.com/AhmedAdelFahim/express-xss-sanitizer
commit_details: repo_url: https://github.com/AhmedAdelFahim/express-xss-sanitizer
author: AhmedAdelFahim
author_date: 2022-09-20 22:09:26+02:00
msg: fix XSS bypass by using prototype pollution issue.
repository: repo_url: https://github.com/AhmedAdelFahim/express-xss-sanitizer
repo_name: AhmedAdelFahim/express-xss-sanitizer
description: Express 4.x middleware which sanitizes user input data (in req.body, req.query, req.headers and req.params) to prevent Cross Site Scripting (XSS) attack.
repo_language: JavaScript
owner: AhmedAdelFahim
file_changes: filename: .eslintrc.json
change_type: ModificationType.MODIFY
diff: @@ -21,6 +21,7 @@
         "prettier/prettier": ["error"],
         "quotes": "off",
         "strict": "off",
-        "prefer-arrow-callback":"off"
+        "prefer-arrow-callback":"off",
+        "operator-linebreak": "off"
     }
 }

programming_language: JavaScript
filename: sanitize.js
change_type: ModificationType.MODIFY
diff: @@ -4,12 +4,19 @@ const sanitizeHtml = require("sanitize-html");
 
 const initializeOptions = (options) => {
   const sanitizerOptions = {};
-  if (Array.isArray(options.allowedTags) && options.allowedTags.length > 0) {
+  if (
+    Object.hasOwn(options, "allowedTags") &&
+    Array.isArray(options.allowedTags) &&
+    options.allowedTags.length > 0
+  ) {
     sanitizerOptions.allowedTags = options.allowedTags;
   }
   return {
     allowedKeys:
-      (Array.isArray(options.allowedKeys) && options.allowedKeys) || [],
+      (Object.hasOwn(options, "allowedKeys") &&
+        Array.isArray(options.allowedKeys) &&
+        options.allowedKeys) ||
+      [],
     sanitizerOptions,
   };
 };

programming_language: JavaScript
filename: test.js
change_type: ModificationType.MODIFY
diff: @@ -1222,4 +1222,34 @@ describe("Express xss Sanitize", function () {
       });
     });
   });
+
+  describe("Sanitize data with custom options as function", function () {
+    describe("Sanitize simple object", function () {
+      it("should sanitize dirty body.", function (done) {
+        expect(sanitize({
+          a: "<script>Test</script>",
+          b: '<p onclick="return;">Test</p>',
+          c: '<img src="/"/>',
+        }, { allowedKeys: ["c"] })).to.eql({
+          a: "",
+          b: "<p>Test</p>",
+          c: '<img src="/"/>',
+        });
+        done();
+      });
+    });
+
+    describe("XSS bypass by using prototype pollution issue", function () {
+      it("should sanitize dirty data after prototype pollution.", function (done) {
+        // eslint-disable-next-line no-extend-native
+        Object.prototype.allowedTags = ['script'];
+        expect(sanitize({
+          a: "<script>Test</script>",
+        }, {})).to.eql({
+          a: "",
+        });
+        done();
+      });
+    });
+  });
 });

programming_language: JavaScript