diff --git a/includes/Admin/SettingsPage.php b/includes/Admin/SettingsPage.php
index 113ab00..a7953ae 100644
--- a/includes/Admin/SettingsPage.php
+++ b/includes/Admin/SettingsPage.php
@@ -56,7 +56,7 @@ final class SettingsPage {
foreach ( \StreamGoals\Core\ClientIp::MODES as $k => $l ) {
echo '' . esc_html( $l ) . ' ';
}
- echo '
Donation forms limit how fast one visitor can start payments (to stop abuse). If your site is behind Cloudflare or another proxy, choose it here so each visitor is counted separately; otherwise everyone looks like the same visitor and a busy moment, like a raid, could block donations. Only choose a proxy option if all traffic to your site really goes through it.
';
+ echo 'Donation forms limit how fast one visitor can start payments (to stop abuse). Behind a proxy, every visitor can look like the same address, and a busy moment, like a raid, could block donations. Automatic recognises Cloudflare by its published addresses and counts each visitor separately; it ignores forged headers from anywhere else. For another kind of proxy, choose its option here, but only if all traffic to your site really goes through it.
';
self::ipDiagnostics( (string) $s['visitor_ip_header'] );
echo '';
@@ -80,7 +80,7 @@ final class SettingsPage {
/** Shows the owner what their server passes to the plugin right now, to help pick the right option. */
private static function ipDiagnostics( string $mode ): void {
- $keys = array( 'REMOTE_ADDR' => 'Connection address', 'HTTP_CF_CONNECTING_IP' => 'CF-Connecting-IP', 'HTTP_X_REAL_IP' => 'X-Real-IP', 'HTTP_X_FORWARDED_FOR' => 'X-Forwarded-For' );
+ $keys = array( 'REMOTE_ADDR' => 'Connection address', 'HTTP_CF_CONNECTING_IP' => 'CF-Connecting-IP', 'HTTP_CF_RAY' => 'CF-Ray', 'HTTP_X_REAL_IP' => 'X-Real-IP', 'HTTP_X_FORWARDED_FOR' => 'X-Forwarded-For' );
$server = array();
foreach ( $keys as $k => $_ ) {
if ( isset( $_SERVER[ $k ] ) && is_string( $_SERVER[ $k ] ) ) {
@@ -91,8 +91,10 @@ final class SettingsPage {
foreach ( $keys as $k => $label ) {
echo '' . esc_html( $label ) . ' ' . ( isset( $server[ $k ] ) ? esc_html( $server[ $k ] ) : '(not sent)' ) . ' ';
}
+ $viaCf = isset( $server['REMOTE_ADDR'] ) && \StreamGoals\Core\CloudflareRanges::isCloudflare( $server['REMOTE_ADDR'], (array) apply_filters( 'streamgoals_cloudflare_ranges', array() ) );
+ echo 'Is this connection from Cloudflare? ' . ( $viaCf ? 'Yes - the connection comes from a Cloudflare address' : 'No' ) . ' ';
echo 'Visitor address used with the saved option ' . esc_html( \StreamGoals\Core\ClientIp::resolve( $server, $mode ) ) . ' ';
- echo 'This is your own visit. If the connection address belongs to your proxy or CDN (not to you) and your proxy\'s header shows your real address, choose that proxy option above and save. Then the "used" row should show your real address.
';
+ echo '' . ( ! $viaCf && ( isset( $server['HTTP_CF_RAY'] ) || isset( $server['HTTP_CF_CONNECTING_IP'] ) ) ? 'Cloudflare headers were sent, but the connection is not from a known Cloudflare address, so "Automatic" ignores them. If your site really is behind Cloudflare, choose "Cloudflare" above (or update the plugin, as Cloudflare may have added new addresses).
' : '' ) . 'This is your own visit. If the connection address belongs to your proxy or CDN (not to you) and your proxy\'s header shows your real address, choose that proxy option above and save. Then the "used" row should show your real address.
';
}
public static function handleSave(): void {
diff --git a/includes/Core/CidrMatcher.php b/includes/Core/CidrMatcher.php
new file mode 100644
index 0000000..aba076f
--- /dev/null
+++ b/includes/Core/CidrMatcher.php
@@ -0,0 +1,47 @@
+ strlen( $netBin ) * 8 ) {
+ return false;
+ }
+ $fullBytes = intdiv( $bits, 8 );
+ if ( $fullBytes > 0 && substr( $ipBin, 0, $fullBytes ) !== substr( $netBin, 0, $fullBytes ) ) {
+ return false;
+ }
+ $rest = $bits % 8;
+ if ( 0 === $rest ) {
+ return true;
+ }
+ $mask = ( 0xFF << ( 8 - $rest ) ) & 0xFF;
+ return ( ord( $ipBin[ $fullBytes ] ) & $mask ) === ( ord( $netBin[ $fullBytes ] ) & $mask );
+ }
+}
diff --git a/includes/Core/ClientIp.php b/includes/Core/ClientIp.php
index 7767ef8..606c716 100644
--- a/includes/Core/ClientIp.php
+++ b/includes/Core/ClientIp.php
@@ -12,12 +12,14 @@ namespace StreamGoals\Core;
*/
final class ClientIp {
+ public const MODE_AUTO = 'auto'; // detect Cloudflare automatically (recommended)
public const MODE_DIRECT = ''; // no proxy: use REMOTE_ADDR
public const MODE_CLOUDFLARE = 'cloudflare'; // CF-Connecting-IP
public const MODE_REAL_IP = 'real_ip'; // X-Real-IP (nginx and many hosts)
public const MODE_FORWARDED = 'forwarded'; // last address in X-Forwarded-For (exactly one proxy in front)
public const MODES = array(
+ self::MODE_AUTO => 'Automatic (recommended): detects Cloudflare',
self::MODE_DIRECT => 'Not behind a proxy or CDN (most sites)',
self::MODE_CLOUDFLARE => 'Cloudflare (CF-Connecting-IP)',
self::MODE_REAL_IP => 'X-Real-IP header',
@@ -25,10 +27,16 @@ final class ClientIp {
);
/** @param array $server $_SERVER-style values (already unslashed). */
- public static function resolve( array $server, string $mode = self::MODE_DIRECT ): string {
+ public static function resolve( array $server, string $mode = self::MODE_DIRECT, ?array $extraCloudflareRanges = null ): string {
$remote = self::valid( $server['REMOTE_ADDR'] ?? null );
$fromHeader = null;
switch ( $mode ) {
+ case self::MODE_AUTO:
+ // Trust CF-Connecting-IP only when the connection itself comes from a Cloudflare address.
+ if ( null !== $remote && CloudflareRanges::isCloudflare( $remote, $extraCloudflareRanges ) ) {
+ $fromHeader = self::valid( $server['HTTP_CF_CONNECTING_IP'] ?? null );
+ }
+ break;
case self::MODE_CLOUDFLARE:
$fromHeader = self::valid( $server['HTTP_CF_CONNECTING_IP'] ?? null );
break;
diff --git a/includes/Core/CloudflareRanges.php b/includes/Core/CloudflareRanges.php
new file mode 100644
index 0000000..8267469
--- /dev/null
+++ b/includes/Core/CloudflareRanges.php
@@ -0,0 +1,25 @@
+ 200,
'min_donation_minor' => 100,
'overlay_token' => '',
- 'visitor_ip_header' => '', // '' | cloudflare | real_ip | forwarded (see Core\ClientIp)
+ 'visitor_ip_header' => 'auto', // auto | '' (direct) | cloudflare | real_ip | forwarded (see Core\ClientIp)
'funding_paylater' => false, // show PayPal "Pay Later" button
'update_token' => '', // optional Gitea read token (only while the update repo is private)
'funding_card' => true, // show the Debit/Credit Card button
diff --git a/includes/Rest/DonateController.php b/includes/Rest/DonateController.php
index d831878..6718838 100644
--- a/includes/Rest/DonateController.php
+++ b/includes/Rest/DonateController.php
@@ -284,7 +284,7 @@ final class DonateController {
$server[ $k ] = sanitize_text_field( wp_unslash( $_SERVER[ $k ] ) );
}
}
- $ip = \StreamGoals\Core\ClientIp::resolve( $server, (string) \StreamGoals\Core\Settings::get( 'visitor_ip_header', '' ) );
+ $ip = \StreamGoals\Core\ClientIp::resolve( $server, (string) \StreamGoals\Core\Settings::get( 'visitor_ip_header', 'auto' ), (array) apply_filters( 'streamgoals_cloudflare_ranges', array() ) );
if ( ( new RateLimiter( new WpCache() ) )->allow( $bucket . '|' . $ip, $max, $window ) ) {
return null;
}
diff --git a/tests/Unit/CidrMatcherTest.php b/tests/Unit/CidrMatcherTest.php
new file mode 100644
index 0000000..2826a2b
--- /dev/null
+++ b/tests/Unit/CidrMatcherTest.php
@@ -0,0 +1,59 @@
+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' );
+ }
+}
diff --git a/tests/Unit/ClientIpTest.php b/tests/Unit/ClientIpTest.php
index 6721e29..5a23577 100644
--- a/tests/Unit/ClientIpTest.php
+++ b/tests/Unit/ClientIpTest.php
@@ -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' ) ) );
+ }
}
+