Ensuring that generated Content-Type headers properly escape params.

This commit is contained in:
Pieter van de Bruggen 2013-01-28 17:30:03 -08:00
parent 43de6d2d8a
commit 7721545ff2
2 changed files with 27 additions and 1 deletions

View File

@ -275,7 +275,10 @@ module Sinatra
params.delete :charset if mime_type.include? 'charset'
unless params.empty?
mime_type << (mime_type.include?(';') ? ', ' : ';')
mime_type << params.map { |kv| kv.join('=') }.join(', ')
mime_type << params.map do |key, val|
val = val.inspect if val =~ /[";,]/
"#{key}=#{val}"
end.join(', ')
end
response['Content-Type'] = mime_type
end

View File

@ -586,6 +586,29 @@ class HelpersTest < Test::Unit::TestCase
get '/'
assert_equal 'text/plain;charset=utf-16', response['Content-Type']
end
it 'properly encodes parameters with delimiter characters' do
mock_app do
before '/comma' do
content_type 'image/png', :comment => 'Hello, world!'
end
before '/semicolon' do
content_type 'image/png', :comment => 'semi;colon'
end
before '/quote' do
content_type 'image/png', :comment => '"Whatever."'
end
get('*') { 'ok' }
end
get '/comma'
assert_equal 'image/png;comment="Hello, world!"', response['Content-Type']
get '/semicolon'
assert_equal 'image/png;comment="semi;colon"', response['Content-Type']
get '/quote'
assert_equal 'image/png;comment="\"Whatever.\""', response['Content-Type']
end
end
describe 'attachment' do