Skip to content

Conversation

@PoroFever
Copy link

Used to only delete own messages.
No can delete other users' messages (required group admin)
add SenderJID in payload

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @PoroFever, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances group chat moderation by empowering group administrators to remove messages sent by other participants. This is achieved by modifying the message deletion API to accept a sender identifier, which is then used by the backend to correctly revoke the message. This change provides a crucial tool for maintaining appropriate content within group conversations.

Highlights

  • Group Admin Message Deletion: Introduced the capability for group administrators to delete messages sent by other users within group chats. Previously, only self-deletion was supported.
  • Payload Enhancement: The DeleteMessage API now accepts an optional SenderJID field in its payload, allowing the specification of the message sender when deleting another user's message.
  • Backend Logic Update: The BuildRevoke function call within the DeleteMessage handler has been updated to utilize the new SenderJID from the request, enabling the deletion of messages from specified senders.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds the ability for group admins to delete messages from other users in a group chat by including a SenderJID in the payload. The changes are well-structured, but there's a critical security vulnerability: the code doesn't verify if the user making the request is actually a group admin, which contradicts the feature's intent. I've added a comment with a suggested fix for this. I've also included a minor suggestion to improve error handling.

Comment on lines +2346 to +2357
// Determine the sender JID: if provided, convert; otherwise use EmptyJID
var sender types.JID
if t.SenderJID != "" {
var parseErr error
sender, parseErr = types.ParseJID(t.SenderJID)
if parseErr != nil {
s.Respond(w, r, http.StatusBadRequest, errors.New("invalid SenderJID"))
return
}
} else {
sender = types.EmptyJID
}
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The current implementation introduces a critical security vulnerability. It allows any user to delete any message in a group chat by providing the SenderJID, without verifying if the user making the request is a group admin. The pull request description mentions this should be restricted to admins, but the check is missing in the code. You should add a check to verify that the user is a group admin before allowing the deletion of another user's message. Comparing user JIDs should be done carefully; using ToNonAD() is a robust way to compare user identities without device-specific information.

		// If SenderJID is provided, verify user is a group admin before proceeding.
		var sender types.JID
		if t.SenderJID != "" {
			// This is an attempt to delete someone else's message.
			// We must verify the chat is a group and the current user is an admin.
			if recipient.Server != types.GroupServer {
				s.Respond(w, r, http.StatusForbidden, errors.New("can only delete other users' messages in a group chat"))
				return
			}

			groupInfo, err := client.GetGroupInfo(context.Background(), recipient)
			if err != nil {
				s.Respond(w, r, http.StatusInternalServerError, errors.New(fmt.Sprintf("failed to get group info: %v", err)))
				return
			}

			currentUserJIDString := r.Context().Value("userinfo").(Values).Get("Jid")
			currentUserJID, err := types.ParseJID(currentUserJIDString)
			if err != nil {
				s.Respond(w, r, http.StatusInternalServerError, errors.New("could not parse current user JID"))
				return
			}

			isAdmin := false
			for _, p := range groupInfo.Participants {
				if p.JID.ToNonAD() == currentUserJID.ToNonAD() {
					if p.IsAdmin {
						isAdmin = true
					}
					break
				}
			}

			if !isAdmin {
				s.Respond(w, r, http.StatusForbidden, errors.New("you must be a group admin to delete other users' messages"))
				return
			}

			// User is admin, proceed to parse SenderJID
			var parseErr error
			sender, parseErr = types.ParseJID(t.SenderJID)
			if parseErr != nil {
				s.Respond(w, r, http.StatusBadRequest, errors.New("invalid SenderJID"))
				return
			}
		} else {
			// Deleting own message, no admin check needed.
			sender = types.EmptyJID
		}

client.BuildRevoke(recipient, sender, t.Id),
)
if err != nil {
s.Respond(w, r, http.StatusInternalServerError, errors.New(fmt.Sprintf("error sending message: %v", err)))
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For better error handling and to preserve the original error context, it's recommended to use fmt.Errorf with the %w verb to wrap errors instead of errors.New(fmt.Sprintf(...)). This allows for more robust error inspection up the call stack.

			s.Respond(w, r, http.StatusInternalServerError, fmt.Errorf("error sending message: %w", err))

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.

1 participant