Skip to content

Commit

Permalink
Strip constructor, prototype, and __proto__ properties in the seriali…
Browse files Browse the repository at this point in the history
…ze step (#267)

* Strip constructor, prototype, and __proto__ properties in the serialize step

* Update src/plainer.ts

Co-authored-by: Simon Knott <[email protected]>

---------

Co-authored-by: Simon Knott <[email protected]>
  • Loading branch information
tmcw and Skn0tt authored Oct 23, 2023
1 parent 5e01a01 commit 47bf57d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,17 @@ 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 Down
10 changes: 10 additions & 0 deletions src/plainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ 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 Down

0 comments on commit 47bf57d

Please sign in to comment.