Fix Tempfile reference being returned as nil

This PR fixes the issue of files being uploaded from users having
the params[:file][:tempfile] being returned as `nil`, rather than
an instance of Tempfile like it should be.

PR fixes #90, and shamelessly stolen from #91.
This commit is contained in:
Albert Engelbrecht 2015-09-30 16:51:50 -05:00
parent ea2af239cc
commit d3c40ffb1f
2 changed files with 22 additions and 0 deletions

View File

@ -1,5 +1,6 @@
require 'rack/protection'
require 'rack/utils'
require 'tempfile'
begin
require 'escape_utils'
@ -66,6 +67,7 @@ module Rack
when Hash then escape_hash(object)
when Array then object.map { |o| escape(o) }
when String then escape_string(object)
when Tempfile then object
else nil
end
end

View File

@ -37,5 +37,25 @@ describe Rack::Protection::EscapedParams do
get '/?95df8d9bf5237ad08df3115ee74dcb10'
expect(body).to eq('hi')
end
it 'leaves TempFiles untouched' do
mock_app do |env|
request = Rack::Request.new(env)
[200, {'Content-Type' => 'text/plain'}, [request.params['file'][:filename] + "\n" + \
request.params['file'][:tempfile].read + "\n" + \
request.params['other']]]
end
temp_file = File.open('_escaped_params_tmp_file', 'w')
begin
temp_file.write('hello world')
temp_file.close
post '/', :file => Rack::Test::UploadedFile.new(temp_file.path), :other => '<bar>'
expect(body).to eq("_escaped_params_tmp_file\nhello world\n&lt;bar&gt;")
ensure
File.unlink(temp_file.path)
end
end
end
end