Skip to content

Commit

Permalink
fix: only emit prototype pollution error when there's metadata emitted
Browse files Browse the repository at this point in the history
  • Loading branch information
Skn0tt committed Dec 5, 2023
1 parent 0130a80 commit 99d8a14
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ test.each(['__proto__', 'prototype', 'constructor'])(
forbidden => {
expect(() => {
SuperJSON.serialize({
[forbidden]: 1,
[forbidden]: NaN,
});
}).toThrowError(/This is a prototype pollution risk/);
}
Expand Down Expand Up @@ -1229,3 +1229,8 @@ test('dedupe=true on a large complicated schema', () => {
expect(nondedupedOut).toEqual(deserialized);
expect(dedupedOut).toEqual(deserialized);
});

test("prototype pollution detector doesn't trigger when there's no meta", () => {
const { meta } = SuperJSON.serialize({ constructor: { name: 'hello' } });
expect(meta).toBeUndefined();
});
22 changes: 12 additions & 10 deletions src/plainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,6 @@ export const walker = (
const innerAnnotations: Record<string, Tree<TypeAnnotation>> = {};

forEach(transformed, (value, index) => {
if (
index === '__proto__' ||
index === 'constructor' ||
index === 'prototype'
) {
throw new Error(
`Detected property ${index}. This is a prototype pollution risk, please remove it from your object.`
);
}

const recursiveResult = walker(
value,
identities,
Expand All @@ -236,6 +226,18 @@ export const walker = (
seenObjects
);

const emitsMeta = recursiveResult.annotations !== undefined;
if (
emitsMeta &&
(index === '__proto__' ||
index === 'constructor' ||
index === 'prototype')
) {
throw new Error(
`Detected property ${index}. This is a prototype pollution risk, please remove it from your object.`
);
}

transformedValue[index] = recursiveResult.transformedValue;

if (isArray(recursiveResult.annotations)) {
Expand Down

0 comments on commit 99d8a14

Please sign in to comment.