From bfb789526addfe61326e8c9ee2326429a0c2af73 Mon Sep 17 00:00:00 2001 From: Benjamin Howe Date: Sat, 13 Jul 2024 18:17:16 +0100 Subject: [PATCH] Added honeypot field handling --- functions/api/contact.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/functions/api/contact.js b/functions/api/contact.js index 34d8d52..4f28185 100644 --- a/functions/api/contact.js +++ b/functions/api/contact.js @@ -1,4 +1,6 @@ export async function onRequest(context) { + const HONEYPOT_FIELD = "name"; + if (context.request.method !== "POST") { return new Response("Invalid request method", { status: 405 }); } @@ -7,6 +9,12 @@ export async function onRequest(context) { const fields = Object.fromEntries(formData.entries()); fields.cf = context.request.cf; fields.headers = Object.fromEntries(context.request.headers.entries()); + if (fields[HONEYPOT_FIELD] !== "") { + return Response.redirect(context.request.headers.get("Referer"), 303); + } else { + delete fields[HONEYPOT_FIELD]; + } + const url = new URL(context.request.url); const sent = await sendFormViaResend({