|
| 1 | +import { Ratelimit } from "@upstash/ratelimit"; |
| 2 | +import { Request as ExpressRequest, Response as ExpressResponse, NextFunction } from "express"; |
| 3 | +import Redis, { RedisOptions } from "ioredis"; |
| 4 | +import { createHash } from "node:crypto"; |
| 5 | +import { env } from "~/env.server"; |
| 6 | +import { logger } from "./logger.server"; |
| 7 | + |
| 8 | +function createRedisRateLimitClient( |
| 9 | + redisOptions: RedisOptions |
| 10 | +): ConstructorParameters<typeof Ratelimit>[0]["redis"] { |
| 11 | + const redis = new Redis(redisOptions); |
| 12 | + |
| 13 | + return { |
| 14 | + sadd: async <TData>(key: string, ...members: TData[]): Promise<number> => { |
| 15 | + return redis.sadd(key, members as (string | number | Buffer)[]); |
| 16 | + }, |
| 17 | + eval: <TArgs extends unknown[], TData = unknown>( |
| 18 | + ...args: [script: string, keys: string[], args: TArgs] |
| 19 | + ): Promise<TData> => { |
| 20 | + const script = args[0]; |
| 21 | + const keys = args[1]; |
| 22 | + const argsArray = args[2]; |
| 23 | + return redis.eval( |
| 24 | + script, |
| 25 | + keys.length, |
| 26 | + ...keys, |
| 27 | + ...(argsArray as (string | Buffer | number)[]) |
| 28 | + ) as Promise<TData>; |
| 29 | + }, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +type Options = { |
| 34 | + log?: { |
| 35 | + requests?: boolean; |
| 36 | + rejections?: boolean; |
| 37 | + }; |
| 38 | + redis: RedisOptions; |
| 39 | + keyPrefix: string; |
| 40 | + pathMatchers: (RegExp | string)[]; |
| 41 | + limiter: ConstructorParameters<typeof Ratelimit>[0]["limiter"]; |
| 42 | +}; |
| 43 | + |
| 44 | +//returns an Express middleware that rate limits using the Bearer token in the Authorization header |
| 45 | +export function authorizationRateLimitMiddleware({ |
| 46 | + redis, |
| 47 | + keyPrefix, |
| 48 | + limiter, |
| 49 | + pathMatchers, |
| 50 | + log = { |
| 51 | + rejections: true, |
| 52 | + requests: true, |
| 53 | + }, |
| 54 | +}: Options) { |
| 55 | + const rateLimiter = new Ratelimit({ |
| 56 | + redis: createRedisRateLimitClient(redis), |
| 57 | + limiter: limiter, |
| 58 | + ephemeralCache: new Map(), |
| 59 | + analytics: false, |
| 60 | + prefix: keyPrefix, |
| 61 | + }); |
| 62 | + |
| 63 | + return async (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => { |
| 64 | + if (log.requests) { |
| 65 | + logger.info(`RateLimiter (${keyPrefix}): request to ${req.path}`); |
| 66 | + } |
| 67 | + |
| 68 | + //first check if any of the pathMatchers match the request path |
| 69 | + const path = req.path; |
| 70 | + if ( |
| 71 | + !pathMatchers.some((matcher) => |
| 72 | + matcher instanceof RegExp ? matcher.test(path) : path === matcher |
| 73 | + ) |
| 74 | + ) { |
| 75 | + if (log.requests) { |
| 76 | + logger.info(`RateLimiter (${keyPrefix}): didn't match ${req.path}`); |
| 77 | + } |
| 78 | + return next(); |
| 79 | + } |
| 80 | + |
| 81 | + if (log.requests) { |
| 82 | + logger.info(`RateLimiter (${keyPrefix}): matched ${req.path}`); |
| 83 | + } |
| 84 | + |
| 85 | + const authorizationValue = req.headers.authorization; |
| 86 | + if (!authorizationValue) { |
| 87 | + if (log.requests) { |
| 88 | + logger.info(`RateLimiter (${keyPrefix}): no key`); |
| 89 | + } |
| 90 | + res.setHeader("Content-Type", "application/problem+json"); |
| 91 | + return res |
| 92 | + .status(401) |
| 93 | + .send( |
| 94 | + JSON.stringify( |
| 95 | + { |
| 96 | + title: "Unauthorized", |
| 97 | + status: 401, |
| 98 | + type: "https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401", |
| 99 | + detail: "No authorization header provided", |
| 100 | + }, |
| 101 | + null, |
| 102 | + 2 |
| 103 | + ) |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + const hash = createHash("sha256"); |
| 108 | + hash.update(authorizationValue); |
| 109 | + const hashedAuthorizationValue = hash.digest("hex"); |
| 110 | + |
| 111 | + const { success, pending, limit, reset, remaining } = await rateLimiter.limit( |
| 112 | + hashedAuthorizationValue |
| 113 | + ); |
| 114 | + |
| 115 | + res.set("x-ratelimit-limit", limit.toString()); |
| 116 | + res.set("x-ratelimit-remaining", remaining.toString()); |
| 117 | + res.set("x-ratelimit-reset", reset.toString()); |
| 118 | + |
| 119 | + if (success) { |
| 120 | + if (log.requests) { |
| 121 | + logger.info(`RateLimiter (${keyPrefix}): under rate limit`, { |
| 122 | + limit, |
| 123 | + reset, |
| 124 | + remaining, |
| 125 | + hashedAuthorizationValue, |
| 126 | + }); |
| 127 | + } |
| 128 | + return next(); |
| 129 | + } |
| 130 | + |
| 131 | + if (log.rejections) { |
| 132 | + logger.warn(`RateLimiter (${keyPrefix}): rate limit exceeded`, { |
| 133 | + limit, |
| 134 | + reset, |
| 135 | + remaining, |
| 136 | + pending, |
| 137 | + hashedAuthorizationValue, |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + res.setHeader("Content-Type", "application/problem+json"); |
| 142 | + return res.status(429).send( |
| 143 | + JSON.stringify( |
| 144 | + { |
| 145 | + title: "Rate Limit Exceeded", |
| 146 | + status: 429, |
| 147 | + type: "https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429", |
| 148 | + detail: `Rate limit exceeded ${remaining}/${limit} requests remaining. Retry after ${reset} seconds.`, |
| 149 | + reset: reset, |
| 150 | + limit: limit, |
| 151 | + }, |
| 152 | + null, |
| 153 | + 2 |
| 154 | + ) |
| 155 | + ); |
| 156 | + }; |
| 157 | +} |
| 158 | + |
| 159 | +type Duration = Parameters<typeof Ratelimit.slidingWindow>[1]; |
| 160 | + |
| 161 | +export const apiRateLimiter = authorizationRateLimitMiddleware({ |
| 162 | + keyPrefix: "ratelimit:api", |
| 163 | + redis: { |
| 164 | + port: env.REDIS_PORT, |
| 165 | + host: env.REDIS_HOST, |
| 166 | + username: env.REDIS_USERNAME, |
| 167 | + password: env.REDIS_PASSWORD, |
| 168 | + enableAutoPipelining: true, |
| 169 | + ...(env.REDIS_TLS_DISABLED === "true" ? {} : { tls: {} }), |
| 170 | + }, |
| 171 | + limiter: Ratelimit.slidingWindow(env.API_RATE_LIMIT_MAX, env.API_RATE_LIMIT_WINDOW as Duration), |
| 172 | + pathMatchers: [/^\/api/], |
| 173 | + log: { |
| 174 | + rejections: true, |
| 175 | + requests: false, |
| 176 | + }, |
| 177 | +}); |
| 178 | + |
| 179 | +export type RateLimitMiddleware = ReturnType<typeof authorizationRateLimitMiddleware>; |
0 commit comments