From 89402d2880d4ea3518480a8c9847c541f2d824fc Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 1 Sep 2021 11:12:24 -0400 Subject: [PATCH] fix: throw error if `parts` contains an element that isn't a string or number Fix #13 --- lib/index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/index.js b/lib/index.js index 338bf9b..c9e7e5f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -64,6 +64,9 @@ exports.get = function(path, o, special, map) { for (var i = 0; i < parts.length; ++i) { part = parts[i]; + if (typeof parts[i] !== 'string' && typeof parts[i] !== 'number') { + throw new TypeError('Each segment of path to `get()` must be a string or number, got ' + typeof parts[i]); + } if (Array.isArray(obj) && !/^\d+$/.test(part)) { // reading a property from the array items @@ -112,6 +115,9 @@ exports.has = function(path, o) { var len = parts.length; var cur = o; for (var i = 0; i < len; ++i) { + if (typeof parts[i] !== 'string' && typeof parts[i] !== 'number') { + throw new TypeError('Each segment of path to `has()` must be a string or number, got ' + typeof parts[i]); + } if (cur == null || typeof cur !== 'object' || !(parts[i] in cur)) { return false; } @@ -143,6 +149,9 @@ exports.unset = function(path, o) { if (cur == null || typeof cur !== 'object' || !(parts[i] in cur)) { return false; } + if (typeof parts[i] !== 'string' && typeof parts[i] !== 'number') { + throw new TypeError('Each segment of path to `unset()` must be a string or number, got ' + typeof parts[i]); + } // Disallow any updates to __proto__ or special properties. if (ignoreProperties.indexOf(parts[i]) !== -1) { return false; @@ -193,6 +202,9 @@ exports.set = function(path, val, o, special, map, _copying) { if (null == o) return; for (var i = 0; i < parts.length; ++i) { + if (typeof parts[i] !== 'string' && typeof parts[i] !== 'number') { + throw new TypeError('Each segment of path to `set()` must be a string or number, got ' + typeof parts[i]); + } // Silently ignore any updates to `__proto__`, these are potentially // dangerous if using mpath with unsanitized data. if (ignoreProperties.indexOf(parts[i]) !== -1) {