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
@@ -0,0 +1,93 @@
defmodule ChesstrainerWeb.LoginThrottleTest do
use ExUnit.Case, async: false
alias ChesstrainerWeb.LoginThrottle
@address {203, 0, 113, 7}
@other_address {198, 51, 100, 9}
@login "someone@example.com"
setup do
LoginThrottle.reset()
:ok
end
test "uses the tightened limits and a 15 minute window" do
assert LoginThrottle.max_per_login() == 5
assert LoginThrottle.max_per_ip() == 10
assert LoginThrottle.window_ms() == :timer.minutes(15)
end
test "blocks the next attempt after the login limit is reached" do
for _ <- 1..(LoginThrottle.max_per_login() - 1) do
assert :ok = LoginThrottle.record_failure(@address, @login)
end
assert :ok = LoginThrottle.check(@address, @login)
assert :ok = LoginThrottle.record_failure(@address, @login)
assert {:throttled, :login, retry_in_ms} = LoginThrottle.check(@address, @login)
assert retry_in_ms > 0
end
test "blocks the next attempt after the address limit is reached across logins" do
for index <- 1..LoginThrottle.max_per_ip() do
assert :ok = LoginThrottle.record_failure(@address, "user#{index}@example.com")
end
assert {:throttled, :ip, retry_in_ms} =
LoginThrottle.check(@address, "fresh@example.com")
assert retry_in_ms > 0
end
test "a successful login clears only the login bucket" do
for _ <- 1..(LoginThrottle.max_per_login() - 1) do
LoginThrottle.record_failure(@address, @login)
end
assert :ok = LoginThrottle.record_success(@login)
for _ <- 1..(LoginThrottle.max_per_login() - 1) do
LoginThrottle.record_failure(@address, @login)
end
assert :ok = LoginThrottle.check(@address, @login)
end
test "addresses are independent" do
for index <- 1..LoginThrottle.max_per_ip() do
LoginThrottle.record_failure(@address, "user#{index}@example.com")
end
assert {:throttled, :ip, _} = LoginThrottle.check(@address, "fresh@example.com")
assert :ok = LoginThrottle.check(@other_address, "fresh@example.com")
end
test "log fingerprints are stable and do not expose the address" do
fingerprint = LoginThrottle.fingerprint(@address)
assert is_binary(fingerprint)
assert fingerprint == LoginThrottle.fingerprint(@address)
refute fingerprint =~ "203.0.113.7"
end
test "confirmation resends are claimed at most once per cooldown" do
assert LoginThrottle.claim_confirmation_resend(@login)
refute LoginThrottle.claim_confirmation_resend(@login)
assert LoginThrottle.claim_confirmation_resend("another@example.com")
end
test "reset clears every bucket" do
for index <- 1..LoginThrottle.max_per_ip() do
LoginThrottle.record_failure(@address, "user#{index}@example.com")
end
assert {:throttled, :ip, _} = LoginThrottle.check(@address, "fresh@example.com")
LoginThrottle.reset()
assert :ok = LoginThrottle.check(@address, "fresh@example.com")
end
end