🔒️ Rate limit by ip address (#1155)

This commit is contained in:
Luke Vella 2024-06-17 22:13:40 +01:00 committed by GitHub
parent 491af5c71b
commit f7eda38e0a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 32 additions and 12 deletions

View file

@ -38,6 +38,7 @@
"@trpc/react-query": "^10.13.0",
"@upstash/ratelimit": "^1.2.1",
"@vercel/kv": "^2.0.0",
"request-ip": "^3.3.0",
"@vercel/functions": "^1.0.2",
"accept-language-parser": "^1.5.0",
"autoprefixer": "^10.4.13",
@ -83,6 +84,7 @@
"@rallly/eslint-config": "*",
"@types/accept-language-parser": "^1.5.3",
"@types/color-hash": "^1.0.2",
"@types/request-ip": "^0.0.41",
"@types/lodash": "^4.14.178",
"@types/react-big-calendar": "^1.8.8",
"@types/smoothscroll-polyfill": "^0.3.1",

View file

@ -4,6 +4,7 @@ import * as Sentry from "@sentry/nextjs";
import { createNextApiHandler } from "@trpc/server/adapters/next";
import { Ratelimit } from "@upstash/ratelimit";
import { kv } from "@vercel/kv";
import requestIp from "request-ip";
import { posthog, posthogApiHandler } from "@/app/posthog";
import { absoluteUrl, shortUrl } from "@/utils/absolute-url";
@ -46,11 +47,18 @@ const trpcApiHandler = createNextApiHandler<AppRouter>({
isEmailBlocked,
absoluteUrl,
shortUrl,
ratelimit: async (key: string) => {
ratelimit: async () => {
if (!process.env.KV_REST_API_URL) {
return { success: true };
}
return ratelimit.limit(key);
const clientIp = requestIp.getClientIp(opts.req);
if (!clientIp) {
return { success: false };
}
return ratelimit.limit(clientIp);
},
});