Skip to content

Bot protection on Cloudflare Workers

Run Centinel in a Cloudflare Worker on the routes you choose while Cloudflare DNS, cache rules and WAF rulesets keep handling delivery and network defense.

How it fits into Cloudflare

Execution model
A standalone Worker script (centinel-worker.js), or the @centinel/cloudflare-worker npm package wrapped around your existing Worker.
Timeout and failure mode
500 ms validator timeout by default (CENTINEL_TIMEOUT). Errors fail open, with backoff from 1 s up to 5 minutes.
Route scoping
protectedPathsInclusion and protectedQueryInclusion narrow what is checked. 40 static-asset extensions are skipped by default.
Verification signal
Protected responses carry Server-Timing: validator;dur=<ms> once the Worker gets a validator duration.

How a request moves through Cloudflare

The Worker sits on your route and decides before the request reaches your origin or your own Worker code.

  1. Route match

    A Cloudflare route such as example.com/* sends the request to the Worker. activateCentinel() tests url.pathname against the path patterns and the query string against protectedQueryInclusion. CORS preflight requests skip the validator.

  2. Validator call

    The Worker POSTs the URL, method, IP, headers and _centinel cookie to the validator with CENTINEL_SECRET_KEY and waits up to 500 ms.

  3. Decision

    Allow forwards the request to your origin or wrapped Worker and adds any validator cookies. Block returns an HTML page with the validator's status, 403 by default. Redirect returns the interstitial page.

  4. Failure

    A timeout, a validator error or a bad key forwards the request without validation. Repeated failures back off from 1 s up to 5 minutes.

Minimal configuration and a live check

Wrap an existing Worker

src/index.js
import { activateCentinel } from '@centinel/cloudflare-worker';

const myWorker = {
  async fetch(request, env, ctx) {
    // Your existing worker code
    return new Response('Hello from my app');
  },
};

export default activateCentinel(myWorker, {
  protectedPathsInclusion: /\/api(\/|$)/,
  protectedQueryInclusion: /[?&]cat=/,
  timeout: 500,
});

Store the key with npx wrangler secret put CENTINEL_SECRET_KEY, never in [vars]. Environment variables override the options passed here.

Check the validator timing header

Verify
curl -s -o /dev/null -D - https://example.com/ | grep -i server-timing

Expect Server-Timing: validator;dur=<ms>. A wrong key also adds this header, so open Logs, Begin log stream and confirm there is no 401 or CENTINEL_SECRET_KEY not set line.

Deployment options

  • Standalone Worker (dashboard or Wrangler 4)

    Paste centinel-worker.js into a new Worker, add CENTINEL_SECRET_KEY as a Secret, and attach a route such as example.com/* in Domains & Routes or wrangler.toml.

    Standalone Worker guide (opens in a new tab)
  • Existing Worker (npm package)

    Already running routing, auth or A/B logic in a Worker? Install @centinel/cloudflare-worker and wrap your fetch handler instead of chaining two Workers.

    Existing Worker npm guide (opens in a new tab)

What to know before deploying

  • Cloudflare routes cannot match query strings

    Attach the Worker to the parent path and pair a narrow protectedPathsInclusion with protectedQueryInclusion, for example /[?&]cat=/, to check costly search or faceted URLs only.

  • Keep activateCentinel() at module top level

    The handler holds the failure-backoff state. Calling activateCentinel() inside fetch resets it on every request and sends more traffic to a failing validator.

  • Use a Worker Secret, not a Secrets Store binding

    The Worker reads CENTINEL_SECRET_KEY as a plain string. A Secrets Store binding arrives as an object, fails silently and leaves traffic unvalidated.

  • Custom exclusions replace the static list

    Setting protectedPathsExclusion replaces the 40 default extensions. To skip one more path such as /health, copy DEFAULT_PATH_EXCLUSION from the script and append it.

  • Your Cloudflare DNS, SSL/TLS and cache rules
  • Your Cloudflare WAF custom rules, managed rulesets and DDoS protection
  • Routes the Worker is not attached to
  • Your origin servers and application code

Cloudflare integration questions

Walk through your Cloudflare setup

Bring one route bots are hitting. We will map the right connector, timeouts, and log-only rollout for your Cloudflare stack.