@@ -50,9 +50,16 @@ module Fastify {
5050 t .start ( ) and
5151 result = server ( creation ) .getAMethodCall ( "register" ) .getArgument ( 0 ) .getALocalSource ( )
5252 or
53+ // Track through require('fastify-plugin')
54+ result = pluginCallback ( creation , t ) .( FastifyPluginCall ) .getArgument ( 0 ) .getALocalSource ( )
55+ or
5356 exists ( DataFlow:: TypeBackTracker t2 | result = pluginCallback ( creation , t2 ) .backtrack ( t2 , t ) )
5457 }
5558
59+ private class FastifyPluginCall extends DataFlow:: CallNode {
60+ FastifyPluginCall ( ) { this = DataFlow:: moduleImport ( "fastify-plugin" ) .getACall ( ) }
61+ }
62+
5663 /** Gets a data flow node being used as a Fastify plugin. */
5764 private DataFlow:: SourceNode pluginCallback ( DataFlow:: SourceNode creation ) {
5865 result = pluginCallback ( creation , DataFlow:: TypeBackTracker:: end ( ) )
@@ -198,18 +205,40 @@ module Fastify {
198205 }
199206
200207 private class PluginRegistration extends Routing:: RouteSetup:: MethodCall {
201- ServerDefinition server ;
208+ PluginRegistration ( ) { this = server ( ) . getAMethodCall ( "register" ) }
202209
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 ) )
206218 }
207219
220+ /** Gets a functino flowing into the first argument. */
221+ DataFlow:: FunctionNode pluginBody ( ) { result = pluginBody ( DataFlow:: TypeBackTracker:: end ( ) ) }
222+
208223 override HTTP:: RequestMethodName getHttpMethod ( ) {
209224 result = getOptionArgument ( 1 , "method" ) .getStringValue ( ) .toUpperCase ( )
210225 }
211226
212227 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+ }
213242 }
214243
215244 /**
@@ -403,14 +432,4 @@ module Fastify {
403432 )
404433 }
405434 }
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- }
416435}
0 commit comments