Skip to content

Commit 3649cb9

Browse files
authored
Use Fly-Client-Ip as rate limit key (#782)
* Use fly-client-ip as rate limit key when applicable * fix types
1 parent 74a42d8 commit 3649cb9

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

server/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,14 @@ const rateLimitDefault = {
146146
max: 1000 * maxMultiple,
147147
standardHeaders: true,
148148
legacyHeaders: false,
149-
// Fly.io prevents spoofing of X-Forwarded-For
150-
// so no need to validate the trustProxy config
151149
validate: { trustProxy: false },
150+
// Malicious users can spoof their IP address which means we should not deault
151+
// to trusting req.ip when hosted on Fly.io. However, users cannot spoof Fly-Client-Ip.
152+
// When sitting behind a CDN such as cloudflare, replace fly-client-ip with the CDN
153+
// specific header such as cf-connecting-ip
154+
keyGenerator: (req: express.Request) => {
155+
return req.get('fly-client-ip') ?? `${req.ip}`
156+
},
152157
}
153158

154159
const strongestRateLimit = rateLimit({

0 commit comments

Comments
 (0)