Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix PagerDuty timestamp validation #122321

Merged
merged 4 commits into from Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -163,6 +163,25 @@ describe('validateParams()', () => {
expect(validateParams(actionType, { timestamp })).toEqual({ timestamp });
});

test('should validate and pass all the valid ISO-8601 formatted dates', () => {
const date1 = '2011-05-06T17:00Z';
const date2 = '2011-05-06T03:30-07';
const date3 = '2011-05-06';

expect(validateParams(actionType, { timestamp: date1 })).toEqual({ timestamp: date1 });
expect(validateParams(actionType, { timestamp: date2 })).toEqual({ timestamp: date2 });
expect(validateParams(actionType, { timestamp: date3 })).toEqual({ timestamp: date3 });
});

test('should validate and throw error when timestamp is a non-ISO-8601 date', () => {
const timestamp = `01-01-2021 10:00:00`;
expect(() => {
validateParams(actionType, {
timestamp,
});
}).toThrowError(`error validating action params: error parsing timestamp "${timestamp}"`);
});

test('should validate and throw error when timestamp is invalid', () => {
const timestamp = `1963-09-55 90:23:45`;
expect(() => {
Expand Down
Expand Up @@ -8,6 +8,7 @@
import { curry, isUndefined, pick, omitBy } from 'lodash';
import { i18n } from '@kbn/i18n';
import { schema, TypeOf } from '@kbn/config-schema';
import moment from 'moment';
import { postPagerduty } from './lib/post_pagerduty';
import { Logger } from '../../../../../src/core/server';
import { ActionType, ActionTypeExecutorOptions, ActionTypeExecutorResult } from '../types';
Expand Down Expand Up @@ -97,8 +98,8 @@ function validateParams(paramsObject: unknown): string | void {
const validatedTimestamp = validateTimestamp(timestamp);
if (validatedTimestamp != null) {
try {
const date = Date.parse(validatedTimestamp);
if (isNaN(date)) {
const date = moment(validatedTimestamp, moment.ISO_8601);
if (!date.isValid()) {
return i18n.translate('xpack.actions.builtin.pagerduty.invalidTimestampErrorMessage', {
defaultMessage: `error parsing timestamp "{timestamp}"`,
values: {
Expand Down