Get Started
Protect your app and website

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.

60 minIntermediateFreeStep 1 free

What you'll do

Set up TLS and the six headers

Get automatic certificates, forced HTTPS and the security headers in place, as a Caddyfile and as a next.config.ts.

Configure CORS and Cloudflare

Fix CORS for an API that uses cookies, then put Cloudflare in front with WAF rules and a rate limit on login.

Wire up Turnstile and test it

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
Before you start
  • 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.py script from mission 1
  • If you host on a VPS: SSH access. Harden a server first if it's a fresh box
Your progress0/6
01/06TLS properly: automatic certs, forced HTTPS, old versions off0 done
STEP 01 of 06~10 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

  1. On a VPS with Caddy installed (apt install caddy or 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 reload and 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.

Tip
Test the whole chain at ssllabs.com/ssltest ↗. An A is the target; A+ needs HSTS. Anything below B lists the exact reason.
Checkpoint
curl -sI http://your-site returns a 301 or 308 to https://, and curl --tls-max 1.1 https://your-site fails.
If it breaksCaddy can't get a certificate + 1 more
Caddy can't get a certificate
Port 80 and 443 must be reachable from the internet for the ACME challenge; check your cloud firewall. If Cloudflare proxy (orange cloud) is already on, Caddy's HTTP challenge may fail: turn the proxy off until the cert is issued, or use Cloudflare origin certificates.
SSL Labs says 'certificate chain incomplete'
You're serving the leaf cert without the intermediate. Caddy and certbot both handle this; a hand-installed cert from a provider often ships as two files, and both go in the chain.
Still stuck? Ask the community
Next mission · Protect your app and websiteUntrusted input and the database: SQL injection, XSS, SSRF, path traversal, and the fixes that end them
→