test_client_ips.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. device_id = "MY_DEVICE"
  32. # Insert a user IP
  33. self.get_success(self.store.store_device(user_id, device_id, "display name",))
  34. self.get_success(
  35. self.store.insert_client_ip(
  36. user_id, "access_token", "ip", "user_agent", device_id
  37. )
  38. )
  39. # Trigger the storage loop
  40. self.reactor.advance(10)
  41. result = self.get_success(
  42. self.store.get_last_client_ip_by_device(user_id, device_id)
  43. )
  44. r = result[(user_id, device_id)]
  45. self.assertDictContainsSubset(
  46. {
  47. "user_id": user_id,
  48. "device_id": device_id,
  49. "ip": "ip",
  50. "user_agent": "user_agent",
  51. "last_seen": 12345678000,
  52. },
  53. r,
  54. )
  55. def test_insert_new_client_ip_none_device_id(self):
  56. """
  57. An insert with a device ID of NULL will not create a new entry, but
  58. update an existing entry in the user_ips table.
  59. """
  60. self.reactor.advance(12345678)
  61. user_id = "@user:id"
  62. # Add & trigger the storage loop
  63. self.get_success(
  64. self.store.insert_client_ip(
  65. user_id, "access_token", "ip", "user_agent", None
  66. )
  67. )
  68. self.reactor.advance(200)
  69. self.pump(0)
  70. result = self.get_success(
  71. self.store.db.simple_select_list(
  72. table="user_ips",
  73. keyvalues={"user_id": user_id},
  74. retcols=["access_token", "ip", "user_agent", "device_id", "last_seen"],
  75. desc="get_user_ip_and_agents",
  76. )
  77. )
  78. self.assertEqual(
  79. result,
  80. [
  81. {
  82. "access_token": "access_token",
  83. "ip": "ip",
  84. "user_agent": "user_agent",
  85. "device_id": None,
  86. "last_seen": 12345678000,
  87. }
  88. ],
  89. )
  90. # Add another & trigger the storage loop
  91. self.get_success(
  92. self.store.insert_client_ip(
  93. user_id, "access_token", "ip", "user_agent", None
  94. )
  95. )
  96. self.reactor.advance(10)
  97. self.pump(0)
  98. result = self.get_success(
  99. self.store.db.simple_select_list(
  100. table="user_ips",
  101. keyvalues={"user_id": user_id},
  102. retcols=["access_token", "ip", "user_agent", "device_id", "last_seen"],
  103. desc="get_user_ip_and_agents",
  104. )
  105. )
  106. # Only one result, has been upserted.
  107. self.assertEqual(
  108. result,
  109. [
  110. {
  111. "access_token": "access_token",
  112. "ip": "ip",
  113. "user_agent": "user_agent",
  114. "device_id": None,
  115. "last_seen": 12345878000,
  116. }
  117. ],
  118. )
  119. def test_disabled_monthly_active_user(self):
  120. self.hs.config.limit_usage_by_mau = False
  121. self.hs.config.max_mau_value = 50
  122. user_id = "@user:server"
  123. self.get_success(
  124. self.store.insert_client_ip(
  125. user_id, "access_token", "ip", "user_agent", "device_id"
  126. )
  127. )
  128. active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
  129. self.assertFalse(active)
  130. def test_adding_monthly_active_user_when_full(self):
  131. self.hs.config.limit_usage_by_mau = True
  132. self.hs.config.max_mau_value = 50
  133. lots_of_users = 100
  134. user_id = "@user:server"
  135. self.store.get_monthly_active_count = Mock(
  136. return_value=defer.succeed(lots_of_users)
  137. )
  138. self.get_success(
  139. self.store.insert_client_ip(
  140. user_id, "access_token", "ip", "user_agent", "device_id"
  141. )
  142. )
  143. active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
  144. self.assertFalse(active)
  145. def test_adding_monthly_active_user_when_space(self):
  146. self.hs.config.limit_usage_by_mau = True
  147. self.hs.config.max_mau_value = 50
  148. user_id = "@user:server"
  149. active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
  150. self.assertFalse(active)
  151. # Trigger the saving loop
  152. self.reactor.advance(10)
  153. self.get_success(
  154. self.store.insert_client_ip(
  155. user_id, "access_token", "ip", "user_agent", "device_id"
  156. )
  157. )
  158. active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
  159. self.assertTrue(active)
  160. def test_updating_monthly_active_user_when_space(self):
  161. self.hs.config.limit_usage_by_mau = True
  162. self.hs.config.max_mau_value = 50
  163. user_id = "@user:server"
  164. self.get_success(self.store.register_user(user_id=user_id, password_hash=None))
  165. active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
  166. self.assertFalse(active)
  167. # Trigger the saving loop
  168. self.reactor.advance(10)
  169. self.get_success(
  170. self.store.insert_client_ip(
  171. user_id, "access_token", "ip", "user_agent", "device_id"
  172. )
  173. )
  174. active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
  175. self.assertTrue(active)
  176. def test_devices_last_seen_bg_update(self):
  177. # First make sure we have completed all updates.
  178. while not self.get_success(
  179. self.store.db.updates.has_completed_background_updates()
  180. ):
  181. self.get_success(
  182. self.store.db.updates.do_next_background_update(100), by=0.1
  183. )
  184. user_id = "@user:id"
  185. device_id = "MY_DEVICE"
  186. # Insert a user IP
  187. self.get_success(self.store.store_device(user_id, device_id, "display name",))
  188. self.get_success(
  189. self.store.insert_client_ip(
  190. user_id, "access_token", "ip", "user_agent", device_id
  191. )
  192. )
  193. # Force persisting to disk
  194. self.reactor.advance(200)
  195. # But clear the associated entry in devices table
  196. self.get_success(
  197. self.store.db.simple_update(
  198. table="devices",
  199. keyvalues={"user_id": user_id, "device_id": device_id},
  200. updatevalues={"last_seen": None, "ip": None, "user_agent": None},
  201. desc="test_devices_last_seen_bg_update",
  202. )
  203. )
  204. # We should now get nulls when querying
  205. result = self.get_success(
  206. self.store.get_last_client_ip_by_device(user_id, device_id)
  207. )
  208. r = result[(user_id, device_id)]
  209. self.assertDictContainsSubset(
  210. {
  211. "user_id": user_id,
  212. "device_id": device_id,
  213. "ip": None,
  214. "user_agent": None,
  215. "last_seen": None,
  216. },
  217. r,
  218. )
  219. # Register the background update to run again.
  220. self.get_success(
  221. self.store.db.simple_insert(
  222. table="background_updates",
  223. values={
  224. "update_name": "devices_last_seen",
  225. "progress_json": "{}",
  226. "depends_on": None,
  227. },
  228. )
  229. )
  230. # ... and tell the DataStore that it hasn't finished all updates yet
  231. self.store.db.updates._all_done = False
  232. # Now let's actually drive the updates to completion
  233. while not self.get_success(
  234. self.store.db.updates.has_completed_background_updates()
  235. ):
  236. self.get_success(
  237. self.store.db.updates.do_next_background_update(100), by=0.1
  238. )
  239. # We should now get the correct result again
  240. result = self.get_success(
  241. self.store.get_last_client_ip_by_device(user_id, device_id)
  242. )
  243. r = result[(user_id, device_id)]
  244. self.assertDictContainsSubset(
  245. {
  246. "user_id": user_id,
  247. "device_id": device_id,
  248. "ip": "ip",
  249. "user_agent": "user_agent",
  250. "last_seen": 0,
  251. },
  252. r,
  253. )
  254. def test_old_user_ips_pruned(self):
  255. # First make sure we have completed all updates.
  256. while not self.get_success(
  257. self.store.db.updates.has_completed_background_updates()
  258. ):
  259. self.get_success(
  260. self.store.db.updates.do_next_background_update(100), by=0.1
  261. )
  262. user_id = "@user:id"
  263. device_id = "MY_DEVICE"
  264. # Insert a user IP
  265. self.get_success(self.store.store_device(user_id, device_id, "display name",))
  266. self.get_success(
  267. self.store.insert_client_ip(
  268. user_id, "access_token", "ip", "user_agent", device_id
  269. )
  270. )
  271. # Force persisting to disk
  272. self.reactor.advance(200)
  273. # We should see that in the DB
  274. result = self.get_success(
  275. self.store.db.simple_select_list(
  276. table="user_ips",
  277. keyvalues={"user_id": user_id},
  278. retcols=["access_token", "ip", "user_agent", "device_id", "last_seen"],
  279. desc="get_user_ip_and_agents",
  280. )
  281. )
  282. self.assertEqual(
  283. result,
  284. [
  285. {
  286. "access_token": "access_token",
  287. "ip": "ip",
  288. "user_agent": "user_agent",
  289. "device_id": device_id,
  290. "last_seen": 0,
  291. }
  292. ],
  293. )
  294. # Now advance by a couple of months
  295. self.reactor.advance(60 * 24 * 60 * 60)
  296. # We should get no results.
  297. result = self.get_success(
  298. self.store.db.simple_select_list(
  299. table="user_ips",
  300. keyvalues={"user_id": user_id},
  301. retcols=["access_token", "ip", "user_agent", "device_id", "last_seen"],
  302. desc="get_user_ip_and_agents",
  303. )
  304. )
  305. self.assertEqual(result, [])
  306. # But we should still get the correct values for the device
  307. result = self.get_success(
  308. self.store.get_last_client_ip_by_device(user_id, device_id)
  309. )
  310. r = result[(user_id, device_id)]
  311. self.assertDictContainsSubset(
  312. {
  313. "user_id": user_id,
  314. "device_id": device_id,
  315. "ip": "ip",
  316. "user_agent": "user_agent",
  317. "last_seen": 0,
  318. },
  319. r,
  320. )
  321. class ClientIpAuthTestCase(unittest.HomeserverTestCase):
  322. servlets = [
  323. synapse.rest.admin.register_servlets_for_client_rest_resource,
  324. login.register_servlets,
  325. ]
  326. def make_homeserver(self, reactor, clock):
  327. hs = self.setup_test_homeserver()
  328. return hs
  329. def prepare(self, hs, reactor, clock):
  330. self.store = self.hs.get_datastore()
  331. self.user_id = self.register_user("bob", "abc123", True)
  332. def test_request_with_xforwarded(self):
  333. """
  334. The IP in X-Forwarded-For is entered into the client IPs table.
  335. """
  336. self._runtest(
  337. {b"X-Forwarded-For": b"127.9.0.1"},
  338. "127.9.0.1",
  339. {"request": XForwardedForRequest},
  340. )
  341. def test_request_from_getPeer(self):
  342. """
  343. The IP returned by getPeer is entered into the client IPs table, if
  344. there's no X-Forwarded-For header.
  345. """
  346. self._runtest({}, "127.0.0.1", {})
  347. def _runtest(self, headers, expected_ip, make_request_args):
  348. device_id = "bleb"
  349. access_token = self.login("bob", "abc123", device_id=device_id)
  350. # Advance to a known time
  351. self.reactor.advance(123456 - self.reactor.seconds())
  352. request, channel = self.make_request(
  353. "GET",
  354. "/_matrix/client/r0/admin/users/" + self.user_id,
  355. access_token=access_token,
  356. **make_request_args
  357. )
  358. request.requestHeaders.addRawHeader(b"User-Agent", b"Mozzila pizza")
  359. # Add the optional headers
  360. for h, v in headers.items():
  361. request.requestHeaders.addRawHeader(h, v)
  362. self.render(request)
  363. # Advance so the save loop occurs
  364. self.reactor.advance(100)
  365. result = self.get_success(
  366. self.store.get_last_client_ip_by_device(self.user_id, device_id)
  367. )
  368. r = result[(self.user_id, device_id)]
  369. self.assertDictContainsSubset(
  370. {
  371. "user_id": self.user_id,
  372. "device_id": device_id,
  373. "ip": expected_ip,
  374. "user_agent": "Mozzila pizza",
  375. "last_seen": 123456100,
  376. },
  377. r,
  378. )