Skip to content
This repository was archived by the owner on Dec 4, 2021. It is now read-only.

Commit 4092717

Browse files
Caleb HooverCaleb Hoover
Caleb Hoover
authored and
Caleb Hoover
committed
fixed recursive checking after splitting check.js into scope.js and custom.js
1 parent b20b224 commit 4092717

File tree

7 files changed

+265
-152
lines changed

7 files changed

+265
-152
lines changed

bin/check

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ var yargs = require('yargs')
88
.describe('o', 'if -j, will output json into a file: <file>_log. If a file is given, outputs to that it. Default is false.')
99
.showHelpOnFail(false);
1010

11-
var check = require('../check.js'),
12-
Scope = require('../scope.js'),
13-
colors = require('colors'),
11+
var colors = require('colors'),
1412
fs = require('fs'),
1513
path = require('path'),
1614
_ = require('underscore'),
@@ -38,10 +36,9 @@ if (argv.h) {
3836
process.exit();
3937
}
4038

41-
check.flags.verbose = argv.v;
42-
check.flags.recursive = argv.r;
43-
if (argv.j)
44-
check.flags.json = argv.j;
39+
var check = require('../check.js');
40+
check.setFlags({verbose: argv.v, recursive: argv.r, json: argv.j});
41+
var Scope = check.Scope;
4542

4643
if (!argv.j)
4744
console.log(' ---- '.yellow, file.white);
@@ -50,12 +47,14 @@ var scope = new Scope({
5047
file: file
5148
});
5249

53-
var ast = check.astFromFile(scope.file);
54-
check.traverse(ast, scope);
50+
var ast = check.astFromFile(file);
51+
if (ast) {
52+
check.traverse(ast, scope);
5553

56-
if (check.flags.json) {
57-
if (check.reports.length !== 0)
58-
console.log(require('prettyjson').render(check.reports));
59-
else
60-
console.log(colors.green('No vulneralbities found'));
54+
if (check.flags.json) {
55+
if (check.reports.length !== 0)
56+
console.log(require('prettyjson').render(check.reports));
57+
else
58+
console.log(colors.green('No vulneralbities found'));
59+
}
6160
}

bin/modlist

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env node
2+
3+
var yargs = require('yargs'),
4+
colors = require('colors'),
5+
fs = require('fs'),
6+
path = require('path'),
7+
_ = require('underscore'),
8+
esprima = require('esprima'),
9+
estraverse = require('estraverse'),
10+
resolve = require('resolve'),
11+
file = process.argv[2];
12+
13+
var argv = yargs.argv;
14+
15+
if (!file) {
16+
yargs.showHelp();
17+
process.exit();
18+
}
19+
20+
file = path.resolve(file);
21+
22+
if (!fs.existsSync(file)) {
23+
console.error(file, 'doesn\'t exist.');
24+
process.exit();
25+
} else if (!fs.lstatSync(file).isFile()) {
26+
console.error(file, 'is not a file.');
27+
process.exit();
28+
}
29+
30+
var lookupTable = {};
31+
var baseFile = file;
32+
33+
var resolvePath = function(file, parent) {
34+
var pkg;
35+
if (file.indexOf('./') === 0 || file.indexOf('../') === 0) {
36+
if (path.extname(file) == '.json') {
37+
return false;
38+
}
39+
}
40+
41+
try {
42+
pkg = resolve.sync(file, {basedir: parent.split('/').slice(0,-1).join('/')});
43+
} catch (e) {
44+
console.error(String(e));
45+
return false;
46+
}
47+
48+
return file == pkg ? false : pkg;
49+
};
50+
51+
var resolveFile = function(file, scope) {
52+
if (!file)
53+
return;
54+
var j = {};
55+
56+
var input = String(fs.readFileSync(file));
57+
input = _.filter(input.split('\n'), function(l) {return (l[0] + l[1])!="#!";}).join('\n');
58+
59+
var ast = esprima.parse(input, {loc: true});
60+
estraverse.traverse(ast, {
61+
enter: function (node, parent) {
62+
// assertions
63+
if (node.type != 'CallExpression')
64+
return;
65+
if (node.callee.type != 'Identifier')
66+
return;
67+
if (node.callee.name != 'require')
68+
return;
69+
if (!node.arguments[0].value)
70+
return;
71+
72+
var arg = node.arguments[0].value;
73+
74+
var path = resolvePath(arg, file);
75+
if (!argv.j) {
76+
if (fs.existsSync(path) || path === false)
77+
console.log(scope, colors.green(arg));
78+
else
79+
console.log(scope, colors.red(arg));
80+
}
81+
if (lookupTable[arg])
82+
return;
83+
lookupTable[arg] = true;
84+
j[arg] = resolveFile(path, scope + ' --') || {};
85+
86+
}
87+
});
88+
89+
return j;
90+
};
91+
92+
var list = resolveFile(file, '-');
93+
// console.log(list);
94+
if (argv.j)
95+
console.log(require('prettyjson').render(list));

0 commit comments

Comments
 (0)