CVE-2020-28441: Improperly Controlled Modification of Object Prototype Attributes ('Prototype Pollution') (CWE-1321)
value: This affects the package conf-cfg-ini before 1.2.2. If an attacker submits a malicious INI file to an application that parses it with decode, they will pollute the prototype on the application. This can be exploited further depending on the context.
CWE 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.
CVSS v3 base score: 9.8
Published: 2022-07-25T14:15Z
Repository: https://github.com/loge5/conf-cfg-ini
Language: JavaScript
Files changed: conf-cfg-ini.js, conf-cfg-ini.spec.js
Commit date: 2020-12-09 17:53:16 +0100
Commit message: fix: prevent prototype pollution attack

Diff: diff --git a/conf-cfg-ini.js b/conf-cfg-ini.js
index adbd4da..fe89449 100644
--- a/conf-cfg-ini.js
+++ b/conf-cfg-ini.js
@@ -33,10 +33,11 @@ Config.prototype.decode = function(data){
             data = data.toString();
         } else {
             throw new Error('expecting string but got '+typeof data);
         }
     }
+    var protectedKeys = ['__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', '__proto__'];
     var result = {};
     var currentSection = undefined;
     var lines = data.split(this.options.lineEnding);
     for(var i = 0; i < lines.length; i++){
         var line = lines[i];
@@ -49,11 +50,11 @@ Config.prototype.decode = function(data){
         
         var sectionRegExp = new RegExp("^\\"+this.options.sectionOpenIdentifier+"(.*?)\\"+this.options.sectionCloseIdentifier+"$");
         var newSection = line.match(sectionRegExp);
         if(newSection !== null){
             currentSection = newSection[1];
-            if(typeof result[currentSection] === 'undefined'){
+            if(typeof result[currentSection] === 'undefined' && !protectedKeys.includes(currentSection)){
                 result[currentSection] = {};
             }
             continue;
         }
 
@@ -76,10 +77,13 @@ Config.prototype.decode = function(data){
             value = line.substr(assignPosition+assignIdentifierLength);
         }
         if (typeof this.options.valueIdentifier === 'string') {
             value = this.valueTrim(value, this.options.valueIdentifier);
         }
+        if (protectedKeys.includes(currentSection) || protectedKeys.includes(key)) {
+            continue;
+        }
         if(typeof currentSection === 'undefined'){
             result[key] = value;
         } else {
             result[currentSection][key] = value;
         }
diff --git a/conf-cfg-ini.spec.js b/conf-cfg-ini.spec.js
index 0ea853b..af718b7 100644
--- a/conf-cfg-ini.spec.js
+++ b/conf-cfg-ini.spec.js
@@ -110,10 +110,20 @@ describe('Config', function() {
         var result = config.decode("[Section]\nfoo:bar\n");
         should.exist(result.Section);
         expect(result.Section.foo).to.equal("bar");
     });
 
+    it('decode should prevent prototype pollution attacks', function () {
+        var config = new Config();
+        config.options.lineEnding = "\n";
+        config.options.assignIdentifier = ":";
+        var result = config.decode("[__proto__]\nfoo:bar\n");
+        should.not.exist(result.__proto__.foo);
+        result = config.decode("[Section]\n__proto__:bar\n");
+        expect(result.Section.__proto__).to.not.equal("bar");
+    });
+
     it('valueTrim should trim custom chars', function () {
         var config = new Config();
         expect(config.valueTrim('"Te"s"t"', '"')).to.equal('Te"s"t');
         expect(config.valueTrim('"Te"s"t"', '')).to.equal('"Te"s"t"');
         expect(config.valueTrim('"Te"s"t"', '#')).to.equal('"Te"s"t"');

Vulnerable code:             if(typeof result[currentSection] === 'undefined'){
Fixed code:     var protectedKeys = ['__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', '__proto__'];
            if(typeof result[currentSection] === 'undefined' && !protectedKeys.includes(currentSection)){
        if (protectedKeys.includes(currentSection) || protectedKeys.includes(key)) {
            continue;
        }
    it('decode should prevent prototype pollution attacks', function () {
        var config = new Config();
        config.options.lineEnding = "\n";
        config.options.assignIdentifier = ":";
        var result = config.decode("[__proto__]\nfoo:bar\n");
        should.not.exist(result.__proto__.foo);
        result = config.decode("[Section]\n__proto__:bar\n");
        expect(result.Section.__proto__).to.not.equal("bar");
    });