Skip to content

Commit

Permalink
fix: throw error if parts contains an element that isn't a string o…
Browse files Browse the repository at this point in the history
…r number

Fix #13
  • Loading branch information
vkarpov15 committed Sep 1, 2021
1 parent 03c4efe commit 89402d2
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 89402d2

Please # to comment.