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: validate cron expression to prevent server crash #456

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.cloud.sonic.controller.models.domain.Jobs;
import org.cloud.sonic.controller.models.dto.JobsDTO;
import org.cloud.sonic.controller.services.JobsService;
import org.cloud.sonic.controller.tools.QuartzJobTools;
import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -120,9 +122,14 @@ public RespModel<List<JSONObject>> findSysJobs() {
@Parameter(name = "type", description = "类型"),
@Parameter(name = "cron", description = "cron表达式")
})

@PutMapping("/updateSysJob")
public RespModel updateSysJob(@RequestBody JSONObject jsonObject) {
jobsService.updateSysJob(jsonObject.getString("type"), jsonObject.getString("cron"));
final String cron = QuartzJobTools.validateOrDisableCronExpression(jsonObject.getString("cron"));
if (!CronExpression.isValidExpression(cron)) { // https://stackoverflow.com/a/2363119/12857692
return new RespModel<>(RespEnum.PARAMS_NOT_VALID);
}
jobsService.updateSysJob(jsonObject.getString("type"), cron);
return new RespModel<>(RespEnum.HANDLE_OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.cloud.sonic.controller.models.domain.Jobs;
import org.cloud.sonic.controller.models.interfaces.JobType;
import org.cloud.sonic.controller.services.JobsService;
import org.cloud.sonic.controller.tools.QuartzJobTools;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -58,7 +59,7 @@ public void createScheduleJob(Jobs jobs) throws SchedulerException {
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobs.getId() + "").build();
jobDetail.getJobDataMap().put("id", jobs.getId());
jobDetail.getJobDataMap().put("type", JobType.TEST_JOB);
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(jobs.getCronExpression())
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(QuartzJobTools.validateOrDisableCronExpression(jobs.getCronExpression()))
.withMisfireHandlingInstructionDoNothing();
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobs.getId() + "").withSchedule(scheduleBuilder).build();
scheduler.scheduleJob(jobDetail, trigger);
Expand Down Expand Up @@ -134,7 +135,7 @@ public void updateScheduleJob(Jobs jobs) throws SchedulerException {
try {
TriggerKey triggerKey = TriggerKey.triggerKey(jobs.getId() + "");
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(jobs.getCronExpression())
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(QuartzJobTools.validateOrDisableCronExpression(jobs.getCronExpression()))
.withMisfireHandlingInstructionDoNothing();
trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
scheduler.rescheduleJob(triggerKey, trigger);
Expand Down Expand Up @@ -238,7 +239,7 @@ public void updateSysScheduleJob(String type, String cron) {
}
break;
}
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron)
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(QuartzJobTools.validateOrDisableCronExpression(cron))
.withMisfireHandlingInstructionDoNothing();
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(type).withSchedule(scheduleBuilder).build();
scheduler.scheduleJob(jobDetail, trigger);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.cloud.sonic.controller.tools;

import org.quartz.CronExpression;

public final class QuartzJobTools {
public static String validateOrDisableCronExpression(final String cron) {
return (cron!=null && !cron.equals("") && CronExpression.isValidExpression(cron) ? cron : "0 0 0 ? * MON 1900");
}
}
Loading