
Mission 3 of 10
Lock down the edge in front of your app: TLS, headers, CORS and Cloudflare.
Between a user's browser and your code sits a layer most people configure once from a blog post. You'll set it properly, learn what each setting blocks and what it doesn't, then test it from outside.
What you'll do
Get automatic certificates, forced HTTPS and the security headers in place, as a Caddyfile and as a next.config.ts.
Fix CORS for an API that uses cookies, then put Cloudflare in front with WAF rules and a rate limit on login.
Replace a CAPTCHA with Turnstile verified on the server, then check the headers, redirect, CORS and TLS versions from outside with curl.
- Know what each setting blocks, and what it doesn't
- A curl check you can rerun after every change
- Your baseline script reading clean on the edge lines
- A site or API you own on a domain you control, deployed anywhere (VPS, Vercel, Cloudflare Pages, Netlify, Fly)
- Access to the domain's DNS so you can put Cloudflare in front (free account)
- curl in a terminal. The
baseline.pyscript from mission 1 - If you host on a VPS: SSH access. Harden a server first if it's a fresh box
Your progress0/6
- 01TLS properly: automatic certs, forced HTTPS, old versions off10 min
- 02The six headers, and what each one blocks12 min
- 03CORS and cookies: the settings that quietly expose your API8 min
- 04Cloudflare in front: WAF, bot fight mode, a rate-limit rule15 min
- 05Turnstile instead of a CAPTCHA, wired end to end10 min
- 06Verify it all with curl, and next: inputs and the database5 min
TLS properly: automatic certs, forced HTTPS, old versions off
HTTPS isn't a tick box. The three mistakes are a cert that expires, HTTP that still works, and TLS 1.0 still enabled.
Start with TLS. The aim is certificates that renew themselves, HTTP that redirects to HTTPS, and old TLS versions switched off.
What to use
If you're on Vercel, Netlify, Cloudflare Pages or Fly, TLS is handled and auto-renewed; your job is only to force HTTPS and set HSTS.
If you're on a VPS, use Caddy as the reverse proxy. It gets a Let's Encrypt certificate on first start, renews it forever, redirects HTTP to HTTPS, and ships with TLS 1.2 and 1.3 only. Nginx can do all of this too; it needs certbot and five times the config to get the same result.
Why
Expired certificates are the most common self-inflicted outage on small sites, and they train users to click through warnings.
HTTP that still answers means a link typed without https:// sends the first request in the clear, including any cookie that isn't Secure. TLS 1.0 and 1.1 have known weaknesses and every browser dropped them in 2020; keeping them on buys you nothing but a red line on a scanner.
How
On a VPS with Caddy installed (
apt install caddyor the Caddy docs ↗), use this as the whole configuration. It includes every header from the next step:/etc/caddy/Caddyfile# Caddyfile: automatic TLS from Let's Encrypt, HTTP redirected, headers on every response. your-site.example { reverse_proxy localhost:8000 header { Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" X-Content-Type-Options "nosniff" X-Frame-Options "DENY" Referrer-Policy "strict-origin-when-cross-origin" Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=()" Content-Security-Policy "default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; script-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'" -Server -X-Powered-By } }Run
caddy reloadand it's live.Caddy also handles the HTTP-to-HTTPS redirect automatically for any site block with a hostname. On Vercel or Netlify there's nothing to do for the cert; both redirect HTTP by default.
If Cloudflare is in front
Putting Cloudflare in front (step 5) adds a wrinkle: Cloudflare terminates TLS for the user, then connects to your origin.
Set SSL/TLS mode to Full (strict) so that second hop is also verified. 'Flexible' means Cloudflare talks HTTP to your server, which is HTTPS theatre.
curl -sI http://your-site returns a 301 or 308 to https://, and curl --tls-max 1.1 https://your-site fails.