-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path027-security-when-writing-contracts.sol
67 lines (54 loc) · 1.96 KB
/
027-security-when-writing-contracts.sol
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
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* Cyber Security in Solidity - against bad actors, accidential occurences
*
* Withdrawal patterns ensures that direct transfer call is bot made which
* poses a security threat
*
* Transfers are atomic(meaning all or nothing)
*/
contract SecurityInSolidity {
// some constant variables for testing
// and getting rid of warnings or errors.
uint256 contributedAmount = 100;
address payable[20] public funders;
// specific addresses
address payable tatiana;
address payable john;
// modifier
modifier onlyOwner() {
require(true);
_;
}
// function to return funds
function returnFunds() public payable onlyOwner returns (bool success) {
for (uint256 i = 0; i < funders.length; i++) {
funders[i].transfer(contributedAmount);
}
return true;
}
// From the above function:
// 1. What if the owner chickens out and does not return the funds
// 2. The loop is unbound considering that the total funders is unknown
// it could run out of gas before allocaing all the funds, what an attacker could
// just do is to make a small bunch of contributions .0000284289
// function transfer funds between two addresses
function returnFundsTwoAddresses()
public
payable
onlyOwner
returns (bool success)
{
tatiana.transfer(contributedAmount);
john.transfer(contributedAmount);
return true;
}
// What could go wrong in this case?
// 1. What if tatiana rejects the transfer
// - The function is going to break or fail and john is not going to get paid
// Thinking that all contracts and accounts would want to receive or accept
// funds automatically is intuitive at first; but it is infact very naive and dangerous
// to build your code like this.
// If it is in their interest to reject the funds they will
}