From bdf6512a36207abe4a59dcb92638dbcfce714144 Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Tue, 27 Jun 2023 11:03:52 +0200 Subject: [PATCH] fix(irc): restrict the start of usernames even more Turns out, they have to start with [a-zA-Z0-9]. --- dibridge/irc.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/dibridge/irc.py b/dibridge/irc.py index 4a55238..cf3ec2b 100644 --- a/dibridge/irc.py +++ b/dibridge/irc.py @@ -19,6 +19,14 @@ # talk to someone if they are in an active conversation with them. LEFT_WHILE_TALKING_TIMEOUT = 60 * 10 +# By RFC, only these characters are allowed in a nickname. +REGEX_NICKNAME_FILTER = r"[^a-zA-Z0-9_\-\[\]\{\}\|]" +# By RFC, a nickname cannot start with a number or a dash. +REGEX_NICKNAME_START_FILTER = r"^[0-9\-]+" +# By implementation, a username is more strict than a nickname in what +# it can start with. This filter is in additional to the nickname filters. +REGEX_USERNAME_START_FILTER = r"^[_\[\]\{\}\|]+" + class IRCRelay(irc.client_aio.AioSimpleIRCClient): def __init__(self, host, port, nickname, channel, puppet_ip_range, puppet_postfix, ignore_list, idle_timeout): @@ -130,9 +138,8 @@ async def _pinger(self): async def _connect(self): while True: username = self._nickname - # An additional constraints usernames have over nicknames, that they are - # also not allowed to start with an underscore. - username = re.sub(r"^_+", "", username) + # Additional constraints usernames have over nicknames. + username = re.sub(REGEX_USERNAME_START_FILTER, "", username) try: await self.connection.connect( @@ -211,10 +218,10 @@ def _sanitize_discord_username(self, discord_username): original_discord_username = discord_username discord_username = discord_username.strip() - # Remove all characters not allowed in IRC usernames. - discord_username = re.sub(r"[^a-zA-Z0-9_\-\[\]\{\}\|]", "", discord_username) - # Make sure a username doesn't start with a number or "-". - discord_username = re.sub(r"^[0-9\-]", "", discord_username) + # Remove all characters not allowed in IRC nicknames. + discord_username = re.sub(REGEX_NICKNAME_FILTER, "", discord_username) + # Make sure a nicknames doesn't start with an invalid character. + discord_username = re.sub(REGEX_NICKNAME_START_FILTER, "", discord_username) # On Discord you can create usernames that don't contain any character valid # on IRC, leaving an empty username. In that case we have no option but to