Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Split up event validation between event and builder #4494

Merged
merged 5 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/4494.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add infrastructure to support different event formats
73 changes: 53 additions & 20 deletions synapse/events/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ class EventValidator(object):

def validate(self, event):
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
EventID.from_string(event.event_id)
RoomID.from_string(event.room_id)

required = [
# "auth_events",
"auth_events",
"content",
# "hashes",
"hashes",
"origin",
# "prev_events",
"prev_events",
"sender",
"type",
]
Expand All @@ -43,33 +42,60 @@ def validate(self, event):
# Check that the following keys have string values
strings = [
"origin",
"sender",
"type",
]

if hasattr(event, "state_key"):
strings.append("state_key")

for s in strings:
if not isinstance(getattr(event, s), string_types):
raise SynapseError(400, "Not '%s' a string type" % (s,))

if event.type == EventTypes.Member:
if "membership" not in event.content:
raise SynapseError(400, "Content has not membership key")
def validate_new(self, event):
"""Validates the event has roughly the right format

if event.content["membership"] not in Membership.LIST:
raise SynapseError(400, "Invalid membership key")
Args:
event (FrozenEvent)
"""
self.validate_builder(event)
self.validate(event)

# Check that the following keys have dictionary values
# TODO
UserID.from_string(event.sender)
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved

if event.type == EventTypes.Message:
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
strings = [
"body",
"msgtype",
]

# Check that the following keys have the correct format for DAGs
# TODO
self._ensure_strings(event.content, strings)

def validate_new(self, event):
self.validate(event)
elif event.type == EventTypes.Topic:
self._ensure_strings(event.content, ["topic"])

elif event.type == EventTypes.Name:
self._ensure_strings(event.content, ["name"])

def validate_builder(self, event):
"""Validates that the builder/event has roughly the right format. Only
checks values that we expect a proto event to have, rather than all the
fields an event would have

Args:
event (EventBuilder|FrozenEvent)
"""

strings = [
"room_id",
"sender",
"type",
]

if hasattr(event, "state_key"):
strings.append("state_key")

for s in strings:
if not isinstance(getattr(event, s), string_types):
raise SynapseError(400, "Not '%s' a string type" % (s,))
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved

RoomID.from_string(event.room_id)
UserID.from_string(event.sender)

if event.type == EventTypes.Message:
Expand All @@ -86,6 +112,13 @@ def validate_new(self, event):
elif event.type == EventTypes.Name:
self._ensure_strings(event.content, ["name"])

elif event.type == EventTypes.Member:
if "membership" not in event.content:
raise SynapseError(400, "Content has not membership key")

if event.content["membership"] not in Membership.LIST:
raise SynapseError(400, "Invalid membership key")

def _ensure_strings(self, d, keys):
for s in keys:
if s not in d:
Expand Down
7 changes: 5 additions & 2 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2278,7 +2278,7 @@ def exchange_third_party_invite(
room_version = yield self.store.get_room_version(room_id)
builder = self.event_builder_factory.new(room_version, event_dict)

EventValidator().validate_new(builder)
EventValidator().validate_builder(builder)
event, context = yield self.event_creation_handler.create_new_client_event(
builder=builder
)
Expand All @@ -2287,6 +2287,8 @@ def exchange_third_party_invite(
room_version, event_dict, event, context
)

EventValidator().validate_new(event)

try:
yield self.auth.check_from_context(event, context)
except AuthError as e:
Expand Down Expand Up @@ -2372,10 +2374,11 @@ def add_display_name_to_third_party_invite(self, room_version, event_dict,
# auth check code will explode appropriately.

builder = self.event_builder_factory.new(room_version, event_dict)
EventValidator().validate_new(builder)
EventValidator().validate_builder(builder)
event, context = yield self.event_creation_handler.create_new_client_event(
builder=builder,
)
EventValidator().validate_new(event)
defer.returnValue((event, context))

@defer.inlineCallbacks
Expand Down
4 changes: 3 additions & 1 deletion synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def create_event(self, requester, event_dict, token_id=None, txn_id=None,

builder = self.event_builder_factory.new(room_version, event_dict)

self.validator.validate_new(builder)
self.validator.validate_builder(builder)

if builder.type == EventTypes.Member:
membership = builder.content.get("membership", None)
Expand Down Expand Up @@ -326,6 +326,8 @@ def create_event(self, requester, event_dict, token_id=None, txn_id=None,
prev_events_and_hashes=prev_events_and_hashes,
)

self.validator.validate_new(event)

defer.returnValue((event, context))

def _is_exempt_from_privacy_policy(self, builder, requester):
Expand Down