test_client_ips.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 OpenMarket Ltd
  3. # Copyright 2018 New Vector Ltd
  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 twisted.internet import defer
  18. import synapse.rest.admin
  19. from synapse.http.site import XForwardedForRequest
  20. from synapse.rest.client.v1 import login
  21. from tests import unittest
  22. class ClientIpStoreTestCase(unittest.HomeserverTestCase):
  23. def make_homeserver(self, reactor, clock):
  24. hs = self.setup_test_homeserver()
  25. return hs
  26. def prepare(self, hs, reactor, clock):
  27. self.store = self.hs.get_datastore()
  28. def test_insert_new_client_ip(self):
  29. self.reactor.advance(12345678)
  30. user_id = "@user:id"
  31. self.get_success(
  32. self.store.insert_client_ip(
  33. user_id, "access_token", "ip", "user_agent", "device_id"
  34. )
  35. )
  36. # Trigger the storage loop
  37. self.reactor.advance(10)
  38. result = self.get_success(
  39. self.store.get_last_client_ip_by_device(user_id, "device_id")
  40. )
  41. r = result[(user_id, "device_id")]
  42. self.assertDictContainsSubset(
  43. {
  44. "user_id": user_id,
  45. "device_id": "device_id",
  46. "access_token": "access_token",
  47. "ip": "ip",
  48. "user_agent": "user_agent",
  49. "last_seen": 12345678000,
  50. },
  51. r,
  52. )
  53. def test_insert_new_client_ip_none_device_id(self):
  54. """
  55. An insert with a device ID of NULL will not create a new entry, but
  56. update an existing entry in the user_ips table.
  57. """
  58. self.reactor.advance(12345678)
  59. user_id = "@user:id"
  60. # Add & trigger the storage loop
  61. self.get_success(
  62. self.store.insert_client_ip(
  63. user_id, "access_token", "ip", "user_agent", None
  64. )
  65. )
  66. self.reactor.advance(200)
  67. self.pump(0)
  68. result = self.get_success(
  69. self.store._simple_select_list(
  70. table="user_ips",
  71. keyvalues={"user_id": user_id},
  72. retcols=["access_token", "ip", "user_agent", "device_id", "last_seen"],
  73. desc="get_user_ip_and_agents",
  74. )
  75. )
  76. self.assertEqual(
  77. result,
  78. [
  79. {
  80. "access_token": "access_token",
  81. "ip": "ip",
  82. "user_agent": "user_agent",
  83. "device_id": None,
  84. "last_seen": 12345678000,
  85. }
  86. ],
  87. )
  88. # Add another & trigger the storage loop
  89. self.get_success(
  90. self.store.insert_client_ip(
  91. user_id, "access_token", "ip", "user_agent", None
  92. )
  93. )
  94. self.reactor.advance(10)
  95. self.pump(0)
  96. result = self.get_success(
  97. self.store._simple_select_list(
  98. table="user_ips",
  99. keyvalues={"user_id": user_id},
  100. retcols=["access_token", "ip", "user_agent", "device_id", "last_seen"],
  101. desc="get_user_ip_and_agents",
  102. )
  103. )
  104. # Only one result, has been upserted.
  105. self.assertEqual(
  106. result,
  107. [
  108. {
  109. "access_token": "access_token",
  110. "ip": "ip",
  111. "user_agent": "user_agent",
  112. "device_id": None,
  113. "last_seen": 12345878000,
  114. }
  115. ],
  116. )
  117. def test_disabled_monthly_active_user(self):
  118. self.hs.config.limit_usage_by_mau = False
  119. self.hs.config.max_mau_value = 50
  120. user_id = "@user:server"
  121. self.get_success(
  122. self.store.insert_client_ip(
  123. user_id, "access_token", "ip", "user_agent", "device_id"
  124. )
  125. )
  126. active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
  127. self.assertFalse(active)
  128. def test_adding_monthly_active_user_when_full(self):
  129. self.hs.config.limit_usage_by_mau = True
  130. self.hs.config.max_mau_value = 50
  131. lots_of_users = 100
  132. user_id = "@user:server"
  133. self.store.get_monthly_active_count = Mock(
  134. return_value=defer.succeed(lots_of_users)
  135. )
  136. self.get_success(
  137. self.store.insert_client_ip(
  138. user_id, "access_token", "ip", "user_agent", "device_id"
  139. )
  140. )
  141. active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
  142. self.assertFalse(active)
  143. def test_adding_monthly_active_user_when_space(self):
  144. self.hs.config.limit_usage_by_mau = True
  145. self.hs.config.max_mau_value = 50
  146. user_id = "@user:server"
  147. active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
  148. self.assertFalse(active)
  149. # Trigger the saving loop
  150. self.reactor.advance(10)
  151. self.get_success(
  152. self.store.insert_client_ip(
  153. user_id, "access_token", "ip", "user_agent", "device_id"
  154. )
  155. )
  156. active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
  157. self.assertTrue(active)
  158. def test_updating_monthly_active_user_when_space(self):
  159. self.hs.config.limit_usage_by_mau = True
  160. self.hs.config.max_mau_value = 50
  161. user_id = "@user:server"
  162. self.get_success(self.store.register_user(user_id=user_id, password_hash=None))
  163. active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
  164. self.assertFalse(active)
  165. # Trigger the saving loop
  166. self.reactor.advance(10)
  167. self.get_success(
  168. self.store.insert_client_ip(
  169. user_id, "access_token", "ip", "user_agent", "device_id"
  170. )
  171. )
  172. active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
  173. self.assertTrue(active)
  174. class ClientIpAuthTestCase(unittest.HomeserverTestCase):
  175. servlets = [
  176. synapse.rest.admin.register_servlets_for_client_rest_resource,
  177. login.register_servlets,
  178. ]
  179. def make_homeserver(self, reactor, clock):
  180. hs = self.setup_test_homeserver()
  181. return hs
  182. def prepare(self, hs, reactor, clock):
  183. self.store = self.hs.get_datastore()
  184. self.user_id = self.register_user("bob", "abc123", True)
  185. def test_request_with_xforwarded(self):
  186. """
  187. The IP in X-Forwarded-For is entered into the client IPs table.
  188. """
  189. self._runtest(
  190. {b"X-Forwarded-For": b"127.9.0.1"},
  191. "127.9.0.1",
  192. {"request": XForwardedForRequest},
  193. )
  194. def test_request_from_getPeer(self):
  195. """
  196. The IP returned by getPeer is entered into the client IPs table, if
  197. there's no X-Forwarded-For header.
  198. """
  199. self._runtest({}, "127.0.0.1", {})
  200. def _runtest(self, headers, expected_ip, make_request_args):
  201. device_id = "bleb"
  202. access_token = self.login("bob", "abc123", device_id=device_id)
  203. # Advance to a known time
  204. self.reactor.advance(123456 - self.reactor.seconds())
  205. request, channel = self.make_request(
  206. "GET",
  207. "/_matrix/client/r0/admin/users/" + self.user_id,
  208. access_token=access_token,
  209. **make_request_args
  210. )
  211. request.requestHeaders.addRawHeader(b"User-Agent", b"Mozzila pizza")
  212. # Add the optional headers
  213. for h, v in headers.items():
  214. request.requestHeaders.addRawHeader(h, v)
  215. self.render(request)
  216. # Advance so the save loop occurs
  217. self.reactor.advance(100)
  218. result = self.get_success(
  219. self.store.get_last_client_ip_by_device(self.user_id, device_id)
  220. )
  221. r = result[(self.user_id, device_id)]
  222. self.assertDictContainsSubset(
  223. {
  224. "user_id": self.user_id,
  225. "device_id": device_id,
  226. "ip": expected_ip,
  227. "user_agent": "Mozzila pizza",
  228. "last_seen": 123456100,
  229. },
  230. r,
  231. )