Skip to content

Commit

Permalink
simple fix for prototype pollution
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-tatar committed Mar 19, 2024
1 parent 80723ba commit bf30b75
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/update-state.test.ts
Expand Up @@ -222,5 +222,14 @@ describe('updateState', () => {
}],
});
});

it('should not update via prototype props', () => {
var BAD_JSON = JSON.parse('{"__proto__":{"polluted":true}}')

const { hasChanges } = updateState(BAD_JSON, {})

expect(hasChanges).to.be.false;
expect((Object.prototype as any).polluted).to.be.undefined;
});
});

5 changes: 5 additions & 0 deletions src/update-state.ts
Expand Up @@ -16,6 +16,9 @@ const arrayItemKeyMap = new Map<string, string>([
['currentSensorStateData', 'name'],
]);

// https://github.com/andrei-tatar/nora-firebase-common/issues/9
const ignoreKeys: any[] = ['__proto__', 'constructor', 'prototype'];

function updateArrayState(update: any[], state: any[], path = ''): boolean {
let hasChanges = false;

Expand Down Expand Up @@ -51,6 +54,8 @@ function updateStateInternal(update: any, state: any, path = ''): boolean {

let hasChanges = false;
for (const [key, newValue] of entries(update)) {
if (ignoreKeys.includes(key)) { continue; }

const oldValue = state[key];
const newType = typeof newValue;
const oldType = typeof oldValue;
Expand Down

0 comments on commit bf30b75

Please sign in to comment.