@@ -54,20 +54,22 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
5454
5555 try {
5656 const ip = getClientIp ( req )
57- if ( ip ) {
58- const storageKey = `public:contact:${ ip } `
59- const { allowed, remaining, resetAt } = await rateLimiter . checkRateLimitDirect (
60- storageKey ,
61- PUBLIC_ENDPOINT_RATE_LIMIT
62- )
63-
64- if ( ! allowed ) {
65- logger . warn ( `[${ requestId } ] Rate limit exceeded for IP ${ ip } ` , { remaining, resetAt } )
66- return NextResponse . json ( TOO_MANY_REQUESTS_RESPONSE , {
67- status : 429 ,
68- headers : { 'Retry-After' : String ( Math . ceil ( ( resetAt . getTime ( ) - Date . now ( ) ) / 1000 ) ) } ,
69- } )
70- }
57+ if ( ! ip ) {
58+ logger . warn ( `[${ requestId } ] Unable to resolve client IP for public rate limit` )
59+ return NextResponse . json ( TOO_MANY_REQUESTS_RESPONSE , { status : 429 } )
60+ }
61+ const storageKey = `public:contact:${ ip } `
62+ const { allowed, remaining, resetAt } = await rateLimiter . checkRateLimitDirect (
63+ storageKey ,
64+ PUBLIC_ENDPOINT_RATE_LIMIT
65+ )
66+
67+ if ( ! allowed ) {
68+ logger . warn ( `[${ requestId } ] Rate limit exceeded for IP ${ ip } ` , { remaining, resetAt } )
69+ return NextResponse . json ( TOO_MANY_REQUESTS_RESPONSE , {
70+ status : 429 ,
71+ headers : { 'Retry-After' : String ( Math . ceil ( ( resetAt . getTime ( ) - Date . now ( ) ) / 1000 ) ) } ,
72+ } )
7173 }
7274
7375 const parsed = await parseRequest ( submitContactContract , req , { } )
@@ -90,7 +92,7 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
9092 typeof captchaToken === 'string' && captchaToken . length > 0 ? captchaToken : null
9193
9294 if ( token ) {
93- const verification = await verifyTurnstileToken ( { token, remoteIp : ip ?? undefined } )
95+ const verification = await verifyTurnstileToken ( { token, remoteIp : ip } )
9496 if ( verification . success ) {
9597 captchaVerified = true
9698 } else if ( ! verification . transportError ) {
@@ -111,10 +113,6 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
111113 }
112114
113115 if ( ! captchaVerified ) {
114- if ( ! ip ) {
115- logger . warn ( `[${ requestId } ] Cannot enforce no-captcha rate limit without a client IP` )
116- return NextResponse . json ( TOO_MANY_REQUESTS_RESPONSE , { status : 429 } )
117- }
118116 const nocaptchaKey = `public:contact:nocaptcha:${ ip } `
119117 const { allowed : nocaptchaAllowed } = await rateLimiter . checkRateLimitDirect (
120118 nocaptchaKey ,
0 commit comments