Skip to content

Improve order of validation and ratelimiting in room creation#18723

Merged
MadLittleMods merged 3 commits intodevelopfrom
madlittlemods/fix-order-of-validation-and-rate-limits-in-room-creation
Aug 4, 2025
Merged

Improve order of validation and ratelimiting in room creation#18723
MadLittleMods merged 3 commits intodevelopfrom
madlittlemods/fix-order-of-validation-and-rate-limits-in-room-creation

Conversation

@MadLittleMods
Copy link
Copy Markdown
Contributor

@MadLittleMods MadLittleMods commented Jul 23, 2025

Improve order of validation and ratelimiting in room creation

Spawning from looking at this stuff while reviewing #18721

Pull Request Checklist

  • Pull request is based on the develop branch
  • Pull request includes a changelog file. The entry should:
    • Be a short description of your change which makes sense to users. "Fixed a bug that prevented receiving messages from other servers." instead of "Moved X method from EventStore to EventWorkerStore.".
    • Use markdown where necessary, mostly for code blocks.
    • End with either a period (.) or an exclamation mark (!).
    • Start with a capital letter.
    • Feel free to credit yourself, by adding a sentence "Contributed by @github_username." or "Contributed by [Your Name]." to the end of the entry.
  • Code style is correct (run the linters)

Comment thread synapse/handlers/room.py Outdated
Comment thread synapse/handlers/room.py
Comment on lines +897 to +908
# Run the spam checker after other validation
if not is_requester_admin:
spam_check = await self._spam_checker_module_callbacks.user_may_create_room(
user_id, config
)
if spam_check != self._spam_checker_module_callbacks.NOT_SPAM:
raise SynapseError(
403,
"You are not permitted to create rooms",
errcode=spam_check[0],
additional_fields=spam_check[1],
)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the spam checker callback after we validate the room config so the spam checker module doesn't have to deal with invalid config and flawed requests.

I still think user_may_create_room is flawed as it doesn't pick up all of the validation, massaging, and defaults we just added above. I think user_may_create_room should be deprecated in favor of a new check that we call in _send_events_for_new_room(...) so that the spam checker module has access to the actual data we're going to create the room with.

(also mentioned in #18721 (comment))

@MadLittleMods MadLittleMods marked this pull request as ready for review July 23, 2025 19:28
@MadLittleMods MadLittleMods requested a review from a team as a code owner July 23, 2025 19:28
…-rate-limits-in-room-creation

Conflicts:
	synapse/handlers/room.py
Comment thread synapse/handlers/room.py
Comment on lines +778 to +795
if ratelimit:
# Limit the rate of room creations,
# using both the limiter specific to room creations as well
# as the general request ratelimiter.
#
# Note that we don't rate limit the individual
# events in the room — room creation isn't atomic and
# historically it was very janky if half the events in the
# initial state don't make it because of rate limiting.

# First check the room creation ratelimiter without updating it
# (this is so we don't consume a token if the other ratelimiter doesn't
# allow us to proceed)
await self.creation_ratelimiter.ratelimit(requester, update=False)

# then apply the ratelimits
await self.common_request_ratelimiter.ratelimit(requester)
await self.creation_ratelimiter.ratelimit(requester)
Copy link
Copy Markdown
Contributor Author

@MadLittleMods MadLittleMods Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the rate limiting earlier so we don't waste resources processing the request if we're going to reject it anyway.

@MadLittleMods MadLittleMods merged commit ddbcd85 into develop Aug 4, 2025
76 of 78 checks passed
@MadLittleMods MadLittleMods deleted the madlittlemods/fix-order-of-validation-and-rate-limits-in-room-creation branch August 4, 2025 16:08
@MadLittleMods
Copy link
Copy Markdown
Contributor Author

Thanks for the review @reivilibre 🐊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants