mirror of
https://github.com/UiO-Gaming/name-100-uio-gamers.git
synced 2026-11-05 13:56:53 +01:00
Validate the guess request body
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e09b2f981f
commit
925d1a4593
2 files changed
+32
-3
No files matched your search
@@ -3,15 +3,43 @@ import { findDiscordMember } from "@/lib/members";
|
||||
import { getServerSession } from "next-auth/next";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
const MAX_INPUT_LENGTH = 100;
|
||||
const MAX_GUESSED_IDS = 10_000;
|
||||
|
||||
function parseGuess(body: unknown): { input: string; guessedIds: string[] } | null {
|
||||
if (typeof body !== "object" || body === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const { input, guessedIds } = body as Record<string, unknown>;
|
||||
|
||||
if (typeof input !== "string" || !input.trim() || input.length > MAX_INPUT_LENGTH) {
|
||||
return null;
|
||||
}
|
||||
if (
|
||||
!Array.isArray(guessedIds) ||
|
||||
guessedIds.length > MAX_GUESSED_IDS ||
|
||||
!guessedIds.every((id) => typeof id === "string")
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return { input, guessedIds };
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const session = await getServerSession(authOptions);
|
||||
if (!session) {
|
||||
return new Response("Unauthorized", { status: 401 });
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const guess = parseGuess(await request.json().catch(() => null));
|
||||
if (!guess) {
|
||||
return NextResponse.json({ error: "Invalid request body" }, { status: 400 });
|
||||
}
|
||||
const { input, guessedIds } = await request.json();
|
||||
|
||||
try {
|
||||
return NextResponse.json(await findDiscordMember(input, guessedIds));
|
||||
return NextResponse.json(await findDiscordMember(guess.input, guess.guessedIds));
|
||||
} catch (error) {
|
||||
console.error("Error finding Discord member:", error);
|
||||
return NextResponse.json({ error: "Failed to look up member" }, { status: 503 });
|
||||
|
||||
@@ -35,6 +35,7 @@ const GuessForm: React.FC<GuessFormProps> = ({ input, onInputChange, onSubmit, d
|
||||
type="text"
|
||||
value={input}
|
||||
onChange={onInputChange}
|
||||
maxLength={100}
|
||||
className={`focus:border-accent-primary h-12 w-60 rounded border px-2 py-1 focus:outline-none md:w-75 ${
|
||||
disabled || loading
|
||||
? "cursor-not-allowed border-gray-500 bg-gray-400 opacity-50"
|
||||
|
||||
Reference in new issue
Block a user