File tree Expand file tree Collapse file tree
docs/skills/epic-security Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -196,7 +196,7 @@ Epic Stack uses `express-rate-limit` para prevenir abuso.
196196
197197``` typescript
198198// server/index.ts
199- import rateLimit from ' express-rate-limit'
199+ import rateLimit , { ipKeyGenerator } from ' express-rate-limit'
200200
201201const rateLimitDefault = {
202202 windowMs: 60 * 1000 , // 1 minute
@@ -205,7 +205,8 @@ const rateLimitDefault = {
205205 legacyHeaders: false ,
206206 validate: { trustProxy: false },
207207 keyGenerator : (req : express .Request ) => {
208- return req .get (' fly-client-ip' ) ?? ` ${req .ip } `
208+ const clientIp = req .get (' fly-client-ip' ) ?? req .ip
209+ return ipKeyGenerator (clientIp )
209210 },
210211}
211212
@@ -508,13 +509,18 @@ export default function SignupRoute({ actionData }: Route.ComponentProps) {
508509
509510``` typescript
510511// server/index.ts
512+ import { ipKeyGenerator } from ' express-rate-limit'
511513const apiRateLimit = rateLimit ({
512514 ... rateLimitDefault ,
513515 windowMs: 60 * 1000 ,
514516 limit: 100 , // 100 requests per minute for API
515517 keyGenerator : (req ) => {
516518 const apiKey = req .get (' X-API-Key' )
517- return apiKey ?? req .get (' fly-client-ip' ) ?? req .ip
519+ if (apiKey ) {
520+ return apiKey
521+ }
522+ const clientIp = req .get (' fly-client-ip' ) ?? req .ip
523+ return ipKeyGenerator (clientIp )
518524 },
519525})
520526
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import chalk from 'chalk'
44import closeWithGrace from 'close-with-grace'
55import compression from 'compression'
66import express from 'express'
7- import rateLimit from 'express-rate-limit'
7+ import rateLimit , { ipKeyGenerator } from 'express-rate-limit'
88import getPort , { portNumbers } from 'get-port'
99import helmet from 'helmet'
1010import morgan from 'morgan'
@@ -128,7 +128,12 @@ const rateLimitDefault = {
128128 // When sitting behind a CDN such as cloudflare, replace fly-client-ip with the CDN
129129 // specific header such as cf-connecting-ip
130130 keyGenerator : ( req : express . Request ) => {
131- return req . get ( 'fly-client-ip' ) ?? `${ req . ip } `
131+ const clientIp : string =
132+ req . get ( 'fly-client-ip' ) ??
133+ req . ip ??
134+ req . socket . remoteAddress ??
135+ '0.0.0.0'
136+ return ipKeyGenerator ( clientIp )
132137 } ,
133138}
134139
You can’t perform that action at this time.
0 commit comments