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

Commit 4fa2ff5

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 <clokep@users.noreply.github.com> Co-authored-by: Sean Quah <8349537+squahtx@users.noreply.github.com>
1 parent e863a99 commit 4fa2ff5

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
@@ -1538,6 +1538,70 @@ def _txn(txn: LoggingTransaction) -> int:
15381538

15391539
return rows
15401540

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

15421606
class DeviceStore(DeviceWorkerStore, DeviceBackgroundUpdateStore):
15431607
# Because we have write access, this will be a StreamIdGenerator
@@ -1596,6 +1660,7 @@ async def store_device(
15961660
values={},
15971661
insertion_values={
15981662
"display_name": initial_device_display_name,
1663+
"last_seen": self._clock.time_msec(),
15991664
"hidden": False,
16001665
},
16011666
desc="store_device",
@@ -1641,7 +1706,7 @@ async def store_device(
16411706
)
16421707
raise StoreError(500, "Problem storing device.")
16431708

1644-
async def delete_devices(self, user_id: str, device_ids: List[str]) -> None:
1709+
async def delete_devices(self, user_id: str, device_ids: Collection[str]) -> None:
16451710
"""Deletes several devices.
16461711
16471712
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)