-
Notifications
You must be signed in to change notification settings - Fork 3
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 #161 from CoinFabrik/156-add-documentation-unexpet…
…ed-revert-with-vector 156 add documentation unexpeted revert with vector
- Loading branch information
Showing
3 changed files
with
71 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
57 changes: 57 additions & 0 deletions
57
docs/docs/detectors/17-dos-unexpected-revert-with-vector.md
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,57 @@ | ||
# DoS unexpected revert with vector | ||
|
||
### What it does | ||
|
||
Checks for array pushes without access control. | ||
|
||
### Why is this bad? | ||
|
||
Arrays have a maximum size according to the storage cell. If the array is full, the push will revert. This can be used to prevent the execution of a function. | ||
|
||
### Known problems | ||
|
||
If the owner validation is performed in an auxiliary function, the warning will be shown, resulting in a false positive. | ||
|
||
### Example | ||
|
||
```rust | ||
pub fn add_candidate(env: Env, candidate: Address, caller: Address) -> Result<(), URError> { | ||
caller.require_auth(); | ||
let mut state = Self::get_state(env.clone()); | ||
if Self::vote_ended(env.clone()) { | ||
return Err(URError::VoteEnded); | ||
} | ||
if state.already_voted.contains_key(caller.clone()) { | ||
return Err(URError::AccountAlreadyVoted); | ||
} else { | ||
state.candidates.push_back(candidate.clone()); | ||
state.votes.set(candidate, 0); | ||
Ok(()) | ||
} | ||
} | ||
|
||
``` | ||
|
||
Use instead: | ||
|
||
```rust | ||
pub fn add_candidate(env: Env, candidate: Address, caller: Address) -> Result<(), URError> { | ||
caller.require_auth(); | ||
let mut state = Self::get_state(env.clone()); | ||
if Self::vote_ended(env.clone()) { | ||
return Err(URError::VoteEnded); | ||
} | ||
if Self::account_has_voted(env.clone(), caller.clone()) { | ||
return Err(URError::AccountAlreadyVoted); | ||
} else { | ||
env.storage().instance().set(&DataKey::Candidate(candidate.clone()), &Candidate{votes: 0}); | ||
state.total_candidates += 1; | ||
env.storage().instance().set(&DataKey::State, &state); | ||
Ok(()) | ||
} | ||
} | ||
``` | ||
|
||
### Implementation | ||
|
||
The detector's implementation can be found at [this link](https://github.com/CoinFabrik/scout-soroban/tree/main/detectors/dos-unexpected-revert-with-vector). |
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