Add a ID sanitization function.
Json allows certain control characters inside of strings. Filter those out for identifiers.
This commit is contained in:
parent
4537635db3
commit
3232f61f7e
1 changed files with 22 additions and 4 deletions
|
@ -22,6 +22,19 @@ import time
|
||||||
from matrix.globals import W, OPTIONS
|
from matrix.globals import W, OPTIONS
|
||||||
from matrix.utils import color_for_tags
|
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():
|
class MatrixEvent():
|
||||||
def __init__(self, server):
|
def __init__(self, server):
|
||||||
|
@ -84,8 +97,8 @@ class MatrixLoginEvent(MatrixEvent):
|
||||||
try:
|
try:
|
||||||
return cls(
|
return cls(
|
||||||
server,
|
server,
|
||||||
parsed_dict["user_id"],
|
sanitize_id(parsed_dict["user_id"]),
|
||||||
parsed_dict["access_token"]
|
sanitize_id(parsed_dict["access_token"])
|
||||||
)
|
)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return MatrixErrorEvent.from_dict(
|
return MatrixErrorEvent.from_dict(
|
||||||
|
@ -133,7 +146,7 @@ class MatrixSendEvent(MatrixEvent):
|
||||||
return cls(
|
return cls(
|
||||||
server,
|
server,
|
||||||
room_id,
|
room_id,
|
||||||
parsed_dict["event_id"],
|
sanitize_id(parsed_dict["event_id"]),
|
||||||
message
|
message
|
||||||
)
|
)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
@ -155,7 +168,12 @@ class MatrixTopicEvent(MatrixEvent):
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_dict(cls, server, room_id, topic, parsed_dict):
|
def from_dict(cls, server, room_id, topic, parsed_dict):
|
||||||
try:
|
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:
|
except KeyError:
|
||||||
return MatrixErrorEvent.from_dict(
|
return MatrixErrorEvent.from_dict(
|
||||||
server,
|
server,
|
||||||
|
|
Loading…
Add table
Reference in a new issue