Skip to content

Commit

Permalink
[SECURITY_SOLUTION] Adds hash validation on UI for trusted app (#94958)…
Browse files Browse the repository at this point in the history
… (#95080)

* Adds hash validation on UI. Display as many error messages as they are instead of displaying just the first one on entry fields. Updates related unit test

* Fixes failing test and added new test case

Co-authored-by: Kibana Machine <[email protected]>

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
dasansol92 and kibanamachine authored Mar 22, 2021
1 parent eb6df37 commit 7ebee0f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
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

0 comments on commit 7ebee0f

Please sign in to comment.