feat(matrixbot): stop auto-joining invites, restrict commands to two rooms
#general is now published to the public room directory, so the bot is reachable by strangers. Two hardening changes: - Invites are only accepted from INVITE_ALLOWED_USERS (defaults to ADMIN_USERS). Anything else is declined via room_leave so hostile invites do not accumulate as pending. Previously any invite from anyone was auto-joined. - Commands and the passive scramble/riddle answer checks only run in COMMAND_ROOMS (#commands and #management). The gate sits early in Callbacks.message, so the bot stays silent everywhere else, including #general. Set COMMAND_ROOMS="*" to restore the old behaviour. Reaction handlers are deliberately left ungated so the welcome flow keeps working; game votes elsewhere cannot match an active game. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
+15
-6
@@ -21,6 +21,7 @@ from config import (
|
||||
MATRIX_ACCESS_TOKEN,
|
||||
MATRIX_DEVICE_ID,
|
||||
MATRIX_PASSWORD,
|
||||
INVITE_ALLOWED_USERS,
|
||||
LOG_LEVEL,
|
||||
ConfigValidator,
|
||||
)
|
||||
@@ -148,13 +149,21 @@ async def main():
|
||||
client.add_event_callback(callbacks.unknown_event, UnknownEvent)
|
||||
client.add_event_callback(callbacks.member, RoomMemberEvent)
|
||||
|
||||
# Auto-accept room invites
|
||||
async def _auto_accept_invite(room, event):
|
||||
if event.membership == "invite" and event.state_key == MATRIX_USER_ID:
|
||||
logger.info("Auto-accepting invite to %s", room.room_id)
|
||||
await client.join(room.room_id)
|
||||
# Accept invites only from trusted users, and decline the rest so they do
|
||||
# not sit pending forever. Never auto-join on an arbitrary invite.
|
||||
async def _handle_invite(room, event):
|
||||
if event.membership != "invite" or event.state_key != MATRIX_USER_ID:
|
||||
return
|
||||
if event.sender not in INVITE_ALLOWED_USERS:
|
||||
logger.warning(
|
||||
"Declining invite to %s from untrusted sender %s", room.room_id, event.sender
|
||||
)
|
||||
await client.room_leave(room.room_id)
|
||||
return
|
||||
logger.info("Accepting invite to %s from %s", room.room_id, event.sender)
|
||||
await client.join(room.room_id)
|
||||
|
||||
client.add_event_callback(_auto_accept_invite, InviteMemberEvent)
|
||||
client.add_event_callback(_handle_invite, InviteMemberEvent)
|
||||
|
||||
# Graceful shutdown
|
||||
loop = asyncio.get_running_loop()
|
||||
|
||||
Reference in New Issue
Block a user