Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reproduce prototype pollution #284

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions src/accessDeep.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { isMap, isArray, isPlainObject, isSet } from './is.js';
import { includes } from './util.js';

const getNthKey = (value: Map<any, any> | Set<any>, n: number): any => {
const keys = value.keys();
Expand All @@ -11,21 +10,7 @@ const getNthKey = (value: Map<any, any> | Set<any>, n: number): any => {
return keys.next().value;
};

function validatePath(path: (string | number)[]) {
if (includes(path, '__proto__')) {
throw new Error('__proto__ is not allowed as a property');
}
if (includes(path, 'prototype')) {
throw new Error('prototype is not allowed as a property');
}
if (includes(path, 'constructor')) {
throw new Error('constructor is not allowed as a property');
}
}

export const getDeep = (object: object, path: (string | number)[]): object => {
validatePath(path);

for (let i = 0; i < path.length; i++) {
const key = path[i];
if (isSet(object)) {
Expand Down Expand Up @@ -56,8 +41,6 @@ export const setDeep = (
path: (string | number)[],
mapper: (v: any) => any
): any => {
validatePath(path);

if (path.length === 0) {
return mapper(object);
}
Expand Down Expand Up @@ -101,6 +84,7 @@ export const setDeep = (
if (isArray(parent)) {
parent[+lastKey] = mapper(parent[+lastKey]);
} else if (isPlainObject(parent)) {
console.log(parent, lastKey, mapper(parent[lastKey]));
parent[lastKey] = mapper(parent[lastKey]);
}

Expand Down
70 changes: 16 additions & 54 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1054,17 +1054,6 @@ test('regression: `Object.create(null)` / object without prototype', () => {
expect(parsed.date).toBeInstanceOf(Date);
});

test.each(['__proto__', 'prototype', 'constructor'])(
'serialize prototype pollution: %s',
forbidden => {
expect(() => {
SuperJSON.serialize({
[forbidden]: 1,
});
}).toThrowError(/This is a prototype pollution risk/);
}
);

test('prototype pollution - __proto__', () => {
expect(() => {
SuperJSON.parse(
Expand All @@ -1079,49 +1068,7 @@ test('prototype pollution - __proto__', () => {
},
})
);
}).toThrowErrorMatchingInlineSnapshot(
`"__proto__ is not allowed as a property"`
);
expect((Object.prototype as any).x).toBeUndefined();
});

test('prototype pollution - prototype', () => {
expect(() => {
SuperJSON.parse(
JSON.stringify({
json: {
myValue: 1337,
},
meta: {
referentialEqualities: {
myValue: ['prototype.x'],
},
},
})
);
}).toThrowErrorMatchingInlineSnapshot(
`"prototype is not allowed as a property"`
);
});

test('prototype pollution - constructor', () => {
expect(() => {
SuperJSON.parse(
JSON.stringify({
json: {
myValue: 1337,
},
meta: {
referentialEqualities: {
myValue: ['constructor.prototype.x'],
},
},
})
);
}).toThrowErrorMatchingInlineSnapshot(
`"prototype is not allowed as a property"`
);

});
expect((Object.prototype as any).x).toBeUndefined();
});

Expand Down Expand Up @@ -1229,3 +1176,18 @@ test('dedupe=true on a large complicated schema', () => {
expect(nondedupedOut).toEqual(deserialized);
expect(dedupedOut).toEqual(deserialized);
});

test.only('repro prototype pollution', () => {
SuperJSON.deserialize({
json: {
a: {},
maliciousProperty: 'pwned',
},
meta: {
referentialEqualities: {
maliciousProperty: ['a.__proto__.test'],
},
},
});
expect(({} as any).test).toEqual('pwned');
});
Loading