Fix silent failure mode when recaptcha is enabled

This commit is contained in:
Dane Everitt 2019-12-15 16:13:44 -08:00
parent 926b5ac099
commit d9d4c0590c
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53

View file

@ -6,8 +6,11 @@ use Closure;
use stdClass; use stdClass;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Pterodactyl\Events\Auth\FailedCaptcha; use Pterodactyl\Events\Auth\FailedCaptcha;
use Illuminate\Contracts\Config\Repository; use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Events\Dispatcher;
use Symfony\Component\HttpKernel\Exception\HttpException;
class VerifyReCaptcha class VerifyReCaptcha
{ {
@ -16,14 +19,21 @@ class VerifyReCaptcha
*/ */
private $config; private $config;
/**
* @var \Illuminate\Contracts\Events\Dispatcher
*/
private $dispatcher;
/** /**
* VerifyReCaptcha constructor. * VerifyReCaptcha constructor.
* *
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
* @param \Illuminate\Contracts\Config\Repository $config * @param \Illuminate\Contracts\Config\Repository $config
*/ */
public function __construct(Repository $config) public function __construct(Dispatcher $dispatcher, Repository $config)
{ {
$this->config = $config; $this->config = $config;
$this->dispatcher = $dispatcher;
} }
/** /**
@ -57,10 +67,15 @@ class VerifyReCaptcha
} }
} }
// Emit an event and return to the previous view with an error (only the captcha error will be shown!) $this->dispatcher->dispatch(
event(new FailedCaptcha($request->ip(), (! isset($result) ?: object_get($result, 'hostname')))); new FailedCaptcha(
$request->ip(), ! empty($result) ? ($result->hostname ?? null) : null
)
);
return redirect()->back()->withErrors(['g-recaptcha-response' => trans('strings.captcha_invalid')])->withInput(); throw new HttpException(
Response::HTTP_BAD_REQUEST, 'Failed to validate reCAPTCHA data.'
);
} }
/** /**