diff --git a/internal/app/controlplane/schedules.go b/internal/app/controlplane/schedules.go index ffe779ec3..676c0e621 100644 --- a/internal/app/controlplane/schedules.go +++ b/internal/app/controlplane/schedules.go @@ -29,6 +29,9 @@ func scheduleDefinition() Definition { func validateSchedules(jobs []scheduling.JobSpec) error { seen := make(map[string]struct{}, len(jobs)) for _, job := range jobs { + if err := scheduling.RefuseDangerousJob(job); err != nil { + return err + } if strings.TrimSpace(job.ID) == "" { return fmt.Errorf("schedule ID is required") } diff --git a/internal/domain/scheduling/guard.go b/internal/domain/scheduling/guard.go new file mode 100644 index 000000000..d664e53a2 --- /dev/null +++ b/internal/domain/scheduling/guard.go @@ -0,0 +1,30 @@ +package scheduling + +import ( + "errors" + "fmt" + "regexp" +) + +// ErrDangerousCommand is returned when a job's payload names a command nobody +// should schedule. The list is deliberately over-broad: refusing a job an +// operator wanted costs a retry, accepting one costs the host. +var ErrDangerousCommand = errors.New("scheduling: payload names a dangerous command") + +// dangerousPayload matches the daemon's own restart, a service or launch +// manager, a process killer, a filesystem format, a raw block write and a +// machine power command. Matching is whole-word and case-insensitive, so "dd" +// is caught while "added" is not. +var dangerousPayload = regexp.MustCompile(`(?i)\b(?:archied restart|systemctl|launchctl|pkill|mkfs|dd|shutdown|reboot)\b`) + +// RefuseDangerousJob refuses a job whose payload names a dangerous command. +// The refusal is at creation time because a scheduled job runs unattended: +// there is nobody at tick time to stop it. Every creation path reaches a job +// spec through this one function, so a new token is added here and nowhere +// else. +func RefuseDangerousJob(job JobSpec) error { + if named := dangerousPayload.FindString(job.Payload.Text); named != "" { + return fmt.Errorf("%w: job %q payload names %q", ErrDangerousCommand, job.ID, named) + } + return nil +} diff --git a/internal/domain/scheduling/guard_test.go b/internal/domain/scheduling/guard_test.go new file mode 100644 index 000000000..aeafaec45 --- /dev/null +++ b/internal/domain/scheduling/guard_test.go @@ -0,0 +1,47 @@ +package scheduling + +import ( + "errors" + "strings" + "testing" +) + +func TestRefuseDangerousJob(t *testing.T) { + cases := []struct { + name string + payload string + want string + }{ + {name: "an ordinary body", payload: "Summarise yesterday's commits and open a PR."}, + {name: "letters inside a word", payload: "Add a note about the middleware address."}, + {name: "a gerund", payload: "Report on the rebooting schedule."}, + {name: "the daemon's own restart", payload: "archied restart", want: "archied restart"}, + {name: "a service manager", payload: "sudo systemctl restart archied", want: "systemctl"}, + {name: "a launch manager", payload: "launchctl unload ~/Library/LaunchAgents/x.plist", want: "launchctl"}, + {name: "a process killer", payload: "pkill -f archied", want: "pkill"}, + {name: "a filesystem format", payload: "mkfs.ext4 /dev/sda1", want: "mkfs"}, + {name: "a raw block write", payload: "sudo dd if=/dev/zero of=/dev/sda", want: "dd"}, + {name: "a machine power command", payload: "shutdown -h now", want: "shutdown"}, + {name: "a restart in capitals", payload: "Please REBOOT the box", want: "REBOOT"}, + } + for _, testCase := range cases { + t.Run(testCase.name, func(t *testing.T) { + job := JobSpec{ID: "nightly", Payload: Payload{Text: testCase.payload}} + err := RefuseDangerousJob(job) + if testCase.want == "" { + if err != nil { + t.Fatalf("RefuseDangerousJob(%q) = %v, want nil", testCase.payload, err) + } + return + } + if !errors.Is(err, ErrDangerousCommand) { + t.Fatalf("RefuseDangerousJob(%q) = %v, want ErrDangerousCommand", testCase.payload, err) + } + for _, want := range []string{testCase.want, `"nightly"`} { + if !strings.Contains(err.Error(), want) { + t.Errorf("RefuseDangerousJob(%q) = %q, want it to name %s", testCase.payload, err, want) + } + } + }) + } +}