Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Server response could not be decoded using UTF-8 #1623

Merged
merged 5 commits into from Feb 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -68,6 +68,7 @@ The following wonderful people contributed directly or indirectly to this projec
- `Pieter Schutz <https://github.com/eldinnie>`_
- `Poolitzer <https://github.com/Poolitzer>`_
- `Rahiel Kasim <https://github.com/rahiel>`_
- `Riko Naka <https://github.com/rikonaka>`_
- `Rizlas <https://github.com/rizlas>`_
- `Sahil Sharma <https://github.com/sahilsharma811>`_
- `Sascha <https://github.com/saschalalala>`_
Expand Down
6 changes: 1 addition & 5 deletions telegram/utils/request.py
Expand Up @@ -178,13 +178,9 @@ def _parse(json_data):

"""

decoded_s = json_data.decode('utf-8', 'replace')
try:
decoded_s = json_data.decode('utf-8')
data = json.loads(decoded_s)
except UnicodeDecodeError:
logging.getLogger(__name__).debug(
'Logging raw invalid UTF-8 response:\n%r', json_data)
raise TelegramError('Server response could not be decoded using UTF-8')
except ValueError:
raise TelegramError('Invalid server response')

Expand Down
16 changes: 13 additions & 3 deletions tests/test_request.py
Expand Up @@ -23,12 +23,22 @@
from telegram.utils.request import Request


def test_parse_illegal_callback_data():
def test_replaced_unprintable_char():
"""
Clients can send arbitrary bytes in callback data.
Make sure the correct error is raised in this case.
"""
server_response = b'{"invalid utf-8": "\x80"}'
server_response = b'{"invalid utf-8": "\x80", "result": "KUKU"}'

with pytest.raises(TelegramError, match='Server response could not be decoded using UTF-8'):
assert Request._parse(server_response) == 'KUKU'


def test_parse_illegal_json():
"""
Clients can send arbitrary bytes in callback data.
Make sure the correct error is raised in this case.
"""
server_response = b'{"invalid utf-8": "\x80", result: "KUKU"}'

with pytest.raises(TelegramError, match='Invalid server response'):
Request._parse(server_response)