|
| 1 | +/** |
| 2 | + * Models Nostream's custom Redis-backed rate limit middleware for CodeQL's |
| 3 | + * js/missing-rate-limiting query. |
| 4 | + * |
| 5 | + * CodeQL only recognizes popular npm rate limiters by default. Nostream uses |
| 6 | + * in-repo middleware that calls isAdminRateLimited() / isRateLimited(). |
| 7 | + */ |
| 8 | + |
| 9 | +import javascript |
| 10 | +import semmle.javascript.security.dataflow.MissingRateLimiting |
| 11 | + |
| 12 | +private predicate isNostreamRateLimitCheck(CallExpr call, string calleeName) { |
| 13 | + call.getCalleeName() = calleeName |
| 14 | +} |
| 15 | + |
| 16 | +private predicate isInFile(Function fn, string fileSuffix) { |
| 17 | + fn.getFile().getRelativePath().regexpMatch(".*/" + fileSuffix + "$") |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * An admin route middleware function that invokes isAdminRateLimited(). |
| 22 | + */ |
| 23 | +class NostreamAdminRateLimiterFunction extends DataFlow::FunctionNode { |
| 24 | + NostreamAdminRateLimiterFunction() { |
| 25 | + exists(CallExpr call | |
| 26 | + isNostreamRateLimitCheck(call, "isAdminRateLimited") and |
| 27 | + call.getEnclosingFunction() = this.getFunction() and |
| 28 | + isInFile(this.getFunction(), "admin-rate-limit-middleware.ts") |
| 29 | + ) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * A connection-level middleware function that invokes isRateLimited(). |
| 35 | + */ |
| 36 | +class NostreamConnectionRateLimiterFunction extends DataFlow::FunctionNode { |
| 37 | + NostreamConnectionRateLimiterFunction() { |
| 38 | + exists(CallExpr call | |
| 39 | + isNostreamRateLimitCheck(call, "isRateLimited") and |
| 40 | + call.getEnclosingFunction() = this.getFunction() and |
| 41 | + isInFile(this.getFunction(), "rate-limiter-middleware.ts") |
| 42 | + ) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +class NostreamAdminRateLimitMiddleware extends RateLimitingMiddleware instanceof NostreamAdminRateLimiterFunction { |
| 47 | +} |
| 48 | + |
| 49 | +class NostreamConnectionRateLimitMiddleware extends RateLimitingMiddleware instanceof NostreamConnectionRateLimiterFunction { |
| 50 | +} |
0 commit comments