Admin panel, login throttling, and Cloudflare edge hardening (#150)

* Document the remote shell for production user management

* add sitemap to footer links

* remove border under theme selection

* Add admin theme and user management (#144)

Add /admin/themes CRUD and /admin/users list/detail with role and plan editing, guarded by a shared Policy module. Include last-super-admin and self-role protections plus structured id-only admin logs.

Also add admin invitations, confirmation resends and password-reset emails, which work in development through the Swoosh mailbox before SES is configured.

* Rate-limit password logins and emit auth events (#142)

Add a two-bucket login throttle (5 failures per login, 10 per IP in a 15-minute window) and a shared client-address helper for controllers and LiveViews. Blocked attempts skip the database and bcrypt.

Log auth.login.failed and auth.login.throttled warnings with boot-scoped HMAC addresses only, and stop repeated confirmation emails on unconfirmed login attempts.

* Harden the production login edge for Cloudflare

Document the layered login defence: the app throttle is primary, Caddy must pass Cloudflare's CF-Connecting-IP to the app, and Cloudflare edge rate limiting covers request floods.

Update the reference Caddyfile to accept only Cloudflare ranges and reject direct-to-origin traffic, and document the exact Cloudflare dashboard rules.

* Document 10-second Cloudflare rate-limit window

Cloudflare may only offer a 10-second period. Keep the edge rule at 10 login POSTs per 10 seconds per IP and rely on the app's 15-minute login/address throttle for credential guessing.

* Document Cloudflare limits for LiveView registration

Registration submits over the shared LiveView socket, so a Cloudflare rule on POST /users/register will not count account creations. Note the app-level throttle/Turnstile options instead.

* Document current Cloudflare free-plan edge controls

Record the production setup: Cloudflare-only Caddy origins, Bot Fight Mode, and the 10-second login flood rule (10 POSTs per 10 seconds, 10-second block). Clarify that the application's 15-minute throttle remains the real credential lockout.

* Auto-refresh Cloudflare CIDRs for Caddy

Move the origin allowlist into a generated cloudflare-ranges.caddy imported by Caddy. Add an hourly systemd timer and script that fetch Cloudflare's v4/v6 lists, reject malformed responses, validate the candidate Caddyfile, and reload Caddy only when the ranges change. Restore the previous file on validation or reload failure.

* Document Cloudflare CIDR refresher setup and operations

Record the installed paths, permissions, timer checks, manual run, update procedure, and the direct-on-VPS setup fallback when the deploy directory is absent.
This commit is contained in:
2026-11-11 21:27:59 +01:00
committed by GitHub
parent 202b9b1f6a
commit 624ddab644
45 changed files with 3602 additions and 70 deletions
+27 -4
View File
@@ -3,13 +3,36 @@
# This file is not installed automatically. On the shared VPS, add this block
# to the existing Caddy config by hand, then validate and reload Caddy.
#
# The app listens on loopback only; Caddy is the public entry point.
# Cloudflare sits in front of Caddy. Two things matter here:
#
# 1. Only accept requests from Cloudflare's published edge ranges.
# 2. Replace X-Forwarded-For with Cloudflare's single-value CF-Connecting-IP
# header, so the app's TRUST_FORWARDED_FOR=true path sees the visitor, not a
# Cloudflare edge address.
#
# The ranges live in cloudflare-ranges.caddy next to this file. A systemd
# timer refreshes them from https://www.cloudflare.com/ips-v4 and /ips-v6;
# see deploy/update-cloudflare-ranges.sh and deploy/README.md.
innersquare.app {
encode zstd gzip
reverse_proxy 127.0.0.1:4100
encode zstd gzip
@cloudflare {
import cloudflare-ranges.caddy
}
handle @cloudflare {
reverse_proxy 127.0.0.1:4100 {
header_up X-Forwarded-For {http.request.header.CF-Connecting-IP}
}
}
# Direct-to-origin requests cannot spoof Cloudflare's client-IP header.
handle {
respond "Access Denied" 403
}
}
www.innersquare.app {
redir https://innersquare.app{uri} permanent
redir https://innersquare.app{uri} permanent
}
+171 -8
View File
@@ -4,9 +4,10 @@ Phoenix release on the shared Ubuntu VPS, behind Caddy, with Postgres in Docker.
```text
Internet
└─ Caddy :443
└─ Phoenix release 127.0.0.1:4100
└─ Postgres 127.0.0.1:5432
└─ Cloudflare (TLS, WAF, edge rate limiting)
└─ Caddy :443 (cloudflare ranges only)
└─ Phoenix release 127.0.0.1:4100
└─ Postgres 127.0.0.1:5432
```
## Pinned versions
@@ -442,13 +443,30 @@ is in place.
## Caddy
On the current VPS the Caddyfile is a single file with no imports. Add the block
from `deploy/Caddyfile` by hand:
Add the site block from `deploy/Caddyfile` by hand. It imports
`cloudflare-ranges.caddy`, which is generated by
`deploy/update-cloudflare-ranges.sh` and refreshed by a systemd timer.
Caddy must only accept Cloudflare traffic and must replace `X-Forwarded-For`
with Cloudflare's `CF-Connecting-IP` value:
```caddy
innersquare.app {
encode zstd gzip
reverse_proxy 127.0.0.1:4100
@cloudflare {
import cloudflare-ranges.caddy
}
handle @cloudflare {
reverse_proxy 127.0.0.1:4100 {
header_up X-Forwarded-For {http.request.header.CF-Connecting-IP}
}
}
handle {
respond "Access Denied" 403
}
}
www.innersquare.app {
@@ -456,7 +474,72 @@ www.innersquare.app {
}
```
If a new VPS uses imports, put the block in its own site file instead.
Install the range file and the hourly refresh timer:
```sh
sudo install -m 0755 /opt/chesstrainer/src/deploy/update-cloudflare-ranges.sh \
/usr/local/sbin/chesstrainer-update-cloudflare-ranges
sudo install -m 0644 /opt/chesstrainer/src/deploy/cloudflare-ranges.caddy \
/etc/caddy/cloudflare-ranges.caddy
sudo install -m 0644 /opt/chesstrainer/src/deploy/chesstrainer-cloudflare-ranges.service \
/etc/systemd/system/chesstrainer-cloudflare-ranges.service
sudo install -m 0644 /opt/chesstrainer/src/deploy/chesstrainer-cloudflare-ranges.timer \
/etc/systemd/system/chesstrainer-cloudflare-ranges.timer
```
The installed pieces are:
| Path | Mode | Purpose |
|---|---|---|
| `/usr/local/sbin/chesstrainer-update-cloudflare-ranges` | `0755` | fetch/validate/reload script |
| `/etc/caddy/cloudflare-ranges.caddy` | `0644` | generated `remote_ip` list |
| `/etc/systemd/system/chesstrainer-cloudflare-ranges.service` | `0644` | oneshot service |
| `/etc/systemd/system/chesstrainer-cloudflare-ranges.timer` | `0644` | hourly timer |
If `/opt/chesstrainer/src/deploy` is not present on the VPS, create the same
four files directly with `sudo tee` from the canonical contents in `deploy/`.
The timer only depends on the installed copies; it does not read the repo.
Run it once manually and enable the timer:
```sh
sudo systemctl daemon-reload
sudo systemctl enable --now chesstrainer-cloudflare-ranges.timer
sudo systemctl start chesstrainer-cloudflare-ranges.service
sudo journalctl -u chesstrainer-cloudflare-ranges.service -n 50 --no-pager
```
The script fetches <https://www.cloudflare.com/ips-v4> and
<https://www.cloudflare.com/ips-v6>, validates the result, and reloads Caddy
only when the ranges actually changed. If the download, Caddy validation or
reload fails, it keeps or restores the previous file.
Check the timer and last run:
```sh
systemctl list-timers chesstrainer-cloudflare-ranges.timer
systemctl status chesstrainer-cloudflare-ranges.service
journalctl -u chesstrainer-cloudflare-ranges.service --since "24 hours ago"
```
Do not edit `/etc/caddy/cloudflare-ranges.caddy` by hand; the next timer run
overwrites it.
To update the refresher itself, replace the installed script or unit files from
the deploy directory, then:
```sh
sudo systemctl daemon-reload
sudo systemctl restart chesstrainer-cloudflare-ranges.timer
sudo systemctl start chesstrainer-cloudflare-ranges.service
```
Without this, `TRUST_FORWARDED_FOR=true` records a Cloudflare edge address
instead of the visitor, and the login throttle and guest puzzle limit bucket
whole Cloudflare POPs together.
If a new VPS uses imports, put the site block in its own file instead; keep the
`import cloudflare-ranges.caddy` path next to the Caddyfile.
Do **not** switch Caddy until a release is running and verified on
`127.0.0.1:4100`. Before that, keep the static coming-soon site live.
@@ -468,8 +551,16 @@ sudo caddy validate --config /etc/caddy/Caddyfile
sudo systemctl reload caddy
```
A direct request to the origin with a forged `CF-Connecting-IP` header should
receive `403 Access Denied`.
Do not let a deploy overwrite the shared Caddyfile.
See `docs/security.md` for the application throttle, real-IP setup and the
Cloudflare edge controls. Production currently has Bot Fight Mode enabled and
the free-plan login flood rule: 10 POSTs per 10 seconds per IP, blocked for 10
seconds.
## Building a release
Build on a machine matching Ubuntu 24.04 / glibc 2.39, or let CI do it.
@@ -694,6 +785,75 @@ Set `Storage=persistent` and a `SystemMaxUse=` value in
rate-limits bursts; if a crash storm drops lines, raise `RateLimitBurst` or
`RateLimitIntervalSec` there.
## Interactive shell and user management
Attach an IEx shell to the **running** release. This does not restart the
service and does not require direct database access:
```sh
sudo /opt/chesstrainer/current/bin/chesstrainer remote
```
You should get a prompt like `iex(chesstrainer@<host>)1>`. Exit with **Ctrl-C
twice**. Never call `System.halt/0`, `:init.stop/0`, or
`Application.stop(:chesstrainer)` from this shell — those stop production.
Use it for account and role management. Go through the application contexts
(not SQL) so changesets, bcrypt hashing, timestamps, and token bookkeeping stay
consistent:
```elixir
# Look up by email or username
u = Chesstrainer.Accounts.get_user_by_login("owen")
# Promote or demote
{:ok, u} = Chesstrainer.Accounts.update_user_role(u, :super_admin)
# Confirm an account without sending email
{:ok, u} = Chesstrainer.Repo.update(Chesstrainer.Accounts.User.confirm_changeset(u))
# Create an account, generating its password server-side
pw = :crypto.strong_rand_bytes(18) |> Base.url_encode64(padding: false)
{:ok, u} =
Chesstrainer.Accounts.register_user(%{
email: "you@example.com",
username: "owen",
password: pw
})
{:ok, u} = Chesstrainer.Repo.update(Chesstrainer.Accounts.User.confirm_changeset(u))
pw
```
Notes:
- `register_user/1` hashes the password with bcrypt; `password` is virtual and
is never written to the database.
- Do not type passwords into the shell if you can avoid it — IEx may keep a
history file — and never paste a password from shell history. Generate one as
above and change it later at `/users/profile/settings`.
- `mix accounts.promote` still works when a compiled source checkout is present
on the host, but the release shell is the supported production path.
- There is no guard against demoting the last `super_admin` outside the future
admin UI, so check the result before demoting.
If `remote` fails while booting its client VM (for example
`Kernel pid terminated (logger)` with `code_server`/`persistent_term` in the
trace), the deployed release is stale, mixed, or missing IEx: rebuild and
redeploy it. To attach in the meantime, use the release's own ERTS:
```sh
sudo bash -c '
COOKIE=$(cat /opt/chesstrainer/current/releases/COOKIE)
exec /opt/chesstrainer/current/erts-*/bin/erl \
-sname debug -setcookie "$COOKIE" -remsh chesstrainer
'
```
That shell speaks Erlang syntax, so quote Elixir module atoms, e.g.
`'Elixir.Chesstrainer.Accounts':register_user(...)`.
## Backups
`docker exec` connects over the container's Unix socket, which the Postgres
@@ -753,6 +913,9 @@ the VPS regularly. A backup that is only on the VPS is not a backup.
until a release exists.
6. Release built on a matching Ubuntu runner and activated.
7. `/health` and `/` verified on `127.0.0.1:4100`.
8. Caddy block added, validated, reloaded.
8. Caddy block, `cloudflare-ranges.caddy`, and the CIDR refresh timer added;
validated and reloaded.
9. Public `/`, `/privacy`, `/terms-conditions`, and `/health` verified.
10. Read-only role created, tunnel verified, backups scheduled and tested.
11. Remote shell attaches and exits cleanly:
`sudo /opt/chesstrainer/current/bin/chesstrainer remote` (Ctrl-C twice).
@@ -0,0 +1,11 @@
[Unit]
Description=Refresh Cloudflare CIDRs for Caddy
Documentation=https://github.com/TheRealOwenRees/chess-endgame-trainer/blob/main/docs/security.md
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/chesstrainer-update-cloudflare-ranges
TimeoutStartSec=2min
Nice=10
@@ -0,0 +1,11 @@
[Unit]
Description=Hourly refresh of Cloudflare CIDRs for Caddy
[Timer]
OnCalendar=hourly
RandomizedDelaySec=10m
Persistent=true
Unit=chesstrainer-cloudflare-ranges.service
[Install]
WantedBy=timers.target
+3
View File
@@ -0,0 +1,3 @@
# Generated by deploy/update-cloudflare-ranges.sh.
# Source: https://www.cloudflare.com/ips-v4 and /ips-v6
remote_ip 173.245.48.0/20 103.21.244.0/22 103.22.200.0/22 103.31.4.0/22 141.101.64.0/18 108.162.192.0/18 190.93.240.0/20 188.114.96.0/20 197.234.240.0/22 198.41.128.0/17 162.158.0.0/15 104.16.0.0/13 104.24.0.0/14 172.64.0.0/13 131.0.72.0/22 2400:cb00::/32 2606:4700::/32 2803:f800::/32 2405:b500::/32 2405:8100::/32 2a06:98c0::/29 2c0f:f248::/32
+115
View File
@@ -0,0 +1,115 @@
#!/usr/bin/env bash
#
# Refresh Caddy's Cloudflare origin allowlist from Cloudflare's published
# ranges. Called by chesstrainer-cloudflare-ranges.timer.
#
# The generated file is imported by /etc/caddy/Caddyfile:
#
# @cloudflare {
# import cloudflare-ranges.caddy
# }
#
# The script validates the candidate Caddyfile before reloading Caddy. If the
# download or validation fails, the previous ranges stay in place.
set -euo pipefail
CADDYFILE="${CADDYFILE:-/etc/caddy/Caddyfile}"
RANGES_FILE="${RANGES_FILE:-/etc/caddy/cloudflare-ranges.caddy}"
CADDY_BIN="${CADDY_BIN:-caddy}"
RELOAD_CMD="${RELOAD_CMD:-systemctl reload caddy}"
LOCK_FILE="${LOCK_FILE:-/run/lock/chesstrainer-cloudflare-ranges.lock}"
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' EXIT
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
echo "another Cloudflare range update is already running" >&2
exit 0
fi
fetch() {
local url="$1"
local output="$2"
curl --fail --silent --show-error --location \
--proto '=https' \
--retry 3 --retry-delay 2 --connect-timeout 10 \
"$url" --output "$output"
}
validate_download() {
local file="$1"
[[ -s "$file" ]] || return 1
# One CIDR per line. Caddy performs the real validation; this prevents
# obviously malformed or injected content from being written.
if grep -Evq '^[0-9A-Fa-f:.]+/[0-9]{1,3}$' "$file"; then
return 1
fi
}
fetch "https://www.cloudflare.com/ips-v4" "$tmpdir/ips-v4"
fetch "https://www.cloudflare.com/ips-v6" "$tmpdir/ips-v6"
validate_download "$tmpdir/ips-v4"
validate_download "$tmpdir/ips-v6"
ranges="$(awk 'NF { printf "%s ", $0 }' "$tmpdir/ips-v4" "$tmpdir/ips-v6" | sed -e 's/[[:space:]]*$//')"
if [[ -z "$ranges" ]]; then
echo "Cloudflare returned an empty CIDR list" >&2
exit 1
fi
candidate="$tmpdir/cloudflare-ranges.caddy"
{
echo "# Generated by deploy/update-cloudflare-ranges.sh."
echo "# Source: https://www.cloudflare.com/ips-v4 and /ips-v6"
echo "remote_ip $ranges"
} >"$candidate"
if cmp -s "$candidate" "$RANGES_FILE"; then
echo "Cloudflare ranges are unchanged"
exit 0
fi
install -d -m 0755 "$(dirname "$RANGES_FILE")"
backup="$tmpdir/previous-ranges.caddy"
if [[ -e "$RANGES_FILE" ]]; then
cp -a "$RANGES_FILE" "$backup"
fi
install -m 0644 "$candidate" "$RANGES_FILE"
if ! "$CADDY_BIN" validate --config "$CADDYFILE" --adapter caddyfile; then
echo "Caddy rejected the new Cloudflare ranges; restoring the previous file" >&2
if [[ -e "$backup" ]]; then
install -m 0644 "$backup" "$RANGES_FILE"
else
rm -f "$RANGES_FILE"
fi
exit 1
fi
read -r -a reload_command <<<"$RELOAD_CMD"
if ! "${reload_command[@]}"; then
echo "Caddy reload failed; restoring the previous ranges" >&2
if [[ -e "$backup" ]]; then
install -m 0644 "$backup" "$RANGES_FILE"
else
rm -f "$RANGES_FILE"
fi
exit 1
fi
echo "Cloudflare ranges updated and Caddy reloaded"