Skip to content

Conversation

@amyulating
Copy link

@amyulating amyulating commented Mar 27, 2025

fixes #759

Summary by Sourcery

Enhance harmful command detection to provide more specific warning messages for different types of potentially destructive commands

Bug Fixes:

  • Improve the precision of harmful command detection by returning specific command types instead of a generic boolean

Enhancements:

  • Modify the is_harmful function to return specific types of harmful commands
  • Add targeted warning messages for different destructive command types like rm, fork bomb, dd, and format commands

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Mar 27, 2025

Reviewer's Guide by Sourcery

This pull request modifies the way harmful commands are handled. Instead of a single warning message, specific messages are now displayed based on the type of harmful command detected. The is_harmful function now returns a string indicating the type of command, and the handle_harmful_message function uses this string to send the appropriate warning.

Sequence diagram for handling harmful commands

sequenceDiagram
  participant User
  participant Discord Bot
  participant is_harmful Function

  User->>Discord Bot: Sends a message containing a command
  Discord Bot->>is_harmful Function: is_harmful(message.content)

  alt Command is RM_COMMAND
    is_harmful Function-->>Discord Bot: Returns "RM_COMMAND"
    Discord Bot->>User: Replies with RM_COMMAND warning
  else Command is FORK_BOMB
    is_harmful Function-->>Discord Bot: Returns "FORK_BOMB"
    Discord Bot->>User: Replies with FORK_BOMB warning
  else Command is DD_COMMAND
    is_harmful Function-->>Discord Bot: Returns "DD_COMMAND"
    Discord Bot->>User: Replies with DD_COMMAND warning
  else Command is FORMAT_COMMAND
    is_harmful Function-->>Discord Bot: Returns "FORMAT_COMMAND"
    Discord Bot->>User: Replies with FORMAT_COMMAND warning
  else Command is not harmful
    is_harmful Function-->>Discord Bot: Returns None
  end
Loading

File-Level Changes

Change Details Files
Modified the is_harmful function to return a string indicating the type of harmful command detected, rather than a boolean.
  • Changed the return type of the is_harmful function from bool to str.
  • Modified the function to return specific strings (e.g., FORK_BOMB, RM_COMMAND) based on the type of harmful command detected.
  • Updated the return statements to reflect the new string-based return values.
  • Added a None return statement if no harmful command is detected.
tux/utils/functions.py
Updated the handle_harmful_message function to send different warning messages based on the type of harmful command detected.
  • Modified the handle_harmful_message function to check the string returned by is_harmful.
  • Added conditional statements to send different warning messages based on the type of harmful command (RM, Fork Bomb, DD, Format).
  • Each command type now has a specific warning message.
tux/handlers/event.py

Assessment against linked issues

Issue Objective Addressed Explanation
#759 The bot should provide a warning when a user sends a potentially harmful command.
#759 The warning message should accurately describe the danger of the specific command.

Possibly linked issues


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!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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

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 @amyulating - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider using a dictionary or a match statement to map the harmful command type to the appropriate message.
  • It might be helpful to add a default case or error handling for when is_harmful returns None.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

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.

stripped_content = strip_formatting(message.content)

if is_harmful(stripped_content):
if is_harmful(stripped_content) == "RM_COMMAND":
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (performance): Avoid multiple evaluations of is_harmful.

The function is_harmful(stripped_content) is currently called repeatedly for each condition. Consider storing its result in a variable (e.g., harmful_type) and then using if/elif statements. This not only improves performance but also enhances code clarity by ensuring the check is only performed once.

Suggested implementation:

        stripped_content = strip_formatting(message.content)
        harmful_type = is_harmful(stripped_content)
        if harmful_type == "RM_COMMAND":
        elif harmful_type == "FORK_BOMB":
        elif harmful_type == "DD_COMMAND":
        elif harmful_type == "FORMAT_COMMAND":



def is_harmful(command: str) -> bool:
def is_harmful(command: str) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Align return type annotation with possible None return.

The function may return a string (e.g., "RM_COMMAND", "FORK_BOMB", etc.) or None. Updating the signature to use Optional[str] would more accurately reflect its behavior.

Suggested implementation:

def is_harmful(command: str) -> Optional[str]:
from typing import Optional

@electron271 electron271 merged commit 29d3383 into allthingslinux:main Mar 27, 2025
4 checks passed
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.

[BUG] - Misleading dangerous command warning for :(){ :|:& };:

2 participants