fix(chat): add ip-based rate limiting to prevent session rotation abuse (#1436)
This commit is contained in:
parent
211cb96a96
commit
ad47aa0ee0
7 changed files with 87 additions and 5 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import { geolocation } from "@vercel/functions";
|
||||
import { checkBotId } from "botid/server";
|
||||
import { geolocation, ipAddress } from "@vercel/functions";
|
||||
import {
|
||||
convertToModelMessages,
|
||||
createUIMessageStream,
|
||||
|
|
@ -31,6 +32,7 @@ import {
|
|||
} from "@/lib/db/queries";
|
||||
import type { DBMessage } from "@/lib/db/schema";
|
||||
import { ChatbotError } from "@/lib/errors";
|
||||
import { checkIpRateLimit } from "@/lib/ratelimit";
|
||||
import type { ChatMessage } from "@/lib/types";
|
||||
import { convertToUIMessages, generateUUID } from "@/lib/utils";
|
||||
import { generateTitleFromUserMessage } from "../../actions";
|
||||
|
|
@ -62,12 +64,18 @@ export async function POST(request: Request) {
|
|||
const { id, message, messages, selectedChatModel, selectedVisibilityType } =
|
||||
requestBody;
|
||||
|
||||
const session = await auth();
|
||||
const [botResult, session] = await Promise.all([checkBotId(), auth()]);
|
||||
|
||||
if (botResult.isBot) {
|
||||
return new ChatbotError("unauthorized:chat").toResponse();
|
||||
}
|
||||
|
||||
if (!session?.user) {
|
||||
return new ChatbotError("unauthorized:chat").toResponse();
|
||||
}
|
||||
|
||||
await checkIpRateLimit(ipAddress(request));
|
||||
|
||||
const userType: UserType = session.user.type;
|
||||
|
||||
const messageCount = await getMessageCountByUserId({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue