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

fix: only throw error on field with annotation #274

Closed
wants to merge 1 commit 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
8 changes: 7 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,13 @@ test.each(['__proto__', 'prototype', 'constructor'])(
forbidden => {
expect(() => {
SuperJSON.serialize({
[forbidden]: 1,
[forbidden]: 1, // doesn't need entry in `meta`, so it's not a risk
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonluca I believe this test case should cover your example from #267 (comment).

});
}).not.toThrowError();

expect(() => {
SuperJSON.serialize({
[forbidden]: new Date(), // needs an entry in `meta`, so it's a risk
});
}).toThrowError(/This is a prototype pollution risk/);
}
Expand Down
14 changes: 2 additions & 12 deletions src/plainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export const walker = (
}

if (!isDeep(object, superJson)) {
const transformed = transformValue(object, superJson);
const transformed = transformValue(object, superJson, path);

const result: Result = transformed
? {
Expand All @@ -209,23 +209,13 @@ export const walker = (
};
}

const transformationResult = transformValue(object, superJson);
const transformationResult = transformValue(object, superJson, path);
const transformed = transformationResult?.value ?? object;

const transformedValue: any = isArray(transformed) ? [] : {};
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
21 changes: 20 additions & 1 deletion src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from './is.js';
import { findArr } from './util.js';
import SuperJSON from './index.js';
import { stringifyPath } from './pathstringifier.js';

export type PrimitiveTypeAnnotation = 'number' | 'undefined' | 'bigint';

Expand Down Expand Up @@ -310,14 +311,31 @@ const customRule = compositeTransformation(

const compositeRules = [classRule, symbolRule, customRule, typedArrayRule];

const checkForPrototypePollution = (path: any[]) => {
const index = path[path.length - 1];
if (
index === '__proto__' ||
index === 'constructor' ||
index === 'prototype'
) {
throw new Error(
`Detected transformable property ${stringifyPath(
path
)}. This is a prototype pollution risk, please remove it from your object.`
);
}
};

export const transformValue = (
value: any,
superJson: SuperJSON
superJson: SuperJSON,
path: any[]
): { value: any; type: TypeAnnotation } | undefined => {
const applicableCompositeRule = findArr(compositeRules, rule =>
rule.isApplicable(value, superJson)
);
if (applicableCompositeRule) {
checkForPrototypePollution(path);
return {
value: applicableCompositeRule.transform(value as never, superJson),
type: applicableCompositeRule.annotation(value, superJson),
Expand All @@ -329,6 +347,7 @@ export const transformValue = (
);

if (applicableSimpleRule) {
checkForPrototypePollution(path);
return {
value: applicableSimpleRule.transform(value as never, superJson),
type: applicableSimpleRule.annotation,
Expand Down
Loading