-
Notifications
You must be signed in to change notification settings - Fork 23
/
helpers.js
328 lines (283 loc) · 10 KB
/
helpers.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
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
const chalk = require('chalk');
const eslintHelpers = require('./eslint');
const fs = require('fs');
const https = require('https');
const parser = require('solidity-parser-antlr');
const path = require('path');
const releases = require('./releases');
const SourceMappingDecoder = require('remix-lib/src/sourceMappingDecoder');
const mythx2Severity = {
High: 2,
Medium: 1,
};
const decoder = new SourceMappingDecoder();
/**
* @returns ESLint formatter module
*/
const getFormatter = () => {
try {
return require('eslint/lib/formatters/stylish');
} catch (ex) {
ex.message = `\nThere was a problem loading formatter option: stylish \nError: ${
ex.message
}`;
throw ex;
}
};
/**
* Turn a srcmap entry (the thing between semicolons) into a line and
* column location.
* We make use of this.sourceMappingDecoder of this class to make
* the conversion.
*
* @param {string} srcEntry - a single entry of solc sourceMap
* @param {Array} lineBreakPositions - array returned by the function 'mapLineBreakPositions'
* @returns {line: number, column: number}
*/
const textSrcEntry2lineColumn = (srcEntry, lineBreakPositions) => {
const ary = srcEntry.split(':');
const sourceLocation = {
length: parseInt(ary[1], 10),
start: parseInt(ary[0], 10),
};
const loc = decoder.convertOffsetToLineColumn(sourceLocation, lineBreakPositions);
// FIXME: note we are lossy in that we don't return the end location
if (loc.start) {
// Adjust because routines starts lines at 0 rather than 1.
loc.start.line++;
}
if (loc.end) {
loc.end.line++;
}
return [loc.start, loc.end];
};
/**
* Convert a MythX issue into an ESLint-style issue.
* The eslint report format which we use, has these fields:
*
* - column,
* - endCol,
* - endLine,
* - fatal,
* - line,
* - message,
* - ruleId,
* - severity
*
* but a MythX JSON report has these fields:
*
* - description.head
* - description.tail,
* - locations
* - severity
* - swcId
* - swcTitle
*
* @param {object} issue - the MythX issue we want to convert
* @param {string} source - holds the contract code
* @returns eslint-issue object
*/
const issue2EsLint = (issue, source) => {
let swcLink;
if (!issue.swcID) {
swcLink = 'N/A';
} else {
swcLink = 'https://smartcontractsecurity.github.io/SWC-registry/docs/' + issue.swcID;
}
const esIssue = {
fatal: false,
ruleId: swcLink,
message: `${issue.description.head}`,
severity: mythx2Severity[issue.severity] || 1,
mythXseverity: issue.severity,
line: -1,
column: 0,
endLine: -1,
endCol: 0,
};
let startLineCol, endLineCol;
const lineBreakPositions = decoder.getLinebreakPositions(source);
if (issue.locations.length) {
const srcEntry = issue.locations[0].sourceMap.split(';')[0];
[startLineCol, endLineCol] = textSrcEntry2lineColumn(srcEntry, lineBreakPositions);
}
if (startLineCol) {
esIssue.line = startLineCol.line;
esIssue.column = startLineCol.column;
esIssue.endLine = endLineCol.line;
esIssue.endCol = endLineCol.column;
}
return esIssue;
};
/**
* Gets the source index from the issue sourcemap
* @param {object} issue - issue item from the collection MythX analyze API output
* @returns {number}
*/
const getSourceIndex = issue => {
if (issue.locations.length) {
const sourceMapRegex = /(\d+):(\d+):(\d+)/g;
const match = sourceMapRegex.exec(issue.locations[0].sourceMap);
// Ignore `-1` source index for compiler generated code
return match ? match[3] : 0;
}
return 0;
};
/**
* Converts MythX analyze API output item to Eslint compatible object
* @param {object} report - issue item from the collection MythX analyze API output
* @param {object} data - Contains array of solidity contracts source code and the input filepath of contract
* @returns {object}
*/
const convertMythXReport2EsIssue = (report, data) => {
const { issues, sourceList } = report;
const results = {};
issues.map(issue => {
const sourceIndex = getSourceIndex(issue);
/**
* MythX API sends `sourceList` with `/` added in the contract name
* Example: sourceList: [ 'token.sol', '/token.sol' ]
*
* TODO: Remove the `replace` hack by fixing the `sourceList` response from MythX API
*/
const filePath = sourceList[sourceIndex].replace(/^\//, '');
if (!results[filePath]) {
results[filePath] = {
errorCount: 0,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
filePath,
messages: [],
};
}
results[filePath].messages.push(issue2EsLint(issue, data.sources[filePath].content));
});
for (let k in results) {
if (results.hasOwnProperty(k)) {
results[k].warningCount = results[k].messages.reduce((acc, { fatal, severity }) =>
!eslintHelpers.isFatal(fatal , severity) ? acc + 1: acc, 0);
results[k].errorCount = results[k].messages.reduce((acc, { fatal, severity }) =>
eslintHelpers.isFatal(fatal , severity) ? acc + 1: acc, 0);
}
}
return Object.values(results);
};
const doReport = (data, issues) => {
const eslintIssues = issues
.map(report => convertMythXReport2EsIssue(report, data))
.reduce((acc, curr) => acc.concat(curr), []);
const uniqueIssues = eslintHelpers.getUniqueIssues(eslintIssues);
if (uniqueIssues.length === 0) {
console.log(chalk.green('✔ No errors/warnings found in ' + data.filePath));
} else {
const formatter = getFormatter();
console.log(formatter(uniqueIssues));
}
};
const getSolidityVersion = fileContents => {
try {
const ast = parser.parse(fileContents, {});
let solidityVersion = releases.latest;
let reg = RegExp(/[><=^]/, 'g');
for (let n of ast.children) {
if ((n.name === 'solidity') && (n.type === 'PragmaDirective')) {
solidityVersion = n.value;
if (!reg.test(solidityVersion)) {
return solidityVersion;
}
break;
}
}
if (solidityVersion !== releases.latest) {
solidityVersion = solidityVersion.replace(/[\^v]/g, '');
let upperLimit = 'latest';
if (solidityVersion.indexOf('<') !== -1) {
if (solidityVersion.indexOf('<=') !== -1) {
solidityVersion = solidityVersion.substring(solidityVersion.length - 5, solidityVersion.length);
} else {
upperLimit = solidityVersion.substring(solidityVersion.length - 5, solidityVersion.length);
}
} else if (solidityVersion.indexOf('>') !== -1) {
solidityVersion = releases.latest;
} else {
upperLimit = '0.' + (parseInt(solidityVersion[2]) + 1).toString() + '.0';
}
if (upperLimit !== 'latest') {
if (upperLimit === '0.6.0' || upperLimit === '0.7.0') {
solidityVersion = releases.latest;
} else if (upperLimit === '0.5.0') {
solidityVersion = '0.4.25';
} else if (upperLimit === '0.4.0') {
solidityVersion = '0.3.6';
} else if (upperLimit === '0.3.0') {
solidityVersion = '0.2.2';
} else {
let x = parseInt(upperLimit[upperLimit.length - 1], 10) - 1;
solidityVersion = '';
for (let i = 0; i < upperLimit.length - 1; i++) {
solidityVersion += upperLimit[i];
}
solidityVersion += x.toString();
}
}
}
return solidityVersion;
} catch (error) {
for (let err of error.errors) {
console.error(err.message);
}
}
};
const getImportPaths = source => {
let matches = [];
let ir = /^(.*import){1}(.+){0,1}\s['"](.+)['"];/gm;
let match = null;
while ((match = ir.exec(source))) {
matches.push(match[3]);
}
return matches;
};
const removeRelativePathFromUrl = url => url.replace(/^.+\.\//, '').replace('./', '');
/* Dynamic linking is not supported. */
const regex = new RegExp(/__\$\w+\$__/,'g');
const address = '0000000000000000000000000000000000000000';
const replaceLinkedLibs = byteCode => byteCode.replace(regex, address);
/* Get solc-js of specific version from temp directory, if exists */
const loadSolcVersion = (version, callback) => {
const tempDir = path.dirname(require.main.filename) + '/.temp/';
const solcPath = tempDir + version + '.js';
if (fs.existsSync(solcPath)) {
callback(fs.readFileSync(solcPath).toString());
} else {
/* Create `.temp` directory if it doesn't exist */
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir);
}
/* Get the solc remote version snapshot of the specified version */
const solcJs = fs.createWriteStream(solcPath);
const url = 'https://ethereum.github.io/solc-bin/bin/soljson-' + version + '.js';
https
.get(url, (response) => {
if (response.statusCode !== 200) {
throw new Error('Error retrieving binary: ' + response.statusMessage);
} else {
const stream = response.pipe(solcJs);
stream.on('finish', () => {
callback(fs.readFileSync(solcPath).toString());
});
}
})
.on('error', (error) => {
throw new Error('Error fetching binary: ' + error);
});
}
};
module.exports = {
doReport,
getImportPaths,
getSolidityVersion,
loadSolcVersion,
removeRelativePathFromUrl,
replaceLinkedLibs
};