Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit ec662bb

Browse files
Filter out unwanted user_agents from udv. (#16124)
1 parent 4adaba9 commit ec662bb

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

changelog.d/16124.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Filter out user agent references to the sliding sync proxy and rust-sdk from the user_daily_visits table to ensure that Element X can be represented fully.

synapse/storage/databases/main/client_ips.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,11 @@ async def insert_client_ip(
579579
device_id: Optional[str],
580580
now: Optional[int] = None,
581581
) -> None:
582+
# The sync proxy continuously triggers /sync even if the user is not
583+
# present so should be excluded from user_ips entries.
584+
if user_agent == "sync-v3-proxy-":
585+
return
586+
582587
if not now:
583588
now = int(self._clock.time_msec())
584589
key = (user_id, access_token, ip)

tests/storage/test_client_ips.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,71 @@ def test_old_user_ips_pruned(self) -> None:
654654
r,
655655
)
656656

657+
def test_invalid_user_agents_are_ignored(self) -> None:
658+
# First make sure we have completed all updates.
659+
self.wait_for_background_updates()
660+
661+
user_id1 = "@user1:id"
662+
user_id2 = "@user2:id"
663+
device_id1 = "MY_DEVICE1"
664+
device_id2 = "MY_DEVICE2"
665+
access_token1 = "access_token1"
666+
access_token2 = "access_token2"
667+
668+
# Insert a user IP 1
669+
self.get_success(
670+
self.store.store_device(
671+
user_id1,
672+
device_id1,
673+
"display name1",
674+
)
675+
)
676+
# Insert a user IP 2
677+
self.get_success(
678+
self.store.store_device(
679+
user_id2,
680+
device_id2,
681+
"display name2",
682+
)
683+
)
684+
685+
self.get_success(
686+
self.store.insert_client_ip(
687+
user_id1, access_token1, "ip", "sync-v3-proxy-", device_id1
688+
)
689+
)
690+
self.get_success(
691+
self.store.insert_client_ip(
692+
user_id2, access_token2, "ip", "user_agent", device_id2
693+
)
694+
)
695+
# Force persisting to disk
696+
self.reactor.advance(200)
697+
698+
# We should see that in the DB
699+
result = self.get_success(
700+
self.store.db_pool.simple_select_list(
701+
table="user_ips",
702+
keyvalues={},
703+
retcols=["access_token", "ip", "user_agent", "device_id", "last_seen"],
704+
desc="get_user_ip_and_agents",
705+
)
706+
)
707+
708+
# ensure user1 is filtered out
709+
self.assertEqual(
710+
result,
711+
[
712+
{
713+
"access_token": access_token2,
714+
"ip": "ip",
715+
"user_agent": "user_agent",
716+
"device_id": device_id2,
717+
"last_seen": 0,
718+
}
719+
],
720+
)
721+
657722

658723
class ClientIpAuthTestCase(unittest.HomeserverTestCase):
659724
servlets = [

0 commit comments

Comments
 (0)