[Web] Accept charset in content-type for json messages

Trac: https://dev.deluge-torrent.org/ticket/3521
Closes: https://github.com/deluge-torrent/deluge/pull/385
This commit is contained in:
ibizaman 2022-05-28 00:02:47 -07:00 committed by Calum Lind
parent d7c520c85e
commit b0dba97fec
No known key found for this signature in database
GPG Key ID: 90597A687B836BA3
2 changed files with 20 additions and 1 deletions

View File

@ -98,6 +98,24 @@ class TestJSON:
with pytest.raises(JSONException):
json._on_json_request(request)
def test_on_json_request_valid_content_type(self):
"""Ensure content-type application/json is accepted"""
json = JSON()
request = MagicMock()
request.getHeader.return_value = b'application/json'
json_data = {'method': 'some.method', 'id': 0, 'params': []}
request.json = json_lib.dumps(json_data).encode()
json._on_json_request(request)
def test_on_json_request_valid_content_type_with_charset(self):
"""Ensure content-type parameters such as charset are ignored"""
json = JSON()
request = MagicMock()
request.getHeader.return_value = b'application/json;charset=utf-8'
json_data = {'method': 'some.method', 'id': 0, 'params': []}
request.json = json_lib.dumps(json_data).encode()
json._on_json_request(request)
@pytest.mark.usefixtures('daemon', 'client', 'component')
class TestJSONCustomUserTestCase:

View File

@ -6,6 +6,7 @@
# See LICENSE for more details.
#
import cgi
import json
import logging
import os
@ -190,7 +191,7 @@ class JSON(resource.Resource, component.Component):
Handler to take the json data as a string and pass it on to the
_handle_request method for further processing.
"""
content_type = request.getHeader(b'content-type').decode()
content_type, _ = cgi.parse_header(request.getHeader(b'content-type').decode())
if content_type != 'application/json':
message = 'Invalid JSON request content-type: %s' % content_type
raise JSONException(message)