mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
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:
@@ -0,0 +1,83 @@
|
||||
defmodule ChesstrainerWeb.ClientAddressTest do
|
||||
use ExUnit.Case, async: false
|
||||
|
||||
import Plug.Test
|
||||
|
||||
alias Chesstrainer.GuestLimits
|
||||
alias ChesstrainerWeb.ClientAddress
|
||||
|
||||
@peer {10, 0, 0, 1}
|
||||
@forwarded {203, 0, 113, 9}
|
||||
|
||||
setup do
|
||||
previous = Application.get_env(:chesstrainer, GuestLimits, [])
|
||||
on_exit(fn -> Application.put_env(:chesstrainer, GuestLimits, previous) end)
|
||||
:ok
|
||||
end
|
||||
|
||||
test "ignores X-Forwarded-For when trust is off" do
|
||||
trust_forwarded_for(false)
|
||||
|
||||
conn =
|
||||
conn(:post, "/users/log-in")
|
||||
|> Map.put(:remote_ip, @peer)
|
||||
|> Map.put(:req_headers, [{"x-forwarded-for", "203.0.113.9"}])
|
||||
|
||||
assert ClientAddress.from_conn(conn) == @peer
|
||||
end
|
||||
|
||||
test "honours the rightmost X-Forwarded-For entry when trust is on" do
|
||||
trust_forwarded_for(true)
|
||||
|
||||
conn =
|
||||
conn(:post, "/users/log-in")
|
||||
|> Map.put(:remote_ip, @peer)
|
||||
|> Map.put(:req_headers, [{"x-forwarded-for", "198.51.100.2, 203.0.113.9"}])
|
||||
|
||||
assert ClientAddress.from_conn(conn) == @forwarded
|
||||
end
|
||||
|
||||
test "falls back to the peer address for an invalid forwarded header" do
|
||||
trust_forwarded_for(true)
|
||||
|
||||
conn =
|
||||
conn(:post, "/users/log-in")
|
||||
|> Map.put(:remote_ip, @peer)
|
||||
|> Map.put(:req_headers, [{"x-forwarded-for", "not-an-address"}])
|
||||
|
||||
assert ClientAddress.from_conn(conn) == @peer
|
||||
end
|
||||
|
||||
test "ignores socket X-Forwarded-For when trust is off" do
|
||||
trust_forwarded_for(false)
|
||||
|
||||
assert ClientAddress.from_socket(socket()) == @peer
|
||||
end
|
||||
|
||||
test "honours socket X-Forwarded-For when trust is on" do
|
||||
trust_forwarded_for(true)
|
||||
|
||||
assert ClientAddress.from_socket(socket()) == @forwarded
|
||||
end
|
||||
|
||||
defp socket do
|
||||
%Phoenix.LiveView.Socket{
|
||||
private: %{
|
||||
connect_info: %{
|
||||
peer_data: %{address: @peer},
|
||||
x_headers: [{"x-forwarded-for", "203.0.113.9"}]
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
defp trust_forwarded_for(enabled?) do
|
||||
config = Application.get_env(:chesstrainer, GuestLimits, [])
|
||||
|
||||
Application.put_env(
|
||||
:chesstrainer,
|
||||
GuestLimits,
|
||||
Keyword.put(config, :trust_forwarded_for, enabled?)
|
||||
)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user