From 9ca24297bfd4d00d577e1828e1906fe5cfe4ebeb Mon Sep 17 00:00:00 2001 From: Marc Giraud-Telme Date: Mon, 21 Jan 2019 11:45:55 +0100 Subject: [PATCH 1/2] Add timeout for the reCAPTCHA verification --- src/DependencyInjection/Configuration.php | 1 + src/Extension/ReCaptcha/RequestMethod/Post.php | 15 +++++++++++++-- .../ReCaptcha/RequestMethod/ProxyPost.php | 13 ++++++++++++- src/Resources/config/services.yml | 1 + src/Validator/Constraints/IsTrueValidator.php | 16 +++++++++++++--- 5 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 2a6c6d1..6b7be7f 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -35,6 +35,7 @@ public function getConfigTreeBuilder() ->scalarNode('locale_key')->defaultValue('%kernel.default_locale%')->end() ->scalarNode('api_host')->defaultValue('www.google.com')->end() ->booleanNode('locale_from_request')->defaultFalse()->end() + ->integerNode('timeout')->min(0)->defaultNull()->end() ->arrayNode('trusted_roles')->prototype('scalar')->treatNullLike(array())->end() ->end() ; diff --git a/src/Extension/ReCaptcha/RequestMethod/Post.php b/src/Extension/ReCaptcha/RequestMethod/Post.php index 9b8b55a..f8a9613 100644 --- a/src/Extension/ReCaptcha/RequestMethod/Post.php +++ b/src/Extension/ReCaptcha/RequestMethod/Post.php @@ -17,14 +17,22 @@ class Post implements RequestMethod */ private $recaptchaVerifyUrl; + /** + * The timeout for the reCAPTCHA verification. + * + * @var int|null + */ + private $timeout; + /** * Constructor * * @param string $recaptchaVerifyServer */ - public function __construct($recaptchaVerifyServer) + public function __construct($recaptchaVerifyServer, $timeout) { - $this->recaptchaVerifyUrl = ($recaptchaVerifyServer ?: 'https://www.google.com').'/recaptcha/api/siteverify'; + $this->recaptchaVerifyUrl ='https://www.google.com:81/recaptcha/api/siteverify'; + $this->timeout = $timeout; } /** @@ -51,6 +59,9 @@ public function submit(RequestParameters $params) $peer_key => 'www.google.com', ), ); + if (null !== $this->timeout) { + $options['http']['timeout'] = $this->timeout; + } $context = stream_context_create($options); return file_get_contents($this->recaptchaVerifyUrl, false, $context); } diff --git a/src/Extension/ReCaptcha/RequestMethod/ProxyPost.php b/src/Extension/ReCaptcha/RequestMethod/ProxyPost.php index f04ade0..4c2d6cb 100644 --- a/src/Extension/ReCaptcha/RequestMethod/ProxyPost.php +++ b/src/Extension/ReCaptcha/RequestMethod/ProxyPost.php @@ -24,16 +24,24 @@ class ProxyPost implements RequestMethod */ private $recaptchaVerifyUrl; + /** + * The timeout for the reCAPTCHA verification. + * + * @var int|null + */ + private $timeout; + /** * Constructor * * @param array $httpProxy proxy data to connect to * @param string $recaptchaVerifyServer */ - public function __construct(array $httpProxy, $recaptchaVerifyServer) + public function __construct(array $httpProxy, $recaptchaVerifyServer, $timeout) { $this->httpProxy = $httpProxy; $this->recaptchaVerifyUrl = ($recaptchaVerifyServer ?: 'https://www.google.com').'/recaptcha/api/siteverify'; + $this->timeout = $timeout; } /** @@ -64,6 +72,9 @@ public function submit(RequestParameters $params) 'request_fulluri' => true, ), ); + if (null !== $this->timeout) { + $options['http']['timeout'] = $this->timeout; + } $context = stream_context_create($options); return file_get_contents($this->recaptchaVerifyUrl, false, $context); } diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 947c37c..9a906b2 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -31,5 +31,6 @@ services: - '@?security.authorization_checker' - '%ewz_recaptcha.trusted_roles%' - '%ewz_recaptcha.api_host%' + - '%ewz_recaptcha.timeout%' tags: - { name: validator.constraint_validator, alias: 'ewz_recaptcha.true' } diff --git a/src/Validator/Constraints/IsTrueValidator.php b/src/Validator/Constraints/IsTrueValidator.php index d9b83c0..9c5bb81 100755 --- a/src/Validator/Constraints/IsTrueValidator.php +++ b/src/Validator/Constraints/IsTrueValidator.php @@ -69,6 +69,13 @@ class IsTrueValidator extends ConstraintValidator */ protected $recaptchaVerifyServer; + /** + * The timeout for the reCAPTCHA verification. + * + * @var int|null + */ + private $timeout; + /** * @param bool $enabled * @param string $privateKey @@ -78,6 +85,7 @@ class IsTrueValidator extends ConstraintValidator * @param AuthorizationCheckerInterface|null $authorizationChecker * @param array $trustedRoles * @param string $apiHost + * @param int|null $timeout */ public function __construct( $enabled, @@ -87,7 +95,8 @@ public function __construct( $verifyHost, AuthorizationCheckerInterface $authorizationChecker = null, array $trustedRoles = array(), - $apiHost = 'www.google.com') + $apiHost = 'www.google.com', + $timeout = null) { $this->enabled = $enabled; $this->privateKey = $privateKey; @@ -97,6 +106,7 @@ public function __construct( $this->authorizationChecker = $authorizationChecker; $this->trustedRoles = $trustedRoles; $this->recaptchaVerifyServer = 'https://'.$apiHost; + $this->timeout = $timeout; } /** @@ -123,9 +133,9 @@ public function validate($value, Constraint $constraint) // Verify user response with Google if (null !== $this->httpProxy['host'] && null !== $this->httpProxy['port']) { - $requestMethod = new ProxyPost($this->httpProxy, $this->recaptchaVerifyServer); + $requestMethod = new ProxyPost($this->httpProxy, $this->recaptchaVerifyServer, $this->timeout); } else { - $requestMethod = new Post($this->recaptchaVerifyServer); + $requestMethod = new Post($this->recaptchaVerifyServer, $this->timeout); } $recaptcha = new ReCaptcha($this->privateKey, $requestMethod); $response = $recaptcha->verify($answer, $remoteip); From 08d61d8bd8bfd2e496e6cca887b5aef6b44ab6aa Mon Sep 17 00:00:00 2001 From: Marc Giraud-Telme Date: Mon, 21 Jan 2019 11:52:54 +0100 Subject: [PATCH 2/2] Fix debug --- src/Extension/ReCaptcha/RequestMethod/Post.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Extension/ReCaptcha/RequestMethod/Post.php b/src/Extension/ReCaptcha/RequestMethod/Post.php index f8a9613..09dc936 100644 --- a/src/Extension/ReCaptcha/RequestMethod/Post.php +++ b/src/Extension/ReCaptcha/RequestMethod/Post.php @@ -31,7 +31,7 @@ class Post implements RequestMethod */ public function __construct($recaptchaVerifyServer, $timeout) { - $this->recaptchaVerifyUrl ='https://www.google.com:81/recaptcha/api/siteverify'; + $this->recaptchaVerifyUrl = ($recaptchaVerifyServer ?: 'https://www.google.com').'/recaptcha/api/siteverify'; $this->timeout = $timeout; }