From 3232f61f7ebac1fde98a402255f28df19a07d77b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?poljar=20=28Damir=20Jeli=C4=87=29?= Date: Wed, 14 Feb 2018 12:40:41 +0100 Subject: [PATCH] Add a ID sanitization function. Json allows certain control characters inside of strings. Filter those out for identifiers. --- matrix/events.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/matrix/events.py b/matrix/events.py index cbbb5f7..5d54914 100644 --- a/matrix/events.py +++ b/matrix/events.py @@ -22,6 +22,19 @@ import time from matrix.globals import W, OPTIONS from matrix.utils import color_for_tags +def sanitize_id(string): + # type: (unicode) -> unicode + remap = { + ord('\b'): None, + ord('\f'): None, + ord('\n'): None, + ord('\r'): None, + ord('\t'): None, + ord('\0'): None + } + + return string.translate(remap) + class MatrixEvent(): def __init__(self, server): @@ -84,8 +97,8 @@ class MatrixLoginEvent(MatrixEvent): try: return cls( server, - parsed_dict["user_id"], - parsed_dict["access_token"] + sanitize_id(parsed_dict["user_id"]), + sanitize_id(parsed_dict["access_token"]) ) except KeyError: return MatrixErrorEvent.from_dict( @@ -133,7 +146,7 @@ class MatrixSendEvent(MatrixEvent): return cls( server, room_id, - parsed_dict["event_id"], + sanitize_id(parsed_dict["event_id"]), message ) except KeyError: @@ -155,7 +168,12 @@ class MatrixTopicEvent(MatrixEvent): @classmethod def from_dict(cls, server, room_id, topic, parsed_dict): try: - return cls(server, room_id, parsed_dict["event_id"], topic) + return cls( + server, + room_id, + sanitize_id(parsed_dict["event_id"]), + topic + ) except KeyError: return MatrixErrorEvent.from_dict( server,