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

Commit 0ab8775

Browse files
authored
Merge pull request #219 from mgiraud/verify-url-timeout
Add timeout for the reCAPTCHA verification
2 parents 110f85a + 08d61d8 commit 0ab8775

5 files changed

Lines changed: 39 additions & 5 deletions

File tree

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function getConfigTreeBuilder()
3535
->scalarNode('locale_key')->defaultValue('%kernel.default_locale%')->end()
3636
->scalarNode('api_host')->defaultValue('www.google.com')->end()
3737
->booleanNode('locale_from_request')->defaultFalse()->end()
38+
->integerNode('timeout')->min(0)->defaultNull()->end()
3839
->arrayNode('trusted_roles')->prototype('scalar')->treatNullLike(array())->end()
3940
->end()
4041
;

src/Extension/ReCaptcha/RequestMethod/Post.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,22 @@ class Post implements RequestMethod
1717
*/
1818
private $recaptchaVerifyUrl;
1919

20+
/**
21+
* The timeout for the reCAPTCHA verification.
22+
*
23+
* @var int|null
24+
*/
25+
private $timeout;
26+
2027
/**
2128
* Constructor
2229
*
2330
* @param string $recaptchaVerifyServer
2431
*/
25-
public function __construct($recaptchaVerifyServer)
32+
public function __construct($recaptchaVerifyServer, $timeout)
2633
{
2734
$this->recaptchaVerifyUrl = ($recaptchaVerifyServer ?: 'https://www.google.com').'/recaptcha/api/siteverify';
35+
$this->timeout = $timeout;
2836
}
2937

3038
/**
@@ -51,6 +59,9 @@ public function submit(RequestParameters $params)
5159
$peer_key => 'www.google.com',
5260
),
5361
);
62+
if (null !== $this->timeout) {
63+
$options['http']['timeout'] = $this->timeout;
64+
}
5465
$context = stream_context_create($options);
5566
return file_get_contents($this->recaptchaVerifyUrl, false, $context);
5667
}

src/Extension/ReCaptcha/RequestMethod/ProxyPost.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,24 @@ class ProxyPost implements RequestMethod
2424
*/
2525
private $recaptchaVerifyUrl;
2626

27+
/**
28+
* The timeout for the reCAPTCHA verification.
29+
*
30+
* @var int|null
31+
*/
32+
private $timeout;
33+
2734
/**
2835
* Constructor
2936
*
3037
* @param array $httpProxy proxy data to connect to
3138
* @param string $recaptchaVerifyServer
3239
*/
33-
public function __construct(array $httpProxy, $recaptchaVerifyServer)
40+
public function __construct(array $httpProxy, $recaptchaVerifyServer, $timeout)
3441
{
3542
$this->httpProxy = $httpProxy;
3643
$this->recaptchaVerifyUrl = ($recaptchaVerifyServer ?: 'https://www.google.com').'/recaptcha/api/siteverify';
44+
$this->timeout = $timeout;
3745
}
3846

3947
/**
@@ -64,6 +72,9 @@ public function submit(RequestParameters $params)
6472
'request_fulluri' => true,
6573
),
6674
);
75+
if (null !== $this->timeout) {
76+
$options['http']['timeout'] = $this->timeout;
77+
}
6778
$context = stream_context_create($options);
6879
return file_get_contents($this->recaptchaVerifyUrl, false, $context);
6980
}

src/Resources/config/services.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ services:
3131
- '@?security.authorization_checker'
3232
- '%ewz_recaptcha.trusted_roles%'
3333
- '%ewz_recaptcha.api_host%'
34+
- '%ewz_recaptcha.timeout%'
3435
tags:
3536
- { name: validator.constraint_validator, alias: 'ewz_recaptcha.true' }

src/Validator/Constraints/IsTrueValidator.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ class IsTrueValidator extends ConstraintValidator
6969
*/
7070
protected $recaptchaVerifyServer;
7171

72+
/**
73+
* The timeout for the reCAPTCHA verification.
74+
*
75+
* @var int|null
76+
*/
77+
private $timeout;
78+
7279
/**
7380
* @param bool $enabled
7481
* @param string $privateKey
@@ -78,6 +85,7 @@ class IsTrueValidator extends ConstraintValidator
7885
* @param AuthorizationCheckerInterface|null $authorizationChecker
7986
* @param array $trustedRoles
8087
* @param string $apiHost
88+
* @param int|null $timeout
8189
*/
8290
public function __construct(
8391
$enabled,
@@ -87,7 +95,8 @@ public function __construct(
8795
$verifyHost,
8896
AuthorizationCheckerInterface $authorizationChecker = null,
8997
array $trustedRoles = array(),
90-
$apiHost = 'www.google.com')
98+
$apiHost = 'www.google.com',
99+
$timeout = null)
91100
{
92101
$this->enabled = $enabled;
93102
$this->privateKey = $privateKey;
@@ -97,6 +106,7 @@ public function __construct(
97106
$this->authorizationChecker = $authorizationChecker;
98107
$this->trustedRoles = $trustedRoles;
99108
$this->recaptchaVerifyServer = 'https://'.$apiHost;
109+
$this->timeout = $timeout;
100110
}
101111

102112
/**
@@ -123,9 +133,9 @@ public function validate($value, Constraint $constraint)
123133

124134
// Verify user response with Google
125135
if (null !== $this->httpProxy['host'] && null !== $this->httpProxy['port']) {
126-
$requestMethod = new ProxyPost($this->httpProxy, $this->recaptchaVerifyServer);
136+
$requestMethod = new ProxyPost($this->httpProxy, $this->recaptchaVerifyServer, $this->timeout);
127137
} else {
128-
$requestMethod = new Post($this->recaptchaVerifyServer);
138+
$requestMethod = new Post($this->recaptchaVerifyServer, $this->timeout);
129139
}
130140
$recaptcha = new ReCaptcha($this->privateKey, $requestMethod);
131141
$response = $recaptcha->verify($answer, $remoteip);

0 commit comments

Comments
 (0)