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

refactor: improve edge case handling for recursion limits #22988

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

Conversation

haiyizxx
Copy link

@haiyizxx haiyizxx commented Dec 18, 2024

Description

Previously, checks for maxDepth and maxCalls.count only triggered when their values were strictly 0, which could lead to unintended behavior or bypassed validations if the surrounding code changes later on.

This PR updates the conditions to trigger for any non-positive values (<= 0), ensuring proper handling of edge cases even if validation was bypassed during one recursion step.


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features

    • Simulation of nested messages.
    • Support for importing hex keys via standard input.
    • Returning v2 server information in the version command.
    • Ability to overwrite client.toml and a helper method for broadcasting test transactions.
  • Improvements

    • Enhanced edge case handling for maximum depth and calls.
    • Upgraded RocksDB libraries to version 9.
    • Simplified testing frameworks.
  • Bug Fixes

    • Resolved issues with the CLI command for pruning.
    • Improved error handling for out-of-gas panics in gRPC query handlers.
  • API Breaking Changes

    • Removal of the testutil/network package and client prompt validations.

@haiyizxx haiyizxx requested a review from a team as a code owner December 18, 2024 18:17
Copy link
Contributor

coderabbitai bot commented Dec 18, 2024

Warning

Rate limit exceeded

@haiyizxx has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 10 minutes and 16 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 8ebd7d5 and 93b5285.

📒 Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough

Walkthrough

This pull request introduces several enhancements to the Cosmos SDK, focusing on improving functionality and error handling. Key changes include simulating nested messages, supporting hex key imports via standard input, and updating the version command to return v2 server information. The modifications also address edge case handling for recursion depth and call limits, simplify testing frameworks, and remove certain utility packages. The changes aim to enhance the SDK's robustness while maintaining backward compatibility.

Changes

File Change Summary
CHANGELOG.md - Added new features: nested message simulation, hex key import, v2 server info in version command
- Upgraded RocksDB libraries to version 9
- Simplified testing frameworks
- Improved edge case handling for maxDepth and maxCalls
- Documented removal of testutil/network and validate_client_prompt
codec/types/interface_registry.go - Modified UnpackAny method conditions for maximum recursion depth and call limits
- Changed depth check from maxDepth == 0 to maxDepth <= 0
- Updated call count check from maxCalls.count == 0 to maxCalls.count <= 0
codec/unknownproto/unknown_fields.go - Updated doRejectUnknownFields function recursion limit check
- Changed condition from recursionLimit == 0 to recursionLimit <= 0
x/tx/decode/unknown.go - Modified doRejectUnknownFields function recursion limit check
- Changed condition from recursionLimit == 0 to recursionLimit <= 0

Possibly related PRs

Suggested labels

C:x/tx, C:core/testing

Suggested reviewers

  • JulianToledano
  • akhilkumarpilli
  • sontrinh16
  • testinginprod
  • kocubinski

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.

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
CHANGELOG.md (3)

Line range hint 1-1: Add title and description at the top of changelog

The changelog would benefit from having a clear title and brief description at the top explaining its purpose and how to read/interpret the entries.

+# Changelog
+
+All notable changes to the Cosmos SDK will be documented in this file.
+
+The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
+and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
+

Line range hint 1-2000: Ensure consistent formatting of PR/Issue references

The changelog has inconsistent formatting of PR/Issue references - some use full URLs while others use just numbers. Consider standardizing on one format.

For example, standardize all references to use the short form #1234 rather than mixing with full URLs like https://github.com/cosmos/cosmos-sdk/pull/1234.


Line range hint 1-2000: Add release dates for all versions

Some version entries are missing their release dates. For historical tracking, all version headers should include their release date.

For example:

-## [v0.44.0]
+## [v0.44.0] - 2021-01-08
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 96cfd52 and 6930d4d.

📒 Files selected for processing (3)
  • CHANGELOG.md (1 hunks)
  • codec/types/interface_registry.go (1 hunks)
  • codec/unknownproto/unknown_fields.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
codec/types/interface_registry.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

codec/unknownproto/unknown_fields.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

CHANGELOG.md (1)

Pattern **/*.md: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"

🔇 Additional comments (3)
codec/types/interface_registry.go (2)

280-282: LGTM: Enhanced validation for maxCalls

The change properly handles edge cases by rejecting both zero and negative values for the call counter, maintaining consistency with the maxDepth check.


277-279: LGTM: Improved validation for maxDepth

The change strengthens the validation by rejecting both zero and negative values for maxDepth, which is a security improvement.

Let's verify there are no direct manipulations of maxDepth that could set it to negative values:

✅ Verification successful

Validation change for maxDepth is safe and well-implemented

The verification shows that maxDepth is safely managed:

  • Initialized with constant MaxUnpackAnyRecursionDepth = 10
  • Only decremented during recursion via cloneForRecursion()
  • No direct assignments that could set negative values
  • Used in a controlled manner within the statefulUnpacker struct
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any assignments to maxDepth
rg -p "maxDepth\s*=[^=]" --type go

Length of output: 36


Script:

#!/bin/bash
# Let's try a broader search for maxDepth usage and initialization
rg -p "maxDepth" --type go -C 2

# Also search for the struct field declaration
ast-grep --pattern 'type $_ struct {
  $$$
  maxDepth $_
  $$$
}'

Length of output: 1326


Script:

#!/bin/bash
# Let's check the MaxUnpackAnyRecursionDepth constant value and usage
rg -p "MaxUnpackAnyRecursionDepth" --type go -C 2

# Also check if there are any other places where maxDepth is modified
ast-grep --pattern '$_.maxDepth = $_'

Length of output: 525

codec/unknownproto/unknown_fields.go (1)

57-59: LGTM: Consistent improvement in recursion limit validation

The change aligns with the modifications in interface_registry.go and properly handles negative recursion limits, enhancing security.

Let's verify there are no places where recursionLimit could be set to negative values:

✅ Verification successful

LGTM: Recursion limit validation is properly implemented

The codebase search confirms that recursionLimit is:

  • Only used as a function parameter in message processing functions
  • Properly decremented (recursionLimit-1) when processing nested messages
  • Consistently checked with the new <= 0 condition
  • No direct assignments that could set it to negative values

The change is safe and improves the handling of invalid recursion limits.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for assignments to recursionLimit
rg -p "recursionLimit\s*=[^=]" --type go

Length of output: 42


Script:

#!/bin/bash
# Let's try a broader search for recursionLimit usage and declarations
rg -p "recursionLimit" --type go -C 2

# Also search for any function parameters or struct fields named recursionLimit
ast-grep --pattern 'struct {
  $$$
  recursionLimit $_
  $$$
}'

ast-grep --pattern 'func $_($$ recursionLimit $_) $_'

Length of output: 2095

@alpe
Copy link
Contributor

alpe commented Dec 19, 2024

👍 Can you update https://github.com/cosmos/cosmos-sdk/blob/main/x/tx/decode/unknown.go#L50 as well?

@julienrbrt julienrbrt added the backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release label Dec 19, 2024
Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

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

ACK, thanks

@haiyizxx haiyizxx changed the title refactor: improve edge case handling for maxDepth and maxCalls refactor: improve edge case handling for recursion limits Dec 19, 2024
@haiyizxx
Copy link
Author

👍 Can you update https://github.com/cosmos/cosmos-sdk/blob/main/x/tx/decode/unknown.go#L50 as well?

updated, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release C:x/tx
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants