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
This commit is contained in:
Adam Spiers 2019-05-02 11:21:14 -06:00
parent 4fbaf41725
commit b14ec32f05
1 changed files with 11 additions and 6 deletions

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)