@@ -8,63 +8,52 @@ var fs = require('fs'),
88var server = http . createServer ( function ( req , res ) {
99 let path = url . parse ( req . url , true ) . query . path ;
1010
11- // BAD: This could read any file on the file system
12- res . write ( fs . readFileSync ( path ) ) ;
11+ res . write ( fs . readFileSync ( path ) ) ; // $ Alert - This could read any file on the file system
1312
14- // BAD: This could still read any file on the file system
15- res . write ( fs . readFileSync ( "/home/user/" + path ) ) ;
13+ res . write ( fs . readFileSync ( "/home/user/" + path ) ) ; // $ Alert - This could still read any file on the file system
1614
1715 if ( path . startsWith ( "/home/user/" ) )
18- res . write ( fs . readFileSync ( path ) ) ; // BAD: Insufficient sanitisation
16+ res . write ( fs . readFileSync ( path ) ) ; // $ Alert - Insufficient sanitisation
1917
2018 if ( path . indexOf ( "secret" ) == - 1 )
21- res . write ( fs . readFileSync ( path ) ) ; // BAD: Insufficient sanitisation
19+ res . write ( fs . readFileSync ( path ) ) ; // $ Alert - Insufficient sanitisation
2220
2321 if ( fs . existsSync ( path ) )
24- res . write ( fs . readFileSync ( path ) ) ; // BAD: Insufficient sanitisation
22+ res . write ( fs . readFileSync ( path ) ) ; // $ Alert - Insufficient sanitisation
2523
2624 if ( path === 'foo.txt' )
27- res . write ( fs . readFileSync ( path ) ) ; // GOOD: Path is compared to white-list
25+ res . write ( fs . readFileSync ( path ) ) ; // OK - Path is compared to white-list
2826
2927 if ( path === 'foo.txt' || path === 'bar.txt' )
30- res . write ( fs . readFileSync ( path ) ) ; // GOOD: Path is compared to white-list
28+ res . write ( fs . readFileSync ( path ) ) ; // OK - Path is compared to white-list
3129
3230 if ( path === 'foo.txt' || path === 'bar.txt' || someOpaqueCondition ( ) )
33- res . write ( fs . readFileSync ( path ) ) ; // BAD: Path is incompletely compared to white-list
31+ res . write ( fs . readFileSync ( path ) ) ; // $ Alert - Path is incompletely compared to white-list
3432
3533 path = sanitize ( path ) ;
36- res . write ( fs . readFileSync ( path ) ) ; // GOOD: Path is sanitized
34+ res . write ( fs . readFileSync ( path ) ) ; // OK - Path is sanitized
3735
3836 path = url . parse ( req . url , true ) . query . path ;
39- // GOOD: basename is safe
37+ // OK - basename is safe
4038 res . write ( fs . readFileSync ( pathModule . basename ( path ) ) ) ;
41- // BAD: taint is preserved
42- res . write ( fs . readFileSync ( pathModule . dirname ( path ) ) ) ;
43- // GOOD: extname is safe
39+ res . write ( fs . readFileSync ( pathModule . dirname ( path ) ) ) ; // $ Alert - taint is preserved
40+ // OK - extname is safe
4441 res . write ( fs . readFileSync ( pathModule . extname ( path ) ) ) ;
45- // BAD: taint is preserved
46- res . write ( fs . readFileSync ( pathModule . join ( path ) ) ) ;
47- // BAD: taint is preserved
48- res . write ( fs . readFileSync ( pathModule . join ( x , y , path , z ) ) ) ;
49- // BAD: taint is preserved
50- res . write ( fs . readFileSync ( pathModule . normalize ( path ) ) ) ;
51- // BAD: taint is preserved
52- res . write ( fs . readFileSync ( pathModule . relative ( x , path ) ) ) ;
53- // BAD: taint is preserved
54- res . write ( fs . readFileSync ( pathModule . relative ( path , x ) ) ) ;
55- // BAD: taint is preserved
56- res . write ( fs . readFileSync ( pathModule . resolve ( path ) ) ) ;
57- // BAD: taint is preserved
58- res . write ( fs . readFileSync ( pathModule . resolve ( x , y , path , z ) ) ) ;
59- // BAD: taint is preserved
60- res . write ( fs . readFileSync ( pathModule . toNamespacedPath ( path ) ) ) ;
42+ res . write ( fs . readFileSync ( pathModule . join ( path ) ) ) ; // $ Alert - taint is preserved
43+ res . write ( fs . readFileSync ( pathModule . join ( x , y , path , z ) ) ) ; // $ Alert - taint is preserved
44+ res . write ( fs . readFileSync ( pathModule . normalize ( path ) ) ) ; // $ Alert - taint is preserved
45+ res . write ( fs . readFileSync ( pathModule . relative ( x , path ) ) ) ; // $ Alert - taint is preserved
46+ res . write ( fs . readFileSync ( pathModule . relative ( path , x ) ) ) ; // $ Alert - taint is preserved
47+ res . write ( fs . readFileSync ( pathModule . resolve ( path ) ) ) ; // $ Alert - taint is preserved
48+ res . write ( fs . readFileSync ( pathModule . resolve ( x , y , path , z ) ) ) ; // $ Alert - taint is preserved
49+ res . write ( fs . readFileSync ( pathModule . toNamespacedPath ( path ) ) ) ; // $ Alert - taint is preserved
6150} ) ;
6251
6352var server = http . createServer ( function ( req , res ) {
6453 // tests for a few uri-libraries
65- res . write ( fs . readFileSync ( require ( "querystringify" ) . parse ( req . url ) . query ) ) ; // NOT OK
66- res . write ( fs . readFileSync ( require ( "query-string" ) . parse ( req . url ) . query ) ) ; // NOT OK
67- res . write ( fs . readFileSync ( require ( "querystring" ) . parse ( req . url ) . query ) ) ; // NOT OK
54+ res . write ( fs . readFileSync ( require ( "querystringify" ) . parse ( req . url ) . query ) ) ; // $ Alert
55+ res . write ( fs . readFileSync ( require ( "query-string" ) . parse ( req . url ) . query ) ) ; // $ Alert
56+ res . write ( fs . readFileSync ( require ( "querystring" ) . parse ( req . url ) . query ) ) ; // $ Alert
6857} ) ;
6958
7059( function ( ) {
@@ -100,7 +89,7 @@ var server = http.createServer(function(req, res) {
10089 path = path . replace ( / \. \. / g, '' ) ; // remove all ".."
10190 }
10291
103- res . write ( fs . readFileSync ( path ) ) ; // OK. Is sanitized above.
92+ res . write ( fs . readFileSync ( path ) ) ; // OK - Is sanitized above.
10493} ) ;
10594
10695var server = http . createServer ( function ( req , res ) {
@@ -113,109 +102,109 @@ var server = http.createServer(function(req, res) {
113102 path = path . replace ( / \. \. / g, '' ) ; // remove all ".."
114103 }
115104
116- res . write ( fs . readFileSync ( path ) ) ; // OK. Is sanitized above.
105+ res . write ( fs . readFileSync ( path ) ) ; // OK - Is sanitized above.
117106} ) ;
118107
119108var server = http . createServer ( function ( req , res ) {
120109 let path = url . parse ( req . url , true ) . query . path ;
121110
122- require ( 'send' ) ( req , path ) ; // NOT OK
111+ require ( 'send' ) ( req , path ) ; // $ Alert
123112} ) ;
124113
125114var server = http . createServer ( function ( req , res ) {
126115 let path = url . parse ( req . url , true ) . query . path ;
127116
128- fs . readFileSync ( path ) ; // NOT OK
117+ fs . readFileSync ( path ) ; // $ Alert
129118
130119 var split = path . split ( "/" ) ;
131120
132- fs . readFileSync ( split . join ( "/" ) ) ; // NOT OK
121+ fs . readFileSync ( split . join ( "/" ) ) ; // $ Alert
133122
134- fs . readFileSync ( prefix + split [ split . length - 1 ] ) // OK
123+ fs . readFileSync ( prefix + split [ split . length - 1 ] )
135124
136- fs . readFileSync ( split [ x ] ) // NOT OK
137- fs . readFileSync ( prefix + split [ x ] ) // NOT OK
125+ fs . readFileSync ( split [ x ] ) // $ Alert
126+ fs . readFileSync ( prefix + split [ x ] ) // $ Alert
138127
139128 var concatted = prefix . concat ( split ) ;
140- fs . readFileSync ( concatted . join ( "/" ) ) ; // NOT OK
129+ fs . readFileSync ( concatted . join ( "/" ) ) ; // $ Alert
141130
142131 var concatted2 = split . concat ( prefix ) ;
143- fs . readFileSync ( concatted2 . join ( "/" ) ) ; // NOT OK
132+ fs . readFileSync ( concatted2 . join ( "/" ) ) ; // $ Alert
144133
145- fs . readFileSync ( split . pop ( ) ) ; // NOT OK
134+ fs . readFileSync ( split . pop ( ) ) ; // $ Alert
146135
147136} ) ;
148137
149138var server = http . createServer ( function ( req , res ) {
150139 let path = url . parse ( req . url , true ) . query . path ;
151140
152141 // Removal of forward-slash or dots.
153- res . write ( fs . readFileSync ( path . replace ( / [ \] \[ * , ; ' " ` < > \\ ? \/ ] / g, '' ) ) ) ; // OK.
154- res . write ( fs . readFileSync ( path . replace ( / [ a b c d ] / g, '' ) ) ) ; // NOT OK
155- res . write ( fs . readFileSync ( path . replace ( / [ . / ] / g, '' ) ) ) ; // OK
156- res . write ( fs . readFileSync ( path . replace ( / [ f o o b a r / f o o b a r ] / g, '' ) ) ) ; // OK
157- res . write ( fs . readFileSync ( path . replace ( / \/ / g, '' ) ) ) ; // OK
158- res . write ( fs . readFileSync ( path . replace ( / \. | \/ / g, '' ) ) ) ; // OK
159-
160- res . write ( fs . readFileSync ( path . replace ( / [ . ] / g, '' ) ) ) ; // NOT OK ( can be absolute)
161- res . write ( fs . readFileSync ( path . replace ( / [ . . ] / g, '' ) ) ) ; // NOT OK ( can be absolute)
162- res . write ( fs . readFileSync ( path . replace ( / \. / g, '' ) ) ) ; // NOT OK ( can be absolute)
163- res . write ( fs . readFileSync ( path . replace ( / \. \. | B L A / g, '' ) ) ) ; // NOT OK ( can be absolute)
142+ res . write ( fs . readFileSync ( path . replace ( / [ \] \[ * , ; ' " ` < > \\ ? \/ ] / g, '' ) ) ) ;
143+ res . write ( fs . readFileSync ( path . replace ( / [ a b c d ] / g, '' ) ) ) ; // $ Alert
144+ res . write ( fs . readFileSync ( path . replace ( / [ . / ] / g, '' ) ) ) ;
145+ res . write ( fs . readFileSync ( path . replace ( / [ f o o b a r / f o o b a r ] / g, '' ) ) ) ;
146+ res . write ( fs . readFileSync ( path . replace ( / \/ / g, '' ) ) ) ;
147+ res . write ( fs . readFileSync ( path . replace ( / \. | \/ / g, '' ) ) ) ;
148+
149+ res . write ( fs . readFileSync ( path . replace ( / [ . ] / g, '' ) ) ) ; // $ Alert - can be absolute
150+ res . write ( fs . readFileSync ( path . replace ( / [ . . ] / g, '' ) ) ) ; // $ Alert - can be absolute
151+ res . write ( fs . readFileSync ( path . replace ( / \. / g, '' ) ) ) ; // $ Alert - can be absolute
152+ res . write ( fs . readFileSync ( path . replace ( / \. \. | B L A / g, '' ) ) ) ; // $ Alert - can be absolute
164153
165154 if ( ! pathModule . isAbsolute ( path ) ) {
166- res . write ( fs . readFileSync ( path . replace ( / [ . ] / g, '' ) ) ) ; // OK
167- res . write ( fs . readFileSync ( path . replace ( / [ . . ] / g, '' ) ) ) ; // OK
168- res . write ( fs . readFileSync ( path . replace ( / \. / g, '' ) ) ) ; // OK
169- res . write ( fs . readFileSync ( path . replace ( / \. \. | B L A / g, '' ) ) ) ; // OK
155+ res . write ( fs . readFileSync ( path . replace ( / [ . ] / g, '' ) ) ) ;
156+ res . write ( fs . readFileSync ( path . replace ( / [ . . ] / g, '' ) ) ) ;
157+ res . write ( fs . readFileSync ( path . replace ( / \. / g, '' ) ) ) ;
158+ res . write ( fs . readFileSync ( path . replace ( / \. \. | B L A / g, '' ) ) ) ;
170159 }
171160
172161 // removing of "../" from prefix.
173- res . write ( fs . readFileSync ( "prefix" + pathModule . normalize ( path ) . replace ( / ^ ( \. \. [ \/ \\ ] ) + / , '' ) ) ) ; // OK
174- res . write ( fs . readFileSync ( "prefix" + pathModule . normalize ( path ) . replace ( / ( \. \. [ \/ \\ ] ) + / , '' ) ) ) ; // OK
175- res . write ( fs . readFileSync ( "prefix" + pathModule . normalize ( path ) . replace ( / ( \. \. \/ ) + / , '' ) ) ) ; // OK
176- res . write ( fs . readFileSync ( "prefix" + pathModule . normalize ( path ) . replace ( / ( \. \. \/ ) * / , '' ) ) ) ; // OK
162+ res . write ( fs . readFileSync ( "prefix" + pathModule . normalize ( path ) . replace ( / ^ ( \. \. [ \/ \\ ] ) + / , '' ) ) ) ;
163+ res . write ( fs . readFileSync ( "prefix" + pathModule . normalize ( path ) . replace ( / ( \. \. [ \/ \\ ] ) + / , '' ) ) ) ;
164+ res . write ( fs . readFileSync ( "prefix" + pathModule . normalize ( path ) . replace ( / ( \. \. \/ ) + / , '' ) ) ) ;
165+ res . write ( fs . readFileSync ( "prefix" + pathModule . normalize ( path ) . replace ( / ( \. \. \/ ) * / , '' ) ) ) ;
177166
178- res . write ( fs . readFileSync ( "prefix" + path . replace ( / ^ ( \. \. [ \/ \\ ] ) + / , '' ) ) ) ; // NOT OK - not normalized
179- res . write ( fs . readFileSync ( pathModule . normalize ( path ) . replace ( / ^ ( \. \. [ \/ \\ ] ) + / , '' ) ) ) ; // NOT OK ( can be absolute)
167+ res . write ( fs . readFileSync ( "prefix" + path . replace ( / ^ ( \. \. [ \/ \\ ] ) + / , '' ) ) ) ; // $ Alert - not normalized
168+ res . write ( fs . readFileSync ( pathModule . normalize ( path ) . replace ( / ^ ( \. \. [ \/ \\ ] ) + / , '' ) ) ) ; // $ Alert - can be absolute
180169} ) ;
181170
182171import normalizeUrl from 'normalize-url' ;
183172
184173var server = http . createServer ( function ( req , res ) {
185174 // tests for a few more uri-libraries
186175 const qs = require ( "qs" ) ;
187- res . write ( fs . readFileSync ( qs . parse ( req . url ) . foo ) ) ; // NOT OK
188- res . write ( fs . readFileSync ( qs . parse ( normalizeUrl ( req . url ) ) . foo ) ) ; // NOT OK
176+ res . write ( fs . readFileSync ( qs . parse ( req . url ) . foo ) ) ; // $ Alert
177+ res . write ( fs . readFileSync ( qs . parse ( normalizeUrl ( req . url ) ) . foo ) ) ; // $ Alert
189178 const parseqs = require ( "parseqs" ) ;
190- res . write ( fs . readFileSync ( parseqs . decode ( req . url ) . foo ) ) ; // NOT OK
179+ res . write ( fs . readFileSync ( parseqs . decode ( req . url ) . foo ) ) ; // $ Alert
191180} ) ;
192181
193182const cp = require ( "child_process" ) ;
194183var server = http . createServer ( function ( req , res ) {
195184 let path = url . parse ( req . url , true ) . query . path ;
196- cp . execSync ( "foobar" , { cwd : path } ) ; // NOT OK
197- cp . execFileSync ( "foobar" , [ "args" ] , { cwd : path } ) ; // NOT OK
198- cp . execFileSync ( "foobar" , { cwd : path } ) ; // NOT OK
185+ cp . execSync ( "foobar" , { cwd : path } ) ; // $ Alert
186+ cp . execFileSync ( "foobar" , [ "args" ] , { cwd : path } ) ; // $ Alert
187+ cp . execFileSync ( "foobar" , { cwd : path } ) ; // $ Alert
199188} ) ;
200189
201190var server = http . createServer ( function ( req , res ) {
202191 let path = url . parse ( req . url , true ) . query . path ;
203192
204193 // Removal of forward-slash or dots.
205- res . write ( fs . readFileSync ( path . replace ( new RegExp ( "[\\]\\[*,;'\"`<>\\?/]" , 'g' ) , '' ) ) ) ; // OK
206- res . write ( fs . readFileSync ( path . replace ( new RegExp ( "[\\]\\[*,;'\"`<>\\?/]" , '' ) , '' ) ) ) ; // NOT OK.
207- res . write ( fs . readFileSync ( path . replace ( new RegExp ( "[\\]\\[*,;'\"`<>\\?/]" , unknownFlags ( ) ) , '' ) ) ) ; // OK -- Might be okay depending on what unknownFlags evaluates to.
194+ res . write ( fs . readFileSync ( path . replace ( new RegExp ( "[\\]\\[*,;'\"`<>\\?/]" , 'g' ) , '' ) ) ) ;
195+ res . write ( fs . readFileSync ( path . replace ( new RegExp ( "[\\]\\[*,;'\"`<>\\?/]" , '' ) , '' ) ) ) ; // $ Alert
196+ res . write ( fs . readFileSync ( path . replace ( new RegExp ( "[\\]\\[*,;'\"`<>\\?/]" , unknownFlags ( ) ) , '' ) ) ) ; // OK - Might be okay depending on what unknownFlags evaluates to.
208197} ) ;
209198
210199var server = http . createServer ( function ( req , res ) {
211200 let path = url . parse ( req . url , true ) . query . path ;
212201
213- res . write ( fs . readFileSync ( path . replace ( new RegExp ( "[.]" , 'g' ) , '' ) ) ) ; // NOT OK ( can be absolute)
202+ res . write ( fs . readFileSync ( path . replace ( new RegExp ( "[.]" , 'g' ) , '' ) ) ) ; // $ Alert - can be absolute
214203
215204 if ( ! pathModule . isAbsolute ( path ) ) {
216- res . write ( fs . readFileSync ( path . replace ( new RegExp ( "[.]" , '' ) , '' ) ) ) ; // NOT OK
217- res . write ( fs . readFileSync ( path . replace ( new RegExp ( "[.]" , 'g' ) , '' ) ) ) ; // OK
218- res . write ( fs . readFileSync ( path . replace ( new RegExp ( "[.]" , unknownFlags ( ) ) , '' ) ) ) ; // OK
205+ res . write ( fs . readFileSync ( path . replace ( new RegExp ( "[.]" , '' ) , '' ) ) ) ; // $ Alert
206+ res . write ( fs . readFileSync ( path . replace ( new RegExp ( "[.]" , 'g' ) , '' ) ) ) ;
207+ res . write ( fs . readFileSync ( path . replace ( new RegExp ( "[.]" , unknownFlags ( ) ) , '' ) ) ) ;
219208 }
220209} ) ;
221210
0 commit comments