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

Commit a2d872e

Browse files
Merge pull request #3708 from matrix-org/neilj/resource_Limit_block_event_creation
Neilj/resource limit block event creation
2 parents fa27073 + 521d369 commit a2d872e

File tree

7 files changed

+29
-3
lines changed

7 files changed

+29
-3
lines changed

changelog.d/3708.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
For resource limit blocked users, prevent writing into rooms

synapse/api/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ def check_auth_blocking(self, user_id=None):
799799
current_mau = yield self.store.get_monthly_active_count()
800800
if current_mau >= self.hs.config.max_mau_value:
801801
raise AuthError(
802-
403, "Monthly Active User Limits AU Limit Exceeded",
802+
403, "Monthly Active User Limit Exceeded",
803803
admin_uri=self.hs.config.admin_uri,
804804
errcode=Codes.RESOURCE_LIMIT_EXCEED
805805
)

synapse/app/homeserver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ def generate_user_daily_visit_stats():
525525
clock.looping_call(
526526
hs.get_datastore().reap_monthly_active_users, 1000 * 60 * 60
527527
)
528+
hs.get_datastore().reap_monthly_active_users()
528529

529530
@defer.inlineCallbacks
530531
def generate_monthly_active_users():

synapse/handlers/message.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,14 @@ def create_event(self, requester, event_dict, token_id=None, txn_id=None,
276276
where *hashes* is a map from algorithm to hash.
277277
278278
If None, they will be requested from the database.
279-
279+
Raises:
280+
ResourceLimitError if server is blocked to some resource being
281+
exceeded
280282
Returns:
281283
Tuple of created event (FrozenEvent), Context
282284
"""
285+
yield self.auth.check_auth_blocking(requester.user.to_string())
286+
283287
builder = self.event_builder_factory.new(event_dict)
284288

285289
self.validator.validate_new(builder)

synapse/handlers/room.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,13 @@ def create_room(self, requester, config, ratelimit=True,
9898
Raises:
9999
SynapseError if the room ID couldn't be stored, or something went
100100
horribly wrong.
101+
ResourceLimitError if server is blocked to some resource being
102+
exceeded
101103
"""
102104
user_id = requester.user.to_string()
103105

106+
self.auth.check_auth_blocking(user_id)
107+
104108
if not self.spam_checker.user_may_create_room(user_id):
105109
raise SynapseError(403, "You are not permitted to create rooms")
106110

synapse/storage/monthly_active_users.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ def _reap_users(txn):
9696
# While Postgres does not require 'LIMIT', but also does not support
9797
# negative LIMIT values. So there is no way to write it that both can
9898
# support
99-
query_args = [self.hs.config.max_mau_value]
99+
safe_guard = self.hs.config.max_mau_value - len(self.reserved_users)
100+
# Must be greater than zero for postgres
101+
safe_guard = safe_guard if safe_guard > 0 else 0
102+
query_args = [safe_guard]
100103

101104
base_sql = """
102105
DELETE FROM monthly_active_users

tests/storage/test_monthly_active_users.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ def test_initialise_reserved_users(self):
7575
active_count = yield self.store.get_monthly_active_count()
7676
self.assertEquals(active_count, user_num)
7777

78+
# Test that regalar users are removed from the db
79+
ru_count = 2
80+
yield self.store.upsert_monthly_active_user("@ru1:server")
81+
yield self.store.upsert_monthly_active_user("@ru2:server")
82+
active_count = yield self.store.get_monthly_active_count()
83+
84+
self.assertEqual(active_count, user_num + ru_count)
85+
self.hs.config.max_mau_value = user_num
86+
yield self.store.reap_monthly_active_users()
87+
88+
active_count = yield self.store.get_monthly_active_count()
89+
self.assertEquals(active_count, user_num)
90+
7891
@defer.inlineCallbacks
7992
def test_can_insert_and_count_mau(self):
8093
count = yield self.store.get_monthly_active_count()

0 commit comments

Comments
 (0)