-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #364 from CoinFabrik/add-documentation-for-storage…
…-events Create 24-storage-change-events.md
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|
||
|
||
|