-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSphereXProtected.t.sol
518 lines (421 loc) · 20.9 KB
/
SphereXProtected.t.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
// SPDX-License-Identifier: UNLICENSED
// (c) SphereX 2023 Terms&Conditions
pragma solidity >=0.6.2;
import "forge-std/Test.sol";
import "./Utils/CFUtils.sol";
import "./Utils/MockEngine.sol";
import "./Utils/CostumerContract.sol";
import "../src/SphereXEngine.sol";
import "../src/SphereXProtected.sol";
contract SphereXProtectedTest is Test, CFUtils {
CostumerContract public costumer_contract;
int256 internal constant ADD_ALLOWED_SENDER_ONCHAIN_INDEX = int256(uint256(keccak256("factory.allowed.sender")));
modifier activateRule2() {
spherex_engine.configureRules(PREFIX_TX_FLOW);
allowed_cf_storage = [
to_int256(costumer_contract.try_allowed_flow.selector),
-to_int256(costumer_contract.try_allowed_flow.selector),
to_int256(costumer_contract.externalCallsExternal.selector),
to_int256(costumer_contract.externalCallee.selector),
-to_int256(costumer_contract.externalCallee.selector),
-to_int256(costumer_contract.externalCallsExternal.selector)
];
addAllowedPattern();
allowed_cf_storage = [
to_int256(costumer_contract.try_allowed_flow.selector),
-to_int256(costumer_contract.try_allowed_flow.selector)
];
addAllowedPattern();
allowed_cf_storage = [int256(1), -1, 11, 12, -12];
allowed_cf_storage = [
to_int256(costumer_contract.try_allowed_flow.selector),
-to_int256(costumer_contract.try_allowed_flow.selector),
to_int256(costumer_contract.externalCallsExternal.selector),
to_int256(costumer_contract.externalCallee.selector),
-to_int256(costumer_contract.externalCallee.selector)
];
addAllowedPattern();
_;
}
function setUp() public virtual {
spherex_engine = new SphereXEngine();
costumer_contract = new CostumerContract();
costumer_contract.changeSphereXOperator(address(this));
allowed_patterns.push(calc_pattern_by_selector(CostumerContract.try_allowed_flow.selector));
allowed_senders.push(address(costumer_contract));
spherex_engine.addAllowedSender(allowed_senders);
spherex_engine.addAllowedPatterns(allowed_patterns);
spherex_engine.configureRules(CF);
costumer_contract.changeSphereXEngine(address(spherex_engine));
}
// ============ Managment functions ============
function test_changeSphereXEngine_disable_engine() external virtual {
// this test covers enable->disable (by default the engine is enabled in the set up)
costumer_contract.changeSphereXEngine(address(0));
costumer_contract.try_blocked_flow();
assertFlowStorageSlotsInInitialState();
}
function test_changeSphereXEngine_disable_enable() external virtual {
costumer_contract.changeSphereXEngine(address(0));
costumer_contract.try_blocked_flow();
costumer_contract.changeSphereXEngine(address(spherex_engine));
costumer_contract.try_allowed_flow();
vm.expectRevert("SphereX error: disallowed tx pattern");
costumer_contract.try_blocked_flow();
assertFlowStorageSlotsInInitialState();
}
function test_changeSphereXEngine_disable_disable() external virtual {
costumer_contract.changeSphereXEngine(address(0));
costumer_contract.try_blocked_flow();
costumer_contract.changeSphereXEngine(address(0));
costumer_contract.try_blocked_flow();
assertFlowStorageSlotsInInitialState();
}
function test_changeSphereXEngine_enable_enable() external virtual {
// the setup function is enabling the engine by default so we only need to
// enable once
costumer_contract.try_allowed_flow();
vm.expectRevert("SphereX error: disallowed tx pattern");
costumer_contract.try_blocked_flow();
costumer_contract.changeSphereXEngine(address(spherex_engine));
costumer_contract.try_allowed_flow();
vm.expectRevert("SphereX error: disallowed tx pattern");
costumer_contract.try_blocked_flow();
assertFlowStorageSlotsInInitialState();
}
function test_changeSphereXAdmin() external virtual {
address otherAddress = address(1);
costumer_contract.transferSphereXAdminRole(otherAddress);
vm.prank(otherAddress);
costumer_contract.acceptSphereXAdminRole();
vm.expectRevert("SphereX error: admin required");
costumer_contract.transferSphereXAdminRole(address(this));
vm.prank(otherAddress);
costumer_contract.transferSphereXAdminRole(address(this));
vm.prank(otherAddress);
vm.expectRevert("SphereX error: not the pending account");
costumer_contract.acceptSphereXAdminRole();
assertFlowStorageSlotsInInitialState();
}
// // ============ Call flow thesis tests ============
function testAllowed() external {
costumer_contract.try_allowed_flow();
assertFlowStorageSlotsInInitialState();
}
function testTwoAllowedCall() external {
costumer_contract.try_allowed_flow();
costumer_contract.try_allowed_flow();
assertFlowStorageSlotsInInitialState();
}
function testBlocked() external {
vm.expectRevert("SphereX error: disallowed tx pattern");
costumer_contract.try_blocked_flow();
assertFlowStorageSlotsInInitialState();
}
function testPartialRevertAllowedFlow() external virtual {
allowed_cf_storage = [
to_int256(costumer_contract.call_inner.selector),
to_int256(bytes4(keccak256(bytes("inner()")))),
-to_int256(bytes4(keccak256(bytes("inner()")))),
-to_int256(costumer_contract.call_inner.selector)
];
addAllowedPattern();
costumer_contract.call_inner();
assertFlowStorageSlotsInInitialState();
}
function testPartialRevertNotAllowedFlow() external virtual {
allowed_cf_storage = [
to_int256(costumer_contract.call_inner.selector),
to_int256(bytes4(keccak256(bytes("inner()")))),
to_int256(costumer_contract.reverts.selector),
-to_int256(costumer_contract.reverts.selector) - to_int256(bytes4(keccak256(bytes("inner()")))),
-to_int256(costumer_contract.call_inner.selector)
];
addAllowedPattern();
vm.expectRevert("SphereX error: disallowed tx pattern");
costumer_contract.call_inner();
assertFlowStorageSlotsInInitialState();
}
/**
* @dev this test will fail if we add a storage logic!
* for now it checks that nothing is being sent
* to the engine at the post call except for the num parameter.
*/
function testPublicFunction() external {
allowed_cf_storage = [
to_int256(costumer_contract.publicFunction.selector),
-to_int256(costumer_contract.publicFunction.selector)
];
addAllowedPattern();
bytes memory publicFunctionMsgData = abi.encodeWithSelector(costumer_contract.publicFunction.selector);
bytes memory engineCallMsgData = abi.encodeWithSelector(
spherex_engine.sphereXValidatePre.selector,
to_int256(costumer_contract.publicFunction.selector),
address(1),
publicFunctionMsgData
);
bytes memory engineCallnoMsgData = abi.encodeWithSelector(spherex_engine.sphereXValidatePost.selector);
vm.expectCall(address(spherex_engine), engineCallMsgData);
vm.expectCall(address(spherex_engine), engineCallnoMsgData);
vm.prank(address(1));
costumer_contract.publicFunction();
assertFlowStorageSlotsInInitialState();
}
function testExternalFunction() external {
bytes memory externalFunctionMsgData = abi.encodeWithSelector(costumer_contract.try_allowed_flow.selector);
bytes memory engineCallMsgData = abi.encodeWithSelector(
spherex_engine.sphereXValidatePre.selector,
to_int256(costumer_contract.try_allowed_flow.selector),
address(1),
externalFunctionMsgData
);
vm.expectCall(address(spherex_engine), engineCallMsgData);
vm.prank(address(1));
costumer_contract.try_allowed_flow();
assertFlowStorageSlotsInInitialState();
}
function testExternalCallsInternalFunction() external virtual {
allowed_cf_storage = [
to_int256(costumer_contract.call_inner.selector),
to_int256(bytes4(keccak256(bytes("inner()")))),
-to_int256(bytes4(keccak256(bytes("inner()")))),
-to_int256(costumer_contract.call_inner.selector)
];
addAllowedPattern();
bytes memory externalFunctionMsgData = abi.encodeWithSelector(costumer_contract.call_inner.selector);
bytes memory engineExternalCallMsgData = abi.encodeWithSelector(
spherex_engine.sphereXValidatePre.selector,
to_int256(costumer_contract.call_inner.selector),
address(1),
externalFunctionMsgData
);
bytes memory engineInternalCallMsgData = abi.encodeWithSelector(
spherex_engine.sphereXValidateInternalPre.selector, to_int256(bytes4(keccak256(bytes("inner()"))))
);
vm.expectCall(address(spherex_engine), engineExternalCallMsgData);
vm.expectCall(address(spherex_engine), engineInternalCallMsgData);
vm.prank(address(1));
costumer_contract.call_inner();
assertFlowStorageSlotsInInitialState();
}
function testPublicCallsPublic() external virtual {
allowed_cf_storage = [
to_int256(costumer_contract.publicCallsPublic.selector),
to_int256(costumer_contract.publicFunction.selector),
-to_int256(costumer_contract.publicFunction.selector),
-to_int256(costumer_contract.publicCallsPublic.selector)
];
addAllowedPattern();
bytes memory publicCallsPublicMsgData = abi.encodeWithSelector(costumer_contract.publicCallsPublic.selector);
bytes memory publicCallsPublicEngineCallMsgData = abi.encodeWithSelector(
spherex_engine.sphereXValidatePre.selector,
to_int256(costumer_contract.publicCallsPublic.selector),
address(1),
publicCallsPublicMsgData
);
bytes memory publicFunctionEngineCallMsgData = abi.encodeWithSelector(
spherex_engine.sphereXValidateInternalPre.selector, to_int256(costumer_contract.publicFunction.selector)
);
vm.expectCall(address(spherex_engine), publicCallsPublicEngineCallMsgData);
vm.expectCall(address(spherex_engine), publicFunctionEngineCallMsgData);
vm.prank(address(1));
costumer_contract.publicCallsPublic();
assertFlowStorageSlotsInInitialState();
}
/**
* @dev this is andesirable behaviour where internally called
* public function, in the context of the same function
* being called externally, will trigger sending msg.data
* twice to the engine.
*/
function testPublicCallsSamePublic() external virtual {
allowed_cf_storage = [
to_int256(costumer_contract.publicCallsSamePublic.selector),
to_int256(costumer_contract.publicCallsSamePublic.selector),
-to_int256(costumer_contract.publicCallsSamePublic.selector),
-to_int256(costumer_contract.publicCallsSamePublic.selector)
];
addAllowedPattern();
allowed_cf_storage = [
to_int256(costumer_contract.publicCallsSamePublic.selector),
to_int256(costumer_contract.publicCallsSamePublic.selector),
-to_int256(costumer_contract.publicCallsSamePublic.selector)
];
addAllowedPattern();
bytes memory publicCallsSamePublicMsgData =
abi.encodeWithSelector(costumer_contract.publicCallsSamePublic.selector, true);
bytes memory engineCallMsgData = abi.encodeWithSelector(
spherex_engine.sphereXValidatePre.selector,
to_int256(costumer_contract.publicCallsSamePublic.selector),
address(1),
publicCallsSamePublicMsgData
);
vm.expectCall(address(spherex_engine), engineCallMsgData);
vm.expectCall(address(spherex_engine), engineCallMsgData);
vm.prank(address(1));
costumer_contract.publicCallsSamePublic(true);
assertFlowStorageSlotsInInitialState();
}
function testArbitraryCall() external {
allowed_cf_storage =
[to_int256(costumer_contract.arbitraryCall.selector), -to_int256(costumer_contract.arbitraryCall.selector)];
addAllowedPattern();
bytes memory engineCallMsgData = abi.encodeWithSelector(
spherex_engine.sphereXValidateInternalPre.selector, to_int256(costumer_contract.arbitraryCall.selector)
);
vm.expectRevert("SphereX error: disallowed tx pattern");
costumer_contract.arbitraryCall(address(spherex_engine), engineCallMsgData);
assertFlowStorageSlotsInInitialState();
}
function testExternalCallsExternalTwice() external {
allowed_cf_storage = [int256(11), 12, -12, -11];
allowed_cf_storage = [
to_int256(costumer_contract.externalCallsExternal.selector),
to_int256(costumer_contract.externalCallee.selector),
-to_int256(costumer_contract.externalCallee.selector),
-to_int256(costumer_contract.externalCallsExternal.selector)
];
addAllowedPattern();
allowed_cf_storage = [int256(11), 12, -12];
allowed_cf_storage = [
to_int256(costumer_contract.externalCallsExternal.selector),
to_int256(costumer_contract.externalCallee.selector),
-to_int256(costumer_contract.externalCallee.selector)
];
addAllowedPattern();
costumer_contract.externalCallsExternal();
costumer_contract.externalCallsExternal();
assertFlowStorageSlotsInInitialState();
}
// ============ Storage thesis helper function test ============
function test_readSlot() external virtual {
MockEngine mock_spherex_engine = new MockEngine();
uint256 before = costumer_contract.slot0();
costumer_contract.changeSphereXEngine(address(mock_spherex_engine));
costumer_contract.changex();
assertEq(mock_spherex_engine.stor(0), before);
assertEq(mock_spherex_engine.stor(1), costumer_contract.slot0());
assertFlowStorageSlotsInInitialState();
}
// ============ Prefix tx flow ============
// We initialize the engine (in the activateRule2 modifier) such that
// the allowed patterns are calling allowed, and calling allowed,externalCallsExternal
// calling only externalCallsExternal is prohibited
function test_PrefixTxFlow_sanity() public activateRule2 {
costumer_contract.try_allowed_flow();
costumer_contract.externalCallsExternal();
}
function test_PrefixTxFlow_sanity_revert() public activateRule2 {
costumer_contract.try_allowed_flow();
vm.roll(2);
vm.expectRevert("SphereX error: disallowed tx pattern");
costumer_contract.externalCallsExternal();
}
function test_PrefixTxFlow_known_issue_good_scenario() public activateRule2 {
costumer_contract.try_allowed_flow();
costumer_contract.externalCallsExternal();
vm.startPrank(address(this), 0x6A08098568eE90b71dD757F070D79364197f944B);
costumer_contract.try_allowed_flow();
costumer_contract.externalCallsExternal();
vm.stopPrank();
costumer_contract.try_allowed_flow();
costumer_contract.externalCallsExternal();
}
function test_PrefixTxFlow_known_issue_bad_scenario() public activateRule2 {
costumer_contract.try_allowed_flow();
costumer_contract.externalCallsExternal();
vm.expectRevert("SphereX error: disallowed tx pattern");
costumer_contract.try_allowed_flow();
}
function test_factorySetup() public virtual {
spherex_engine.grantRole(spherex_engine.SENDER_ADDER_ROLE(), address(costumer_contract));
allowed_cf_storage =
[to_int256(costumer_contract.factory.selector), -to_int256(costumer_contract.factory.selector)];
addAllowedPattern();
address someContract = costumer_contract.factory();
assertEq(
SphereXProtectedBase(someContract).sphereXEngine(), SphereXProtected(costumer_contract).sphereXEngine()
);
assertEq(SphereXProtectedBase(someContract).sphereXAdmin(), SphereXProtected(costumer_contract).sphereXAdmin());
assertEq(
SphereXProtectedBase(someContract).sphereXOperator(), SphereXProtected(costumer_contract).sphereXOperator()
);
}
function test_factoryAllowedSender() public virtual {
spherex_engine.grantRole(spherex_engine.SENDER_ADDER_ROLE(), address(costumer_contract));
allowed_cf_storage =
[to_int256(costumer_contract.factory.selector), -to_int256(costumer_contract.factory.selector)];
addAllowedPattern();
address someContract = costumer_contract.factory();
// If the factory failed to add the contract to allowed sender
// we would get SphereX error: disallowed sender.
vm.expectRevert("SphereX error: disallowed tx pattern");
SomeContract(someContract).someFunc();
}
function test_factoryfailsAllowedSender() public virtual {
vm.expectRevert("SphereX error: sender adder required");
address someContract = costumer_contract.factory();
}
function test_factory_callCreatedContract() public virtual {
spherex_engine.grantRole(spherex_engine.SENDER_ADDER_ROLE(), address(costumer_contract));
allowed_cf_storage =
[to_int256(costumer_contract.factory.selector), -to_int256(costumer_contract.factory.selector)];
addAllowedPattern();
allowed_cf_storage = [to_int256(SomeContract.someFunc.selector), -to_int256(SomeContract.someFunc.selector)];
addAllowedPattern();
address someContract = costumer_contract.factory();
SomeContract(someContract).someFunc();
}
function test_factoryEngineDisabled() public virtual {
spherex_engine.grantRole(spherex_engine.SENDER_ADDER_ROLE(), address(costumer_contract));
// deactivate the engine and check that the call to create the factory
// does not fail.
spherex_engine.deactivateAllRules();
address someContract = costumer_contract.factory();
// activate the engine and see that the new contract is allowed and the pattern is not
spherex_engine.configureRules(PREFIX_TX_FLOW);
vm.expectRevert("SphereX error: disallowed tx pattern");
SomeContract(someContract).someFunc();
}
function test_grantSenderAdderRoleOnlyOperator() public virtual {
allowed_cf_storage =
[to_int256(costumer_contract.factory.selector), -to_int256(costumer_contract.factory.selector)];
addAllowedPattern();
allowed_cf_storage = [to_int256(SomeContract.someFunc.selector), -to_int256(SomeContract.someFunc.selector)];
addAllowedPattern();
spherex_engine.revokeRole(spherex_engine.OPERATOR_ROLE(), address(this));
spherex_engine.grantRole(spherex_engine.OPERATOR_ROLE(), address(1));
vm.prank(address(1));
spherex_engine.grantSenderAdderRole(address(costumer_contract));
address someContract = costumer_contract.factory();
SomeContract(someContract).someFunc();
}
function test_grantSenderAdderRoleAdminRevert() public {
spherex_engine.revokeRole(spherex_engine.OPERATOR_ROLE(), address(this));
spherex_engine.grantRole(spherex_engine.OPERATOR_ROLE(), address(1));
vm.expectRevert("SphereX error: operator required");
spherex_engine.grantSenderAdderRole(address(costumer_contract));
}
function test_changeSphereXEngine_from_protected_function_engine_on() public virtual {
spherex_engine.configureRules(CF);
allowed_cf_storage =
[to_int256(costumer_contract.setEngine.selector), -to_int256(costumer_contract.setEngine.selector)];
addAllowedPattern();
allowed_cf_storage = [
to_int256(costumer_contract.publicFunction.selector),
-to_int256(costumer_contract.publicFunction.selector)
];
addAllowedPattern();
// call the publicFunction and see it doesnt revert
costumer_contract.publicFunction();
SphereXEngine spherex_engine_2 = new SphereXEngine();
spherex_engine_2.configureRules(CF);
allowed_senders.push(address(costumer_contract));
spherex_engine_2.addAllowedSender(allowed_senders);
costumer_contract.changeSphereXOperator(address(costumer_contract));
costumer_contract.setEngine(address(spherex_engine_2));
// after successfully cahging the engine call the publicFunction function and expect revert
vm.expectRevert("SphereX error: disallowed tx pattern");
costumer_contract.publicFunction();
}
}