forked from Timi16/soroban-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity_diff_utf8.txt
More file actions
281 lines (256 loc) · 9.12 KB
/
Copy pathsecurity_diff_utf8.txt
File metadata and controls
281 lines (256 loc) · 9.12 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
diff --git a/src/analyzer/security.rs b/src/analyzer/security.rs
index 412ac74..feb5c1c 100644
--- a/src/analyzer/security.rs
+++ b/src/analyzer/security.rs
@@ -39,14 +39,35 @@ pub struct SecurityFinding {
pub rationale: Option<String>,
}
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct RuleMetadata {
+ pub id: String,
+ pub name: String,
+ pub description: String,
+ pub severity: Severity,
+}
+
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct SecurityReport {
pub findings: Vec<SecurityFinding>,
+ pub rules: HashMap<String, RuleMetadata>,
}
pub trait SecurityRule {
+ fn id(&self) -> &str;
fn name(&self) -> &str;
fn description(&self) -> &str;
+ fn severity(&self) -> Severity;
+
+ fn metadata(&self) -> RuleMetadata {
+ RuleMetadata {
+ id: self.id().to_string(),
+ name: self.name().to_string(),
+ description: self.description().to_string(),
+ severity: self.severity(),
+ }
+ }
+
fn analyze_static(&self, _wasm_bytes: &[u8]) -> Result<Vec<SecurityFinding>> {
Ok(vec![])
}
@@ -87,29 +108,37 @@ impl SecurityAnalyzer {
let mut report = SecurityReport::default();
for rule in &self.rules {
- let name = rule.name();
+ let id = rule.id();
- if !filter.enable_rules.is_empty() && !filter.enable_rules.iter().any(|r| r == name) {
+ if !filter.enable_rules.is_empty() && !filter.enable_rules.iter().any(|r| r == id) {
continue;
}
- if filter.disable_rules.iter().any(|r| r == name) {
+ if filter.disable_rules.iter().any(|r| r == id) {
continue;
}
let static_findings = rule.analyze_static(wasm_bytes)?;
- report.findings.extend(
- static_findings
- .into_iter()
- .filter(|f| f.severity >= filter.min_severity),
- );
+ let filtered_static: Vec<_> = static_findings
+ .into_iter()
+ .filter(|f| f.severity >= filter.min_severity)
+ .collect();
+
+ if !filtered_static.is_empty() {
+ report.rules.insert(id.to_string(), rule.metadata());
+ report.findings.extend(filtered_static);
+ }
if let Some(tr) = trace {
let dynamic_findings = rule.analyze_dynamic(executor, tr)?;
- report.findings.extend(
- dynamic_findings
- .into_iter()
- .filter(|f| f.severity >= filter.min_severity),
- );
+ let filtered_dynamic: Vec<_> = dynamic_findings
+ .into_iter()
+ .filter(|f| f.severity >= filter.min_severity)
+ .collect();
+
+ if !filtered_dynamic.is_empty() {
+ report.rules.insert(id.to_string(), rule.metadata());
+ report.findings.extend(filtered_dynamic);
+ }
}
}
@@ -212,13 +241,22 @@ fn is_valid_strkey(s: &str) -> bool {
struct HardcodedAddressRule;
impl SecurityRule for HardcodedAddressRule {
- fn name(&self) -> &str {
+ fn id(&self) -> &str {
"hardcoded-address"
}
+
+ fn name(&self) -> &str {
+ "Hardcoded Address detector"
+ }
+
fn description(&self) -> &str {
"Detects hardcoded Stellar addresses in WASM data sections."
}
+ fn severity(&self) -> Severity {
+ Severity::Medium
+ }
+
fn analyze_static(&self, wasm_bytes: &[u8]) -> Result<Vec<SecurityFinding>> {
let mut findings = Vec::new();
@@ -238,7 +276,7 @@ impl SecurityRule for HardcodedAddressRule {
&& is_valid_strkey(word)
{
findings.push(SecurityFinding {
- rule_id: self.name().to_string(),
+ rule_id: self.id().to_string(),
severity: Severity::Medium,
location: "Data Section".to_string(),
description: format!("Found potential hardcoded address: {}", word),
@@ -260,13 +298,22 @@ impl SecurityRule for HardcodedAddressRule {
struct ArithmeticCheckRule;
impl SecurityRule for ArithmeticCheckRule {
- fn name(&self) -> &str {
+ fn id(&self) -> &str {
"arithmetic-overflow"
}
+
+ fn name(&self) -> &str {
+ "Arithmetic Overflow detector"
+ }
+
fn description(&self) -> &str {
"Detects potential for unchecked arithmetic overflow."
}
+ fn severity(&self) -> Severity {
+ Severity::Medium
+ }
+
fn analyze_static(&self, wasm_bytes: &[u8]) -> Result<Vec<SecurityFinding>> {
let mut findings = Vec::new();
let instructions = parse_instructions(wasm_bytes);
@@ -274,7 +321,7 @@ impl SecurityRule for ArithmeticCheckRule {
for (i, instr) in instructions.iter().enumerate() {
if Self::is_arithmetic(instr) && !Self::is_guarded(&instructions, i) {
findings.push(SecurityFinding {
- rule_id: self.name().to_string(),
+ rule_id: self.id().to_string(),
severity: Severity::Medium,
location: format!("Instruction {}", i),
description: format!("Unchecked arithmetic operation detected: {:?}", instr),
@@ -305,13 +352,22 @@ impl ArithmeticCheckRule {
struct AuthorizationCheckRule;
impl SecurityRule for AuthorizationCheckRule {
- fn name(&self) -> &str {
+ fn id(&self) -> &str {
"missing-auth"
}
+
+ fn name(&self) -> &str {
+ "Missing Authorization detector"
+ }
+
fn description(&self) -> &str {
"Detects sensitive flows missing authorization checks."
}
+ fn severity(&self) -> Severity {
+ Severity::High
+ }
+
fn analyze_dynamic(
&self,
_executor: Option<&ContractExecutor>,
@@ -349,7 +405,7 @@ impl SecurityRule for AuthorizationCheckRule {
// If we have storage writes without preceding auth, report a finding
if !problematic_storage_writes.is_empty() {
findings.push(SecurityFinding {
- rule_id: self.name().to_string(),
+ rule_id: self.id().to_string(),
severity: Severity::High,
location: "Dynamic trace".to_string(),
description: format!(
@@ -368,13 +424,22 @@ impl SecurityRule for AuthorizationCheckRule {
struct ReentrancyPatternRule;
impl SecurityRule for ReentrancyPatternRule {
- fn name(&self) -> &str {
+ fn id(&self) -> &str {
"reentrancy-pattern"
}
+
+ fn name(&self) -> &str {
+ "Reentrancy Pattern detector"
+ }
+
fn description(&self) -> &str {
"Detects cross-contract calls followed by storage writes in the same call frame."
}
+ fn severity(&self) -> Severity {
+ Severity::High
+ }
+
fn analyze_dynamic(
&self,
_executor: Option<&ContractExecutor>,
@@ -400,14 +465,22 @@ struct PendingCrossCall {
struct CrossContractImportRule;
impl SecurityRule for CrossContractImportRule {
- fn name(&self) -> &str {
+ fn id(&self) -> &str {
"cross-contract-import"
}
+ fn name(&self) -> &str {
+ "Cross-Contract Import detector"
+ }
+
fn description(&self) -> &str {
"Detects cross-contract host function imports with robust name matching."
}
+ fn severity(&self) -> Severity {
+ Severity::Low
+ }
+
fn analyze_static(&self, wasm_bytes: &[u8]) -> Result<Vec<SecurityFinding>> {
let mut matches = Vec::new();
@@ -439,7 +512,7 @@ impl SecurityRule for CrossContractImportRule {
}
Ok(vec![SecurityFinding {
- rule_id: self.name().to_string(),
+ rule_id: self.id().to_string(),
severity: Severity::Low,
location: "Import Section".to_string(),
description: format!(
@@ -504,13 +577,22 @@ fn is_cross_contract_host_import(module: &str, name: &str) -> bool {
struct UnboundedIterationRule;
impl SecurityRule for UnboundedIterationRule {
- fn name(&self) -> &str {
+ fn id(&self) -> &str {
"unbounded-iteration"
}
+
+ fn name(&self) -> &str {
+ "Unbounded Iteration detector"
+ }
+
fn description(&self) -> &str {
"Detects storage-driven loops and unbounded read patterns."
}
+ fn severity(&self) -> Severity {
+ Severity::High
+ }
+
fn analyze_static(&self, wasm_bytes: &[u8]) -> Result<Vec<SecurityFinding>> {
let analysis = analyze_unbounded_iteration_static(wasm_bytes);
if !analysis.suspicious {
@@ -725,9 +807,6 @@ fn analyze_unbounded_iteration_static(wasm_bytes: &[u8]) -> UnboundedStaticSigna
signal.suspicious = storage_calls_in_loops > 0;
signal
-
- signal.suspicious = storage_calls_in_loops > 0;
- signal
}
fn is_storage_read_import(module: &str, name: &str) -> bool {