Jake Vanderwerf
2025-12-21 3aada9949d51024a92a8b5c6cb70d12f9c3cac16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
namespace JVBase\managers;
 
if (!defined('ABSPATH')) {
    exit;
}
 
/**
 * Simple rate limiter for AJAX requests (non-REST)
 * Includes both hourly limits AND burst protection
 */
class AjaxRateLimiter
{
    protected array $limits = [
        'login' => [
            'count' => 20,           // Hourly limit
            'window' => 3600,        // 1 hour
            'burst_count' => 5,      // Burst limit
            'burst_window' => 60     // 1 minute
        ],
        'register' => [
            'count' => 10,
            'window' => 3600,
            'burst_count' => 3,
            'burst_window' => 60
        ],
        'lostpassword' => [
            'count' => 10,
            'window' => 3600,
            'burst_count' => 3,
            'burst_window' => 60
        ],
        'resetpass' => [
            'count' => 10,
            'window' => 3600,
            'burst_count' => 3,
            'burst_window' => 60
        ],
    ];
 
    /**
     * Check if action is within rate limits (both hourly and burst)
     *
     * @param string $action The action being performed (login, register, etc.)
     * @return bool True if within limits, false if exceeded
     */
    public function checkLimit(string $action): bool
    {
        // Check burst protection first (stricter, prevents rapid-fire)
        if (!$this->checkBurstLimit($action)) {
            return false;
        }
 
        // Then check hourly limit
        return $this->checkHourlyLimit($action);
    }
 
    /**
     * Check burst protection (prevents rapid-fire attempts)
     *
     * Example: 5 login attempts in 10 seconds = blocked
     *
     * @param string $action The action being performed
     * @return bool True if within burst limits, false if exceeded
     */
    protected function checkBurstLimit(string $action): bool
    {
        $limit = $this->getLimit($action);
 
        // Skip if no burst protection configured
        if (!isset($limit['burst_count'])) {
            return true;
        }
 
        $key = $this->getCacheKey($action) . '_burst';
        $data = get_transient($key);
 
        if (!$data) {
            $data = ['count' => 0, 'first_attempt' => time()];
        }
 
        // Check if burst window expired
        $elapsed = time() - $data['first_attempt'];
        if ($elapsed >= $limit['burst_window']) {
            // Window expired, reset
            $data = ['count' => 0, 'first_attempt' => time()];
        }
 
        // Check if burst limit exceeded
        if ($data['count'] >= $limit['burst_count']) {
            // Log for security monitoring
            error_log(sprintf(
                'Burst rate limit exceeded for %s from %s: %d attempts in %d seconds',
                $action,
                $this->getClientIp(),
                $data['count'],
                $elapsed
            ));
            return false;
        }
 
        // Increment and save
        $data['count']++;
        set_transient($key, $data, $limit['burst_window']);
 
        return true;
    }
 
    /**
     * Check hourly rate limit
     *
     * @param string $action The action being performed
     * @return bool True if within hourly limits, false if exceeded
     */
    protected function checkHourlyLimit(string $action): bool
    {
        $key = $this->getCacheKey($action);
        $limit = $this->getLimit($action);
 
        // Get current count
        $data = get_transient($key);
        if (!$data) {
            $data = ['count' => 0, 'first_attempt' => time()];
        }
 
        // Check if window has expired
        if (time() - $data['first_attempt'] >= $limit['window']) {
            // Window expired, reset
            $data = ['count' => 0, 'first_attempt' => time()];
        }
 
        // Check if limit exceeded
        if ($data['count'] >= $limit['count']) {
            // Log for security monitoring
            error_log(sprintf(
                'Hourly rate limit exceeded for %s from %s: %d attempts',
                $action,
                $this->getClientIp(),
                $data['count']
            ));
            return false;
        }
 
        // Increment and save
        $data['count']++;
        set_transient($key, $data, $limit['window']);
 
        return true;
    }
 
    /**
     * Get remaining attempts for an action
     *
     * @param string $action The action being performed
     * @return array ['remaining' => int, 'reset_at' => int, 'burst_remaining' => int, 'burst_reset_at' => int]
     */
    public function getRemaining(string $action): array
    {
        $limit = $this->getLimit($action);
 
        // Hourly remaining
        $key = $this->getCacheKey($action);
        $data = get_transient($key);
 
        $hourly_remaining = $limit['count'];
        $hourly_reset_at = time() + $limit['window'];
 
        if ($data) {
            $hourly_remaining = max(0, $limit['count'] - $data['count']);
            $hourly_reset_at = $data['first_attempt'] + $limit['window'];
        }
 
        // Burst remaining (if configured)
        $burst_remaining = $limit['burst_count'] ?? null;
        $burst_reset_at = null;
 
        if (isset($limit['burst_count'])) {
            $burst_key = $key . '_burst';
            $burst_data = get_transient($burst_key);
 
            if ($burst_data) {
                $burst_remaining = max(0, $limit['burst_count'] - $burst_data['count']);
                $burst_reset_at = $burst_data['first_attempt'] + $limit['burst_window'];
            } else {
                $burst_reset_at = time() + $limit['burst_window'];
            }
        }
 
        return [
            'remaining' => $hourly_remaining,
            'reset_at' => $hourly_reset_at,
            'burst_remaining' => $burst_remaining,
            'burst_reset_at' => $burst_reset_at
        ];
    }
 
    /**
     * Generate cache key based on IP and action
     *
     * @param string $action The action being performed
     * @return string Cache key
     */
    protected function getCacheKey(string $action): string
    {
        $ip = $this->getClientIp();
        $user_id = get_current_user_id(); // 0 if not logged in
 
        return BASE . 'ajax_rate_limit_' . md5($ip . '_' . $user_id . '_' . $action);
    }
 
    /**
     * Get client IP address (supports proxies)
     *
     * @return string IP address
     */
    protected function getClientIp(): string
    {
        // Check for proxy headers first
        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            // X-Forwarded-For can contain multiple IPs, get the first one
            $ips = explode(',', $ip);
            return trim($ips[0]);
        }
 
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
            return $_SERVER['HTTP_CLIENT_IP'];
        }
 
        return $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
    }
 
    /**
     * Get limit configuration for an action
     *
     * @param string $action The action being performed
     * @return array Limit configuration
     */
    protected function getLimit(string $action): array
    {
        return $this->limits[$action] ?? $this->limits['login'];
    }
 
    /**
     * Clear rate limit for a specific action (useful for testing)
     *
     * @param string $action The action to clear
     * @return bool True if cleared, false otherwise
     */
    public function clearLimit(string $action): bool
    {
        $key = $this->getCacheKey($action);
        $burst_key = $key . '_burst';
 
        $result1 = delete_transient($key);
        $result2 = delete_transient($burst_key);
 
        return $result1 || $result2;
    }
 
    /**
     * Update limit configuration
     *
     * @param string $action The action to update
     * @param int $count Max attempts per window
     * @param int $window Time window in seconds
     * @param int|null $burst_count Optional burst limit
     * @param int|null $burst_window Optional burst window
     */
    public function setLimit(
        string $action,
        int $count,
        int $window,
        ?int $burst_count = null,
        ?int $burst_window = null
    ): void {
        $this->limits[$action] = [
            'count' => $count,
            'window' => $window
        ];
 
        if ($burst_count !== null && $burst_window !== null) {
            $this->limits[$action]['burst_count'] = $burst_count;
            $this->limits[$action]['burst_window'] = $burst_window;
        }
    }
 
    /**
     * Check if IP is currently rate limited
     *
     * @param string $action The action to check
     * @return bool True if rate limited, false otherwise
     */
    public function isRateLimited(string $action): bool
    {
        // Check both burst and hourly without incrementing
        $limit = $this->getLimit($action);
 
        // Check burst
        if (isset($limit['burst_count'])) {
            $burst_key = $this->getCacheKey($action) . '_burst';
            $burst_data = get_transient($burst_key);
 
            if ($burst_data) {
                $elapsed = time() - $burst_data['first_attempt'];
                if ($elapsed < $limit['burst_window'] && $burst_data['count'] >= $limit['burst_count']) {
                    return true;
                }
            }
        }
 
        // Check hourly
        $key = $this->getCacheKey($action);
        $data = get_transient($key);
 
        if ($data) {
            $elapsed = time() - $data['first_attempt'];
            if ($elapsed < $limit['window'] && $data['count'] >= $limit['count']) {
                return true;
            }
        }
 
        return false;
    }
}