-
Notifications
You must be signed in to change notification settings - Fork 19
/
ScopeGuard.sol
222 lines (199 loc) · 8.1 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
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
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.6;
import "@gnosis.pm/zodiac/contracts/guard/BaseGuard.sol";
import "@gnosis.pm/zodiac/contracts/factory/FactoryFriendly.sol";
contract ScopeGuard is FactoryFriendly, BaseGuard {
event SetTargetAllowed(address target, bool allowed);
event SetTargetScoped(address target, bool scoped);
event SetFallbackAllowedOnTarget(address target, bool allowed);
event SetValueAllowedOnTarget(address target, bool allowed);
event SetDelegateCallAllowedOnTarget(address target, bool allowed);
event SetFunctionAllowedOnTarget(
address target,
bytes4 functionSig,
bool allowed
);
event ScopeGuardSetup(address indexed initiator, address indexed owner);
constructor(address _owner) {
bytes memory initializeParams = abi.encode(_owner);
setUp(initializeParams);
}
/// @dev Initialize function, will be triggered when a new proxy is deployed
/// @param initializeParams Parameters of initialization encoded
function setUp(bytes memory initializeParams) public override {
__Ownable_init();
address _owner = abi.decode(initializeParams, (address));
transferOwnership(_owner);
emit ScopeGuardSetup(msg.sender, _owner);
}
struct Target {
bool allowed;
bool scoped;
bool delegateCallAllowed;
bool fallbackAllowed;
bool valueAllowed;
mapping(bytes4 => bool) allowedFunctions;
}
mapping(address => Target) public allowedTargets;
/// @dev Set whether or not calls can be made to an address.
/// @notice Only callable by owner.
/// @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, allowedTargets[target].allowed);
}
/// @dev Set whether or not delegate calls can be made to a target.
/// @notice Only callable by owner.
/// @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,
allowedTargets[target].delegateCallAllowed
);
}
/// @dev Sets whether or not calls to an address should be scoped to specific function signatures.
/// @notice Only callable by owner.
/// @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, allowedTargets[target].scoped);
}
/// @dev Sets whether or not a target can be sent to (incluces fallback/receive functions).
/// @notice Only callable by owner.
/// @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,
allowedTargets[target].fallbackAllowed
);
}
/// @dev Sets whether or not a target can be sent to (incluces fallback/receive functions).
/// @notice Only callable by owner.
/// @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,
allowedTargets[target].valueAllowed
);
}
/// @dev Sets whether or not a specific function signature should be allowed on a scoped target.
/// @notice Only callable by owner.
/// @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,
allowedTargets[target].allowedFunctions[functionSig]
);
}
/// @dev Returns bool to indicate if an address is an allowed target.
/// @param target Address to check.
function isAllowedTarget(address target) public view returns (bool) {
return (allowedTargets[target].allowed);
}
/// @dev Returns bool to indicate if an address is scoped.
/// @param target Address to check.
function isScoped(address target) public view returns (bool) {
return (allowedTargets[target].scoped);
}
/// @dev Returns bool to indicate if fallback is allowed to a target.
/// @param target Address to check.
function isfallbackAllowed(address target) public view returns (bool) {
return (allowedTargets[target].fallbackAllowed);
}
/// @dev Returns bool to indicate if ETH can be sent to a target.
/// @param target Address to check.
function isValueAllowed(address target) public view returns (bool) {
return (allowedTargets[target].valueAllowed);
}
/// @dev Returns bool to indicate if a function signature is allowed for a target address.
/// @param target Address to check.
/// @param functionSig Signature to check.
function isAllowedFunction(address target, bytes4 functionSig)
public
view
returns (bool)
{
return (allowedTargets[target].allowedFunctions[functionSig]);
}
/// @dev Returns bool to indicate if delegate calls are allowed to a target address.
/// @param target Address to check.
function isAllowedToDelegateCall(address target)
public
view
returns (bool)
{
return (allowedTargets[target].delegateCallAllowed);
}
// solhint-disallow-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.
}
function checkTransaction(
address to,
uint256 value,
bytes memory data,
Enum.Operation operation,
uint256,
uint256,
uint256,
address,
// solhint-disallow-next-line no-unused-vars
address payable,
bytes memory,
address
) external view 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"
);
}
}
function checkAfterExecution(bytes32, bool) external view override {}
}