Files

109 lines
3.1 KiB
PHP
Raw Permalink Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Http\Middleware;
2026-10-05 18:10:18 -04:00
use Closure;
2017-10-29 12:37:25 -05:00
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
2017-10-29 12:37:25 -05:00
use Illuminate\Http\Request;
use Illuminate\Http\Response;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Arr;
2017-03-31 20:48:35 -04:00
use Pterodactyl\Events\Auth\FailedCaptcha;
use Symfony\Component\HttpKernel\Exception\HttpException;
class VerifyReCaptcha
{
/**
* Handle an incoming request.
*/
2026-10-05 18:10:18 -04:00
/**
* @param Closure(Request): \Symfony\Component\HttpFoundation\Response $next
* @return \Symfony\Component\HttpFoundation\Response
*/
public function handle(Request $request, Closure $next): mixed
{
2026-10-08 17:22:14 -04:00
if (! config('recaptcha.enabled')) {
2017-03-31 20:48:35 -04:00
return $next($request);
}
$result = $this->verifyCaptcha($request);
2026-10-11 19:06:44 -04:00
$domainValid = ! config()->boolean('recaptcha.verify_domain') || $this->isResponseVerified($result, $request);
if ($result['success'] && $domainValid) {
return $next($request);
}
2017-03-31 20:48:35 -04:00
2026-10-08 17:22:14 -04:00
event(
new FailedCaptcha(
2026-10-05 18:10:18 -04:00
$request->ip() ?? 'unknown',
$result['hostname'] ?? 'unknown'
)
);
2017-10-29 12:37:25 -05:00
2021-01-23 12:33:34 -08:00
throw new HttpException(Response::HTTP_BAD_REQUEST, 'Failed to validate reCAPTCHA data.');
2017-10-29 12:37:25 -05:00
}
/**
* @return array{success: bool, hostname: string|null}
*/
private function verifyCaptcha(Request $request): array
{
if (! $request->filled('g-recaptcha-response')) {
return ['success' => false, 'hostname' => null];
}
try {
$client = new Client(['timeout' => 5, 'connect_timeout' => 2]);
2026-10-11 16:09:53 -04:00
$res = $client->post(config()->string('recaptcha.domain'), [
'form_params' => [
2026-10-08 17:22:14 -04:00
'secret' => config('recaptcha.secret_key'),
'response' => $request->input('g-recaptcha-response'),
],
]);
if ($res->getStatusCode() === 200) {
return $this->decodeResponse($res->getBody()->__toString());
}
2026-10-08 09:54:19 +01:00
} catch (GuzzleException) {
// Ignore the error entirely, we will just return a failed response below.
}
return ['success' => false, 'hostname' => null];
}
2017-10-29 12:37:25 -05:00
/**
* Determine if the response from the recaptcha servers was valid.
*/
2026-10-05 18:10:18 -04:00
/**
* @param array{success: bool, hostname: string|null} $result
*/
private function isResponseVerified(array $result, Request $request): bool
2017-10-29 12:37:25 -05:00
{
$url = parse_url($request->url());
2017-03-31 20:48:35 -04:00
2026-10-05 18:10:18 -04:00
return $url !== false && $result['hostname'] === Arr::get($url, 'host');
}
/**
* @return array{success: bool, hostname: string|null}
*/
private function decodeResponse(string $response): array
{
$decoded = json_decode($response, true);
if (! is_array($decoded)) {
return ['success' => false, 'hostname' => null];
}
$success = $decoded['success'] ?? false;
$hostname = $decoded['hostname'] ?? null;
return [
'success' => is_bool($success) && $success,
'hostname' => is_string($hostname) ? $hostname : null,
];
}
}