Skip to content

fix: require ManageMessages permission to reset another user's context - #49

Merged
calebephrem merged 3 commits into
open-devhub:mainfrom
goldstac:fix/resetctx-perms
Sep 4, 2026
Merged

calebephrem merged 3 commits into
open-devhub:mainfrom
goldstac:fix/resetctx-perms

Conversation

@goldstac

@goldstac goldstac commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds a permission check to the command so that only users with Manage Messages permission can reset someone else's conversation context. Anyone can still reset their own context.

Why

Previously any user could reset any other user's context by mentioning them, which is an abuse vector.

Changes

  • src/commands/ai/resetctx.ts: Check ManageMessages permission before allowing reset of another user's context

Behavior

  • : resets your own (always works)
  • @user: requires Manage Messages permission, otherwise replies with error

@devhub-bot devhub-bot Bot added the fix Bug fix label Sep 4, 2026
@beetle-ai

beetle-ai Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary by Beetle

This PR implements a critical security fix for the resetctx command in a Discord bot. The change adds permission-based access control to prevent unauthorized users from resetting other users' conversation contexts. Previously, any user could reset another user's AI context if they mentioned them, which was a security vulnerability. Now, only users with the Manage Messages permission can reset someone else's context, while users can always reset their own context without restrictions.

📁 File Changes Summary (Consolidated across all commits):

File Status Changes Description
src/commands/ai/resetctx.ts Modified +17/-4 Added permission validation to require ManageMessages permission when resetting another user's context. Removed unused args parameter. Introduced isSelf flag for cleaner logic flow and improved code readability.

Total Changes: 1 file changed, +17 additions, -4 deletions

🎯 Key Changes:

  • Permission Check Added: Implemented validation using Discord's PermissionFlagsBits.ManageMessages to restrict context resets for other users
  • Self-Reset Exemption: Users can always reset their own context without requiring any special permissions
  • Improved Logic: Introduced isSelf boolean flag to eliminate redundant checks and improve code clarity
  • Removed Unused Parameter: Cleaned up the args parameter from the command callback since it wasn't being used
  • User Feedback: Added clear error message informing users when they lack the required permission
  • Code Quality: Refactored conditional logic to be more maintainable and easier to understand

📊 Impact Assessment:

  • Security: ✅ Significant Improvement - This fix closes a security vulnerability by preventing unauthorized users from manipulating other users' AI conversation contexts. The permission check ensures only moderators/admins (users with Manage Messages permission) can reset others' contexts, protecting user data integrity.
  • Performance: ✅ Neutral - The added permission check is a simple boolean operation with negligible performance impact. The removal of unused args parameter slightly reduces memory overhead.
  • Maintainability: ✅ Improved - The introduction of the isSelf flag makes the code more readable and reduces cognitive load. The logic flow is now clearer with explicit permission validation separated from the context reset logic. Future developers will easily understand the permission requirements.
  • Testing: ⚠️ Requires Attention - This change should be tested with:
  • Users with Manage Messages permission resetting other users' contexts (should succeed)
  • Users without Manage Messages permission attempting to reset other users' contexts (should fail with error message)
  • All users resetting their own context (should always succeed)
  • Bot users attempting the command (should be ignored as before)
⚙️ Settings

Severity Threshold: Medium — Balanced feedback — medium and high severity issues only. — Change in Settings
Custom Rules: Define your own review rules — Set Custom Rules
PR Summary: Configure PR summary — Change in Settings

📖 User Guide
  • Once repos are connected, PR analysis is automatically enabled. You can disable analysis for this repo from beetleai.dev/analysis
  • Comment @beetle on any PR to start analysis manually
  • Comment @beetle stop to stop any ongoing analysis

Follow us: Beetle · X · LinkedIn

Comment thread src/commands/ai/resetctx.ts Outdated
Comment on lines +16 to +18
const hasPerms = message.member?.permissions.has(
PermissionFlagsBits.ManageMessages,
);

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.

The permission check uses optional chaining but doesn't explicitly handle the null case. If message.member is null, the expression evaluates to undefined, which makes !hasPerms truthy on line 19, incorrectly denying permission. This could cause false negatives in edge cases.

Confidence: 4/5

Suggested Fix
Suggested change
const hasPerms = message.member?.permissions.has(
PermissionFlagsBits.ManageMessages,
);
const hasPerms = message.member?.permissions.has(
PermissionFlagsBits.ManageMessages,
) ?? false;

Use the nullish coalescing operator (??) to explicitly default to false when message.member is null or undefined. This ensures the permission check behaves predictably in all scenarios.

Prompt for AI

Copy this prompt to your AI IDE to fix this issue locally:

In src/commands/ai/resetctx.ts around line 16-18, the permission check uses optional chaining
but doesn't handle the null case explicitly. When message.member is null, hasPerms becomes
undefined instead of false, causing the permission denial logic to trigger incorrectly.
Add the nullish coalescing operator (?? false) to ensure hasPerms defaults to false when
message.member is null or undefined.

📍 This suggestion applies to lines 16-18

@devhub-bot

devhub-bot Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Note

Linting checks passed successfully 🎉

All formatting and code quality checks are clean.

You're good to merge 🚀

@beetle-ai

beetle-ai Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary by Beetle

This PR fixes a potential null reference bug in the resetctx command's permission check logic. The change addresses a scenario where message.member could be null or undefined, which would cause the permission check to fail silently or throw an error. The fix implements a nullish coalescing operator (??) to explicitly default to false when the member object or its permissions are unavailable, ensuring predictable behavior and preventing unauthorized access.

📁 File Changes Summary (Consolidated across all commits):

File Status Changes Description
src/commands/ai/resetctx.ts Modified +3/-3 Fixed null reference handling in permission check by adding nullish coalescing operator (??) to default to false when message.member is null/undefined, preventing potential runtime errors and ensuring secure permission validation

Total Changes: 1 file changed, +3 additions, -3 deletions

🎯 Key Changes:

  • Permission Check Hardening: Changed from optional chaining alone (message.member?.permissions.has(...)) to optional chaining with nullish coalescing (message.member?.permissions.has(...) ?? false), ensuring the permission check always returns a boolean value
  • Null Safety: Explicitly handles the case where message.member is null or undefined by defaulting to false, preventing undefined behavior
  • Security Improvement: Ensures that users without a valid member object cannot bypass permission checks by defaulting to deny access

📊 Impact Assessment:

  • Security: ✅ Positive Impact - This fix prevents a potential security vulnerability where a null message.member could result in undefined permission behavior. By explicitly defaulting to false, the code ensures that permission checks fail securely (deny by default), preventing unauthorized context resets.
  • Performance: ✅ Neutral - The change has negligible performance impact. The nullish coalescing operator is a simple runtime check with no measurable overhead.
  • Maintainability: ✅ Improved - The explicit ?? false makes the intent clearer to future maintainers. It's immediately obvious that the permission check defaults to deny access when the member object is unavailable, improving code readability and reducing cognitive load.
  • Testing: ⚠️ Consideration - This fix should be validated with test cases covering:
  • Scenarios where message.member is null
  • Scenarios where message.member is undefined
  • Scenarios where message.member.permissions is unavailable
  • Verification that unauthorized users are properly denied access to reset others' contexts
⚙️ Settings

Severity Threshold: Medium — Balanced feedback — medium and high severity issues only. — Change in Settings
Custom Rules: Define your own review rules — Set Custom Rules
PR Summary: Configure PR summary — Change in Settings

📖 User Guide
  • Once repos are connected, PR analysis is automatically enabled. You can disable analysis for this repo from beetleai.dev/analysis
  • Comment @beetle on any PR to start analysis manually
  • Comment @beetle stop to stop any ongoing analysis

Follow us: Beetle · X · LinkedIn

@beetle-ai

beetle-ai Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

✅ You're good to merge this PR! No issues found. Great job!

Settings
⚙️ Settings

Severity Threshold: Medium — Balanced feedback — medium and high severity issues only. — Change in Settings
Custom Rules: Define your own review rules — Set Custom Rules
PR Summary: Configure PR summary — Change in Settings

📖 User Guide
  • Once repos are connected, PR analysis is automatically enabled. You can disable analysis for this repo from beetleai.dev/analysis
  • Comment @beetle on any PR to start analysis manually
  • Comment @beetle stop to stop any ongoing analysis

@devhub-bot

devhub-bot Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Note

Linting checks passed successfully 🎉

All formatting and code quality checks are clean.

You're good to merge 🚀

@goldstac

goldstac commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

@calebephrem :3

@calebephrem

Copy link
Copy Markdown
Member

I don't think this's right, someone should be abled to reset only his context (nomatter what permission the user has)

@goldstac

goldstac commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

I don't think this's right, someone should be abled to reset only his context (nomatter what permission the user has)

well cant mods reset someones context

@calebephrem

Copy link
Copy Markdown
Member

no

@goldstac

goldstac commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

no

yap yap yap sahur ok :3

@goldstac

goldstac commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

give me 10 to 11 business hours

@beetle-ai

beetle-ai Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary by Beetle

This PR implements a security fix that restricts the resetctx command to only allow users to reset their own conversation context. Previously, the command allowed users with "Manage Messages" permission to reset context for other users. This change simplifies the permission model and prevents unauthorized context manipulation by enforcing a strict self-only policy.

📁 File Changes Summary (Consolidated across all commits):

File Status Changes Description
src/commands/ai/resetctx.ts Modified +7/-24 Removed permission-based context reset for other users. Now only allows users to reset their own conversation context. Simplified logic by removing the PermissionFlagsBits import, permission checks, and conditional response messages. The command now rejects any attempt to reset another user's context with a clear error message.

Total Changes: 1 file changed, +7 additions, -24 deletions

🎯 Key Changes:

  • Removed Permission-Based Access: Eliminated the PermissionFlagsBits.ManageMessages permission check that previously allowed moderators to reset other users' contexts
  • Enforced Self-Only Policy: Added validation to reject any mention of another user, returning an error message: "You can only reset your own context."
  • Simplified Command Logic: Removed conditional branching for self vs. other user scenarios, reducing code complexity from 37 lines to 20 lines
  • Removed Unused Import: Deleted the discord.js PermissionFlagsBits import as it's no longer needed
  • Updated Description: Changed command description from "Reset conversation context for a user" to "Reset your conversation context" to reflect the new self-only behavior

📊 Impact Assessment:

  • Security: ✅ Positive Impact - This change significantly improves security by preventing unauthorized context manipulation. Users can no longer have their conversation history reset by other users, even with elevated permissions. This protects user privacy and prevents potential abuse of the context reset feature.
  • Performance: ✅ Neutral/Positive Impact - The simplified logic with fewer conditional branches and removed permission checks results in slightly faster execution. The command now performs fewer operations per invocation.
  • Maintainability: ✅ Positive Impact - Code complexity is reduced by ~46% (37 → 20 lines). The removal of permission-based logic and conditional branching makes the code more straightforward and easier to understand. Future maintainers will have a clearer intent: this command is strictly for personal use.
  • Testing: ⚠️ Requires Attention - Existing tests that verify permission-based context reset for other users will need to be updated or removed. New tests should verify that the command rejects attempts to reset other users' contexts. Test coverage should confirm that only the command invoker's context is reset.
⚙️ Settings

Severity Threshold: Medium — Balanced feedback — medium and high severity issues only. — Change in Settings
Custom Rules: Define your own review rules — Set Custom Rules
PR Summary: Configure PR summary — Change in Settings

📖 User Guide
  • Once repos are connected, PR analysis is automatically enabled. You can disable analysis for this repo from beetleai.dev/analysis
  • Comment @beetle on any PR to start analysis manually
  • Comment @beetle stop to stop any ongoing analysis

Follow us: Beetle · X · LinkedIn

@devhub-bot

devhub-bot Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Note

Linting checks passed successfully 🎉

All formatting and code quality checks are clean.

You're good to merge 🚀

@beetle-ai

beetle-ai Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

✅ You're good to merge this PR! No issues found. Great job!

Settings
⚙️ Settings

Severity Threshold: Medium — Balanced feedback — medium and high severity issues only. — Change in Settings
Custom Rules: Define your own review rules — Set Custom Rules
PR Summary: Configure PR summary — Change in Settings

📖 User Guide
  • Once repos are connected, PR analysis is automatically enabled. You can disable analysis for this repo from beetleai.dev/analysis
  • Comment @beetle on any PR to start analysis manually
  • Comment @beetle stop to stop any ongoing analysis

@calebephrem calebephrem left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

Comment on lines +11 to +16
const mentioned = message.mentions.users.first();
if (mentioned && mentioned.id !== message.author.id) {
await message.reply("You can only reset your own context.");
return;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ion think this part is necessary but informative ig

@goldstac

goldstac commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

MERGE

@calebephrem

Copy link
Copy Markdown
Member

lol alr

@calebephrem
calebephrem merged commit ebbf53e into open-devhub:main Sep 4, 2026
4 checks passed
@calebephrem

Copy link
Copy Markdown
Member

thx for contributing :3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants