-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScopeGuard.sol
183 lines (161 loc) · 6.87 KB
/
ScopeGuard.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
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
// SPDX-License-Identifier: Immuni Software PTE Ltd General Source License
// https://github.com/immunefi-team/vaults/blob/main/LICENSE.md
pragma solidity 0.8.18;
import { BaseGuard } from "@gnosis.pm/zodiac/contracts/guard/BaseGuard.sol";
import { OwnableUpgradeable } from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import { Enum } from "@gnosis.pm/safe-contracts/contracts/common/Enum.sol";
import { IScopeGuardEvents } from "../events/IScopeGuardEvents.sol";
/**
* @title ScopeGuard
* @author Immunefi
* @notice Scope Guard from Zodiac with some modifications
* @dev https://github.com/gnosis/zodiac-guard-scope/blob/main/contracts/ScopeGuard.sol
*/
contract ScopeGuard is OwnableUpgradeable, BaseGuard, IScopeGuardEvents {
struct Target {
bool allowed;
bool scoped;
bool delegateCallAllowed;
bool fallbackAllowed;
bool valueAllowed;
mapping(bytes4 => bool) allowedFunctions;
}
mapping(address => Target) public allowedTargets;
constructor() {
_disableInitializers();
}
/**
* @notice Initializes the contract.
* @param _owner Owner of the contract
*/
function setUp(address _owner) public initializer {
__Ownable_init();
require(_owner != address(0), "ScopeGuard: owner is the zero address");
_transferOwnership(_owner);
emit ScopeGuardSetup(msg.sender, _owner);
}
/**
* @notice Sets whether or not calls can be made to an address.
* @param target Address to be allowed/disallowed.
* @param allow Bool to allow (true) or disallow (false) calls to target.
*/
function setTargetAllowed(address target, bool allow) public onlyOwner {
allowedTargets[target].allowed = allow;
emit SetTargetAllowed(target, allow);
}
/**
* @notice Sets whether or not delegate calls can be made to a target.
* @param target Address to which delegate calls should be allowed/disallowed.
* @param allow Bool to allow (true) or disallow (false) delegate calls to target.
*/
function setDelegateCallAllowedOnTarget(address target, bool allow) public onlyOwner {
allowedTargets[target].delegateCallAllowed = allow;
emit SetDelegateCallAllowedOnTarget(target, allow);
}
/**
* @notice Sets whether or not calls to an address should be scoped to specific function signatures.
* @param target Address to be scoped/unscoped.
* @param scoped Bool to scope (true) or unscope (false) function calls on target.
*/
function setScoped(address target, bool scoped) public onlyOwner {
allowedTargets[target].scoped = scoped;
emit SetTargetScoped(target, scoped);
}
/**
* @notice Sets whether or not a target can be sent to (includes fallback/receive functions).
* @param target Address to be allow/disallow sends to.
* @param allow Bool to allow (true) or disallow (false) sends on target.
*/
function setFallbackAllowedOnTarget(address target, bool allow) public onlyOwner {
allowedTargets[target].fallbackAllowed = allow;
emit SetFallbackAllowedOnTarget(target, allow);
}
/**
* @notice Sets whether or not ETH can be sent to a target.
* @param target Address to be allow/disallow sends to.
* @param allow Bool to allow (true) or disallow (false) sends on target.
*/
function setValueAllowedOnTarget(address target, bool allow) public onlyOwner {
allowedTargets[target].valueAllowed = allow;
emit SetValueAllowedOnTarget(target, allow);
}
/**
* @notice Sets whether or not a specific function signature should be allowed on a scoped target.
* @param target Scoped address on which a function signature should be allowed/disallowed.
* @param functionSig Function signature to be allowed/disallowed.
* @param allow Bool to allow (true) or disallow (false) calls a function signature on target.
*/
function setAllowedFunction(address target, bytes4 functionSig, bool allow) public onlyOwner {
allowedTargets[target].allowedFunctions[functionSig] = allow;
emit SetFunctionAllowedOnTarget(target, functionSig, allow);
}
function isAllowedTarget(address target) public view returns (bool) {
return (allowedTargets[target].allowed);
}
function isScoped(address target) public view returns (bool) {
return (allowedTargets[target].scoped);
}
function isfallbackAllowed(address target) public view returns (bool) {
return (allowedTargets[target].fallbackAllowed);
}
function isValueAllowed(address target) public view returns (bool) {
return (allowedTargets[target].valueAllowed);
}
function isAllowedFunction(address target, bytes4 functionSig) public view returns (bool) {
return (allowedTargets[target].allowedFunctions[functionSig]);
}
function isAllowedToDelegateCall(address target) public view returns (bool) {
return (allowedTargets[target].delegateCallAllowed);
}
// solhint-disable-next-line payable-fallback
fallback() external {
// We don't revert on fallback to avoid issues in case of a Safe upgrade
// E.g. The expected check method might change and then the Safe would be locked.
}
/**
* @notice Checks a transaction before execution.
* @param to Destination address of vault transaction.
* @param value Ether value of vault transaction.
* @param data Data payload of vault transaction.
* @param operation Operation type for avatar execution.
*/
function checkTransaction(
address to,
uint256 value,
bytes memory data,
Enum.Operation operation,
uint256,
uint256,
uint256,
address,
// solhint-disable-next-line no-unused-vars
address payable,
bytes memory,
address
) public view virtual override {
require(
operation != Enum.Operation.DelegateCall || allowedTargets[to].delegateCallAllowed,
"Delegate call not allowed to this address"
);
require(allowedTargets[to].allowed, "Target address is not allowed");
if (value > 0) {
require(allowedTargets[to].valueAllowed, "Cannot send ETH to this target");
}
if (data.length >= 4) {
require(
!allowedTargets[to].scoped || allowedTargets[to].allowedFunctions[bytes4(data)],
"Target function is not allowed"
);
} else {
require(data.length == 0, "Function signature too short");
require(
!allowedTargets[to].scoped || allowedTargets[to].fallbackAllowed,
"Fallback not allowed for this address"
);
}
}
/**
* @notice Checks a transaction after execution.
*/
function checkAfterExecution(bytes32, bool) external view virtual override {}
}