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

[Security Solution][Detection Engine] Fixes critical bug with the same index being passed in #79949

Merged
merged 1 commit into from
Oct 8, 2020
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 @@ -67,7 +67,7 @@ describe('Helpers', () => {
type: 'mapping',
value: 'some os',
};
const output = getFormattedEntry(payloadIndexPattern, payloadItem, 0);
const output = getFormattedEntry(payloadIndexPattern, payloadIndexPattern, payloadItem, 0);
const expected: FormattedEntry = {
entryIndex: 0,
field: {
Expand All @@ -88,10 +88,10 @@ describe('Helpers', () => {
});

describe('#getFormattedEntries', () => {
test('it returns formatted entry with fields undefined if it unable to find a matching index pattern field', () => {
const payloadIndexPattern: IndexPattern = getMockIndexPattern();
test('it returns formatted entry with field and value undefined if it unable to find a matching index pattern field', () => {
const payloadIndexPattern = getMockIndexPattern();
const payloadItems: Entry[] = [{ field: 'field.one', type: 'mapping', value: 'field.one' }];
const output = getFormattedEntries(payloadIndexPattern, payloadItems);
const output = getFormattedEntries(payloadIndexPattern, payloadIndexPattern, payloadItems);
const expected: FormattedEntry[] = [
{
entryIndex: 0,
Expand All @@ -103,13 +103,71 @@ describe('Helpers', () => {
expect(output).toEqual(expected);
});

test('it returns "undefined" value if cannot match a pattern field', () => {
const payloadIndexPattern = getMockIndexPattern();
const payloadItems: Entry[] = [{ field: 'machine.os', type: 'mapping', value: 'yolo' }];
const output = getFormattedEntries(payloadIndexPattern, payloadIndexPattern, payloadItems);
const expected: FormattedEntry[] = [
{
entryIndex: 0,
field: {
name: 'machine.os',
type: 'string',
esTypes: ['text'],
count: 0,
scripted: false,
searchable: true,
aggregatable: true,
readFromDocValues: false,
},
value: undefined,
type: 'mapping',
},
];
expect(output).toEqual(expected);
});

test('it returns value and field when they match two independent index patterns', () => {
const payloadIndexPattern = getMockIndexPattern();
const threatIndexPattern = getMockIndexPattern();
const payloadItems: Entry[] = [{ field: 'machine.os', type: 'mapping', value: 'machine.os' }];
const output = getFormattedEntries(payloadIndexPattern, threatIndexPattern, payloadItems);
const expected: FormattedEntry[] = [
{
entryIndex: 0,
field: {
name: 'machine.os',
type: 'string',
esTypes: ['text'],
count: 0,
scripted: false,
searchable: true,
aggregatable: true,
readFromDocValues: false,
},
value: {
name: 'machine.os',
type: 'string',
esTypes: ['text'],
count: 0,
scripted: false,
searchable: true,
aggregatable: true,
readFromDocValues: false,
},
type: 'mapping',
},
];
expect(output).toEqual(expected);
});

test('it returns formatted entries', () => {
const payloadIndexPattern: IndexPattern = getMockIndexPattern();
const payloadItems: Entry[] = [
{ field: 'machine.os', type: 'mapping', value: 'machine.os' },
{ field: 'ip', type: 'mapping', value: 'ip' },
];
const output = getFormattedEntries(payloadIndexPattern, payloadItems);
const output = getFormattedEntries(payloadIndexPattern, payloadIndexPattern, payloadItems);
const expected: FormattedEntry[] = [
{
field: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ import { Entry, FormattedEntry, ThreatMapEntries, EmptyEntry } from './types';
*/
export const getFormattedEntry = (
indexPattern: IndexPattern,
threatIndexPatterns: IndexPattern,
item: Entry,
itemIndex: number
): FormattedEntry => {
const { fields } = indexPattern;
const { fields: threatFields } = threatIndexPatterns;
const field = item.field;
const threatField = item.value;
const [foundField] = fields.filter(({ name }) => field != null && field === name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could these be memoized? My only hesitance with memoizing would be that it may just run on every render regardless because the hook doesn't do a deep equal to my understanding.

Copy link
Contributor Author

@FrankHassanabad FrankHassanabad Oct 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is in the callingReact Component which I think is what we want rather than in the pure functions.

    const entries = useMemo(
      (): FormattedEntry[] =>
        indexPattern != null && listItem.entries.length > 0
          ? getFormattedEntries(indexPattern, threatIndexPatterns, listItem.entries)
          : [],
      [listItem.entries, indexPattern, threatIndexPatterns]
    );

const [threatFoundField] = fields.filter(
const [threatFoundField] = threatFields.filter(
({ name }) => threatField != null && threatField === name
);
return {
Expand All @@ -48,10 +50,11 @@ export const getFormattedEntry = (
*/
export const getFormattedEntries = (
indexPattern: IndexPattern,
threatIndexPatterns: IndexPattern,
entries: Entry[]
): FormattedEntry[] => {
return entries.reduce<FormattedEntry[]>((acc, item, index) => {
const newItemEntry = getFormattedEntry(indexPattern, item, index);
const newItemEntry = getFormattedEntry(indexPattern, threatIndexPatterns, item, index);
return [...acc, newItemEntry];
}, []);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ export const ListItemComponent = React.memo<ListItemProps>(
const entries = useMemo(
(): FormattedEntry[] =>
indexPattern != null && listItem.entries.length > 0
? getFormattedEntries(indexPattern, listItem.entries)
? getFormattedEntries(indexPattern, threatIndexPatterns, listItem.entries)
: [],
[listItem.entries, indexPattern]
[listItem.entries, indexPattern, threatIndexPatterns]
);
return (
<EuiFlexItem>
Expand Down