Skip to content

Commit 7ad6af4

Browse files
Merge pull request #262 from 418sec/1-npm-fast-json-patch
Security Fix for Prototype Pollution - huntr.dev
2 parents 34d6405 + 5edc97d commit 7ad6af4

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

commonjs/core.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,10 @@ function applyOperation(document, operation, validateOperation, mutateDocument,
188188
if (key && key.indexOf('~') != -1) {
189189
key = helpers_js_1.unescapePathComponent(key);
190190
}
191-
if (banPrototypeModifications && key == '__proto__') {
192-
throw new TypeError('JSON-Patch: modifying `__proto__` prop is banned for security reasons, if this was on purpose, please set `banPrototypeModifications` flag false and pass it to this function. More info in fast-json-patch README');
191+
if (banPrototypeModifications &&
192+
(key == '__proto__' ||
193+
(key == 'prototype' && t > 0 && keys[t - 1] == 'constructor'))) {
194+
throw new TypeError('JSON-Patch: modifying `__proto__` or `constructor/prototype` prop is banned for security reasons, if this was on purpose, please set `banPrototypeModifications` flag false and pass it to this function. More info in fast-json-patch README');
193195
}
194196
if (validateOperation) {
195197
if (existingPathFragment === undefined) {

module/core.mjs

+4-2
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,10 @@ export function applyOperation(document, operation, validateOperation, mutateDoc
186186
if (key && key.indexOf('~') != -1) {
187187
key = unescapePathComponent(key);
188188
}
189-
if (banPrototypeModifications && key == '__proto__') {
190-
throw new TypeError('JSON-Patch: modifying `__proto__` prop is banned for security reasons, if this was on purpose, please set `banPrototypeModifications` flag false and pass it to this function. More info in fast-json-patch README');
189+
if (banPrototypeModifications &&
190+
(key == '__proto__' ||
191+
(key == 'prototype' && t > 0 && keys[t - 1] == 'constructor'))) {
192+
throw new TypeError('JSON-Patch: modifying `__proto__` or `constructor/prototype` prop is banned for security reasons, if this was on purpose, please set `banPrototypeModifications` flag false and pass it to this function. More info in fast-json-patch README');
191193
}
192194
if (validateOperation) {
193195
if (existingPathFragment === undefined) {

src/core.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,11 @@ export function applyOperation<T>(document: T, operation: Operation, validateOpe
251251
key = unescapePathComponent(key);
252252
}
253253

254-
if(banPrototypeModifications && key == '__proto__') {
255-
throw new TypeError('JSON-Patch: modifying `__proto__` prop is banned for security reasons, if this was on purpose, please set `banPrototypeModifications` flag false and pass it to this function. More info in fast-json-patch README');
254+
if(banPrototypeModifications &&
255+
(key == '__proto__' ||
256+
(key == 'prototype' && t>0 && keys[t-1] == 'constructor'))
257+
) {
258+
throw new TypeError('JSON-Patch: modifying `__proto__` or `constructor/prototype` prop is banned for security reasons, if this was on purpose, please set `banPrototypeModifications` flag false and pass it to this function. More info in fast-json-patch README');
256259
}
257260

258261
if (validateOperation) {

0 commit comments

Comments
 (0)