test_redaction.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2019 The Matrix.org Foundation C.I.C.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from mock import Mock
  17. from canonicaljson import json
  18. from twisted.internet import defer
  19. from synapse.api.constants import EventTypes, Membership
  20. from synapse.api.room_versions import RoomVersions
  21. from synapse.types import RoomID, UserID
  22. from tests import unittest
  23. from tests.utils import create_room
  24. class RedactionTestCase(unittest.HomeserverTestCase):
  25. def make_homeserver(self, reactor, clock):
  26. config = self.default_config()
  27. config["redaction_retention_period"] = "30d"
  28. return self.setup_test_homeserver(
  29. resource_for_federation=Mock(), http_client=None, config=config
  30. )
  31. def prepare(self, reactor, clock, hs):
  32. self.store = hs.get_datastore()
  33. self.storage = hs.get_storage()
  34. self.event_builder_factory = hs.get_event_builder_factory()
  35. self.event_creation_handler = hs.get_event_creation_handler()
  36. self.u_alice = UserID.from_string("@alice:test")
  37. self.u_bob = UserID.from_string("@bob:test")
  38. self.room1 = RoomID.from_string("!abc123:test")
  39. self.get_success(
  40. create_room(hs, self.room1.to_string(), self.u_alice.to_string())
  41. )
  42. self.depth = 1
  43. def inject_room_member(
  44. self, room, user, membership, replaces_state=None, extra_content={}
  45. ):
  46. content = {"membership": membership}
  47. content.update(extra_content)
  48. builder = self.event_builder_factory.for_room_version(
  49. RoomVersions.V1,
  50. {
  51. "type": EventTypes.Member,
  52. "sender": user.to_string(),
  53. "state_key": user.to_string(),
  54. "room_id": room.to_string(),
  55. "content": content,
  56. },
  57. )
  58. event, context = self.get_success(
  59. self.event_creation_handler.create_new_client_event(builder)
  60. )
  61. self.get_success(self.storage.persistence.persist_event(event, context))
  62. return event
  63. def inject_message(self, room, user, body):
  64. self.depth += 1
  65. builder = self.event_builder_factory.for_room_version(
  66. RoomVersions.V1,
  67. {
  68. "type": EventTypes.Message,
  69. "sender": user.to_string(),
  70. "state_key": user.to_string(),
  71. "room_id": room.to_string(),
  72. "content": {"body": body, "msgtype": "message"},
  73. },
  74. )
  75. event, context = self.get_success(
  76. self.event_creation_handler.create_new_client_event(builder)
  77. )
  78. self.get_success(self.storage.persistence.persist_event(event, context))
  79. return event
  80. def inject_redaction(self, room, event_id, user, reason):
  81. builder = self.event_builder_factory.for_room_version(
  82. RoomVersions.V1,
  83. {
  84. "type": EventTypes.Redaction,
  85. "sender": user.to_string(),
  86. "state_key": user.to_string(),
  87. "room_id": room.to_string(),
  88. "content": {"reason": reason},
  89. "redacts": event_id,
  90. },
  91. )
  92. event, context = self.get_success(
  93. self.event_creation_handler.create_new_client_event(builder)
  94. )
  95. self.get_success(self.storage.persistence.persist_event(event, context))
  96. return event
  97. def test_redact(self):
  98. self.get_success(
  99. self.inject_room_member(self.room1, self.u_alice, Membership.JOIN)
  100. )
  101. msg_event = self.get_success(self.inject_message(self.room1, self.u_alice, "t"))
  102. # Check event has not been redacted:
  103. event = self.get_success(self.store.get_event(msg_event.event_id))
  104. self.assertObjectHasAttributes(
  105. {
  106. "type": EventTypes.Message,
  107. "user_id": self.u_alice.to_string(),
  108. "content": {"body": "t", "msgtype": "message"},
  109. },
  110. event,
  111. )
  112. self.assertFalse("redacted_because" in event.unsigned)
  113. # Redact event
  114. reason = "Because I said so"
  115. self.get_success(
  116. self.inject_redaction(self.room1, msg_event.event_id, self.u_alice, reason)
  117. )
  118. event = self.get_success(self.store.get_event(msg_event.event_id))
  119. self.assertEqual(msg_event.event_id, event.event_id)
  120. self.assertTrue("redacted_because" in event.unsigned)
  121. self.assertObjectHasAttributes(
  122. {
  123. "type": EventTypes.Message,
  124. "user_id": self.u_alice.to_string(),
  125. "content": {},
  126. },
  127. event,
  128. )
  129. self.assertObjectHasAttributes(
  130. {
  131. "type": EventTypes.Redaction,
  132. "user_id": self.u_alice.to_string(),
  133. "content": {"reason": reason},
  134. },
  135. event.unsigned["redacted_because"],
  136. )
  137. def test_redact_join(self):
  138. self.get_success(
  139. self.inject_room_member(self.room1, self.u_alice, Membership.JOIN)
  140. )
  141. msg_event = self.get_success(
  142. self.inject_room_member(
  143. self.room1, self.u_bob, Membership.JOIN, extra_content={"blue": "red"}
  144. )
  145. )
  146. event = self.get_success(self.store.get_event(msg_event.event_id))
  147. self.assertObjectHasAttributes(
  148. {
  149. "type": EventTypes.Member,
  150. "user_id": self.u_bob.to_string(),
  151. "content": {"membership": Membership.JOIN, "blue": "red"},
  152. },
  153. event,
  154. )
  155. self.assertFalse(hasattr(event, "redacted_because"))
  156. # Redact event
  157. reason = "Because I said so"
  158. self.get_success(
  159. self.inject_redaction(self.room1, msg_event.event_id, self.u_alice, reason)
  160. )
  161. # Check redaction
  162. event = self.get_success(self.store.get_event(msg_event.event_id))
  163. self.assertTrue("redacted_because" in event.unsigned)
  164. self.assertObjectHasAttributes(
  165. {
  166. "type": EventTypes.Member,
  167. "user_id": self.u_bob.to_string(),
  168. "content": {"membership": Membership.JOIN},
  169. },
  170. event,
  171. )
  172. self.assertObjectHasAttributes(
  173. {
  174. "type": EventTypes.Redaction,
  175. "user_id": self.u_alice.to_string(),
  176. "content": {"reason": reason},
  177. },
  178. event.unsigned["redacted_because"],
  179. )
  180. def test_circular_redaction(self):
  181. redaction_event_id1 = "$redaction1_id:test"
  182. redaction_event_id2 = "$redaction2_id:test"
  183. class EventIdManglingBuilder:
  184. def __init__(self, base_builder, event_id):
  185. self._base_builder = base_builder
  186. self._event_id = event_id
  187. @defer.inlineCallbacks
  188. def build(self, prev_event_ids):
  189. built_event = yield self._base_builder.build(prev_event_ids)
  190. built_event.event_id = self._event_id
  191. built_event._event_dict["event_id"] = self._event_id
  192. return built_event
  193. @property
  194. def room_id(self):
  195. return self._base_builder.room_id
  196. event_1, context_1 = self.get_success(
  197. self.event_creation_handler.create_new_client_event(
  198. EventIdManglingBuilder(
  199. self.event_builder_factory.for_room_version(
  200. RoomVersions.V1,
  201. {
  202. "type": EventTypes.Redaction,
  203. "sender": self.u_alice.to_string(),
  204. "room_id": self.room1.to_string(),
  205. "content": {"reason": "test"},
  206. "redacts": redaction_event_id2,
  207. },
  208. ),
  209. redaction_event_id1,
  210. )
  211. )
  212. )
  213. self.get_success(self.storage.persistence.persist_event(event_1, context_1))
  214. event_2, context_2 = self.get_success(
  215. self.event_creation_handler.create_new_client_event(
  216. EventIdManglingBuilder(
  217. self.event_builder_factory.for_room_version(
  218. RoomVersions.V1,
  219. {
  220. "type": EventTypes.Redaction,
  221. "sender": self.u_alice.to_string(),
  222. "room_id": self.room1.to_string(),
  223. "content": {"reason": "test"},
  224. "redacts": redaction_event_id1,
  225. },
  226. ),
  227. redaction_event_id2,
  228. )
  229. )
  230. )
  231. self.get_success(self.storage.persistence.persist_event(event_2, context_2))
  232. # fetch one of the redactions
  233. fetched = self.get_success(self.store.get_event(redaction_event_id1))
  234. # it should have been redacted
  235. self.assertEqual(fetched.unsigned["redacted_by"], redaction_event_id2)
  236. self.assertEqual(
  237. fetched.unsigned["redacted_because"].event_id, redaction_event_id2
  238. )
  239. def test_redact_censor(self):
  240. """Test that a redacted event gets censored in the DB after a month
  241. """
  242. self.get_success(
  243. self.inject_room_member(self.room1, self.u_alice, Membership.JOIN)
  244. )
  245. msg_event = self.get_success(self.inject_message(self.room1, self.u_alice, "t"))
  246. # Check event has not been redacted:
  247. event = self.get_success(self.store.get_event(msg_event.event_id))
  248. self.assertObjectHasAttributes(
  249. {
  250. "type": EventTypes.Message,
  251. "user_id": self.u_alice.to_string(),
  252. "content": {"body": "t", "msgtype": "message"},
  253. },
  254. event,
  255. )
  256. self.assertFalse("redacted_because" in event.unsigned)
  257. # Redact event
  258. reason = "Because I said so"
  259. self.get_success(
  260. self.inject_redaction(self.room1, msg_event.event_id, self.u_alice, reason)
  261. )
  262. event = self.get_success(self.store.get_event(msg_event.event_id))
  263. self.assertTrue("redacted_because" in event.unsigned)
  264. self.assertObjectHasAttributes(
  265. {
  266. "type": EventTypes.Message,
  267. "user_id": self.u_alice.to_string(),
  268. "content": {},
  269. },
  270. event,
  271. )
  272. event_json = self.get_success(
  273. self.store._simple_select_one_onecol(
  274. table="event_json",
  275. keyvalues={"event_id": msg_event.event_id},
  276. retcol="json",
  277. )
  278. )
  279. self.assert_dict(
  280. {"content": {"body": "t", "msgtype": "message"}}, json.loads(event_json)
  281. )
  282. # Advance by 30 days, then advance again to ensure that the looping call
  283. # for updating the stream position gets called and then the looping call
  284. # for the censoring gets called.
  285. self.reactor.advance(60 * 60 * 24 * 31)
  286. self.reactor.advance(60 * 60 * 2)
  287. event_json = self.get_success(
  288. self.store._simple_select_one_onecol(
  289. table="event_json",
  290. keyvalues={"event_id": msg_event.event_id},
  291. retcol="json",
  292. )
  293. )
  294. self.assert_dict({"content": {}}, json.loads(event_json))
  295. def test_redact_redaction(self):
  296. """Tests that we can redact a redaction and can fetch it again.
  297. """
  298. self.get_success(
  299. self.inject_room_member(self.room1, self.u_alice, Membership.JOIN)
  300. )
  301. msg_event = self.get_success(self.inject_message(self.room1, self.u_alice, "t"))
  302. first_redact_event = self.get_success(
  303. self.inject_redaction(
  304. self.room1, msg_event.event_id, self.u_alice, "Redacting message"
  305. )
  306. )
  307. self.get_success(
  308. self.inject_redaction(
  309. self.room1,
  310. first_redact_event.event_id,
  311. self.u_alice,
  312. "Redacting redaction",
  313. )
  314. )
  315. # Now lets jump to the future where we have censored the redaction event
  316. # in the DB.
  317. self.reactor.advance(60 * 60 * 24 * 31)
  318. # We just want to check that fetching the event doesn't raise an exception.
  319. self.get_success(
  320. self.store.get_event(first_redact_event.event_id, allow_none=True)
  321. )