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 := `Safe report

Readable report

` + if err := os.WriteFile(filepath.Join(directory, "attack.html"), []byte(attack), 0600); err != nil { + t.Fatal(err) + } + var requests atomic.Int32 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/attack" { + requests.Add(1) + w.WriteHeader(http.StatusNoContent) + return + } + handler.ServeHTTP(w, r) + })) + defer srv.Close() + cmd := exec.Command("node", "../../web/test/artifact-security-probe.mjs", srv.URL+artifactURL(id, step.Sequence, "attack.html")) + if output, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("browser isolation: %v\n%s", err, output) + } else { + t.Log(string(output)) + } + if requests.Load() != 0 { + t.Fatal("artifact script reached the control origin") + } +} + func TestArtifactHTTPGetPublicSharedFile(t *testing.T) { handler, step := runWithArtifact(t) var shared protocol.StageArtifact diff --git a/internal/server/authoring_test.go b/internal/server/authoring_test.go new file mode 100644 index 00000000..e13b4665 --- /dev/null +++ b/internal/server/authoring_test.go @@ -0,0 +1,59 @@ +package server + +import ( + "context" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + "time" + + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" + "github.com/coder/websocket" + "github.com/coder/websocket/wsjson" +) + +func TestExecutionServerRefusesPublicationByDefault(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + store, err := pipeline.Open(filepath.Join(t.TempDir(), "definitions.db")) + if err != nil { + t.Fatal(err) + } + defer store.Close() + handler := Handler(ctx, store) + defer func() { cancel(); WaitRuns(handler) }() + srv := httptest.NewServer(handler) + defer srv.Close() + conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil) + if err != nil { + t.Fatal(err) + } + defer conn.CloseNow() + var reply protocol.Message + if err := wsjson.Read(ctx, conn, &reply); err != nil { + t.Fatal(err) + } + source := pipeline.Template([]string{"work"}, true) + // This is the same publication envelope used by submit, import, templates, + // and raw RPC; no client-selected flag can enable the server capability. + for _, kind := range []string{protocol.Validate, protocol.Submit} { + if err := wsjson.Write(ctx, conn, protocol.Message{ID: kind, Type: kind, Name: "unauthorized", Source: source}); err != nil { + t.Fatal(err) + } + if err := wsjson.Read(ctx, conn, &reply); err != nil { + t.Fatal(err) + } + if kind == protocol.Validate && reply.Type != protocol.Validated { + t.Fatalf("validation should remain read-only: %+v", reply) + } + if kind == protocol.Submit && (reply.Type != protocol.Error || !strings.Contains(reply.Error, "authoring is disabled")) { + t.Fatalf("publication bypassed gate: %+v", reply) + } + } + items, err := store.List(ctx) + if err != nil || len(items) != 0 { + t.Fatalf("disabled authoring changed definitions: %+v %v", items, err) + } +} diff --git a/internal/server/schedule_websocket_test.go b/internal/server/schedule_websocket_test.go index c88af102..00cb2ded 100644 --- a/internal/server/schedule_websocket_test.go +++ b/internal/server/schedule_websocket_test.go @@ -21,7 +21,7 @@ func TestScheduleSubmissionOverWebSocket(t *testing.T) { t.Fatal(err) } defer store.Close() - handler := Handler(ctx, store) + handler := HandlerWithOptions(ctx, Options{AllowAuthoring: true}, store) defer func() { cancel(); WaitRuns(handler) }() srv := httptest.NewServer(handler) defer srv.Close() diff --git a/internal/server/templates_test.go b/internal/server/templates_test.go index 6471d34e..b23ff5f5 100644 --- a/internal/server/templates_test.go +++ b/internal/server/templates_test.go @@ -22,7 +22,7 @@ func TestTemplatesDoNotSeedDatabaseAndCreateOnlyPreservesExisting(t *testing.T) t.Fatal(err) } defer store.Close() - handler := Handler(ctx, store) + handler := HandlerWithOptions(ctx, Options{AllowAuthoring: true}, store) defer func() { cancel(); WaitRuns(handler) }() srv := httptest.NewServer(handler) defer srv.Close() diff --git a/internal/server/websocket.go b/internal/server/websocket.go index 5fc1ebfa..68c2c64b 100644 --- a/internal/server/websocket.go +++ b/internal/server/websocket.go @@ -20,6 +20,10 @@ import ( // Options configures the HTTP handler. type Options struct { + // AllowAuthoring permits publishing immutable definitions. Execution servers + // default to run-only: a CLI --yes flag or raw RPC cannot grant this server + // capability. Enable deliberately on a separate trusted authoring instance. + AllowAuthoring bool // ArtifactsRoot overrides where stage artifacts live. Empty uses the store's // directory (the database's parent), matching the data-directory default. ArtifactsRoot string @@ -175,6 +179,10 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin return } log.Printf("command received: client=%s type=%q id=%q pipeline=%d version=%d", r.RemoteAddr, request.Type, request.ID, request.PipelineID, request.Version) + if request.Type == protocol.Submit && !options.AllowAuthoring { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "pipeline authoring is disabled on this execution server; ask the operator to publish through an explicitly enabled authoring server"}) + continue + } if handleChat(connCtx, request, options.Chat, hub, registry, clientID, write) { continue } diff --git a/web/test/artifact-security-probe.mjs b/web/test/artifact-security-probe.mjs new file mode 100644 index 00000000..8018b573 --- /dev/null +++ b/web/test/artifact-security-probe.mjs @@ -0,0 +1,20 @@ +// Invoked by the Go scratch-server test, never against the deployed service. +import assert from "node:assert/strict"; +import { launchChrome } from "./browser.mjs"; + +const url = process.argv[2]; +assert.ok(url?.startsWith("http://127.0.0.1:"), "scratch loopback fixture required"); +const browser = await launchChrome({ width: 390, height: 844 }); +try { + const page = await browser.openPage("about:blank"); + await page.goto(url); + await page.waitForSelector("#report"); + assert.equal(await page.evaluate("document.title"), "Safe report"); + assert.equal(await page.evaluate("document.querySelector('#report').textContent"), "Readable report"); + assert.equal(await page.evaluate("getComputedStyle(document.querySelector('#report')).color"), "rgb(12, 34, 56)"); + // The inline attack would change the title and call /attack if it ran. The + // server-side test also asserts that endpoint received no request. + console.log("Raw HTML report is readable; inline script blocked by response policy."); +} finally { + await browser.close(); +}