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

Commit 78c0349

Browse files
Caleb HooverCaleb Hoover
Caleb Hoover
authored and
Caleb Hoover
committed
checks express.post and express.get along with binary expressions as arguments
1 parent 9493c41 commit 78c0349

File tree

1 file changed

+48
-29
lines changed

1 file changed

+48
-29
lines changed

check.js

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,38 @@ function(scope, node, ce) { // http.get
7676

7777
return true;
7878

79+
}, function(scope, node, ce) {// (new require('express').Router()).route() && .post()
80+
var ceName = scope.resolve(ce.name);
81+
if (typeof ceName != "string" || ceName.indexOf('express') == -1)
82+
return false;
83+
if (['post', 'get'].indexOf(ceName.split('.').slice(-1)[0] != 'post') == -1)
84+
return false;
85+
86+
if (ce.arguments && ce.arguments[1]) {
87+
var func = ce.arguments[1];
88+
89+
if (func && func.scope) {
90+
func.scope.sources.push(func.params[0]);
91+
func.scope.log('SOURCE', node, func.params[0]);
92+
traverse(func.body, func.scope);
93+
94+
}
95+
}
96+
97+
return true;
98+
7999
}, function(scope, node, ce) {// (new require('hapi').server()).route()
80100
var ceName = scope.resolve(ce.name);
81101
if (ceName != 'require(\'fs\').readFile') {
82102
return false;
83103
}
84104

85105
var func = ce.arguments[2]; // the callback
86-
87-
func.scope.sources.push(func.params[1]); // data
88-
func.scope.log('SOURCE', node, func.params[1]);
89-
traverse(func.body, func.scope);
106+
if (func) {
107+
func.scope.sources.push(func.params[1]); // data
108+
func.scope.log('SOURCE', node, func.params[1]);
109+
traverse(func.body, func.scope);
110+
}
90111
return true;
91112
}, function(scope, node, ce) { // require
92113
if (ce.name != 'require')
@@ -105,8 +126,8 @@ function(scope, node, ce) { // http.get
105126
return;
106127
}
107128

108-
if (file == 'hapi' || file.indexOf('hapi') != -1) // just ignore anything hapi
109-
return;
129+
if (['hapi', 'express', 'jade'].indexOf(file) != -1 || file.indexOf('hapi') != -1)
130+
return; // just ignore these things
110131

111132
scope.resolvePath(file, function (pkg) {
112133
if (!pkg)
@@ -239,7 +260,11 @@ Scope.prototype.resolveStatement = function(node) {
239260
return false;
240261
var resolved = scope.resolve(arg);
241262

242-
if (scope.isSource(arg.name || arg) || scope.isSource(resolved.name || resolved)) {
263+
if (scope.isSource(arg.name || arg) || scope.isSource(resolved.name || resolved) ||
264+
arg.left?_.some(climbBE(arg), function (a) {
265+
var r = scope.resolve(a);
266+
return scope.isSource(a.name || a) || scope.isSource(r.name || r);}):false) {
267+
243268
if (scope.isSink(ceName)) {
244269
scope.log('SINK', node, ce.raw, ceName);
245270
return true;
@@ -251,8 +276,8 @@ Scope.prototype.resolveStatement = function(node) {
251276
return false;
252277
});
253278

254-
if (flags.verbose || t[0] == 'S')
255-
this.log(t, node, ce.raw, ceName);
279+
if ((flags.verbose || t[0] == 'S') && typeof ceName == 'string')
280+
this.log(t, node, ce.raw, typeof ceName == 'string'?ceName:{});
256281

257282
return ce;
258283
case 'AssignmentExpression':
@@ -381,7 +406,10 @@ Scope.prototype.resolveExpression = function(right, isSourceCB) {
381406
return false;
382407
var resolved = scope.resolve(arg);
383408

384-
if (scope.isSource(arg.name || arg) || scope.isSource(resolved.name || resolved)) {
409+
if (scope.isSource(arg.name || arg) || scope.isSource(resolved.name || resolved) ||
410+
arg.left?_.some(climbBE(arg), function (a) {
411+
var r = scope.resolve(a);
412+
return scope.isSource(a.name || a) || scope.isSource(r.name || r);}):false) {
385413

386414
if (scope.isSink(ceName)) {
387415
scope.log('SINK', right, ce.raw, ceName);
@@ -400,7 +428,7 @@ Scope.prototype.resolveExpression = function(right, isSourceCB) {
400428
}
401429

402430
if (flags.verbose || t[0] == 'S')
403-
this.log(t, right, ce.raw, ceName);
431+
this.log(t, right, ce.raw, typeof ceName == 'string'?ceName:{});
404432

405433
return ce;
406434
case 'MemberExpression': // a.b.c.d
@@ -478,7 +506,7 @@ Scope.prototype.resolveCallExpression = function(node) {
478506

479507
Scope.prototype.resolveForStatement = function(node) {
480508
var fs = {};
481-
/* in ECMAScript 5 for statements do not create their own scope,
509+
/* in ECMAScript 5, for statements do not create their own scope,
482510
* so create a variable, then track it in current scope */
483511
if (node.init && node.init.declarations)
484512
for (var i = 0; i < node.init.declarations.length; i++) {
@@ -567,7 +595,6 @@ Scope.prototype.traverse = function(ast, returnCB) {
567595
scope.log('SOURCES', ast, scope.sources);
568596
}
569597

570-
571598
if (ast.type == 'BlockStatement'){
572599
(ast.body || [ast]).forEach(function (node) {
573600
if (node.type == 'ExpressionStatement')
@@ -592,10 +619,6 @@ Scope.prototype.resolvePath = function(file, cb) {
592619
var pkg;
593620
if (file.indexOf('./') === 0 || file.indexOf('../') === 0) {
594621
if (path.extname(file) == '.json') {
595-
// input = JSON.parse(input);
596-
// if (Array.isArray(input)) {
597-
// input.forEach(cb);
598-
// }
599622
return false;
600623
}
601624
}
@@ -631,9 +654,8 @@ Scope.prototype.isSource = function(name) {
631654
Scope.prototype.isSink = function(name) {
632655
if (typeof name != 'string')
633656
return false;
634-
// console.log(name);
657+
635658
for (var i in this.sinks) {
636-
// console.log('\t', this.sinks[i], name.search(this.sinks[i]));
637659
if (name.search(this.sinks[i]) === 0) {
638660
return true;
639661
}
@@ -694,16 +716,13 @@ climb = module.exports.climb = function(ast) {
694716
}
695717
};
696718

719+
climbBE = module.exports.climbBE = function (be, func) {
720+
if (!be.left)
721+
return be;
722+
return[be.left.left?climbBE(be.left):be.left, be.right.left?climbBE(be.right):be.right];
723+
};
724+
697725
// Convience function to return the line of a node assuming a node has one.
698726
module.exports.pos = pos = function(node) {
699727
return node.loc ? String(node.loc.start.line) : "-1";
700-
};
701-
702-
// function get(json, key) {
703-
// keys = key.split('.');
704-
// if (keys.length == 1)
705-
// return json[key];
706-
// else {
707-
// return get(json[keys[0]], keys.slice(1));
708-
// }
709-
// }
728+
};

0 commit comments

Comments
 (0)