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

Merged
merged 8 commits into from
Jan 6, 2025

Conversation

haiyizxx
Copy link
Contributor

@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.
  • Bug Fixes

    • Resolved issues with pagination in queries.
    • Improved error handling for out-of-gas panics in gRPC query handlers.
  • API Breaking Changes

    • Removal of the x/params module and the testutil/network package.
    • Elimination of client prompt validations.
    • Deprecation of the Invariants and associated methods.

@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

📝 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
- Documented removal of testutil/network and validate_client_prompt
- Highlighted API breaking changes and deprecations
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/accounts, C:x/accounts/base

Suggested reviewers

  • kocubinski
  • julienrbrt
  • sontrinh16
  • tac0turtle
  • testinginprod
  • aaronc
  • facundomedica

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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
Contributor Author

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

updated, thanks

@aljo242 aljo242 enabled auto-merge January 3, 2025 16:42
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 (1)
CHANGELOG.md (1)

51-51: Fix spelling error in changelog

Change "recusion" to "recursion" in the line describing recursion depth limits.

- * Fix [ABS-0043/ABS-0044](https://github.com/cosmos/cosmos-sdk/security/advisories/GHSA-8wcc-m6j2-qxvm) Limit recusion depth for unknown field detection and unpack any
+ * Fix [ABS-0043/ABS-0044](https://github.com/cosmos/cosmos-sdk/security/advisories/GHSA-8wcc-m6j2-qxvm) Limit recursion depth for unknown field detection and unpack any
🧰 Tools
🪛 GitHub Actions: Spell Check

[warning] 51-51: Spelling error: 'recusion' should be 'recursion' or 'reclusion'

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8ebd7d5 and 41bd596.

📒 Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
CHANGELOG.md (1)

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

🪛 GitHub Actions: Spell Check
CHANGELOG.md

[warning] 51-51: Spelling error: 'recusion' should be 'recursion' or 'reclusion'

auto-merge was automatically disabled January 3, 2025 21:04

Head branch was pushed to by a user without write access

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 (2)
CHANGELOG.md (2)

Line range hint 1-2000: Improve changelog formatting and organization

The changelog has good content but could benefit from some improvements:

  1. Use consistent bullet point style - currently mixes * and -
  2. Add missing version comparison links at the bottom of each release section
  3. Consider grouping breaking changes by impact (client, API, state machine) more consistently across all versions

Example format for version links:

+ [v0.47.0]: https://github.com/cosmos/cosmos-sdk/compare/v0.46.0...v0.47.0
+ [v0.46.0]: https://github.com/cosmos/cosmos-sdk/compare/v0.45.0...v0.46.0

Line range hint 1-50: Add summary section at the top

Consider adding a high-level summary section at the top of the changelog to highlight the most important changes across versions. This helps users quickly understand major changes.

Example:

# Changelog

All notable changes to the Cosmos SDK will be documented in this file.

## Summary

This changelog covers all releases from v0.42.0 onwards. Major highlights include:

- Migration to Protobuf for state serialization
- IBC implementation
- Stargate upgrade support
- Multiple stability and performance improvements
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 41bd596 and bbf4379.

📒 Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • x/tx/decode/unknown.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • x/tx/decode/unknown.go
🧰 Additional context used
📓 Path-based instructions (1)
CHANGELOG.md (1)

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

@julienrbrt julienrbrt enabled auto-merge January 6, 2025 08:11
@julienrbrt julienrbrt added this pull request to the merge queue Jan 6, 2025
Merged via the queue into cosmos:main with commit 93282e1 Jan 6, 2025
67 of 69 checks passed
mergify bot pushed a commit that referenced this pull request Jan 6, 2025
Co-authored-by: Alex | Skip <alex@skip.money>
(cherry picked from commit 93282e1)

# Conflicts:
#	CHANGELOG.md
#	x/tx/decode/unknown.go
julienrbrt added a commit that referenced this pull request Jan 6, 2025
…22988) (#23191)

Co-authored-by: haiyizxx <haiyizxx@gmail.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
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.

4 participants