Browse Source

Check that subscription regexen compile before accepting them

Previously we blindly accepted any string as a regex, but then
it could fail to compile at search-time when a track topic got
updated, which would cause the bot to crash.

So do a precautionary compile of the regex in order to ensure
we only accept valid regexen.

Change-Id: Id347b88adfc7150ff26550cf9170071e7ac33227
changes/16/656816/1
Adam Spiers 2 years ago
parent
commit
b14ec32f05
1 changed files with 11 additions and 6 deletions
  1. +11
    -6
      ptgbot/bot.py

+ 11
- 6
ptgbot/bot.py View File

@ -223,12 +223,17 @@ class PTGBot(SASL, SSL, irc.bot.SingleServerIRCBot):
reply_to, nick,
"Your current subscription regex is: " + existing_re)
else:
self.data.set_subscription(nick, new_re)
self.send_priv_or_pub(
reply_to, nick,
"Subscription set to " + new_re +
(" (was %s)" % existing_re if existing_re else "")
)
try:
re.compile(new_re)
except Exception as e:
self.send_priv_or_pub(reply_to, nick, "Invalid regex: %s" % e)
else:
self.data.set_subscription(nick, new_re)
self.send_priv_or_pub(
reply_to, nick,
"Subscription set to " + new_re +
(" (was %s)" % existing_re if existing_re else "")
)
def unsubscribe(self, reply_to, nick):
existing_re = self.data.get_subscription(nick)


Loading…
Cancel
Save