aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar S. Co1 <[email protected]>2020-03-08 19:00:47 -0400
committerGravatar S. Co1 <[email protected]>2020-03-08 19:00:47 -0400
commit8cf851b5daccb8d8c8b520fe34c3fd5dada8505b (patch)
tree7294433b431d10f4bae597bd66cbdf28c0160dbd
parentMerge pull request #820 from python-discord/feat/ci/pre-commit-tweaks (diff)
Refactor token detection to check all potential substrings in message
-rw-r--r--bot/cogs/token_remover.py13
1 files changed, 10 insertions, 3 deletions
diff --git a/bot/cogs/token_remover.py b/bot/cogs/token_remover.py
index 82c01ae96..547ba8da0 100644
--- a/bot/cogs/token_remover.py
+++ b/bot/cogs/token_remover.py
@@ -96,12 +96,19 @@ class TokenRemover(Cog):
if msg.author.bot:
return False
- maybe_match = TOKEN_RE.search(msg.content)
- if maybe_match is None:
+ # Use findall rather than search to guard against method calls prematurely returning the
+ # token check (e.g. `message.channel.send` also matches our token pattern)
+ maybe_matches = TOKEN_RE.findall(msg.content)
+ if not maybe_matches:
return False
+ return any(cls.is_maybe_token(substr) for substr in maybe_matches)
+
+ @classmethod
+ def is_maybe_token(cls, test_str: str) -> bool:
+ """Check the provided string to see if it is a seemingly valid token."""
try:
- user_id, creation_timestamp, hmac = maybe_match.group(0).split('.')
+ user_id, creation_timestamp, hmac = test_str.split('.')
except ValueError:
return False