CVE-2020-7638: Improperly Controlled Modification of Object Prototype Attributes ('Prototype Pollution') (CWE-1321)
value: confinit through 0.3.0 is vulnerable to Prototype Pollution.The 'setDeepProperty' function could be tricked into adding or modifying properties of 'Object.prototype' using a '__proto__' payload.
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: 5.3
Published: 2020-04-06T13:15Z
Repository: https://github.com/davideicardi/confinit
Language: TypeScript
Files changed: index.d.ts, index.js, index.js.map, index.ts, sample/index.js, sample/index.js.map, sample/index.ts, test/setDeepProperty.test.js, test/setDeepProperty.test.js.map, test/setDeepProperty.test.ts
Commit date: 2020-03-01 10:59:42 +0100
Commit message: Close #1

Diff: diff --git a/index.d.ts b/index.d.ts
index b3f6167..1cfa6bd 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -4,8 +4,10 @@ export interface IConfigSection {
 }
 export declare function validate(configuration: any): void;
 export declare function applyEnvVariables(configuration: any, envVariables: NodeJS.ProcessEnv, envPrefix?: string): void;
 export declare function applyConfigFile(configuration: any, configFile: string): void;
 export declare function applyCommandArgs(configuration: any, argv: string[]): void;
-export declare function setDeepProperty(obj: any, propertyPath: string, value: any): void;
+export declare function setDeepProperty(obj: {
+    [key: string]: any;
+}, propertyPath: string, value: any): void;
 export declare function getDeepProperty(obj: any, propertyPath: string): any;
 export declare function objectsAreEqual(obj1: any, obj2: any, leftOnly?: boolean): boolean;
diff --git a/index.js b/index.js
index f311bfe..b1d07e4 100644
--- a/index.js
+++ b/index.js
@@ -56,39 +56,67 @@ function applyCommandArgs(configuration, argv) {
     const argvKeys = Object.keys(parsedArgv);
     if (!argvKeys.length) {
         return;
     }
     debug("Appling command arguments:", parsedArgv);
-    if (parsedArgv.config) {
-        const configFile = path.resolve(process.cwd(), parsedArgv.config);
+    const CONFIG_PROP = 'config';
+    if (parsedArgv[CONFIG_PROP]) {
+        const configFile = path.resolve(process.cwd(), parsedArgv[CONFIG_PROP]);
         applyConfigFile(configuration, configFile);
     }
     for (const key in parsedArgv) {
         if (!parsedArgv.hasOwnProperty(key)) {
             continue;
         }
+        if (key.startsWith('_')) {
+            continue;
+        }
+        if (key.endsWith('_')) {
+            continue;
+        }
+        if (key === CONFIG_PROP) {
+            continue;
+        }
         const configKey = key
             .replace(/_/g, ".");
         debug(`Found config value from cmd args '${key}' to '${configKey}'`);
         setDeepProperty(configuration, configKey, parsedArgv[key]);
     }
 }
 exports.applyCommandArgs = applyCommandArgs;
 function setDeepProperty(obj, propertyPath, value) {
-    const a = splitPath(propertyPath);
-    const n = a.length;
-    for (let i = 0; i < n - 1; i++) {
-        const k = a[i];
-        if (!(k in obj)) {
-            obj[k] = {};
+    if (!obj) {
+        throw new Error("Invalid object");
+    }
+    if (!propertyPath) {
+        throw new Error("Invalid property path");
+    }
+    const pathParts = splitPath(propertyPath);
+    const pathPartsLen = pathParts.length;
+    for (let i = 0; i < pathPartsLen - 1; i++) {
+        const pathPart = pathParts[i];
+        if (!(pathPart in obj)) {
+            setProp(obj, pathPart, {});
         }
-        obj = obj[k];
+        obj = getProp(obj, pathPart);
     }
-    obj[a[n - 1]] = value;
+    setProp(obj, pathParts[pathPartsLen - 1], value);
     return;
 }
 exports.setDeepProperty = setDeepProperty;
+function setProp(obj, property, value) {
+    if (!obj.hasOwnProperty(property)) {
+        throw new Error(`Property '${property}' is not valid`);
+    }
+    obj[property] = value;
+}
+function getProp(obj, property) {
+    if (!obj.hasOwnProperty(property)) {
+        throw new Error(`Property '${property}' is not valid`);
+    }
+    return obj[property];
+}
 function getDeepProperty(obj, propertyPath) {
     let ret = obj;
     const a = splitPath(propertyPath);
     const n = a.length;
     for (let i = 0; i < n; ++i) {
diff --git a/index.js.map b/index.js.map
index 8ca1314..8ebbed1 100644
--- a/index.js.map
+++ b/index.js.map
@@ -1 +1 @@
-{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;AAAA,6BAA6B;AAE7B,+BAA+B;AAC/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Q,EAAE;QACd,0CAA0C;QAC1C,KAAK,MAAM,CAAC,IAAI,IAAI,EAAE;YACrB,IAAI,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE;gBAC5B,SAAS;aACT;YAED,wCAAwC;YACxC,IAAI,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE;gBAC5B,OAAO,KAAK,CAAC;aACb;SACD;KACD;IAED,OAAO,IAAI,CAAC;AACb,CAAC;AAlDD,0CAkDC;AAED,SAAS,SAAS,CAAC,YAAoB;IACtC,YAAY,GAAG,YAAY,CAAC,OAAO,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC,CAAC,gCAAgC;IAC1F,YAAY,GAAG,YAAY,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAW,sBAAsB;IAChF,OAAO,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;AAChC,CAAC;AAED,SAAS,SAAS,CAAC,KAAa;IAC/B,2CAA2C;IAC3C,MAAM,SAAS,GAAG,0QAA0Q,CAAC;IAC7R,OAAO,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC9B,CAAC"}
\ No newline at end of file
diff --git a/index.ts b/index.ts
index 46f5bbc..017c3f5 100644
--- a/index.ts
+++ b/index.ts
@@ -73,48 +73,80 @@ export function applyCommandArgs(configuration: any, argv: string[]) {
 		return;
 	}
 
 	debug("Appling command arguments:", parsedArgv);
 
-	if (parsedArgv.config) {
-		const configFile = path.resolve(process.cwd(), parsedArgv.config);
+	const CONFIG_PROP = 'config';
+
+	if (parsedArgv[CONFIG_PROP]) {
+		const configFile = path.resolve(process.cwd(), parsedArgv[CONFIG_PROP]);
 		applyConfigFile(configuration, configFile);
 	}
 
 	for (const key in parsedArgv) {
 		if (!parsedArgv.hasOwnProperty(key)) {
 			continue;
 		}
 
+		if (key.startsWith('_')) {
+			continue;
+		}
+		if (key.endsWith('_')) {
+			continue;
+		}
+		if (key === CONFIG_PROP) {
+			continue;
+		}
+
 		const configKey = key
 			.replace(/_/g, ".");
 
 		debug(`Found config value from cmd args '${key}' to '${configKey}'`);
 
 		setDeepProperty(configuration, configKey, parsedArgv[key]);
 	}
 }
 
 
-export function setDeepProperty(obj: any, propertyPath: string, value: any): void {
-	const a = splitPath(propertyPath);
-	const n = a.length;
+export function setDeepProperty(obj: {[key: string]: any}, propertyPath: string, value: any): void {
+	if (!obj) {
+		throw new Error("Invalid object");
+	}
+	if (!propertyPath) {
+		throw new Error("Invalid property path");
+	}
 
-	for (let i = 0; i < n - 1; i++) {
-		const k = a[i];
+	const pathParts = splitPath(propertyPath);
+	const pathPartsLen = pathParts.length;
+
+	for (let i = 0; i < pathPartsLen - 1; i++) {
+		const pathPart = pathParts[i];
 
-		if (!(k in obj)) {
-			obj[k] = {};
+		if (!(pathPart in obj)) {
+			setProp(obj, pathPart, {});
 		}
-		obj = obj[k];
+		obj = getProp(obj, pathPart);
 	}
 
-
-	obj[a[n - 1]] = value;
+	setProp(obj, pathParts[pathPartsLen - 1], value);
 	return;
 }
 
+function setProp(obj: {[key: string]: any}, property: string, value: any): void {
+	if (!obj.hasOwnProperty(property)) {
+		throw new Error(`Property '${property}' is not valid`);
+	}
+	obj[property] = value;
+}
+
+function getProp(obj: {[key: string]: any}, property: string): any {
+	if (!obj.hasOwnProperty(property)) {
+		throw new Error(`Property '${property}' is not valid`);
+	}
+	return obj[property];
+}
+
 export function getDeepProperty(obj: any, propertyPath: string): any {
 	let ret: any = obj;
 
 	const a = splitPath(propertyPath);
 	const n = a.length;
diff --git a/sample/index.js b/sample/index.js
index 3072e4d..9aea0e0 100644
--- a/sample/index.js
+++ b/sample/index.js
@@ -1,6 +1,7 @@
 "use strict";
+// See README.md for details
 Object.defineProperty(exports, "__esModule", { value: true });
 const confinit = require("../index");
 const path = require("path");
 class Section1Config {
     constructor() {
@@ -34,15 +35,18 @@ class Configuration {
         this.section1 = new Section1Config();
         this.webServer = new WebServerConfig();
         if (!env) {
             env = process.env;
         }
+        // Enable config file
         if (env.config) {
             const configFile = path.resolve(process.cwd(), env.config);
             confinit.applyConfigFile(this, configFile);
         }
+        // Enable environment variables
         confinit.applyEnvVariables(this, process.env, "cfg_");
+        // Enable command arguments
         confinit.applyCommandArgs(this, process.argv);
         confinit.validate(this);
     }
 }
 exports.Configuration = Configuration;
diff --git a/sample/index.js.map b/sample/index.js.map
index 6c9f431..5d5a31d 100644
--- a/sample/index.js.map
+++ b/sample/index.js.map
@@ -1 +1 @@
-{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;AAAA,qCAAqC;AACrC,6BAA6B;AAE7B,MAAa,cAAc;IAA3B;QACC,QAAG,GAAW,EAAE,CAAC;IAOlB,CAAC;IALA,QAAQ;QACP,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE;YACd,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;SAC1C;IACF,CAAC;CACD;AARD,wCAQC;AAED,MAAa,eAAe;IAG3B;QAFA,SAAI,GAAG,IAAI,CAAC;QAGX,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC;QACjC,IAAI,OAAO,EAAE;YACZ,IAAI,CAAC,IAAI,GAAG,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;SAClC;IACF,CAAC;IAED,QAAQ;QACP,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE;YACf,MAAM,IAAI,KAAK,CAAC,cAAc,CAAC,CAAC;SAChC;QACD,IAAI,CAAC,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE,EAAE,CAAC,CAAC;IAChD,CAAC;CACD;AAhBD,0CAgBC;AAED,MAAa,aAAa;IAIzB,YAAY,GAAuB;QAH1B,aAAQ,GAAG,IAAI,cAAc,EAAE,CAAC;QAChC,cAAS,GAAG,IAAI,eAAe,EAAE,CAAC;QAG1C,IAAI,CAAC,GAAG,EAAE;YACT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Q,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC;CACD;AArBD,sCAqBC;AAED,OAAO,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;AACpC,MAAM,MAAM,GAAG,IAAI,aAAa,EAAE,CAAC;AAEnC,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;AAC/B,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;AACzB,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;AAC/B,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAC7B,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;AAC/B,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;AAC1B,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;AAC/B,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC"}
\ No newline at end of file
diff --git a/sample/index.ts b/sample/index.ts
index a7a134a..f7aa8c0 100644
--- a/sample/index.ts
+++ b/sample/index.ts
@@ -1,5 +1,7 @@
+// See README.md for details
+
 import * as confinit from "../index";
 import * as path from "path";
 
 export class Section1Config implements confinit.IConfigSection {
 	url: string = "";
@@ -35,15 +37,19 @@ export class Configuration {
 
 	constructor(env?: NodeJS.ProcessEnv) {
 		if (!env) {
 			env = process.env;
 		}
+
+		// Enable config file
 		if (env.config) {
 			const configFile = path.resolve(process.cwd(), env.config);
 			confinit.applyConfigFile(this, configFile);
 		}
+		// Enable environment variables
 		confinit.applyEnvVariables(this, process.env, "cfg_");
+		// Enable command arguments
 		confinit.applyCommandArgs(this, process.argv);
 
 		confinit.validate(this);
 	}
 }
diff --git a/test/setDeepProperty.test.js b/test/setDeepProperty.test.js
index b3ccdaf..9960244 100644
--- a/test/setDeepProperty.test.js
+++ b/test/setDeepProperty.test.js
@@ -1,23 +1,47 @@
 "use strict";
 Object.defineProperty(exports, "__esModule", { value: true });
 const assert = require("assert");
-const __1 = require("..");
+const index_1 = require("../index");
 describe('setDeepProperty', () => {
     it('should set a property with deep 1', () => {
         const obj = {
             test: "A"
         };
-        __1.setDeepProperty(obj, "test", "B");
+        index_1.setDeepProperty(obj, "test", "B");
         assert.equal(obj.test, "B");
     });
     it('should set a property with deep 2', () => {
         const obj = {
             parent: {
                 test: "A"
             }
         };
-        __1.setDeepProperty(obj, "parent.test", "B");
+        index_1.setDeepProperty(obj, "parent.test", "B");
         assert.equal(obj.parent.test, "B");
     });
+    it('should not allow to set a not existing property', () => {
+        const obj = {
+            test: "A"
+        };
+        assert.throws(() => index_1.setDeepProperty(obj, "not_existing", "B"));
+    });
+    it('should not allow to set a property on null/undefined obj', () => {
+        assert.throws(() => index_1.setDeepProperty(null, "not_existing", "B"));
+        assert.throws(() => index_1.setDeepProperty(undefined, "not_existing", "B"));
+    });
+    it('should not allow to set a null/undefined property', () => {
+        const obj = {
+            test: "A"
+        };
+        assert.throws(() => index_1.setDeepProperty(obj, null, "B"));
+        assert.throws(() => index_1.setDeepProperty(obj, undefined, "B"));
+        assert.throws(() => index_1.setDeepProperty(obj, "", "B"));
+    });
+    it('should not allow to set __proto__ property', () => {
+        const obj = {
+            test: "A"
+        };
+        assert.throws(() => index_1.setDeepProperty(obj, "__proto__.xyz", "B"));
+    });
 });
 //# sourceMappingURL=setDeepProperty.test.js.map
\ No newline at end of file
diff --git a/test/setDeepProperty.test.js.map b/test/setDeepProperty.test.js.map
index b23651a..6835ca4 100644
--- a/test/setDeepProperty.test.js.map
+++ b/test/setDeepProperty.test.js.map
@@ -1 +1 @@
-{"version":3,"file":"setDeepProperty.test.js","sourceRoot":"","sources":["setDeepProperty.test.ts"],"names":[],"mappings":";;AAAA,iCAAiC;AACjC,0BAAqC;AAErC,QAAQ,CAAC,iBAAiB,EAAE,GAAG,EAAE;IAE/B,EAAE,CAAC,mCAAmC,EAAE,GAAG,EAAE;QAC3C,MAAM,GAAG,GAAG;YACV,IAAI,EAAE,GAAG;SACV,CAAA;QACD,mBAAe,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC;QAClC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAC9B,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,mCAAmC,EAAE,GAAG,EAAE;QAC3C,MAAM,GAAG,GAAG;YACV,MAAM,EAAE;gBACN,IAAI,EAAE,GAAG;aACV;SACF,CAAA;QACD,mBAAe,CAAC,GAAG,EAAE,aAAa,EAAE,GAAG,CAAC,CAAC;QACzC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IACrC,CAAC,CAAC,CAAC;AAEL,CAAC,CAAC,CAAC"}
\ No newline at end of file
+{"version":3,"file":"setDeepProperty.test.js","sourceRoot":"","sources":["setDeepProperty.test.ts"],"names":[],"mappings":";;AAAA,iCAAiC;AACjC,oCAA2C;AAE3C,QAAQ,CAAC,iBAAiB,EAAE,GAAG,EAAE;IAE/B,EAAE,CAAC,mCAAmC,EAAE,GAAG,EAAE;QAC3C,MAAM,GAAG,GAAG;YACV,IAAI,EAAE,GAAG;SACV,CAAA;QACD,uBAAe,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC;QAClC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAC9B,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,mCAAmC,EAAE,GAAG,EAAE;QAC3C,MAAM,GAAG,GAAG;YACV,MAAM,EAAE;gBACN,IAAI,EAAE,GAAG;aACV;SACF,CAAA;QACD,uBAAe,CAAC,GAAG,EAAE,aAAa,EAAE,GAAG,CAAC,CAAC;QACzC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IACrC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,iDA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
diff --git a/test/setDeepProperty.test.ts b/test/setDeepProperty.test.ts
index ed48768..f7083b0 100644
--- a/test/setDeepProperty.test.ts
+++ b/test/setDeepProperty.test.ts
@@ -1,7 +1,7 @@
 import * as assert from 'assert';
-import { setDeepProperty } from '..';
+import { setDeepProperty } from '../index';
 
 describe('setDeepProperty', () => {
 
   it('should set a property with deep 1', () => {
     const obj = {
@@ -19,6 +19,34 @@ describe('setDeepProperty', () => {
     }
     setDeepProperty(obj, "parent.test", "B");
     assert.equal(obj.parent.test, "B");
   });
 
+  it('should not allow to set a not existing property', () => {
+    const obj = {
+      test: "A"
+    }
+    assert.throws(() => setDeepProperty(obj, "not_existing", "B"));
+  });
+
+  it('should not allow to set a property on null/undefined obj', () => {
+    assert.throws(() => setDeepProperty(null as any, "not_existing", "B"));
+    assert.throws(() => setDeepProperty(undefined as any, "not_existing", "B"));
+  });
+
+  it('should not allow to set a null/undefined property', () => {
+    const obj = {
+      test: "A"
+    }
+    assert.throws(() => setDeepProperty(obj, null as any, "B"));
+    assert.throws(() => setDeepProperty(obj, undefined as any, "B"));
+    assert.throws(() => setDeepProperty(obj, "", "B"));
+  });
+
+  it('should not allow to set __proto__ property', () => {
+    const obj = {
+      test: "A"
+    }
+    assert.throws(() => setDeepProperty(obj, "__proto__.xyz", "B"));
+  });
+
 });
\ No newline at end of file

Vulnerable code: export declare function setDeepProperty(obj: any, propertyPath: string, value: any): void;
    if (parsedArgv.config) {
        const configFile = path.resolve(process.cwd(), parsedArgv.config);
    const a = splitPath(propertyPath);
    const n = a.length;
    for (let i = 0; i < n - 1; i++) {
        const k = a[i];
        if (!(k in obj)) {
            obj[k] = {};
        obj = obj[k];
    obj[a[n - 1]] = value;
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;AAAA,6BAA6B;AAE7B,+BAA+B;AAC/B,MAAM,KAAK,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC;AAG9B,MAAM,KAAK,GAAG,OAAO,CAAC,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Y,CAAC,OAAO,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC,CAAC,gCAAgC;IAC1F,YAAY,GAAG,YAAY,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAW,sBAAsB;IAChF,OAAO,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;AAChC,CAAC;AAED,SAAS,SAAS,CAAC,KAAa;IAC/B,2CAA2C;IAC3C,MAAM,SAAS,GAAG,0QAA0Q,CAAC;IAC7R,OAAO,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC9B,CAAC"}
	if (parsedArgv.config) {
		const configFile = path.resolve(process.cwd(), parsedArgv.config);
export function setDeepProperty(obj: any, propertyPath: string, value: any): void {
	const a = splitPath(propertyPath);
	const n = a.length;
	for (let i = 0; i < n - 1; i++) {
		const k = a[i];
		if (!(k in obj)) {
			obj[k] = {};
		obj = obj[k];

	obj[a[n - 1]] = value;
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;AAAA,qCAAqC;AACrC,6BAA6B;AAE7B,MAAa,cAAc;IAA3B;QACC,QAAG,GAAW,EAAE,CAAC;IAOlB,CAAC;IALA,QAAQ;QACP,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE;YACd,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;SAC1C;IACF,CAAC;CACD;AARD,wCAQC;AAED,MAAa,eAAe;IAG3B;QAFA,SAAI,GAAG,IAAI,CAAC;QAGX,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC;QACjC,IAAI,OAAO,EAAE;YACZ,IAAI,CAAC,IAAI,GAAG,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;SAClC;IACF,CAAC;IAED,QAAQ;QACP,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE;YACf,MAAM,IAAI,KAAK,CAAC,cAAc,CAAC,CAAC;SAChC;QACD,IAAI,CAAC,IAA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C,CAAC;AAC/B,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC"}
const __1 = require("..");
        __1.setDeepProperty(obj, "test", "B");
        __1.setDeepProperty(obj, "parent.test", "B");
{"version":3,"file":"setDeepProperty.test.js","sourceRoot":"","sources":["setDeepProperty.test.ts"],"names":[],"mappings":";;AAAA,iCAAiC;AACjC,0BAAqC;AAErC,QAAQ,CAAC,iBAAiB,EAAE,GAAG,EAAE;IAE/B,EAAE,CAAC,mCAAmC,EAAE,GAAG,EAAE;QAC3C,MAAM,GAAG,GAAG;YACV,IAAI,EAAE,GAAG;SACV,CAAA;QACD,mBAAe,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC;QAClC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAC9B,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,mCAAmC,EAAE,GAAG,EAAE;QAC3C,MAAM,GAAG,GAAG;YACV,MAAM,EAAE;gBACN,IAAI,EAAE,GAAG;aACV;SACF,CAAA;QACD,mBAAe,CAAC,GAAG,EAAE,aAAa,EAAE,GAAG,CAAC,CAAC;QACzC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IACrC,CAAC,CAAC,CAAC;AAEL,CAAC,CAAC,CAAC"}
import { setDeepProperty } from '..';
Fixed code: export declare function setDeepProperty(obj: {
    [key: string]: any;
}, propertyPath: string, value: any): void;
    const CONFIG_PROP = 'config';
    if (parsedArgv[CONFIG_PROP]) {
        const configFile = path.resolve(process.cwd(), parsedArgv[CONFIG_PROP]);
        if (key.startsWith('_')) {
            continue;
        }
        if (key.endsWith('_')) {
            continue;
        }
        if (key === CONFIG_PROP) {
            continue;
        }
    if (!obj) {
        throw new Error("Invalid object");
    }
    if (!propertyPath) {
        throw new Error("Invalid property path");
    }
    const pathParts = splitPath(propertyPath);
    const pathPartsLen = pathParts.length;
    for (let i = 0; i < pathPartsLen - 1; i++) {
        const pathPart = pathParts[i];
        if (!(pathPart in obj)) {
            setProp(obj, pathPart, {});
        obj = getProp(obj, pathPart);
    setProp(obj, pathParts[pathPartsLen - 1], value);
function setProp(obj, property, value) {
    if (!obj.hasOwnProperty(property)) {
        throw new Error(`Property '${property}' is not valid`);
    }
    obj[property] = value;
}
function getProp(obj, property) {
    if (!obj.hasOwnProperty(property)) {
        throw new Error(`Property '${property}' is not valid`);
    }
    return obj[property];
}
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;AAAA,6BAA6B;AAE7B,+BAA+B;AAC/B,MAAM,KAAK,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC;AAG9B,MAAM,KAAK,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;AAMtC,SAAS,SAAS,CAAC,KAAU;IAC5B,OAAO,KAAK,IAAK,KAAwB,CAAC,QAAQ,KAAK,SAAS,CAAC;AAClE,CAAC;AAED,SAAgB,QAAQ,CAAC,aAAkB;IAC1C,KAAK,MAAM,cAAc,IAAI,aAAa,EAAE;QAC3C,IAAI,CAAC,aAAa,CAAC,cAAc,CAAC,cAAc,CAAC,EAAE;YAClD,SAAS;SACT;QAED,MAAM,KAAK,GAAG,aAAa,CAAC,cAAc,CAAC,CAAC;QAC5C,IAAI,SAAS,CAAC,KAAK,CAAC,EAAE;YACrB,KAAK,CAAC,QAAQ,EAAE,CAAC;SACjB;KACD;AACF,CAAC;AAXD,4BAWC;AAED,SAAgB,iBAAiB,CAAC,aAAkB,EAAE,YAA+B,EAAE,SAAS,GAAG,UAAU;IAC5G,KAAK,MAAM,MAAM,IAAI,YAAY,EAAE;QAClC,IAAI,CAAC,YAAY,CAAC,cAAc,CAAC,MAAM,CAAC;eACpC,OAAO,YAAY,CAAC,MAAM,CAAC,KAAK,QAAQ;eACxC,CAAC,MAAM,CAAC,UAAU,CAAC,SAAS,CAAC;eAC7B,MAAM,CAAC,M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;YACxC,IAAI,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE;gBAC5B,OAAO,KAAK,CAAC;aACb;SACD;KACD;IAED,OAAO,IAAI,CAAC;AACb,CAAC;AAlDD,0CAkDC;AAED,SAAS,SAAS,CAAC,YAAoB;IACtC,YAAY,GAAG,YAAY,CAAC,OAAO,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC,CAAC,gCAAgC;IAC1F,YAAY,GAAG,YAAY,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAW,sBAAsB;IAChF,OAAO,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;AAChC,CAAC;AAED,SAAS,SAAS,CAAC,KAAa;IAC/B,2CAA2C;IAC3C,MAAM,SAAS,GAAG,0QAA0Q,CAAC;IAC7R,OAAO,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC9B,CAAC"}
	const CONFIG_PROP = 'config';

	if (parsedArgv[CONFIG_PROP]) {
		const configFile = path.resolve(process.cwd(), parsedArgv[CONFIG_PROP]);
		if (key.startsWith('_')) {
			continue;
		}
		if (key.endsWith('_')) {
			continue;
		}
		if (key === CONFIG_PROP) {
			continue;
		}

export function setDeepProperty(obj: {[key: string]: any}, propertyPath: string, value: any): void {
	if (!obj) {
		throw new Error("Invalid object");
	}
	if (!propertyPath) {
		throw new Error("Invalid property path");
	}
	const pathParts = splitPath(propertyPath);
	const pathPartsLen = pathParts.length;

	for (let i = 0; i < pathPartsLen - 1; i++) {
		const pathPart = pathParts[i];
		if (!(pathPart in obj)) {
			setProp(obj, pathPart, {});
		obj = getProp(obj, pathPart);
	setProp(obj, pathParts[pathPartsLen - 1], value);
function setProp(obj: {[key: string]: any}, property: string, value: any): void {
	if (!obj.hasOwnProperty(property)) {
		throw new Error(`Property '${property}' is not valid`);
	}
	obj[property] = value;
}

function getProp(obj: {[key: string]: any}, property: string): any {
	if (!obj.hasOwnProperty(property)) {
		throw new Error(`Property '${property}' is not valid`);
	}
	return obj[property];
}

// See README.md for details
        // Enable config file
        // Enable environment variables
        // Enable command arguments
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";AAAA,4BAA4B;;AAE5B,qCAAqC;AACrC,6BAA6B;AAE7B,MAAa,cAAc;IAA3B;QACC,QAAG,GAAW,EAAE,CAAC;IAOlB,CAAC;IALA,QAAQ;QACP,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE;YACd,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;SAC1C;IACF,CAAC;CACD;AARD,wCAQC;AAED,MAAa,eAAe;IAG3B;QAFA,SAAI,GAAG,IAAI,CAAC;QAGX,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC;QACjC,IAAI,OAAO,EAAE;YACZ,IAAI,CAAC,IAAI,GAAG,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;SAClC;IACF,CAAC;IAED,QAAQ;QACP,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE;YACf,MAAM,IAAI,KAAK,CAAC,cAAc,CAAC,CAAC;SAChC;QACD,IAAI,CAAC,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE,EAAE,CAAC,CAAC;IAChD,CAAC;CACD;AAhBD,0CAgBC;AAED,MAAa,aAAa;IAIzB,YAAY,GAAuB;QAH1B,aAAQ,GAAG,IAAI,cAAc,EAAE,CAAC;QAChC,cAAS,GAAG,IAAI,eAAe,EAAE,CAAC;QAG1C,IAAI,CAAC,GAAG,EAAE;YACT,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;SAClB;QAED,qBAAqB;QACrB,IAAI,GAAG,CAAC,MAAM,EAAE;YACf,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;YAC3D,QAAQ,CAAC,eAAe,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;SAC3C;QACD,+BAA+B;QAC/B,QAAQ,CAAC,iBAAiB,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QACtD,2BAA2B;QAC3B,QAAQ,CAAC,gBAAgB,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;QAE9C,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC;CACD;AArBD,sCAqBC;AAED,OAAO,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;AACpC,MAAM,MAAM,GAAG,IAAI,aAAa,EAAE,CAAC;AAEnC,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;AAC/B,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;AACzB,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;AAC/B,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAC7B,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;AAC/B,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;AAC1B,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;AAC/B,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC"}
// See README.md for details


		// Enable config file
		// Enable environment variables
		// Enable command arguments
const index_1 = require("../index");
        index_1.setDeepProperty(obj, "test", "B");
        index_1.setDeepProperty(obj, "parent.test", "B");
    it('should not allow to set a not existing property', () => {
        const obj = {
            test: "A"
        };
        assert.throws(() => index_1.setDeepProperty(obj, "not_existing", "B"));
    });
    it('should not allow to set a property on null/undefined obj', () => {
        assert.throws(() => index_1.setDeepProperty(null, "not_existing", "B"));
        assert.throws(() => index_1.setDeepProperty(undefined, "not_existing", "B"));
    });
    it('should not allow to set a null/undefined property', () => {
        const obj = {
            test: "A"
        };
        assert.throws(() => index_1.setDeepProperty(obj, null, "B"));
        assert.throws(() => index_1.setDeepProperty(obj, undefined, "B"));
        assert.throws(() => index_1.setDeepProperty(obj, "", "B"));
    });
    it('should not allow to set __proto__ property', () => {
        const obj = {
            test: "A"
        };
        assert.throws(() => index_1.setDeepProperty(obj, "__proto__.xyz", "B"));
    });
{"version":3,"file":"setDeepProperty.test.js","sourceRoot":"","sources":["setDeepProperty.test.ts"],"names":[],"mappings":";;AAAA,iCAAiC;AACjC,oCAA2C;AAE3C,QAAQ,CAAC,iBAAiB,EAAE,GAAG,EAAE;IAE/B,EAAE,CAAC,mCAAmC,EAAE,GAAG,EAAE;QAC3C,MAAM,GAAG,GAAG;YACV,IAAI,EAAE,GAAG;SACV,CAAA;QACD,uBAAe,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC;QAClC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAC9B,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,mCAAmC,EAAE,GAAG,EAAE;QAC3C,MAAM,GAAG,GAAG;YACV,MAAM,EAAE;gBACN,IAAI,EAAE,GAAG;aACV;SACF,CAAA;QACD,uBAAe,CAAC,GAAG,EAAE,aAAa,EAAE,GAAG,CAAC,CAAC;QACzC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IACrC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,iDAAiD,EAAE,GAAG,EAAE;QACzD,MAAM,GAAG,GAAG;YACV,IAAI,EAAE,GAAG;SACV,CAAA;QACD,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,uBAAe,CAAC,GAAG,EAAE,cAAc,EAAE,GAAG,CAAC,CAAC,CAAC;IACjE,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,0DAA0D,EAAE,GAAG,EAAE;QAClE,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,uBAAe,CAAC,IAAW,EAAE,cAAc,EAAE,GAAG,CAAC,CAAC,CAAC;QACvE,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,uBAAe,CAAC,SAAgB,EAAE,cAAc,EAAE,GAAG,CAAC,CAAC,CAAC;IAC9E,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,mDAAmD,EAAE,GAAG,EAAE;QAC3D,MAAM,GAAG,GAAG;YACV,IAAI,EAAE,GAAG;SACV,CAAA;QACD,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,uBAAe,CAAC,GAAG,EAAE,IAAW,EAAE,GAAG,CAAC,CAAC,CAAC;QAC5D,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,uBAAe,CAAC,GAAG,EAAE,SAAgB,EAAE,GAAG,CAAC,CAAC,CAAC;QACjE,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,uBAAe,CAAC,GAAG,EAAE,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC;IACrD,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,4CAA4C,EAAE,GAAG,EAAE;QACpD,MAAM,GAAG,GAAG;YACV,IAAI,EAAE,GAAG;SACV,CAAA;QACD,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,uBAAe,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,CAAC,CAAC,CAAC;IAClE,CAAC,CAAC,CAAC;AAEL,CAAC,CAAC,CAAC"}
import { setDeepProperty } from '../index';
  it('should not allow to set a not existing property', () => {
    const obj = {
      test: "A"
    }
    assert.throws(() => setDeepProperty(obj, "not_existing", "B"));
  });

  it('should not allow to set a property on null/undefined obj', () => {
    assert.throws(() => setDeepProperty(null as any, "not_existing", "B"));
    assert.throws(() => setDeepProperty(undefined as any, "not_existing", "B"));
  });

  it('should not allow to set a null/undefined property', () => {
    const obj = {
      test: "A"
    }
    assert.throws(() => setDeepProperty(obj, null as any, "B"));
    assert.throws(() => setDeepProperty(obj, undefined as any, "B"));
    assert.throws(() => setDeepProperty(obj, "", "B"));
  });

  it('should not allow to set __proto__ property', () => {
    const obj = {
      test: "A"
    }
    assert.throws(() => setDeepProperty(obj, "__proto__.xyz", "B"));
  });