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

πŸ›‘οΈ Insufficient Error Handling in Object Access Within Objectarium #561

Closed
ccamel opened this issue May 23, 2024 · 4 comments
Closed
Assignees
Labels
security audit Categorizes an issue or PR as relevant to Security Audit

Comments

@ccamel
Copy link
Member

ccamel commented May 23, 2024

Note

Severity: Info
target: v5.0.0 - Commit: cde785fbd2dad71608d53f8524e0ef8c8f8178af
Ref: OKP4 CosmWasm Audit Report v1.0 - 02-05-2024 - BlockApex

Description

The pin_object function in the Objectarium contract lacks preemptive checks to confirm the existence of an object before attempting operations on it. This oversight leads to attempts to manipulate non-existent objects, resulting in generic errors which are not descriptive of the actual issue. This issue is also evident in similar operations, such as forget_object, where object existence is assumed rather than verified before proceeding with further logic.

Recommendation

Implement and enforce existence checks before performing any operations on objects within the contract.

@ccamel ccamel added the security audit Categorizes an issue or PR as relevant to Security Audit label May 23, 2024
@github-project-automation github-project-automation bot moved this to πŸ“‹ Backlog in πŸ’» Development May 23, 2024
@ccamel ccamel moved this from πŸ“‹ Backlog to πŸ“† To do in πŸ’» Development May 23, 2024
@bdeneux bdeneux self-assigned this May 31, 2024
@bdeneux
Copy link
Contributor

bdeneux commented May 31, 2024

πŸ”Ž Analysis

πŸ“Œ pin_object

The current implementation of the pin_object message already includes implicit error handling for non-existing or invalid object_id. This is managed through storage querying, and the unit tests already verify this behavior. When an error occurs, it returns a StdError::not_found error through ContractError::Std.

let object = objects().load(deps.storage, id.clone())?;

TC {
// Object not exists
objects: vec![ObjectId::from(
"abafa4428bdc8c34dae28bbc17303a62175f274edf59757b3e9898215a428a56",
)],
senders: vec![mock_info("bob", &[])],
expected_count: 0,
expected_error: Some(ContractError::Std(StdError::not_found(
not_found_object_info::<Object>(
"abafa4428bdc8c34dae28bbc17303a62175f274edf59757b3e9898215a428a56",
),
))),
expected_object_pin_count: vec![(
ObjectId::from(
"315d0d9ab12c5f8884100055f79de50b72db4bd2c9bfd3df049d89640fed1fa6",
),
Uint128::zero(),
)],
},

πŸ“Œ unpin_object

The unpin_object message follows a similar pattern, with error handling for non-existing or invalid object_id.

TC {
// Object not exists
pin: vec![ObjectId::from(
"315d0d9ab12c5f8884100055f79de50b72db4bd2c9bfd3df049d89640fed1fa6",
)],
pin_senders: vec![mock_info("bob", &[])],
unpin: vec![ObjectId::from(
"abafa4428bdc8c34dae28bbc17303a62175f274edf59757b3e9898215a428a56",
)],
unpin_senders: vec![mock_info("martin", &[])],
expected_count: 1,
expected_error: Some(ContractError::Std(StdError::not_found(
not_found_object_info::<Object>(
"abafa4428bdc8c34dae28bbc17303a62175f274edf59757b3e9898215a428a56",
),
))),
expected_object_pin_count: vec![(
ObjectId::from(
"315d0d9ab12c5f8884100055f79de50b72db4bd2c9bfd3df049d89640fed1fa6",
),
Uint128::one(),
)],
},

πŸ—‘ forget_object

The forget_object message also includes error handling for non-existing or invalid object_id.

TC {
pins: vec![
ObjectId::from(
"315d0d9ab12c5f8884100055f79de50b72db4bd2c9bfd3df049d89640fed1fa6",
),
ObjectId::from(
"315d0d9ab12c5f8884100055f79de50b72db4bd2c9bfd3df049d89640fed1fa6",
),
],
pins_senders: vec![mock_info("bob", &[]), mock_info("alice", &[])],
forget_objects: vec![ObjectId::from(
"abafa4428bdc8c34dae28bbc17303a62175f274edf59757b3e9898215a428a56",
)],
forget_senders: vec![mock_info("bob", &[])], // the sender is the same as the pinner, but another pinner is on it so error
expected_count: 3,
expected_total_size: Uint128::new(13),
expected_error: Some(ContractError::Std(StdError::not_found(
not_found_object_info::<Object>(
"abafa4428bdc8c34dae28bbc17303a62175f274edf59757b3e9898215a428a56",
),
))),
},

πŸ“ˆ Suggested Improvement

To enhance the clarity of error messages, we could consider replacing the generic Std error with a custom contract error, similar to ContractError::ObjectPinned. For instance, we could introduce ContractError::ObjectNotFound to handle cases where the object_id does not exist.

@ccamel, @amimart

@amimart
Copy link
Member

amimart commented Jun 4, 2024

To enhance the clarity of error messages, we could consider replacing the generic Std error with a custom contract error, similar to ContractError::ObjectPinned. For instance, we could introduce ContractError::ObjectNotFound to handle cases where the object_id does not exist.

In my sense I'd rather use existing well known types if they suit, and the StdError::NotFound carry the concerned type so providing a specific ContractError::ObjectNotFound doesn't bring more value I think.

Actually I don't see this issue relevant because the checks and the errors are already here and pretty expressive, I don't see how we can do better.. What do you think? I'd be in favour to close this one

@ccamel
Copy link
Member Author

ccamel commented Jun 12, 2024

@amimart I share your point of view. The standard StdError::NotFound exception conveys a sufficient level of information given the context in which it is used. I'd also be in favour of closing the ticket.

@amimart
Copy link
Member

amimart commented Jun 13, 2024

Ok let's closing it :)

@amimart amimart closed this as completed Jun 13, 2024
@github-project-automation github-project-automation bot moved this from πŸ“† To do to βœ… Done in πŸ’» Development Jun 13, 2024
@ccamel ccamel moved this from βœ… Done to πŸ™… Declined in πŸ’» Development Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
security audit Categorizes an issue or PR as relevant to Security Audit
Projects
Status: πŸ™… Declined
Development

No branches or pull requests

3 participants