From 95645f55510e445d00186ef41761a78fcb79764e Mon Sep 17 00:00:00 2001 From: nagachika Date: Thu, 14 Dec 2017 13:51:34 +0000 Subject: [PATCH] merge revision(s) 61242: [Backport #14185] Fix a command injection vulnerability in Net::FTP. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@61245 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/net/ftp.rb | 10 +- test/net/ftp/test_ftp.rb | 234 +++++++++++++++++++++++++++++++++++++++ version.h | 2 +- 3 files changed, 240 insertions(+), 6 deletions(-) diff --git a/lib/net/ftp.rb b/lib/net/ftp.rb index 601d2cb4c374c7..bd74323987f295 100644 --- a/lib/net/ftp.rb +++ b/lib/net/ftp.rb @@ -754,10 +754,10 @@ def getbinaryfile(remotefile, localfile = File.basename(remotefile), if localfile if @resume rest_offset = File.size?(localfile) - f = open(localfile, "a") + f = File.open(localfile, "a") else rest_offset = nil - f = open(localfile, "w") + f = File.open(localfile, "w") end elsif !block_given? result = String.new @@ -787,7 +787,7 @@ def gettextfile(remotefile, localfile = File.basename(remotefile), f = nil result = nil if localfile - f = open(localfile, "w") + f = File.open(localfile, "w") elsif !block_given? result = String.new end @@ -833,7 +833,7 @@ def putbinaryfile(localfile, remotefile = File.basename(localfile), else rest_offset = nil end - f = open(localfile) + f = File.open(localfile) begin f.binmode if rest_offset @@ -852,7 +852,7 @@ def putbinaryfile(localfile, remotefile = File.basename(localfile), # passing in the transmitted data one line at a time. # def puttextfile(localfile, remotefile = File.basename(localfile), &block) # :yield: line - f = open(localfile) + f = File.open(localfile) begin storlines("STOR #{remotefile}", f, &block) ensure diff --git a/test/net/ftp/test_ftp.rb b/test/net/ftp/test_ftp.rb index 6976f11b8f5db0..54b7626598ff2f 100644 --- a/test/net/ftp/test_ftp.rb +++ b/test/net/ftp/test_ftp.rb @@ -5,6 +5,7 @@ require "ostruct" require "stringio" require "tempfile" +require "tmpdir" class FTPTest < Test::Unit::TestCase SERVER_NAME = "localhost" @@ -2132,6 +2133,227 @@ def test_abort_tls end end + def test_getbinaryfile_command_injection + skip "| is not allowed in filename on Windows" if windows? + [false, true].each do |resume| + commands = [] + binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3 + server = create_ftp_server { |sock| + sock.print("220 (test_ftp).\r\n") + commands.push(sock.gets) + sock.print("331 Please specify the password.\r\n") + commands.push(sock.gets) + sock.print("230 Login successful.\r\n") + commands.push(sock.gets) + sock.print("200 Switching to Binary mode.\r\n") + line = sock.gets + commands.push(line) + host, port = process_port_or_eprt(sock, line) + commands.push(sock.gets) + sock.print("150 Opening BINARY mode data connection for |echo hello (#{binary_data.size} bytes)\r\n") + conn = TCPSocket.new(host, port) + binary_data.scan(/.{1,1024}/nm) do |s| + conn.print(s) + end + conn.shutdown(Socket::SHUT_WR) + conn.read + conn.close + sock.print("226 Transfer complete.\r\n") + } + begin + chdir_to_tmpdir do + begin + ftp = Net::FTP.new + ftp.resume = resume + ftp.read_timeout = 0.2 + ftp.connect(SERVER_ADDR, server.port) + ftp.login + assert_match(/\AUSER /, commands.shift) + assert_match(/\APASS /, commands.shift) + assert_equal("TYPE I\r\n", commands.shift) + ftp.getbinaryfile("|echo hello") + assert_equal(binary_data, File.binread("./|echo hello")) + assert_match(/\A(PORT|EPRT) /, commands.shift) + assert_equal("RETR |echo hello\r\n", commands.shift) + assert_equal(nil, commands.shift) + ensure + ftp.close if ftp + end + end + ensure + server.close + end + end + end + + def test_gettextfile_command_injection + skip "| is not allowed in filename on Windows" if windows? + commands = [] + text_data = <