Skip to content

Commit 8c868ba

Browse files
authored
Merge pull request #18883 from Homebrew/ww/attestations-verify-multiple-subjects
2 parents eb23509 + 057a6c5 commit 8c868ba

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

Library/Homebrew/attestation.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,12 @@ def self.check_attestation(bottle, signing_repo, signing_workflow = nil, subject
164164

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

170175
attestation = if bottle.tag.to_sym == :all
@@ -175,12 +180,15 @@ def self.check_attestation(bottle, signing_repo, signing_workflow = nil, subject
175180
# This is sound insofar as the signature has already been verified. However,
176181
# longer term, we should also directly attest to `:all`-tagged bottles.
177182
attestations.find do |a|
178-
actual_subject = a.dig("verificationResult", "statement", "subject", 0, "name")
179-
actual_subject.start_with? "#{bottle.filename.name}--#{bottle.filename.version}"
183+
candidate_subjects = a.dig("verificationResult", "statement", "subject")
184+
candidate_subjects.any? do |candidate|
185+
candidate["name"].start_with? "#{bottle.filename.name}--#{bottle.filename.version}"
186+
end
180187
end
181188
else
182189
attestations.find do |a|
183-
a.dig("verificationResult", "statement", "subject", 0, "name") == subject
190+
candidate_subjects = a.dig("verificationResult", "statement", "subject")
191+
candidate_subjects.any? { |candidate| candidate["name"] == subject }
184192
end
185193
end
186194

Library/Homebrew/test/attestation_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@
3434
} },
3535
]))
3636
end
37+
let(:fake_result_json_resp_multi_subject) do
38+
instance_double(SystemCommand::Result,
39+
stdout: JSON.dump([
40+
{ verificationResult: {
41+
verifiedTimestamps: [{ timestamp: "2024-03-13T00:00:00Z" }],
42+
statement: { subject: [{ name: "nonsense" }, { name: fake_bottle_filename.to_s }] },
43+
} },
44+
]))
45+
end
3746
let(:fake_result_json_resp_backfill) do
3847
digest = Digest::SHA256.hexdigest(fake_bottle_url)
3948
instance_double(SystemCommand::Result,
@@ -234,6 +243,17 @@
234243
described_class.check_core_attestation fake_bottle
235244
end
236245

246+
it "calls gh with args for homebrew-core and handles a multi-subject attestation" do
247+
expect(described_class).to receive(:system_command!)
248+
.with(fake_gh, args: ["attestation", "verify", cached_download, "--repo",
249+
described_class::HOMEBREW_CORE_REPO, "--format", "json"],
250+
env: { "GH_TOKEN" => fake_gh_creds, "GH_HOST" => "github.com" }, secrets: [fake_gh_creds],
251+
print_stderr: false, chdir: HOMEBREW_TEMP)
252+
.and_return(fake_result_json_resp_multi_subject)
253+
254+
described_class.check_core_attestation fake_bottle
255+
end
256+
237257
it "calls gh with args for backfill when homebrew-core attestation is missing" do
238258
expect(described_class).to receive(:system_command!)
239259
.with(fake_gh, args: ["attestation", "verify", cached_download, "--repo",

0 commit comments

Comments
 (0)