mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
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:
parent
ea2af239cc
commit
d3c40ffb1f
2 changed files with 22 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
||||||
require 'rack/protection'
|
require 'rack/protection'
|
||||||
require 'rack/utils'
|
require 'rack/utils'
|
||||||
|
require 'tempfile'
|
||||||
|
|
||||||
begin
|
begin
|
||||||
require 'escape_utils'
|
require 'escape_utils'
|
||||||
|
@ -66,6 +67,7 @@ module Rack
|
||||||
when Hash then escape_hash(object)
|
when Hash then escape_hash(object)
|
||||||
when Array then object.map { |o| escape(o) }
|
when Array then object.map { |o| escape(o) }
|
||||||
when String then escape_string(object)
|
when String then escape_string(object)
|
||||||
|
when Tempfile then object
|
||||||
else nil
|
else nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -37,5 +37,25 @@ describe Rack::Protection::EscapedParams do
|
||||||
get '/?95df8d9bf5237ad08df3115ee74dcb10'
|
get '/?95df8d9bf5237ad08df3115ee74dcb10'
|
||||||
expect(body).to eq('hi')
|
expect(body).to eq('hi')
|
||||||
end
|
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<bar>")
|
||||||
|
ensure
|
||||||
|
File.unlink(temp_file.path)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue