| 1 | package release |
| 2 | |
| 3 | import ( |
| 4 | "encoding/hex" |
| 5 | "net/url" |
| 6 | "reflect" |
| 7 | "regexp" |
| 8 | "strings" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 11 | ) |
| 12 | |
| 13 | // InstallManifest is transported over HTTPS from the configured instance. The |
| 14 | // runner digest lives outside the embedded policy manifest to avoid a hash cycle. |
| 15 | type InstallManifest struct { |
| 16 | Gitleaks string `json:"gitleaks,omitempty"` |
| 17 | Version string `json:"version"` |
| 18 | Target string `json:"target"` |
| 19 | RunnerSHA256 string `json:"runner_sha256"` |
| 20 | NamespaceArgs []string `json:"namespace_args"` |
| 21 | Release Manifest `json:"release"` |
| 22 | // Targets are every platform's runner in this release with its |
| 23 | // digests (Mac runners spec §3.1): linux/amd64 (the top-level fields |
| 24 | // above, kept for older bootstraps) and darwin/arm64, a Mac worker |
| 25 | // served under DarwinDir. |
| 26 | Targets map[string]InstallTarget `json:"targets,omitempty"` |
| 27 | } |
| 28 | |
| 29 | // InstallTarget is one platform's runner and tool proxy digests, and on |
| 30 | // Linux the renderer's: the SHA-256 of renderer.tar.gz's MANIFEST, the |
| 31 | // pin linked into that runner (rendererSHA256), and the archive's own |
| 32 | // SHA-256 and size (rendererArchiveSHA256, rendererArchiveSize). The |
| 33 | // archive is published on the forge, not served by the instance; these |
| 34 | // say which one, and the runner refuses a manifest that disagrees with |
| 35 | // what is linked into it. |
| 36 | type InstallTarget struct { |
| 37 | GitleaksSHA256 string `json:"gitleaks_sha256,omitempty"` |
| 38 | RendererSHA256 string `json:"renderer_sha256,omitempty"` |
| 39 | RendererArchiveSHA256 string `json:"renderer_archive_sha256,omitempty"` |
| 40 | RendererArchiveSize int64 `json:"renderer_archive_size,omitempty"` |
| 41 | RunnerSHA256 string `json:"runner_sha256"` |
| 42 | ToolProxySHA256 string `json:"tool_proxy_sha256,omitempty"` |
| 43 | } |
| 44 | |
| 45 | // The Mac worker's target and where its files are served, under |
| 46 | // /releases/<version>/. |
| 47 | const ( |
| 48 | TargetLinux = "linux/amd64" |
| 49 | TargetDarwin = "darwin/arm64" |
| 50 | DarwinDir = "darwin-arm64" |
| 51 | ) |
| 52 | |
| 53 | // DarwinTarget is the Mac runner of a release manifest, checked: the |
| 54 | // version this runner asked for and two SHA-256 digests. A Mac worker |
| 55 | // runs checks only, so the Linux runner's pinned programs do not apply to it. |
| 56 | func (m InstallManifest) DarwinTarget(version string) (InstallTarget, error) { |
| 57 | t, ok := m.Targets[TargetDarwin] |
| 58 | if !ValidInstallVersion(version) || m.Version != version || !ok || !sha256Hex(t.RunnerSHA256) { |
| 59 | return InstallTarget{}, errManifest |
| 60 | } |
| 61 | return t, nil |
| 62 | } |
| 63 | |
| 64 | func sha256Hex(s string) bool { |
| 65 | b, err := hex.DecodeString(s) |
| 66 | return err == nil && len(b) == 32 && strings.ToLower(s) == s |
| 67 | } |
| 68 | |
| 69 | var installVersion = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._-]{0,79}$`) |
| 70 | |
| 71 | func ValidInstallVersion(v string) bool { |
| 72 | return installVersion.MatchString(v) && v != "dev" && v != "latest" |
| 73 | } |
| 74 | func InstallOrigin(origin string) bool { |
| 75 | u, e := url.Parse(origin) |
| 76 | 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'\"`$\\#") |
| 77 | } |
| 78 | |
| 79 | // Installable reports whether this build can install or serve a runner: |
| 80 | // the Linux target, the pinned bubblewrap, the launcher and policy this |
| 81 | // code builds, and a digest for each of the five executables, the tool |
| 82 | // proxy's among them. |
| 83 | func (m Manifest) Installable() bool { |
| 84 | 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() == "" { |
| 85 | return false |
| 86 | } |
| 87 | seen := map[string]bool{} |
| 88 | for _, b := range m.Binaries { |
| 89 | if seen[b.Name] || !sha256Hex(b.SHA256) { |
| 90 | return false |
| 91 | } |
| 92 | seen[b.Name] = true |
| 93 | } |
| 94 | for _, name := range []string{"claude", "codex", "codex-code-mode-host", "bwrap", "equipo-tool-proxy"} { |
| 95 | if !seen[name] { |
| 96 | return false |
| 97 | } |
| 98 | } |
| 99 | return true |
| 100 | } |
| 101 | func (m InstallManifest) Validate(version string) error { |
| 102 | // The tool proxy the install manifest lists is the one the release |
| 103 | // recorded: one build, one digest, in both places. |
| 104 | if m.Gitleaks != "" && m.Gitleaks != GitleaksVersion { |
| 105 | return errManifest |
| 106 | } |
| 107 | linux := m.Targets[TargetLinux] |
| 108 | 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()) { |
| 109 | return errManifest |
| 110 | } |
| 111 | // A renderer pin comes with its archive's digest and size, and an |
| 112 | // archive with a pin. |
| 113 | archive := linux.RendererArchiveSHA256 != "" || linux.RendererArchiveSize != 0 |
| 114 | if archive != (linux.RendererSHA256 != "") || archive && (!sha256Hex(linux.RendererSHA256) || !sha256Hex(linux.RendererArchiveSHA256) || linux.RendererArchiveSize <= 0 || linux.RendererArchiveSize > RendererArchiveMax) { |
| 115 | return errManifest |
| 116 | } |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | // InstallCommand uses positional arguments and shell quoting, never an origin |
| 121 | // or path interpolated as shell source. The bootstrap verifies before executing. |
| 122 | func InstallCommand(origin, version string, instance ...string) (string, error) { |
| 123 | if !InstallOrigin(origin) || !ValidInstallVersion(version) { |
| 124 | return "", errManifest |
| 125 | } |
| 126 | extra := "" |
| 127 | if len(instance) > 0 { |
| 128 | if !installVersion.MatchString(instance[0]) { |
| 129 | return "", errManifest |
| 130 | } |
| 131 | extra = " '" + instance[0] + "'" |
| 132 | } |
| 133 | return "curl --fail --silent --show-error --proto '=https' --tlsv1.2 '" + origin + "/releases/" + version + "/install-v1.sh' | sh -s -- '" + origin + "' '" + version + "'" + extra, nil |
| 134 | } |
| 135 | |
| 136 | // ToolProxySHA256 is the recorded digest of this release's tool proxy, or |
| 137 | // "" when the build recorded none or the entry is malformed. |
| 138 | func (m Manifest) ToolProxySHA256() string { |
| 139 | hash := "" |
| 140 | for _, b := range m.Binaries { |
| 141 | if b.Name == "equipo-tool-proxy" { |
| 142 | if hash != "" || b.Version != m.LauncherVersion || !sha256Hex(b.SHA256) { |
| 143 | return "" |
| 144 | } |
| 145 | hash = b.SHA256 |
| 146 | } |
| 147 | } |
| 148 | return hash |
| 149 | } |
| 150 | |