Skip to content

Commit edb0869

Browse files
fix: malformed charset param (#2263) (#2277)
If a content type header is provided with a malformed part, there's no nice way of interpreting it since instead of being a key/value pair it's just a key. Instead, we mimic the behaviour of query parameters and opt to return an empty string in lieu of missing values—this way we can still identify their presence, but won't blow up. The behaviour isn't identical, but we're also not worried about backwards compatibility here (given the alternative was exploding) Co-authored-by: Ally <28497049+AllyMarthaJ@users.noreply.github.com>
1 parent 09de82f commit edb0869

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

lib/rack/media_type.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ def type(content_type)
2727
# provided. e.g., when the CONTENT_TYPE is "text/plain;charset=utf-8",
2828
# this method responds with the following Hash:
2929
# { 'charset' => 'utf-8' }
30+
#
31+
# This will pass back parameters with empty strings in the hash if they
32+
# lack a value (e.g., "text/plain;charset=" will return { 'charset' => '' },
33+
# and "text/plain;charset" will return { 'charset' => '' }, similarly to
34+
# the query params parser (barring the latter case, which returns nil instead)).
3035
def params(content_type)
3136
return {} if content_type.nil?
3237

@@ -40,9 +45,9 @@ def params(content_type)
4045

4146
private
4247

43-
def strip_doublequotes(str)
44-
(str.start_with?('"') && str.end_with?('"')) ? str[1..-2] : str
45-
end
48+
def strip_doublequotes(str)
49+
(str && str.start_with?('"') && str.end_with?('"')) ? str[1..-2] : str || ''
50+
end
4651
end
4752
end
4853
end

test/spec_media_type.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,28 @@
4444
Rack::MediaType.params(@content_type)['charset'].must_equal 'utf-8'
4545
end
4646
end
47+
48+
describe 'when content_type contains media_type and incomplete params' do
49+
before { @content_type = 'application/text;CHARSET' }
50+
51+
it '#type is application/text' do
52+
Rack::MediaType.type(@content_type).must_equal 'application/text'
53+
end
54+
55+
it '#params has key "charset" with value ""' do
56+
Rack::MediaType.params(@content_type)['charset'].must_equal ''
57+
end
58+
end
59+
60+
describe 'when content_type contains media_type and empty params' do
61+
before { @content_type = 'application/text;CHARSET=' }
62+
63+
it '#type is application/text' do
64+
Rack::MediaType.type(@content_type).must_equal 'application/text'
65+
end
66+
67+
it '#params has key "charset" with value of empty string' do
68+
Rack::MediaType.params(@content_type)['charset'].must_equal ''
69+
end
70+
end
4771
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy