Skip to content

Commit 59ebb7a

Browse files
Ferryx349Anshumancanrockcameri
authored
fix(codeql): recognize custom admin rate limit middleware (#692)
Signed-off-by: ABHAY PANDEY <pandeyabhay967@gmail.com> Co-authored-by: Anshuman <109489361+Anshumancanrock@users.noreply.github.com> Co-authored-by: Ricardo Cabral <me@ricardocabral.io>
1 parent fd7f56a commit 59ebb7a

5 files changed

Lines changed: 69 additions & 11 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"nostream": patch
3+
---
4+
5+
fix: teach CodeQL to recognize custom admin rate limit middleware

‎.github/codeql/codeql-config.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: nostream-codeql
2+
3+
packs:
4+
javascript-typescript:
5+
- cameri/nostream-javascript-models
6+
7+
queries:
8+
- uses: security-and-quality
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: cameri/nostream-javascript-models
2+
version: 0.0.1
3+
library: true
4+
dependencies:
5+
codeql/javascript-all: "*"

‎.github/workflows/codeql.yml‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,7 @@ jobs:
8585
with:
8686
languages: ${{ matrix.language }}
8787
build-mode: ${{ matrix.build-mode }}
88-
# If you wish to specify custom queries, you can do so here or in
89-
# a config file. By default, queries listed here will override any
90-
# specified in a config file.
91-
# Prefix the list here with "+" to use these queries and those in
92-
# the config file.
93-
94-
# For more details on CodeQL's query packs, refer to:
95-
# https://docs.github.com/en/code-security/code-scanning/
96-
# automatically-scanning-your-code-for-vulnerabilities-and-errors/
97-
# configuring-code-scanning#using-queries-in-ql-packs
98-
# queries: security-extended,security-and-quality
88+
config-file: ./.github/codeql/codeql-config.yml
9989

10090
# If the analyze step fails for one of the languages you are analyzing
10191
# with "We were unable to automatically build your code", modify the

0 commit comments

Comments
 (0)