@@ -9,7 +9,7 @@ import { MatrixHostResolver } from "../utils/matrix-host-resolver";
99import IPCIDR from "ip-cidr" ;
1010import { isIP } from "net" ;
1111import { promises as dns } from "dns" ;
12- import ratelimiter , { RateLimitInfo , Options as RatelimitOptions , AugmentedRequest } from "express-rate-limit" ;
12+ import ratelimiter , { Options as RatelimitOptions } from "express-rate-limit" ;
1313import { Methods } from "./request" ;
1414import { Logger } from ".." ;
1515
@@ -109,7 +109,7 @@ export interface ProvisioningApiOpts {
109109 * Options for ratelimiting requests to the api server. Does not affect
110110 * static content loading.
111111 */
112- ratelimit ?: boolean | RatelimitOptions ;
112+ ratelimit ?: boolean | Partial < RatelimitOptions > ;
113113}
114114
115115
@@ -145,14 +145,21 @@ export class ProvisioningApi {
145145 this . app . get ( '/health' , this . getHealth . bind ( this ) ) ;
146146
147147 const limiter = this . opts . ratelimit && ratelimiter ( {
148- handler : ( req , _res , next ) => {
149- const info = ( req as AugmentedRequest ) . ratelimit as RateLimitInfo ;
150- const retryAfterMs = info ?. resetTime ? info . resetTime . getTime ( ) - Date . now ( ) : null ;
151- next ( new ApiError ( "Too many requests" , ErrCode . Ratelimited , 429 , { retry_after_ms : retryAfterMs } ) ) ;
148+ handler : ( req , _res , next , options ) => {
149+ next ( new ApiError (
150+ "Too many requests" ,
151+ ErrCode . Ratelimited ,
152+ 429 ,
153+ {
154+ retry_after_ms : options . windowMs ,
155+ }
156+ ) ) ;
152157 } ,
153- windowMs : 6 * 60 * 1000 , // 5 minutes
154- max : 100 , // Limit each IP to 100 requests per `window` (here, per 15 minutes)
155- ...( typeof this . opts . ratelimit === "object" ? this . opts . ratelimit : undefined )
158+ windowMs : 1 * 60 * 1000 , // 1 minute
159+ max : 30 , // Limit per window
160+ standardHeaders : true ,
161+ legacyHeaders : false ,
162+ ...( typeof this . opts . ratelimit === "object" ? this . opts . ratelimit : { } )
156163 } ) ;
157164
158165 this . baseRoute = router ( ) ;
0 commit comments