Add sanitize_token() function.

This commit is contained in:
poljar (Damir Jelić) 2018-02-20 10:59:44 +01:00
parent 2741a7807d
commit ee2bcc69cb

View file

@ -32,7 +32,17 @@ from matrix.utils import (
from matrix.colors import Formatted from matrix.colors import Formatted
def sanitize_id(string): def sanitize_token(string):
# type: (str) -> str
string = sanitize_string(string)
if len(string) > 512:
raise ValueError
return string
def sanitize_string(string):
# type: (str) -> str # type: (str) -> str
if not isinstance(string, str): if not isinstance(string, str):
raise TypeError raise TypeError
@ -49,6 +59,16 @@ def sanitize_id(string):
return string.translate(remap) return string.translate(remap)
def sanitize_id(string):
# type: (str) -> str
string = sanitize_string(string)
if len(string) > 128:
raise ValueError
return string
def sanitize_age(age): def sanitize_age(age):
# type: (int) -> int # type: (int) -> int
if not isinstance(age, int): if not isinstance(age, int):
@ -143,7 +163,7 @@ class MatrixLoginEvent(MatrixEvent):
return cls( return cls(
server, server,
sanitize_id(parsed_dict["user_id"]), sanitize_id(parsed_dict["user_id"]),
sanitize_id(parsed_dict["access_token"]) sanitize_token(parsed_dict["access_token"])
) )
except (KeyError, TypeError, ValueError): except (KeyError, TypeError, ValueError):
return MatrixErrorEvent.from_dict( return MatrixErrorEvent.from_dict(