Skip to content

Conversation

@meatharvester
Copy link
Collaborator

@meatharvester meatharvester commented Jul 26, 2025

Description

Added some additional checks to ensure only messages from Tux can only be deleted in DMs. previously it was possible for any user (even without elevated permissions) to delete any message in the server that the bot was active in.

Guidelines

  • My code follows the style guidelines of this project (formatted with Ruff)

  • I have performed a self-review of my own code

  • I have commented my code, particularly in hard-to-understand areas

  • I have made corresponding changes to the documentation if needed

  • My changes generate no new warnings

  • I have tested this change

  • Any dependent changes have been merged and published in downstream modules

  • I have added all appropriate labels to this PR

  • I have followed all of these guidelines.

How Has This Been Tested? (if applicable)

I tested both on an account who owned the server and one without any permissions and made sure both could delete the Tux DM messages

Summary by Sourcery

Restrict bookmark removal to direct messages and bot-authored messages by adding channel type and author checks.

Bug Fixes:

  • Prevent bookmark removals in non-DM channels by returning early if the channel is not a DMChannel
  • Ensure the remove bookmark emoji only deletes messages authored by the bot

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jul 26, 2025

Reviewer's Guide

This PR refines the on_raw_reaction_add handler to prevent unauthorized bookmark removals by enforcing that removal reactions occur only in direct message channels and only on messages originally sent by the bot.

Sequence diagram for bookmark removal reaction handling in DMs

sequenceDiagram
    participant User
    participant Discord
    participant Bot
    participant BookmarksService
    User->>Discord: Reacts with remove bookmark emoji on a message
    Discord->>Bot: on_raw_reaction_add event
    Bot->>BookmarksService: on_raw_reaction_add(payload)
    BookmarksService->>BookmarksService: Check if channel is DMChannel
    alt Not a DMChannel
        BookmarksService-->>Bot: Return (do nothing)
    else Is DMChannel
        BookmarksService->>BookmarksService: Check if message.author is bot user
        alt Message author is bot
            BookmarksService->>BookmarksService: remove_bookmark(message)
        else Message author is not bot
            BookmarksService-->>Bot: Return (do nothing)
        end
    end
Loading

Class diagram for updated bookmark removal logic

classDiagram
    class BookmarksService {
        +on_raw_reaction_add(payload)
        +add_bookmark(user, message)
        +remove_bookmark(message)
    }
    class discord.DMChannel
    class discord.Message {
        +author
    }
    class discord.RawReactionActionEvent {
        +emoji
        +channel_id
        +message_id
        +user_id
    }
    BookmarksService --|> discord.RawReactionActionEvent
    BookmarksService ..> discord.DMChannel : checks instance
    BookmarksService ..> discord.Message : checks author
Loading

File-Level Changes

Change Details Files
Restrict bookmark removals to DMs on bot-authored messages
  • Added a guard to verify the channel is a DMChannel and early-return otherwise
  • Updated the removal conditional to require both the remove-bookmark emoji and message.author === bot.user
  • Removed the previous unconditional elif branch allowing removals anywhere
tux/cogs/services/bookmarks.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions
Copy link
Contributor

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @cherryl1k - I've reviewed your changes - here's some feedback:

  • Consider using an elif for the remove-bookmark branch instead of two separate ifs so you don’t perform the DM guard check after the add-bookmark logic.
  • Use == rather than is when comparing message.author to self.bot.user to avoid potential identity issues with discord.py model instances.
  • Correct the comment typo to “Ensures we’re in a user’s DMs before removing…” for clarity.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider using an elif for the remove-bookmark branch instead of two separate ifs so you don’t perform the DM guard check after the add-bookmark logic.
- Use == rather than is when comparing message.author to self.bot.user to avoid potential identity issues with discord.py model instances.
- Correct the comment typo to “Ensures we’re in a user’s DMs before removing…” for clarity.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@meatharvester meatharvester changed the title fix: Rnsure the bookmarks service will only delete messages in DMs fix: Ensure the bookmarks service will only delete messages in DMs Jul 26, 2025
@electron271 electron271 merged commit 0a8bc8e into main Jul 26, 2025
14 checks passed
@electron271 electron271 deleted the fix-bookmarks branch July 26, 2025 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants