Skip to content

Commit

Permalink
Loosen UUID regex to accept uuidv1 or uuidv4 (#85338) (#85358)
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallmain authored Dec 9, 2020
1 parent d57b5df commit 40f1321
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { validateSingleAction, validateRuleActionsField } from './schema';
import { isUuidv4, getActionTypeName, validateMustache, validateActionParams } from './utils';
import { isUuid, getActionTypeName, validateMustache, validateActionParams } from './utils';
import { actionTypeRegistryMock } from '../../../../../../triggers_actions_ui/public/application/action_type_registry.mock';
import { FormHook } from '../../../../shared_imports';
jest.mock('./utils');
Expand All @@ -15,7 +15,7 @@ describe('stepRuleActions schema', () => {

describe('validateSingleAction', () => {
it('should validate single action', () => {
(isUuidv4 as jest.Mock).mockReturnValue(true);
(isUuid as jest.Mock).mockReturnValue(true);
(validateActionParams as jest.Mock).mockReturnValue([]);
(validateMustache as jest.Mock).mockReturnValue([]);

Expand All @@ -33,7 +33,7 @@ describe('stepRuleActions schema', () => {
});

it('should validate single action with invalid mustache template', () => {
(isUuidv4 as jest.Mock).mockReturnValue(true);
(isUuid as jest.Mock).mockReturnValue(true);
(validateActionParams as jest.Mock).mockReturnValue([]);
(validateMustache as jest.Mock).mockReturnValue(['Message is not valid mustache template']);

Expand All @@ -54,7 +54,7 @@ describe('stepRuleActions schema', () => {
});

it('should validate single action with incorrect id', () => {
(isUuidv4 as jest.Mock).mockReturnValue(false);
(isUuid as jest.Mock).mockReturnValue(false);
(validateMustache as jest.Mock).mockReturnValue([]);
(validateActionParams as jest.Mock).mockReturnValue([]);

Expand Down Expand Up @@ -117,9 +117,9 @@ describe('stepRuleActions schema', () => {
});

it('should validate multiple incorrect rule actions field', () => {
(isUuidv4 as jest.Mock).mockReturnValueOnce(false);
(isUuid as jest.Mock).mockReturnValueOnce(false);
(getActionTypeName as jest.Mock).mockReturnValueOnce('Slack');
(isUuidv4 as jest.Mock).mockReturnValueOnce(true);
(isUuid as jest.Mock).mockReturnValueOnce(true);
(getActionTypeName as jest.Mock).mockReturnValueOnce('Pagerduty');
(validateActionParams as jest.Mock).mockReturnValue(['Summary is required']);
(validateMustache as jest.Mock).mockReturnValue(['Component is not valid mustache template']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import {
import { FormSchema, ValidationFunc, ERROR_CODE } from '../../../../shared_imports';
import { ActionsStepRule } from '../../../pages/detection_engine/rules/types';
import * as I18n from './translations';
import { isUuidv4, getActionTypeName, validateMustache, validateActionParams } from './utils';
import { isUuid, getActionTypeName, validateMustache, validateActionParams } from './utils';

export const validateSingleAction = (
actionItem: AlertAction,
actionTypeRegistry: ActionTypeRegistryContract
): string[] => {
if (!isUuidv4(actionItem.id)) {
if (!isUuid(actionItem.id)) {
return [I18n.NO_CONNECTOR_SELECTED];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
*/

import { actionTypeRegistryMock } from '../../../../../../triggers_actions_ui/public/application/action_type_registry.mock';
import { isUuidv4, getActionTypeName, validateMustache, validateActionParams } from './utils';
import { isUuid, getActionTypeName, validateMustache, validateActionParams } from './utils';

describe('stepRuleActions utils', () => {
describe('isUuidv4', () => {
it('should validate proper uuid v4 value', () => {
expect(isUuidv4('817b8bca-91d1-4729-8ee1-3a83aaafd9d4')).toEqual(true);
expect(isUuid('817b8bca-91d1-4729-8ee1-3a83aaafd9d4')).toEqual(true);
});

it('should validate incorrect uuid v4 value', () => {
expect(isUuidv4('ad9d4')).toEqual(false);
expect(isUuid('ad9d4')).toEqual(false);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import {
} from '../../../../../../triggers_actions_ui/public';
import * as I18n from './translations';

const UUID_V4_REGEX = /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;
const UUID_REGEX = /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i;

export const isUuidv4 = (id: AlertAction['id']) => !!id.match(UUID_V4_REGEX);
export const isUuid = (id: AlertAction['id']) => !!id.match(UUID_REGEX);

export const getActionTypeName = (actionTypeId: AlertAction['actionTypeId']) => {
if (!actionTypeId) return '';
Expand Down

0 comments on commit 40f1321

Please sign in to comment.