Skip to content

Rate Guard Developer Guide

Ed Mozley edited this page Nov 28, 2026 · 1 revision

Rate guard - developer guide

Count attempts, and block a source that makes too many - anywhere. includes/rate_guard.php, 5.12.0. It began as the analyst sign-in's IP ban (its own ip_login_bans table inside auth/login.php) and was made shared so portal sign-up and password resets could use the same thing.

πŸ“ Files

File What
includes/rate_guard.php The functions below
rate_guard table One row per (scope, subject): attempt_count, block_count, blocked_until, last_attempt. Unique on (scope, subject)
auth/login.php checkIpBan() / recordIpAttempt() - now two-line wrappers, scope login
includes/portal_registration.php portalRegistrationThrottle() - scopes portal_register_ip, portal_register_email, portal_reset_ip, portal_reset_email
api/system/debug-tools/D004_local_login.php Counts active login blocks
tests/portal-registration.php The guard's own checks, plus the sign-in's

The idea

A scope is what is protected (login, portal_register_ip). A subject is who is counted - an IP address, an email address, anything up to 191 characters, stored trimmed and lower case. Each (scope, subject) keeps a count; when it reaches the limit the subject is blocked for a while and the count starts again.

require_once __DIR__ . '/rate_guard.php';

if (($m = rateGuardBlockedMinutes($conn, 'portal_register_ip', $ip)) > 0) {
    http_response_code(429);   // ... "try again in $m minutes"
}
$nowBlocked = rateGuardHit($conn, 'portal_register_ip', $ip, 10, 60, 60);   // limit, window minutes, block minutes
Function
rateGuardBlockedMinutes($conn, $scope, $subject) Minutes left, 0 when not blocked
rateGuardHit($conn, $scope, $subject, $limit, $windowMinutes, $blockMinutes, $escalate = false, $escalateFloor = 1) Count one attempt; true when this one reached the limit. $limit <= 0 = off
rateGuardClear($conn, $scope, $subject) Forget a subject - an administrator lifting a block, or a success that should start the count again
rateGuardWait($minutes) ['unit' => 'hours'/'minutes', 'n' => ...] for a message
rateGuardSubject($s) / rateGuardReady($conn) The stored form; has Database Verification made the table
  • Window: attempts older than $windowMinutes since the last one are forgotten. 0 = never - how the sign-in has always counted.
  • Escalate: each earlier block lowers the next limit by one, never below $escalateFloor - a repeat offender is blocked sooner each time (the sign-in's rule: 5, then 4, 3, 2, 2).
  • The attempt that reaches the limit returns true; whether the caller refuses it or only the next one is the caller's choice. The sign-in and the portal both let it through and refuse the next.
  • The time left is worked out in SQL against UTC_TIMESTAMP(), so PHP's time zone cannot get it wrong (it is what the 5.1.0 summer-time lockout bug was).

πŸͺ€ Traps

  • TRAP: a database it cannot use never blocks. Every failed read answers "not blocked" and every failed write is skipped - as the sign-in's ban always did. Refusing on a database error would turn a hiccup into an outage for every sign-in. Before Database Verification has made the table there is simply no guard.
  • Pick a new scope name for a new use. Two features sharing a scope share a count: ten portal sign-ups would ban an analyst's IP from signing in.
  • Record the block, not every refused attempt. A caller that writes an audit row should write it once, when rateGuardHit() returns true - a sweep would otherwise fill the audit table (portalRegistrationThrottle() does this).
  • Tests from one machine share an IP. localhost is counted like anyone else and window 0 never forgets; a test that signs in wrongly should clear its scope for 127.0.0.1 / ::1 first and put the row back after (tests/security-report-2026-10-part2.php step 18).

The move from ip_login_bans

The sign-in counts exactly as before: limit max_ip_attempts, window 0, 24-hour block, escalating to min_ip_attempts. The one difference is at a limit of 1: the first attempt now blocks, where the old code needed two.

Database Verification copies ip_login_bans into scope login once - only on the run that creates rate_guard, so a lifted ban is never brought back by a later run. ip_login_bans stays in the schema, retired, and nothing reads or writes it.

See also

Self-Service Developer Guide Β§ Who may sign up Β· System Β· Keep me signed in

FreeITSM

Getting Started

Modules

Multi-tenancy (planned)

Responses to feedback

Blue sky thinking

Bugs resolved

Pull requests

Links

Clone this wiki locally