Files
archie/internal/domain/scheduling/guard.go
T
sam f33a33d854 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.
2026-10-08 22:45:28 +11:00

31 lines
1.3 KiB
Go

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
}