diff --git a/cmd/pipelines-server/main.go b/cmd/pipelines-server/main.go index 273a2bc4..fec2be21 100644 --- a/cmd/pipelines-server/main.go +++ b/cmd/pipelines-server/main.go @@ -22,6 +22,7 @@ import ( func main() { addr := flag.String("addr", "127.0.0.1:8080", "HTTP/WebSocket listen address") + allowAuthoring := flag.Bool("allow-authoring", false, "Explicitly enable pipeline definition publication (disabled on execution servers by default)") // -opencode-endpoint opts in to brokering chat from an operator-supplied // OpenCode service instead of the spawned default. Its default is the // environment variable, so both are the same opt-in; the recorded choice (set @@ -47,7 +48,7 @@ func main() { // Each new run gets a bounded one-line title from the configured default // harness; the summariser falls back to the prompt's first line, so title // generation never blocks or fails run creation. - options := server.Options{ArtifactsRoot: artifactsRoot, RunTitles: summarise.GenerateTitle, StepVerdicts: summarise.GenerateVerdictReason, VerificationFixtures: server.VerificationFixturesFromEnvironment(os.Getenv)} + options := server.Options{AllowAuthoring: *allowAuthoring, ArtifactsRoot: artifactsRoot, RunTitles: summarise.GenerateTitle, StepVerdicts: summarise.GenerateVerdictReason, VerificationFixtures: server.VerificationFixturesFromEnvironment(os.Getenv)} recorded := "" if value, err := store.OpenCodeEndpoint(ctx); err != nil { log.Printf("OpenCode endpoint setting unreadable: %v", err) diff --git a/docs/system-hardening.md b/docs/system-hardening.md new file mode 100644 index 00000000..974693bb --- /dev/null +++ b/docs/system-hardening.md @@ -0,0 +1,50 @@ +# Execution safety and reliability + +## Artifact trust boundary + +Generated files are untrusted input, not application code. Artifact responses +carry a CSP sandbox with no script, form, or connection permission, plus +`nosniff` and `private, no-store`. This applies to direct/raw links as well as +embedded previews. Inline CSS and same-origin/data images remain readable; +interactive HTML applications are deliberately not supported as trusted reports. + +The opt-in scratch-browser regression is: + +```sh +PIPELINES_BROWSER_TEST=1 go test ./internal/server -run TestArtifactHTMLBrowserIsolation -v +``` + +It serves a malicious fixture from an ephemeral server and checks that the report +remains readable without executing its script or contacting the control origin. +It never uses a deployed server or model. + +## Definition publication is a server capability + +The server defaults to execution-only. Browsing and validation are read-only; +publication via `submit`, import, template creation, or raw RPC is refused unless +the operator starts the server with `-allow-authoring` (also accepted through +`pipelines serve`). No client `--yes` flag can enable this capability. + +Enable it deliberately for authorized definition changes, preferably in a +separate trusted authoring deployment; leave it disabled for execution workers. +This is a deployment-level gate, not a claim that a program can infer human +intent. An enabled authoring server still trusts its authenticated operator. +An agent with the operator's full OS authority can alter configuration or access +the database: preventing that requires an actual OS/service identity boundary, +not more command-string patterns. Do not give execution agents the authoring +service's credentials or database access. + +## Shell tools are trusted local execution + +Tool declarations, edit paths, and protected-file read rules are useful controls, +but an arbitrary shell is not a filesystem jail. Programs launched by the shell +can use the host user's permissions, credentials, and network. Use a dedicated +unprivileged account, VM, or container with an explicit workspace mount for +untrusted definitions. Do not advertise prompt instructions as sandboxing. + +## Delivery discipline + +Checks and browser probes use scratch databases and loopback ports. Installing +changes and restarting shared services are separate operator actions; restarts +must not be used to test cancellation or recover a run while unrelated work is +active. Preserve existing sessions, outputs, branches, and database backups. diff --git a/internal/cli/app_test.go b/internal/cli/app_test.go index 415baaf9..5545f7d5 100644 --- a/internal/cli/app_test.go +++ b/internal/cli/app_test.go @@ -26,7 +26,7 @@ func setup(t *testing.T) (func(...string) (string, error), *pipeline.Store, stri if err != nil { t.Fatal(err) } - handler := server.Handler(ctx, store) + handler := server.HandlerWithOptions(ctx, server.Options{AllowAuthoring: true}, store) http := httptest.NewServer(handler) t.Cleanup(func() { cancel(); http.Close(); server.WaitRuns(handler); store.Close() }) url := "ws" + strings.TrimPrefix(http.URL, "http") + "/connect" diff --git a/internal/server/artifacts.go b/internal/server/artifacts.go index ed9b61ae..041b3bd2 100644 --- a/internal/server/artifacts.go +++ b/internal/server/artifacts.go @@ -270,5 +270,12 @@ func (r *runner) serveArtifact(w http.ResponseWriter, req *http.Request) { http.NotFound(w, req) return } + // An artifact is untrusted generated content, not part of the control UI. + // Enforce the boundary on the response itself: opening the raw HTML/SVG in + // a new tab must be as safe as the embedded sandboxed preview. No scripts, + // network calls, forms, same-origin authority or top-level navigation. + w.Header().Set("Content-Security-Policy", "sandbox; default-src 'none'; script-src 'none'; connect-src 'none'; style-src 'unsafe-inline'; img-src 'self' data:; media-src 'self' data:; base-uri 'none'; form-action 'none'; frame-ancestors 'self'") + w.Header().Set("X-Content-Type-Options", "nosniff") + w.Header().Set("Cache-Control", "private, no-store") http.ServeContent(w, req, path.Base(name), opened.ModTime(), file) } diff --git a/internal/server/artifacts_test.go b/internal/server/artifacts_test.go index baf538b1..55903dc5 100644 --- a/internal/server/artifacts_test.go +++ b/internal/server/artifacts_test.go @@ -8,9 +8,11 @@ import ( "net/http" "net/http/httptest" "os" + "os/exec" "path/filepath" "reflect" "strings" + "sync/atomic" "testing" "git.a-wan.net/alan/pipelines/internal/pipeline" @@ -261,6 +263,14 @@ func TestArtifactHTTPGetHeadRangeAndRefusals(t *testing.T) { if cl := resp.Header.Get("Content-Length"); cl != fmt.Sprint(len(payload)) { t.Fatalf("GET content length wrong: %q", cl) } + for _, directive := range []string{"sandbox;", "script-src 'none'", "connect-src 'none'", "form-action 'none'", "frame-ancestors 'self'"} { + if !strings.Contains(resp.Header.Get("Content-Security-Policy"), directive) { + t.Fatalf("artifact lacks response isolation %q: %v", directive, resp.Header) + } + } + if resp.Header.Get("X-Content-Type-Options") != "nosniff" || resp.Header.Get("Cache-Control") != "private, no-store" { + t.Fatalf("artifact content/cache policy = %v", resp.Header) + } head, err := http.Head(base) if err != nil { @@ -314,6 +324,40 @@ func TestArtifactHTTPGetHeadRangeAndRefusals(t *testing.T) { } } +// Opt-in real browser regression. The server, database, run and malicious file +// are all test fixtures; no model or deployed service is used. +func TestArtifactHTMLBrowserIsolation(t *testing.T) { + if os.Getenv("PIPELINES_BROWSER_TEST") != "1" { + t.Skip("set PIPELINES_BROWSER_TEST=1 for the scratch Chrome check") + } + handler, step := runWithArtifact(t) + id := runIDFromURL(step.Artifacts[0].URL) + directory := filepath.Join(handler.runner.artifactsRoot, fmt.Sprint(id), "stages", fmt.Sprintf("%02d-%s", step.Sequence, step.Stage)) + attack := `