validator.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from six import string_types
  16. from synapse.api.constants import MAX_ALIAS_LENGTH, EventTypes, Membership
  17. from synapse.api.errors import Codes, SynapseError
  18. from synapse.api.room_versions import EventFormatVersions
  19. from synapse.types import EventID, RoomID, UserID
  20. class EventValidator(object):
  21. def validate_new(self, event):
  22. """Validates the event has roughly the right format
  23. Args:
  24. event (FrozenEvent)
  25. """
  26. self.validate_builder(event)
  27. if event.format_version == EventFormatVersions.V1:
  28. EventID.from_string(event.event_id)
  29. required = [
  30. "auth_events",
  31. "content",
  32. "hashes",
  33. "origin",
  34. "prev_events",
  35. "sender",
  36. "type",
  37. ]
  38. for k in required:
  39. if not hasattr(event, k):
  40. raise SynapseError(400, "Event does not have key %s" % (k,))
  41. # Check that the following keys have string values
  42. event_strings = ["origin"]
  43. for s in event_strings:
  44. if not isinstance(getattr(event, s), string_types):
  45. raise SynapseError(400, "'%s' not a string type" % (s,))
  46. if event.type == EventTypes.Aliases:
  47. if "aliases" in event.content:
  48. for alias in event.content["aliases"]:
  49. if len(alias) > MAX_ALIAS_LENGTH:
  50. raise SynapseError(
  51. 400,
  52. (
  53. "Can't create aliases longer than"
  54. " %d characters" % (MAX_ALIAS_LENGTH,)
  55. ),
  56. Codes.INVALID_PARAM,
  57. )
  58. def validate_builder(self, event):
  59. """Validates that the builder/event has roughly the right format. Only
  60. checks values that we expect a proto event to have, rather than all the
  61. fields an event would have
  62. Args:
  63. event (EventBuilder|FrozenEvent)
  64. """
  65. strings = ["room_id", "sender", "type"]
  66. if hasattr(event, "state_key"):
  67. strings.append("state_key")
  68. for s in strings:
  69. if not isinstance(getattr(event, s), string_types):
  70. raise SynapseError(400, "Not '%s' a string type" % (s,))
  71. RoomID.from_string(event.room_id)
  72. UserID.from_string(event.sender)
  73. if event.type == EventTypes.Message:
  74. strings = ["body", "msgtype"]
  75. self._ensure_strings(event.content, strings)
  76. elif event.type == EventTypes.Topic:
  77. self._ensure_strings(event.content, ["topic"])
  78. self._ensure_state_event(event)
  79. elif event.type == EventTypes.Name:
  80. self._ensure_strings(event.content, ["name"])
  81. self._ensure_state_event(event)
  82. elif event.type == EventTypes.Member:
  83. if "membership" not in event.content:
  84. raise SynapseError(400, "Content has not membership key")
  85. if event.content["membership"] not in Membership.LIST:
  86. raise SynapseError(400, "Invalid membership key")
  87. self._ensure_state_event(event)
  88. elif event.type == EventTypes.Tombstone:
  89. if "replacement_room" not in event.content:
  90. raise SynapseError(400, "Content has no replacement_room key")
  91. if event.content["replacement_room"] == event.room_id:
  92. raise SynapseError(
  93. 400, "Tombstone cannot reference the room it was sent in"
  94. )
  95. self._ensure_state_event(event)
  96. def _ensure_strings(self, d, keys):
  97. for s in keys:
  98. if s not in d:
  99. raise SynapseError(400, "'%s' not in content" % (s,))
  100. if not isinstance(d[s], string_types):
  101. raise SynapseError(400, "'%s' not a string type" % (s,))
  102. def _ensure_state_event(self, event):
  103. if not event.is_state():
  104. raise SynapseError(400, "'%s' must be state events" % (event.type,))