test_admin.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. # Copyright 2019 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from collections import Counter
  15. from unittest.mock import Mock
  16. from twisted.test.proto_helpers import MemoryReactor
  17. import synapse.rest.admin
  18. import synapse.storage
  19. from synapse.api.constants import EventTypes, JoinRules
  20. from synapse.api.room_versions import RoomVersions
  21. from synapse.rest.client import knock, login, room
  22. from synapse.server import HomeServer
  23. from synapse.types import UserID
  24. from synapse.util import Clock
  25. from tests import unittest
  26. class ExfiltrateData(unittest.HomeserverTestCase):
  27. servlets = [
  28. synapse.rest.admin.register_servlets_for_client_rest_resource,
  29. login.register_servlets,
  30. room.register_servlets,
  31. knock.register_servlets,
  32. ]
  33. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  34. self.admin_handler = hs.get_admin_handler()
  35. self._store = hs.get_datastores().main
  36. self.user1 = self.register_user("user1", "password")
  37. self.token1 = self.login("user1", "password")
  38. self.user2 = self.register_user("user2", "password")
  39. self.token2 = self.login("user2", "password")
  40. def test_single_public_joined_room(self) -> None:
  41. """Test that we write *all* events for a public room"""
  42. room_id = self.helper.create_room_as(
  43. self.user1, tok=self.token1, is_public=True
  44. )
  45. self.helper.send(room_id, body="Hello!", tok=self.token1)
  46. self.helper.join(room_id, self.user2, tok=self.token2)
  47. self.helper.send(room_id, body="Hello again!", tok=self.token1)
  48. writer = Mock()
  49. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  50. writer.write_events.assert_called()
  51. # Since we can see all events there shouldn't be any extremities, so no
  52. # state should be written
  53. writer.write_state.assert_not_called()
  54. # Collect all events that were written
  55. written_events = []
  56. for (called_room_id, events), _ in writer.write_events.call_args_list:
  57. self.assertEqual(called_room_id, room_id)
  58. written_events.extend(events)
  59. # Check that the right number of events were written
  60. counter = Counter(
  61. (event.type, getattr(event, "state_key", None)) for event in written_events
  62. )
  63. self.assertEqual(counter[(EventTypes.Message, None)], 2)
  64. self.assertEqual(counter[(EventTypes.Member, self.user1)], 1)
  65. self.assertEqual(counter[(EventTypes.Member, self.user2)], 1)
  66. def test_single_private_joined_room(self) -> None:
  67. """Tests that we correctly write state when we can't see all events in
  68. a room.
  69. """
  70. room_id = self.helper.create_room_as(self.user1, tok=self.token1)
  71. self.helper.send_state(
  72. room_id,
  73. EventTypes.RoomHistoryVisibility,
  74. body={"history_visibility": "joined"},
  75. tok=self.token1,
  76. )
  77. self.helper.send(room_id, body="Hello!", tok=self.token1)
  78. self.helper.join(room_id, self.user2, tok=self.token2)
  79. self.helper.send(room_id, body="Hello again!", tok=self.token1)
  80. writer = Mock()
  81. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  82. writer.write_events.assert_called()
  83. # Since we can't see all events there should be one extremity.
  84. writer.write_state.assert_called_once()
  85. # Collect all events that were written
  86. written_events = []
  87. for (called_room_id, events), _ in writer.write_events.call_args_list:
  88. self.assertEqual(called_room_id, room_id)
  89. written_events.extend(events)
  90. # Check that the right number of events were written
  91. counter = Counter(
  92. (event.type, getattr(event, "state_key", None)) for event in written_events
  93. )
  94. self.assertEqual(counter[(EventTypes.Message, None)], 1)
  95. self.assertEqual(counter[(EventTypes.Member, self.user1)], 1)
  96. self.assertEqual(counter[(EventTypes.Member, self.user2)], 1)
  97. def test_single_left_room(self) -> None:
  98. """Tests that we don't see events in the room after we leave."""
  99. room_id = self.helper.create_room_as(self.user1, tok=self.token1)
  100. self.helper.send(room_id, body="Hello!", tok=self.token1)
  101. self.helper.join(room_id, self.user2, tok=self.token2)
  102. self.helper.send(room_id, body="Hello again!", tok=self.token1)
  103. self.helper.leave(room_id, self.user2, tok=self.token2)
  104. self.helper.send(room_id, body="Helloooooo!", tok=self.token1)
  105. writer = Mock()
  106. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  107. writer.write_events.assert_called()
  108. # Since we can see all events there shouldn't be any extremities, so no
  109. # state should be written
  110. writer.write_state.assert_not_called()
  111. written_events = []
  112. for (called_room_id, events), _ in writer.write_events.call_args_list:
  113. self.assertEqual(called_room_id, room_id)
  114. written_events.extend(events)
  115. # Check that the right number of events were written
  116. counter = Counter(
  117. (event.type, getattr(event, "state_key", None)) for event in written_events
  118. )
  119. self.assertEqual(counter[(EventTypes.Message, None)], 2)
  120. self.assertEqual(counter[(EventTypes.Member, self.user1)], 1)
  121. self.assertEqual(counter[(EventTypes.Member, self.user2)], 2)
  122. def test_single_left_rejoined_private_room(self) -> None:
  123. """Tests that see the correct events in private rooms when we
  124. repeatedly join and leave.
  125. """
  126. room_id = self.helper.create_room_as(self.user1, tok=self.token1)
  127. self.helper.send_state(
  128. room_id,
  129. EventTypes.RoomHistoryVisibility,
  130. body={"history_visibility": "joined"},
  131. tok=self.token1,
  132. )
  133. self.helper.send(room_id, body="Hello!", tok=self.token1)
  134. self.helper.join(room_id, self.user2, tok=self.token2)
  135. self.helper.send(room_id, body="Hello again!", tok=self.token1)
  136. self.helper.leave(room_id, self.user2, tok=self.token2)
  137. self.helper.send(room_id, body="Helloooooo!", tok=self.token1)
  138. self.helper.join(room_id, self.user2, tok=self.token2)
  139. self.helper.send(room_id, body="Helloooooo!!", tok=self.token1)
  140. writer = Mock()
  141. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  142. writer.write_events.assert_called_once()
  143. # Since we joined/left/joined again we expect there to be two gaps.
  144. self.assertEqual(writer.write_state.call_count, 2)
  145. written_events = []
  146. for (called_room_id, events), _ in writer.write_events.call_args_list:
  147. self.assertEqual(called_room_id, room_id)
  148. written_events.extend(events)
  149. # Check that the right number of events were written
  150. counter = Counter(
  151. (event.type, getattr(event, "state_key", None)) for event in written_events
  152. )
  153. self.assertEqual(counter[(EventTypes.Message, None)], 2)
  154. self.assertEqual(counter[(EventTypes.Member, self.user1)], 1)
  155. self.assertEqual(counter[(EventTypes.Member, self.user2)], 3)
  156. def test_invite(self) -> None:
  157. """Tests that pending invites get handled correctly."""
  158. room_id = self.helper.create_room_as(self.user1, tok=self.token1)
  159. self.helper.send(room_id, body="Hello!", tok=self.token1)
  160. self.helper.invite(room_id, self.user1, self.user2, tok=self.token1)
  161. writer = Mock()
  162. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  163. writer.write_events.assert_not_called()
  164. writer.write_state.assert_not_called()
  165. writer.write_invite.assert_called_once()
  166. args = writer.write_invite.call_args[0]
  167. self.assertEqual(args[0], room_id)
  168. self.assertEqual(args[1].content["membership"], "invite")
  169. self.assertTrue(args[2]) # Assert there is at least one bit of state
  170. def test_knock(self) -> None:
  171. """Tests that knock get handled correctly."""
  172. # create a knockable v7 room
  173. room_id = self.helper.create_room_as(
  174. self.user1, room_version=RoomVersions.V7.identifier, tok=self.token1
  175. )
  176. self.helper.send_state(
  177. room_id,
  178. EventTypes.JoinRules,
  179. {"join_rule": JoinRules.KNOCK},
  180. tok=self.token1,
  181. )
  182. self.helper.send(room_id, body="Hello!", tok=self.token1)
  183. self.helper.knock(room_id, self.user2, tok=self.token2)
  184. writer = Mock()
  185. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  186. writer.write_events.assert_not_called()
  187. writer.write_state.assert_not_called()
  188. writer.write_knock.assert_called_once()
  189. args = writer.write_knock.call_args[0]
  190. self.assertEqual(args[0], room_id)
  191. self.assertEqual(args[1].content["membership"], "knock")
  192. self.assertTrue(args[2]) # Assert there is at least one bit of state
  193. def test_profile(self) -> None:
  194. """Tests that user profile get exported."""
  195. writer = Mock()
  196. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  197. writer.write_events.assert_not_called()
  198. writer.write_profile.assert_called_once()
  199. # check only a few values, not all available
  200. args = writer.write_profile.call_args[0]
  201. self.assertEqual(args[0]["name"], self.user2)
  202. self.assertIn("displayname", args[0])
  203. self.assertIn("avatar_url", args[0])
  204. self.assertIn("threepids", args[0])
  205. self.assertIn("external_ids", args[0])
  206. self.assertIn("creation_ts", args[0])
  207. def test_devices(self) -> None:
  208. """Tests that user devices get exported."""
  209. writer = Mock()
  210. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  211. writer.write_events.assert_not_called()
  212. writer.write_devices.assert_called_once()
  213. args = writer.write_devices.call_args[0]
  214. self.assertEqual(len(args[0]), 1)
  215. self.assertEqual(args[0][0]["user_id"], self.user2)
  216. self.assertIn("device_id", args[0][0])
  217. self.assertIsNone(args[0][0]["display_name"])
  218. self.assertIsNone(args[0][0]["last_seen_user_agent"])
  219. self.assertIsNone(args[0][0]["last_seen_ts"])
  220. self.assertIsNone(args[0][0]["last_seen_ip"])
  221. def test_connections(self) -> None:
  222. """Tests that user sessions / connections get exported."""
  223. # Insert a user IP
  224. self.get_success(
  225. self._store.insert_client_ip(
  226. self.user2, "access_token", "ip", "user_agent", "MY_DEVICE"
  227. )
  228. )
  229. writer = Mock()
  230. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  231. writer.write_events.assert_not_called()
  232. writer.write_connections.assert_called_once()
  233. args = writer.write_connections.call_args[0]
  234. self.assertEqual(len(args[0]), 1)
  235. self.assertEqual(args[0][0]["ip"], "ip")
  236. self.assertEqual(args[0][0]["user_agent"], "user_agent")
  237. self.assertGreater(args[0][0]["last_seen"], 0)
  238. self.assertNotIn("access_token", args[0][0])
  239. def test_account_data(self) -> None:
  240. """Tests that user account data get exported."""
  241. # add account data
  242. self.get_success(
  243. self._store.add_account_data_for_user(self.user2, "m.global", {"a": 1})
  244. )
  245. self.get_success(
  246. self._store.add_account_data_to_room(
  247. self.user2, "test_room", "m.per_room", {"b": 2}
  248. )
  249. )
  250. writer = Mock()
  251. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  252. # two calls, one call for user data and one call for room data
  253. writer.write_account_data.assert_called()
  254. args = writer.write_account_data.call_args_list[0][0]
  255. self.assertEqual(args[0], "global")
  256. self.assertEqual(args[1]["m.global"]["a"], 1)
  257. args = writer.write_account_data.call_args_list[1][0]
  258. self.assertEqual(args[0], "test_room")
  259. self.assertEqual(args[1]["m.per_room"]["b"], 2)
  260. def test_media_ids(self) -> None:
  261. """Tests that media's metadata get exported."""
  262. self.get_success(
  263. self._store.store_local_media(
  264. media_id="media_1",
  265. media_type="image/png",
  266. time_now_ms=self.clock.time_msec(),
  267. upload_name=None,
  268. media_length=50,
  269. user_id=UserID.from_string(self.user2),
  270. )
  271. )
  272. writer = Mock()
  273. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  274. writer.write_media_id.assert_called_once()
  275. args = writer.write_media_id.call_args[0]
  276. self.assertEqual(args[0], "media_1")
  277. self.assertEqual(args[1]["media_id"], "media_1")
  278. self.assertEqual(args[1]["media_length"], 50)
  279. self.assertGreater(args[1]["created_ts"], 0)
  280. self.assertIsNone(args[1]["upload_name"])
  281. self.assertIsNone(args[1]["last_access_ts"])