Skip to content

Commit 744f0b1

Browse files
committed
JS: Use type info to recognize routers
1 parent c06fd45 commit 744f0b1

File tree

1 file changed

+14
-9
lines changed
  • javascript/ql/src/semmle/javascript/frameworks

1 file changed

+14
-9
lines changed

javascript/ql/src/semmle/javascript/frameworks/Express.qll

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,23 @@ module Express {
3939
router.flowsTo(e)
4040
}
4141

42+
/**
43+
* Holds if `e` may refer to a router object.
44+
*/
45+
private predicate isRouter(Expr e) {
46+
isRouter(e, _)
47+
or
48+
e.getType().hasUnderlyingType("express-serve-static-core", "Router")
49+
}
50+
4251
/**
4352
* An expression that refers to a route.
4453
*/
4554
class RouteExpr extends MethodCallExpr {
46-
RouterDefinition router;
47-
48-
RouteExpr() { isRouter(this, router) }
55+
RouteExpr() { isRouter(this) }
4956

50-
/** Gets the router from which this route was created. */
51-
RouterDefinition getRouter() { result = router }
57+
/** Gets the router from which this route was created, if it is known. */
58+
RouterDefinition getRouter() { isRouter(this, result) }
5259
}
5360

5461
/**
@@ -68,18 +75,16 @@ module Express {
6875
* A call to an Express router method that sets up a route.
6976
*/
7077
class RouteSetup extends HTTP::Servers::StandardRouteSetup, MethodCallExpr {
71-
RouterDefinition router;
72-
7378
RouteSetup() {
74-
isRouter(getReceiver(), router) and
79+
isRouter(getReceiver()) and
7580
getMethodName() = routeSetupMethodName()
7681
}
7782

7883
/** Gets the path associated with the route. */
7984
string getPath() { getArgument(0).mayHaveStringValue(result) }
8085

8186
/** Gets the router on which handlers are being registered. */
82-
RouterDefinition getRouter() { result = router }
87+
RouterDefinition getRouter() { isRouter(getReceiver(), result) }
8388

8489
/** Holds if this is a call `use`, such as `app.use(handler)`. */
8590
predicate isUseCall() { getMethodName() = "use" }

0 commit comments

Comments
 (0)