forked from protofire/solhint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity-rules.js
275 lines (204 loc) · 8.86 KB
/
security-rules.js
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
const assert = require('assert');
const linter = require('./../lib/index');
const contractWith = require('./common/contract-builder').contractWith;
const funcWith = require('./common/contract-builder').funcWith;
const { noIndent } = require('./common/configs');
const { assertWarnsCount, assertErrorMessage, assertNoWarnings, assertErrorCount } = require('./common/asserts');
describe('Linter - SecurityRules', function () {
it('should return pragma error', function () {
const report = linter.processStr('pragma solidity ^0.4.4;');
assertWarnsCount(report, 1);
assertErrorMessage(report, 'Compiler');
});
it('should return compiler version error', function () {
const report = linter.processStr('pragma solidity 0.3.4;');
assert.equal(report.errorCount, 1);
assert.ok(report.reports[0].message.includes('0.4'));
});
it('should return "send" call verification error', function () {
const code = funcWith('x.send(55);');
const report = linter.processStr(code, noIndent());
assert.equal(report.errorCount, 1);
assert.ok(report.reports[0].message.includes('send'));
});
it('should return "call.value" verification error', function () {
const code = funcWith('x.call.value(55)();');
const report = linter.processStr(code, noIndent());
assert.equal(report.errorCount, 1);
assert.ok(report.reports[0].message.includes('call.value'));
});
it('should return required visibility error', function () {
const code = contractWith('function b() { }');
const report = linter.processStr(code, noIndent());
assert.equal(report.warningCount, 1);
assert.ok(report.reports[0].message.includes('visibility'));
});
it('should return required visibility error for state', function () {
const code = contractWith('uint a;');
const report = linter.processStr(code, noIndent());
assert.equal(report.warningCount, 1);
assert.ok(report.reports[0].message.includes('visibility'));
});
it('should return that fallback must be simple', function () {
const code = contractWith(`function () public payable {
make1();
make2();
make3();
}`);
const report = linter.processStr(code, noIndent());
assert.equal(report.warningCount, 1);
assert.ok(report.reports[0].message.includes('Fallback'));
});
it('should return error that function and event names are similar', function () {
const code = contractWith(`
event Name1();
function name1() public payable { }
`);
const report = linter.processStr(code, noIndent());
assert.equal(report.warningCount, 1);
assert.ok(report.reports[0].message.includes('Event and function names must be different'));
});
it('should return error that function and event names are similar', function () {
const code = contractWith(`
function name1() public payable { }
event Name1();
`);
const report = linter.processStr(code, noIndent());
assert.equal(report.warningCount, 1);
assert.ok(report.reports[0].message.includes('Event and function names must be different'));
});
// it('should return error that external contract is not marked as trusted / untrusted', function () {
// const code = funcWith('Bank.withdraw(100);');
//
// const report = linter.processStr(code, config());
//
// assert.equal(report.warningCount, 1);
// assert.ok(report.reports[0].message.includes('trusted'));
// });
const DEPRECATION_ERRORS = ['sha3("test");', 'throw;', 'suicide();'];
DEPRECATION_ERRORS.forEach(curText =>
it(`should return error that used deprecations ${curText}`, function () {
const code = funcWith(curText);
const report = linter.processStr(code, noIndent());
assert.equal(report.errorCount, 1);
assert.ok(report.reports[0].message.includes('deprecate'));
}));
it('should return error that multiple send calls used in transation', function () {
const code = funcWith(`
uint aRes = a.send(1);
uint bRes = b.send(2);
`);
const report = linter.processStr(code, noIndent());
assert.equal(report.errorCount, 1);
assert.ok(report.reports[0].message.includes('multiple'));
});
it('should return error that multiple send calls used in loop', function () {
const code = funcWith(`
while (ac > b) { uint res = a.send(1); }
`);
const report = linter.processStr(code, noIndent());
assertErrorCount(report, 1);
assertErrorMessage(report, 'multiple');
});
it('should return error that used tx.origin', function () {
const code = funcWith(`
uint aRes = tx.origin;
`);
const report = linter.processStr(code, noIndent());
assert.equal(report.errorCount, 1);
assertErrorMessage(report, 'origin');
});
const TIME_BASED_LOGIC = [
funcWith('now >= start + daysAfter * 1 days;'),
funcWith('start >= block.timestamp + daysAfter * 1 days;')
];
TIME_BASED_LOGIC.forEach(curCode =>
it('should return warn when business logic rely on time', function () {
const report = linter.processStr(curCode, noIndent());
assertWarnsCount(report, 1);
assertErrorMessage(report, 'time');
}));
it('should return warn when function use inline assembly', function () {
const code = funcWith(' assembly { "test" } ');
const report = linter.processStr(code, noIndent());
assertWarnsCount(report, 1);
assertErrorMessage(report, 'assembly');
});
it('should return warn when function rely on block has', function () {
const code = funcWith('end >= block.blockhash + daysAfter * 1 days;');
const report = linter.processStr(code, noIndent());
assertWarnsCount(report, 1);
assertErrorMessage(report, 'block.blockhash');
});
describe('Reentrancy', function () {
const REENTRANCY_ERROR = [
contractWith(`
mapping(address => uint) private shares;
function b() external {
uint amount = shares[msg.sender];
bool a = msg.sender.send(amount);
if (a) { shares[msg.sender] = 0; }
}
`),
contractWith(`
mapping(address => uint) private shares;
function b() external {
uint amount = shares[msg.sender];
msg.sender.transfer(amount);
shares[msg.sender] = 0;
}
`)
];
REENTRANCY_ERROR.forEach(curCode =>
it('should return warn when code contains possible reentrancy', function () {
const report = linter.processStr(curCode, noIndent());
assertWarnsCount(report, 1);
assertErrorMessage(report, 'reentrancy');
})
);
const NO_REENTRANCY_ERRORS = [
contractWith(`
mapping(address => uint) private shares;
function b() external {
uint amount = shares[msg.sender];
shares[msg.sender] = 0;
msg.sender.transfer(amount);
}
`),
contractWith(`
mapping(address => uint) private shares;
function b() external {
uint amount = shares[msg.sender];
user.test(amount);
shares[msg.sender] = 0;
}
`),
funcWith(`
uint[] shares;
uint amount = shares[msg.sender];
msg.sender.transfer(amount);
shares[msg.sender] = 0;
`)
];
NO_REENTRANCY_ERRORS.forEach(curCode =>
it('should not return warn when code do not contains transfer', function () {
const report = linter.processStr(curCode, noIndent());
assertNoWarnings(report);
})
);
});
describe('Avoid low level calls', function () {
const LOW_LEVEL_CALLS = [
funcWith('msg.sender.call(code);'),
funcWith('a.callcode(test1);'),
funcWith('a.delegatecall(test1);')
];
LOW_LEVEL_CALLS.forEach(curCode =>
it('should return warn when code contains possible reentrancy', function () {
const report = linter.processStr(curCode, noIndent());
assertWarnsCount(report, 1);
assertErrorMessage(report, 'low level');
})
);
});
});