Skip to content

Commit

Permalink
[ML] [7.17] Potential prototype pollution vulnerability (#194538)
Browse files Browse the repository at this point in the history
Fixes potential prototype pollution vulnerability in `setNestedProperty`
function.
  • Loading branch information
jgowdyelastic authored Oct 2, 2024
1 parent c5a6128 commit 4ab101a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 11 additions & 1 deletion x-pack/plugins/transform/common/utils/object_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { getNestedProperty } from './object_utils';
import { getNestedProperty, setNestedProperty } from './object_utils';

describe('object_utils', () => {
test('getNestedProperty()', () => {
Expand Down Expand Up @@ -67,5 +67,15 @@ describe('object_utils', () => {
const test11 = getNestedProperty(falseyObj, 'the.other_nested.value');
expect(typeof test11).toBe('number');
expect(test11).toBe(0);

expect(() => {
setNestedProperty(testObj, 'the.__proto__', 'update');
}).toThrow('Invalid accessor');
expect(() => {
setNestedProperty(testObj, 'the.prototype', 'update');
}).toThrow('Invalid accessor');
expect(() => {
setNestedProperty(testObj, 'the.constructor', 'update');
}).toThrow('Invalid accessor');
});
});
6 changes: 6 additions & 0 deletions x-pack/plugins/transform/common/utils/object_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ export function getNestedProperty(
return o;
}

const INVALID_ACCESSORS = ['__proto__', 'prototype', 'constructor'];

export const setNestedProperty = (obj: Record<string, any>, accessor: string, value: any) => {
let ref = obj;
const accessors = accessor.split('.');
if (accessors.some((a) => INVALID_ACCESSORS.includes(a))) {
throw new Error('Invalid accessor');
}

const len = accessors.length;
for (let i = 0; i < len - 1; i++) {
const attribute = accessors[i];
Expand Down

0 comments on commit 4ab101a

Please sign in to comment.