security: isolate artifact documents and default servers to execution-only
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user