Automatic Cloudflare detection for rate limiting (trusts CF-Connecting-IP only from Cloudflare ranges)
- Core\CidrMatcher (IPv4/IPv6), Core\CloudflareRanges (published ranges, filterable) - ClientIp 'auto' mode, default for new installs; diagnostics show whether the connection is from Cloudflare Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5ea1e1d273
commit
43106d9edc
8 files changed
+171
-6
No files matched your search
@@ -56,4 +56,28 @@ final class ClientIpTest extends TestCase {
|
||||
$srv = array( 'REMOTE_ADDR' => self::CLIENT, 'HTTP_CF_CONNECTING_IP' => '1.2.3.4' );
|
||||
$this->assertSame( self::CLIENT, ClientIp::resolve( $srv, 'something_else' ) );
|
||||
}
|
||||
|
||||
public function test_auto_mode_trusts_cf_header_only_from_a_cloudflare_connection(): void {
|
||||
$srv = array( 'REMOTE_ADDR' => '172.70.1.1', 'HTTP_CF_CONNECTING_IP' => self::CLIENT );
|
||||
$this->assertSame( self::CLIENT, ClientIp::resolve( $srv, ClientIp::MODE_AUTO ) );
|
||||
$v6 = array( 'REMOTE_ADDR' => '2606:4700:3030::1', 'HTTP_CF_CONNECTING_IP' => self::CLIENT );
|
||||
$this->assertSame( self::CLIENT, ClientIp::resolve( $v6, ClientIp::MODE_AUTO ) );
|
||||
}
|
||||
|
||||
public function test_auto_mode_ignores_a_forged_header_from_a_non_cloudflare_connection(): void {
|
||||
$srv = array( 'REMOTE_ADDR' => '47.151.185.180', 'HTTP_CF_CONNECTING_IP' => '6.6.6.6', 'HTTP_X_FORWARDED_FOR' => '7.7.7.7' );
|
||||
$this->assertSame( '47.151.185.180', ClientIp::resolve( $srv, ClientIp::MODE_AUTO ) );
|
||||
}
|
||||
|
||||
public function test_auto_mode_falls_back_when_header_missing_or_invalid(): void {
|
||||
$this->assertSame( '172.70.1.1', ClientIp::resolve( array( 'REMOTE_ADDR' => '172.70.1.1' ), ClientIp::MODE_AUTO ) );
|
||||
$this->assertSame( '172.70.1.1', ClientIp::resolve( array( 'REMOTE_ADDR' => '172.70.1.1', 'HTTP_CF_CONNECTING_IP' => 'nope' ), ClientIp::MODE_AUTO ) );
|
||||
}
|
||||
|
||||
public function test_auto_mode_extra_ranges_from_the_filter(): void {
|
||||
$srv = array( 'REMOTE_ADDR' => '198.18.0.5', 'HTTP_CF_CONNECTING_IP' => self::CLIENT );
|
||||
$this->assertSame( '198.18.0.5', ClientIp::resolve( $srv, ClientIp::MODE_AUTO ) );
|
||||
$this->assertSame( self::CLIENT, ClientIp::resolve( $srv, ClientIp::MODE_AUTO, array( '198.18.0.0/15' ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user