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

fix(claimsmanager): properly check index to avoid invalid data in UserClaims,UserLastEpochClaims #1234

Conversation

odeke-em
Copy link
Contributor

@odeke-em odeke-em commented Mar 5, 2024

This change ensures that we correctly check for missing values in keeper.(UserClaims, UserLastEpochClaims) because otherwise due to the blind assumption that the key would always be well formed, +1 and +1+1 were being added respectively which would mean that the condition "idx >= 0" would ALWAYS pass and send over the wrong data if the key happened to contain the address bytes.

Fixes #1217
Supersedes PR #1223

Summary by CodeRabbit

  • Refactor
    • Improved the accuracy of claims data processing by adjusting key index calculations and byte comparisons.

Copy link

vercel bot commented Mar 5, 2024

@odeke-em is attempting to deploy a commit to the joe-bowman's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Mar 5, 2024

Walkthrough

The recent update focuses on enhancing the robustness of the claimsmanager/keeper module by refining the handling of key indices and byte comparisons. This adjustment aims to prevent runtime panics and data corruption by ensuring that keys adhere to expected structures before processing. The modifications specifically target the accurate processing of claims data, addressing vulnerabilities by validating key structures and indices.

Changes

File(s) Change Summary
x/claimsmanager/keeper/grpc_query.go Adjusted index calculations and byte comparisons for UserClaims and UserLastEpochClaims functions.

Assessment against linked issues

Objective Addressed Explanation
Identify and address potential runtime panics by checking bytes.Index results (#1217)
Ensure bytes.Index returns are validated to prevent index out of bounds errors (#1217)
Implement checks for index values to prevent data corruption and runtime panics (#1217)
Mitigate risk of data corruption due to non-conforming key structures (#1217)
Enhance code robustness by validating key structures before processing (#1217)

Poem

In the realm of code, where bugs often hide,
A rabbit hopped in, with a leap and a stride.
With a twitch of its nose, and a flick of its ear,
It chased away panics, and the code became clear.

🐰 "Fear not," it whispered, "for I've found the clue,
Ensuring your keys, are structured true."
With a hop and a skip, it danced in delight,
For the code was now robust, and perfectly right.

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>.
    • Generate unit-tests 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 tests 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

…rClaims,UserLastEpochClaims

This change ensures that we correctly check for missing values in
keeper.(UserClaims, UserLastEpochClaims) because otherwise due
to the blind assumption that the key would always be well formed,
+1 and +1+1 were being added respectively which would mean that
the condition "idx >= 0" would ALWAYS pass and send over the wrong
data if the key happened to contain the address bytes.

Fixes quicksilver-zone#1217
Supersedes PR quicksilver-zone#1223
@odeke-em odeke-em force-pushed the x-claimsmanager-keeper-properly-skip-over-negative-indices branch from e024aed to 6f8f1c2 Compare March 5, 2024 15:15
@odeke-em
Copy link
Contributor Author

odeke-em commented Mar 5, 2024

/cc @joe-bowman @faddat @minhngoc274 please see this PR which fixes the issue. Thank you!

Copy link
Contributor

@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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between dd56067 and e024aed.
Files selected for processing (1)
  • x/claimsmanager/keeper/grpc_query.go (2 hunks)
Additional comments: 2
x/claimsmanager/keeper/grpc_query.go (2)
  • 72-80: The logic for calculating the index to check for the presence of address bytes in the key within the UserClaims function has been updated. While the adjustment to skip the first prefix byte (0x00) and the separator is correct, there are a few considerations:
  • Ensure that the length of key is always greater than idx+len(addrBytes) to avoid potential index out of bounds errors.
  • The addition of 1 + 1 in line 78 is correct in this context, as it accounts for the initial byte and the separator. However, adding a brief comment explaining why 2 is added (to skip the prefix and the separator) could enhance code readability.

However, consider adding a safety check for the key length and improving the comment for clarity.

  • 95-103: In the UserLastEpochClaims function, the logic for identifying the index of address bytes within the key has been modified. This change correctly accounts for the first byte being 0x01 and skips the separator with idx += 1. Similar considerations apply here:
  • It's crucial to ensure that the length of key is sufficient to prevent index out of bounds errors when accessing key[idx:idx+len(addrBytes)].
  • The rationale behind the index calculation and the assumption about the key structure could benefit from more detailed comments for future maintainability.

Recommend adding a length check for key and enhancing comments for better understanding.

Copy link
Contributor

@joe-bowman joe-bowman left a comment

Choose a reason for hiding this comment

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

Lgtm

Copy link
Contributor

@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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between dd56067 and 6f8f1c2.
Files selected for processing (1)
  • x/claimsmanager/keeper/grpc_query.go (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • x/claimsmanager/keeper/grpc_query.go

@joe-bowman joe-bowman merged commit e3864e0 into quicksilver-zone:main Mar 5, 2024
1 check failed
@odeke-em odeke-em deleted the x-claimsmanager-keeper-properly-skip-over-negative-indices branch March 5, 2024 22:38
@joe-bowman joe-bowman mentioned this pull request Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants