Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor auth token fetching #291

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

Refactor auth token fetching #291

wants to merge 12 commits into from

Conversation

MattyTheHacker
Copy link
Member

No description provided.

guest_role = await ctx.bot.guest_role
MAKE_EPHEMERAL: bool = True

if "committee" in ctx.channel.name.lower():
Copy link
Member

Choose a reason for hiding this comment

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

This is an insecure way of checking if the message should be ephemeral. The name of a channel has no bearing on whether the channel is actually only accessible to committee members. Although it would be foolish for users to name a channel containing committee yet allow others to read it, we have an obligation to ensure security despite the stupidity of users.

I would also argue that a channel that has permissions set to only allow committee to read it, yet does not have the word "committee" in the channel's name should be allowed to not have messages sent ephemerally, whereas this check would prevent it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I mean to a certain extent I think we could just remove the check entirely and leave the responsibility of determining whether it should be ephemeral or not up to the committee member running the command. Could be in the form of an argument or just two commands.

cogs/get_token_authorisation.py Outdated Show resolved Hide resolved
cogs/get_token_authorisation.py Outdated Show resolved Hide resolved
logger.debug(
"No admin table was found, meaning the token provided "
"does not have admin access to any societies.",
if isinstance(page_title, bs4.NavigableString) and "Login" in page_title or "Login" in page_title.string: # type: ignore[operator, union-attr] # noqa: E501
Copy link
Member

Choose a reason for hiding this comment

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

Flip guard clause check & improve invalid type handling rather than ignoring errors.

Fix:

BAD_TOKEN_MESSAGE: Final[str] = (
    "Unable to fetch profile page because the token was not valid."
)
profile_page_invalid_message: str | None = None

if isinstance(page_title, bs4.Tag):
    if not hasattr(page_title, "string"):
        profile_page_invalid_message = "Profile page had invalid format"

    else:
        title_string: object = getattr(page_title, "string")
        if not isinstance(title_string, str):
            raise TypeError

        if "login" in title_string.lower():
            profile_page_invalid_message = BAD_TOKEN_MESSAGE

elif isinstance(page_title, bs4.NavigableString):
    if "login" in page_title.lower():
        profile_page_invalid_message = BAD_TOKEN_MESSAGE

else:
    raise TypeError

if profile_page_invalid_message is not None:
    logger.warning(profile_page_invalid_message)
    await ctx.respond(content=profile_page_invalid_message)
    return

(This could be improved by using Python's new match-case statement, but that's not too big of an issue)

cogs/get_token_authorisation.py Outdated Show resolved Hide resolved
cogs/get_token_authorisation.py Outdated Show resolved Hide resolved
@CarrotManMatt CarrotManMatt added bug Something isn't working enhancement New feature or request and removed bug Something isn't working labels Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants