From b0dba97fec8dccdaa45f1112f84068cf7415a4ed Mon Sep 17 00:00:00 2001 From: ibizaman Date: Sat, 28 May 2022 00:02:47 -0700 Subject: [PATCH] [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 --- deluge/tests/test_json_api.py | 18 ++++++++++++++++++ deluge/ui/web/json_api.py | 3 ++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/deluge/tests/test_json_api.py b/deluge/tests/test_json_api.py index c6eedd331..41efb0206 100644 --- a/deluge/tests/test_json_api.py +++ b/deluge/tests/test_json_api.py @@ -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: diff --git a/deluge/ui/web/json_api.py b/deluge/ui/web/json_api.py index c487ddf3c..3f256140e 100644 --- a/deluge/ui/web/json_api.py +++ b/deluge/ui/web/json_api.py @@ -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)