package release import ( "crypto/sha256" "encoding/hex" "encoding/json" "io" "io/fs" "net/http" "os" "path/filepath" "reflect" "slices" "strings" "sync" ) // InstallAssets is configured before use and must not be copied after use. type InstallAssets struct { Root, Version string Manifest Manifest mu sync.Mutex files map[string]os.FileInfo available bool // darwin: the release lists a Mac worker and its files verified. darwin bool embedded fs.FS digest func(io.Reader) (string, error) } func NewInstallAssets(root, version string, manifest Manifest) *InstallAssets { a := &InstallAssets{Root: root, Version: version, Manifest: manifest, embedded: embeddedInstallFiles()} a.Available() return a } var installAssetNames = []string{"install-manifest.json", "equipo-runner", "equipo-tool-proxy", "install-v1.sh"} // darwinAssetNames are the Mac worker's files, served when the release // manifest lists the darwin/arm64 target and they match its digests. var darwinAssetNames = []string{DarwinDir + "/equipo-runner", DarwinDir + "/gitleaks"} type installFile interface { fs.File io.ReadSeeker } func (a *InstallAssets) open(name string) (installFile, error) { if a.Root == "" { if a.embedded == nil { return nil, errManifest } f, err := a.embedded.Open(name) if err != nil { return nil, err } seekable, ok := f.(installFile) if !ok { f.Close() return nil, errManifest } return seekable, nil } if !filepath.IsAbs(a.Root) || filepath.Clean(a.Root) != a.Root { return nil, errManifest } path := filepath.Join(a.Root, name) real, e := filepath.EvalSymlinks(path) if e != nil || real != path { return nil, errManifest } f, e := os.Open(path) if e != nil { return nil, e } i, e := f.Stat() limit := int64(128 << 20) switch name { case "install-manifest.json", "install-v1.sh": limit = 1 << 20 } if e != nil || !i.Mode().IsRegular() || i.Size() > limit { f.Close() return nil, errManifest } return f, nil } func sameInstallFile(a, b os.FileInfo) bool { return a != nil && b != nil && os.SameFile(a, b) && a.Size() == b.Size() && a.ModTime() == b.ModTime() && a.Mode() == b.Mode() } func (a *InstallAssets) unchanged() bool { if a.Root == "" { return a.files != nil } for name := range a.files { f, e := a.open(name) if e != nil { return false } i, e := f.Stat() f.Close() if e != nil || !sameInstallFile(a.files[name], i) { return false } } return true } // CachedAvailable only checks metadata. Page rendering never hashes a release. func (a *InstallAssets) CachedAvailable() bool { a.mu.Lock() defer a.mu.Unlock() return a.available && a.unchanged() } func (a *InstallAssets) Available() bool { a.mu.Lock() defer a.mu.Unlock() if !ValidInstallVersion(a.Version) || !a.Manifest.Installable() { return false } if a.files != nil && a.unchanged() { return a.available } a.available, a.darwin = false, false files := map[string]installFile{} infos := map[string]os.FileInfo{} defer func() { for _, f := range files { f.Close() } }() for _, name := range installAssetNames { f, e := a.open(name) if e != nil { a.files = nil return false } files[name] = f infos[name], e = f.Stat() if e != nil { a.files = nil return false } } a.files = infos var m InstallManifest decoder := json.NewDecoder(files["install-manifest.json"]) if decoder.Decode(&m) != nil || m.Validate(a.Version) != nil || !reflect.DeepEqual(m.Release, a.Manifest) { return false } if decoder.Decode(new(any)) != io.EOF { return false } digest := a.digest if digest == nil { digest = func(r io.Reader) (string, error) { h := sha256.New() _, e := io.Copy(h, r) return hex.EncodeToString(h.Sum(nil)), e } } for name, want := range map[string]string{"equipo-runner": m.RunnerSHA256, "equipo-tool-proxy": a.Manifest.ToolProxySHA256()} { got, e := digest(files[name]) if e != nil || want == "" || got != want { return false } } if want := m.Targets[TargetLinux].GitleaksSHA256; want != "" { f, err := a.open("gitleaks") if err != nil { return false } files["gitleaks"] = f got, err := digest(f) if err != nil || got != want { return false } infos["gitleaks"], err = f.Stat() if err != nil { return false } } // The page renderer is not served here: the Linux runner fetches it // from the forge (RendererForge), checked against what is linked into // it. An instance carrying it would pass the platform's ship cap. // The Mac worker: listed, present and matching, or not served at all. if t, e := m.DarwinTarget(a.Version); e == nil { mac := map[string]installFile{} for _, name := range darwinAssetNames { if f, e := a.open(name); e == nil { mac[name] = f files[name] = f } } ok := len(mac) == len(darwinAssetNames) if t.GitleaksSHA256 == "" { ok = mac[darwinAssetNames[0]] != nil } wants := map[string]string{darwinAssetNames[0]: t.RunnerSHA256} if t.GitleaksSHA256 != "" { wants[darwinAssetNames[1]] = t.GitleaksSHA256 } for name, want := range wants { if !ok { break } info, e := mac[name].Stat() got, de := digest(mac[name]) ok = e == nil && de == nil && got == want if ok { infos[name] = info } } a.darwin = ok if ok { a.files = infos } } // A replacement or write during verification must not enter the cache. if !a.unchanged() { a.files = nil return false } a.available = true return true } func (a *InstallAssets) ServeHTTP(w http.ResponseWriter, r *http.Request) { prefix := "/releases/" + a.Version + "/" name := strings.TrimPrefix(r.URL.Path, prefix) mac := slices.Contains(darwinAssetNames, name) if !strings.HasPrefix(r.URL.Path, prefix) || (r.Method != "GET" && r.Method != "HEAD") || (name != "install-v1.sh" && name != "install-manifest.json" && name != "equipo-runner" && name != "equipo-tool-proxy" && name != "gitleaks" && !mac) { w.WriteHeader(http.StatusNotFound) return } if !a.Available() { w.WriteHeader(http.StatusServiceUnavailable) return } a.mu.Lock() served := (!mac || a.darwin) && a.files[name] != nil a.mu.Unlock() if !served { w.WriteHeader(http.StatusNotFound) return } f, e := a.open(name) if e != nil { w.WriteHeader(http.StatusServiceUnavailable) return } defer f.Close() i, e := f.Stat() a.mu.Lock() valid := e == nil && a.available && (a.Root == "" || sameInstallFile(a.files[name], i)) a.mu.Unlock() if !valid { w.WriteHeader(http.StatusServiceUnavailable) return } w.Header().Set("Content-Type", "application/octet-stream") w.Header().Set("X-Content-Type-Options", "nosniff") w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") http.ServeContent(w, r, name, i.ModTime(), f) }