Skip to content

Commit

Permalink
[ML] Improving empty object creation in ML packages (elastic#191901)
Browse files Browse the repository at this point in the history
Replacing instances of empty object creation with Object.create(null) to
remove any risk of prototype pollution.
Related to elastic#191518
  • Loading branch information
jgowdyelastic authored Sep 3, 2024
1 parent bac95ea commit e204184
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function getFieldValuePairCounts(cpgs: SignificantItemGroup[]): FieldValu
return cpgs.reduce<FieldValuePairCounts>((p, { group }) => {
group.forEach(({ fieldName, fieldValue }) => {
if (p[fieldName] === undefined) {
p[fieldName] = {};
p[fieldName] = Object.create(null);
}
p[fieldName][fieldValue] = p[fieldName][fieldValue] ? p[fieldName][fieldValue] + 1 : 1;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ describe('isPopulatedObject', () => {
])
).toBe(false);
});
it('does not allow an object with a required attribute in the prototype ', () => {
const testObject = { attribute: 'value', __proto__: { otherAttribute: 'value' } };
expect(isPopulatedObject(testObject, ['otherAttribute'])).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const isPopulatedObject = <U extends string = string, T extends unknown =
typeof arg === 'object' &&
arg !== null &&
Object.keys(arg).length > 0 &&
(requiredAttributes.length === 0 ||
requiredAttributes.every((d) => ({}.hasOwnProperty.call(arg, d))))
(requiredAttributes.length === 0 || requiredAttributes.every((d) => Object.hasOwn(arg, d)))
);
};
2 changes: 1 addition & 1 deletion x-pack/packages/ml/json_schemas/src/json_schema_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class JsonSchemaService {
};
}

private allComponents: Record<string, object> = {};
private allComponents: Record<string, object> = Object.create(null);
private componentsDict = new Set<string>();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const setNestedProperty = (obj: Record<string, any>, accessor: string, va
for (let i = 0; i < len - 1; i++) {
const attribute = accessors[i];
if (typeof ref[attribute] !== 'object') {
ref[attribute] = {};
ref[attribute] = Object.create(null);
}

ref = ref[attribute];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export class RandomSampler {
const mode = this.getMode();
const probability = this.getProbability();

let prob = {};
let prob = Object.create(null);
if (mode === RANDOM_SAMPLER_OPTION.ON_MANUAL) {
prob = { probability };
} else if (mode === RANDOM_SAMPLER_OPTION.OFF) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function getCombinedRuntimeMappings(
dataView: DataView | undefined,
runtimeMappings?: RuntimeMappings
): RuntimeMappings | undefined {
let combinedRuntimeMappings = {};
let combinedRuntimeMappings = Object.create(null);

// Add runtime field mappings defined by index pattern
if (dataView) {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/packages/ml/url_state/src/url_state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function isRisonSerializationRequired(queryParam: string): boolean {
}

export function parseUrlState(search: string): Dictionary<any> {
const urlState: Dictionary<any> = {};
const urlState: Dictionary<any> = Object.create(null);
const parsedQueryString = parse(search, { sort: false });

try {
Expand Down Expand Up @@ -125,7 +125,7 @@ export const UrlStateProvider: FC<PropsWithChildren<unknown>> = ({ children }) =
const parsedQueryString = parse(prevSearchString, { sort: false });

if (!Object.hasOwn(urlState, accessor)) {
urlState[accessor] = {};
urlState[accessor] = Object.create(null);
}

if (typeof attribute === 'string') {
Expand Down

0 comments on commit e204184

Please sign in to comment.