Skip to content
This repository was archived by the owner on Feb 25, 2026. It is now read-only.

Commit 8bb31b2

Browse files
authored
Merge pull request #273 from fixeditforyou/master
Fix action name validation
2 parents 88646b5 + cfe7c7f commit 8bb31b2

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

src/Form/Type/EWZRecaptchaV3Type.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
class EWZRecaptchaV3Type extends AbstractEWZRecaptchaType
1111
{
12+
public const DEFAULT_ACTION_NAME = 'form';
13+
1214
/** @var bool */
1315
private $hideBadge;
1416

src/Resources/views/Form/v3/ewz_recaptcha_widget.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<script{% if form.vars.script_nonce_csp is defined and form.vars.script_nonce_csp is not same as('') %} nonce="{{ form.vars.script_nonce_csp }}"{% endif %}>
1111
grecaptcha.ready(function () {
12-
grecaptcha.execute('{{ form.vars.public_key }}', { action: '{{ form.vars.action_name|default('form') }}' }).then(function (token) {
12+
grecaptcha.execute('{{ form.vars.public_key }}', { action: '{{ form.vars.action_name|default(constant('EWZ\\Bundle\\RecaptchaBundle\\Form\\Type\\EWZRecaptchaV3Type::DEFAULT_ACTION_NAME')) }}' }).then(function (token) {
1313
var recaptchaResponse = document.getElementById('{{ id }}');
1414
recaptchaResponse.value = token;
1515
});

src/Validator/Constraints/IsTrueValidatorV3.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace EWZ\Bundle\RecaptchaBundle\Validator\Constraints;
44

5+
use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaV3Type;
56
use Psr\Log\LoggerInterface;
67
use ReCaptcha\ReCaptcha;
8+
use Symfony\Component\Form\FormInterface;
79
use Symfony\Component\HttpFoundation\RequestStack;
810
use Symfony\Component\Validator\Constraint;
911
use Symfony\Component\Validator\ConstraintValidator;
@@ -87,11 +89,12 @@ private function isTokenValid($token)
8789
{
8890
try {
8991
$remoteIp = $this->requestStack->getCurrentRequest()->getClientIp();
92+
$action = $this->getActionName();
9093

9194
$recaptcha = new ReCaptcha($this->secretKey);
9295

9396
$response = $recaptcha
94-
->setExpectedAction('form')
97+
->setExpectedAction($action)
9598
->setScoreThreshold($this->scoreThreshold)
9699
->verify($token, $remoteIp);
97100

@@ -107,4 +110,16 @@ private function isTokenValid($token)
107110
return false;
108111
}
109112
}
113+
114+
private function getActionName(): string
115+
{
116+
$object = $this->context->getObject();
117+
$action = null;
118+
119+
if ($object instanceof FormInterface) {
120+
$action = $object->getConfig()->getOption('action_name');
121+
}
122+
123+
return $action ?: EWZRecaptchaV3Type::DEFAULT_ACTION_NAME;
124+
}
110125
}

0 commit comments

Comments
 (0)