sherlock-audit / 2024-05-andromeda-ado-judging

1 stars 0 forks source link

jollytesimal.eth - The test_execute_withdraw_native function: Improper Error Handling in execute_withdraw Leads to Panic (Error Handling Vulnerability). #40

Closed sherlock-admin3 closed 3 months ago

sherlock-admin3 commented 3 months ago

jollytesimal.eth

High

The test_execute_withdraw_native function: Improper Error Handling in execute_withdraw Leads to Panic (Error Handling Vulnerability).

Summary

Here is my clear bug summary :

In short explanation the bug is about improper error handling, which can cause the program to crash or behave unexpectedly if certain operations fail.

Vulnerability Detail

Vulnerability Details

The vulnerability exists in the test_execute_withdraw_native function, specifically in the use of the unwrap method to handle the result of operations that may return an error.

The affected code snippets are:

ADOContract::default().owner.save(deps.as_mut().storage, &Addr::unchecked(owner)).unwrap(); ADOContract::default().withdrawable_tokens.save(deps.as_mut().storage, "uusd", &AssetInfo::Native("uusd".into()),).unwrap(); let res = ADOContract::default().execute_withdraw(ExecuteContext::new(deps.as_mut(), info, mock_env()), Some(Recipient::from_string("address".to_string())), None,).unwrap();

The unwrap method is used to handle the result of the save and execute_withdraw operations. However if any of these operations fail, the unwrap method will panic and crash the program.

This vulnerability can lead to unexpected behavior, errors, or crashes, which can have serious consequences.

The root cause of this vulnerability is the assumption that the operations will always succeed without properly handling potential errors. So To fix this vulnerability, proper error handling should be implemented using match or ok_or to handle potential errors, instead of relying on unwrap. like it should be written in this way::::

match ADOContract::default().owner.save(deps.asmut().storage, &Addr::unchecked(owner)) { Ok() => {}, Err(error) => { // Handle the error } }

By using unwrap without proper error handling the code is exposed to potential errors and crashes, which can be exploited or lead to unexpected behavior.

Impact

The impact of this bug is:

Program Crash or Panic

If any of the operations protected by unwrap fail, the program will crash or panic and This can lead to:

The consequences of this bug include:

An attacker could potentially exploit this bug by:

Mitigation

To mitigate this bug proper error handling should be implemented using match or ok_or to handle potential errors, instead of relying on unwrap.

Code Snippet

https://github.com/sherlock-audit/2024-05-andromeda-ado/blob/main/andromeda-core/packages/std/src/ado_contract/withdraw.rs#L174-L193

Tool used

Manual Review

Recommendation

Replace the unwrap method with proper error handling mechanism using match or ok_or to handle potential errors.

My POC

Here is my runnable Proof of Concept (POC) to demonstrate the bug:

use cosmwasm_std::{storage, Addr};

fn main() { let mut deps = mock_dependencies_custom(&[]); let owner = Addr::unchecked("owner");

// Save owner address (will succeed)
ADOContract::default().owner.save(deps.as_mut().storage, &owner).unwrap();

// Try to save an invalid address (will fail)
let invalid_owner = Addr::unchecked("invalid_owner_with_a_very_long_name_that_exceeds_the_max_length");
let result = ADOContract::default().owner.save(deps.as_mut().storage, &invalid_owner);

// The bug: unwrap will panic if the operation fails
let _ = result.unwrap();

}

This POC will panic when run, demonstrating the bug. SO To fix the bug, replace unwrap with proper error handling such as:

match result { Ok(_) => {}, Err(error) => { println!("Error saving owner address: {}", error); } }