Skip to content

Commit

Permalink
Merge pull request #364 from CoinFabrik/add-documentation-for-storage…
Browse files Browse the repository at this point in the history
…-events

Create 24-storage-change-events.md
  • Loading branch information
matiascabello authored Sep 5, 2024
2 parents f47476c + e2764a8 commit 2140f25
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions docs/docs/detectors/24-storage-change-events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Storage change events

## Description

- Category: `Best practices`
- Severity: `Minor`
- Detectors: [`storage-change-events`](https://github.com/CoinFabrik/scout-soroban/tree/main/detectors/storage-change-events)
- Test Cases: [`storage-change-events-1`](https://github.com/CoinFabrik/scout-soroban/tree/main/test-cases/storage-change-events/storage-change-events-1)


In Rust, it is very important to control storage, since it contains a large part of the information of a contract. For this reason, it is common to control storage movements through events, in order to record the changes that occur. If there is no control over these changes, it can lead to potential problems in the contract.

## Why is this bad?

If there is no control over storage changes, it can lead to security and transparency issues within the contract.

## Issue example

Consider the following `Soroban` contract:

```rust

fn set_counter(env: Env, counter: CounterState) {
env.storage().instance().set(&STATE, &counter);
}

```

In this example, the `set_counter()` function does not emit an event to notify of a change in the storage.

The code example can be found [here](https://github.com/CoinFabrik/scout-soroban/tree/main/test-cases/storage-change-events/storage-change-events-1/vulnerable-example).


## Remediated example

```rust
fn set_counter(env: Env, counter: CounterState) {
env.storage().instance().set(&STATE, &counter);
env.events()
.publish((COUNTER, symbol_short!("set")), counter.count);
}
```
In this example, the `set_counter()` function emits an event to notify of a change in the storage.

The remediated code example can be found [here](https://github.com/CoinFabrik/scout-soroban/tree/main/test-cases/storage-change-events/storage-change-events-1/remediated-example).

## How is it detected?

Checks if the function emits an event in case a change has occurred in the storage.




0 comments on commit 2140f25

Please sign in to comment.