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

[ML] Various fixes for possible prototype pollution vulnerabilities #194529

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,15 @@ describe('object_utils', () => {

const test11 = setNestedProperty(getFalseyObject(), 'the.other_nested.value', 'update');
expect(test11.the.other_nested.value).toBe('update');

expect(() => {
setNestedProperty(getTestObj(), 'the.__proto__', 'update');
}).toThrow('Invalid accessor');
expect(() => {
setNestedProperty(getTestObj(), 'the.prototype', 'update');
}).toThrow('Invalid accessor');
expect(() => {
setNestedProperty(getTestObj(), 'the.constructor', 'update');
}).toThrow('Invalid accessor');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
* 2.0.
*/

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
4 changes: 2 additions & 2 deletions x-pack/plugins/ml/server/saved_objects/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export function mlSavedObjectServiceFactory(
if (id.match('\\*') === null) {
return jobIds.includes(id);
}
const regex = new RegExp(id.replace('*', '.*'));
const regex = new RegExp(id.replaceAll('*', '.*'));
return jobIds.some((jId) => typeof jId === 'string' && regex.exec(jId));
});
}
Expand Down Expand Up @@ -640,7 +640,7 @@ export function mlSavedObjectServiceFactory(
if (id.match('\\*') === null) {
return modelIds.includes(id);
}
const regex = new RegExp(id.replace('*', '.*'));
const regex = new RegExp(id.replaceAll('*', '.*'));
return modelIds.some((jId) => typeof jId === 'string' && regex.exec(jId));
});
}
Expand Down