Fix IP spoofing vulnerability in rate limiter by prioritizing request.ip (#132)

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
Ralksta
2026-04-21 22:51:35 +02:00
committed by GitHub
parent 16e1c7647b
commit 13fcfe8436
2 changed files with 50 additions and 1 deletions

View File

@@ -12,7 +12,11 @@
import { NextRequest } from 'next/server';
export function getClientIp(request: NextRequest): string {
// @ts-expect-error - Next.js 15 removed request.ip but hosting platforms like Vercel still populate it
const ip = request.ip as string | undefined;
return (
ip ??
request.headers.get('x-real-ip') ??
request.headers.get('x-forwarded-for')?.split(',')[0].trim() ??
'unknown'