1
0

test_redaction.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. # Copyright 2014-2021 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 typing import List, Optional, cast
  15. from canonicaljson import json
  16. from twisted.test.proto_helpers import MemoryReactor
  17. from synapse.api.constants import EventTypes, Membership
  18. from synapse.api.room_versions import RoomVersions
  19. from synapse.events import EventBase, _EventInternalMetadata
  20. from synapse.events.builder import EventBuilder
  21. from synapse.server import HomeServer
  22. from synapse.types import JsonDict, RoomID, UserID
  23. from synapse.util import Clock
  24. from tests import unittest
  25. from tests.utils import create_room
  26. class RedactionTestCase(unittest.HomeserverTestCase):
  27. def default_config(self) -> JsonDict:
  28. config = super().default_config()
  29. config["redaction_retention_period"] = "30d"
  30. return config
  31. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  32. self.store = hs.get_datastores().main
  33. storage = hs.get_storage_controllers()
  34. assert storage.persistence is not None
  35. self._persistence = storage.persistence
  36. self.event_builder_factory = hs.get_event_builder_factory()
  37. self.event_creation_handler = hs.get_event_creation_handler()
  38. self.u_alice = UserID.from_string("@alice:test")
  39. self.u_bob = UserID.from_string("@bob:test")
  40. self.room1 = RoomID.from_string("!abc123:test")
  41. self.get_success(
  42. create_room(hs, self.room1.to_string(), self.u_alice.to_string())
  43. )
  44. self.depth = 1
  45. def inject_room_member( # type: ignore[override]
  46. self,
  47. room: RoomID,
  48. user: UserID,
  49. membership: str,
  50. extra_content: Optional[JsonDict] = None,
  51. ) -> EventBase:
  52. content = {"membership": membership}
  53. content.update(extra_content or {})
  54. builder = self.event_builder_factory.for_room_version(
  55. RoomVersions.V1,
  56. {
  57. "type": EventTypes.Member,
  58. "sender": user.to_string(),
  59. "state_key": user.to_string(),
  60. "room_id": room.to_string(),
  61. "content": content,
  62. },
  63. )
  64. event, unpersisted_context = self.get_success(
  65. self.event_creation_handler.create_new_client_event(builder)
  66. )
  67. context = self.get_success(unpersisted_context.persist(event))
  68. self.get_success(self._persistence.persist_event(event, context))
  69. return event
  70. def inject_message(self, room: RoomID, user: UserID, body: str) -> EventBase:
  71. self.depth += 1
  72. builder = self.event_builder_factory.for_room_version(
  73. RoomVersions.V1,
  74. {
  75. "type": EventTypes.Message,
  76. "sender": user.to_string(),
  77. "state_key": user.to_string(),
  78. "room_id": room.to_string(),
  79. "content": {"body": body, "msgtype": "message"},
  80. },
  81. )
  82. event, unpersisted_context = self.get_success(
  83. self.event_creation_handler.create_new_client_event(builder)
  84. )
  85. context = self.get_success(unpersisted_context.persist(event))
  86. self.get_success(self._persistence.persist_event(event, context))
  87. return event
  88. def inject_redaction(
  89. self, room: RoomID, event_id: str, user: UserID, reason: str
  90. ) -> EventBase:
  91. builder = self.event_builder_factory.for_room_version(
  92. RoomVersions.V1,
  93. {
  94. "type": EventTypes.Redaction,
  95. "sender": user.to_string(),
  96. "state_key": user.to_string(),
  97. "room_id": room.to_string(),
  98. "content": {"reason": reason},
  99. "redacts": event_id,
  100. },
  101. )
  102. event, unpersisted_context = self.get_success(
  103. self.event_creation_handler.create_new_client_event(builder)
  104. )
  105. context = self.get_success(unpersisted_context.persist(event))
  106. self.get_success(self._persistence.persist_event(event, context))
  107. return event
  108. def test_redact(self) -> None:
  109. self.inject_room_member(self.room1, self.u_alice, Membership.JOIN)
  110. msg_event = self.inject_message(self.room1, self.u_alice, "t")
  111. # Check event has not been redacted:
  112. event = self.get_success(self.store.get_event(msg_event.event_id))
  113. self.assertObjectHasAttributes(
  114. {
  115. "type": EventTypes.Message,
  116. "user_id": self.u_alice.to_string(),
  117. "content": {"body": "t", "msgtype": "message"},
  118. },
  119. event,
  120. )
  121. self.assertFalse("redacted_because" in event.unsigned)
  122. # Redact event
  123. reason = "Because I said so"
  124. self.inject_redaction(self.room1, msg_event.event_id, self.u_alice, reason)
  125. event = self.get_success(self.store.get_event(msg_event.event_id))
  126. self.assertEqual(msg_event.event_id, event.event_id)
  127. self.assertTrue("redacted_because" in event.unsigned)
  128. self.assertObjectHasAttributes(
  129. {
  130. "type": EventTypes.Message,
  131. "user_id": self.u_alice.to_string(),
  132. "content": {},
  133. },
  134. event,
  135. )
  136. self.assertObjectHasAttributes(
  137. {
  138. "type": EventTypes.Redaction,
  139. "user_id": self.u_alice.to_string(),
  140. "content": {"reason": reason},
  141. },
  142. event.unsigned["redacted_because"],
  143. )
  144. def test_redact_join(self) -> None:
  145. self.inject_room_member(self.room1, self.u_alice, Membership.JOIN)
  146. msg_event = self.inject_room_member(
  147. self.room1, self.u_bob, Membership.JOIN, extra_content={"blue": "red"}
  148. )
  149. event = self.get_success(self.store.get_event(msg_event.event_id))
  150. self.assertObjectHasAttributes(
  151. {
  152. "type": EventTypes.Member,
  153. "user_id": self.u_bob.to_string(),
  154. "content": {"membership": Membership.JOIN, "blue": "red"},
  155. },
  156. event,
  157. )
  158. self.assertFalse(hasattr(event, "redacted_because"))
  159. # Redact event
  160. reason = "Because I said so"
  161. self.inject_redaction(self.room1, msg_event.event_id, self.u_alice, reason)
  162. # Check redaction
  163. event = self.get_success(self.store.get_event(msg_event.event_id))
  164. self.assertTrue("redacted_because" in event.unsigned)
  165. self.assertObjectHasAttributes(
  166. {
  167. "type": EventTypes.Member,
  168. "user_id": self.u_bob.to_string(),
  169. "content": {"membership": Membership.JOIN},
  170. },
  171. event,
  172. )
  173. self.assertObjectHasAttributes(
  174. {
  175. "type": EventTypes.Redaction,
  176. "user_id": self.u_alice.to_string(),
  177. "content": {"reason": reason},
  178. },
  179. event.unsigned["redacted_because"],
  180. )
  181. def test_circular_redaction(self) -> None:
  182. redaction_event_id1 = "$redaction1_id:test"
  183. redaction_event_id2 = "$redaction2_id:test"
  184. class EventIdManglingBuilder:
  185. def __init__(self, base_builder: EventBuilder, event_id: str):
  186. self._base_builder = base_builder
  187. self._event_id = event_id
  188. async def build(
  189. self,
  190. prev_event_ids: List[str],
  191. auth_event_ids: Optional[List[str]],
  192. depth: Optional[int] = None,
  193. ) -> EventBase:
  194. built_event = await self._base_builder.build(
  195. prev_event_ids=prev_event_ids, auth_event_ids=auth_event_ids
  196. )
  197. built_event._event_id = self._event_id # type: ignore[attr-defined]
  198. built_event._dict["event_id"] = self._event_id
  199. assert built_event.event_id == self._event_id
  200. return built_event
  201. @property
  202. def room_id(self) -> str:
  203. return self._base_builder.room_id
  204. @property
  205. def type(self) -> str:
  206. return self._base_builder.type
  207. @property
  208. def internal_metadata(self) -> _EventInternalMetadata:
  209. return self._base_builder.internal_metadata
  210. event_1, unpersisted_context_1 = self.get_success(
  211. self.event_creation_handler.create_new_client_event(
  212. cast(
  213. EventBuilder,
  214. EventIdManglingBuilder(
  215. self.event_builder_factory.for_room_version(
  216. RoomVersions.V1,
  217. {
  218. "type": EventTypes.Redaction,
  219. "sender": self.u_alice.to_string(),
  220. "room_id": self.room1.to_string(),
  221. "content": {"reason": "test"},
  222. "redacts": redaction_event_id2,
  223. },
  224. ),
  225. redaction_event_id1,
  226. ),
  227. )
  228. )
  229. )
  230. context_1 = self.get_success(unpersisted_context_1.persist(event_1))
  231. self.get_success(self._persistence.persist_event(event_1, context_1))
  232. event_2, unpersisted_context_2 = self.get_success(
  233. self.event_creation_handler.create_new_client_event(
  234. cast(
  235. EventBuilder,
  236. EventIdManglingBuilder(
  237. self.event_builder_factory.for_room_version(
  238. RoomVersions.V1,
  239. {
  240. "type": EventTypes.Redaction,
  241. "sender": self.u_alice.to_string(),
  242. "room_id": self.room1.to_string(),
  243. "content": {"reason": "test"},
  244. "redacts": redaction_event_id1,
  245. },
  246. ),
  247. redaction_event_id2,
  248. ),
  249. )
  250. )
  251. )
  252. context_2 = self.get_success(unpersisted_context_2.persist(event_2))
  253. self.get_success(self._persistence.persist_event(event_2, context_2))
  254. # fetch one of the redactions
  255. fetched = self.get_success(self.store.get_event(redaction_event_id1))
  256. # it should have been redacted
  257. self.assertEqual(fetched.unsigned["redacted_by"], redaction_event_id2)
  258. self.assertEqual(
  259. fetched.unsigned["redacted_because"].event_id, redaction_event_id2
  260. )
  261. def test_redact_censor(self) -> None:
  262. """Test that a redacted event gets censored in the DB after a month"""
  263. self.inject_room_member(self.room1, self.u_alice, Membership.JOIN)
  264. msg_event = self.inject_message(self.room1, self.u_alice, "t")
  265. # Check event has not been redacted:
  266. event = self.get_success(self.store.get_event(msg_event.event_id))
  267. self.assertObjectHasAttributes(
  268. {
  269. "type": EventTypes.Message,
  270. "user_id": self.u_alice.to_string(),
  271. "content": {"body": "t", "msgtype": "message"},
  272. },
  273. event,
  274. )
  275. self.assertFalse("redacted_because" in event.unsigned)
  276. # Redact event
  277. reason = "Because I said so"
  278. self.inject_redaction(self.room1, msg_event.event_id, self.u_alice, reason)
  279. event = self.get_success(self.store.get_event(msg_event.event_id))
  280. self.assertTrue("redacted_because" in event.unsigned)
  281. self.assertObjectHasAttributes(
  282. {
  283. "type": EventTypes.Message,
  284. "user_id": self.u_alice.to_string(),
  285. "content": {},
  286. },
  287. event,
  288. )
  289. event_json = self.get_success(
  290. self.store.db_pool.simple_select_one_onecol(
  291. table="event_json",
  292. keyvalues={"event_id": msg_event.event_id},
  293. retcol="json",
  294. )
  295. )
  296. self.assert_dict(
  297. {"content": {"body": "t", "msgtype": "message"}}, json.loads(event_json)
  298. )
  299. # Advance by 30 days, then advance again to ensure that the looping call
  300. # for updating the stream position gets called and then the looping call
  301. # for the censoring gets called.
  302. self.reactor.advance(60 * 60 * 24 * 31)
  303. self.reactor.advance(60 * 60 * 2)
  304. event_json = self.get_success(
  305. self.store.db_pool.simple_select_one_onecol(
  306. table="event_json",
  307. keyvalues={"event_id": msg_event.event_id},
  308. retcol="json",
  309. )
  310. )
  311. self.assert_dict({"content": {}}, json.loads(event_json))
  312. def test_redact_redaction(self) -> None:
  313. """Tests that we can redact a redaction and can fetch it again."""
  314. self.inject_room_member(self.room1, self.u_alice, Membership.JOIN)
  315. msg_event = self.inject_message(self.room1, self.u_alice, "t")
  316. first_redact_event = self.inject_redaction(
  317. self.room1, msg_event.event_id, self.u_alice, "Redacting message"
  318. )
  319. self.inject_redaction(
  320. self.room1,
  321. first_redact_event.event_id,
  322. self.u_alice,
  323. "Redacting redaction",
  324. )
  325. # Now lets jump to the future where we have censored the redaction event
  326. # in the DB.
  327. self.reactor.advance(60 * 60 * 24 * 31)
  328. # We just want to check that fetching the event doesn't raise an exception.
  329. self.get_success(
  330. self.store.get_event(first_redact_event.event_id, allow_none=True)
  331. )
  332. def test_store_redacted_redaction(self) -> None:
  333. """Tests that we can store a redacted redaction."""
  334. self.inject_room_member(self.room1, self.u_alice, Membership.JOIN)
  335. builder = self.event_builder_factory.for_room_version(
  336. RoomVersions.V1,
  337. {
  338. "type": EventTypes.Redaction,
  339. "sender": self.u_alice.to_string(),
  340. "room_id": self.room1.to_string(),
  341. "content": {"reason": "foo"},
  342. },
  343. )
  344. redaction_event, unpersisted_context = self.get_success(
  345. self.event_creation_handler.create_new_client_event(builder)
  346. )
  347. context = self.get_success(unpersisted_context.persist(redaction_event))
  348. self.get_success(self._persistence.persist_event(redaction_event, context))
  349. # Now lets jump to the future where we have censored the redaction event
  350. # in the DB.
  351. self.reactor.advance(60 * 60 * 24 * 31)
  352. # We just want to check that fetching the event doesn't raise an exception.
  353. self.get_success(
  354. self.store.get_event(redaction_event.event_id, allow_none=True)
  355. )