Skip to content

Commit e725ed9

Browse files
committed
Fixed due to comments
1 parent 6e89502 commit e725ed9

5 files changed

Lines changed: 41 additions & 14 deletions

File tree

x-pack/plugins/actions/server/builtin_action_types/webhook.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ describe('config validation', () => {
114114
});
115115
});
116116

117+
test('config validation failed when a url is invalid', () => {
118+
const config: Record<string, string> = {
119+
url: 'example.com/do-something',
120+
};
121+
expect(() => {
122+
validateConfig(actionType, config);
123+
}).toThrowErrorMatchingInlineSnapshot(
124+
'"error validating action type config: error configuring webhook action: unable to parse host name from Url"'
125+
);
126+
});
127+
117128
test('config validation passes when valid headers are provided', () => {
118129
// any for testing
119130
// eslint-disable-next-line @typescript-eslint/no-explicit-any

x-pack/plugins/actions/server/builtin_action_types/webhook.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ function validateActionTypeConfig(
8585
configurationUtilities: ActionsConfigurationUtilities,
8686
configObject: ActionTypeConfigType
8787
) {
88+
let url: URL;
89+
try {
90+
url = new URL(configObject.url);
91+
} catch (err) {
92+
return i18n.translate('xpack.actions.builtin.webhook.webhookConfigurationErrorNoHostname', {
93+
defaultMessage: 'error configuring webhook action: unable to parse host name from Url',
94+
});
95+
}
96+
8897
try {
8998
configurationUtilities.ensureWhitelistedUri(configObject.url);
9099
} catch (whitelistError) {

x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/webhook/webhook.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { lazy } from 'react';
77
import { i18n } from '@kbn/i18n';
88
import { ActionTypeModel, ValidationResult } from '../../../../types';
99
import { WebhookActionParams, WebhookActionConnector } from '../types';
10-
import { isUrlInvalid } from '../../../lib/value_validators';
10+
import { isValidUrl } from '../../../lib/value_validators';
1111

1212
export function getActionType(): ActionTypeModel<WebhookActionConnector, WebhookActionParams> {
1313
return {
@@ -44,11 +44,11 @@ export function getActionType(): ActionTypeModel<WebhookActionConnector, Webhook
4444
)
4545
);
4646
}
47-
if (isUrlInvalid(action.config.url)) {
47+
if (action.config.url && !isValidUrl(action.config.url)) {
4848
errors.url = [
4949
...errors.url,
5050
i18n.translate(
51-
'xpack.triggersActionsUI.components.builtinActionTypes.servicenow.invalidApiUrlTextField',
51+
'xpack.triggersActionsUI.components.builtinActionTypes.webhookAction.error.invalidUrlTextField',
5252
{
5353
defaultMessage: 'URL is invalid.',
5454
}

x-pack/plugins/triggers_actions_ui/public/application/lib/value_validators.test.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6-
import { throwIfAbsent, throwIfIsntContained, isUrlInvalid } from './value_validators';
6+
import { throwIfAbsent, throwIfIsntContained, isValidUrl } from './value_validators';
77
import uuid from 'uuid';
88

99
describe('throwIfAbsent', () => {
@@ -80,12 +80,16 @@ describe('throwIfIsntContained', () => {
8080
});
8181
});
8282

83-
describe('isUrlInvalid', () => {
83+
describe('isValidUrl', () => {
8484
test('verifies invalid url', () => {
85-
expect(isUrlInvalid('this is not a url')).toBeTruthy();
85+
expect(isValidUrl('this is not a url')).toBeFalsy();
8686
});
8787

88-
test('verifies valid url', () => {
89-
expect(isUrlInvalid('https://www.elastic.co')).toBeFalsy();
88+
test('verifies valid url any protocol', () => {
89+
expect(isValidUrl('https://www.elastic.co/')).toBeTruthy();
90+
});
91+
92+
test('verifies valid url with specific protocol', () => {
93+
expect(isValidUrl('https://www.elastic.co/', 'https:')).toBeTruthy();
9094
});
9195
});

x-pack/plugins/triggers_actions_ui/public/application/lib/value_validators.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ export function throwIfIsntContained<T>(
3232
};
3333
}
3434

35-
const urlExpression = /^https?:\/\/([\w\d\-]+\.)+\w{2,}(\/.+)?$/;
36-
37-
export const isUrlInvalid = (url: string | null | undefined) => {
38-
if (!isEmpty(url) && url != null && url.match(urlExpression) == null) {
39-
return true;
35+
export const isValidUrl = (urlString: string, protocol?: string) => {
36+
try {
37+
const urlObject = new URL(urlString);
38+
if (protocol === undefined || urlObject.protocol === protocol) {
39+
return true;
40+
}
41+
return false;
42+
} catch (err) {
43+
return false;
4044
}
41-
return false;
4245
};

0 commit comments

Comments
 (0)