Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion tux/handlers/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,27 @@ async def handle_harmful_message(message: discord.Message) -> None:
return

stripped_content = strip_formatting(message.content)
harmful = is_harmful(stripped_content)

if is_harmful(stripped_content):
if harmful == "RM_COMMAND":
await message.reply(
"-# ⚠️ **This command is likely harmful. By running it, all directory contents will be deleted. There is no undo. Ensure you fully understand the consequences before proceeding. If you have received this message in error, please disregard it.**",
)
return
if harmful == "FORK_BOMB":
await message.reply(
"-# ⚠️ **This command is likely harmful. By running it, all the memory in your system will be used. Ensure you fully understand the consequences before proceeding. If you have received this message in error, please disregard it.**",
)
return
if harmful == "DD_COMMAND":
await message.reply(
"-# ⚠️ **This command is likely harmful. By running it, your disk will be overwritten or erased irreversibly. Ensure you fully understand the consequences before proceeding. If you have received this message in error, please disregard it.**",
)
return
if harmful == "FORMAT_COMMAND":
await message.reply(
"-# ⚠️ **This command is likely harmful. By running it, your disk will be formatted. Ensure you fully understand the consequences before proceeding. If you have received this message in error, please disregard it.**",
)

@commands.Cog.listener()
async def on_message_edit(self, before: discord.Message, after: discord.Message) -> None:
Expand Down
12 changes: 7 additions & 5 deletions tux/utils/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
FORMAT_COMMANDS = r"mkfs\..*\s+/dev/([hs]d[a-z]|nvme\d+n\d+)"


def is_harmful(command: str) -> bool:
def is_harmful(command: str) -> str | None:
# sourcery skip: assign-if-exp, boolean-if-exp-identity, reintroduce-else
"""
Check if a command is potentially harmful to the system.
Expand All @@ -44,18 +44,20 @@ def is_harmful(command: str) -> bool:
# Normalize command by removing all whitespace for fork bomb check
normalized = "".join(command.strip().lower().split())
if normalized in FORK_BOMB_PATTERNS:
return True
return "FORK_BOMB"

# Check for dangerous rm commands
if re.search(DANGEROUS_RM_COMMANDS, command, re.IGNORECASE):
return True
return "RM_COMMAND"

# Check for dangerous dd commands
if re.search(DANGEROUS_DD_COMMANDS, command, re.IGNORECASE):
return True
return "DD_COMMAND"

# Check for format commands
return bool(re.search(FORMAT_COMMANDS, command, re.IGNORECASE))
if bool(re.search(FORMAT_COMMANDS, command, re.IGNORECASE)):
return "FORMAT_COMMAND"
return None


def strip_formatting(content: str) -> str:
Expand Down