2017-03-31 12:19:44 +02:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2017-03-31 12:19:44 +02:00
|
|
|
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;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Contracts\Config\Repository;
|
|
|
|
|
use Illuminate\Contracts\Events\Dispatcher;
|
2017-10-29 12:37:25 -05:00
|
|
|
use Illuminate\Http\Request;
|
2019-12-15 16:13:44 -08:00
|
|
|
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;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Support\JsonValueGuard;
|
2019-12-15 16:13:44 -08:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
2017-03-31 12:19:44 +02:00
|
|
|
|
|
|
|
|
class VerifyReCaptcha
|
|
|
|
|
{
|
2017-10-29 12:37:25 -05:00
|
|
|
/**
|
|
|
|
|
* VerifyReCaptcha constructor.
|
|
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function __construct(private readonly Dispatcher $dispatcher, private readonly Repository $config) {}
|
2017-10-29 12:37:25 -05:00
|
|
|
|
2017-03-31 12:19:44 +02:00
|
|
|
/**
|
|
|
|
|
* 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
|
2017-03-31 12:19:44 +02:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
if (! $this->config->get('recaptcha.enabled')) {
|
2017-03-31 20:48:35 -04:00
|
|
|
return $next($request);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-17 13:07:38 -06:00
|
|
|
if ($request->filled('g-recaptcha-response')) {
|
2026-10-05 18:10:18 -04:00
|
|
|
$client = new Client(['timeout' => 5, 'connect_timeout' => 2]);
|
|
|
|
|
$res = $client->post(JsonValueGuard::string($this->config->get('recaptcha.domain')), [
|
2017-03-31 12:19:44 +02:00
|
|
|
'form_params' => [
|
2017-10-29 12:37:25 -05:00
|
|
|
'secret' => $this->config->get('recaptcha.secret_key'),
|
2017-03-31 21:12:49 -04:00
|
|
|
'response' => $request->input('g-recaptcha-response'),
|
2017-03-31 12:19:44 +02:00
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($res->getStatusCode() === 200) {
|
2026-10-05 18:10:18 -04:00
|
|
|
$result = $this->decodeResponse($res->getBody()->__toString());
|
2017-03-31 12:19:44 +02:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
if ($result['success'] && (! $this->config->get('recaptcha.verify_domain') || $this->isResponseVerified($result, $request))) {
|
2017-03-31 12:19:44 +02:00
|
|
|
return $next($request);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-31 20:48:35 -04:00
|
|
|
|
2019-12-15 16:13:44 -08:00
|
|
|
$this->dispatcher->dispatch(
|
|
|
|
|
new FailedCaptcha(
|
2026-10-05 18:10:18 -04:00
|
|
|
$request->ip() ?? 'unknown',
|
|
|
|
|
$result['hostname'] ?? 'unknown'
|
2019-12-15 16:13:44 -08:00
|
|
|
)
|
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
if (! $this->config->get('recaptcha.verify_domain')) {
|
2017-10-29 12:37:25 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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,
|
|
|
|
|
];
|
2017-03-31 12:19:44 +02:00
|
|
|
}
|
|
|
|
|
}
|