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
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use StreamGoals\Core\CidrMatcher;
|
||||
use StreamGoals\Core\CloudflareRanges;
|
||||
|
||||
final class CidrMatcherTest extends TestCase {
|
||||
|
||||
public function test_ipv4_boundaries(): void {
|
||||
$r = array( '173.245.48.0/20' ); // 173.245.48.0 - 173.245.63.255
|
||||
$this->assertTrue( CidrMatcher::contains( '173.245.48.0', $r ) );
|
||||
$this->assertTrue( CidrMatcher::contains( '173.245.63.255', $r ) );
|
||||
$this->assertFalse( CidrMatcher::contains( '173.245.47.255', $r ) );
|
||||
$this->assertFalse( CidrMatcher::contains( '173.245.64.0', $r ) );
|
||||
}
|
||||
|
||||
public function test_non_byte_aligned_and_wide_masks(): void {
|
||||
$this->assertTrue( CidrMatcher::contains( '104.27.255.255', array( '104.24.0.0/14' ) ) ); // /14 spans 104.24 - 104.27
|
||||
$this->assertFalse( CidrMatcher::contains( '104.28.0.0', array( '104.24.0.0/14' ) ) );
|
||||
$this->assertFalse( CidrMatcher::contains( '104.23.255.255', array( '104.24.0.0/14' ) ) );
|
||||
$this->assertTrue( CidrMatcher::contains( '8.8.8.8', array( '0.0.0.0/0' ) ) );
|
||||
$this->assertTrue( CidrMatcher::contains( '1.2.3.4', array( '1.2.3.4/32' ) ) );
|
||||
$this->assertFalse( CidrMatcher::contains( '1.2.3.5', array( '1.2.3.4/32' ) ) );
|
||||
}
|
||||
|
||||
public function test_ipv6(): void {
|
||||
$this->assertTrue( CidrMatcher::contains( '2606:4700::1', array( '2606:4700::/32' ) ) );
|
||||
$this->assertTrue( CidrMatcher::contains( '2a06:98c7:ffff::1', array( '2a06:98c0::/29' ) ) );
|
||||
$this->assertFalse( CidrMatcher::contains( '2a06:98c8::1', array( '2a06:98c0::/29' ) ) );
|
||||
$this->assertFalse( CidrMatcher::contains( '2606:4701::1', array( '2606:4700::/32' ) ) );
|
||||
}
|
||||
|
||||
public function test_families_never_cross_match_and_bad_input_is_false(): void {
|
||||
$this->assertFalse( CidrMatcher::contains( '1.2.3.4', array( '2606:4700::/32' ) ) );
|
||||
$this->assertFalse( CidrMatcher::contains( '2606:4700::1', array( '173.245.48.0/20' ) ) );
|
||||
foreach ( array( 'garbage', '', '999.1.1.1', "1.2.3.4\nX: y", '1.2.3.4, 5.6.7.8' ) as $bad ) {
|
||||
$this->assertFalse( CidrMatcher::contains( $bad, array( '0.0.0.0/0' ) ), "matched bad ip: $bad" );
|
||||
}
|
||||
foreach ( array( '1.2.3.4', '1.2.3.4/x', '1.2.3.4/33', '1.2.3.4/-1', 'nope/8', '' ) as $badCidr ) {
|
||||
$this->assertFalse( CidrMatcher::contains( '1.2.3.4', array( $badCidr ) ), "matched bad cidr: $badCidr" );
|
||||
}
|
||||
}
|
||||
|
||||
public function test_surrounding_whitespace_is_tolerated(): void {
|
||||
$this->assertTrue( CidrMatcher::contains( ' 173.245.48.1 ', array( '173.245.48.0/20' ) ) );
|
||||
}
|
||||
|
||||
public function test_cloudflare_ranges(): void {
|
||||
$this->assertTrue( CloudflareRanges::isCloudflare( '172.70.1.1' ) );
|
||||
$this->assertTrue( CloudflareRanges::isCloudflare( '104.16.0.1' ) );
|
||||
$this->assertTrue( CloudflareRanges::isCloudflare( '2606:4700:3030::1' ) );
|
||||
$this->assertFalse( CloudflareRanges::isCloudflare( '203.0.113.9' ) );
|
||||
$this->assertFalse( CloudflareRanges::isCloudflare( '47.151.185.180' ) );
|
||||
$this->assertTrue( CloudflareRanges::isCloudflare( '203.0.113.9', array( '203.0.113.0/24' ) ), 'filter-supplied ranges extend the list' );
|
||||
}
|
||||
}
|
||||
@@ -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