2024-04-08 16:18:15 -04:00
|
|
|
# typed: true
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "date"
|
|
|
|
require "json"
|
|
|
|
require "utils/popen"
|
|
|
|
require "exceptions"
|
|
|
|
|
|
|
|
module Homebrew
|
|
|
|
module Attestation
|
2024-04-08 16:22:57 -04:00
|
|
|
# @api private
|
2024-04-08 16:18:15 -04:00
|
|
|
HOMEBREW_CORE_REPO = "Homebrew/homebrew-core"
|
2024-04-08 16:22:57 -04:00
|
|
|
# @api private
|
2024-04-08 16:18:15 -04:00
|
|
|
HOMEBREW_CORE_CI_URI = "https://github.com/Homebrew/homebrew-core/.github/workflows/publish-commit-bottles.yml@refs/heads/master"
|
|
|
|
|
2024-04-08 16:22:57 -04:00
|
|
|
# @api private
|
2024-04-08 16:18:15 -04:00
|
|
|
BACKFILL_REPO = "trailofbits/homebrew-brew-verify"
|
2024-04-08 16:22:57 -04:00
|
|
|
# @api private
|
2024-04-08 16:18:15 -04:00
|
|
|
BACKFILL_REPO_CI_URI = "https://github.com/trailofbits/homebrew-brew-verify/.github/workflows/backfill_signatures.yml@refs/heads/main"
|
|
|
|
|
|
|
|
# No backfill attestations after this date are considered valid.
|
|
|
|
# @api private
|
2024-04-08 16:21:31 -04:00
|
|
|
BACKFILL_CUTOFF = DateTime.new(2024, 3, 14).freeze
|
2024-04-08 16:18:15 -04:00
|
|
|
|
|
|
|
# Verifies the given bottle against a cryptographic attestation of build provenance.
|
|
|
|
#
|
|
|
|
# The provenance is verified as originating from `signing_repo`, which is a `String`
|
|
|
|
# that should be formatted as a GitHub `owner/repo`.
|
|
|
|
#
|
|
|
|
# Callers may additionally pass in `signing_workflow`, which will scope the attestation
|
|
|
|
# down to an exact GitHub Actions workflow, in
|
|
|
|
# `https://github/OWNER/REPO/.github/workflows/WORKFLOW.yml@REF` format.
|
|
|
|
#
|
|
|
|
# @return [Hash] the JSON-decoded response.
|
2024-04-08 16:22:57 -04:00
|
|
|
# @raise [InvalidAttestationError] on any verification failures
|
2024-04-08 16:18:15 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def self.check_attestation(bottle, signing_repo, signing_workflow = nil)
|
|
|
|
cmd = [HOMEBREW_GH, "attestation", "verify", bottle.cached_download, "--repo", signing_repo, "--format", "json"]
|
|
|
|
|
2024-04-08 16:21:31 -04:00
|
|
|
cmd += ["--cert-identity", signing_workflow] unless signing_workflow.nil?
|
2024-04-08 16:18:15 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
output = Utils.safe_popen_read(*cmd)
|
|
|
|
rescue ErrorDuringExecution => e
|
|
|
|
raise InvalidAttestationError, "attestation verification failed: #{e}"
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
data = JSON.parse(output)
|
2024-04-08 16:21:31 -04:00
|
|
|
rescue JSON::ParserError
|
2024-04-08 16:18:15 -04:00
|
|
|
raise InvalidAttestationError, "attestation verification returned malformed JSON"
|
|
|
|
end
|
|
|
|
|
|
|
|
raise InvalidAttestationError, "attestation output is empty" if data.empty?
|
|
|
|
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
|
|
|
# Verifies the given bottle against a cryptographic attestation of build provenance
|
|
|
|
# from homebrew-core's CI, falling back on a "backfill" attestation for older bottles.
|
|
|
|
#
|
|
|
|
# This is a specialization of `check_attestation` for homebrew-core.
|
2024-04-08 16:22:57 -04:00
|
|
|
#
|
|
|
|
# @return [Hash] the JSON-decoded response
|
|
|
|
# @raise [InvalidAttestationError] on any verification failures
|
|
|
|
#
|
|
|
|
# @api private
|
2024-04-08 16:18:15 -04:00
|
|
|
def self.check_core_attestation(bottle)
|
|
|
|
begin
|
|
|
|
attestation = check_attestation bottle, HOMEBREW_CORE_REPO
|
|
|
|
return attestation
|
|
|
|
rescue InvalidAttestationError
|
|
|
|
odebug "falling back on backfilled attestation"
|
|
|
|
backfill_attestation = check_attestation bottle, BACKFILL_REPO, BACKFILL_REPO_CI_URI
|
|
|
|
timestamp = backfill_attestation.dig(0, "verificationResult", "verifiedTimestamps",
|
|
|
|
0, "timestamp")
|
|
|
|
|
|
|
|
raise InvalidAttestationError, "backfill attestation is missing verified timestamp" if timestamp.nil?
|
|
|
|
|
|
|
|
if DateTime.parse(timestamp) > BACKFILL_CUTOFF
|
|
|
|
raise InvalidAttestationError, "backfill attestation post-dates cutoff"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
backfill_attestation
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|