Integrations · Forms

Validate every form on your site with a Cloudflare Worker

Add email validation to every form on your site without touching the site itself. If your site runs through Cloudflare, you deploy one small Worker to your own Cloudflare account and put it in front of the site. From then on it does two things:

  • In the browser. On every page that has an email field, it adds the form guard. The guard offers a one-click fix for typos and stops the form when the address cannot receive mail or is a throwaway inbox.
  • At the edge. Every form post to the paths it covers is checked before it reaches your server, so bots that never run the page's JavaScript are caught too. Log-in posts are left alone (which). The Worker blocks the form with a clear message, or passes it on with the result added.

You choose the settings on a small settings page that the Worker serves on your own site. Download it: email-validation-cloudflare.zip holds worker.mjs (the Worker), wrangler.jsonc (its configuration) and LICENSE (the MIT licence).

What you need

  • A site whose domain is on Cloudflare, with its traffic proxied (the orange cloud), and a Cloudflare account on any plan. The Worker runs on every request its route covers, which counts toward your Workers plan's request limits.
  • Two API keys from the API keys page:
    • a server key for the check at the edge. Give it only Validate addresses and no allowed origins. Add a daily credit cap a little above your busiest day. It is stored as a Worker secret and never reaches a browser. A browser key does not work here: the Worker refuses it.
    • a browser key for the form guard, as its setup describes: only Validate addresses, your site as an allowed origin and a daily credit cap. It sits in your pages, so it is public by design (why that is safe). The Worker puts no other key in a page.
  • A settings token: a random string of at least 32 characters, for example from openssl rand -hex 32 or your password manager's generator.

Deploy with Wrangler

From the unzipped folder, with Node.js installed:

cd email-validation-cloudflare
npx wrangler login
npx wrangler secret put EV_SERVER_KEY       # paste the server key
npx wrangler secret put EV_SETTINGS_TOKEN   # paste the settings token
npx wrangler deploy

Before npx wrangler deploy, open wrangler.jsonc and:

  1. put your domain in routes and remove the // in front of that line (see routes);
  2. change EV_SETTINGS_PATH to a path of your own that is hard to guess, such as /_ev-settings-k3v9q2;
  3. for the settings page to save, create a KV namespace with npx wrangler kv namespace create EV_SETTINGS and put its id in the line that binds EV_SETTINGS, without the //;
  4. put the browser key in EV_BROWSER_KEY under vars (or enter it on the settings page once the Worker runs). Without it the form guard in the browser only offers typo fixes and stops no form.

Wrangler stores the keys as encrypted secrets: nobody can read them back in the dashboard or with Wrangler.

Deploy from the Cloudflare dashboard

No terminal? In the Cloudflare dashboard:

  1. Open Workers & Pages → Create, choose Create Worker, name it ev-form-validation and select Deploy.
  2. Select Edit code, replace the starter code with the whole of worker.mjs from the unzipped folder (open it in any text editor and copy all of it) and select Deploy.
  3. In the Worker's Settings → Variables and Secrets, add EV_SERVER_KEY and EV_SETTINGS_TOKEN as type Secret, and EV_SETTINGS_PATH and EV_BROWSER_KEY as type Text.
  4. Optional, for the settings page to save: under Storage & Databases → KV, create a namespace, then bind it to the Worker under Settings → Bindings with the variable name EV_SETTINGS.
  5. Add the route below under Settings → Domains & Routes, and on the same page turn off the workers.dev address and Preview URLs, so the Worker answers only on your own site. (The wrangler.jsonc in the download turns both off for you.) The settings page never opens on a workers.dev address either.

Cloudflare's Deploy to Cloudflare button deploys only from a public GitHub or GitLab repository. This Worker comes as a download, so deploy it with Wrangler or from the dashboard as above.

Put it in front of your site

A route runs the Worker for the addresses it matches. To cover the whole site:

"routes": [
  { "pattern": "example.com/*", "zone_name": "example.com" },
  { "pattern": "www.example.com/*", "zone_name": "example.com" }
]

Every request then goes through the Worker. Requests it has nothing to do with (images, scripts, other methods) pass straight to your server. To cover only part of the site, use narrower routes, or keep the routes wide and set EV_PATHS (below).

Choose the settings

Open https://example.com/<your EV_SETTINGS_PATH> and enter the settings token. The page shows whether the server key is set and every setting below, and saves them in the KV namespace. Saved settings win over the variables, and reach every Cloudflare location within about a minute (how KV works). Back to the variables' settings deletes what was saved. Without a KV namespace the page shows the settings read-only, and you change them as variables instead.

The page asks for the token every time, compares it in constant time, sets no cookie and runs no script. It opens only over https: asked over http, it sends the browser to the https address, and it refuses a post over http, so the token never travels unencrypted. It stays off while the token is shorter than 32 characters or the path is still the placeholder from wrangler.jsonc. Requests to it never reach your server.

Put the settings page behind Cloudflare Access

For a second lock, let only your team reach the page. With Cloudflare Access, in Zero Trust → Access controls → Applications, create a Self-hosted application with one public hostname for each hostname your routes cover (for example example.com and www.example.com), each with the path of EV_SETTINGS_PATH, and add an Allow policy for your team's email addresses. Cloudflare then asks for a sign-in before the request reaches the Worker, which still asks for the token.

Settings

Every setting is a variable of the Worker (in wrangler.jsonc under vars, or in the dashboard), and a field on the settings page. A value that does not pass its check is left out and the default stays; the settings page lists it.

VariableDefaultWhat it does
EV_MODEblockblock: a form whose address has a blocked status is refused. tag: every form is passed on with the result. off: no check at the edge
EV_BLOCKinvalid,disposableThe statuses to block, any of invalid, disposable, catch_all, unknown. valid always goes through. The browser never stops unknown
EV_BLOCK_HTTP_STATUS422The HTTP status of a refused form: 422, 400 or 403
EV_MSG_INVALID, EV_MSG_DISPOSABLE, EV_MSG_CATCH_ALL, EV_MSG_UNKNOWNthe form guard's wordingThe message for each blocked status, up to 300 characters. The first three are shown in the browser too
EV_PATHS/*The paths covered, comma-separated; * matches anything
EV_SKIP_PATHS/cdn-cgi/*, /wp-admin/*, and every path holding login, log-in, log_in, signin, sign-in, sign_in, logout, log-out, log_out, signout, sign-out, sign_out or passwordPaths never touched, checked first. Only the path counts, not the query string: WordPress's own registration (/wp-login.php?action=register) shares its path with the log-in, so it is skipped too; add a check in your own code if you use it
EV_FIELDS(empty)The names of the email fields to check at the edge. Empty: every field whose name says email (email, e-mail, mail) and holds an @, and every value shaped like an address, of any length (a quoted local part such as "jo"@example.com and a domain literal such as jo@[192.0.2.1] included; a domain without a dot, such as jo@localhost, is not taken, since passwords such as P@ssw0rd have that shape; a Unicode full stop, 。, . or 。, counts as a dot, as UTS-46 maps it to one). A form with two different addresses among them, or a list of addresses in one field, is passed on unchecked (several_addresses); if your form has a second address field, name the one to check here. Names are compared the way PHP and ASP.NET read them (case ignored, leading spaces dropped, dots and spaces read as underscores, anything from a closed bracket on ignored). A field also counts when its last name part (the part after its last dot or bracket, so model.Email gives Email and user[email]x gives x) is the name, as ASP.NET Core MVC binds model.Email over Email; and when any one of its name parts (split at dots and brackets) is the last part of the name you set, since PHP reads user[email]x as user[email] and ASP.NET Core reads Input[Email] as Input.Email. Each name is also compared as ASP.NET Core MVC's jQuery reading turns it (Em[]ail as Email, Input[Em]ail as Input.Email) and without a leading ! or _ (Spring binds !email to email). So a second field your server could read under the same name, in the form, the query string or a cookie, also makes the form several_addresses; with Input.Email set, another field ending in Email (Shipping.Email) does too. Set a plain name, not a bracketed one: with customer[email] (Shopify) or user[email] (Rails), every field under that prefix counts, so real forms go through unchecked as several_addresses; set email for those
EV_TAG_FIELDSoffIn tag mode, also put the result in the form as the fields ev_status and ev_reason (which to trust)
EV_JSONoffAlso check JSON posts (application/json), such as forms your scripts send. The email field is found as in a form, also inside nested objects; the result goes in the headers only. A post with more than 1000 text values or nested more than 5 levels deep, or that declares a character set other than UTF-8, goes through unchecked (bad_body)
EV_TIMEOUT_MS1500The most time a check may add to a form post, 200 to 5000 milliseconds. Past it the form goes through unchecked
EV_MAX_BODY_KB1024The largest form read, 16 to 2048 KiB (the Worker holds the form in memory while it checks). A larger form (such as one with a large upload) goes through unchecked
EV_CLIENTonAdd the form guard to pages
EV_INJECTformsforms: pages whose HTML has an email field. all: every HTML page, for forms that scripts draw later
EV_BROWSER_KEY(empty)The browser key. Without it the guard only offers typo fixes. Only a browser key is accepted here, never a server key
EV_SKIP_FORMSforms whose action holds login, signin, sign-in or password, and forms whose id or class holds loginCSS selectors, comma-separated, of forms the browser check leaves alone. In the browser only: at the edge a post is skipped by its path (EV_SKIP_PATHS) or as a log-in, never by these selectors
EV_PINNEDoffLoad the frozen guard version with Subresource Integrity
EV_REGIONuseu: checks go to https://api-eu.email-validation.org, from the edge and from the browser (EU data residency); the same keys work

Secrets, never variables: EV_SERVER_KEY and EV_SETTINGS_TOKEN. Not a setting: EV_SETTINGS_PATH, the settings page's path.

What happens to each request

Pages. When your server answers a page with HTML, the Worker reads it as it streams to the visitor with Cloudflare's HTMLRewriter:

  • every <input type="email"> is found by the form guard itself; a text field whose name, id or autocomplete says email gets data-ev-guard, so the guard checks it too;
  • a form that matches EV_SKIP_FORMS gets data-ev-guard="off", which the guard leaves alone;
  • the form guard's tag goes in before </body>, unless the page has one already.

Nothing else on the page changes, and cookies and headers pass through untouched.

Form posts. A POST to a covered path whose body is a form (application/x-www-form-urlencoded or multipart/form-data) is checked with the server key before your server sees it. With EV_JSON on, so is a JSON post (application/json), such as a form your scripts send. The address is looked for in the URL's query string too, since some frameworks (Rails params, Go's FormValue, ASP.NET Core MVC) read a value there over the form's: a second address in the query also makes the form several_addresses, and the same address there is one check. A %u0040 escape, which only classic ASP.NET decodes (as @), is read both ways. A cookie that holds an address counts the same way when its name matches EV_FIELDS or, with EV_FIELDS empty, says email or is the name of a field that gave an address: PHP's $_REQUEST (with no php.ini) and classic ASP.NET's Request[...] read cookies under a field's name. A cookie is never the address checked, only a second value: one that remembers the visitor's own address is one check, but one holding another address makes the form several_addresses, which goes through unchecked. That includes the address WordPress (comment_author_email_…) and Drupal (Drupal.visitor.mail) remember for a commenter: a visitor who commented with one address and signs up with another goes through unchecked. With EV_FIELDS set to your field's name, the WordPress cookie no longer counts; Drupal's still does under EV_FIELDS=mail (Drupal's own field name), since Drupal.visitor.mail ends in mail.

  • Blocked: the visitor gets a short page with the message and a link back to the form. A JSON post, or a script that asks for JSON, gets {"error": "email_rejected", "status": "invalid", "message": "…"}. Your server never sees the form.
  • Passed on: your server receives the form exactly as it was sent, with these headers added:
HeaderValue
X-EV-Statusvalid, invalid, catch_all, unknown or disposable (statuses), or unchecked
X-EV-ReasonThe result's reason, such as no_mailbox
X-EV-ErrorOnly with unchecked: timeout, no_email, login_form (a log-in; nothing is checked), several_addresses (two different addresses, or a list; nothing is checked), bad_body (a multipart form not in the exact shape browsers send, a form that declares a character set other than UTF-8 or carries qs's ISO-8859-1 sentinel utf8=%26%2310003%3B, or JSON past its limits: your server could read it differently; nothing is checked), too_large, not_configured, browser_key (the server key is a browser key), network, bad_answer, redirect or http_<status> (our API could not be reached or gave no usable answer), internal (the Worker itself failed), or our API's error code, such as insufficient_credits

It fails open. If our API does not answer within EV_TIMEOUT_MS, answers with an error, or the key or credits need fixing, the form goes through with X-EV-Status: unchecked. Your forms keep working whatever happens to us. After a refused key or with no credits left, the Worker stops calling for five minutes; after a rate limit, for as long as our API asks, at most five minutes. Forms go through unchecked meanwhile. JSON requests (unless EV_JSON is on), other methods and paths you skip are never touched.

Log-in posts

A field whose name says password (password, pass, pwd, passwd, pass1, user[password], newPassword, in any case) is never read as an address, so its value is never sent to us. The Worker knows a password field only by its name: one named another way (passcode, or a form builder's numbered field such as input_2) is read like any other field, and its value is sent for a check when it is shaped like an address, so skip the path of such a log-in. The word pass on its own says password even in a name that says email (emailPass, pass_holder_email: never sent); a longer word that ends in pass does not there unless the part before pass ends in mail (compass_email is an email field, emailpass a password field). A post with a password-named field and no field whose name says email (nor one named in EV_FIELDS) is a log-in: it is passed on unchecked (login_form) with no call. WordPress's log-in (log and pwd) and WooCommerce's (username and password, posted to /my-account/, where its registration is still checked) are such posts.

Still checked: a log-in whose user field is named email, such as Devise's (user[email] and user[password]), when its path is not in EV_SKIP_PATHS. The default skips /users/sign_in and the other usual log-in paths; if your log-in posts elsewhere, add its path. EV_SKIP_PATHS replaces the default list, so list the defaults in it too; the settings page shows the current list, defaults included, to add to.

A value found only by its shape that is not even an address's syntax (a version such as react@18.3.1 in a field whose name does not say email) is passed on with its result, never blocked.

Which result to trust

The Worker removes every header starting with X-EV- that a visitor sends, also when spelled with underscores (X_EV_Status, which servers such as Puma read as X-EV-Status), so on a request that went through the Worker these headers are always the Worker's own. They prove nothing on a request that reached your server some other way (straight to its IP address, or on a hostname your routes do not cover): let only Cloudflare reach your server (protect your origin server: Authenticated Origin Pulls, a Cloudflare Tunnel or a firewall that allows only Cloudflare's addresses), and treat a form without X-EV-Status as unchecked.

A request also skips the Worker when your account is over its daily Workers request limit and the route's request limit failure mode is Fail open: Cloudflare then sends requests on as if no Worker were there, a visitor's X-EV- headers included. On the Free plan every request on the route counts toward that limit, so it can be used up on purpose. Rely on X-EV-Status only on the Workers Paid plan, or with the failure mode set to Fail closed, which shows an error page on the route once the limit is reached instead.

Read the address from the form body only ($_POST, Request.Form, request.form), never from a source that merges the body with the query string or cookies ($_REQUEST, classic ASP.NET's Request[...] or Params). The Worker treats a second address in those places as several_addresses, but the body is the one place where the address checked and the address you read are surely the same, and only for a field the Worker reads by its name: one named in EV_FIELDS, or, with EV_FIELDS empty, one whose name says email. A field it finds only by its value (a form builder's input_2) can be shadowed by a second copy whose value it does not take for an address, or by a name your framework shortens (Gin's PostFormMap and QueryMap read user[login][pwd] as user[login]), so when your email field's name does not say email, set EV_FIELDS to it. The Worker reads the address from the form body, the query string and cookies, as common frameworks read them: each pair is also read as qs (Express, NestJS, Koa) cuts it, at its first ]= rather than its first =, so [user]q=r[email]=… is also the field user[email]. A server that takes the address from anywhere else (a value provider you added, a route value such as /signup/{email}) must read it from the form body only. One server setting the Worker does not follow: Express's body-parser 2 with defaultCharset: 'iso-8859-1' and interpretNumericEntities (both off by default) reads &#64; in any value as @, so a field the Worker reads as jo&#64;example.com, no address, is jo@example.com to your server. On such a server set EV_FIELDS to your email field's name: a value holding such a reference is then never checked as the address your server reads (it is several_addresses, or no valid address when the reference ends the value).

In tag mode with EV_TAG_FIELDS, the form the Worker read also carries ev_status and ev_reason: the Worker's own (unchecked and the error code when there is no result). The visitor's own fields of those names are taken out, with the spellings the Worker recognises (such as ev_status[], which PHP reads as ev_status), and so are query string parameters of those names in the URL. Two forms go on exactly as sent, with the result in the headers only: one larger than EV_MAX_BODY_KB (too_large), which the Worker does not read, and a form not in the exact shape browsers send, such as a multipart form in another shape or a form that declares a character set other than UTF-8 (bad_body). A visitor's own ev_status can then still be in them, and a spelling some framework reads in its own way could survive, so the header is the result: trust ev_status only when it equals X-EV-Status. A form carrying qs's ISO-8859-1 sentinel utf8=%26%2310003%3B is not checked either (bad_body), but it is tagged like any form the Worker read: the visitor's own ev_status and ev_reason out, ev_status=unchecked and ev_reason=bad_body in.

Test it

  1. Open a page with a form on your site and type disposable@sandbox.email-validation.org in its email field: the form guard shows its message and stops the form.
  2. Check the edge without the browser:
curl -i https://example.com/your-form-path \
  --data-urlencode "email=disposable@sandbox.email-validation.org"

In Windows PowerShell 5.1 or PowerShell 7 (a blocked form prints its status code):

try { Invoke-WebRequest -UseBasicParsing -Method Post -Uri 'https://example.com/your-form-path' -Body @{ email = 'disposable@sandbox.email-validation.org' } } catch { $_.Exception.Response.StatusCode.value__ }

The answer is 422 with the message. With accepted@sandbox.email-validation.org the form reaches your server with X-EV-Status: valid. Test addresses are free and answer the result their name says (all test addresses). Test on a form whose sign-up sends no email (no confirmation, welcome or newsletter): test addresses receive no mail, and the bounce hurts your sending.

The Worker's log (Workers & Pages → your Worker → Logs) shows one line per checked form, with the outcome, status, reason and time. The Worker never writes an address there.

Privacy, cost and limits

  • The Worker's own log lines never hold an address, and it stores none: they hold outcomes, statuses and timings only. The only service it calls is our API, and only for the address in a form. Cloudflare's Workers Logs, which wrangler.jsonc turns on, also record each request's URL, so a form that sends its fields in the address (method="get") puts them in those logs. To keep fewer, set a lower head sampling rate under observability (Workers Logs), or turn observability off.
  • Each check uses one credit; unknown results and test addresses are free. A sign-up checked in the browser and again at the edge uses two. Turn either part off (EV_CLIENT=off, or EV_MODE=off) to use one.
  • One copy of the Worker does not pay for the same check twice. Each running copy remembers its answers for ten minutes (keyed by a hash, never the address), and never sends a check again while one is running, so a form sent again to the same copy, such as after a double click, uses no other credit. Cloudflare runs many copies, in each location and over time, and they share no memory: the same address sent through another copy is checked, and paid for, again. unknown answers are not kept: they are free.
  • Limit the rate of form posts. A bot that posts thousands of forms makes thousands of checks. Add a rate limiting rule in Security → WAF for POST requests to your form paths (for example 10 per minute per IP address), and keep the server key's daily credit cap.
  • Time: a form post waits at most EV_TIMEOUT_MS for the check. Past it, the form goes on unchecked while the check finishes in the background, so its answer is kept for the next time that address is sent. Pages are rewritten as they stream, without waiting for the whole page.
  • Content Security Policy: if your site sends one, allow the form guard as its CSP section says. The Worker does not change your headers.
  • Workers limits: each checked form makes one call to our API besides the request to your server, well inside Cloudflare's limits. If the Worker itself fails, the request goes on to your server as if it were not there.

More on Cloudflare

Updated · Maintained by: Email Validation LLC

Need help? Contact support, or ask the help chat. Something wrong or missing on this page? Tell us