Preserve saved schedule run-level write scope

This commit is contained in:
2026-10-21 01:03:39 +01:00
parent 8c3f116190
commit fe02d153f1
8 changed files with 279 additions and 28 deletions
+44
View File
@@ -0,0 +1,44 @@
package pipeline
import (
"fmt"
"os"
"path/filepath"
"strings"
)
// ValidateRunWritePaths validates optional exact run-relative paths against an
// already resolved server workspace. Saves and manual/scheduled admission share
// this policy; admission rechecks it in case filesystem links changed since save.
// An omitted or empty list retains the existing run-level default, not a grant
// beyond the pinned workflow's own tool permissions.
func ValidateRunWritePaths(workspace string, paths []string) error {
if len(paths) > 20 {
return fmt.Errorf("too many write paths")
}
for _, path := range paths {
if filepath.IsAbs(path) || filepath.Clean(path) != path || path == ".." || strings.HasPrefix(path, ".."+string(os.PathSeparator)) || strings.ContainsAny(path, "*?\\") {
return fmt.Errorf("optional write paths must be exact run-directory-relative paths")
}
parent := filepath.Join(workspace, path)
for {
_, err := os.Lstat(parent)
if err == nil {
break
}
if !os.IsNotExist(err) {
return err
}
next := filepath.Dir(parent)
if next == parent {
return fmt.Errorf("write path is unavailable")
}
parent = next
}
resolved, err := filepath.EvalSymlinks(parent)
if err != nil || (resolved != workspace && !strings.HasPrefix(resolved, workspace+string(os.PathSeparator))) {
return fmt.Errorf("write path must stay within the run directory")
}
}
return nil
}
+94
View File
@@ -0,0 +1,94 @@
package pipeline
import (
"context"
"os"
"path/filepath"
"slices"
"testing"
"time"
)
func TestValidateRunWritePaths(t *testing.T) {
workspace, err := ResolveRunWorkspace(t.TempDir(), true)
if err != nil {
t.Fatal(err)
}
if err = os.Mkdir(filepath.Join(workspace, "internal"), 0700); err != nil {
t.Fatal(err)
}
if err = os.Symlink(t.TempDir(), filepath.Join(workspace, "escape")); err != nil {
t.Fatal(err)
}
if err = os.Symlink(filepath.Join(workspace, "internal"), filepath.Join(workspace, "inside")); err != nil {
t.Fatal(err)
}
for _, paths := range [][]string{nil, {}, {"."}, {"new.go", "internal/another/new.go"}, {"inside/new.go"}} {
if err = ValidateRunWritePaths(workspace, paths); err != nil {
t.Fatal("valid scope rejected", paths, err)
}
}
for _, paths := range invalidRunWritePaths(workspace) {
if err = ValidateRunWritePaths(workspace, paths); err == nil {
t.Fatal("invalid scope accepted", paths)
}
}
}
func invalidRunWritePaths(workspace string) [][]string {
return [][]string{{""}, {".."}, {"../outside.go"}, {"internal/../outside.go"}, {"./one.go"}, {"one//two.go"}, {"*.go"}, {"file?.go"}, {`one\two.go`}, {filepath.Join(workspace, "absolute.go")}, {"escape/new.go"}, slices.Repeat([]string{"new.go"}, 21)}
}
func TestSavedScheduleWriteScopeValidationAndIdempotency(t *testing.T) {
s, pid, spec, now := savedStoreFixture(t)
ctx := context.Background()
if err := os.Symlink(t.TempDir(), filepath.Join(spec.Workspace, "escape")); err != nil {
t.Fatal(err)
}
spec.WritePaths = []string{"internal/one.go", "README.md"}
spec.AllowTools = true
a, err := s.SaveSchedule(ctx, spec, now, "restricted-create")
if err != nil {
t.Fatal(err)
}
if !a.AllowTools || !slices.Equal(a.WritePaths, spec.WritePaths) {
t.Fatal("scope omitted on save", a)
}
for _, paths := range invalidRunWritePaths(spec.Workspace) {
bad := spec
bad.WritePaths = paths
if _, err = s.SaveSchedule(ctx, bad, now, "invalid-create"); err == nil {
t.Fatal("invalid scope created a schedule", paths)
}
bad.ID = a.ID
bad.Revision = a.Revision
if _, err = s.SaveSchedule(ctx, bad, now.Add(time.Minute)); err == nil {
t.Fatal("invalid scope edited a schedule", paths)
}
}
for _, change := range []string{"paths", "allow_tools"} {
bad := spec
if change == "paths" {
bad.WritePaths = []string{"different.go"}
} else {
bad.AllowTools = false
}
if _, err = s.SaveSchedule(ctx, bad, now, "restricted-create"); err == nil {
t.Fatal("scope missing from create payload hash", change)
}
}
other, err := Open(s.path)
if err != nil {
t.Fatal(err)
}
defer other.Close()
entries, err := other.ListSchedules(ctx, pid)
if err != nil || len(entries) != 1 || entries[0].Revision != a.Revision || entries[0].NextDue != a.NextDue || !entries[0].AllowTools || !slices.Equal(entries[0].WritePaths, spec.WritePaths) {
t.Fatal("scope changed or invalid save mutated state", entries, err)
}
// Rejected saves did not reserve their request key or consume an ID.
b, err := s.SaveSchedule(ctx, spec, now, "invalid-create")
if err != nil || b.ID != a.ID+1 {
t.Fatal(b, err)
}
}
+3
View File
@@ -212,6 +212,9 @@ func (s *Store) SaveSchedule(ctx context.Context, spec protocol.SavedSchedule, n
return nil, err
}
spec.Workspace = workspace
if err = ValidateRunWritePaths(workspace, spec.WritePaths); err != nil {
return nil, err
}
if prompt, ok := spec.Input["prompt"].(string); !ok || strings.TrimSpace(prompt) == "" {
return nil, fmt.Errorf("a nonempty user prompt is required in input.prompt")
}
+2
View File
@@ -20,6 +20,8 @@ type SavedSchedule struct {
Enabled bool `json:"enabled"`
Input map[string]any `json:"input"`
Workspace string `json:"workspace"`
WritePaths []string `json:"write_paths,omitempty"`
AllowTools bool `json:"allow_tools,omitempty"`
Model string `json:"model,omitempty"`
StageModels map[string]string `json:"stage_models,omitempty"`
NotifyMode string `json:"notify_mode,omitempty"`
+2 -26
View File
@@ -152,32 +152,8 @@ func (r *runner) startWithSchedule(request protocol.Message, origin *pipeline.Sc
if err != nil {
return nil, err
}
if len(request.WritePaths) > 20 {
return nil, fmt.Errorf("too many write paths")
}
for _, path := range request.WritePaths {
if filepath.IsAbs(path) || filepath.Clean(path) != path || path == ".." || strings.HasPrefix(path, ".."+string(os.PathSeparator)) || strings.ContainsAny(path, "*?\\") {
return nil, fmt.Errorf("optional write paths must be exact run-directory-relative paths")
}
parent := filepath.Join(workspace, path)
for {
_, err := os.Lstat(parent)
if err == nil {
break
}
if !os.IsNotExist(err) {
return nil, err
}
next := filepath.Dir(parent)
if next == parent {
return nil, fmt.Errorf("write path is unavailable")
}
parent = next
}
resolved, err := filepath.EvalSymlinks(parent)
if err != nil || (resolved != workspace && !strings.HasPrefix(resolved, workspace+string(os.PathSeparator))) {
return nil, fmt.Errorf("write path must stay within the run directory")
}
if err = pipeline.ValidateRunWritePaths(workspace, request.WritePaths); err != nil {
return nil, err
}
request.Input = maps.Clone(request.Input)
if request.Input == nil {
+123
View File
@@ -0,0 +1,123 @@
package server
import (
"context"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
"git.a-wan.net/alan/pipelines/internal/summarise"
)
func TestScheduledRunPreservesWriteScopeAndWorkflowPermissions(t *testing.T) {
for _, test := range []struct {
name string
allowTools bool
declaredTools string
}{
{"restricted coding", true, "edit"},
{"no tools", false, ""},
{"run flag does not grant undeclared tools", true, ""},
{"workflow tools remain authoritative", false, "edit"},
} {
t.Run(test.name, func(t *testing.T) {
ctx, s, r, pid := scheduleFixture(t)
version := 1
if test.declaredTools != "" {
source := strings.Replace(pipeline.Template([]string{"one"}, true), "```tools\n```", "```tools\n"+test.declaredTools+"\n```", 1)
var err error
version, err = s.Submit(ctx, "research", source)
if err != nil {
t.Fatal(err)
}
}
spec := protocol.SavedSchedule{Name: "restricted run", PipelineID: pid, Version: version, Enabled: true, Workspace: t.TempDir(), Input: map[string]any{"prompt": "scoped coding", "pwd": "/untrusted/input/folder"}, WritePaths: []string{"internal/one.go", "README.md"}, AllowTools: test.allowTools, Rule: protocol.Schedule{Every: "1m"}, NotifyMode: "off", Title: "Scoped task"}
entry, err := s.SaveSchedule(ctx, spec, time.Now())
if err != nil {
t.Fatal(err)
}
entries, err := s.ListSchedules(ctx, pid)
if err != nil || len(entries) != 1 || entries[0].AllowTools != test.allowTools || !slices.Equal(entries[0].WritePaths, spec.WritePaths) {
t.Fatal("saved scope roundtrip", entries, err)
}
calls := 0
r.execute = func(_ context.Context, def stage.Definition, input stage.Values) (stage.Result, error) {
calls++
if input["pwd"] != entry.Workspace {
t.Error("saved input.pwd overrode server workspace", input["pwd"])
}
tools, explicit, err := stage.ToolNames(def)
if err != nil || !explicit || strings.Join(tools, ",") != test.declaredTools {
t.Error("run flag widened stage permissions", tools, explicit, err)
}
// Scope is runtime policy, not prompt input: verify the committed run
// request at the fake executor boundary, before any native/model work.
runs, err := s.ListRuns(ctx)
if err != nil || len(runs) != 1 {
t.Error(runs, err)
return stage.Result{}, nil
}
detail, err := s.GetRun(ctx, runs[0].ID)
expectedTools := test.allowTools || test.declaredTools != ""
if err != nil || detail.AllowTools != expectedTools || !slices.Equal(detail.WritePaths, spec.WritePaths) || detail.Workspace != entry.Workspace || detail.Input["pwd"] != entry.Workspace {
t.Error("dispatch dropped scope", detail, err)
}
return stage.Result{Outcome: "success", Output: stage.Values{"done": true}}, nil
}
if err = r.scheduleTick(dueOf(t, entry), false); err != nil {
t.Fatal(err)
}
r.wg.Wait()
runs, err := s.ListRuns(ctx)
if err != nil || len(runs) != 1 || runs[0].Status != "succeeded" || calls != 1 {
t.Fatal(runs, calls, err)
}
})
}
}
func TestScheduleRechecksWriteScopeBeforeTitleOrExecution(t *testing.T) {
ctx, s, r, pid := scheduleFixture(t)
workspace := t.TempDir()
if err := os.Mkdir(filepath.Join(workspace, "allowed"), 0700); err != nil {
t.Fatal(err)
}
spec := protocol.SavedSchedule{Name: "restricted", PipelineID: pid, Version: 1, Enabled: true, Workspace: workspace, Input: map[string]any{"prompt": "task"}, WritePaths: []string{"allowed/new.go"}, AllowTools: true, Rule: protocol.Schedule{Every: "1m"}, NotifyMode: "off"}
entry, err := s.SaveSchedule(ctx, spec, time.Now())
if err != nil {
t.Fatal(err)
}
// A scope valid when saved can become unsafe before dispatch.
if err = os.Rename(filepath.Join(workspace, "allowed"), filepath.Join(workspace, "original")); err != nil {
t.Fatal(err)
}
if err = os.Symlink(t.TempDir(), filepath.Join(workspace, "allowed")); err != nil {
t.Fatal(err)
}
r.title = func(context.Context, string) summarise.Title {
t.Error("invalid scope reached title model")
return summarise.Title{}
}
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
t.Error("invalid scope reached executor")
return stage.Result{}, nil
}
if err = r.scheduleTick(dueOf(t, entry), false); err != nil {
t.Fatal(err)
}
r.wg.Wait()
// The exact same shared policy rejects manual admission as well.
if _, err = r.start(protocol.Message{PipelineID: pid, Version: 1, Workspace: workspace, Input: spec.Input, WritePaths: spec.WritePaths, AllowTools: true, RequestKey: "manual-unsafe"}); err == nil {
t.Fatal("manual path validation diverged")
}
runs, err := s.ListRuns(ctx)
if err != nil || len(runs) != 0 {
t.Fatal("invalid scope admitted work", runs, err)
}
}
+10 -1
View File
@@ -4,6 +4,7 @@ import (
"context"
"net/http/httptest"
"path/filepath"
"slices"
"strings"
"testing"
"time"
@@ -55,12 +56,15 @@ func TestScheduleAPIOverWebSocket(t *testing.T) {
return response
}
// Saving run information is not definition authoring, and works on run-only servers.
spec := protocol.SavedSchedule{Name: "first", PipelineID: items[0].ID, Version: 1, Enabled: true, Workspace: t.TempDir(), Input: map[string]any{"prompt": "first research", "arbitrary": "retained"}, Rule: protocol.Schedule{Every: "15m"}, Model: "openai/gpt", NotifyMode: "off", Title: "Research"}
spec := protocol.SavedSchedule{Name: "first", PipelineID: items[0].ID, Version: 1, Enabled: true, Workspace: t.TempDir(), Input: map[string]any{"prompt": "first research", "arbitrary": "retained"}, WritePaths: []string{"internal/one.go"}, AllowTools: true, Rule: protocol.Schedule{Every: "15m"}, Model: "openai/gpt", NotifyMode: "off", Title: "Research"}
response := rpc(protocol.Message{Type: protocol.SaveSchedule, Schedule: &spec, RequestKey: "editor-first"})
if response.Type != protocol.SavedScheduleReply || response.Schedule == nil || response.Schedule.ID == 0 || response.Schedule.Revision != 1 {
t.Fatal(response)
}
a := *response.Schedule
if !a.AllowTools || !slices.Equal(a.WritePaths, spec.WritePaths) {
t.Fatal("scope omitted from save reply", a)
}
response = rpc(protocol.Message{Type: protocol.SaveSchedule, Schedule: &spec, RequestKey: "editor-first"})
if response.Schedule == nil || response.Schedule.ID != a.ID || response.Schedule.Revision != a.Revision {
t.Fatal("lost reply duplicated timer", response)
@@ -82,6 +86,9 @@ func TestScheduleAPIOverWebSocket(t *testing.T) {
if response.Type != protocol.Schedules || len(response.Schedules) != 2 {
t.Fatal(response)
}
if !response.Schedules[0].AllowTools || !slices.Equal(response.Schedules[0].WritePaths, spec.WritePaths) {
t.Fatal("scope omitted from list reply", response)
}
a.Enabled = false
response = rpc(protocol.Message{Type: protocol.SaveSchedule, Schedule: &a})
if response.Schedule == nil || response.Schedule.Enabled || response.Schedule.Revision != 2 {
@@ -121,6 +128,8 @@ func TestScheduleAPIOverWebSocket(t *testing.T) {
func(s *protocol.SavedSchedule) { s.Model = "invalid" },
func(s *protocol.SavedSchedule) { s.NotifyMode = "invalid" },
func(s *protocol.SavedSchedule) { s.OutputFormat = "invalid" },
func(s *protocol.SavedSchedule) { s.WritePaths = []string{"../outside.go"} },
func(s *protocol.SavedSchedule) { s.WritePaths = []string{"*.go"} },
} {
bad := spec
mutate(&bad)
+1 -1
View File
@@ -72,7 +72,7 @@ func (r *runner) scheduleTick(now time.Time, recovery bool) error {
if !claimed || status != "claimed" {
continue
}
request := protocol.Message{PipelineID: item.PipelineID, Version: item.Version, RequestKey: fmt.Sprintf("saved-schedule:%d:%d", item.ID, due.UnixNano()), Input: item.Input, Workspace: item.Workspace, Model: item.Model, StageModels: item.StageModels, NotifyMode: item.NotifyMode, Title: item.Title, OutputFormat: item.OutputFormat}
request := protocol.Message{PipelineID: item.PipelineID, Version: item.Version, RequestKey: fmt.Sprintf("saved-schedule:%d:%d", item.ID, due.UnixNano()), Input: item.Input, Workspace: item.Workspace, WritePaths: item.WritePaths, AllowTools: item.AllowTools, Model: item.Model, StageModels: item.StageModels, NotifyMode: item.NotifyMode, Title: item.Title, OutputFormat: item.OutputFormat}
run, err := r.startWithSchedule(request, &pipeline.ScheduleAdmission{ID: item.ID, Revision: item.Revision, Due: due, Name: item.Name})
diagnostic := ""
status = "started"