@@ -50,9 +50,16 @@ module Fastify {
50
50
t .start ( ) and
51
51
result = server ( creation ) .getAMethodCall ( "register" ) .getArgument ( 0 ) .getALocalSource ( )
52
52
or
53
+ // Track through require('fastify-plugin')
54
+ result = pluginCallback ( creation , t ) .( FastifyPluginCall ) .getArgument ( 0 ) .getALocalSource ( )
55
+ or
53
56
exists ( DataFlow:: TypeBackTracker t2 | result = pluginCallback ( creation , t2 ) .backtrack ( t2 , t ) )
54
57
}
55
58
59
+ private class FastifyPluginCall extends DataFlow:: CallNode {
60
+ FastifyPluginCall ( ) { this = DataFlow:: moduleImport ( "fastify-plugin" ) .getACall ( ) }
61
+ }
62
+
56
63
/** Gets a data flow node being used as a Fastify plugin. */
57
64
private DataFlow:: SourceNode pluginCallback ( DataFlow:: SourceNode creation ) {
58
65
result = pluginCallback ( creation , DataFlow:: TypeBackTracker:: end ( ) )
@@ -198,18 +205,40 @@ module Fastify {
198
205
}
199
206
200
207
private class PluginRegistration extends Routing:: RouteSetup:: MethodCall {
201
- ServerDefinition server ;
208
+ PluginRegistration ( ) { this = server ( ) . getAMethodCall ( "register" ) }
202
209
203
- PluginRegistration ( ) {
204
- server .flowsTo ( this .getReceiver ( ) .asExpr ( ) ) and
205
- getMethodName ( ) = "register"
210
+ private DataFlow:: SourceNode pluginBody ( DataFlow:: TypeBackTracker t ) {
211
+ t .start ( ) and
212
+ result = getArgument ( 0 ) .getALocalSource ( )
213
+ or
214
+ // step through calls to require('fastify-plugin')
215
+ result = pluginBody ( t ) .( FastifyPluginCall ) .getArgument ( 0 ) .getALocalSource ( )
216
+ or
217
+ exists ( DataFlow:: TypeBackTracker t2 | result = pluginBody ( t2 ) .backtrack ( t2 , t ) )
206
218
}
207
219
220
+ /** Gets a functino flowing into the first argument. */
221
+ DataFlow:: FunctionNode pluginBody ( ) { result = pluginBody ( DataFlow:: TypeBackTracker:: end ( ) ) }
222
+
208
223
override HTTP:: RequestMethodName getHttpMethod ( ) {
209
224
result = getOptionArgument ( 1 , "method" ) .getStringValue ( ) .toUpperCase ( )
210
225
}
211
226
212
227
override string getRelativePath ( ) { result = getOptionArgument ( 1 , "prefix" ) .getStringValue ( ) }
228
+
229
+ override DataFlow:: Node getChildNode ( int n ) {
230
+ n = 0 and
231
+ (
232
+ // If we can see the plugin body, use its server parameter as the child to ensure
233
+ // plugins or routes installed in the plugin are ordered
234
+ result = pluginBody ( ) .getParameter ( 0 )
235
+ or
236
+ // If we can't see the plugin body, just use the plugin expression so we can
237
+ // check if something is guarded by that plugin.
238
+ not exists ( pluginBody ( ) ) and
239
+ result = getArgument ( 0 )
240
+ )
241
+ }
213
242
}
214
243
215
244
/**
@@ -403,14 +432,4 @@ module Fastify {
403
432
)
404
433
}
405
434
}
406
-
407
- private class RouteHandlerTracking extends Routing:: RouteHandlerTrackingStep {
408
- override predicate step ( DataFlow:: Node pred , DataFlow:: Node succ ) {
409
- exists ( DataFlow:: CallNode call |
410
- call = DataFlow:: moduleImport ( "fastify-plugin" ) and
411
- pred = call .getArgument ( 0 ) and
412
- succ = call
413
- )
414
- }
415
- }
416
435
}
0 commit comments