Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

replace create_tempfile with a safer alternative #180

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 6 additions & 15 deletions lib/execjs/external_runtime.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "tmpdir"
require "tempfile"
require "execjs/runtime"

module ExecJS
Expand Down Expand Up @@ -31,7 +31,7 @@ def exec(source, options = {})
begin
extract_result(@runtime.exec_runtime(tmpfile.path))
ensure
File.unlink(tmpfile)
tmpfile.unlink
end
end

Expand All @@ -40,18 +40,8 @@ def call(identifier, *args)
end

protected
# See Tempfile.create on Ruby 2.1
def create_tempfile(basename)
tmpfile = nil
Dir::Tmpname.create(basename) do |tmpname|
mode = File::WRONLY | File::CREAT | File::EXCL
tmpfile = File.open(tmpname, mode, 0600)
end
tmpfile
end

def write_to_tempfile(contents)
tmpfile = create_tempfile(['execjs', 'js'])
tmpfile = Tempfile.new(['execjs', 'js'])
tmpfile.write(contents)
tmpfile.close
tmpfile
Expand Down Expand Up @@ -146,13 +136,14 @@ def encode_unicode_codepoints(str)

if ExecJS.windows?
def exec_runtime(filename)
path = Dir::Tmpname.create(['execjs', 'json']) {}
tmpfile = TempFile.new(['execjs', 'json'])
path = tmpfile.path
begin
command = binary.split(" ") << filename
`#{shell_escape(*command)} 2>&1 > #{path}`
output = File.open(path, 'rb', @popen_options) { |f| f.read }
ensure
File.unlink(path) if path
tmpfile.unlink
end

if $?.success?
Expand Down