Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: check recipient address length #22

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

twothirtyfive
Copy link
Contributor

@twothirtyfive twothirtyfive commented Nov 18, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced error handling for message validation during transfer execution.
    • Added recipient address length validation in account registration and address queries.
  • Bug Fixes

    • Improved robustness of transfer execution by ensuring only valid messages are processed.

Copy link

coderabbitai bot commented Nov 18, 2024

Walkthrough

The changes in this pull request involve modifications to the Keeper struct and its methods across three files: keeper/keeper.go, keeper/msg_server.go, and keeper/query_server.go. Key updates include enhanced error handling and validation for recipient addresses in the RegisterAccount and Address methods, as well as improvements to the ExecuteForwards method to ensure only valid messages are processed. The overall structure and other methods remain unchanged, maintaining the existing functionality while increasing robustness through additional validation checks.

Changes

File Change Summary
keeper/keeper.go Updated ExecuteForwards for improved error handling and message validation.
No changes to SendRestrictionFn and SetIBCKeepers method signatures.
keeper/msg_server.go Added validation for Recipient address length in RegisterAccount method.
Retained existing error handling for Fallback address.
ClearAccount and SetAllowedDenoms methods remain unchanged.
keeper/query_server.go Introduced error handling for req.Recipient length in Address method.
Other methods (Denoms, Stats, StatsByChannel) remain unchanged.
.changelog/unreleased/improvements/22-recipient-length-check.md Added documentation for recipient address length validation functionality.

Poem

🐇 In the land of code, where bunnies hop,
New validations sprout, making errors stop.
With addresses checked, and transfers so bright,
Our Keeper stands strong, ready to take flight!
Hooray for the changes, so clever and neat,
In the garden of code, we dance on our feet! 🐇


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 56fa446 and 2298f2e.

📒 Files selected for processing (2)
  • .changelog/unreleased/improvements/22-recipient-length-check.md (1 hunks)
  • keeper/keeper.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • keeper/keeper.go
🔇 Additional comments (2)
.changelog/unreleased/improvements/22-recipient-length-check.md (2)

1-2: LGTM! The changelog entry is well-structured.

The entry clearly describes the improvement and includes the necessary context about the IBC limit.


1-2: Consider deferring the changelog until the PR is ready.

Since the PR is marked as WIP and currently failing e2e tests, it might be premature to add the changelog entry. Consider adding it once the implementation is complete and all tests are passing.

Let's check the PR status:


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (4)
keeper/query_server.go (1)

31-33: Consider using errors.Wrap for consistency

The error handling could be more consistent with the codebase style. Consider using errors.Wrap with a custom error type, similar to how other errors are handled in this file.

-		return nil, fmt.Errorf("recipient address must not exceed %d bytes", transfertypes.MaximumReceiverLength)
+		return nil, errors.Wrapf(errorstypes.ErrInvalidRequest, "recipient address must not exceed %d bytes", transfertypes.MaximumReceiverLength)
keeper/msg_server.go (1)

23-25: Consider adding minimum length validation.

While maximum length validation is good, consider also adding a minimum length check to ensure recipient addresses are not empty or too short to be valid.

+	if len(msg.Recipient) == 0 {
+		return nil, errors.New("recipient address cannot be empty")
+	}
 	if len(msg.Recipient) > transfertypes.MaximumReceiverLength {
 		return nil, fmt.Errorf("recipient address must not exceed %d bytes", transfertypes.MaximumReceiverLength)
 	}
keeper/keeper.go (2)

142-145: Good addition of validation, but consider enhancing error details

The addition of ValidateBasic() check is a good practice. However, the error logging could be more specific about which validation failed.

Consider enhancing the error logging:

-				k.Logger().Error("message validation failed", "channel", forward.Channel, "address", forward.GetAddress().String(), "amount", balance.String(), "err", err)
+				k.Logger().Error("transfer message validation failed", 
+					"channel", forward.Channel,
+					"from", forward.GetAddress().String(),
+					"to", forward.Recipient,
+					"amount", balance.String(),
+					"err", err)

Line range hint 146-154: Consider implementing retry mechanism for failed forwards

The TODO comment suggests implementing retries for failed forwards. This would improve reliability, especially for temporary failures.

Would you like me to help design and implement a retry mechanism using persistent storage? This could include:

  • Retry count tracking
  • Exponential backoff
  • Maximum retry limit
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 9d7657a and 56fa446.

📒 Files selected for processing (3)
  • keeper/keeper.go (2 hunks)
  • keeper/msg_server.go (1 hunks)
  • keeper/query_server.go (2 hunks)
🔇 Additional comments (3)
keeper/query_server.go (1)

31-33: Verify consistent usage of MaximumReceiverLength

Let's verify that the MaximumReceiverLength constant is used consistently across the codebase for recipient address validation.

✅ Verification successful

MaximumReceiverLength is consistently used for recipient validation

The verification shows that MaximumReceiverLength is used consistently in both query_server.go and msg_server.go for recipient address validation, with identical error messages. No other recipient length validations using different constants were found.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other usages of MaximumReceiverLength to ensure consistent validation
rg "MaximumReceiverLength" --type go

# Search for other recipient length validations that might use different constants
ast-grep --pattern 'if len($recipient) > $_' 

Length of output: 513

keeper/msg_server.go (1)

23-25: LGTM! Verify impact on existing integrations.

The validation check is well-placed and uses the standard IBC transfer module's maximum length constant. This is a good security improvement to prevent potential buffer overflow attacks.

Let's verify the constant usage and potential impact:

✅ Verification successful

Based on the search results, I can now provide a final response:

Length validation is consistently applied and uses IBC transfer module's standard limit

The validation check is correctly implemented and consistently used:

  • The same MaximumReceiverLength from transfertypes is used in both msg_server.go and query_server.go
  • The validation occurs early in request processing
  • The constant comes from the IBC transfer module which ensures compatibility with IBC standards
  • Error messages are clear and consistent
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for any existing recipient addresses that might exceed the new limit
ast-grep --pattern 'msg.Recipient' | rg -A 2 'msg.Recipient'

# Check if the same validation is consistently applied across the codebase
ast-grep --pattern $'if len($x) > transfertypes.MaximumReceiverLength'

Length of output: 130


Script:

#!/bin/bash
# Let's try a different approach to find recipient-related validations and usages

# Search for any recipient-related code
rg -i "recipient" -A 3 -B 1

# Look for length-related validations
rg -i "len.*recipient|recipient.*len" -A 2

# Check for imports of transfertypes to understand usage
rg "transfertypes"

# Find all validation patterns in msg_server.go
rg "if.*err|return.*err" keeper/msg_server.go -A 1

Length of output: 68252

keeper/keeper.go (1)

Line range hint 132-141: LGTM: Message construction looks good

The MsgTransfer construction includes all required fields with appropriate values:

  • Uses the transfer module's port ID
  • Sets proper timeout using the header service
  • Includes sender, receiver, and token information

Copy link
Member

@johnletey johnletey left a comment

Choose a reason for hiding this comment

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

Looking great so far @twothirtyfive! Just leaving a quick suggestion 😄

Could you also add a CHANGELOG entry using unclog!

keeper/keeper.go Outdated Show resolved Hide resolved
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.

2 participants