Skip to content

Commit 89402d2

Browse files
committed
fix: throw error if parts contains an element that isn't a string or number
Fix #13
1 parent 03c4efe commit 89402d2

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lib/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ exports.get = function(path, o, special, map) {
6464

6565
for (var i = 0; i < parts.length; ++i) {
6666
part = parts[i];
67+
if (typeof parts[i] !== 'string' && typeof parts[i] !== 'number') {
68+
throw new TypeError('Each segment of path to `get()` must be a string or number, got ' + typeof parts[i]);
69+
}
6770

6871
if (Array.isArray(obj) && !/^\d+$/.test(part)) {
6972
// reading a property from the array items
@@ -112,6 +115,9 @@ exports.has = function(path, o) {
112115
var len = parts.length;
113116
var cur = o;
114117
for (var i = 0; i < len; ++i) {
118+
if (typeof parts[i] !== 'string' && typeof parts[i] !== 'number') {
119+
throw new TypeError('Each segment of path to `has()` must be a string or number, got ' + typeof parts[i]);
120+
}
115121
if (cur == null || typeof cur !== 'object' || !(parts[i] in cur)) {
116122
return false;
117123
}
@@ -143,6 +149,9 @@ exports.unset = function(path, o) {
143149
if (cur == null || typeof cur !== 'object' || !(parts[i] in cur)) {
144150
return false;
145151
}
152+
if (typeof parts[i] !== 'string' && typeof parts[i] !== 'number') {
153+
throw new TypeError('Each segment of path to `unset()` must be a string or number, got ' + typeof parts[i]);
154+
}
146155
// Disallow any updates to __proto__ or special properties.
147156
if (ignoreProperties.indexOf(parts[i]) !== -1) {
148157
return false;
@@ -193,6 +202,9 @@ exports.set = function(path, val, o, special, map, _copying) {
193202
if (null == o) return;
194203

195204
for (var i = 0; i < parts.length; ++i) {
205+
if (typeof parts[i] !== 'string' && typeof parts[i] !== 'number') {
206+
throw new TypeError('Each segment of path to `set()` must be a string or number, got ' + typeof parts[i]);
207+
}
196208
// Silently ignore any updates to `__proto__`, these are potentially
197209
// dangerous if using mpath with unsanitized data.
198210
if (ignoreProperties.indexOf(parts[i]) !== -1) {

0 commit comments

Comments
 (0)