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

Commit e0414eb

Browse files
erikjohnstonclokepsquahtx
committed
POC delete stale non-e2e devices for users (#14038)
This should help reduce the number of devices e.g. simple bots the repeatedly login rack up. We only delete non-e2e devices as they should be safe to delete, whereas if we delete e2e devices for a user we may accidentally break their ability to receive e2e keys for a message. Co-authored-by: Patrick Cloke <[email protected]> Co-authored-by: Sean Quah <[email protected]>
1 parent 656dce4 commit e0414eb

File tree

5 files changed

+83
-4
lines changed

5 files changed

+83
-4
lines changed

changelog.d/14038.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prune user's old devices on login if they have too many.

synapse/handlers/device.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,9 @@ async def check_device_registered(
421421

422422
self._check_device_name_length(initial_device_display_name)
423423

424+
# Prune the user's device list if they already have a lot of devices.
425+
await self._prune_too_many_devices(user_id)
426+
424427
if device_id is not None:
425428
new_device = await self.store.store_device(
426429
user_id=user_id,
@@ -452,6 +455,14 @@ async def check_device_registered(
452455

453456
raise errors.StoreError(500, "Couldn't generate a device ID.")
454457

458+
async def _prune_too_many_devices(self, user_id: str) -> None:
459+
"""Delete any excess old devices this user may have."""
460+
device_ids = await self.store.check_too_many_devices_for_user(user_id)
461+
if not device_ids:
462+
return
463+
464+
await self.delete_devices(user_id, device_ids)
465+
455466
async def _delete_stale_devices(self) -> None:
456467
"""Background task that deletes devices which haven't been accessed for more than
457468
a configured time period.
@@ -481,7 +492,7 @@ async def delete_all_devices_for_user(
481492
device_ids = [d for d in device_ids if d != except_device_id]
482493
await self.delete_devices(user_id, device_ids)
483494

484-
async def delete_devices(self, user_id: str, device_ids: List[str]) -> None:
495+
async def delete_devices(self, user_id: str, device_ids: Collection[str]) -> None:
485496
"""Delete several devices
486497
487498
Args:

synapse/storage/databases/main/devices.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,70 @@ def _txn(txn: LoggingTransaction) -> int:
15331533

15341534
return rows
15351535

1536+
async def check_too_many_devices_for_user(self, user_id: str) -> Collection[str]:
1537+
"""Check if the user has a lot of devices, and if so return the set of
1538+
devices we can prune.
1539+
1540+
This does *not* return hidden devices or devices with E2E keys.
1541+
"""
1542+
1543+
num_devices = await self.db_pool.simple_select_one_onecol(
1544+
table="devices",
1545+
keyvalues={"user_id": user_id, "hidden": False},
1546+
retcol="COALESCE(COUNT(*), 0)",
1547+
desc="count_devices",
1548+
)
1549+
1550+
# We let users have up to ten devices without pruning.
1551+
if num_devices <= 10:
1552+
return ()
1553+
1554+
# We prune everything older than N days.
1555+
max_last_seen = self._clock.time_msec() - 14 * 24 * 60 * 60 * 1000
1556+
1557+
if num_devices > 50:
1558+
# If the user has more than 50 devices, then we chose a last seen
1559+
# that ensures we keep at most 50 devices.
1560+
sql = """
1561+
SELECT last_seen FROM devices
1562+
WHERE
1563+
user_id = ?
1564+
AND NOT hidden
1565+
AND last_seen IS NOT NULL
1566+
AND key_json IS NULL
1567+
ORDER BY last_seen DESC
1568+
LIMIT 1
1569+
OFFSET 50
1570+
"""
1571+
1572+
rows = await self.db_pool.execute(
1573+
"check_too_many_devices_for_user_last_seen", None, sql, (user_id,)
1574+
)
1575+
if rows:
1576+
max_last_seen = max(rows[0][0], max_last_seen)
1577+
1578+
# Now fetch the devices to delete.
1579+
sql = """
1580+
SELECT DISTINCT device_id FROM devices
1581+
LEFT JOIN e2e_device_keys_json USING (user_id, device_id)
1582+
WHERE
1583+
user_id = ?
1584+
AND NOT hidden
1585+
AND last_seen < ?
1586+
AND key_json IS NULL
1587+
"""
1588+
1589+
def check_too_many_devices_for_user_txn(
1590+
txn: LoggingTransaction,
1591+
) -> Collection[str]:
1592+
txn.execute(sql, (user_id, max_last_seen))
1593+
return {device_id for device_id, in txn}
1594+
1595+
return await self.db_pool.runInteraction(
1596+
"check_too_many_devices_for_user",
1597+
check_too_many_devices_for_user_txn,
1598+
)
1599+
15361600

15371601
class DeviceStore(DeviceWorkerStore, DeviceBackgroundUpdateStore):
15381602
# Because we have write access, this will be a StreamIdGenerator
@@ -1591,6 +1655,7 @@ async def store_device(
15911655
values={},
15921656
insertion_values={
15931657
"display_name": initial_device_display_name,
1658+
"last_seen": self._clock.time_msec(),
15941659
"hidden": False,
15951660
},
15961661
desc="store_device",
@@ -1636,7 +1701,7 @@ async def store_device(
16361701
)
16371702
raise StoreError(500, "Problem storing device.")
16381703

1639-
async def delete_devices(self, user_id: str, device_ids: List[str]) -> None:
1704+
async def delete_devices(self, user_id: str, device_ids: Collection[str]) -> None:
16401705
"""Deletes several devices.
16411706
16421707
Args:

tests/handlers/test_device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_get_devices_by_user(self) -> None:
115115
"device_id": "xyz",
116116
"display_name": "display 0",
117117
"last_seen_ip": None,
118-
"last_seen_ts": None,
118+
"last_seen_ts": 1000000,
119119
},
120120
device_map["xyz"],
121121
)

tests/storage/test_client_ips.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ def test_get_last_client_ip_by_device(self, after_persisting: bool):
169169
)
170170
)
171171

172+
last_seen = self.clock.time_msec()
173+
172174
if after_persisting:
173175
# Trigger the storage loop
174176
self.reactor.advance(10)
@@ -189,7 +191,7 @@ def test_get_last_client_ip_by_device(self, after_persisting: bool):
189191
"device_id": device_id,
190192
"ip": None,
191193
"user_agent": None,
192-
"last_seen": None,
194+
"last_seen": last_seen,
193195
},
194196
],
195197
)

0 commit comments

Comments
 (0)