From 954eaddc8102de9d5775e0f3e0ca83005181785c Mon Sep 17 00:00:00 2001 From: becarta Date: Thu, 12 Jun 2025 23:51:58 +0200 Subject: [PATCH] Enhance spam detection logic in contact form API - Improved spam detection by integrating Gemini AI and heuristic checks, allowing for more accurate identification of spam messages. - Updated logging to specify the source of spam detection (Gemini or heuristic) for better debugging and monitoring. - Maintained existing functionality while enhancing the overall user experience in the contact form submission process. --- src/pages/api/contact.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/pages/api/contact.ts b/src/pages/api/contact.ts index 3dfabf7..12e7486 100644 --- a/src/pages/api/contact.ts +++ b/src/pages/api/contact.ts @@ -211,16 +211,20 @@ export const POST: APIRoute = async ({ request, clientAddress }) => { errors.disclaimer = 'Please check the required consent box before submitting'; } - // Check for spam - if (isSpam(message, name, email)) { - errors.spam = 'Your message was flagged as potential spam. Please revise your message and try again.'; - } - - // Gemini AI spam detection + // Gemini AI spam detection FIRST + let spamDetected = false; + let spamReason = ''; if (await isSpamWithGemini(message)) { + spamDetected = true; + spamReason = 'Gemini'; + } else if (isSpam(message, name, email)) { + spamDetected = true; + spamReason = 'heuristic'; + } + if (spamDetected) { const token = jwt.sign({ email, message }, MANUAL_REVIEW_SECRET, { expiresIn: '1h' }); console.warn( - `[SPAM DETECTED by Gemini]`, + `[SPAM DETECTED by ${spamReason === 'Gemini' ? 'Gemini' : 'heuristic'}]`, { name, email, message, ip: request.headers.get('x-forwarded-for') } ); return new Response(