diff --git a/src/modules/Antispam/Api/Admin.php b/src/modules/Antispam/Api/Admin.php index d9d206f8b..816c161b4 100644 --- a/src/modules/Antispam/Api/Admin.php +++ b/src/modules/Antispam/Api/Admin.php @@ -27,6 +27,7 @@ class Admin extends \Api_Abstract 'captcha_provider' => $config['captcha_provider'] ?? 'recaptcha_v2', 'captcha_recaptcha_publickey' => $config['captcha_recaptcha_publickey'] ?? null, 'captcha_recaptcha_privatekey' => $config['captcha_recaptcha_privatekey'] ?? null, + 'captcha_recaptcha_v3_threshold' => $config['captcha_recaptcha_v3_threshold'] ?? 0.5, 'turnstile_site_key' => $config['turnstile_site_key'] ?? null, 'turnstile_secret_key' => $config['turnstile_secret_key'] ?? null, 'hcaptcha_site_key' => $config['hcaptcha_site_key'] ?? null, diff --git a/src/modules/Antispam/Api/Guest.php b/src/modules/Antispam/Api/Guest.php index 4c88c7fac..8f5acb8eb 100644 --- a/src/modules/Antispam/Api/Guest.php +++ b/src/modules/Antispam/Api/Guest.php @@ -26,6 +26,7 @@ class Guest extends \Api_Abstract 'enabled' => $config['captcha_enabled'] ?? false, 'version' => $config['captcha_version'] ?? null, 'captcha_provider' => $config['captcha_provider'] ?? 'recaptcha_v2', + 'recaptcha_v3_threshold' => $config['captcha_recaptcha_v3_threshold'] ?? 0.5, 'turnstile_site_key' => $config['turnstile_site_key'] ?? null, 'hcaptcha_site_key' => $config['hcaptcha_site_key'] ?? null, ]; diff --git a/src/modules/Antispam/Service.php b/src/modules/Antispam/Service.php index 320c22201..97f9510de 100644 --- a/src/modules/Antispam/Service.php +++ b/src/modules/Antispam/Service.php @@ -154,7 +154,7 @@ class Service implements InjectionAwareInterface if (isset($config['captcha_enabled']) && $config['captcha_enabled']) { $provider = $config['captcha_provider'] ?? 'recaptcha_v2'; - if ($provider === 'recaptcha_v2') { + if ($provider === 'recaptcha_v2' || $provider === 'recaptcha_v3') { if (!isset($config['captcha_recaptcha_privatekey']) || $config['captcha_recaptcha_privatekey'] == '') { throw new \FOSSBilling\InformationException("To use reCAPTCHA you must get an API key from here"); } @@ -176,6 +176,26 @@ class Service implements InjectionAwareInterface if (!isset($content['success']) || $content['success'] !== true) { throw new \FOSSBilling\InformationException('reCAPTCHA verification failed.'); } + + if ($provider === 'recaptcha_v3') { +$threshold = (float) ($config['captcha_recaptcha_v3_threshold'] ?? 0.5); +if (!is_finite($threshold)) { + $threshold = 0.5; +} +$threshold = min(1.0, max(0.0, $threshold)); +$score = isset($content['score']) ? (float) $content['score'] : 0.0; + + if ($score < $threshold) { + throw new \FOSSBilling\InformationException('reCAPTCHA verification failed.'); + } + +$expectedAction = 'fossbilling_submit'; +$action = $params['g-recaptcha-action'] ?? $expectedAction; + +if ($action !== $expectedAction || ($content['action'] ?? null) !== $expectedAction) { + throw new \FOSSBilling\InformationException('reCAPTCHA verification failed.'); +} + } } elseif ($provider === 'turnstile') { if (empty($config['turnstile_secret_key'])) { throw new \FOSSBilling\InformationException('Cloudflare Turnstile secret key is not configured.'); diff --git a/src/modules/Antispam/templates/admin/mod_antispam_settings.html.twig b/src/modules/Antispam/templates/admin/mod_antispam_settings.html.twig index 1e8c9140c..b322381b4 100644 --- a/src/modules/Antispam/templates/admin/mod_antispam_settings.html.twig +++ b/src/modules/Antispam/templates/admin/mod_antispam_settings.html.twig @@ -78,6 +78,7 @@ @@ -87,13 +88,18 @@
- +
+
+ + + {{ 'Google returns a score from 0.0 to 1.0. Higher thresholds are stricter.'|trans }} +
@@ -198,7 +204,8 @@ document.addEventListener('DOMContentLoaded', function () { function toggleProviderSettings() { const selectedProvider = document.getElementById('provider-select').value; - document.getElementById('recaptcha_v2_settings').style.display = selectedProvider === 'recaptcha_v2' ? 'block' : 'none'; + document.getElementById('recaptcha_v2_settings').style.display = selectedProvider === 'recaptcha_v2' || selectedProvider === 'recaptcha_v3' ? 'block' : 'none'; + document.getElementById('recaptcha_v3_threshold_wrap').style.display = selectedProvider === 'recaptcha_v3' ? 'block' : 'none'; document.getElementById('turnstile_settings').style.display = selectedProvider === 'turnstile' ? 'block' : 'none'; document.getElementById('hcaptcha_settings').style.display = selectedProvider === 'hcaptcha' ? 'block' : 'none'; } diff --git a/src/modules/Antispam/templates/client/mod_antispam_recaptcha.html.twig b/src/modules/Antispam/templates/client/mod_antispam_recaptcha.html.twig index 2271dd6de..624168281 100644 --- a/src/modules/Antispam/templates/client/mod_antispam_recaptcha.html.twig +++ b/src/modules/Antispam/templates/client/mod_antispam_recaptcha.html.twig @@ -12,6 +12,50 @@
{% endif %} +{% elseif provider == 'recaptcha_v3' %} +{% if rc.publickey %} + + + + +{% endif %} {% elseif provider == 'hcaptcha' %} {% if rc.hcaptcha_site_key %} @@ -19,4 +63,4 @@ {% endif %} {% endif %} {% endif %} -{% endif %} \ No newline at end of file +{% endif %} diff --git a/src/modules/Embed/templates/client/mod_embed_contact.html.twig b/src/modules/Embed/templates/client/mod_embed_contact.html.twig index 1dd29fe96..05dc98922 100644 --- a/src/modules/Embed/templates/client/mod_embed_contact.html.twig +++ b/src/modules/Embed/templates/client/mod_embed_contact.html.twig @@ -68,45 +68,7 @@ -{% if guest.extension_is_on({ "mod": "antispam" }) %} -{% set rc = guest.antispam_recaptcha %} - {% if rc.enabled %} - {% if rc.version == 2 %} - -
- {% else %} - {% set server = "https://www.google.com/recaptcha/api" %} - - - - - - - - {% endif %} - {% endif %} - {% endif %} + {{ include('mod_antispam_recaptcha.html.twig') }} diff --git a/src/themes/admin_default/html/macro_functions.html.twig b/src/themes/admin_default/html/macro_functions.html.twig index 09f7bc9ba..d74405409 100644 --- a/src/themes/admin_default/html/macro_functions.html.twig +++ b/src/themes/admin_default/html/macro_functions.html.twig @@ -161,6 +161,50 @@
{% endif %} + {% elseif provider == 'recaptcha_v3' %} + {% if config.publickey %} + + + + + {% endif %} {% elseif provider == 'hcaptcha' %} {% if config.hcaptcha_site_key %}
diff --git a/src/themes/huraga/html/macro_functions.html.twig b/src/themes/huraga/html/macro_functions.html.twig index f49b65d57..f15bf13c6 100644 --- a/src/themes/huraga/html/macro_functions.html.twig +++ b/src/themes/huraga/html/macro_functions.html.twig @@ -27,6 +27,50 @@
{% endif %} + {% elseif provider == 'recaptcha_v3' %} + {% if config.publickey %} + + + + + {% endif %} {% elseif provider == 'hcaptcha' %} {% if config.hcaptcha_site_key %}
diff --git a/tests-legacy/modules/Antispam/Api/GuestTest.php b/tests-legacy/modules/Antispam/Api/GuestTest.php index 2cc317c7c..3516b41c3 100644 --- a/tests-legacy/modules/Antispam/Api/GuestTest.php +++ b/tests-legacy/modules/Antispam/Api/GuestTest.php @@ -30,6 +30,7 @@ final class GuestTest extends \BBTestCase 'enabled' => true, 'version' => null, 'captcha_provider' => 'recaptcha_v2', + 'recaptcha_v3_threshold' => 0.5, 'turnstile_site_key' => null, 'hcaptcha_site_key' => null, ], @@ -43,6 +44,7 @@ final class GuestTest extends \BBTestCase 'enabled' => true, 'version' => null, 'captcha_provider' => 'recaptcha_v2', + 'recaptcha_v3_threshold' => 0.5, 'turnstile_site_key' => null, 'hcaptcha_site_key' => null, ], @@ -58,6 +60,7 @@ final class GuestTest extends \BBTestCase 'enabled' => false, 'version' => 2, 'captcha_provider' => 'recaptcha_v2', + 'recaptcha_v3_threshold' => 0.5, 'turnstile_site_key' => null, 'hcaptcha_site_key' => null, ], @@ -71,6 +74,7 @@ final class GuestTest extends \BBTestCase 'enabled' => false, 'version' => null, 'captcha_provider' => 'recaptcha_v2', + 'recaptcha_v3_threshold' => 0.5, 'turnstile_site_key' => null, 'hcaptcha_site_key' => null, ], @@ -86,6 +90,7 @@ final class GuestTest extends \BBTestCase 'enabled' => true, 'version' => null, 'captcha_provider' => 'turnstile', + 'recaptcha_v3_threshold' => 0.5, 'turnstile_site_key' => 'abc', 'hcaptcha_site_key' => null, ], @@ -101,10 +106,28 @@ final class GuestTest extends \BBTestCase 'enabled' => true, 'version' => null, 'captcha_provider' => 'hcaptcha', + 'recaptcha_v3_threshold' => 0.5, 'turnstile_site_key' => null, 'hcaptcha_site_key' => 'abc', ], ], + [ + [ + 'captcha_enabled' => true, + 'captcha_provider' => 'recaptcha_v3', + 'captcha_recaptcha_publickey' => 'abc', + 'captcha_recaptcha_v3_threshold' => '0.7', + ], + [ + 'publickey' => 'abc', + 'enabled' => true, + 'version' => null, + 'captcha_provider' => 'recaptcha_v3', + 'recaptcha_v3_threshold' => '0.7', + 'turnstile_site_key' => null, + 'hcaptcha_site_key' => null, + ], + ], ]; } diff --git a/tests/Modules/Antispam/GuestTest.php b/tests/Modules/Antispam/GuestTest.php index 1053bdbd2..33a453ba6 100644 --- a/tests/Modules/Antispam/GuestTest.php +++ b/tests/Modules/Antispam/GuestTest.php @@ -35,6 +35,7 @@ final class GuestTest extends TestCase 'captcha_provider' => 'hcaptcha', 'hcaptcha_site_key' => 'site-key', 'hcaptcha_secret_key' => 'secret-key', + 'captcha_recaptcha_v3_threshold' => '0.7', 'check_temp_emails' => false, 'sfs' => false, ]); @@ -49,6 +50,7 @@ final class GuestTest extends TestCase $this->assertSame('hcaptcha', $config['captcha_provider']); $this->assertSame('site-key', $config['hcaptcha_site_key']); $this->assertSame('secret-key', $config['hcaptcha_secret_key']); + $this->assertSame('0.7', $config['captcha_recaptcha_v3_threshold']); $this->assertFalse((bool) $config['check_temp_emails']); $this->assertFalse((bool) $config['sfs']); } @@ -60,8 +62,9 @@ final class GuestTest extends TestCase $result = Request::makeRequest('admin/extension/config_save', [ 'ext' => 'mod_antispam', 'captcha_enabled' => true, - 'captcha_provider' => 'turnstile', - 'turnstile_site_key' => 'turnstile-site-key', + 'captcha_provider' => 'recaptcha_v3', + 'captcha_recaptcha_publickey' => 'recaptcha-site-key', + 'captcha_recaptcha_v3_threshold' => '0.8', ]); $this->assertTrue($result->wasSuccessful(), $result->generatePHPUnitMessage()); @@ -71,9 +74,9 @@ final class GuestTest extends TestCase $config = $result->getResult(); $this->assertIsArray($config); $this->assertTrue((bool) $config['enabled']); - $this->assertSame('turnstile', $config['captcha_provider']); - $this->assertSame('turnstile-site-key', $config['turnstile_site_key']); - $this->assertNull($config['publickey']); + $this->assertSame('recaptcha_v3', $config['captcha_provider']); + $this->assertSame('recaptcha-site-key', $config['publickey']); + $this->assertSame('0.8', $config['recaptcha_v3_threshold']); } private function captureOriginalConfig(): void