Skip to content

Commit

Permalink
[SECURITY_SOLUTION][ENDPOINT] Trusted Apps - fix error for duplicate …
Browse files Browse the repository at this point in the history
…fields to correctly mention the field at fault (#79853) (#79885)

* Fix error for duplicate fields to correctly mention the field at fault
* Add new tests to duplicate field validation
  • Loading branch information
paul-tavares authored Oct 7, 2020
1 parent 988e488 commit 3d6b3e8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,28 @@ describe('When invoking Trusted Apps Schema', () => {
});

it('should validate that `entry.field` is used only once', () => {
const bodyMsg = {
let bodyMsg = {
...getCreateTrustedAppItem(),
entries: [getTrustedAppItemEntryItem(), getTrustedAppItemEntryItem()],
};
expect(() => body.validate(bodyMsg)).toThrow();
expect(() => body.validate(bodyMsg)).toThrow('[Path] field can only be used once');

bodyMsg = {
...getCreateTrustedAppItem(),
entries: [
{
...getTrustedAppItemEntryItem(),
field: 'process.hash.*',
value: VALID_HASH_MD5,
},
{
...getTrustedAppItemEntryItem(),
field: 'process.hash.*',
value: VALID_HASH_MD5,
},
],
};
expect(() => body.validate(bodyMsg)).toThrow('[Hash] field can only be used once');
});

it('should validate Hash field valid value', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { schema } from '@kbn/config-schema';
import { TrustedApp } from '../types';

const hashLengths: readonly number[] = [
32, // MD5
Expand All @@ -13,6 +14,12 @@ const hashLengths: readonly number[] = [
];
const hasInvalidCharacters = /[^0-9a-f]/i;

const entryFieldLabels: { [k in TrustedApp['entries'][0]['field']]: string } = {
'process.hash.*': 'Hash',
'process.executable.caseless': 'Path',
'process.code_signature': 'Signer',
};

export const DeleteTrustedAppsRequestSchema = {
params: schema.object({
id: schema.string(),
Expand Down Expand Up @@ -47,7 +54,7 @@ export const PostTrustedAppCreateRequestSchema = {
const usedFields: string[] = [];
for (const { field, value } of entries) {
if (usedFields.includes(field)) {
return `[Hash] field can only be used once`;
return `[${entryFieldLabels[field]}] field can only be used once`;
}

usedFields.push(field);
Expand Down

0 comments on commit 3d6b3e8

Please sign in to comment.