|
23 | 23 | */
|
24 | 24 |
|
25 | 25 | import javascript
|
| 26 | +private import semmle.javascript.frameworks.ConnectExpressShared::ConnectExpressShared |
26 | 27 |
|
27 | 28 | // main concepts
|
28 | 29 | /**
|
@@ -155,3 +156,43 @@ class RouteHandlerLimitedByExpressLimiter extends RateLimitedRouteHandlerExpr {
|
155 | 156 | )
|
156 | 157 | }
|
157 | 158 | }
|
| 159 | + |
| 160 | +/** |
| 161 | + * A rate-handler function implemented using one of the rate-limiting classes provided |
| 162 | + * by the `rate-limiter-flexible` package. |
| 163 | + * |
| 164 | + * We look for route handlers that invoke the `consume` method of one of the `RateLimiter*` |
| 165 | + * classes from the `rate-limiter-flexible` package on a property of their request parameter, |
| 166 | + * like the `rateLimiterMiddleware` function in this example: |
| 167 | + * |
| 168 | + * ``` |
| 169 | + * import { RateLimiterRedis } from 'rate-limiter-flexible'; |
| 170 | + * const rateLimiter = new RateLimiterRedis(...); |
| 171 | + * function rateLimiterMiddleware(req, res, next) { |
| 172 | + * rateLimiter.consume(req.ip).then(next).catch(res.status(429).send('rate limited')); |
| 173 | + * } |
| 174 | + * ``` |
| 175 | + */ |
| 176 | +class RateLimiterFlexibleRateLimiter extends DataFlow::FunctionNode { |
| 177 | + RateLimiterFlexibleRateLimiter() { |
| 178 | + exists( |
| 179 | + string rateLimiterClassName, DataFlow::SourceNode rateLimiterClass, |
| 180 | + DataFlow::SourceNode rateLimiterInstance, DataFlow::ParameterNode request |
| 181 | + | |
| 182 | + rateLimiterClassName.matches("RateLimiter%") and |
| 183 | + rateLimiterClass = DataFlow::moduleMember("rate-limiter-flexible", rateLimiterClassName) and |
| 184 | + rateLimiterInstance = rateLimiterClass.getAnInstantiation() and |
| 185 | + request.getParameter() = getRouteHandlerParameter(getFunction(), "request") and |
| 186 | + request.getAPropertyRead() = rateLimiterInstance.getAMemberCall("consume").getAnArgument() |
| 187 | + ) |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +/** |
| 192 | + * A route-handler expression that is rate-limited by the `rate-limiter-flexible` package. |
| 193 | + */ |
| 194 | +class RouteHandlerLimitedByRateLimiterFlexible extends RateLimiter { |
| 195 | + RouteHandlerLimitedByRateLimiterFlexible() { |
| 196 | + any(RateLimiterFlexibleRateLimiter rl).flowsToExpr(this) |
| 197 | + } |
| 198 | +} |
0 commit comments