From 97cc42ffa8672e7d0d0996b5fbda7f7fe699da2d Mon Sep 17 00:00:00 2001 From: Alexander Russell Date: Tue, 2 May 2023 07:36:07 -0600 Subject: [PATCH] fix: Cron Validation (#4842) * Add Cron Next Time Validation The cron job can't be created if the year is more than 100 years in the future. Getting the next valid time will return null if this is the case. * add next cron validation to api * add next cron validation to job settings page * Add Missing Import --- src/Ombi.Settings/Settings/Models/JobSettingsHelper.cs | 5 ++++- src/Ombi/Controllers/V1/SettingsController.cs | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Ombi.Settings/Settings/Models/JobSettingsHelper.cs b/src/Ombi.Settings/Settings/Models/JobSettingsHelper.cs index b80943cc5..fb51ae147 100644 --- a/src/Ombi.Settings/Settings/Models/JobSettingsHelper.cs +++ b/src/Ombi.Settings/Settings/Models/JobSettingsHelper.cs @@ -1,5 +1,6 @@ using Ombi.Helpers; using Quartz; +using System; namespace Ombi.Settings.Settings.Models { @@ -104,7 +105,9 @@ namespace Ombi.Settings.Settings.Models private static string ValidateCron(string cron) { - if (CronExpression.IsValidExpression(cron)) + CronExpression expression = new CronExpression(cron); + DateTimeOffset? nextFireUTCTime = expression.GetNextValidTimeAfter(DateTime.Now); + if (CronExpression.IsValidExpression(cron) && nextFireUTCTime != null) { return cron; } diff --git a/src/Ombi/Controllers/V1/SettingsController.cs b/src/Ombi/Controllers/V1/SettingsController.cs index 0892b1dda..c77913e48 100644 --- a/src/Ombi/Controllers/V1/SettingsController.cs +++ b/src/Ombi/Controllers/V1/SettingsController.cs @@ -652,7 +652,9 @@ namespace Ombi.Controllers.V1 try { var isValid = CronExpression.IsValidExpression(expression); - if (!isValid) + CronExpression cron = new CronExpression(expression); + DateTimeOffset? nextFireUTCTime = cron.GetNextValidTimeAfter(DateTime.Now); + if (!isValid || nextFireUTCTime == null) { return new JobSettingsViewModel {