security: isolate artifact documents and default servers to execution-only
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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 := `<!doctype html><title>Safe report</title><style>#report {color:rgb(12,34,56)}</style><h1 id="report">Readable report</h1><script>document.title='executed';fetch('/attack')</script>`
|
||||
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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user