Update SpamChecker to be able to use Cloudflare Turnstile and hCaptcha (#3004)

* Do not include local dev database, which is used for debugging

* Revert of ceddc35aa7

* Add turnstile to config and a selector of providers

* Add turnstile's logic

* add turnstile variables to the API

* add turnstile to the macro and html_client

* Add hCaptcha

* Update src/modules/Spamchecker/Service.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update src/modules/Spamchecker/Service.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update src/modules/Spamchecker/Service.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update src/modules/Spamchecker/html_admin/mod_spamchecker_settings.html.twig

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fix bugs

* Fix space consistency in Guest.php

* Apply suggestion from @Copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fix macros

* Add link to hcaptcha's website

* Add and fix unit tests for the Spamchecker

* Fix cspell

* Reverse the "recaptcha_private/publickey" to "captcha_recaptcha_private/publickey"

* Apply suggestion from @Copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Apply suggestion from @Copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update cspell.json with new terms and ignore paths

* Update src/modules/Spamchecker/html_client/mod_spamchecker_recaptcha.html.twig

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update tests-legacy/modules/Spamchecker/Api/GuestTest.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update tests-legacy/modules/Spamchecker/Api/GuestTest.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update tests-legacy/modules/Spamchecker/Api/GuestTest.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update tests-legacy/modules/Spamchecker/Api/GuestTest.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update tests-legacy/modules/Spamchecker/Api/GuestTest.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update tests-legacy/modules/Spamchecker/Api/GuestTest.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update src/modules/Spamchecker/Service.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fix array formatting in GuestTest

* Update tests-legacy/modules/Spamchecker/Api/GuestTest.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update src/modules/Spamchecker/Service.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update src/modules/Spamchecker/Service.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update src/modules/Spamchecker/html_admin/mod_spamchecker_settings.html.twig

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update src/modules/Spamchecker/Service.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update src/modules/Spamchecker/Service.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update src/modules/Spamchecker/Service.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Apply suggestion from @Copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Rewrite toggleProviderSettings in vanilla JS

* Remove useless comment

* Update src/modules/Spamchecker/html_admin/mod_spamchecker_settings.html.twig

Co-authored-by: Adam Daley <admdly@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Adam Daley <admdly@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2025-11-06 17:50:14 +00:00
1 parent f9c1be6c88
commit e85b1eeff9
8 files changed
+233 -47

No files matched your search

+3
View File
@@ -28,6 +28,9 @@ class Guest extends \Api_Abstract
'publickey' => $config['captcha_recaptcha_publickey'] ?? null,
'enabled' => $config['captcha_enabled'] ?? false,
'version' => $config['captcha_version'] ?? null,
'captcha_provider' => $config['captcha_provider'] ?? 'recaptcha_v2',
'turnstile_site_key' => $config['turnstile_site_key'] ?? null,
'hcaptcha_site_key' => $config['hcaptcha_site_key'] ?? null,
];
}
}
+51 -5
View File
@@ -87,7 +87,9 @@ class Service implements InjectionAwareInterface
$config = $di['mod_config']('Spamchecker');
if (isset($config['captcha_enabled']) && $config['captcha_enabled']) {
if (isset($config['captcha_version']) && $config['captcha_version'] == 2) {
$provider = $config['captcha_provider'] ?? 'recaptcha_v2';
if ($provider === 'recaptcha_v2') {
if (!isset($config['captcha_recaptcha_privatekey']) || $config['captcha_recaptcha_privatekey'] == '') {
throw new \FOSSBilling\InformationException("To use reCAPTCHA you must get an API key from <a href='https://www.google.com/recaptcha/admin/create'>here</a>");
}
@@ -106,14 +108,58 @@ class Service implements InjectionAwareInterface
]);
$content = $response->toArray();
if (!$content['success']) {
if (!isset($content['success']) || $content['success'] !== true) {
throw new \FOSSBilling\InformationException('reCAPTCHA verification failed.');
}
} else {
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.');
}
$turnstile_response = $params['cf-turnstile-response'] ?? null;
if (empty($turnstile_response)) {
throw new \FOSSBilling\InformationException('Please complete the CAPTCHA verification.');
}
$client = HttpClient::create(['bindto' => BIND_TO]);
$response = $client->request('POST', 'https://challenges.cloudflare.com/turnstile/v0/siteverify', [
'body' => [
'secret' => $config['turnstile_secret_key'],
'response' => $turnstile_response,
'remoteip' => $di['request']->getClientIp(),
],
]);
$content = $response->toArray();
if (!isset($content['success']) || $content['success'] !== true) {
throw new \FOSSBilling\InformationException('CAPTCHA verification failed. Please try again.');
}
} elseif ($provider === 'hcaptcha') {
if (empty($config['hcaptcha_secret_key'])) {
throw new \FOSSBilling\InformationException('hCaptcha secret key is not configured.');
}
$hcaptcha_response = $params['h-captcha-response'] ?? null;
if (empty($hcaptcha_response)) {
throw new \FOSSBilling\InformationException('Please complete the CAPTCHA verification.');
}
$client = HttpClient::create(['bindto' => BIND_TO]);
$response = $client->request('POST', 'https://hcaptcha.com/siteverify', [
'body' => [
'secret' => $config['hcaptcha_secret_key'],
'response' => $hcaptcha_response,
'remoteip' => $di['request']->getClientIp(),
],
]);
$content = $response->toArray();
if (!isset($content['success']) || $content['success'] !== true) {
throw new \FOSSBilling\InformationException('CAPTCHA verification failed. Please try again.');
}
}
}
if (isset($config['sfs']) && $config['sfs']) {
$spamCheckerService = $di['mod_service']('Spamchecker');
$spamCheckerService->isInStopForumSpamDatabase($data);
@@ -51,14 +51,13 @@
</div>
<div class="card-body">
<h3 class="card-title">{{ 'reCAPTCHA settings'|trans }}</h3>
<h3 class="card-title">{{ 'CAPTCHA settings'|trans }}</h3>
<p class="card-subtitle">
{{ 'Enable captcha for client registration and contact forms'|trans }}<br>
{{ 'Sign up for reCAPTCHA key for your domain at'|trans }}
<a href="https://www.google.com/recaptcha/about/" target="_blank">Google reCAPTCHA</a>
{{ 'Get keys from %recaptcha%, %turnstile% or %hcaptcha%.'|trans({ '%recaptcha%': '<a href="https://www.google.com/recaptcha/about/" target="_blank">Google reCAPTCHA</a>', '%turnstile%': '<a href="https://www.cloudflare.com/products/turnstile/" target="_blank">Cloudflare Turnstile</a>', '%hcaptcha%': '<a href="https://www.hcaptcha.com" target="_blank">hCaptcha</a>' })|raw }}
</p>
<div class="mb-3 row">
<label class="col-md-3 col-form-label" for="captcha_enabled">{{ 'Enable reCAPTCHA'|trans }}</label>
<label class="col-md-3 col-form-label" for="captcha_enabled">{{ 'Enable CAPTCHA'|trans }}</label>
<div class="col-md-6 pt-2 ps-0">
<label class="form-check form-check-single form-switch ps-0">
<input id="captcha_enabled" class="form-check-input" name="captcha_enabled" type="checkbox"
@@ -66,33 +65,62 @@
</label>
</div>
</div>
<div class="mb-3 row">
<label class="col-md-3 col-form-label">{{ 'reCAPTCHA version'|trans }}</label>
<div class="col-md-6 pt-2 ps-0">
<label class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="captcha_version" value="2"
{% if params.captcha_version == "2" %}checked="checked"{% endif %}>
<span class="form-check-label">{{ 'v2'|trans }}</span>
</label>
{# <label class="form-check form-check-inline">#}
{# <input class="form-check-input" type="radio" value="3" disabled#}
{# {% if params.captcha_version == "3" %}checked="checked"{% endif %}>#}
{# <span class="form-check-label">{{ 'v3'|trans }}</span>#}
{# </label>#}
</div>
</div>
<div class="mb-3 row">
<label class="col-md-3 col-form-label" for="captcha_recaptcha_publickey">{{ 'reCAPTCHA Public key'|trans }}</label>
<label class="col-md-3 col-form-label" for="provider-select">{{ 'CAPTCHA Provider'|trans }}</label>
<div class="col-md-6 row">
<input class="form-control" type="text" id="captcha_recaptcha_publickey" name="captcha_recaptcha_publickey"
value="{{ params.captcha_recaptcha_publickey }}">
<select name="captcha_provider" class="form-select" id="provider-select">
<option value="recaptcha_v2" {% if not params.captcha_provider or params.captcha_provider == 'recaptcha_v2' %}selected{% endif %}>{{ 'Google reCAPTCHA v2'|trans }}</option>
<option value="turnstile" {% if params.captcha_provider == 'turnstile' %}selected{% endif %}>{{ 'Cloudflare Turnstile'|trans }}</option>
<option value="hcaptcha" {% if params.captcha_provider == 'hcaptcha' %}selected{% endif %}>{{ 'hCaptcha'|trans }}</option>
</select>
</div>
</div>
<div class="mb-3 row">
<label class="col-md-3 col-form-label" for="captcha_recaptcha_privatekey">{{ 'reCAPTCHA Secret key'|trans }}</label>
<div class="col-md-6 pt-2 row">
<input class="form-control" type="text" id="captcha_recaptcha_privatekey" name="captcha_recaptcha_privatekey"
value="{{ params.captcha_recaptcha_privatekey }}">
<div id="recaptcha_v2_settings">
<div class="mb-3 row">
<label class="col-md-3 col-form-label" for="captcha_recaptcha_publickey">{{ 'reCAPTCHA Public key'|trans }}</label>
<div class="col-md-6 row">
<input class="form-control" type="text" id="captcha_recaptcha_publickey" name="captcha_recaptcha_publickey"
value="{{ params.captcha_recaptcha_publickey }}">
</div>
</div>
<div class="mb-3 row">
<label class="col-md-3 col-form-label" for="captcha_recaptcha_privatekey">{{ 'reCAPTCHA Secret key'|trans }}</label>
<div class="col-md-6 pt-2 row">
<input class="form-control" type="text" id="captcha_recaptcha_privatekey" name="captcha_recaptcha_privatekey"
value="{{ params.captcha_recaptcha_privatekey }}">
</div>
</div>
</div>
<div id="turnstile_settings">
<div class="mb-3 row">
<label class="col-md-3 col-form-label" for="turnstile_site_key">{{ 'Turnstile Site Key'|trans }}</label>
<div class="col-md-6 row">
<input class="form-control" type="text" id="turnstile_site_key" name="turnstile_site_key" value="{{ params.turnstile_site_key }}">
</div>
</div>
<div class="mb-3 row">
<label class="col-md-3 col-form-label" for="turnstile_secret_key">{{ 'Turnstile Secret Key'|trans }}</label>
<div class="col-md-6 row">
<input class="form-control" type="text" id="turnstile_secret_key" name="turnstile_secret_key" value="{{ params.turnstile_secret_key }}">
</div>
</div>
</div>
<div id="hcaptcha_settings">
<div class="mb-3 row">
<label class="col-md-3 col-form-label" for="hcaptcha_site_key">{{ 'hCaptcha Site Key'|trans }}</label>
<div class="col-md-6 row">
<input class="form-control" type="text" id="hcaptcha_site_key" name="hcaptcha_site_key" value="{{ params.hcaptcha_site_key }}">
</div>
</div>
<div class="mb-3 row">
<label class="col-md-3 col-form-label" for="hcaptcha_secret_key">{{ 'hCaptcha Secret Key'|trans }}</label>
<div class="col-md-6 row">
<input class="form-control" type="text" id="hcaptcha_secret_key" name="hcaptcha_secret_key" value="{{ params.hcaptcha_secret_key }}">
</div>
</div>
</div>
</div>
@@ -132,3 +160,35 @@
</form>
</div>
{% endblock %}
{% block js %}
<script>
document.addEventListener("DOMContentLoaded", function() {
function toggleProviderSettings() {
var selectedProvider = document.getElementById("provider-select").value;
if (selectedProvider === 'recaptcha_v2') {
document.getElementById("recaptcha_v2_settings").style.display = "block";
document.getElementById("turnstile_settings").style.display = "none";
document.getElementById("hcaptcha_settings").style.display = "none";
} else if (selectedProvider === 'turnstile') {
document.getElementById("recaptcha_v2_settings").style.display = "none";
document.getElementById("turnstile_settings").style.display = "block";
document.getElementById("hcaptcha_settings").style.display = "none";
} else if (selectedProvider === 'hcaptcha') {
document.getElementById("recaptcha_v2_settings").style.display = "none";
document.getElementById("turnstile_settings").style.display = "none";
document.getElementById("hcaptcha_settings").style.display = "block";
}
}
// Run on page load
toggleProviderSettings();
// Run when the dropdown changes
document.getElementById("provider-select").addEventListener("change", () => {
toggleProviderSettings();
});
});
</script>
{% endblock %}
@@ -1,9 +1,21 @@
{% if guest.extension_is_on({"mod":"spamchecker"}) %}
{% set rc = guest.spamchecker_recaptcha %}
{% if rc.enabled %}
{% if rc.version == 2 %}
<script src='https://www.google.com/recaptcha/api.js' async defer></script>
<div class="g-recaptcha" data-sitekey="{{ rc.publickey }}"></div>
{% endif %}
{% set provider = rc.captcha_provider|default('recaptcha_v2') %}
{% if provider == 'turnstile' %}
{% if rc.turnstile_site_key %}
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
<div class="cf-turnstile" data-sitekey="{{ rc.turnstile_site_key }}"></div>
{% endif %}
{% elseif provider == 'recaptcha_v2' %}
{% if rc.publickey %}
<script src='https://www.google.com/recaptcha/api.js' async defer></script>
<div class="g-recaptcha" data-sitekey="{{ rc.publickey }}"></div>
{% endif %}
{% elseif provider == 'hcaptcha' %}
{% if rc.hcaptcha_site_key %}
<script src='https://js.hcaptcha.com/1/api.js' async defer></script>
<div class="h-captcha" data-sitekey="{{ rc.hcaptcha_site_key }}"></div>
{% endif %}
{% endif %}
{% endif %}