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: Prototype Pollution Vulnerability Affecting redoc <=2.2.0 #2638

Merged
merged 2 commits into from
Jan 28, 2025
Merged
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
24 changes: 24 additions & 0 deletions src/utils/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ describe('Utils', () => {
const obj2 = { a: ['C'], b: ['D'] };
expect(mergeObjects({}, obj1, obj2)).toEqual({ a: ['C'], b: ['D'] });
});
test('should prevent prototype pollution', () => {
const target = {};
const source = JSON.parse('{"__proto__": {"polluted": "yes"}}');

mergeObjects(target, source);

expect(({} as any).polluted).toBeUndefined();
});
test('should merge objects correctly', () => {
const target = { a: 1 };
const source = { b: 2 };

const result = mergeObjects(target, source);

expect(result).toEqual({ a: 1, b: 2 });
});
test('should handle nested objects', () => {
const target = { a: { b: 1 } };
const source = { a: { c: 2 } };

const result = mergeObjects(target, source);

expect(result).toEqual({ a: { b: 1, c: 2 } });
});
});

describe('titleize', () => {
Expand Down
15 changes: 8 additions & 7 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export function appendToMdHeading(md: string, heading: string, content: string)
}
}

// credits https://stackoverflow.com/a/46973278/1749888
export const mergeObjects = (target: any, ...sources: any[]): any => {
if (!sources.length) {
return target;
Expand All @@ -93,13 +92,15 @@ export const mergeObjects = (target: any, ...sources: any[]): any => {

if (isMergebleObject(target) && isMergebleObject(source)) {
Object.keys(source).forEach((key: string) => {
if (isMergebleObject(source[key])) {
if (!target[key]) {
target[key] = {};
if (Object.prototype.hasOwnProperty.call(source, key) && key !== '__proto__') {
if (isMergebleObject(source[key])) {
if (!target[key]) {
target[key] = {};
}
mergeObjects(target[key], source[key]);
} else {
target[key] = source[key];
}
mergeObjects(target[key], source[key]);
} else {
target[key] = source[key];
}
});
}
Expand Down
Loading