Skip to content

Commit 1dcee03

Browse files
Merge pull request #1933 from aliismayilov/ignore-tempfile-path
Consider Tempfile.create.path as safe input
2 parents 21ae593 + b9ec28f commit 1dcee03

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

lib/brakeman/checks/base_check.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,13 @@ def boolean_method? method
151151
method[-1] == "?"
152152
end
153153

154-
TEMP_FILE_PATH = s(:call, s(:call, s(:const, :Tempfile), :new), :path).freeze
154+
TEMP_FILE_PATH = [
155+
s(:call, s(:call, s(:const, :Tempfile), :new), :path).freeze,
156+
s(:call, s(:call, s(:const, :Tempfile), :create), :path).freeze
157+
].freeze
155158

156159
def temp_file_path? exp
157-
exp == TEMP_FILE_PATH
160+
TEMP_FILE_PATH.include? exp
158161
end
159162

160163
#Report a warning

lib/brakeman/processors/alias_processor.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,12 @@ def temp_file_open? exp
436436
exp.method == :open
437437
end
438438

439+
def temp_file_create? exp
440+
call? exp and
441+
exp.target == TEMP_FILE_CLASS and
442+
exp.method == :create
443+
end
444+
439445
def temp_file_new line
440446
s(:call, TEMP_FILE_CLASS, :new).line(line)
441447
end
@@ -465,6 +471,9 @@ def process_iter exp
465471
elsif temp_file_open? call
466472
local = Sexp.new(:lvar, block_args.last)
467473
env.current[local] = temp_file_new(exp.line)
474+
elsif temp_file_create? call
475+
local = Sexp.new(:lvar, block_args.last)
476+
env.current[local] = temp_file_new(exp.line)
468477
else
469478
block_args.each do |e|
470479
#Force block arg(s) to be local

test/apps/rails5.2/lib/shell.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,14 @@ def open3_capture_stdin_data
136136
Open3.capture2e("cat", stdin_data: "User.z = #{u.z}")
137137
Open3.capture3("cat", stdin_data: "User.z = #{u.z}")
138138
end
139+
140+
def tempfile_create
141+
# these should not warn
142+
tempfile = Tempfile.create
143+
`something -out #{tempfile.path}`
144+
145+
Tempfile.create do |tempfile|
146+
system("something -out #{tempfile.path}")
147+
end
148+
end
139149
end

0 commit comments

Comments
 (0)