forked from FinChippay/Finchippay-Solution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_fuzz_targets.py
More file actions
361 lines (328 loc) · 13.3 KB
/
Copy pathfix_fuzz_targets.py
File metadata and controls
361 lines (328 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import os
BASE = r'C:\Users\USER\Finchippay-Solution'
TARGETS_DIR = os.path.join(BASE, 'contracts', 'finchippay-contract', 'fuzz', 'fuzz_targets')
SHARED_HEADER = '''#![no_main]
use libfuzzer_sys::fuzz_target;
use soroban_sdk::{Address, Env, Symbol, Vec};
use soroban_sdk::token::TokenClient;
use soroban_sdk::testutils::Address as _;
use finchippay_contract::FinchippayContractClient;
fn setup_env() -> (Env, FinchippayContractClient, Address) {
let env = Env::default();
let contract_id = env.register(
finchippay_contract::FinchippayContract,
(),
);
let client = FinchippayContractClient::new(&env, &contract_id);
let admin = Address::generate(&env);
let signers = soroban_sdk::Vec::from_array(&env, [admin.clone()]);
let _ = client.initialize(&signers, &1);
// Register a real Stellar Asset Contract so TokenClient works
let token_id = env.register_stellar_asset_contract(admin.clone());
let tc = TokenClient::new(&env, &token_id);
tc.mint(&admin, &1_000_000_000_000_000i128);
(env, client, token_id)
}
'''
targets = {
'open_stream': '''
fuzz_target!(|data: &[u8]| {
if data.len() < 8 { return; }
let (env, client, token_id) = setup_env();
let payer = Address::generate(&env);
let recipient = Address::generate(&env);
let tc = TokenClient::new(&env, &token_id);
tc.mint(&payer, &1_000_000_000_000_000i128);
// Derive rate and deposit from first 8 bytes
let mut arr = [0u8; 16];
let copy_len = data.len().min(16);
arr[..copy_len].copy_from_slice(&data[..copy_len]);
let rate = i128::from_be_bytes(arr).abs() % 1_000_000_000_000i128 + 1;
let deposit = if data.len() >= 32 {
let mut arr2 = [0u8; 16];
arr2.copy_from_slice(&data[16..32]);
i128::from_be_bytes(arr2).abs() % 10_000_000_000_000i128 + 1
} else { 1000 };
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let _id = client.open_stream(&token_id, &payer, &recipient, &rate, &deposit);
}));
});
''',
'claim_stream': '''
fuzz_target!(|data: &[u8]| {
if data.len() < 4 { return; }
let mut id_bytes = [0u8; 4];
id_bytes.copy_from_slice(&data[..4]);
let stream_id = u32::from_be_bytes(id_bytes);
let (env, client, token_id) = setup_env();
let payer = Address::generate(&env);
let recipient = Address::generate(&env);
let tc = TokenClient::new(&env, &token_id);
tc.mint(&payer, &1_000_000_000_000_000i128);
// Create a real stream first
let real_id = client.open_stream(&token_id, &payer, &recipient, &100, &100_000);
let target_id = if stream_id % 2 == 0 { real_id } else { stream_id };
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let _claimed = client.claim_stream(&target_id, &recipient);
}));
});
''',
'create_escrow': '''
fuzz_target!(|data: &[u8]| {
if data.len() < 8 { return; }
let mut amt_bytes = [0u8; 16];
let copy_len = data.len().min(16);
amt_bytes[..copy_len].copy_from_slice(&data[..copy_len]);
let amount = i128::from_be_bytes(amt_bytes).abs() % 1_000_000_000_000i128 + 1;
let mut led_bytes = [0u8; 4];
if data.len() >= 20 { led_bytes.copy_from_slice(&data[16..20]); }
let offset = u32::from_be_bytes(led_bytes) % 100_000 + 1;
let (env, client, token_id) = setup_env();
let from = Address::generate(&env);
let to = Address::generate(&env);
let tc = TokenClient::new(&env, &token_id);
tc.mint(&from, &1_000_000_000_000_000i128);
let release = env.ledger().sequence() + offset;
let memo = Symbol::new(&env, "fuzz");
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let _id = client.create_escrow(&token_id, &from, &to, &amount, &release, &memo);
}));
});
''',
'claim_escrow': '''
fuzz_target!(|data: &[u8]| {
if data.len() < 4 { return; }
let mut id_bytes = [0u8; 4];
id_bytes.copy_from_slice(&data[..4]);
let escrow_id = u32::from_be_bytes(id_bytes);
let (env, client, token_id) = setup_env();
let from = Address::generate(&env);
let to = Address::generate(&env);
let tc = TokenClient::new(&env, &token_id);
tc.mint(&from, &1_000_000_000_000_000i128);
let current = env.ledger().sequence();
let release = current + 10;
let _real_id = client.create_escrow(&token_id, &from, &to, &1000, &release, &Symbol::new(&env, "fz"));
// Advance ledger so escrow is claimable
env.ledger().set_sequence(current + 20);
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
client.claim_escrow(&escrow_id);
}));
});
''',
'batch_send': '''
fuzz_target!(|data: &[u8]| {
if data.len() < 8 { return; }
let mut cnt_bytes = [0u8; 4];
cnt_bytes.copy_from_slice(&data[..4]);
let count = (u32::from_be_bytes(cnt_bytes) % 5 + 1) as u32;
let (env, client, token_id) = setup_env();
let from = Address::generate(&env);
let tc = TokenClient::new(&env, &token_id);
tc.mint(&from, &1_000_000_000_000_000i128);
let mut recipients: Vec<Address> = Vec::new(&env);
let mut amounts: Vec<i128> = Vec::new(&env);
let mut memos: Vec<Symbol> = Vec::new(&env);
for i in 0..count {
recipients.push_back(Address::generate(&env));
amounts.push_back((i as i128 + 1) * 100);
memos.push_back(Symbol::new(&env, "fz"));
}
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let _res = client.batch_send(&token_id, &from, &recipients, &amounts, &memos);
}));
});
''',
'create_multisig': '''
fuzz_target!(|data: &[u8]| {
if data.len() < 12 { return; }
let mut amt_bytes = [0u8; 16];
let copy_len = data.len().min(16);
amt_bytes[..copy_len].copy_from_slice(&data[..copy_len]);
let amount = i128::from_be_bytes(amt_bytes).abs() % 1_000_000_000_000i128 + 1;
let mut thresh_bytes = [0u8; 4];
thresh_bytes.copy_from_slice(&data[16..20]);
let signers_count = (u32::from_be_bytes(thresh_bytes) % 4 + 2) as u32; // 2..5
let threshold = if signers_count > 1 { signers_count - 1 } else { 1 };
let (env, client, token_id) = setup_env();
let proposer = Address::generate(&env);
let recipient = Address::generate(&env);
let tc = TokenClient::new(&env, &token_id);
tc.mint(&proposer, &1_000_000_000_000_000i128);
let mut signers: Vec<Address> = Vec::new(&env);
for _ in 0..signers_count {
signers.push_back(Address::generate(&env));
}
let expiration = env.ledger().sequence() + 1_000_000;
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let _id = client.create_multisig(&token_id, &proposer, &recipient, &amount, &threshold, &signers, &expiration);
}));
});
''',
'approve_multisig': '''
fuzz_target!(|data: &[u8]| {
if data.len() < 4 { return; }
let mut id_bytes = [0u8; 4];
id_bytes.copy_from_slice(&data[..4]);
let prop_id = u32::from_be_bytes(id_bytes);
let (env, client, token_id) = setup_env();
let proposer = Address::generate(&env);
let recipient = Address::generate(&env);
let signer1 = Address::generate(&env);
let signer2 = Address::generate(&env);
let tc = TokenClient::new(&env, &token_id);
tc.mint(&proposer, &1_000_000_000_000_000i128);
let mut signers: Vec<Address> = Vec::new(&env);
signers.push_back(signer1.clone());
signers.push_back(signer2.clone());
let expiration = env.ledger().sequence() + 1_000_000;
let _real_id = client.create_multisig(&token_id, &proposer, &recipient, &1000, &2, &signers, &expiration);
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
client.approve_multisig(&prop_id, &signer1);
}));
});
''',
}
for name, body in targets.items():
content = SHARED_HEADER + body
path = os.path.join(TARGETS_DIR, f'{name}.rs')
with open(path, 'w') as f:
f.write(content)
print(f"Fixed {name}.rs")
# ================================================================
# Update .github/workflows/ci.yml - add fuzzing job
# ================================================================
ci_path = os.path.join(BASE, '.github', 'workflows', 'ci.yml')
with open(ci_path, 'r') as f:
ci = f.read()
fuzz_job = '''
# --- Fuzz Testing (cargo-fuzz) -----------------------------------------------\n
fuzz:\n
name: Fuzz Testing (cargo-fuzz)\n
runs-on: ubuntu-latest\n
defaults:\n
run:\n
working-directory: contracts/finchippay-contract\n
\n
steps:\n
- uses: actions/checkout@v4\n
\n
- name: Install Rust nightly\n
uses: dtolnay/rust-toolchain@v1\n
with:\n
toolchain: nightly\n
\n
- name: Cache cargo\n
uses: actions/cache@v4\n
with:\n
path: |\n
~/.cargo/registry\n
~/.cargo/git\n
contracts/finchippay-contract/target\n
key: ${{ runner.os }}-cargo-fuzz-${{ hashFiles('contracts/finchippay-contract/Cargo.lock') }}\n
\n
- name: Install cargo-fuzz\n
run: cargo install cargo-fuzz\n
\n
- name: Verify fuzz targets compile\n
run: cargo fuzz list\n
\n
- name: Fuzz open_stream (120s)\n
run: cargo fuzz run open_stream -- -max_total_time=120 || true\n
continue-on-error: true\n
\n
- name: Fuzz claim_stream (120s)\n
run: cargo fuzz run claim_stream -- -max_total_time=120 || true\n
continue-on-error: true\n
\n
- name: Fuzz create_escrow (120s)\n
run: cargo fuzz run create_escrow -- -max_total_time=120 || true\n
continue-on-error: true\n
\n
- name: Fuzz claim_escrow (120s)\n
run: cargo fuzz run claim_escrow -- -max_total_time=120 || true\n
continue-on-error: true\n
\n
- name: Fuzz batch_send (120s)\n
run: cargo fuzz run batch_send -- -max_total_time=120 || true\n
continue-on-error: true\n
\n
- name: Fuzz create_multisig (120s)\n
run: cargo fuzz run create_multisig -- -max_total_time=120 || true\n
continue-on-error: true\n
\n
- name: Fuzz approve_multisig (120s)\n
run: cargo fuzz run approve_multisig -- -max_total_time=120 || true\n
continue-on-error: true\n
'''
# Insert the fuzz job before the last " # --- Validate Config ---" section
# Find the validate section
insert_point = '\n # --- Validate Config'
if insert_point in ci:
ci = ci.replace(insert_point, fuzz_job + insert_point, 1)
with open(ci_path, 'w') as f:
f.write(ci)
print("Updated .github/workflows/ci.yml with fuzzing job")
else:
print("WARNING: Could not find insert point in CI config")
# ================================================================
# Update README.md with fuzz testing instructions
# ================================================================
readme_path = os.path.join(BASE, 'README.md')
with open(readme_path, 'r') as f:
readme = f.read()
fuzz_section = '''
## Fuzz Testing
Coverage-guided fuzz testing is implemented using [cargo-fuzz](https://github.com/rust-fuzz/cargo-fuzz) (libfuzzer) for all value-transferring contract entry-points.
### Prerequisites
```bash
# Install cargo-fuzz (requires Rust nightly)
rustup toolchain install nightly
cargo install cargo-fuzz
```
### Running Fuzz Targets
Navigate to the contract directory and run any of the 7 fuzz targets:
```bash
cd contracts/finchippay-contract
# Run all targets (120 seconds each)
cargo fuzz run open_stream -- -max_total_time=120
cargo fuzz run claim_stream -- -max_total_time=120
cargo fuzz run create_escrow -- -max_total_time=120
cargo fuzz run claim_escrow -- -max_total_time=120
cargo fuzz run batch_send -- -max_total_time=120
cargo fuzz run create_multisig -- -max_total_time=120
cargo fuzz run approve_multisig -- -max_total_time=120
# Run a target indefinitely (stop with Ctrl+C)
cargo fuzz run open_stream
```
### Fuzz Targets
| Target | Contract Function | Description |
|--------|-----------------|-------------|
| `open_stream` | `open_stream()` | Opens a payment stream with random rate/deposit |
| `claim_stream` | `claim_stream()` | Claims from an existing stream or non-existent ID |
| `create_escrow` | `create_escrow()` | Creates a time-locked escrow with random params |
| `claim_escrow` | `claim_escrow()` | Claims an escrow after advancing the ledger |
| `batch_send` | `batch_send()` | Batch sends to 1-5 random recipients |
| `create_multisig` | `create_multisig()` | Creates multi-sig proposals with 2-5 signers |
| `approve_multisig` | `approve_multisig()` | Approves a multi-sig proposal |
### CI Integration
The CI pipeline runs all 7 fuzz targets for 120 seconds each using `cargo fuzz run`. Failures are reported but do not block PRs (fuzz crashes are triaged separately).
### How It Works
Each fuzz target:
1. Creates a fresh Soroban `Env` with a registered contract instance and Stellar Asset Contract
2. Parses fuzz input bytes into constrained random parameters (amounts, IDs, counts)
3. Calls the contract function wrapped in `catch_unwind` to gracefully handle expected panics
4. libfuzzer instruments the WASM binary and explores edge cases automatically
'''
# Add fuzz section before the License section
if '## License' in readme:
readme = readme.replace('## License', fuzz_section + '\n## License')
with open(readme_path, 'w') as f:
f.write(readme)
print("Updated README.md with fuzz testing instructions")
else:
# Append at the end
with open(readme_path, 'a') as f:
f.write(fuzz_section)
print("Appended fuzz section to README.md")
print("\n=== ALL FIXES DONE ===")