Skip to content

Commit f540d66

Browse files
authored
Correct rate limiter client IP guidance (#8361)
Refs cakephp/cakephp#19565, cakephp/cakephp#19567.
1 parent 7f43200 commit f540d66

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

‎docs/en/controllers/middleware/rate-limit.md‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ The middleware accepts the following configuration options:
6363
- **headers** - Whether to include rate limit headers in responses (default: true)
6464
- **includeRetryAfter** - Whether to include Retry-After header in 429 responses (default: true)
6565
- **message** - Custom error message for rate limit exceeded (default: 'Rate limit exceeded. Please try again later.')
66-
- **ipHeader** - Header name(s) to check for client IP when behind a proxy (default: 'x-forwarded-for')
66+
- **ipHeader** - Header name(s) to check for client IP, or `remote_addr` to
67+
use the connecting IP address (default: `remote_addr`).
6768
- **tokenHeaders** - Array of headers to check for API tokens (default: `['Authorization', 'X-API-Key']`)
6869
- **skipCheck** - Callback to determine if a request should skip rate limiting
6970
- **costCallback** - Callback to determine the cost of a request
@@ -89,8 +90,9 @@ new RateLimitMiddleware([
8990
])
9091
```
9192

92-
The middleware automatically handles proxy headers. You can configure
93-
which headers to check using the `ipHeader` option:
93+
By default, the middleware uses `REMOTE_ADDR` and ignores forwarded headers.
94+
When a proxy you control overwrites client-supplied forwarding headers, you can
95+
opt in to those headers with the `ipHeader` option:
9496

9597
```php
9698
new RateLimitMiddleware([
@@ -99,6 +101,15 @@ new RateLimitMiddleware([
99101
])
100102
```
101103

104+
For a comma-separated header such as `X-Forwarded-For`, the leftmost address
105+
is used. Header names in an array are checked in order; if none has a value,
106+
the middleware falls back to `REMOTE_ADDR`.
107+
108+
> [!WARNING]
109+
> Only enable forwarded headers when a trusted proxy overwrites them and clients
110+
> cannot bypass that proxy. Otherwise, clients can spoof their IP address and
111+
> bypass rate limits.
112+
102113
### User-based
103114

104115
Track requests per authenticated user:

0 commit comments

Comments
 (0)