A static site on Cloudflare Workers with a locked-down contact form
How innerpulse.net serves static pages from Cloudflare Workers and handles its one dynamic page, the contact form, with signed tokens, rate limits and strict headers.
This site is written in PHP, but no PHP runs when you read it. Every page is rendered ahead of time into plain HTML and served from Cloudflare Workers. There is nothing to patch, no database to keep warm and almost nothing to attack. The one page that has to do something on request is the contact form, and that small piece of code is where the security work went.
Render once, serve everywhere
A build script walks every route of the PHP site and writes the result to a folder of static files, along with the sitemap, redirects and security headers. That folder is deployed as a Worker’s static assets, so pages are served straight from Cloudflare’s network close to the reader. Connecticut Atlas is hosted the same way.
Keeping PHP as the authoring tool means templates, shared layouts and content files stay easy to edit, while the public site has the speed and simplicity of plain files. The static output is also easy to check: a smoke test runs against the live site after every deploy.
One route runs code
The Worker is configured to run first only for /contact. Every other request goes straight to a static file. That keeps the code that can be reached from the internet as small as possible.
When someone opens the contact page, the Worker takes the pre-rendered form and adds a signed token on the way out, using Cloudflare’s streaming HTML rewriter. The token is the current time plus an HMAC-SHA256 signature made with a secret key that only the Worker knows. The page is marked as not to be cached, so every visitor gets a fresh token.
Checking a submission
When the form is sent, the Worker checks several things before it stores anything.
- The token must be genuine and signed with the secret, so the form cannot be posted from a script that never loaded the page.
- The token must be old enough, because a person takes a few seconds to fill in a form and a bot usually does not, and young enough, so a token cannot be saved and reused for days.
- A hidden field that people never see must be left empty. Simple bots fill in every field they find.
- Each address may send only a few inquiries an hour. The count is kept in the database with an index on address and time, so the check stays fast.
A submission that passes is stored in Cloudflare D1, a SQLite database that runs alongside the Worker. A notification is sent in the background, after the response, so a slow notification service never makes the visitor wait. The visitor is then redirected to a thank-you page, which also means refreshing the page will not send the form twice.
Headers that limit the damage
Every response carries a strict Content Security Policy. Pages may load only the site’s own scripts, styles, fonts and images, plus Cloudflare’s privacy-focused analytics script, forms may only post back to the site, and other sites may not frame its pages. The rules are set in the static headers file and again by the Worker on the pages it handles, so the dynamic route is never less protected than the static ones.
The site uses no third-party fonts, advertising or tracking scripts, which keeps that policy short enough to read at a glance and easy to keep strict.
Why this suits most business sites
- Most pages change only when someone edits them, so they can be built once and served as files.
- Running code on one route instead of every route shrinks what can go wrong.
- Signed tokens, a timing check, a hidden field and a rate limit stop most form spam without a CAPTCHA.
- Inquiries land in a database you own, not only in an inbox.
- Hosting costs stay close to nothing, and there is no server to maintain.
The same approach works for any site whose pages are mostly content, with a few forms or small features that need to run on request.
