Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attestation: handle multiple subjects #18883

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions Library/Homebrew/attestation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,12 @@ def self.check_attestation(bottle, signing_repo, signing_workflow = nil, subject

# `gh attestation verify` returns a JSON array of one or more results,
# for all attestations that match the input's digest. We want to additionally
# filter these down to just the attestation whose subject matches the bottle's name.
# filter these down to just the attestation whose subject(s) contain the bottle's name.
# As of 2024-12-04 GitHub's Artifact Attestation feature can put multiple subjects
woodruffw marked this conversation as resolved.
Show resolved Hide resolved
# in a single attestation, so we check every subject in each attestation
# and select the first attestation with a matching subject.
# In particular, this happens with v2.0.0 and later of the
# `actions/attest-build-provenance` action.
subject = bottle.filename.to_s if subject.blank?

attestation = if bottle.tag.to_sym == :all
Expand All @@ -175,12 +180,15 @@ def self.check_attestation(bottle, signing_repo, signing_workflow = nil, subject
# This is sound insofar as the signature has already been verified. However,
# longer term, we should also directly attest to `:all`-tagged bottles.
attestations.find do |a|
actual_subject = a.dig("verificationResult", "statement", "subject", 0, "name")
actual_subject.start_with? "#{bottle.filename.name}--#{bottle.filename.version}"
candidate_subjects = a.dig("verificationResult", "statement", "subject")
candidate_subjects.any? do |candidate|
candidate["name"].start_with? "#{bottle.filename.name}--#{bottle.filename.version}"
end
end
else
attestations.find do |a|
a.dig("verificationResult", "statement", "subject", 0, "name") == subject
candidate_subjects = a.dig("verificationResult", "statement", "subject")
candidate_subjects.any? { |candidate| candidate["name"] == subject }
end
end

Expand Down
20 changes: 20 additions & 0 deletions Library/Homebrew/test/attestation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
} },
]))
end
let(:fake_result_json_resp_multi_subject) do
instance_double(SystemCommand::Result,
stdout: JSON.dump([
{ verificationResult: {
verifiedTimestamps: [{ timestamp: "2024-03-13T00:00:00Z" }],
statement: { subject: [{ name: "nonsense" }, { name: fake_bottle_filename.to_s }] },
} },
]))
end
let(:fake_result_json_resp_backfill) do
digest = Digest::SHA256.hexdigest(fake_bottle_url)
instance_double(SystemCommand::Result,
Expand Down Expand Up @@ -234,6 +243,17 @@
described_class.check_core_attestation fake_bottle
end

it "calls gh with args for homebrew-core and handles a multi-subject attestation" do
expect(described_class).to receive(:system_command!)
.with(fake_gh, args: ["attestation", "verify", cached_download, "--repo",
described_class::HOMEBREW_CORE_REPO, "--format", "json"],
env: { "GH_TOKEN" => fake_gh_creds, "GH_HOST" => "github.com" }, secrets: [fake_gh_creds],
print_stderr: false, chdir: HOMEBREW_TEMP)
.and_return(fake_result_json_resp_multi_subject)

described_class.check_core_attestation fake_bottle
end

it "calls gh with args for backfill when homebrew-core attestation is missing" do
expect(described_class).to receive(:system_command!)
.with(fake_gh, args: ["attestation", "verify", cached_download, "--repo",
Expand Down
Loading