Skip to content

Commit 9f87b0f

Browse files
authored
Merge pull request #9327 from ruby/copilot/sub-pr-9325
Use Tempfile for auto-attestation bundles to avoid file conflicts and cleanup issues
2 parents 8330423 + ffcce01 commit 9f87b0f

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

lib/rubygems/commands/push_command.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,12 @@ def send_push_request_with_attestation(name, args)
121121
Gem.read_binary(attestation)
122122
end
123123
else
124-
[Gem.read_binary(attest!(name))]
124+
bundle_path = attest!(name)
125+
begin
126+
[Gem.read_binary(bundle_path)]
127+
ensure
128+
File.unlink(bundle_path) if bundle_path && File.exist?(bundle_path)
129+
end
125130
end
126131
bundles = "[" + attestations.join(",") + "]"
127132

@@ -136,8 +141,14 @@ def send_push_request_with_attestation(name, args)
136141

137142
def attest!(name)
138143
require "open3"
144+
require "tempfile"
145+
146+
# Create a temporary file for the bundle
147+
basename = File.basename(name, ".*")
148+
tempfile = Tempfile.new([basename, ".sigstore.json"])
149+
bundle = tempfile.path
150+
tempfile.close(false) # Close but don't unlink - we need the file for sigstore-cli
139151

140-
bundle = "#{name}.sigstore.json"
141152
env = defined?(Bundler.unbundled_env) ? Bundler.unbundled_env : ENV.to_h
142153
out, st = Open3.capture2e(
143154
env,

test/rubygems/test_gem_commands_push_command.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ def test_execute_attestation_auto
123123
@fetcher.data["#{Gem.host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
124124

125125
attestation_path = "#{@path}.sigstore.json"
126-
File.write(attestation_path, "auto-attestation")
126+
attestation_content = "auto-attestation"
127+
File.write(attestation_path, attestation_content)
127128
@cmd.options[:args] = [@path]
128129

129130
@cmd.stub(:attest!, attestation_path) do
@@ -133,7 +134,7 @@ def test_execute_attestation_auto
133134
assert_equal Gem::Net::HTTP::Post, @fetcher.last_request.class
134135
content_length = @fetcher.last_request["Content-Length"].to_i
135136
assert_equal content_length, @fetcher.last_request.body.length
136-
assert_attestation_multipart Gem.read_binary(attestation_path)
137+
assert_attestation_multipart attestation_content
137138
end
138139

139140
def test_execute_attestation_fallback

0 commit comments

Comments
 (0)