visibility.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. import itertools
  16. import logging
  17. import operator
  18. from twisted.internet import defer
  19. from synapse.api.constants import EventTypes, Membership
  20. from synapse.events.utils import prune_event
  21. from synapse.util.logcontext import make_deferred_yieldable, preserve_fn
  22. logger = logging.getLogger(__name__)
  23. VISIBILITY_PRIORITY = (
  24. "world_readable",
  25. "shared",
  26. "invited",
  27. "joined",
  28. )
  29. MEMBERSHIP_PRIORITY = (
  30. Membership.JOIN,
  31. Membership.INVITE,
  32. Membership.KNOCK,
  33. Membership.LEAVE,
  34. Membership.BAN,
  35. )
  36. @defer.inlineCallbacks
  37. def filter_events_for_client(store, user_id, events, is_peeking=False,
  38. always_include_ids=frozenset()):
  39. """
  40. Check which events a user is allowed to see
  41. Args:
  42. store (synapse.storage.DataStore): our datastore (can also be a worker
  43. store)
  44. user_id(str): user id to be checked
  45. events(list[synapse.events.EventBase]): sequence of events to be checked
  46. is_peeking(bool): should be True if:
  47. * the user is not currently a member of the room, and:
  48. * the user has not been a member of the room since the given
  49. events
  50. always_include_ids (set(event_id)): set of event ids to specifically
  51. include (unless sender is ignored)
  52. Returns:
  53. Deferred[list[synapse.events.EventBase]]
  54. """
  55. types = (
  56. (EventTypes.RoomHistoryVisibility, ""),
  57. (EventTypes.Member, user_id),
  58. )
  59. event_id_to_state = yield store.get_state_for_events(
  60. frozenset(e.event_id for e in events),
  61. types=types,
  62. )
  63. forgotten = yield make_deferred_yieldable(defer.gatherResults([
  64. defer.maybeDeferred(
  65. preserve_fn(store.who_forgot_in_room),
  66. room_id,
  67. )
  68. for room_id in frozenset(e.room_id for e in events)
  69. ], consumeErrors=True))
  70. # Set of membership event_ids that have been forgotten
  71. event_id_forgotten = frozenset(
  72. row["event_id"] for rows in forgotten for row in rows
  73. )
  74. ignore_dict_content = yield store.get_global_account_data_by_type_for_user(
  75. "m.ignored_user_list", user_id,
  76. )
  77. # FIXME: This will explode if people upload something incorrect.
  78. ignore_list = frozenset(
  79. ignore_dict_content.get("ignored_users", {}).keys()
  80. if ignore_dict_content else []
  81. )
  82. erased_senders = yield store.are_users_erased((e.sender for e in events))
  83. def allowed(event):
  84. """
  85. Args:
  86. event (synapse.events.EventBase): event to check
  87. Returns:
  88. None|EventBase:
  89. None if the user cannot see this event at all
  90. a redacted copy of the event if they can only see a redacted
  91. version
  92. the original event if they can see it as normal.
  93. """
  94. if not event.is_state() and event.sender in ignore_list:
  95. return None
  96. if event.event_id in always_include_ids:
  97. return event
  98. state = event_id_to_state[event.event_id]
  99. # get the room_visibility at the time of the event.
  100. visibility_event = state.get((EventTypes.RoomHistoryVisibility, ""), None)
  101. if visibility_event:
  102. visibility = visibility_event.content.get("history_visibility", "shared")
  103. else:
  104. visibility = "shared"
  105. if visibility not in VISIBILITY_PRIORITY:
  106. visibility = "shared"
  107. # Always allow history visibility events on boundaries. This is done
  108. # by setting the effective visibility to the least restrictive
  109. # of the old vs new.
  110. if event.type == EventTypes.RoomHistoryVisibility:
  111. prev_content = event.unsigned.get("prev_content", {})
  112. prev_visibility = prev_content.get("history_visibility", None)
  113. if prev_visibility not in VISIBILITY_PRIORITY:
  114. prev_visibility = "shared"
  115. new_priority = VISIBILITY_PRIORITY.index(visibility)
  116. old_priority = VISIBILITY_PRIORITY.index(prev_visibility)
  117. if old_priority < new_priority:
  118. visibility = prev_visibility
  119. # likewise, if the event is the user's own membership event, use
  120. # the 'most joined' membership
  121. membership = None
  122. if event.type == EventTypes.Member and event.state_key == user_id:
  123. membership = event.content.get("membership", None)
  124. if membership not in MEMBERSHIP_PRIORITY:
  125. membership = "leave"
  126. prev_content = event.unsigned.get("prev_content", {})
  127. prev_membership = prev_content.get("membership", None)
  128. if prev_membership not in MEMBERSHIP_PRIORITY:
  129. prev_membership = "leave"
  130. # Always allow the user to see their own leave events, otherwise
  131. # they won't see the room disappear if they reject the invite
  132. if membership == "leave" and (
  133. prev_membership == "join" or prev_membership == "invite"
  134. ):
  135. return event
  136. new_priority = MEMBERSHIP_PRIORITY.index(membership)
  137. old_priority = MEMBERSHIP_PRIORITY.index(prev_membership)
  138. if old_priority < new_priority:
  139. membership = prev_membership
  140. # otherwise, get the user's membership at the time of the event.
  141. if membership is None:
  142. membership_event = state.get((EventTypes.Member, user_id), None)
  143. if membership_event:
  144. # XXX why do we do this?
  145. # https://github.com/matrix-org/synapse/issues/3350
  146. if membership_event.event_id not in event_id_forgotten:
  147. membership = membership_event.membership
  148. # if the user was a member of the room at the time of the event,
  149. # they can see it.
  150. if membership == Membership.JOIN:
  151. return event
  152. # otherwise, it depends on the room visibility.
  153. if visibility == "joined":
  154. # we weren't a member at the time of the event, so we can't
  155. # see this event.
  156. return None
  157. elif visibility == "invited":
  158. # user can also see the event if they were *invited* at the time
  159. # of the event.
  160. return (
  161. event if membership == Membership.INVITE else None
  162. )
  163. elif visibility == "shared" and is_peeking:
  164. # if the visibility is shared, users cannot see the event unless
  165. # they have *subequently* joined the room (or were members at the
  166. # time, of course)
  167. #
  168. # XXX: if the user has subsequently joined and then left again,
  169. # ideally we would share history up to the point they left. But
  170. # we don't know when they left. We just treat it as though they
  171. # never joined, and restrict access.
  172. return None
  173. # the visibility is either shared or world_readable, and the user was
  174. # not a member at the time. We allow it, provided the original sender
  175. # has not requested their data to be erased, in which case, we return
  176. # a redacted version.
  177. if erased_senders[event.sender]:
  178. return prune_event(event)
  179. return event
  180. # check each event: gives an iterable[None|EventBase]
  181. filtered_events = itertools.imap(allowed, events)
  182. # remove the None entries
  183. filtered_events = filter(operator.truth, filtered_events)
  184. # we turn it into a list before returning it.
  185. defer.returnValue(list(filtered_events))