# TLS-terminating reverse proxy for the PUBLIC submission service. Replace ledger.example.org.
# Caddy obtains and renews the certificate itself. The Python service binds to 127.0.0.1:8080 only.
#
# Two settings matter for safety:
#   * X-Forwarded-For is SET to the real client address by header_up (it overwrites whatever the client sent). public_portal.py
#     (run with --trust-proxy) rate-limits on that value, so a client must not be able to choose it.
#   * the request body is capped well above MAX_UPLOAD (200 KB) so oversized uploads are refused at the proxy.

ledger.example.org {
	encode gzip

	request_body {
		max_size 300KB
	}

	# the static site (python3 build_site.py --out /srv/ledger/site ...) is served from disk, INCLUDING its own /assets/site.css;
	# only /round, /s, /api and /portal-assets (the service's own stylesheet, a different path on purpose) go to the Python service
	@service path /round/* /s/* /api/* /portal-assets/* /healthz
	handle @service {
		reverse_proxy 127.0.0.1:8080 {
			header_up X-Forwarded-For {remote_host}
			transport http {
				dial_timeout 3s
				response_header_timeout 30s
			}
		}
	}

	handle {
		root * /srv/ledger/site
		# the static pages carry one inline theme script and a few inline style attributes, hence 'unsafe-inline'; nothing here handles user input
		header X-Frame-Options "DENY"
		header Content-Security-Policy "default-src 'none'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src https://fonts.gstatic.com; script-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'"
		file_server
	}

	header {
		Strict-Transport-Security "max-age=31536000"
		X-Content-Type-Options "nosniff"
		Referrer-Policy "no-referrer"
		-Server
	}
}

# Any other Host name pointed at this machine (DNS rebinding, a stray domain) is refused; without this block Caddy answers an empty 200.
:443 {
	respond "forbidden" 403
}
