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

[7.x] [SECURITY_SOLUTION] Adds hash validation on UI for trusted app (#94958) #95080

Merged
merged 1 commit into from
Mar 22, 2021
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 @@ -267,6 +267,11 @@ describe('When showing the Trusted App Create Form', () => {
expect(renderResult.getByText('Name is required'));
});

it('should validate invalid Hash value', () => {
setTextFieldValue(getConditionValue(getCondition(renderResult)), 'someHASH');
expect(renderResult.getByText('[1] Invalid hash value'));
});

it('should validate that a condition value has a non empty space value', () => {
setTextFieldValue(getConditionValue(getCondition(renderResult)), ' ');
expect(renderResult.getByText('[1] Field entry must have a value'));
Expand All @@ -281,13 +286,27 @@ describe('When showing the Trusted App Create Form', () => {
setTextFieldValue(getConditionValue(getCondition(renderResult)), 'someHASH');
expect(renderResult.getByText('[2] Field entry must have a value'));
});

it('should validate multiple errors in form', () => {
const andButton = getConditionBuilderAndButton(renderResult);
reactTestingLibrary.act(() => {
fireEvent.click(andButton, { button: 1 });
});

setTextFieldValue(getConditionValue(getCondition(renderResult)), 'someHASH');
expect(renderResult.getByText('[1] Invalid hash value'));
expect(renderResult.getByText('[2] Field entry must have a value'));
});
});

describe('and all required data passes validation', () => {
it('should call change callback with isValid set to true and contain the new item', () => {
const renderResult = render();
setTextFieldValue(getNameField(renderResult), 'Some Process');
setTextFieldValue(getConditionValue(getCondition(renderResult)), 'someHASH');
setTextFieldValue(
getConditionValue(getCondition(renderResult)),
'e50fb1a0e5fff590ece385082edc6c41'
);
setTextFieldValue(getDescriptionField(renderResult), 'some description');

expect(getAllValidationErrors(renderResult)).toHaveLength(0);
Expand All @@ -300,7 +319,7 @@ describe('When showing the Trusted App Create Form', () => {
field: ConditionEntryField.HASH,
operator: 'included',
type: 'match',
value: 'someHASH',
value: 'e50fb1a0e5fff590ece385082edc6c41',
},
],
name: 'Some Process',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ import {
import { i18n } from '@kbn/i18n';
import { EuiFormProps } from '@elastic/eui/src/components/form/form';
import {
ConditionEntryField,
MacosLinuxConditionEntry,
NewTrustedApp,
OperatingSystem,
} from '../../../../../../common/endpoint/types';
import { isValidHash } from '../../../../../../common/endpoint/validation/trusted_apps';

import {
isMacosLinuxTrustedAppCondition,
isWindowsTrustedAppCondition,
Expand Down Expand Up @@ -113,7 +116,7 @@ const validateFormValues = (values: NewTrustedApp): ValidationResult => {
})
);
} else {
values.entries.some((entry, index) => {
values.entries.forEach((entry, index) => {
if (!entry.field || !entry.value.trim()) {
isValid = false;
addResultToValidation(
Expand All @@ -128,9 +131,18 @@ const validateFormValues = (values: NewTrustedApp): ValidationResult => {
}
)
);
return true;
} else if (entry.field === ConditionEntryField.HASH && !isValidHash(entry.value)) {
isValid = false;
addResultToValidation(
validation,
'entries',
'errors',
i18n.translate('xpack.securitySolution.trustedapps.create.conditionFieldInvalidHashMsg', {
defaultMessage: '[{row}] Invalid hash value',
values: { row: index + 1 },
})
);
}
return false;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ describe('When on the Trusted Apps Page', () => {

fireEvent.change(
getByTestId('addTrustedAppFlyout-createForm-conditionsBuilder-group1-entry0-value'),
{ target: { value: 'SOME$HASH#HERE' } }
{ target: { value: '44ed10b389dbcd1cf16cec79d16d7378' } }
);

fireEvent.change(getByTestId('addTrustedAppFlyout-createForm-descriptionField'), {
Expand Down Expand Up @@ -363,6 +363,29 @@ describe('When on the Trusted Apps Page', () => {
});
});
});

describe('and when the form data is not valid', () => {
it('should not enable the Flyout Add button with an invalid hash', async () => {
const renderResult = await renderAndClickAddButton();
const { getByTestId } = renderResult;

reactTestingLibrary.act(() => {
fireEvent.change(getByTestId('addTrustedAppFlyout-createForm-nameTextField'), {
target: { value: 'trusted app A' },
});

fireEvent.change(
getByTestId('addTrustedAppFlyout-createForm-conditionsBuilder-group1-entry0-value'),
{ target: { value: 'invalid hash' } }
);
});

const flyoutAddButton = getByTestId(
'addTrustedAppFlyout-createButton'
) as HTMLButtonElement;
expect(flyoutAddButton.disabled).toBe(true);
});
});
});

describe('and there are no trusted apps', () => {
Expand Down