# SITE_ADDRESS is the domain to serve (e.g. lottery.example.com) — Caddy
# automatically requests a Let's Encrypt certificate for it.
#
# Left at the default "localhost" (dev mode, no domain), Caddy detects it's
# not a public hostname and issues a locally-trusted self-signed certificate
# instead, via its internal CA. Browsers will still warn on first visit
# unless that CA is explicitly trusted — expected for local/dev use.
{$SITE_ADDRESS:localhost} {
	encode gzip

	# B-43: Caddy adds none of these on its own. The session cookie is HttpOnly, but a CSP is
	# still the main mitigation against injected script. 'unsafe-inline' stays for now because
	# Django Admin uses inline scripts and styles; the pages of the new site (roadmap A5-A8)
	# are written without them so the policy can be tightened for them later.
	header {
		Strict-Transport-Security "max-age=31536000; includeSubDomains"
		X-Content-Type-Options "nosniff"
		X-Frame-Options "DENY"
		Referrer-Policy "strict-origin-when-cross-origin"
		Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self'; connect-src 'self'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; object-src 'none'"
	}

	# B-54: Caddy *appends* the real peer address to whatever X-Forwarded-For the
	# client sent, so without this the header arrives as "<whatever the client
	# claimed>, <real ip>" and every IP-keyed control in the app (the login and
	# registration throttles of B-33, the SSE per-IP subscriber cap of B-38) is
	# defeated by simply rotating a fake value per request. Overwriting the header
	# with the actual peer makes the app's assumption true at the source; it also
	# reads the last hop rather than the first, so the two defences hold independently.
	reverse_proxy web:8000 {
		header_up X-Forwarded-For {remote_host}
	}
}
