2016-07-20 11:34:00 -04:00
|
|
|
# Copyright 2016 OpenMarket Ltd
|
2018-09-20 06:14:34 -04:00
|
|
|
# Copyright 2018 New Vector Ltd
|
2016-07-20 11:34:00 -04:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2018-09-20 06:14:34 -04:00
|
|
|
|
2023-10-26 13:01:36 -04:00
|
|
|
from typing import Any, Dict, List, Optional, Tuple, cast
|
2023-08-24 19:38:46 -04:00
|
|
|
from unittest.mock import AsyncMock
|
2016-07-20 11:34:00 -04:00
|
|
|
|
2021-10-01 12:22:13 -04:00
|
|
|
from parameterized import parameterized
|
|
|
|
|
2022-12-09 12:36:32 -05:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
|
|
|
|
2019-05-01 10:32:38 -04:00
|
|
|
import synapse.rest.admin
|
2018-09-20 06:14:34 -04:00
|
|
|
from synapse.http.site import XForwardedForRequest
|
2021-08-17 07:57:58 -04:00
|
|
|
from synapse.rest.client import login
|
2022-12-09 12:36:32 -05:00
|
|
|
from synapse.server import HomeServer
|
2023-10-17 08:47:42 -04:00
|
|
|
from synapse.storage.databases.main.client_ips import (
|
|
|
|
LAST_SEEN_GRANULARITY,
|
|
|
|
DeviceLastConnectionInfo,
|
|
|
|
)
|
2021-10-01 12:22:13 -04:00
|
|
|
from synapse.types import UserID
|
2022-12-09 12:36:32 -05:00
|
|
|
from synapse.util import Clock
|
2018-09-20 06:14:34 -04:00
|
|
|
|
|
|
|
from tests import unittest
|
2020-11-16 09:45:22 -05:00
|
|
|
from tests.server import make_request
|
2020-05-22 16:47:07 -04:00
|
|
|
from tests.unittest import override_config
|
2016-07-20 11:34:00 -04:00
|
|
|
|
|
|
|
|
2018-09-20 06:14:34 -04:00
|
|
|
class ClientIpStoreTestCase(unittest.HomeserverTestCase):
|
2022-12-09 12:36:32 -05:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
|
|
|
self.store = hs.get_datastores().main
|
2016-07-20 11:34:00 -04:00
|
|
|
|
2022-12-09 12:36:32 -05:00
|
|
|
def test_insert_new_client_ip(self) -> None:
|
2018-09-20 06:14:34 -04:00
|
|
|
self.reactor.advance(12345678)
|
|
|
|
|
2016-07-20 11:34:00 -04:00
|
|
|
user_id = "@user:id"
|
2019-12-10 11:22:29 -05:00
|
|
|
device_id = "MY_DEVICE"
|
|
|
|
|
|
|
|
# Insert a user IP
|
|
|
|
self.get_success(
|
|
|
|
self.store.store_device(
|
|
|
|
user_id,
|
|
|
|
device_id,
|
|
|
|
"display name",
|
|
|
|
)
|
2021-02-16 17:32:34 -05:00
|
|
|
)
|
2018-09-20 06:14:34 -04:00
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
2019-12-10 11:22:29 -05:00
|
|
|
user_id, "access_token", "ip", "user_agent", device_id
|
2018-09-20 06:14:34 -04:00
|
|
|
)
|
2016-07-20 11:34:00 -04:00
|
|
|
)
|
|
|
|
|
2018-09-20 06:14:34 -04:00
|
|
|
# Trigger the storage loop
|
|
|
|
self.reactor.advance(10)
|
|
|
|
|
|
|
|
result = self.get_success(
|
2019-12-10 11:22:29 -05:00
|
|
|
self.store.get_last_client_ip_by_device(user_id, device_id)
|
2018-09-20 06:14:34 -04:00
|
|
|
)
|
2016-07-20 11:34:00 -04:00
|
|
|
|
2019-12-10 11:22:29 -05:00
|
|
|
r = result[(user_id, device_id)]
|
2023-10-17 08:47:42 -04:00
|
|
|
self.assertEqual(
|
|
|
|
DeviceLastConnectionInfo(
|
|
|
|
user_id=user_id,
|
|
|
|
device_id=device_id,
|
|
|
|
ip="ip",
|
|
|
|
user_agent="user_agent",
|
|
|
|
last_seen=12345678000,
|
|
|
|
),
|
|
|
|
r,
|
2016-07-20 11:34:00 -04:00
|
|
|
)
|
2018-08-02 08:47:19 -04:00
|
|
|
|
2022-12-09 12:36:32 -05:00
|
|
|
def test_insert_new_client_ip_none_device_id(self) -> None:
|
2019-01-09 06:26:25 -05:00
|
|
|
"""
|
|
|
|
An insert with a device ID of NULL will not create a new entry, but
|
|
|
|
update an existing entry in the user_ips table.
|
|
|
|
"""
|
|
|
|
self.reactor.advance(12345678)
|
|
|
|
|
|
|
|
user_id = "@user:id"
|
|
|
|
|
|
|
|
# Add & trigger the storage loop
|
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
|
|
|
user_id, "access_token", "ip", "user_agent", None
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.reactor.advance(200)
|
|
|
|
self.pump(0)
|
|
|
|
|
2023-10-26 13:01:36 -04:00
|
|
|
result = cast(
|
|
|
|
List[Tuple[str, str, str, Optional[str], int]],
|
|
|
|
self.get_success(
|
|
|
|
self.store.db_pool.simple_select_list(
|
|
|
|
table="user_ips",
|
|
|
|
keyvalues={"user_id": user_id},
|
|
|
|
retcols=[
|
|
|
|
"access_token",
|
|
|
|
"ip",
|
|
|
|
"user_agent",
|
|
|
|
"device_id",
|
|
|
|
"last_seen",
|
|
|
|
],
|
|
|
|
desc="get_user_ip_and_agents",
|
|
|
|
)
|
|
|
|
),
|
2019-01-09 06:26:25 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
2023-10-26 13:01:36 -04:00
|
|
|
result, [("access_token", "ip", "user_agent", None, 12345678000)]
|
2019-01-09 06:26:25 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
# Add another & trigger the storage loop
|
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
|
|
|
user_id, "access_token", "ip", "user_agent", None
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.reactor.advance(10)
|
|
|
|
self.pump(0)
|
|
|
|
|
2023-10-26 13:01:36 -04:00
|
|
|
result = cast(
|
|
|
|
List[Tuple[str, str, str, Optional[str], int]],
|
|
|
|
self.get_success(
|
|
|
|
self.store.db_pool.simple_select_list(
|
|
|
|
table="user_ips",
|
|
|
|
keyvalues={"user_id": user_id},
|
|
|
|
retcols=[
|
|
|
|
"access_token",
|
|
|
|
"ip",
|
|
|
|
"user_agent",
|
|
|
|
"device_id",
|
|
|
|
"last_seen",
|
|
|
|
],
|
|
|
|
desc="get_user_ip_and_agents",
|
|
|
|
)
|
|
|
|
),
|
2019-01-09 06:26:25 -05:00
|
|
|
)
|
|
|
|
# Only one result, has been upserted.
|
|
|
|
self.assertEqual(
|
2023-10-26 13:01:36 -04:00
|
|
|
result, [("access_token", "ip", "user_agent", None, 12345878000)]
|
2019-01-09 06:26:25 -05:00
|
|
|
)
|
|
|
|
|
2021-10-12 06:24:05 -04:00
|
|
|
@parameterized.expand([(False,), (True,)])
|
2022-12-09 12:36:32 -05:00
|
|
|
def test_get_last_client_ip_by_device(self, after_persisting: bool) -> None:
|
2021-10-12 06:24:05 -04:00
|
|
|
"""Test `get_last_client_ip_by_device` for persisted and unpersisted data"""
|
|
|
|
self.reactor.advance(12345678)
|
|
|
|
|
|
|
|
user_id = "@user:id"
|
|
|
|
device_id = "MY_DEVICE"
|
|
|
|
|
|
|
|
# Insert a user IP
|
|
|
|
self.get_success(
|
|
|
|
self.store.store_device(
|
|
|
|
user_id,
|
|
|
|
device_id,
|
|
|
|
"display name",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
|
|
|
user_id, "access_token", "ip", "user_agent", device_id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if after_persisting:
|
|
|
|
# Trigger the storage loop
|
|
|
|
self.reactor.advance(10)
|
2021-11-01 07:20:54 -04:00
|
|
|
else:
|
|
|
|
# Check that the new IP and user agent has not been stored yet
|
2023-10-26 13:01:36 -04:00
|
|
|
db_result = cast(
|
|
|
|
List[Tuple[str, Optional[str], Optional[str], str, Optional[int]]],
|
|
|
|
self.get_success(
|
|
|
|
self.store.db_pool.simple_select_list(
|
|
|
|
table="devices",
|
|
|
|
keyvalues={},
|
|
|
|
retcols=(
|
|
|
|
"user_id",
|
|
|
|
"ip",
|
|
|
|
"user_agent",
|
|
|
|
"device_id",
|
|
|
|
"last_seen",
|
|
|
|
),
|
|
|
|
),
|
2021-11-01 07:20:54 -04:00
|
|
|
),
|
|
|
|
)
|
2023-10-26 13:01:36 -04:00
|
|
|
self.assertEqual(db_result, [(user_id, None, None, device_id, None)])
|
2021-10-12 06:24:05 -04:00
|
|
|
|
|
|
|
result = self.get_success(
|
|
|
|
self.store.get_last_client_ip_by_device(user_id, device_id)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
result,
|
|
|
|
{
|
2023-10-17 08:47:42 -04:00
|
|
|
(user_id, device_id): DeviceLastConnectionInfo(
|
|
|
|
user_id=user_id,
|
|
|
|
device_id=device_id,
|
|
|
|
ip="ip",
|
|
|
|
user_agent="user_agent",
|
|
|
|
last_seen=12345678000,
|
|
|
|
),
|
2021-10-12 06:24:05 -04:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-12-09 12:36:32 -05:00
|
|
|
def test_get_last_client_ip_by_device_combined_data(self) -> None:
|
2021-11-01 07:20:54 -04:00
|
|
|
"""Test that `get_last_client_ip_by_device` combines persisted and unpersisted
|
|
|
|
data together correctly
|
|
|
|
"""
|
|
|
|
self.reactor.advance(12345678)
|
|
|
|
|
|
|
|
user_id = "@user:id"
|
|
|
|
device_id_1 = "MY_DEVICE_1"
|
|
|
|
device_id_2 = "MY_DEVICE_2"
|
|
|
|
|
|
|
|
# Insert user IPs
|
|
|
|
self.get_success(
|
|
|
|
self.store.store_device(
|
|
|
|
user_id,
|
|
|
|
device_id_1,
|
|
|
|
"display name",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.get_success(
|
|
|
|
self.store.store_device(
|
|
|
|
user_id,
|
|
|
|
device_id_2,
|
|
|
|
"display name",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
|
|
|
user_id, "access_token_1", "ip_1", "user_agent_1", device_id_1
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
|
|
|
user_id, "access_token_2", "ip_2", "user_agent_2", device_id_2
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Trigger the storage loop and wait for the rate limiting period to be over
|
|
|
|
self.reactor.advance(10 + LAST_SEEN_GRANULARITY / 1000)
|
|
|
|
|
|
|
|
# Update the user agent for the second device, without running the storage loop
|
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
|
|
|
user_id, "access_token_2", "ip_2", "user_agent_3", device_id_2
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check that the new IP and user agent has not been stored yet
|
2023-10-26 13:01:36 -04:00
|
|
|
db_result = cast(
|
|
|
|
List[Tuple[str, Optional[str], Optional[str], str, Optional[int]]],
|
|
|
|
self.get_success(
|
|
|
|
self.store.db_pool.simple_select_list(
|
|
|
|
table="devices",
|
|
|
|
keyvalues={},
|
|
|
|
retcols=("user_id", "ip", "user_agent", "device_id", "last_seen"),
|
|
|
|
),
|
2021-11-01 07:20:54 -04:00
|
|
|
),
|
|
|
|
)
|
|
|
|
self.assertCountEqual(
|
|
|
|
db_result,
|
|
|
|
[
|
2023-10-26 13:01:36 -04:00
|
|
|
(user_id, "ip_1", "user_agent_1", device_id_1, 12345678000),
|
|
|
|
(user_id, "ip_2", "user_agent_2", device_id_2, 12345678000),
|
2021-11-01 07:20:54 -04:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check that data from the database and memory are combined together correctly
|
|
|
|
result = self.get_success(
|
|
|
|
self.store.get_last_client_ip_by_device(user_id, None)
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
result,
|
|
|
|
{
|
2023-10-17 08:47:42 -04:00
|
|
|
(user_id, device_id_1): DeviceLastConnectionInfo(
|
|
|
|
user_id=user_id,
|
|
|
|
device_id=device_id_1,
|
|
|
|
ip="ip_1",
|
|
|
|
user_agent="user_agent_1",
|
|
|
|
last_seen=12345678000,
|
|
|
|
),
|
|
|
|
(user_id, device_id_2): DeviceLastConnectionInfo(
|
|
|
|
user_id=user_id,
|
|
|
|
device_id=device_id_2,
|
|
|
|
ip="ip_2",
|
|
|
|
user_agent="user_agent_3",
|
|
|
|
last_seen=12345688000 + LAST_SEEN_GRANULARITY,
|
|
|
|
),
|
2021-11-01 07:20:54 -04:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-10-01 12:22:13 -04:00
|
|
|
@parameterized.expand([(False,), (True,)])
|
2022-12-09 12:36:32 -05:00
|
|
|
def test_get_user_ip_and_agents(self, after_persisting: bool) -> None:
|
2021-10-01 12:22:13 -04:00
|
|
|
"""Test `get_user_ip_and_agents` for persisted and unpersisted data"""
|
|
|
|
self.reactor.advance(12345678)
|
|
|
|
|
|
|
|
user_id = "@user:id"
|
|
|
|
user = UserID.from_string(user_id)
|
|
|
|
|
|
|
|
# Insert a user IP
|
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
|
|
|
user_id, "access_token", "ip", "user_agent", "MY_DEVICE"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if after_persisting:
|
|
|
|
# Trigger the storage loop
|
|
|
|
self.reactor.advance(10)
|
2021-11-01 07:20:54 -04:00
|
|
|
else:
|
|
|
|
# Check that the new IP and user agent has not been stored yet
|
|
|
|
db_result = self.get_success(
|
|
|
|
self.store.db_pool.simple_select_list(
|
|
|
|
table="user_ips",
|
|
|
|
keyvalues={},
|
|
|
|
retcols=("access_token", "ip", "user_agent", "last_seen"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
self.assertEqual(db_result, [])
|
2021-10-01 12:22:13 -04:00
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
self.get_success(self.store.get_user_ip_and_agents(user)),
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"access_token": "access_token",
|
|
|
|
"ip": "ip",
|
|
|
|
"user_agent": "user_agent",
|
|
|
|
"last_seen": 12345678000,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2022-12-09 12:36:32 -05:00
|
|
|
def test_get_user_ip_and_agents_combined_data(self) -> None:
|
2021-11-01 07:20:54 -04:00
|
|
|
"""Test that `get_user_ip_and_agents` combines persisted and unpersisted data
|
|
|
|
together correctly
|
|
|
|
"""
|
|
|
|
self.reactor.advance(12345678)
|
|
|
|
|
|
|
|
user_id = "@user:id"
|
|
|
|
user = UserID.from_string(user_id)
|
|
|
|
|
|
|
|
# Insert user IPs
|
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
|
|
|
user_id, "access_token", "ip_1", "user_agent_1", "MY_DEVICE_1"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
|
|
|
user_id, "access_token", "ip_2", "user_agent_2", "MY_DEVICE_2"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Trigger the storage loop and wait for the rate limiting period to be over
|
|
|
|
self.reactor.advance(10 + LAST_SEEN_GRANULARITY / 1000)
|
|
|
|
|
|
|
|
# Update the user agent for the second device, without running the storage loop
|
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
|
|
|
user_id, "access_token", "ip_2", "user_agent_3", "MY_DEVICE_2"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check that the new IP and user agent has not been stored yet
|
2023-10-26 13:01:36 -04:00
|
|
|
db_result = cast(
|
|
|
|
List[Tuple[str, str, str, int]],
|
|
|
|
self.get_success(
|
|
|
|
self.store.db_pool.simple_select_list(
|
|
|
|
table="user_ips",
|
|
|
|
keyvalues={},
|
|
|
|
retcols=("access_token", "ip", "user_agent", "last_seen"),
|
|
|
|
),
|
2021-11-01 07:20:54 -04:00
|
|
|
),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
db_result,
|
|
|
|
[
|
2023-10-26 13:01:36 -04:00
|
|
|
("access_token", "ip_1", "user_agent_1", 12345678000),
|
|
|
|
("access_token", "ip_2", "user_agent_2", 12345678000),
|
2021-11-01 07:20:54 -04:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check that data from the database and memory are combined together correctly
|
|
|
|
self.assertCountEqual(
|
|
|
|
self.get_success(self.store.get_user_ip_and_agents(user)),
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"access_token": "access_token",
|
|
|
|
"ip": "ip_1",
|
|
|
|
"user_agent": "user_agent_1",
|
|
|
|
"last_seen": 12345678000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"access_token": "access_token",
|
|
|
|
"ip": "ip_2",
|
|
|
|
"user_agent": "user_agent_3",
|
|
|
|
"last_seen": 12345688000 + LAST_SEEN_GRANULARITY,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2020-05-22 16:47:07 -04:00
|
|
|
@override_config({"limit_usage_by_mau": False, "max_mau_value": 50})
|
2022-12-09 12:36:32 -05:00
|
|
|
def test_disabled_monthly_active_user(self) -> None:
|
2018-08-02 08:47:19 -04:00
|
|
|
user_id = "@user:server"
|
2018-09-20 06:14:34 -04:00
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
|
|
|
user_id, "access_token", "ip", "user_agent", "device_id"
|
|
|
|
)
|
2018-08-02 08:47:19 -04:00
|
|
|
)
|
2018-09-20 06:14:34 -04:00
|
|
|
active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
|
2018-08-02 08:47:19 -04:00
|
|
|
self.assertFalse(active)
|
|
|
|
|
2020-05-22 16:47:07 -04:00
|
|
|
@override_config({"limit_usage_by_mau": True, "max_mau_value": 50})
|
2022-12-09 12:36:32 -05:00
|
|
|
def test_adding_monthly_active_user_when_full(self) -> None:
|
2018-08-02 08:47:19 -04:00
|
|
|
lots_of_users = 100
|
|
|
|
user_id = "@user:server"
|
|
|
|
|
2023-08-24 19:38:46 -04:00
|
|
|
self.store.get_monthly_active_count = AsyncMock(return_value=lots_of_users)
|
2018-09-20 06:14:34 -04:00
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
|
|
|
user_id, "access_token", "ip", "user_agent", "device_id"
|
|
|
|
)
|
2018-08-02 08:47:19 -04:00
|
|
|
)
|
2018-09-20 06:14:34 -04:00
|
|
|
active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
|
2018-08-02 08:47:19 -04:00
|
|
|
self.assertFalse(active)
|
|
|
|
|
2020-05-22 16:47:07 -04:00
|
|
|
@override_config({"limit_usage_by_mau": True, "max_mau_value": 50})
|
2022-12-09 12:36:32 -05:00
|
|
|
def test_adding_monthly_active_user_when_space(self) -> None:
|
2018-08-02 08:47:19 -04:00
|
|
|
user_id = "@user:server"
|
2018-09-20 06:14:34 -04:00
|
|
|
active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
|
2018-08-02 08:47:19 -04:00
|
|
|
self.assertFalse(active)
|
|
|
|
|
2018-09-20 06:14:34 -04:00
|
|
|
# Trigger the saving loop
|
|
|
|
self.reactor.advance(10)
|
|
|
|
|
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
|
|
|
user_id, "access_token", "ip", "user_agent", "device_id"
|
|
|
|
)
|
2018-08-02 08:47:19 -04:00
|
|
|
)
|
2018-09-20 06:14:34 -04:00
|
|
|
active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
|
2018-08-02 08:47:19 -04:00
|
|
|
self.assertTrue(active)
|
|
|
|
|
2020-05-22 16:47:07 -04:00
|
|
|
@override_config({"limit_usage_by_mau": True, "max_mau_value": 50})
|
2022-12-09 12:36:32 -05:00
|
|
|
def test_updating_monthly_active_user_when_space(self) -> None:
|
2018-08-02 08:47:19 -04:00
|
|
|
user_id = "@user:server"
|
2019-07-10 11:26:49 -04:00
|
|
|
self.get_success(self.store.register_user(user_id=user_id, password_hash=None))
|
2018-08-02 08:47:19 -04:00
|
|
|
|
2018-09-20 06:14:34 -04:00
|
|
|
active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
|
2018-08-02 08:47:19 -04:00
|
|
|
self.assertFalse(active)
|
|
|
|
|
2018-09-20 06:14:34 -04:00
|
|
|
# Trigger the saving loop
|
|
|
|
self.reactor.advance(10)
|
|
|
|
|
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
|
|
|
user_id, "access_token", "ip", "user_agent", "device_id"
|
|
|
|
)
|
2018-08-02 08:47:19 -04:00
|
|
|
)
|
2018-09-20 06:14:34 -04:00
|
|
|
active = self.get_success(self.store.user_last_seen_monthly_active(user_id))
|
2018-08-02 08:47:19 -04:00
|
|
|
self.assertTrue(active)
|
2018-09-20 06:14:34 -04:00
|
|
|
|
2022-12-09 12:36:32 -05:00
|
|
|
def test_devices_last_seen_bg_update(self) -> None:
|
2019-09-23 11:50:31 -04:00
|
|
|
# First make sure we have completed all updates.
|
2021-10-06 08:56:45 -04:00
|
|
|
self.wait_for_background_updates()
|
2019-09-23 11:50:31 -04:00
|
|
|
|
|
|
|
user_id = "@user:id"
|
2019-12-10 11:22:29 -05:00
|
|
|
device_id = "MY_DEVICE"
|
|
|
|
|
|
|
|
# Insert a user IP
|
|
|
|
self.get_success(
|
|
|
|
self.store.store_device(
|
|
|
|
user_id,
|
|
|
|
device_id,
|
|
|
|
"display name",
|
|
|
|
)
|
2021-02-16 17:32:34 -05:00
|
|
|
)
|
2019-09-23 11:50:31 -04:00
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
2019-12-10 11:22:29 -05:00
|
|
|
user_id, "access_token", "ip", "user_agent", device_id
|
2019-09-23 11:50:31 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
# Force persisting to disk
|
|
|
|
self.reactor.advance(200)
|
|
|
|
|
|
|
|
# But clear the associated entry in devices table
|
|
|
|
self.get_success(
|
2020-08-05 16:38:57 -04:00
|
|
|
self.store.db_pool.simple_update(
|
2019-09-23 11:50:31 -04:00
|
|
|
table="devices",
|
2019-12-10 11:22:29 -05:00
|
|
|
keyvalues={"user_id": user_id, "device_id": device_id},
|
2019-09-23 11:50:31 -04:00
|
|
|
updatevalues={"last_seen": None, "ip": None, "user_agent": None},
|
|
|
|
desc="test_devices_last_seen_bg_update",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# We should now get nulls when querying
|
|
|
|
result = self.get_success(
|
2019-12-10 11:22:29 -05:00
|
|
|
self.store.get_last_client_ip_by_device(user_id, device_id)
|
2019-09-23 11:50:31 -04:00
|
|
|
)
|
|
|
|
|
2019-12-10 11:22:29 -05:00
|
|
|
r = result[(user_id, device_id)]
|
2023-10-17 08:47:42 -04:00
|
|
|
self.assertEqual(
|
|
|
|
DeviceLastConnectionInfo(
|
|
|
|
user_id=user_id,
|
|
|
|
device_id=device_id,
|
|
|
|
ip=None,
|
|
|
|
user_agent=None,
|
|
|
|
last_seen=None,
|
|
|
|
),
|
|
|
|
r,
|
2019-09-23 11:50:31 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# Register the background update to run again.
|
|
|
|
self.get_success(
|
2020-08-05 16:38:57 -04:00
|
|
|
self.store.db_pool.simple_insert(
|
2019-09-23 11:50:31 -04:00
|
|
|
table="background_updates",
|
|
|
|
values={
|
|
|
|
"update_name": "devices_last_seen",
|
|
|
|
"progress_json": "{}",
|
|
|
|
"depends_on": None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# ... and tell the DataStore that it hasn't finished all updates yet
|
2020-08-05 16:38:57 -04:00
|
|
|
self.store.db_pool.updates._all_done = False
|
2019-09-23 11:50:31 -04:00
|
|
|
|
|
|
|
# Now let's actually drive the updates to completion
|
2021-10-06 08:56:45 -04:00
|
|
|
self.wait_for_background_updates()
|
2019-09-23 11:50:31 -04:00
|
|
|
|
|
|
|
# We should now get the correct result again
|
|
|
|
result = self.get_success(
|
2019-12-10 11:22:29 -05:00
|
|
|
self.store.get_last_client_ip_by_device(user_id, device_id)
|
2019-09-23 11:50:31 -04:00
|
|
|
)
|
|
|
|
|
2019-12-10 11:22:29 -05:00
|
|
|
r = result[(user_id, device_id)]
|
2023-10-17 08:47:42 -04:00
|
|
|
self.assertEqual(
|
|
|
|
DeviceLastConnectionInfo(
|
|
|
|
user_id=user_id,
|
|
|
|
device_id=device_id,
|
|
|
|
ip="ip",
|
|
|
|
user_agent="user_agent",
|
|
|
|
last_seen=0,
|
|
|
|
),
|
|
|
|
r,
|
2019-09-23 11:50:31 -04:00
|
|
|
)
|
|
|
|
|
2022-12-09 12:36:32 -05:00
|
|
|
def test_old_user_ips_pruned(self) -> None:
|
2019-09-24 10:51:42 -04:00
|
|
|
# First make sure we have completed all updates.
|
2021-10-06 08:56:45 -04:00
|
|
|
self.wait_for_background_updates()
|
2019-09-24 10:51:42 -04:00
|
|
|
|
|
|
|
user_id = "@user:id"
|
2019-12-10 11:22:29 -05:00
|
|
|
device_id = "MY_DEVICE"
|
|
|
|
|
|
|
|
# Insert a user IP
|
|
|
|
self.get_success(
|
|
|
|
self.store.store_device(
|
|
|
|
user_id,
|
|
|
|
device_id,
|
|
|
|
"display name",
|
|
|
|
)
|
2021-02-16 17:32:34 -05:00
|
|
|
)
|
2019-09-24 10:51:42 -04:00
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
2019-12-10 11:22:29 -05:00
|
|
|
user_id, "access_token", "ip", "user_agent", device_id
|
2019-09-24 10:51:42 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Force persisting to disk
|
|
|
|
self.reactor.advance(200)
|
|
|
|
|
|
|
|
# We should see that in the DB
|
2023-10-26 13:01:36 -04:00
|
|
|
result = cast(
|
|
|
|
List[Tuple[str, str, str, Optional[str], int]],
|
|
|
|
self.get_success(
|
|
|
|
self.store.db_pool.simple_select_list(
|
|
|
|
table="user_ips",
|
|
|
|
keyvalues={"user_id": user_id},
|
|
|
|
retcols=[
|
|
|
|
"access_token",
|
|
|
|
"ip",
|
|
|
|
"user_agent",
|
|
|
|
"device_id",
|
|
|
|
"last_seen",
|
|
|
|
],
|
|
|
|
desc="get_user_ip_and_agents",
|
|
|
|
)
|
|
|
|
),
|
2019-09-24 10:51:42 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
result,
|
2023-10-26 13:01:36 -04:00
|
|
|
[("access_token", "ip", "user_agent", device_id, 0)],
|
2019-09-24 10:51:42 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# Now advance by a couple of months
|
|
|
|
self.reactor.advance(60 * 24 * 60 * 60)
|
|
|
|
|
|
|
|
# We should get no results.
|
2023-10-26 13:01:36 -04:00
|
|
|
result = cast(
|
|
|
|
List[Tuple[str, str, str, Optional[str], int]],
|
|
|
|
self.get_success(
|
|
|
|
self.store.db_pool.simple_select_list(
|
|
|
|
table="user_ips",
|
|
|
|
keyvalues={"user_id": user_id},
|
|
|
|
retcols=[
|
|
|
|
"access_token",
|
|
|
|
"ip",
|
|
|
|
"user_agent",
|
|
|
|
"device_id",
|
|
|
|
"last_seen",
|
|
|
|
],
|
|
|
|
desc="get_user_ip_and_agents",
|
|
|
|
)
|
|
|
|
),
|
2019-09-24 10:51:42 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(result, [])
|
|
|
|
|
|
|
|
# But we should still get the correct values for the device
|
2022-12-09 12:36:32 -05:00
|
|
|
result2 = self.get_success(
|
2019-12-10 11:22:29 -05:00
|
|
|
self.store.get_last_client_ip_by_device(user_id, device_id)
|
2019-09-24 10:51:42 -04:00
|
|
|
)
|
|
|
|
|
2022-12-09 12:36:32 -05:00
|
|
|
r = result2[(user_id, device_id)]
|
2023-10-17 08:47:42 -04:00
|
|
|
self.assertEqual(
|
|
|
|
DeviceLastConnectionInfo(
|
|
|
|
user_id=user_id,
|
|
|
|
device_id=device_id,
|
|
|
|
ip="ip",
|
|
|
|
user_agent="user_agent",
|
|
|
|
last_seen=0,
|
|
|
|
),
|
|
|
|
r,
|
2019-09-24 10:51:42 -04:00
|
|
|
)
|
|
|
|
|
2023-08-23 09:00:34 -04:00
|
|
|
def test_invalid_user_agents_are_ignored(self) -> None:
|
|
|
|
# First make sure we have completed all updates.
|
|
|
|
self.wait_for_background_updates()
|
|
|
|
|
|
|
|
user_id1 = "@user1:id"
|
|
|
|
user_id2 = "@user2:id"
|
|
|
|
device_id1 = "MY_DEVICE1"
|
|
|
|
device_id2 = "MY_DEVICE2"
|
|
|
|
access_token1 = "access_token1"
|
|
|
|
access_token2 = "access_token2"
|
|
|
|
|
|
|
|
# Insert a user IP 1
|
|
|
|
self.get_success(
|
|
|
|
self.store.store_device(
|
|
|
|
user_id1,
|
|
|
|
device_id1,
|
|
|
|
"display name1",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
# Insert a user IP 2
|
|
|
|
self.get_success(
|
|
|
|
self.store.store_device(
|
|
|
|
user_id2,
|
|
|
|
device_id2,
|
|
|
|
"display name2",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
|
|
|
user_id1, access_token1, "ip", "sync-v3-proxy-", device_id1
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.get_success(
|
|
|
|
self.store.insert_client_ip(
|
|
|
|
user_id2, access_token2, "ip", "user_agent", device_id2
|
|
|
|
)
|
|
|
|
)
|
|
|
|
# Force persisting to disk
|
|
|
|
self.reactor.advance(200)
|
|
|
|
|
|
|
|
# We should see that in the DB
|
2023-10-26 13:01:36 -04:00
|
|
|
result = cast(
|
|
|
|
List[Tuple[str, str, str, Optional[str], int]],
|
|
|
|
self.get_success(
|
|
|
|
self.store.db_pool.simple_select_list(
|
|
|
|
table="user_ips",
|
|
|
|
keyvalues={},
|
|
|
|
retcols=[
|
|
|
|
"access_token",
|
|
|
|
"ip",
|
|
|
|
"user_agent",
|
|
|
|
"device_id",
|
|
|
|
"last_seen",
|
|
|
|
],
|
|
|
|
desc="get_user_ip_and_agents",
|
|
|
|
)
|
|
|
|
),
|
2023-08-23 09:00:34 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# ensure user1 is filtered out
|
2023-10-26 13:01:36 -04:00
|
|
|
self.assertEqual(result, [(access_token2, "ip", "user_agent", device_id2, 0)])
|
2023-08-23 09:00:34 -04:00
|
|
|
|
2018-09-20 06:14:34 -04:00
|
|
|
|
|
|
|
class ClientIpAuthTestCase(unittest.HomeserverTestCase):
|
2019-05-02 06:59:16 -04:00
|
|
|
servlets = [
|
2021-04-09 04:44:40 -04:00
|
|
|
synapse.rest.admin.register_servlets,
|
2019-05-02 06:59:16 -04:00
|
|
|
login.register_servlets,
|
|
|
|
]
|
2018-09-20 06:14:34 -04:00
|
|
|
|
2022-12-09 12:36:32 -05:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = self.hs.get_datastores().main
|
2018-10-01 10:11:58 -04:00
|
|
|
self.user_id = self.register_user("bob", "abc123", True)
|
2018-09-20 06:14:34 -04:00
|
|
|
|
2022-12-09 12:36:32 -05:00
|
|
|
def test_request_with_xforwarded(self) -> None:
|
2018-09-20 06:14:34 -04:00
|
|
|
"""
|
|
|
|
The IP in X-Forwarded-For is entered into the client IPs table.
|
|
|
|
"""
|
|
|
|
self._runtest(
|
|
|
|
{b"X-Forwarded-For": b"127.9.0.1"},
|
|
|
|
"127.9.0.1",
|
|
|
|
{"request": XForwardedForRequest},
|
|
|
|
)
|
|
|
|
|
2022-12-09 12:36:32 -05:00
|
|
|
def test_request_from_getPeer(self) -> None:
|
2018-09-20 06:14:34 -04:00
|
|
|
"""
|
|
|
|
The IP returned by getPeer is entered into the client IPs table, if
|
|
|
|
there's no X-Forwarded-For header.
|
|
|
|
"""
|
|
|
|
self._runtest({}, "127.0.0.1", {})
|
|
|
|
|
2022-12-09 12:36:32 -05:00
|
|
|
def _runtest(
|
|
|
|
self,
|
|
|
|
headers: Dict[bytes, bytes],
|
|
|
|
expected_ip: str,
|
|
|
|
make_request_args: Dict[str, Any],
|
|
|
|
) -> None:
|
2018-09-20 06:14:34 -04:00
|
|
|
device_id = "bleb"
|
|
|
|
|
2018-10-01 10:11:58 -04:00
|
|
|
access_token = self.login("bob", "abc123", device_id=device_id)
|
2018-09-20 06:14:34 -04:00
|
|
|
|
|
|
|
# Advance to a known time
|
|
|
|
self.reactor.advance(123456 - self.reactor.seconds())
|
|
|
|
|
2020-11-16 09:45:22 -05:00
|
|
|
headers1 = {b"User-Agent": b"Mozzila pizza"}
|
|
|
|
headers1.update(headers)
|
|
|
|
|
2020-11-15 17:49:21 -05:00
|
|
|
make_request(
|
2020-11-16 09:45:22 -05:00
|
|
|
self.reactor,
|
2020-11-16 10:51:47 -05:00
|
|
|
self.site,
|
2018-09-20 06:14:34 -04:00
|
|
|
"GET",
|
2021-04-09 04:44:40 -04:00
|
|
|
"/_synapse/admin/v2/users/" + self.user_id,
|
2018-09-20 06:14:34 -04:00
|
|
|
access_token=access_token,
|
2020-11-16 09:45:22 -05:00
|
|
|
custom_headers=headers1.items(),
|
2018-09-20 06:14:34 -04:00
|
|
|
**make_request_args,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Advance so the save loop occurs
|
|
|
|
self.reactor.advance(100)
|
|
|
|
|
|
|
|
result = self.get_success(
|
|
|
|
self.store.get_last_client_ip_by_device(self.user_id, device_id)
|
|
|
|
)
|
|
|
|
r = result[(self.user_id, device_id)]
|
2023-10-17 08:47:42 -04:00
|
|
|
self.assertEqual(
|
|
|
|
DeviceLastConnectionInfo(
|
|
|
|
user_id=self.user_id,
|
|
|
|
device_id=device_id,
|
|
|
|
ip=expected_ip,
|
|
|
|
user_agent="Mozzila pizza",
|
|
|
|
last_seen=123456100,
|
|
|
|
),
|
|
|
|
r,
|
2018-09-20 06:14:34 -04:00
|
|
|
)
|