Skip to content

Commit

Permalink
Fixed due to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
YulNaumenko committed Jun 30, 2020
1 parent 6e89502 commit e725ed9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
11 changes: 11 additions & 0 deletions x-pack/plugins/actions/server/builtin_action_types/webhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ describe('config validation', () => {
});
});

test('config validation failed when a url is invalid', () => {
const config: Record<string, string> = {
url: 'example.com/do-something',
};
expect(() => {
validateConfig(actionType, config);
}).toThrowErrorMatchingInlineSnapshot(
'"error validating action type config: error configuring webhook action: unable to parse host name from Url"'
);
});

test('config validation passes when valid headers are provided', () => {
// any for testing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/actions/server/builtin_action_types/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ function validateActionTypeConfig(
configurationUtilities: ActionsConfigurationUtilities,
configObject: ActionTypeConfigType
) {
let url: URL;
try {
url = new URL(configObject.url);
} catch (err) {
return i18n.translate('xpack.actions.builtin.webhook.webhookConfigurationErrorNoHostname', {
defaultMessage: 'error configuring webhook action: unable to parse host name from Url',
});
}

try {
configurationUtilities.ensureWhitelistedUri(configObject.url);
} catch (whitelistError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { lazy } from 'react';
import { i18n } from '@kbn/i18n';
import { ActionTypeModel, ValidationResult } from '../../../../types';
import { WebhookActionParams, WebhookActionConnector } from '../types';
import { isUrlInvalid } from '../../../lib/value_validators';
import { isValidUrl } from '../../../lib/value_validators';

export function getActionType(): ActionTypeModel<WebhookActionConnector, WebhookActionParams> {
return {
Expand Down Expand Up @@ -44,11 +44,11 @@ export function getActionType(): ActionTypeModel<WebhookActionConnector, Webhook
)
);
}
if (isUrlInvalid(action.config.url)) {
if (action.config.url && !isValidUrl(action.config.url)) {
errors.url = [
...errors.url,
i18n.translate(
'xpack.triggersActionsUI.components.builtinActionTypes.servicenow.invalidApiUrlTextField',
'xpack.triggersActionsUI.components.builtinActionTypes.webhookAction.error.invalidUrlTextField',
{
defaultMessage: 'URL is invalid.',
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { throwIfAbsent, throwIfIsntContained, isUrlInvalid } from './value_validators';
import { throwIfAbsent, throwIfIsntContained, isValidUrl } from './value_validators';
import uuid from 'uuid';

describe('throwIfAbsent', () => {
Expand Down Expand Up @@ -80,12 +80,16 @@ describe('throwIfIsntContained', () => {
});
});

describe('isUrlInvalid', () => {
describe('isValidUrl', () => {
test('verifies invalid url', () => {
expect(isUrlInvalid('this is not a url')).toBeTruthy();
expect(isValidUrl('this is not a url')).toBeFalsy();
});

test('verifies valid url', () => {
expect(isUrlInvalid('https://www.elastic.co')).toBeFalsy();
test('verifies valid url any protocol', () => {
expect(isValidUrl('https://www.elastic.co/')).toBeTruthy();
});

test('verifies valid url with specific protocol', () => {
expect(isValidUrl('https://www.elastic.co/', 'https:')).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ export function throwIfIsntContained<T>(
};
}

const urlExpression = /^https?:\/\/([\w\d\-]+\.)+\w{2,}(\/.+)?$/;

export const isUrlInvalid = (url: string | null | undefined) => {
if (!isEmpty(url) && url != null && url.match(urlExpression) == null) {
return true;
export const isValidUrl = (urlString: string, protocol?: string) => {
try {
const urlObject = new URL(urlString);
if (protocol === undefined || urlObject.protocol === protocol) {
return true;
}
return false;
} catch (err) {
return false;
}
return false;
};

0 comments on commit e725ed9

Please sign in to comment.