Skip to content

Commit 493fa1d

Browse files
authored
fix: use cURL instead of file_get_contents for siteverify (#14)
* fix: use cURL instead of file_get_contents for siteverify * chore: bump module version to 4.0.4 * fix: harden siteverify with POST body and curl_init guard
1 parent 3986528 commit 493fa1d

2 files changed

Lines changed: 30 additions & 12 deletions

File tree

Action/ReCaptchaAction.php

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,49 @@ public function checkCaptcha(ReCaptchaCheckEvent $event)
3232
$event->setHuman($this->captchaVerified);
3333
return;
3434
}
35-
$requestUrl = "https://www.google.com/recaptcha/api/siteverify";
35+
36+
if (!function_exists('curl_init')) {
37+
$this->captchaVerified = false;
38+
return;
39+
}
3640

3741
$secretKey = ReCaptcha::getConfigValue('secret_key');
3842
$minScore = ReCaptcha::getConfigValue('min_score', 0.3);
39-
$requestUrl .= "?secret=$secretKey";
4043

4144
$captchaResponse = $event->getCaptchaResponse();
4245
if (null == $captchaResponse && $this->request) {
4346
$captchaResponse = $this->request->request->get('g-recaptcha-response');
4447
}
4548

46-
$requestUrl .= "&response=$captchaResponse";
47-
4849
$remoteIp = $event->getRemoteIp();
4950
if (null == $remoteIp && $this->request) {
5051
$remoteIp = $this->request->server->get('REMOTE_ADDR');
5152
}
5253

53-
$requestUrl .= "&remoteip=$remoteIp";
54-
55-
$result = json_decode(file_get_contents($requestUrl), true);
56-
if ($result['success'] == true && (!array_key_exists('score', $result) || $result['score'] > $minScore)) {
57-
$event->setHuman(true);
58-
$this->captchaVerified = true;
59-
return;
54+
$payload = http_build_query([
55+
'secret' => (string) $secretKey,
56+
'response' => (string) $captchaResponse,
57+
'remoteip' => (string) $remoteIp,
58+
]);
59+
60+
$curl = curl_init('https://www.google.com/recaptcha/api/siteverify');
61+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
62+
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
63+
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
64+
curl_setopt($curl, CURLOPT_POST, true);
65+
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
66+
$response = curl_exec($curl);
67+
curl_close($curl);
68+
69+
if (is_string($response)) {
70+
$result = json_decode($response, true);
71+
if (is_array($result)
72+
&& ($result['success'] ?? false) === true
73+
&& (!array_key_exists('score', $result) || $result['score'] > $minScore)) {
74+
$event->setHuman(true);
75+
$this->captchaVerified = true;
76+
return;
77+
}
6078
}
6179

6280
$this->captchaVerified = false;

Config/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<language>en_US</language>
2121
<language>fr_FR</language>
2222
</languages>
23-
<version>4.0.3</version>
23+
<version>4.0.4</version>
2424
<authors>
2525
<author>
2626
<name>Vincent Lopes-Vicente</name>

0 commit comments

Comments
 (0)