// Package pushgate holds the pure half of scan-before-push (ADR-0093 §5, // WP-06): which digest an OCI archive carries, whether the registry kept it, // and what the platform's pre-push verdict says. // // Standard library only, for the reason evidence/ is: the generated `dagger` // package panics at init without a Dagger session, so nothing in package main // runs under a plain `go test`. // // The order every rendered engine runs (ADR-0093 §5): // // build to an OCI archive -> scan the archive -> ask the verdict for the // archive's manifest digest -> push only if the answer is not `refuse` -> // the registry's digest must equal the archive's (mismatch fails). // // The push step runs in the build job, which has executed the change's code, // so it is hygiene, not a control. The control is G3: the deploy gate judges // the pushed DIGEST (ADR-0093 §5.4). package pushgate import ( "encoding/json" "errors" "fmt" "regexp" "strings" ) var digestRE = regexp.MustCompile(`^sha256:[0-9a-f]{64}$`) // versionRE is the tag grammar of the platform's deploy endpoint and of the // security verdict (`security_verdict._VERSION_RE`). var versionRE = regexp.MustCompile(`^[A-Za-z0-9_][A-Za-z0-9._-]{0,127}$`) // manifestMediaTypes are the single-image manifests an archive may name. An // image INDEX is refused: the scan describes one platform's image, and a // pushed index would carry a digest no scan describes. var manifestMediaTypes = map[string]bool{ "application/vnd.oci.image.manifest.v1+json": true, "application/vnd.docker.distribution.manifest.v2+json": true, } // RegistryDigestPrefix starts the line the push script prints with the digest // the REGISTRY reports for the pushed reference. const RegistryDigestPrefix = "REGISTRY_DIGEST " // IsDigest reports whether s is `sha256:<64 lower-case hex>`. func IsDigest(s string) bool { return digestRE.MatchString(s) } // ManifestDigestFromIndex returns the manifest digest an OCI image layout's // index.json names. Exactly one manifest, of a single-image media type. func ManifestDigestFromIndex(indexJSON []byte) (string, error) { var index struct { Manifests []struct { MediaType string `json:"mediaType"` Digest string `json:"digest"` } `json:"manifests"` } if err := json.Unmarshal(indexJSON, &index); err != nil { return "", fmt.Errorf("the archive's index.json is not JSON: %w", err) } if len(index.Manifests) != 1 { return "", fmt.Errorf( "the archive names %d manifests; one platform per digest is supported", len(index.Manifests)) } m := index.Manifests[0] if !manifestMediaTypes[m.MediaType] { return "", fmt.Errorf("the archive's manifest has media type %q, not a single image", m.MediaType) } if !IsDigest(m.Digest) { return "", fmt.Errorf("the archive's manifest digest %q is not sha256", m.Digest) } return m.Digest, nil } // RegistryDigestFrom returns the digest on the push script's // RegistryDigestPrefix line, or "" when there is none (or it is not sha256). func RegistryDigestFrom(out string) string { for _, line := range strings.Split(out, "\n") { line = strings.TrimSpace(line) if rest, ok := strings.CutPrefix(line, RegistryDigestPrefix); ok { rest = strings.TrimSpace(rest) if IsDigest(rest) { return rest } } } return "" } // SameDigest fails unless the registry kept the archive's manifest byte for // byte. On a mismatch the caller must FAIL and record nothing: the scan and // the verdict described the archive's digest, never the registry's // (ADR-0093 §5.2, "the push step fails rather than rebinding evidence"). func SameDigest(archive, registry string) error { if !IsDigest(archive) { return fmt.Errorf("archive digest %q is not sha256", archive) } if !IsDigest(registry) { return errors.New("the registry reported no sha256 digest for the pushed reference") } if archive != registry { return fmt.Errorf( "registry digest %s differs from the scanned archive's %s: the registry rewrote the image; "+ "nothing is recorded for it", registry, archive) } return nil } // Request is the body of POST /api/v1/secure-delivery/push-verdict. type Request struct { ServiceID string `json:"service_id"` ImageDigest string `json:"image_digest"` Version string `json:"version"` } // RequestBody validates and encodes the verdict request. func RequestBody(serviceID, digest, version string) ([]byte, error) { if strings.TrimSpace(serviceID) == "" { return nil, errors.New("push-verdict needs --service-id") } if !IsDigest(digest) { return nil, fmt.Errorf("push-verdict: %q is not a sha256 digest", digest) } if !versionRE.MatchString(version) { return nil, fmt.Errorf("push-verdict: version %q is not an image tag", version) } return json.Marshal(Request{ServiceID: serviceID, ImageDigest: digest, Version: version}) } // Answer is the platform's reply (app/security/schemas/push_verdict.py // PushVerdictRead). apps/api/tests/test_scan_before_push.py holds the JSON // names equal on both sides. type Answer struct { Decision string `json:"decision"` Result string `json:"result"` Mode string `json:"mode"` Reasons []string `json:"reasons"` WouldBlock []string `json:"would_block"` } // Outcome is what the push step does with an answer. type Outcome struct { Push bool Message string } // Judge turns the platform's HTTP answer into the push decision. // // An explicit, well-formed `refuse` stops the push, and so does an explicit // AUTHORIZATION refusal (401/403): the caller configured a token, and the // platform said this token may not ask about this service or revision // (expired, revoked, `scan-token-wrong-revision`, `scan-token-unbound`). // Pushing then would let a refused binding publish silently, with no server // trace (WP-06 review, 2026-11-04). // // Anything else -- the platform unreachable (a Jenkins on a customer network // often cannot call back), a 5xx or other non-2xx, an unreadable body, an // unknown decision -- pushes and says so loudly: this step is hygiene, and the // deploy gate (G3) still judges the pushed digest. Failing closed on transport // errors would stop every build whose CI cannot reach the platform, without // making anything safer. func Judge(status int, body []byte) Outcome { if status == 401 || status == 403 { return Outcome{false, fmt.Sprintf( "verdict refused the request (HTTP %d: the token may not ask for this service/revision): "+ "image NOT pushed", status)} } if status < 200 || status >= 300 { return Outcome{true, fmt.Sprintf( "verdict NOT answered (HTTP %d): pushed; the deploy gate judges this digest", status)} } var a Answer if err := json.Unmarshal(body, &a); err != nil { return Outcome{true, "verdict NOT readable: pushed; the deploy gate judges this digest"} } switch a.Decision { case "refuse": families := "unknown" if len(a.Reasons) > 0 { names := make([]string, 0, len(a.Reasons)) for _, r := range a.Reasons { name, _, _ := strings.Cut(r, ":") names = append(names, strings.TrimSpace(name)) } families = strings.Join(names, ", ") } return Outcome{false, fmt.Sprintf( "verdict: block (%s): image NOT pushed. %s", families, strings.Join(a.Reasons, "; "))} case "push": msg := fmt.Sprintf("verdict: %s (mode %s): pushing", a.Result, a.Mode) if len(a.WouldBlock) > 0 { msg += fmt.Sprintf("; would block in enforce: %s (recorded as secure_delivery.would_block)", strings.Join(a.WouldBlock, ", ")) } return Outcome{true, msg} default: return Outcome{true, fmt.Sprintf( "verdict answered %q, which is not push/refuse: pushed; the deploy gate judges this digest", a.Decision)} } }