Skip to content

Fix 400 when "clyde" is in any webhook username#1009

Merged
kwzrd merged 5 commits into
masterfrom
bug/mod/bot-2a/webhook-clyde
Jun 19, 2020
Merged

Fix 400 when "clyde" is in any webhook username#1009
kwzrd merged 5 commits into
masterfrom
bug/mod/bot-2a/webhook-clyde

Conversation

@MarkKoz
Copy link
Copy Markdown
Contributor

@MarkKoz MarkKoz commented Jun 16, 2020

Discord just disallows this name.

The replacement character is Mathematical Sans-Serif Small E, which I felt was the best choice due to its similarity to a normal "e". However, I'm open to changing it to another character, putting it in parenthesis, or whatever else.

@MarkKoz MarkKoz added t: bug Something isn't working a: moderation Related to community moderation functionality: (moderation, defcon, verification) p: 3 - low Low Priority labels Jun 16, 2020
@MarkKoz MarkKoz requested a review from a team as a code owner June 16, 2020 01:11
@MarkKoz MarkKoz requested review from eivl and manusaurio and removed request for a team June 16, 2020 01:11
@ghost ghost added the needs 2 reviews label Jun 16, 2020
@MarkKoz MarkKoz changed the title Fix 400 when "clyde" is in webhook username Fix 400 when "clyde" is in watch channel webhook username Jun 16, 2020
@sentry
Copy link
Copy Markdown

sentry Bot commented Jun 16, 2020

Sentry issue: BOT-2A

@MarkKoz MarkKoz marked this pull request as draft June 16, 2020 01:29
Discord just disallows this name.
@MarkKoz MarkKoz force-pushed the bug/mod/bot-2a/webhook-clyde branch from e935a9f to bcf6993 Compare June 16, 2020 02:16
@MarkKoz MarkKoz marked this pull request as ready for review June 16, 2020 02:18
@MarkKoz MarkKoz changed the title Fix 400 when "clyde" is in watch channel webhook username Fix 400 when "clyde" is in any webhook username Jun 16, 2020
@sentry
Copy link
Copy Markdown

sentry Bot commented Jun 16, 2020

Sentry issue: BOT-3Y

Copy link
Copy Markdown
Contributor

@Numerlor Numerlor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the cyrillic е would be better here and seems to look the same as a normal e discord Es with the desktop client.
The e's case is now also "destroyed" when it goes through the sub, so I think it wouldn't hurt to find something that looks like an E, if there is something like that and is worth the effort.

Do we also need to sub the names in reddit/python_news? The consistency is nice but having clyde in those is extremely unlikely

Comment thread bot/utils/messages.py
return urls


def sub_clyde(username: Optional[str]) -> Optional[str]:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what case username can be None? So much how I see, this should pass str every time.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's optional here though every use of that function passes a name.

async def webhook_send(
self,
content: Optional[str] = None,
username: Optional[str] = None,
avatar_url: Optional[str] = None,
embed: Optional[Embed] = None,
) -> None:
"""Sends a message to the webhook with the specified kwargs."""
username = messages.sub_clyde(username)

I don't see a problem with leaving it optional anyway.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this internally handles the None (and it does) then I agree the annotation is appropriate, but why does it handle it? Is it to be "shielded" from callers which may accidentally give a None? Won't that just push the error further, since something else that is expecting a string will get the declyded None?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None is a valid value for webhook username (acts like an empty string and defaults to the webhook name) that may get passed in so I don't think there's any problem with letting None through.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case I agree that this is perfectly fine.

@MarkKoz
Copy link
Copy Markdown
Contributor Author

MarkKoz commented Jun 17, 2020

I believe the cyrillic е would be better here and seems to look the same as a normal e

Good find. The website I used didn't show Cyrillic characters as characters based on "e" (probably only considered latin alphabet). Cyrillic also has an upper case E so I will use that too. I wonder if there's a good way to do a case sensitive substitution. May just be easiest to chain two substitutions.

Do we also need to sub the names in reddit/python_news? The consistency is nice but having clyde in those is extremely unlikely

It's only one extra line of code, so better safe than sorry. I'd say this should be used for any name that's coming from an external source.

The Cyrillic characters are more likely to be rendered similarly to
their Latin counterparts than the math sans-serif characters.
Copy link
Copy Markdown
Contributor

@kwzrd kwzrd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's one small thing that I'd like to see adjusted, but otherwise I think this is a good fix.

My personal approach would have been turning it into something like clyd[e] (if that fixes the 400) just because I'd see that as more predictable w.r.t. various fonts and platforms. It'd also feel more "direct" to me, i.e. fixing the issue rather than "hiding" it, but I don't have a problem with this approach (the only change requested is the "" -> None issue).

Comment thread bot/utils/messages.py
Comment on lines +120 to +132
def sub_clyde(username: Optional[str]) -> Optional[str]:
"""
Replace "e"/"E" in any "clyde" in `username` with a Cyrillic "е"/"E" and return the new string.

Discord disallows "clyde" anywhere in the username for webhooks. It will return a 400.
Return None only if `username` is None.
"""
def replace_e(match: re.Match) -> str:
char = "е" if match[2] == "e" else "Е"
return match[1] + char

if username:
return re.sub(r"(clyd)(e)", replace_e, username, flags=re.I)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also return None if an empty string is given, which I suppose is fine if they are interchangeable in the webhook username field, but the docstring denies that. Explicitly checking for None on L131 seems like a better idea than adjusting the docstring though.

@MarkKoz MarkKoz requested a review from kwzrd June 19, 2020 22:16
@ghost ghost removed the needs 1 approval label Jun 19, 2020
@kwzrd kwzrd merged commit e370416 into master Jun 19, 2020
@kwzrd kwzrd deleted the bug/mod/bot-2a/webhook-clyde branch June 19, 2020 23:47
kwzrd pushed a commit that referenced this pull request Jun 20, 2020
With PR #1009 merged, we now apply the same fix to our relay function.

This prevents the "clyde" word from sneaking into the webhook username,
which is forbidden and will return a 400.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: moderation Related to community moderation functionality: (moderation, defcon, verification) p: 3 - low Low Priority t: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants