feat(scheduling): refuse a job payload that names a dangerous command

A scheduled job runs unattended, so a payload that restarts the daemon,
formats a disk or powers the machine down is refused when the job is
created rather than when it fires. scheduling.RefuseDangerousJob is the
one rule every creation path reaches: the dashboard, any control-plane
client and the config seed all replace the schedules document through the
same validator, so a new token is added in one place.
This commit is contained in:
sam committed 2026-10-08 22:45:28 +11:00
1 parent 1c887b5e39
commit 0a3ecf268b
3 files changed
+80

No files matched your search

+3
View File
@@ -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")
}
+30
View File
@@ -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
}
+47
View File
@@ -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)
}
}
})
}
}