The Problem: Automated Abuse on Public Serverless Webhooks
![]() |
| Image generated with Gemini AI |
Deploying a serverless proxy on Cloudflare Workers successfully protects upstream API keys when integrating static blogs with email service providers like Brevo. However, exposing an unauthenticated HTTPS endpoint creates a critical attack vector: malicious automated bots and scraping scripts can flood the webhook, depleting transactional credits and polluting subscriber databases with junk records.
The Solution: Edge Defense via Honeypot and Payload Sanitization
Rather than degrading user experience with heavy third-party CAPTCHA widgets, we implement a lightweight, dual-stage security perimeter directly at the edge:
- Client-Side Honeypot Field: A hidden form field invisible to human users via CSS, but readily filled by automated crawlers traversing the DOM. If populated, the worker immediately drops the request.
- Edge-Level Structural Validation: The Cloudflare Worker validates origin headers, checks RFC email compliance, and filters malformed payloads before dispatching upstream requests to Brevo.
Hardened Cloudflare Worker Implementation
export default {
async fetch(request, env) {
// Handle CORS Preflight
if (request.method === "OPTIONS") {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "https://itchronicle.com",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type"
}
});
}
if (request.method !== "POST") {
return new Response(JSON.stringify({ error: "Method not allowed" }), {
status: 405,
headers: { "Content-Type": "application/json" }
});
}
try {
const payload = await request.json();
const { email, lang, redirectUrl, hp_field } = payload;
// 1. Anti-Bot Honeypot Verification
if (hp_field && hp_field.trim() !== "") {
return new Response(JSON.stringify({ error: "Spam detected" }), {
status: 400,
headers: {
"Access-Control-Allow-Origin": "https://itchronicle.com",
"Content-Type": "application/json"
}
});
}
// 2. Strict Email Validation
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!email || !emailRegex.test(email)) {
return new Response(JSON.stringify({ error: "Invalid email format" }), {
status: 400,
headers: {
"Access-Control-Allow-Origin": "https://itchronicle.com",
"Content-Type": "application/json"
}
});
}
const templateId = lang === "en" ? 12 : 11;
const listId = lang === "en" ? 5 : 4;
const brevoPayload = {
email: email,
includeListIds: [listId],
templateId: templateId,
redirectionUrl: redirectUrl
};
const response = await fetch("https://api.brevo.com/v3/contacts/doubleOptinConfirmation", {
method: "POST",
headers: {
"api-key": env.BREVO_API_KEY,
"Content-Type": "application/json",
"Accept": "application/json"
},
body: JSON.stringify(brevoPayload)
});
return new Response(JSON.stringify({ success: response.ok }), {
status: response.status,
headers: {
"Access-Control-Allow-Origin": "https://itchronicle.com",
"Content-Type": "application/json"
}
});
} catch (err) {
return new Response(JSON.stringify({ error: err.message }), {
status: 500,
headers: {
"Access-Control-Allow-Origin": "https://itchronicle.com",
"Content-Type": "application/json"
}
});
}
}
};
Index: The Genesis of a Newsletter
- Ep. 1: Serverless Architecture with Cloudflare Workers and Brevo
- Ep. 2: Git Subtree and Delivery Decoupling
- Ep. 3: Cloudflare Workers, Anti-Spam Protection & Rate Limiting at the Edge [You are here]
- Ep. 4: Advanced Brevo API Error Handling on Cloudflare Workers
Your Reward: Cloudflare Worker Security Boilerplate
Download our battle-tested Cloudflare Worker boilerplate equipped with honeypot filters, input sanitization, and enterprise CORS controls. Enter your email to receive it.
Zero spam. Pure sysadmin architecture, operational code, and engineering field logs.

Commenti
Posta un commento