Skip to content

Commit 63f8779

Browse files
committed
fix(security): prevent command injection in FileResponse#read_body
Also add general test coverage for FileResponse#read_body Related to GHSA-qrqm-fpv6-6r8g
1 parent b48b12f commit 63f8779

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

lib/mechanize/file_response.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def read_body
1515
if directory?
1616
yield dir_body
1717
else
18-
open @file_path, 'rb' do |io|
18+
::File.open(@file_path, 'rb') do |io|
1919
yield io.read
2020
end
2121
end

test/test_mechanize_file_response.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require 'mechanize/test_case'
22

33
class TestMechanizeFileResponse < Mechanize::TestCase
4-
54
def test_content_type
65
Tempfile.open %w[pi .nothtml] do |tempfile|
76
res = Mechanize::FileResponse.new tempfile.path
@@ -19,5 +18,24 @@ def test_content_type
1918
end
2019
end
2120

22-
end
21+
def test_read_body
22+
Tempfile.open %w[pi .html] do |tempfile|
23+
tempfile.write("asdfasdfasdf")
24+
tempfile.close
2325

26+
res = Mechanize::FileResponse.new(tempfile.path)
27+
res.read_body do |input|
28+
assert_equal("asdfasdfasdf", input)
29+
end
30+
end
31+
end
32+
33+
def test_read_body_does_not_allow_command_injection
34+
in_tmpdir do
35+
FileUtils.touch('| ruby -rfileutils -e \'FileUtils.touch("vul.txt")\'')
36+
res = Mechanize::FileResponse.new('| ruby -rfileutils -e \'FileUtils.touch("vul.txt")\'')
37+
res.read_body { |_| }
38+
refute_operator(File, :exist?, "vul.txt")
39+
end
40+
end
41+
end

0 commit comments

Comments
 (0)