Skip to content

Commit b8b3c4b

Browse files
committed
Guard valid content type check against bad Content-Type
Avoid `undefined method 'start_with?' for nil:NilClass` in `Rack::MediaType` `strip_doublequotes` when given a bad content type. Given: Clients MUST send all JSON:API data in request documents with the header Content-Type: application/vnd.api+json without any media type parameters. Clients MUST ignore any parameters for the application/vnd.api+json media type received in the Content-Type header of response documents. We can just check if the content type == `application/vnd.api+json`.
1 parent d59b6b1 commit b8b3c4b

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/jsonapi/rails/filter_media_type.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def call(env)
2020

2121
def valid_content_type?(content_type)
2222
Rack::MediaType.type(content_type) != JSONAPI_MEDIA_TYPE ||
23-
Rack::MediaType.params(content_type) == {}
23+
content_type == JSONAPI_MEDIA_TYPE
2424
end
2525

2626
def valid_accept?(accept)

spec/filter_media_type_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
end
2828
end
2929

30+
context 'when receiving a bad Content-Type' do
31+
it 'fails with 415 Unsupported Media Type' do
32+
env = { 'CONTENT_TYPE' => 'application/vnd.api+json, application/vnd.api+json' }
33+
34+
expect(described_class.new(app).call(env)[0]).to eq(415)
35+
end
36+
end
37+
3038
context 'when not receiving JSON API in Accept' do
3139
it 'passes through' do
3240
env = { 'HTTP_ACCEPT' => 'application/json' }

0 commit comments

Comments
 (0)