Skip to content

Commit

Permalink
Merge pull request #51 from dellalibera/fix-prototype-pollution
Browse files Browse the repository at this point in the history
fix-prototype-pollution
  • Loading branch information
janl authored Oct 31, 2021
2 parents b67e402 + 00c9afe commit a0345f3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
10 changes: 7 additions & 3 deletions jsonpointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ function setter (obj, pointer, value) {
var part
var hasNextPart

if (pointer[1] === 'constructor' && pointer[2] === 'prototype') return obj
if (pointer[1] === '__proto__') return obj

for (var p = 1, len = pointer.length; p < len;) {
if (pointer[p] === 'constructor' || pointer[p] === 'prototype' || pointer[p] === '__proto__') return obj

part = untilde(pointer[p++])
hasNextPart = len > p

Expand Down Expand Up @@ -53,6 +52,11 @@ function compilePointer (pointer) {
if (pointer[0] === '') return pointer
throw new Error('Invalid JSON pointer.')
} else if (Array.isArray(pointer)) {
for (const part of pointer) {
if (typeof part !== 'string' && typeof part !== 'number') {
throw new Error('Invalid JSON pointer. Must be of type string or number.')
}
}
return pointer
}

Expand Down
24 changes: 24 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,28 @@ var c = {}
jsonpointer.set({}, '/__proto__/boo', 'polluted')
assert(!c.boo, 'should not boo')

var d = {}
jsonpointer.set({}, '/foo/__proto__/boo', 'polluted')
assert(!d.boo, 'should not boo')

jsonpointer.set({}, '/foo/__proto__/__proto__/boo', 'polluted')
assert(!d.boo, 'should not boo')

var e = {}
jsonpointer.set({}, '/foo/constructor/prototype/boo', 'polluted')
assert(!e.boo, 'should not boo')

jsonpointer.set({}, '/foo/constructor/constructor/prototype/boo', 'polluted')
assert(!e.boo, 'should not boo')

assert.throws(function () { jsonpointer.set({}, [['__proto__'], 'boo'], 'polluted')}, validateError)
assert.throws(function () { jsonpointer.set({}, [[['__proto__']], 'boo'], 'polluted')}, validateError)
assert.throws(function () { jsonpointer.set({}, [['__proto__'], ['__proto__'], 'boo'], 'polluted')}, validateError)
assert.throws(function () { jsonpointer.set({}, [[['__proto__']], [['__proto__']], 'boo'], 'polluted')}, validateError)
assert.throws(function () { jsonpointer.set({}, [['__proto__'], ['__proto__'], ['__proto__'], 'boo'], 'polluted')}, validateError)
assert.throws(function () { jsonpointer.set({}, [['foo'], ['__proto__'], 'boo'], 'polluted')}, validateError)
assert.throws(function () { jsonpointer.set({}, [['foo'], ['__proto__'], ['__proto__'], 'boo'], 'polluted')}, validateError)
assert.throws(function () { jsonpointer.set({}, [['constructor'], ['prototype'], 'boo'], 'polluted')}, validateError)
assert.throws(function () { jsonpointer.set({}, [['constructor'], ['constructor'], ['prototype'], 'boo'], 'polluted')}, validateError)

console.log('All tests pass.')

0 comments on commit a0345f3

Please # to comment.