forked from Cyfrin/aderyn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.rs
314 lines (292 loc) · 10.4 KB
/
helpers.rs
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
use semver::{Error, VersionReq};
use crate::{
ast::{
ASTNode, Expression, FunctionDefinition, Identifier, LiteralKind, MemberAccess, NodeID,
PragmaDirective, VariableDeclaration, Visibility,
},
context::{
browser::{
ExtractBinaryOperations, ExtractFunctionCallOptions, ExtractFunctionCalls,
ExtractMemberAccesses,
},
workspace_context::WorkspaceContext,
},
};
/// Count the number of identifiers that reference a given ID in the context.
pub fn count_identifiers_that_reference_an_id(
context: &WorkspaceContext,
function_id: NodeID,
) -> i32 {
let mut count = 0;
context.identifiers().iter().for_each(|&identifier| {
if let Some(reference_id) = identifier.referenced_declaration {
if reference_id == function_id {
count += 1;
}
}
});
count
}
pub fn get_calls_and_delegate_calls(context: &WorkspaceContext) -> Vec<&MemberAccess> {
context
.member_accesses()
.into_iter()
.filter(|member_access| {
member_access.member_name == "call" || member_access.member_name == "delegatecall"
})
.collect()
}
// Get all implemented external and public functions in the context.
pub fn get_implemented_external_and_public_functions(
context: &WorkspaceContext,
) -> impl Iterator<Item = &FunctionDefinition> {
context
.function_definitions()
.into_iter()
.filter(|function| {
(function.visibility == Visibility::Public
|| function.visibility == Visibility::External)
&& function.implemented
})
}
pub fn pragma_directive_to_semver(pragma_directive: &PragmaDirective) -> Result<VersionReq, Error> {
let mut version_string = String::new();
for literal in &pragma_directive.literals {
if literal == "solidity" {
continue;
}
if version_string.is_empty() && literal.contains("0.") {
version_string.push('=');
}
if version_string.len() > 5 && (literal == "<" || literal == "=") {
version_string.push(',');
}
version_string.push_str(literal);
}
VersionReq::parse(&version_string)
}
// Check if an ast_node has a `msg.sender` binary operation.
// Examples:
// ```
// function foo() public {
// require(msg.sender == owner);
// }
// ```
pub fn has_msg_sender_binary_operation(ast_node: &ASTNode) -> bool {
// Directly return the evaluation of the condition
ExtractBinaryOperations::from(ast_node)
.extracted
.iter()
.any(|binary_operation| {
ExtractMemberAccesses::from(binary_operation)
.extracted
.iter()
.any(|member_access| {
member_access.member_name == "sender"
&& if let Expression::Identifier(identifier) =
member_access.expression.as_ref()
{
identifier.name == "msg"
} else {
false
}
})
})
}
// Check if an ast_node sends native eth
// Examples:
// ```
// function foo() public {
// address(0x1).call{value: 10}("...")
// }
// ```
pub fn has_calls_that_sends_native_eth(ast_node: &ASTNode) -> bool {
// Check for address(..).call{value: 10}("...") pattern
let function_call_ops = ExtractFunctionCallOptions::from(ast_node).extracted;
for function_call in &function_call_ops {
let call_carries_value = function_call.options.iter().any(|c| {
if let Expression::Literal(literal) = c {
return literal.value.is_some();
}
if let Expression::Identifier(_) = c {
return true;
}
false
});
if !call_carries_value {
continue;
}
if let Expression::MemberAccess(member_access) = function_call.expression.as_ref() {
let is_call = member_access.member_name == "call";
if !is_call {
continue;
}
}
return true;
}
// Now, check for :-
// payable(address(..)).transfer(100)
// payable(address(..)).send(100)
let function_calls = ExtractFunctionCalls::from(ast_node).extracted;
for function_call in function_calls {
if let Expression::MemberAccess(member_access) = function_call.expression.as_ref() {
if member_access.member_name == "transfer" || member_access.member_name == "send" {
if let Some(type_description) = member_access.expression.type_descriptions() {
if type_description
.type_string
.as_ref()
.is_some_and(|type_string| type_string.starts_with("address"))
{
return true;
}
}
}
}
}
false
}
/// Detects for the pattern
/// x.delegatecall("...") where x is not a state variable
/// That means, it can be
/// a) An Identifier that references a variable declaration which is not `state_variable`
/// b) A literal adresss
pub fn has_delegate_calls_on_non_state_variables(
ast_node: &ASTNode,
context: &WorkspaceContext,
) -> bool {
let member_accesses = ExtractMemberAccesses::from(ast_node).extracted;
member_accesses.into_iter().any(|member| {
let is_delegate_call = member.member_name == "delegatecall";
let mut is_on_non_state_variable = false;
if let Expression::Identifier(identifier) = member.expression.as_ref() {
if let Some(referenced_id) = identifier.referenced_declaration {
if let Some(ASTNode::VariableDeclaration(v)) = context.nodes.get(&referenced_id) {
if !v.state_variable {
is_on_non_state_variable = true;
}
}
}
} else if let Expression::Literal(_) = member.expression.as_ref() {
is_on_non_state_variable = true;
}
is_delegate_call && is_on_non_state_variable
})
}
/// Detects for the pattern
/// x.call("...") where x is not a state variable
/// That means, it can be
/// a) An Identifier that references a variable declaration which is not `state_variable`
/// b) A literal adresss
pub fn get_low_level_calls_on_non_state_variable_addresses(
ast_node: &ASTNode,
context: &WorkspaceContext,
) -> Vec<MemberAccess> {
ExtractMemberAccesses::from(ast_node)
.extracted
.into_iter()
.filter_map(|member| {
if member.member_name != "call" {
return None;
}
if let Expression::Identifier(identifier) = member.expression.as_ref() {
if let Some(referenced_id) = identifier.referenced_declaration {
if let Some(ASTNode::VariableDeclaration(v)) = context.nodes.get(&referenced_id)
{
if !v.state_variable {
return Some(member);
}
}
}
} else if let Expression::Literal(_) = member.expression.as_ref() {
return Some(member);
}
None
})
.collect::<_>()
}
pub fn has_binary_checks_on_some_address(ast_node: &ASTNode) -> bool {
let binary_operations = ExtractBinaryOperations::from(ast_node).extracted;
binary_operations.into_iter().any(|op| {
[op.left_expression, op.right_expression].iter().any(|op| {
op.as_ref().type_descriptions().is_some_and(|desc| {
desc.type_string.as_ref().is_some_and(|type_string| {
type_string == "address" || type_string == "address payable"
})
})
})
})
}
pub fn get_literal_value_or_constant_variable_value(
node_id: NodeID,
context: &WorkspaceContext,
) -> Option<String> {
fn get_constant_variable_declaration_value(variable: &VariableDeclaration) -> Option<String> {
if variable.mutability() == Some(&crate::ast::Mutability::Constant) {
if let Some(Expression::Literal(literal)) = variable.value.as_ref() {
return literal.value.to_owned();
}
}
None
}
if let Some(node) = context.nodes.get(&node_id) {
match node {
ASTNode::Literal(literal) => return literal.value.to_owned(),
ASTNode::VariableDeclaration(variable) => {
return get_constant_variable_declaration_value(variable);
}
_ => (),
}
}
None
}
pub fn get_node_offset(node: &ASTNode) -> Option<usize> {
let src_location = node.src()?;
let chopped_location = match src_location.rfind(':') {
Some(index) => &src_location[..index],
None => src_location, // No colon found, return the original string
}
.to_string();
let (offset, _) = chopped_location.split_once(':').unwrap();
offset.parse::<usize>().ok()
}
/*
Check if expression in constant
Expression::Literal whose value is false/true
Expression::Identifier that refers to a constant boolean variable declaration
Expression::UnaryOperation with ! operator followed by a sub expression that could be either of the above
*/
pub fn is_constant_boolean(context: &WorkspaceContext, ast_node: &Expression) -> bool {
if let Expression::Literal(literal) = ast_node {
if literal.kind == LiteralKind::Bool
&& literal
.value
.as_ref()
.is_some_and(|value| value == "false" || value == "true")
{
return true;
}
}
if let Expression::Identifier(Identifier {
referenced_declaration: Some(id),
..
}) = ast_node
{
if let Some(ASTNode::VariableDeclaration(variable_declaration)) = context.nodes.get(id) {
if variable_declaration
.type_descriptions
.type_string
.as_ref()
.is_some_and(|value| value == "bool")
&& variable_declaration.mutability() == Some(&crate::ast::Mutability::Constant)
{
return true;
}
}
}
if let Expression::UnaryOperation(operation) = ast_node {
if operation.operator == "!" {
return is_constant_boolean(context, operation.sub_expression.as_ref());
}
}
false
}