-
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 #328 from CoinFabrik/327-instance-storage-detector…
…-refinement Edit `dynamic-storage`
- Loading branch information
Showing
20 changed files
with
205 additions
and
182 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...ctors/dynamic-instance-storage/Cargo.toml → detectors/dynamic-storage/Cargo.toml
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
edition = "2021" | ||
name = "dynamic-instance-storage" | ||
name = "dynamic-storage" | ||
version = "0.1.0" | ||
|
||
[lib] | ||
|
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
43 changes: 0 additions & 43 deletions
43
test-cases/dynamic-instance-storage/dynamic-instance-storage-1/vulnerable-example/src/lib.rs
This file was deleted.
Oops, something went wrong.
43 changes: 0 additions & 43 deletions
43
test-cases/dynamic-instance-storage/dynamic-instance-storage-2/remediated-example/src/lib.rs
This file was deleted.
Oops, something went wrong.
43 changes: 0 additions & 43 deletions
43
test-cases/dynamic-instance-storage/dynamic-instance-storage-2/vulnerable-example/src/lib.rs
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
test-cases/dynamic-instance-storage/dynamic-instance-storage-3/remediated-example/Cargo.toml
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
test-cases/dynamic-instance-storage/dynamic-instance-storage-3/vulnerable-example/Cargo.toml
This file was deleted.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
...cases/dynamic-instance-storage/Cargo.toml → test-cases/dynamic-storage/Cargo.toml
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
2 changes: 1 addition & 1 deletion
2
...e-storage-2/vulnerable-example/Cargo.toml → ...c-storage-1/remediated-example/Cargo.toml
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
63 changes: 63 additions & 0 deletions
63
test-cases/dynamic-storage/dynamic-storage-1/remediated-example/src/lib.rs
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,63 @@ | ||
#![no_std] | ||
use soroban_sdk::{contract, contractimpl, contracttype, Env, Vec}; | ||
|
||
#[contract] | ||
pub struct VectorStorage; | ||
|
||
#[derive(Clone)] | ||
#[contracttype] | ||
pub enum DataKey { | ||
VecElement(u32), | ||
} | ||
|
||
#[contractimpl] | ||
impl VectorStorage { | ||
pub fn store_vector(e: Env, data: Vec<i32>) { | ||
for (i, value) in data.iter().enumerate() { | ||
let key = DataKey::VecElement(i as u32); | ||
e.storage().persistent().set(&key, &value); | ||
} | ||
} | ||
|
||
pub fn get_vector(e: Env) -> Vec<i32> { | ||
let mut result = Vec::new(&e); | ||
let mut i = 0; | ||
|
||
while let Some(value) = VectorStorage::get_element(e.clone(), i) { | ||
result.push_back(value); | ||
i += 1; | ||
} | ||
|
||
result | ||
} | ||
|
||
pub fn get_element(e: Env, index: u32) -> Option<i32> { | ||
let key = DataKey::VecElement(index); | ||
e.storage().persistent().get(&key) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use soroban_sdk::{vec, Env}; | ||
|
||
#[test] | ||
fn test_vector_storage() { | ||
// Given | ||
let env = Env::default(); | ||
let contract_id = env.register_contract(None, VectorStorage); | ||
let client = VectorStorageClient::new(&env, &contract_id); | ||
|
||
// When | ||
let test_vec = vec![&env, 1, 2, 3, 4, 5]; | ||
client.store_vector(&test_vec); | ||
|
||
// Then | ||
let retrieved_vec = client.get_vector(); | ||
assert_eq!(test_vec, retrieved_vec); | ||
|
||
assert_eq!(client.get_element(&2), Some(3)); | ||
assert_eq!(client.get_element(&5), None); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...e-storage-1/vulnerable-example/Cargo.toml → ...c-storage-1/vulnerable-example/Cargo.toml
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...e-storage-2/remediated-example/Cargo.toml → ...c-storage-2/remediated-example/Cargo.toml
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
44 changes: 44 additions & 0 deletions
44
test-cases/dynamic-storage/dynamic-storage-2/remediated-example/src/lib.rs
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,44 @@ | ||
#![no_std] | ||
use soroban_sdk::{contract, contractimpl, Env, Map, Symbol}; | ||
|
||
#[contract] | ||
pub struct MapStorage; | ||
|
||
#[contractimpl] | ||
impl MapStorage { | ||
pub fn store_map(e: Env, data: Map<Symbol, i32>) { | ||
data.iter().for_each(|(key, value)| { | ||
e.storage().persistent().set(&key, &value); | ||
}); | ||
} | ||
|
||
pub fn get_key(e: Env, key: Symbol) -> i32 { | ||
e.storage().persistent().get(&key).unwrap() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use soroban_sdk::{symbol_short, Env, Map}; | ||
|
||
#[test] | ||
fn test_map_storage() { | ||
// Given | ||
let env = Env::default(); | ||
let contract_id = env.register_contract(None, MapStorage); | ||
let client = MapStorageClient::new(&env, &contract_id); | ||
|
||
// When | ||
let mut test_map = Map::new(&env); | ||
test_map.set(symbol_short!("key1"), 1); | ||
test_map.set(symbol_short!("key2"), 2); | ||
client.store_map(&test_map); | ||
|
||
// Then | ||
let key1 = client.get_key(&symbol_short!("key1")); | ||
let key2 = client.get_key(&symbol_short!("key2")); | ||
assert_eq!(test_map.get(symbol_short!("key1")).unwrap(), key1); | ||
assert_eq!(test_map.get(symbol_short!("key2")).unwrap(), key2); | ||
} | ||
} |
Oops, something went wrong.