Change to is_valid

mv/add-mxid-validation-log
Mathieu Velten 2023-09-08 13:37:00 +02:00
parent 42a392f4e2
commit 01c582ff36
7 changed files with 42 additions and 13 deletions

View File

@ -1105,9 +1105,9 @@ class DeviceListUpdater(DeviceListWorkerUpdater):
)
prev_ids = [str(p) for p in prev_ids] # They may come as ints
# The result of `validate` is not used yet because for now we only want to
# The result of `is_valid` is not used yet because for now we only want to
# log invalid mxids in the wild.
UserID.from_string(user_id).validate(allow_historical_mxids=True)
UserID.is_valid(user_id, allow_historical_mxids=True)
if get_domain_from_id(user_id) != origin:
# TODO: Raise?

View File

@ -109,9 +109,9 @@ class DeviceMessageHandler:
origin,
sender_user_id,
)
# The result of `validate` is not used yet because for now we only want to
# The result of `is_valid` is not used yet because for now we only want to
# log invalid mxids in the wild.
UserID.from_string(sender_user_id).validate(allow_historical_mxids=True)
UserID.is_valid(sender_user_id, allow_historical_mxids=True)
message_type = content["type"]
message_id = content["message_id"]

View File

@ -1593,9 +1593,9 @@ class SigningKeyEduUpdater:
logger.warning("Got signing key update edu for %r from %r", user_id, origin)
return
# The result of `validate` is not used yet because for now we only want to
# The result of `is_valid` is not used yet because for now we only want to
# log invalid mxids in the wild.
UserID.from_string(user_id).validate(allow_historical_mxids=True)
UserID.is_valid(user_id, allow_historical_mxids=True)
room_ids = await self.store.get_rooms_for_user(user_id)
if not room_ids:

View File

@ -117,9 +117,9 @@ class ReceiptsHandler:
max_batch_id: Optional[int] = None
for receipt in receipts:
# The result of `validate` is not used yet because for now we only want to
# The result of `is_valid` is not used yet because for now we only want to
# log invalid mxids in the wild.
UserID.from_string(receipt.user_id).validate(allow_historical_mxids=True)
UserID.is_valid(receipt.user_id, allow_historical_mxids=True)
res = await self.store.insert_receipt(
receipt.room_id,

View File

@ -370,9 +370,9 @@ class TypingWriterHandler(FollowerTypingHandler):
room_id = content["room_id"]
user_id = content["user_id"]
# The result of `validate` is not used yet because for now we only want to
# The result of `is_valid` is not used yet because for now we only want to
# log invalid mxids in the wild.
UserID.from_string(user_id).validate(allow_historical_mxids=True)
UserID.is_valid(user_id, allow_historical_mxids=True)
# If we're not in the room just ditch the event entirely. This is
# probably an old server that has come back and thinks we're still in

View File

@ -398,9 +398,9 @@ class EventsPersistenceStorageController:
event_ids: List[str] = []
partitioned: Dict[str, List[Tuple[EventBase, EventContext]]] = {}
for event, ctx in events_and_contexts:
# The result of `validate` is not used yet because for now we only want to
# The result of `is_valid` is not used yet because for now we only want to
# log invalid mxids in the wild.
UserID.from_string(event.user_id).validate(allow_historical_mxids=True)
UserID.is_valid(event.user_id, allow_historical_mxids=True)
partitioned.setdefault(event.room_id, []).append((event, ctx))
event_ids.append(event.event_id)

View File

@ -310,7 +310,7 @@ class DomainSpecificString(metaclass=abc.ABCMeta):
return "%s%s:%s" % (self.SIGIL, self.localpart, self.domain)
@classmethod
def is_valid(cls: Type[DS], s: str) -> bool:
def is_valid(cls: Type[DS], s: str, **kwargs: Any) -> bool:
"""Parses the input string and attempts to ensure it is valid."""
# TODO: this does not reject an empty localpart or an overly-long string.
# See https://spec.matrix.org/v1.2/appendices/#identifier-grammar
@ -333,6 +333,35 @@ class UserID(DomainSpecificString):
SIGIL = "@"
@classmethod
def is_valid(cls: Type[DS], s: str, **kwargs: Any) -> bool:
""""""
"""Parses the user id str and attempts to ensure it is valid per the spec.
Args:
allow_historical_mxids: True to allow historical mxids, which can
include all printable ASCII chars minus `:`
Returns:
False if the user ID is invalid per the spec
"""
allow_historical_mxids = kwargs.get("allow_historical_mxids", False)
is_valid = DomainSpecificString.is_valid(s)
if len(s.encode("utf-8")) > 255:
logger.warn(
f"User ID {s} has more than 255 bytes and is invalid per the spec"
)
is_valid = False
obj = UserID.from_string(s)
if contains_invalid_mxid_characters(obj.localpart, allow_historical_mxids):
logger.warn(
f"localpart of User ID {s} contains invalid characters per the spec"
)
is_valid = False
return is_valid
def validate(self, allow_historical_mxids: Optional[bool] = False) -> bool:
"""Validate an user ID against the spec.