Skip to content

Commit fa3caad

Browse files
author
Jonathan Turpie
committed
Use Tempfile to block sniping temp files.
1 parent f38d8b9 commit fa3caad

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

bin/install

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ begin
4747
require 'open-uri'
4848
require 'uri'
4949
require 'getoptlong'
50+
require 'tempfile'
5051

5152
def usage
5253
print <<EOF
@@ -221,10 +222,8 @@ EOF
221222

222223
# stream package file to disk
223224
begin
224-
File.open(package_file, 'w+b') do |file|
225-
uri.open(:ssl_verify_mode => OpenSSL::SSL::VERIFY_PEER, :redirect => true, :read_timeout => 120) do |s3|
226-
file.write(s3.read)
227-
end
225+
uri.open(:ssl_verify_mode => OpenSSL::SSL::VERIFY_PEER, :redirect => true, :read_timeout => 120) do |s3|
226+
package_file.write(s3.read)
228227
end
229228
rescue OpenURI::HTTPError => e
230229
@log.error("Could not find package to download at '#{uri.to_s}'")
@@ -252,20 +251,24 @@ EOF
252251
version_data = get_version_file_from_s3(region, bucket, version_file_key)
253252

254253
package_key = version_data[type]
255-
package_base_name = package_key.split('/')[-1] # base name for the key in S3
256-
package_file = "/tmp/#{package_base_name}"
254+
package_base_name = File.basename(package_key)
255+
package_extension = File.extname(package_base_name)
256+
package_name = File.basename(package_base_name, package_extension)
257+
package_file = Tempfile.new(["#{package_name}.tmp-", package_extension]) # unique file with 0600 permissions
257258

258259
get_package_from_s3(region, bucket, package_key, package_file)
259-
install_cmd << package_file
260+
package_file.close
261+
262+
install_cmd << package_file.path
260263
@log.info("Executing `#{install_cmd.join(" ")}`...")
261264

262265
if (!run_command(*install_cmd))
263-
@log.error("Error installing #{package_file}.")
264-
FileUtils.rm(package_file)
266+
@log.error("Error installing #{package_file.path}.")
267+
package_file.unlink
265268
exit(1)
266269
end
267270

268-
FileUtils.rm(package_file)
271+
package_file.unlink
269272
end
270273

271274
def do_sanity_check(cmd)

0 commit comments

Comments
 (0)