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

arbitrary-send-eth: Don't report if destination is immutable state var #2488

Merged
merged 1 commit into from
Jun 24, 2024

Conversation

smonicas
Copy link
Contributor

@smonicas smonicas commented Jun 21, 2024

Summary by CodeRabbit

  • New Features

    • Introduced send_immutable function in smart contracts to send ETH to an immutable address.
  • Bug Fixes

    • Updated analysis to skip cases where the destination is an immutable state variable.
  • Tests

    • Modified test scenarios to reflect changes in ETH transfer methods and immutable variables.

Copy link

coderabbitai bot commented Jun 21, 2024

Walkthrough

Walkthrough

The updates enhance the arbitrary_send_eth.py detector by excluding cases involving immutable state variables. Corresponding Solidity test files were modified to add an immutable variable and incorporate it in new functions. The test scenarios were also adjusted to align with the detector improvements.

Changes

Files (Grouped by Similar Changes) Change Summary
slither/detectors/functions/arbitrary_send_eth.py Enhanced the detector to skip analysis for immutable state variable destinations. Added import for StateVariable.
tests/e2e/detectors/test_data/arbitrary-send-eth/0.6.11/arbitrary_send_eth.sol, Added immutable variable destination_imm and function send_immutable to test handling of immutable variables.
tests/e2e/detectors/test_data/arbitrary-send-eth/0.7.6/arbitrary_send_eth.sol Added immutable variable destination_imm and function send_immutable to test handling of immutable variables.
tests/e2e/detectors/snapshots/detectors__detector_ArbitrarySendEth_0_6_11_arbitrary_send_eth_sol__0.txt, Modified test case methods Test.indirect() and Test.direct() to adjust ETH sending scenarios and calls.
tests/e2e/detectors/snapshots/detectors__detector_ArbitrarySendEth_0_7_6_arbitrary_send_eth_sol__0.txt Updated line numbers and calls in test methods Test.direct() and Test.indirect() for ETH sending scenarios.

Sequence Diagram(s)

N/A

Possibly related issues

  • Arbitrary-send-eth false positive  #1225:
    • The enhancement addresses false positives in detecting "arbitrary-send-eth" for immutable state variables, aligning with the issue's objective of refining detection accuracy.

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

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.
  • 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 (1)
slither/detectors/functions/arbitrary_send_eth.py (1)

Line range hint 50-51: Refactor suggestion: Simplify nested if statements.

The static analysis tool suggests using a single if statement instead of multiple nested ones. This can improve readability and reduce complexity.

- if isinstance(ir, (HighLevelCall)):
-     if isinstance(ir.function, Function):
-         if ir.function.full_name == "transferFrom(address,address,uint256)":
-             return False
+ if isinstance(ir, HighLevelCall) and isinstance(ir.function, Function) and ir.function.full_name == "transferFrom(address,address,uint256)":
+     return False

Also applies to: 63-65, 64-65, 80-81

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 02df0dc and 469286f.

Files ignored due to path filters (2)
  • tests/e2e/detectors/test_data/arbitrary-send-eth/0.6.11/arbitrary_send_eth.sol-0.6.11.zip is excluded by !**/*.zip
  • tests/e2e/detectors/test_data/arbitrary-send-eth/0.7.6/arbitrary_send_eth.sol-0.7.6.zip is excluded by !**/*.zip
Files selected for processing (5)
  • slither/detectors/functions/arbitrary_send_eth.py (2 hunks)
  • tests/e2e/detectors/snapshots/detectors__detector_ArbitrarySendEth_0_6_11_arbitrary_send_eth_sol__0.txt (1 hunks)
  • tests/e2e/detectors/snapshots/detectors__detector_ArbitrarySendEth_0_7_6_arbitrary_send_eth_sol__0.txt (1 hunks)
  • tests/e2e/detectors/test_data/arbitrary-send-eth/0.6.11/arbitrary_send_eth.sol (1 hunks)
  • tests/e2e/detectors/test_data/arbitrary-send-eth/0.7.6/arbitrary_send_eth.sol (1 hunks)
Additional context used
Ruff
slither/detectors/functions/arbitrary_send_eth.py

50-51: Use a single if statement instead of nested if statements (SIM102)


63-65: Use a single if statement instead of nested if statements (SIM102)


64-65: Use a single if statement instead of nested if statements (SIM102)


80-81: Use a single if statement instead of nested if statements (SIM102)

Additional comments not posted (12)
tests/e2e/detectors/snapshots/detectors__detector_ArbitrarySendEth_0_7_6_arbitrary_send_eth_sol__0.txt (2)

3-3: Change in recipient of transaction in Test.direct() method.

The method now sends ETH to the caller (msg.sender) instead of a predefined destination. This change should be clearly documented and justified, as it fundamentally alters the behavior of the function.


7-7: Inconsistency in handling of destination in Test.indirect() method.

The method continues to use the old destination.send(address(this).balance) method. This may be inconsistent with the new approach in Test.direct() and could lead to confusion or errors. Please verify if this is intentional or an oversight.

tests/e2e/detectors/snapshots/detectors__detector_ArbitrarySendEth_0_6_11_arbitrary_send_eth_sol__0.txt (2)

3-3: Change in recipient of transaction in Test.direct() method.

The method now sends ETH to the caller (msg.sender) instead of a predefined destination. This change should be clearly documented and justified, as it fundamentally alters the behavior of the function.


7-7: Inconsistency in handling of destination in Test.indirect() method.

The method continues to use the old destination.send(address(this).balance) method. This may be inconsistent with the new approach in Test.direct() and could lead to confusion or errors. Please verify if this is intentional or an oversight.

tests/e2e/detectors/test_data/arbitrary-send-eth/0.6.11/arbitrary_send_eth.sol (3)

4-4: Introduction of immutable destination variable destination_imm.

This change enhances security by making the destination address constant after deployment, preventing unauthorized changes. It aligns with best practices for handling critical addresses in smart contracts.


8-8: Initialization of destination_imm in the constructor.

The constructor initializes destination_imm with msg.sender, setting the contract deployer as the default destination. This decision should be clearly documented to ensure clarity.


12-14: New method send_immutable() correctly uses the immutable destination.

This method sends the contract’s balance to the destination_imm. The use of the immutable variable here ensures that the destination cannot be altered, enhancing the security of the transaction.

tests/e2e/detectors/test_data/arbitrary-send-eth/0.7.6/arbitrary_send_eth.sol (3)

4-4: Introduction of immutable destination variable destination_imm.

This change enhances security by making the destination address constant after deployment, preventing unauthorized changes. It aligns with best practices for handling critical addresses in smart contracts.


8-8: Initialization of destination_imm in the constructor.

The constructor initializes destination_imm with msg.sender, setting the contract deployer as the default destination. This decision should be clearly documented to ensure clarity.


12-14: New method send_immutable() correctly uses the immutable destination.

This method sends the contract’s balance to the destination_imm. The use of the immutable variable here ensures that the destination cannot be altered, enhancing the security of the transaction.

slither/detectors/functions/arbitrary_send_eth.py (2)

33-33: Import added: StateVariable from slither.core.variables.state_variable.

This import is necessary for the new logic to check if a destination is an immutable state variable. Ensure that StateVariable is utilized properly in the rest of the code.


71-72: Optimization in conditional checks for immutable state variables.

The addition of a check for immutable state variables (ir.destination.is_immutable) is a crucial update to ensure that the detector does not report false positives when the destination is a state variable marked as immutable.

Verification successful

The search results confirm that the is_immutable attribute is used in the context of state variables. The specific line in slither/detectors/functions/arbitrary_send_eth.py that includes the check for is_immutable is also found, verifying that the new logic is in place.

  • slither/detectors/functions/arbitrary_send_eth.py: if isinstance(ir.destination, StateVariable) and ir.destination.is_immutable:

This confirms that the new check correctly identifies immutable state variables and skips them, ensuring the detector does not report false positives.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the new check correctly identifies immutable state variables and skips them.

# Test: Search for usage of `is_immutable` in the context of state variables.
rg --type python $'is_immutable'

Length of output: 62



Script:

#!/bin/bash
# Description: Verify that the new check correctly identifies immutable state variables and skips them.

# Correct the file type and search for usage of `is_immutable` in the Python codebase.
rg 'is_immutable' --type py

Length of output: 1566

@0xalpharush 0xalpharush merged commit f3fbcdc into dev Jun 24, 2024
83 checks passed
@0xalpharush 0xalpharush deleted the dev-arb-send-eth-immutable branch June 24, 2024 13:46
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