forked from muellerberndt/sabre
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sabre.js
executable file
·151 lines (115 loc) · 4.89 KB
/
sabre.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
#!/usr/bin/env node
const solc = require('solc');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const ora = require('ora');
const requireFromString = require('require-from-string');
const client = require('./lib/client');
const compiler = require('./lib/compiler');
const report = require('./lib/report');
const releases = require('./lib/releases');
const util = require('util');
let ethAddress = process.env.MYTHX_ETH_ADDRESS;
let password = process.env.MYTHX_PASSWORD;
const args = require('minimist')(process.argv.slice(2), {
boolean: [ 'noCacheLookup', 'debug', 'sendAST' ],
default: { mode: 'quick' },
});
const helpText = `Minimum viable CLI for the MythX security analysis platform.
USAGE:
$ sabre [options] <solidity_file>
OPTIONS:
--mode <quick/full> Analysis mode (default=quick)
--clientToolName <string> Override clientToolNames
--noCacheLookup Deactivate MythX cache lookups
--sendAST Submit AST instead of source code
--debug Print MythX API request and response
`;
if (!args._.length) {
console.log(helpText);
process.exit(-1);
}
if (!['quick', 'full'].includes(args.mode)) {
console.log('Invalid analysis mode. Please use either "quick" or "full".');
process.exit(-1);
}
const solidity_file_path = args._[0];
const solidity_file_name = path.basename(solidity_file_path);
if (!(ethAddress && password)) {
ethAddress = '0x0000000000000000000000000000000000000000';
password = 'trial';
}
let solidity_code;
try {
solidity_code = fs.readFileSync(solidity_file_path, 'utf8');
} catch (err) {
console.log('Error opening input file' + err.message);
process.exit(-1);
}
const solidity_file_dir = path.dirname(solidity_file_path);
/* Parse all the import sources and add them to the `sourceList` */
const { sources, sourceList } = compiler.parseImports(solidity_code, solidity_file_dir);
/* Add original solidity file to the last of the list */
sourceList.push(solidity_file_name);
/* Get the input config for the Solidity Compiler */
const input = compiler.getSolcInput(solidity_file_name, solidity_code, sources);
/* Get the version of the Solidity Compiler */
const version = compiler.getSolidityVersion(solidity_code);
const solcSpinner = ora({ text: `Downloading solc v${version}`, color: 'yellow', spinner: 'bouncingBar' }).start();
try {
compiler.loadSolcVersion(releases[version], (solcString) => {
solcSpinner.succeed(`Compiled with solc v${version} successfully`);
// NOTE: `solcSnapshot` has the same interface as `solc`
const solcSnapshot = solc.setupMethods(requireFromString(solcString), 'soljson-' + releases[version] + '.js');
let compiledData;
try {
compiledData = compiler.getCompiledContracts(input, solcSnapshot, solidity_file_name);
} catch (e) {
console.log(chalk.red(e.message));
process.exit(1);
}
const data = client.getRequestData(
input,
compiledData,
sourceList,
solidity_file_name,
args.sendAST
);
if (args.debug) {
console.log('-------------------');
console.log('MythX Request Body:\n');
console.log(util.inspect(data, false, null, true /* enable colors */));
}
const mythxSpinner = ora({ text: 'Analyzing ' + compiledData.contractName, color: 'yellow', spinner: 'bouncingBar' }).start();
client.getMythXReport(args, ethAddress, password, data)
.then(result => {
// Stop the spinner and clear from the terminal
mythxSpinner.stop();
/* Add all the imported contracts source code to the `data` to sourcemap the issue location */
data.sources = { ...input.sources };
if (args.debug){
console.log('-------------------');
console.log('MythX Response Body:\n');
console.log(util.inspect(result, { showHidden: false, depth: null }));
console.log('-------------------');
}
const { issues } = result;
const uniqueIssues = report.formatIssues(data, issues);
if (uniqueIssues.length === 0) {
console.log(chalk.green('✔ No errors/warnings found in ' + solidity_file_path));
} else {
const formatter = report.getFormatter();
console.log(formatter(uniqueIssues));
}
})
.catch(err => {
// Stop the spinner and clear from the terminal
mythxSpinner.stop();
console.log(chalk.red(err));
});
});
} catch (err) {
solcSpinner.fail(`Compilation with solc v${version} failed`);
console.log(chalk.red(err));
}