package release import ( "encoding/hex" "net/url" "reflect" "regexp" "strings" "amadan.net/oficina/equipo/internal/runner/provider" ) // InstallManifest is transported over HTTPS from the configured instance. The // runner digest lives outside the embedded policy manifest to avoid a hash cycle. type InstallManifest struct { Gitleaks string `json:"gitleaks,omitempty"` Version string `json:"version"` Target string `json:"target"` RunnerSHA256 string `json:"runner_sha256"` NamespaceArgs []string `json:"namespace_args"` Release Manifest `json:"release"` // Targets are every platform's runner in this release with its // digests (Mac runners spec ยง3.1): linux/amd64 (the top-level fields // above, kept for older bootstraps) and darwin/arm64, a Mac worker // served under DarwinDir. Targets map[string]InstallTarget `json:"targets,omitempty"` } // InstallTarget is one platform's runner and tool proxy digests, and on // Linux the renderer's: the SHA-256 of renderer.tar.gz's MANIFEST, the // pin linked into that runner (rendererSHA256), and the archive's own // SHA-256 and size (rendererArchiveSHA256, rendererArchiveSize). The // archive is published on the forge, not served by the instance; these // say which one, and the runner refuses a manifest that disagrees with // what is linked into it. type InstallTarget struct { GitleaksSHA256 string `json:"gitleaks_sha256,omitempty"` RendererSHA256 string `json:"renderer_sha256,omitempty"` RendererArchiveSHA256 string `json:"renderer_archive_sha256,omitempty"` RendererArchiveSize int64 `json:"renderer_archive_size,omitempty"` RunnerSHA256 string `json:"runner_sha256"` ToolProxySHA256 string `json:"tool_proxy_sha256,omitempty"` } // The Mac worker's target and where its files are served, under // /releases//. const ( TargetLinux = "linux/amd64" TargetDarwin = "darwin/arm64" DarwinDir = "darwin-arm64" ) // DarwinTarget is the Mac runner of a release manifest, checked: the // version this runner asked for and two SHA-256 digests. A Mac worker // runs checks only, so the Linux runner's pinned programs do not apply to it. func (m InstallManifest) DarwinTarget(version string) (InstallTarget, error) { t, ok := m.Targets[TargetDarwin] if !ValidInstallVersion(version) || m.Version != version || !ok || !sha256Hex(t.RunnerSHA256) { return InstallTarget{}, errManifest } return t, nil } func sha256Hex(s string) bool { b, err := hex.DecodeString(s) return err == nil && len(b) == 32 && strings.ToLower(s) == s } var installVersion = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._-]{0,79}$`) func ValidInstallVersion(v string) bool { return installVersion.MatchString(v) && v != "dev" && v != "latest" } func InstallOrigin(origin string) bool { u, e := url.Parse(origin) return e == nil && u.Scheme == "https" && u.Host != "" && u.User == nil && u.Path == "" && u.RawQuery == "" && !u.ForceQuery && u.Fragment == "" && !strings.ContainsAny(origin, "\r\n\x00'\"`$\\#") } // Installable reports whether this build can install or serve a runner: // the Linux target, the pinned bubblewrap, the launcher and policy this // code builds, and a digest for each of the five executables, the tool // proxy's among them. func (m Manifest) Installable() bool { if m.Schema != 3 || m.RunnerTarget != "linux/amd64" || m.BwrapVersion != "0.11.0" || m.LauncherVersion != provider.LauncherVersion || m.PolicyVersion != provider.PolicyVersion || len(m.Binaries) != 5 || m.ToolProxySHA256() == "" { return false } seen := map[string]bool{} for _, b := range m.Binaries { if seen[b.Name] || !sha256Hex(b.SHA256) { return false } seen[b.Name] = true } for _, name := range []string{"claude", "codex", "codex-code-mode-host", "bwrap", "equipo-tool-proxy"} { if !seen[name] { return false } } return true } func (m InstallManifest) Validate(version string) error { // The tool proxy the install manifest lists is the one the release // recorded: one build, one digest, in both places. if m.Gitleaks != "" && m.Gitleaks != GitleaksVersion { return errManifest } linux := m.Targets[TargetLinux] if !ValidInstallVersion(version) || m.Version != version || m.Target != TargetLinux || !sha256Hex(m.RunnerSHA256) || linux.RunnerSHA256 != m.RunnerSHA256 || linux.ToolProxySHA256 != m.Release.ToolProxySHA256() || !m.Release.Installable() || !reflect.DeepEqual(m.NamespaceArgs, provider.NamespaceArguments()) { return errManifest } // A renderer pin comes with its archive's digest and size, and an // archive with a pin. archive := linux.RendererArchiveSHA256 != "" || linux.RendererArchiveSize != 0 if archive != (linux.RendererSHA256 != "") || archive && (!sha256Hex(linux.RendererSHA256) || !sha256Hex(linux.RendererArchiveSHA256) || linux.RendererArchiveSize <= 0 || linux.RendererArchiveSize > RendererArchiveMax) { return errManifest } return nil } // InstallCommand uses positional arguments and shell quoting, never an origin // or path interpolated as shell source. The bootstrap verifies before executing. func InstallCommand(origin, version string, instance ...string) (string, error) { if !InstallOrigin(origin) || !ValidInstallVersion(version) { return "", errManifest } extra := "" if len(instance) > 0 { if !installVersion.MatchString(instance[0]) { return "", errManifest } extra = " '" + instance[0] + "'" } return "curl --fail --silent --show-error --proto '=https' --tlsv1.2 '" + origin + "/releases/" + version + "/install-v1.sh' | sh -s -- '" + origin + "' '" + version + "'" + extra, nil } // ToolProxySHA256 is the recorded digest of this release's tool proxy, or // "" when the build recorded none or the entry is malformed. func (m Manifest) ToolProxySHA256() string { hash := "" for _, b := range m.Binaries { if b.Name == "equipo-tool-proxy" { if hash != "" || b.Version != m.LauncherVersion || !sha256Hex(b.SHA256) { return "" } hash = b.SHA256 } } return hash }