visibility.py 8.0 KB

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