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
+84
View File
@@ -0,0 +1,84 @@
defmodule ChesstrainerWeb.ClientAddress do
@moduledoc """
Resolves the client's IP address from either a controller connection or a
LiveView socket.
`X-Forwarded-For` is only read when
`Chesstrainer.GuestLimits.trust_forwarded_for?/0` is enabled. In that case the
rightmost entry is used, because that is the value our own proxy appended.
Invalid or missing headers fall back to the peer address.
Production is Cloudflare -> Caddy -> Phoenix. `deploy/Caddyfile` makes Caddy
overwrite `X-Forwarded-For` with Cloudflare's `CF-Connecting-IP` and rejects
direct-to-origin requests, so the rightmost value is the real visitor rather
than a Cloudflare edge address.
"""
alias Chesstrainer.GuestLimits
@typedoc "An IPv4/IPv6 address, or `nil` when no peer data is available."
@type address :: :inet.ip_address() | nil
@doc "Returns the client address for a controller connection."
@spec from_conn(Plug.Conn.t()) :: address()
def from_conn(conn) do
if GuestLimits.trust_forwarded_for?() do
conn
|> Plug.Conn.get_req_header("x-forwarded-for")
|> parse_forwarded_header()
|> fallback(conn.remote_ip)
else
conn.remote_ip
end
end
@doc "Returns the client address for a connected LiveView socket."
@spec from_socket(Phoenix.LiveView.Socket.t()) :: address()
def from_socket(socket) do
if GuestLimits.trust_forwarded_for?() do
socket
|> Phoenix.LiveView.get_connect_info(:x_headers)
|> parse_x_headers()
|> fallback(peer_address(socket))
else
peer_address(socket)
end
end
defp peer_address(socket) do
case Phoenix.LiveView.get_connect_info(socket, :peer_data) do
%{address: address} -> address
_ -> nil
end
end
defp parse_forwarded_header([value | _]) when is_binary(value), do: parse_address(value)
defp parse_forwarded_header(_headers), do: nil
defp parse_x_headers(headers) when is_list(headers) do
case List.keyfind(headers, "x-forwarded-for", 0) do
{_, value} when is_binary(value) -> parse_address(value)
_ -> nil
end
end
defp parse_x_headers(_headers), do: nil
# The rightmost entry is the one our own proxy appended.
defp parse_address(value) do
value
|> String.split(",")
|> List.last()
|> to_string()
|> String.trim()
|> String.to_charlist()
|> :inet.parse_address()
|> case do
{:ok, address} -> address
{:error, _} -> nil
end
end
defp fallback(nil, fallback), do: fallback
defp fallback(address, _fallback), do: address
end