Remove workspace-root configuration and use client run directories

This commit is contained in:
2026-10-09 00:32:00 +01:00
parent 4697bb4465
commit c5a5521015
17 changed files with 97 additions and 116 deletions
+12
View File
@@ -9,6 +9,7 @@ import (
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
@@ -49,6 +50,17 @@ func TestCLIHeadlessRunInspectionCancellationAndRPC(t *testing.T) {
if response.RunDetail.Input["prompt"] != "exact task" || response.RunDetail.AllowTools {
t.Fatal("prompt or approval changed", response)
}
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
cwd, err = filepath.EvalSymlinks(cwd)
if err != nil {
t.Fatal(err)
}
if response.RunDetail.Workspace != cwd {
t.Fatal("CLI did not use invocation directory", response.RunDetail.Workspace, cwd)
}
output, err = run("run", "workflow", "exact task", "--yes", "--json", "--request-key", "same-key")
if err != nil {
t.Fatal(err)
+7 -26
View File
@@ -16,14 +16,13 @@ import (
)
type runner struct {
ctx context.Context
store *pipeline.Store
mu sync.Mutex
active map[int64]context.CancelFunc
execute func(context.Context, stage.Definition, stage.Values) (stage.Result, error)
wg sync.WaitGroup
schedulerWG sync.WaitGroup
workspaceRoots []string
ctx context.Context
store *pipeline.Store
mu sync.Mutex
active map[int64]context.CancelFunc
execute func(context.Context, stage.Definition, stage.Values) (stage.Result, error)
wg sync.WaitGroup
schedulerWG sync.WaitGroup
}
func newRunner(ctx context.Context, s *pipeline.Store) (*runner, error) {
@@ -69,24 +68,6 @@ func (r *runner) start(request protocol.Message) (*protocol.RunDetail, error) {
if err != nil || !info.IsDir() {
return nil, fmt.Errorf("run workspace must be a directory")
}
allowed := selected == workspace
for _, root := range r.workspaceRoots {
root, err = filepath.Abs(root)
if err != nil {
return nil, err
}
root, err = filepath.EvalSymlinks(root)
if err != nil {
return nil, fmt.Errorf("configured workspace root is unavailable")
}
rel, err := filepath.Rel(root, selected)
if err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
allowed = true
}
}
if !allowed {
return nil, fmt.Errorf("run workspace is outside the server's allowed workspace roots")
}
workspace = selected
}
if len(request.WritePaths) > 20 {
-10
View File
@@ -17,22 +17,12 @@ import (
// Handler serves the persistent connection. Canceling ctx stops active clients.
func Handler(ctx context.Context, stores ...*pipeline.Store) http.Handler {
return handler(ctx, nil, stores...)
}
// HandlerForWorkspaces permits run workspaces only below explicitly configured roots.
func HandlerForWorkspaces(ctx context.Context, store *pipeline.Store, roots []string) http.Handler {
return handler(ctx, roots, store)
}
func handler(ctx context.Context, roots []string, stores ...*pipeline.Store) http.Handler {
mux := http.NewServeMux()
var runs *runner
var runErr error
if len(stores) > 0 {
runs, runErr = newRunner(ctx, stores[0])
if runErr == nil {
runs.workspaceRoots = append([]string(nil), roots...)
runs.schedulerWG.Add(1)
go runs.scheduleLoop()
}
+9 -6
View File
@@ -10,7 +10,7 @@ import (
"testing"
)
func TestRepositoryWorkspaceAllowlistAndAudit(t *testing.T) {
func TestClientSelectedRepositoryWorkspaceAndAudit(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "workspace.db"))
@@ -32,11 +32,11 @@ func TestRepositoryWorkspaceAllowlistAndAudit(t *testing.T) {
if err != nil {
t.Fatal(err)
}
r.workspaceRoots = []string{root}
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
return stage.Result{Outcome: "success", Output: stage.Values{"done": true}}, nil
}
for i, path := range []string{a, b} {
os.Symlink(outside, filepath.Join(root, "linked-repo"))
for i, path := range []string{a, b, outside, filepath.Join(root, "linked-repo")} {
detail, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: path, Input: map[string]any{"prompt": "task"}, Workspace: path})
if err != nil {
t.Fatal(err)
@@ -51,10 +51,13 @@ func TestRepositoryWorkspaceAllowlistAndAudit(t *testing.T) {
t.Fatal("incorrect workspace audit", i, detail)
}
}
os.Symlink(outside, filepath.Join(root, "escape"))
for _, path := range []string{outside, filepath.Join(root, "escape"), "relative/repo"} {
file := filepath.Join(root, "file")
if err := os.WriteFile(file, []byte("not a directory"), 0600); err != nil {
t.Fatal(err)
}
for _, path := range []string{filepath.Join(root, "missing"), file, "relative/repo"} {
if _, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: path, Input: map[string]any{"prompt": "task"}, Workspace: path}); err == nil {
t.Fatal("unauthorized workspace accepted", path)
t.Fatal("invalid run directory accepted", path)
}
}
}