Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

426 строки
15 KiB

  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. # Copyright 2022 The Matrix.org Foundation C.I.C.
  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 logging
  16. import re
  17. from enum import Enum
  18. from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Pattern, Sequence
  19. import attr
  20. from netaddr import IPSet
  21. from synapse.api.constants import EventTypes
  22. from synapse.events import EventBase
  23. from synapse.types import DeviceListUpdates, JsonDict, JsonMapping, UserID
  24. from synapse.util.caches.descriptors import _CacheContext, cached
  25. if TYPE_CHECKING:
  26. from synapse.appservice.api import ApplicationServiceApi
  27. from synapse.storage.databases.main import DataStore
  28. logger = logging.getLogger(__name__)
  29. # Type for the `device_one_time_keys_count` field in an appservice transaction
  30. # user ID -> {device ID -> {algorithm -> count}}
  31. TransactionOneTimeKeysCount = Dict[str, Dict[str, Dict[str, int]]]
  32. # Type for the `device_unused_fallback_key_types` field in an appservice transaction
  33. # user ID -> {device ID -> [algorithm]}
  34. TransactionUnusedFallbackKeys = Dict[str, Dict[str, List[str]]]
  35. class ApplicationServiceState(Enum):
  36. DOWN = "down"
  37. UP = "up"
  38. @attr.s(slots=True, frozen=True, auto_attribs=True)
  39. class Namespace:
  40. exclusive: bool
  41. regex: Pattern[str]
  42. class ApplicationService:
  43. """Defines an application service. This definition is mostly what is
  44. provided to the /register AS API.
  45. Provides methods to check if this service is "interested" in events.
  46. """
  47. NS_USERS = "users"
  48. NS_ALIASES = "aliases"
  49. NS_ROOMS = "rooms"
  50. # The ordering here is important as it is used to map database values (which
  51. # are stored as ints representing the position in this list) to namespace
  52. # values.
  53. NS_LIST = [NS_USERS, NS_ALIASES, NS_ROOMS]
  54. def __init__(
  55. self,
  56. token: str,
  57. id: str,
  58. sender: str,
  59. url: Optional[str] = None,
  60. namespaces: Optional[JsonDict] = None,
  61. hs_token: Optional[str] = None,
  62. protocols: Optional[Iterable[str]] = None,
  63. rate_limited: bool = True,
  64. ip_range_whitelist: Optional[IPSet] = None,
  65. supports_ephemeral: bool = False,
  66. msc3202_transaction_extensions: bool = False,
  67. ):
  68. self.token = token
  69. self.url = (
  70. url.rstrip("/") if isinstance(url, str) else None
  71. ) # url must not end with a slash
  72. self.hs_token = hs_token
  73. # The full Matrix ID for this application service's sender.
  74. self.sender = sender
  75. self.namespaces = self._check_namespaces(namespaces)
  76. self.id = id
  77. self.ip_range_whitelist = ip_range_whitelist
  78. self.supports_ephemeral = supports_ephemeral
  79. self.msc3202_transaction_extensions = msc3202_transaction_extensions
  80. if "|" in self.id:
  81. raise Exception("application service ID cannot contain '|' character")
  82. # .protocols is a publicly visible field
  83. if protocols:
  84. self.protocols = set(protocols)
  85. else:
  86. self.protocols = set()
  87. self.rate_limited = rate_limited
  88. def _check_namespaces(
  89. self, namespaces: Optional[JsonDict]
  90. ) -> Dict[str, List[Namespace]]:
  91. # Sanity check that it is of the form:
  92. # {
  93. # users: [ {regex: "[A-z]+.*", exclusive: true}, ...],
  94. # aliases: [ {regex: "[A-z]+.*", exclusive: true}, ...],
  95. # rooms: [ {regex: "[A-z]+.*", exclusive: true}, ...],
  96. # }
  97. if namespaces is None:
  98. namespaces = {}
  99. result: Dict[str, List[Namespace]] = {}
  100. for ns in ApplicationService.NS_LIST:
  101. result[ns] = []
  102. if ns not in namespaces:
  103. continue
  104. if not isinstance(namespaces[ns], list):
  105. raise ValueError("Bad namespace value for '%s'" % ns)
  106. for regex_obj in namespaces[ns]:
  107. if not isinstance(regex_obj, dict):
  108. raise ValueError("Expected dict regex for ns '%s'" % ns)
  109. exclusive = regex_obj.get("exclusive")
  110. if not isinstance(exclusive, bool):
  111. raise ValueError("Expected bool for 'exclusive' in ns '%s'" % ns)
  112. regex = regex_obj.get("regex")
  113. if not isinstance(regex, str):
  114. raise ValueError("Expected string for 'regex' in ns '%s'" % ns)
  115. # Pre-compile regex.
  116. result[ns].append(Namespace(exclusive, re.compile(regex)))
  117. return result
  118. def _matches_regex(
  119. self, namespace_key: str, test_string: str
  120. ) -> Optional[Namespace]:
  121. for namespace in self.namespaces[namespace_key]:
  122. if namespace.regex.match(test_string):
  123. return namespace
  124. return None
  125. def _is_exclusive(self, namespace_key: str, test_string: str) -> bool:
  126. namespace = self._matches_regex(namespace_key, test_string)
  127. if namespace:
  128. return namespace.exclusive
  129. return False
  130. @cached(num_args=1, cache_context=True)
  131. async def _matches_user_in_member_list(
  132. self,
  133. room_id: str,
  134. store: "DataStore",
  135. cache_context: _CacheContext,
  136. ) -> bool:
  137. """Check if this service is interested a room based upon its membership
  138. Args:
  139. room_id: The room to check.
  140. store: The datastore to query.
  141. Returns:
  142. True if this service would like to know about this room.
  143. """
  144. # We can use `get_local_users_in_room(...)` here because an application service
  145. # can only be interested in local users of the server it's on (ignore any remote
  146. # users that might match the user namespace regex).
  147. #
  148. # In the future, we can consider re-using
  149. # `store.get_app_service_users_in_room` which is very similar to this
  150. # function but has a slightly worse performance than this because we
  151. # have an early escape-hatch if we find a single user that the
  152. # appservice is interested in. The juice would be worth the squeeze if
  153. # `store.get_app_service_users_in_room` was used in more places besides
  154. # an experimental MSC. But for now we can avoid doing more work and
  155. # barely using it later.
  156. local_user_ids = await store.get_local_users_in_room(
  157. room_id, on_invalidate=cache_context.invalidate
  158. )
  159. # check joined member events
  160. for user_id in local_user_ids:
  161. if self.is_interested_in_user(user_id):
  162. return True
  163. return False
  164. def is_interested_in_user(
  165. self,
  166. user_id: str,
  167. ) -> bool:
  168. """
  169. Returns whether the application is interested in a given user ID.
  170. The appservice is considered to be interested in a user if either: the
  171. user ID is in the appservice's user namespace, or if the user is the
  172. appservice's configured sender_localpart.
  173. Args:
  174. user_id: The ID of the user to check.
  175. Returns:
  176. True if the application service is interested in the user, False if not.
  177. """
  178. return (
  179. # User is the appservice's configured sender_localpart user
  180. user_id == self.sender
  181. # User is in the appservice's user namespace
  182. or self.is_user_in_namespace(user_id)
  183. )
  184. @cached(num_args=1, cache_context=True)
  185. async def is_interested_in_room(
  186. self,
  187. room_id: str,
  188. store: "DataStore",
  189. cache_context: _CacheContext,
  190. ) -> bool:
  191. """
  192. Returns whether the application service is interested in a given room ID.
  193. The appservice is considered to be interested in the room if either: the ID or one
  194. of the aliases of the room is in the appservice's room ID or alias namespace
  195. respectively, or if one of the members of the room fall into the appservice's user
  196. namespace.
  197. Args:
  198. room_id: The ID of the room to check.
  199. store: The homeserver's datastore class.
  200. Returns:
  201. True if the application service is interested in the room, False if not.
  202. """
  203. # Check if we have interest in this room ID
  204. if self.is_room_id_in_namespace(room_id):
  205. return True
  206. # likewise with the room's aliases (if it has any)
  207. alias_list = await store.get_aliases_for_room(
  208. room_id, on_invalidate=cache_context.invalidate
  209. )
  210. for alias in alias_list:
  211. if self.is_room_alias_in_namespace(alias):
  212. return True
  213. # And finally, perform an expensive check on whether any of the
  214. # users in the room match the appservice's user namespace
  215. return await self._matches_user_in_member_list(
  216. room_id, store, on_invalidate=cache_context.invalidate
  217. )
  218. @cached(num_args=1, cache_context=True)
  219. async def is_interested_in_event(
  220. self,
  221. event_id: str,
  222. event: EventBase,
  223. store: "DataStore",
  224. cache_context: _CacheContext,
  225. ) -> bool:
  226. """Check if this service is interested in this event.
  227. Args:
  228. event_id: The ID of the event to check. This is purely used for simplifying the
  229. caching of calls to this method.
  230. event: The event to check.
  231. store: The datastore to query.
  232. Returns:
  233. True if this service would like to know about this event, otherwise False.
  234. """
  235. # Check if we're interested in this event's sender by namespace (or if they're the
  236. # sender_localpart user)
  237. if self.is_interested_in_user(event.sender):
  238. return True
  239. # additionally, if this is a membership event, perform the same checks on
  240. # the user it references
  241. if event.type == EventTypes.Member and self.is_interested_in_user(
  242. event.state_key
  243. ):
  244. return True
  245. # This will check the datastore, so should be run last
  246. if await self.is_interested_in_room(
  247. event.room_id, store, on_invalidate=cache_context.invalidate
  248. ):
  249. return True
  250. return False
  251. @cached(num_args=1, cache_context=True)
  252. async def is_interested_in_presence(
  253. self, user_id: UserID, store: "DataStore", cache_context: _CacheContext
  254. ) -> bool:
  255. """Check if this service is interested a user's presence
  256. Args:
  257. user_id: The user to check.
  258. store: The datastore to query.
  259. Returns:
  260. True if this service would like to know about presence for this user.
  261. """
  262. # Find all the rooms the sender is in
  263. if self.is_interested_in_user(user_id.to_string()):
  264. return True
  265. room_ids = await store.get_rooms_for_user(
  266. user_id.to_string(), on_invalidate=cache_context.invalidate
  267. )
  268. # Then find out if the appservice is interested in any of those rooms
  269. for room_id in room_ids:
  270. if await self.is_interested_in_room(
  271. room_id, store, on_invalidate=cache_context.invalidate
  272. ):
  273. return True
  274. return False
  275. def is_user_in_namespace(self, user_id: str) -> bool:
  276. return bool(self._matches_regex(ApplicationService.NS_USERS, user_id))
  277. def is_room_alias_in_namespace(self, alias: str) -> bool:
  278. return bool(self._matches_regex(ApplicationService.NS_ALIASES, alias))
  279. def is_room_id_in_namespace(self, room_id: str) -> bool:
  280. return bool(self._matches_regex(ApplicationService.NS_ROOMS, room_id))
  281. def is_exclusive_user(self, user_id: str) -> bool:
  282. return (
  283. self._is_exclusive(ApplicationService.NS_USERS, user_id)
  284. or user_id == self.sender
  285. )
  286. def is_interested_in_protocol(self, protocol: str) -> bool:
  287. return protocol in self.protocols
  288. def is_exclusive_alias(self, alias: str) -> bool:
  289. return self._is_exclusive(ApplicationService.NS_ALIASES, alias)
  290. def is_exclusive_room(self, room_id: str) -> bool:
  291. return self._is_exclusive(ApplicationService.NS_ROOMS, room_id)
  292. def get_exclusive_user_regexes(self) -> List[Pattern[str]]:
  293. """Get the list of regexes used to determine if a user is exclusively
  294. registered by the AS
  295. """
  296. return [
  297. namespace.regex
  298. for namespace in self.namespaces[ApplicationService.NS_USERS]
  299. if namespace.exclusive
  300. ]
  301. def is_rate_limited(self) -> bool:
  302. return self.rate_limited
  303. def __str__(self) -> str:
  304. # copy dictionary and redact token fields so they don't get logged
  305. dict_copy = self.__dict__.copy()
  306. dict_copy["token"] = "<redacted>"
  307. dict_copy["hs_token"] = "<redacted>"
  308. return "ApplicationService: %s" % (dict_copy,)
  309. class AppServiceTransaction:
  310. """Represents an application service transaction."""
  311. def __init__(
  312. self,
  313. service: ApplicationService,
  314. id: int,
  315. events: Sequence[EventBase],
  316. ephemeral: List[JsonMapping],
  317. to_device_messages: List[JsonMapping],
  318. one_time_keys_count: TransactionOneTimeKeysCount,
  319. unused_fallback_keys: TransactionUnusedFallbackKeys,
  320. device_list_summary: DeviceListUpdates,
  321. ):
  322. self.service = service
  323. self.id = id
  324. self.events = events
  325. self.ephemeral = ephemeral
  326. self.to_device_messages = to_device_messages
  327. self.one_time_keys_count = one_time_keys_count
  328. self.unused_fallback_keys = unused_fallback_keys
  329. self.device_list_summary = device_list_summary
  330. async def send(self, as_api: "ApplicationServiceApi") -> bool:
  331. """Sends this transaction using the provided AS API interface.
  332. Args:
  333. as_api: The API to use to send.
  334. Returns:
  335. True if the transaction was sent.
  336. """
  337. return await as_api.push_bulk(
  338. service=self.service,
  339. events=self.events,
  340. ephemeral=self.ephemeral,
  341. to_device_messages=self.to_device_messages,
  342. one_time_keys_count=self.one_time_keys_count,
  343. unused_fallback_keys=self.unused_fallback_keys,
  344. device_list_summary=self.device_list_summary,
  345. txn_id=self.id,
  346. )
  347. async def complete(self, store: "DataStore") -> None:
  348. """Completes this transaction as successful.
  349. Marks this transaction ID on the application service and removes the
  350. transaction contents from the database.
  351. Args:
  352. store: The database store to operate on.
  353. """
  354. await store.complete_appservice_txn(service=self.service, txn_id=self.id)