Let the login throttle see real client IPs behind Cloudflare

Behind a proxy REMOTE_ADDR is the proxy, so the per-IP limit would lump
every visitor together. New optional CLIENT_IP_HEADER in config.php
(e.g. 'CF-Connecting-IP') tells the throttle which header carries the
real address; invalid or absent values fall back to REMOTE_ADDR, and
configs that predate the setting behave as before. Only for origins
unreachable except through the proxy — documented in the README, which
also covers the mod_remoteip/real_ip alternative.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SePf61iazhCLJBefdRLBDm
This commit is contained in:
Harwinder Sekhon
2026-07-30 21:17:46 -04:00
parent 96651adca7
commit eb93303c09
4 changed files with 35 additions and 1 deletions
+19
View File
@@ -102,6 +102,25 @@ const LOGIN_FAIL_WINDOW = 900; // seconds
const LOGIN_MAX_USER_FAILS = 5;
const LOGIN_MAX_IP_FAILS = 20;
/**
* The client's IP for the throttle. Behind a proxy that hides the visitor
* (Cloudflare), REMOTE_ADDR is the proxy — set CLIENT_IP_HEADER in
* config.php to the header carrying the real address instead. Optional and
* off by default: installs whose config.php predates the setting keep using
* REMOTE_ADDR.
*/
function clientIp(): string
{
if (defined('CLIENT_IP_HEADER') && CLIENT_IP_HEADER !== '') {
$key = 'HTTP_' . strtoupper(str_replace('-', '_', CLIENT_IP_HEADER));
$ip = trim(explode(',', $_SERVER[$key] ?? '')[0]);
if ($ip !== '' && filter_var($ip, FILTER_VALIDATE_IP)) {
return $ip;
}
}
return $_SERVER['REMOTE_ADDR'] ?? '';
}
/** Seconds until this username/IP may try again — 0 means go ahead. */
function loginRetryAfter(string $username, string $ip): int
{