package isolation import ( "bytes" "context" "crypto/sha256" "encoding/base64" "encoding/json" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "testing" ) func TestInstallPythonRuntimeUsesVerifiedMirrorAndAtomicDestination(t *testing.T) { const version = "3.12.15" archive := []byte("verified CPython test archive") digest := sha256.Sum256(archive) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/" + version + "/Python-" + version + ".tar.xz.sigstore": _ = json.NewEncoder(w).Encode(map[string]any{"messageSignature": map[string]any{"messageDigest": map[string]any{"algorithm": "SHA2_256", "digest": base64.StdEncoding.EncodeToString(digest[:])}}}) case "/" + version + "/Python-" + version + ".tar.xz": _, _ = w.Write(archive) default: http.NotFound(w, r) } })) defer server.Close() managerRoot := filepath.Join(t.TempDir(), "versions") buildRoot := filepath.Join(t.TempDir(), "builds") if err := os.MkdirAll(managerRoot, 0755); err != nil { t.Fatal(err) } finalRoot := filepath.Join(managerRoot, version) commands := make([]string, 0, 5) agent := Agent{ PythonManagerRoot: managerRoot, PythonBuildRoot: buildRoot, PythonMetadataBaseURL: server.URL, PythonSourceBaseURLs: []string{server.URL}, runCommand: func(_ context.Context, name string, args ...string) error { commands = append(commands, name+" "+strings.Join(args, " ")) switch name { case "tar": sourceRoot := filepath.Join(args[len(args)-1], "Python-"+version) if err := os.MkdirAll(sourceRoot, 0700); err != nil { return err } return os.WriteFile(filepath.Join(sourceRoot, "configure"), []byte("test"), 0755) case "make": for _, argument := range args { if strings.HasPrefix(argument, "DESTDIR=") { stageBase := strings.TrimPrefix(argument, "DESTDIR=") python := filepath.Join(stagedPythonInstallRoot(stageBase, finalRoot), "bin", "python3") if err := os.MkdirAll(filepath.Dir(python), 0755); err != nil { return err } return os.WriteFile(python, []byte("test"), 0755) } } } return nil }, } body, _ := json.Marshal(PythonRuntimeInstallSpec{Version: version}) response := httptest.NewRecorder() agent.Handler().ServeHTTP(response, httptest.NewRequest(http.MethodPost, "/v1/runtime/python/install", bytes.NewReader(body))) if response.Code != http.StatusOK { t.Fatalf("install status = %d, body = %s", response.Code, response.Body.String()) } if _, err := os.Stat(filepath.Join(finalRoot, "bin", "python3")); err != nil { t.Fatal(err) } joined := strings.Join(commands, "\n") if !strings.Contains(joined, "--prefix="+finalRoot) || !strings.Contains(joined, "DESTDIR="+managerRoot) { t.Fatalf("Python build was not confined to managed paths:\n%s", joined) } } func TestInstallPythonRuntimeRejectsUnsupportedBranch(t *testing.T) { agent := Agent{} body, _ := json.Marshal(PythonRuntimeInstallSpec{Version: "3.9.25"}) response := httptest.NewRecorder() agent.Handler().ServeHTTP(response, httptest.NewRequest(http.MethodPost, "/v1/runtime/python/install", bytes.NewReader(body))) if response.Code != http.StatusBadRequest { t.Fatalf("status = %d, want 400", response.Code) } } func TestPythonSourceDigestMismatchCannotReachExtraction(t *testing.T) { const version = "3.12.15" digest := sha256.Sum256([]byte("official source")) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if strings.HasSuffix(r.URL.Path, ".sigstore") { _ = json.NewEncoder(w).Encode(map[string]any{"messageSignature": map[string]any{"messageDigest": map[string]any{"algorithm": "SHA2_256", "digest": base64.StdEncoding.EncodeToString(digest[:])}}}) return } _, _ = w.Write([]byte("tampered source")) })) defer server.Close() managerRoot, buildRoot := t.TempDir(), t.TempDir() called := false agent := Agent{ PythonManagerRoot: managerRoot, PythonBuildRoot: buildRoot, PythonMetadataBaseURL: server.URL, PythonSourceBaseURLs: []string{server.URL}, runCommand: func(context.Context, string, ...string) error { called = true; return nil }, } if err := agent.installPythonSource(context.Background(), version); err == nil || !strings.Contains(err.Error(), "digest mismatch") { t.Fatalf("mismatched archive result = %v", err) } if called { t.Fatal("unverified source reached extraction or compilation") } if _, err := os.Stat(filepath.Join(managerRoot, version)); !os.IsNotExist(err) { t.Fatal("unverified source created an installed version") } if _, err := os.Stat(filepath.Join(buildRoot, "Python-"+version+".tar.xz")); !os.IsNotExist(err) { t.Fatal("unverified archive was retained") } }