Check the type while sanitizing a ID.

This commit is contained in:
poljar (Damir Jelić) 2018-02-14 15:09:11 +01:00
parent 1466531d48
commit fc25fe54c9

View file

@ -24,6 +24,10 @@ from matrix.utils import color_for_tags
def sanitize_id(string): def sanitize_id(string):
# type: (unicode) -> unicode # type: (unicode) -> unicode
if not isinstance(string, str):
raise TypeError
remap = { remap = {
ord('\b'): None, ord('\b'): None,
ord('\f'): None, ord('\f'): None,
@ -100,7 +104,7 @@ class MatrixLoginEvent(MatrixEvent):
sanitize_id(parsed_dict["user_id"]), sanitize_id(parsed_dict["user_id"]),
sanitize_id(parsed_dict["access_token"]) sanitize_id(parsed_dict["access_token"])
) )
except KeyError: except (KeyError, TypeError):
return MatrixErrorEvent.from_dict( return MatrixErrorEvent.from_dict(
server, server,
"Error logging in", "Error logging in",
@ -149,7 +153,7 @@ class MatrixSendEvent(MatrixEvent):
sanitize_id(parsed_dict["event_id"]), sanitize_id(parsed_dict["event_id"]),
message message
) )
except KeyError: except (KeyError, TypeError):
return MatrixErrorEvent.from_dict( return MatrixErrorEvent.from_dict(
server, server,
"Error sending message", "Error sending message",
@ -174,7 +178,7 @@ class MatrixTopicEvent(MatrixEvent):
sanitize_id(parsed_dict["event_id"]), sanitize_id(parsed_dict["event_id"]),
topic topic
) )
except KeyError: except (KeyError, TypeError):
return MatrixErrorEvent.from_dict( return MatrixErrorEvent.from_dict(
server, server,
"Error setting topic", "Error setting topic",
@ -199,7 +203,7 @@ class MatrixRedactEvent(MatrixEvent):
sanitize_id(parsed_dict["event_id"]), sanitize_id(parsed_dict["event_id"]),
reason reason
) )
except KeyError: except (KeyError, TypeError):
return MatrixErrorEvent.from_dict( return MatrixErrorEvent.from_dict(
server, server,
"Error redacting message", "Error redacting message",
@ -222,7 +226,7 @@ class MatrixJoinEvent(MatrixEvent):
room_id, room_id,
sanitize_id(parsed_dict["room_id"]), sanitize_id(parsed_dict["room_id"]),
) )
except KeyError: except (KeyError, TypeError):
return MatrixErrorEvent.from_dict( return MatrixErrorEvent.from_dict(
server, server,
"Error joining room", "Error joining room",