61 lines
1.8 KiB
Go
61 lines
1.8 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"pipelines/internal/pipeline"
|
|
"pipelines/internal/protocol"
|
|
"pipelines/internal/stage"
|
|
"testing"
|
|
)
|
|
|
|
func TestRepositoryWorkspaceAllowlistAndAudit(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
store, err := pipeline.Open(filepath.Join(t.TempDir(), "workspace.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
root := t.TempDir()
|
|
a := filepath.Join(root, "repo-a")
|
|
b := filepath.Join(root, "repo-b")
|
|
os.Mkdir(a, 0700)
|
|
os.Mkdir(b, 0700)
|
|
outside := t.TempDir()
|
|
if _, err := store.Submit(ctx, "test", pipeline.Template([]string{"one"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, _ := store.List(ctx)
|
|
r, err := newRunner(ctx, store)
|
|
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} {
|
|
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)
|
|
}
|
|
r.wg.Wait()
|
|
detail, err = store.GetRun(ctx, detail.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
expected, _ := filepath.EvalSymlinks(path)
|
|
if detail.Workspace != expected || detail.Status != "succeeded" {
|
|
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"} {
|
|
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)
|
|
}
|
|
}
|
|
}
|