1
1
/*
2
2
;(function() {eval(String(require('fs').readFileSync(process.argv[1])));})()
3
+
4
+ Terms:
5
+ BE - Binary Expression
6
+ CE - Call Expression (functions)
7
+ SCE - (source) Call Expression
8
+ SCES - (source) Call Expression Statement
9
+ SASSIGN - Assign as a sink
10
+
3
11
*/
4
12
5
13
var fs = require ( 'fs' ) ,
@@ -23,6 +31,7 @@ var cs = {
23
31
'SCE' : colors . red ,
24
32
'SCES' : colors . red ,
25
33
'SINK' : colors . red ,
34
+ 'SASSIGN' : colors . red ,
26
35
'SOURCE' : colors . red ,
27
36
'SOURCES' : colors . yellow ,
28
37
'RETURN' : colors . red
@@ -33,7 +42,7 @@ function log(type, node, name, value) {
33
42
if ( flags . recursive )
34
43
p = path . relative ( baseFile . split ( '/' ) . reverse ( ) . slice ( 1 ) . reverse ( ) . join ( '/' ) , this . file ) + ':' + p ;
35
44
36
- console . log ( cs [ type ] ?cs [ type ] ( '[' + type + ']' ) :colors . blue ( '[' + type + ']' ) ,
45
+ console . log ( ' ' , cs [ type ] ?cs [ type ] ( '[' + type + ']' ) :colors . blue ( '[' + type + ']' ) ,
37
46
colors . grey ( p ) , name , value ? value : '' ) ;
38
47
}
39
48
@@ -70,7 +79,6 @@ function(scope, node, ce) { // http.get
70
79
func . scope . sources . push ( func . params [ 0 ] ) ;
71
80
func . scope . log ( 'SOURCE' , node , false , func . params [ 0 ] ) ;
72
81
traverse ( func . body , func . scope ) ;
73
-
74
82
}
75
83
}
76
84
@@ -116,7 +124,6 @@ function(scope, node, ce) { // http.get
116
124
if ( ! flags . recursive )
117
125
return false ;
118
126
119
-
120
127
var r ;
121
128
if ( ce . arguments [ 0 ] ) {
122
129
var file ;
@@ -140,8 +147,9 @@ function(scope, node, ce) { // http.get
140
147
141
148
var ast = astFromFile ( pkg ) ;
142
149
if ( ast ) {
143
- if ( flags . verbose )
150
+ if ( flags . verbose && ! flags . json )
144
151
console . log ( ' ---- ' . yellow , pkg ) ;
152
+
145
153
var newScope = new Scope ( {
146
154
sinks : sinks ,
147
155
sources : sources ,
@@ -169,15 +177,14 @@ Scope = module.exports.Scope = function(scope) {
169
177
this . vars = scope . vars || { } ;
170
178
if ( ! this . vars . module ) this . vars . module = { exports : { } } ;
171
179
if ( ! this . vars . global ) this . vars . global = { } ;
172
- if ( ! this . vars . process ) this . vars . process = { } ;
173
180
this . sources = scope . sources || sources ;
174
181
this . sinks = scope . sinks || sinks ;
175
182
this . file = scope . file ;
176
183
if ( ! baseFile ) baseFile = scope . file ;
177
184
this . log = scope . log || log ;
178
185
this . leaveScope = scope . leaveScope ;
179
186
this . createNewScope = scope . createNewScope ;
180
- this . reports = [ ] ;
187
+ this . reports = scope . reports || [ { source : { name : 'process.argv' } } ] ;
181
188
} ;
182
189
183
190
// handles creation of variables.
@@ -206,24 +213,18 @@ Scope.prototype.track = function(variable) {
206
213
207
214
// returns a value for a variable if one exists
208
215
Scope . prototype . resolve = function ( name ) {
209
- if ( ! name )
210
- return false ;
211
- else if ( typeof name != 'string' )
216
+ if ( ! name || typeof name != 'string' )
212
217
return false ;
218
+
219
+ if ( get ( this . vars , name ) ) {
220
+ return eval ( 'this.vars.' + name ) ;
221
+ }
222
+ else if ( name . indexOf ( '.' ) != - 1 ) {
223
+ var s = name . split ( '.' ) ;
224
+ var r = this . resolve ( s . slice ( 0 , - 1 ) . join ( '.' ) ) ;
225
+ r = r . raw || r ;
226
+ return r + '.' + s . slice ( - 1 ) ;
213
227
214
- if ( name . indexOf ( '.' ) == - 1 ) {
215
- if ( get ( this . vars , name ) ) {
216
- return eval ( 'this.vars.' + name ) ;
217
- }
218
- } else {
219
- if ( get ( this . vars , name ) ) {
220
- return eval ( 'this.vars.' + name ) ;
221
- } else {
222
- var s = name . split ( '.' ) ;
223
- var r = this . resolve ( s . slice ( 0 , - 1 ) . join ( '.' ) ) ;
224
- r = r . raw || r ;
225
- return r + '.' + s . slice ( - 1 ) ;
226
- }
227
228
}
228
229
229
230
return name ;
@@ -266,7 +267,6 @@ Scope.prototype.resolveStatement = function(node) {
266
267
return false ;
267
268
var resolved = scope . resolve ( arg ) ;
268
269
var source = resolved ;
269
-
270
270
if ( scope . isSource ( arg . name || arg ) || scope . isSource ( resolved . name || resolved ) ||
271
271
( traverseJSON ( arg , function ( a ) {
272
272
if ( ! a ) return false ;
@@ -298,7 +298,10 @@ Scope.prototype.resolveStatement = function(node) {
298
298
if ( value ) {
299
299
var resolved = scope . resolve ( value ) ;
300
300
if ( resolved && typeof resolved == 'string' ) {
301
- if ( scope . isSource ( resolved . name || resolved ) || scope . isSource ( value . name || value ) || isSource ) {
301
+ if ( scope . isSink ( value . name || value ) || scope . isSink ( resolved . name || resolved ) ) {
302
+ scope . sinks . push ( names ) ;
303
+ scope . log ( 'SASSIGN' , node , names . length == 1 ?names [ 0 ] :names , value ) ;
304
+ } else if ( scope . isSource ( resolved . name || resolved ) || scope . isSource ( value . name || value ) ) {
302
305
scope . sources . push ( names ) ;
303
306
scope . log ( 'SOURCE' , node , names . length == 1 ?names [ 0 ] :names , value ) ;
304
307
}
@@ -581,7 +584,8 @@ Scope.prototype.traverse = function(ast, returnCB) {
581
584
var scope = this ;
582
585
if ( flags . verbose ) {
583
586
( scope . createNewScope || function ( ) {
584
- console . log ( 'Creating new scope' . yellow ) ;
587
+ if ( ! flags . json )
588
+ console . log ( 'Creating new scope' . yellow ) ;
585
589
} ) ( ) ;
586
590
scope . log ( 'SOURCES' , ast , scope . sources ) ;
587
591
}
@@ -600,12 +604,12 @@ Scope.prototype.traverse = function(ast, returnCB) {
600
604
this . resolveStatement ( ast . expression || ast ) ;
601
605
}
602
606
603
- // if (flags.verbose)
604
- // (scope.leaveScope || function () {
605
- // console.log('leaving scope'.yellow);
606
- // })();
607
- if ( scope . leaveScope )
608
- scope . leaveScope ( ) ;
607
+ if ( flags . verbose )
608
+ ( scope . leaveScope || function ( ) {
609
+ console . log ( 'leaving scope' . yellow ) ;
610
+ } ) ( ) ;
611
+ // if (scope.leaveScope)
612
+ // scope.leaveScope();
609
613
} ;
610
614
611
615
Scope . prototype . resolvePath = function ( file , cb ) {
@@ -666,7 +670,8 @@ traverse = module.exports.traverse = function(ast, scope) {
666
670
}
667
671
if ( flags . verbose ) {
668
672
( scope . createNewScope || function ( ) {
669
- console . log ( 'Creating new scope' . yellow ) ;
673
+ if ( ! flags . json )
674
+ console . log ( 'Creating new scope' . yellow ) ;
670
675
} ) ( ) ;
671
676
scope . log ( 'SOURCES' , ast , scope . sources ) ;
672
677
}
@@ -677,13 +682,13 @@ traverse = module.exports.traverse = function(ast, scope) {
677
682
scope . resolveStatement ( node ) ;
678
683
} ) ;
679
684
680
- // if (flags.verbose) {
681
- // (scope.leaveScope || function () {
682
- // console.log('leaving scope'.yellow);
683
- // })();
684
- // }
685
- if ( scope . leaveScope )
686
- scope . leaveScope ( ) ;
685
+ if ( flags . verbose ) {
686
+ ( scope . leaveScope || function ( ) {
687
+ console . log ( 'leaving scope' . yellow ) ;
688
+ } ) ( ) ;
689
+ }
690
+ // if (scope.leaveScope)
691
+ // scope.leaveScope();
687
692
} ;
688
693
689
694
astFromFile = module . exports . astFromFile = function ( file , output ) {
0 commit comments