From 916a3f2c6a3c856ed4eefd158c6dbd5775677295 Mon Sep 17 00:00:00 2001 From: Laurent Muller Date: Mon, 6 May 2024 20:08:23 +0200 Subject: [PATCH] Updated verify signature of RecaptchaService. --- src/Service/RecaptchaService.php | 3 +-- src/Validator/RecaptchaValidator.php | 14 +++++++++++-- tests/Validator/RecaptchaValidatorTest.php | 24 ++++++++++++++++++++-- translations/validators.fr_CH.yaml | 1 + 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/Service/RecaptchaService.php b/src/Service/RecaptchaService.php index abdde6688..ec42879a9 100644 --- a/src/Service/RecaptchaService.php +++ b/src/Service/RecaptchaService.php @@ -100,9 +100,8 @@ public function translateErrors(array $codes): array return \array_map(fn (mixed $code): string => $this->translateError("recaptcha.$code"), $codes); } - public function verify(string $response, ?Request $request = null): Response + public function verify(string $response, Request $request): Response { - $request ??= Request::createFromGlobals(); $recaptcha = new ReCaptcha($this->secretKey); $recaptcha->setChallengeTimeout($this->challengeTimeout) ->setScoreThreshold($this->scoreThreshold) diff --git a/src/Validator/RecaptchaValidator.php b/src/Validator/RecaptchaValidator.php index 563c5ba11..5816c1c0c 100644 --- a/src/Validator/RecaptchaValidator.php +++ b/src/Validator/RecaptchaValidator.php @@ -13,6 +13,8 @@ namespace App\Validator; use App\Service\RecaptchaService; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Validator\Constraint; /** @@ -22,7 +24,7 @@ */ class RecaptchaValidator extends AbstractConstraintValidator { - public function __construct(private readonly RecaptchaService $service) + public function __construct(private readonly RecaptchaService $service, private readonly RequestStack $requestStack) { parent::__construct(Recaptcha::class); } @@ -32,7 +34,15 @@ public function __construct(private readonly RecaptchaService $service) */ protected function doValidate(string $value, Constraint $constraint): void { - $response = $this->service->verify($value); + $request = $this->requestStack->getCurrentRequest(); + if (!$request instanceof Request) { + $this->context->buildViolation('recaptcha.no-request') + ->addViolation(); + + return; + } + + $response = $this->service->verify($value, $request); if ($response->isSuccess()) { return; } diff --git a/tests/Validator/RecaptchaValidatorTest.php b/tests/Validator/RecaptchaValidatorTest.php index 0093d4ee3..766b6766f 100644 --- a/tests/Validator/RecaptchaValidatorTest.php +++ b/tests/Validator/RecaptchaValidatorTest.php @@ -17,6 +17,8 @@ use App\Validator\RecaptchaValidator; use PHPUnit\Framework\MockObject\Exception; use ReCaptcha\Response; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; /** @@ -83,7 +85,10 @@ public function testSuccess(): void */ protected function createValidator(): RecaptchaValidator { - return new RecaptchaValidator($this->createService()); + $service = $this->createService(); + $requestStack = $this->createRequestStack(); + + return new RecaptchaValidator($service, $requestStack); } private function createConstraint(): Recaptcha @@ -91,6 +96,20 @@ private function createConstraint(): Recaptcha return new Recaptcha(); } + /** + * @throws Exception + */ + private function createRequestStack(): RequestStack + { + $request = $this->createMock(Request::class); + $requestStack = $this->createMock(RequestStack::class); + $requestStack->expects(self::any()) + ->method('getCurrentRequest') + ->willReturn($request); + + return $requestStack; + } + /** * @throws Exception */ @@ -114,7 +133,8 @@ private function createService(string $code = ''): RecaptchaService private function initValidator(string $code = ''): RecaptchaValidator { $service = $this->createService($code); - $this->validator = new RecaptchaValidator($service); + $requestStack = $this->createRequestStack(); + $this->validator = new RecaptchaValidator($service, $requestStack); $this->validator->initialize($this->context); return $this->validator; diff --git a/translations/validators.fr_CH.yaml b/translations/validators.fr_CH.yaml index 3928df630..80475a11b 100644 --- a/translations/validators.fr_CH.yaml +++ b/translations/validators.fr_CH.yaml @@ -63,6 +63,7 @@ recaptcha: timeout-or-duplicate: Le délai d'attente est atteint ou la validation a été appelée deux fois. unknown-error: Une erreur inconnue s'est produite durant de la validation. incorrect-captcha-sol: Une erreur inconnue s'est produite durant de la validation. + no-request: Aucune requête valide n'est définie. state: unique_code: Ce nom est déjà utilisé par un autre statut. task: