2015-12-06 18:58:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Exceptions;
|
|
|
|
|
|
|
|
use Exception;
|
2020-06-24 04:24:37 +00:00
|
|
|
use Throwable;
|
2018-02-04 19:31:39 +00:00
|
|
|
use PDOException;
|
2021-01-23 20:09:16 +00:00
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Support\Collection;
|
2018-03-10 19:02:41 +00:00
|
|
|
use Illuminate\Container\Container;
|
2019-03-03 02:28:28 +00:00
|
|
|
use Illuminate\Database\Connection;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2021-01-23 20:09:16 +00:00
|
|
|
use Illuminate\Foundation\Application;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Illuminate\Auth\AuthenticationException;
|
2017-08-12 20:29:01 +00:00
|
|
|
use Illuminate\Session\TokenMismatchException;
|
|
|
|
use Illuminate\Validation\ValidationException;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2017-08-12 20:29:01 +00:00
|
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Symfony\Component\Mailer\Exception\TransportException;
|
2017-09-24 17:32:29 +00:00
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
2020-10-03 18:18:14 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
2015-12-06 18:58:49 +00:00
|
|
|
|
|
|
|
class Handler extends ExceptionHandler
|
|
|
|
{
|
2018-03-05 04:21:23 +00:00
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* The validation parser in Laravel formats custom rules using the class name
|
2018-03-05 04:21:23 +00:00
|
|
|
* resulting in some weird rule names. This string will be parsed out and
|
|
|
|
* replaced with 'p_' in the response code.
|
|
|
|
*/
|
|
|
|
private const PTERODACTYL_RULE_STRING = 'pterodactyl\_rules\_';
|
|
|
|
|
2015-12-06 18:58:49 +00:00
|
|
|
/**
|
|
|
|
* A list of the exception types that should not be reported.
|
|
|
|
*/
|
|
|
|
protected $dontReport = [
|
2017-08-12 20:29:01 +00:00
|
|
|
AuthenticationException::class,
|
|
|
|
AuthorizationException::class,
|
|
|
|
HttpException::class,
|
|
|
|
ModelNotFoundException::class,
|
2017-09-24 17:32:29 +00:00
|
|
|
RecordNotFoundException::class,
|
2017-08-12 20:29:01 +00:00
|
|
|
TokenMismatchException::class,
|
|
|
|
ValidationException::class,
|
2015-12-06 18:58:49 +00:00
|
|
|
];
|
|
|
|
|
2022-05-22 20:05:58 +00:00
|
|
|
/**
|
|
|
|
* Maps exceptions to a specific response code. This handles special exception
|
|
|
|
* types that don't have a defined response code.
|
|
|
|
*/
|
|
|
|
protected static array $exceptionResponseCodes = [
|
|
|
|
AuthenticationException::class => 401,
|
|
|
|
AuthorizationException::class => 403,
|
|
|
|
ValidationException::class => 422,
|
|
|
|
];
|
|
|
|
|
2017-12-17 20:57:05 +00:00
|
|
|
/**
|
|
|
|
* A list of the inputs that are never flashed for validation exceptions.
|
|
|
|
*/
|
|
|
|
protected $dontFlash = [
|
2018-01-21 22:02:03 +00:00
|
|
|
'token',
|
|
|
|
'secret',
|
2017-12-17 20:57:05 +00:00
|
|
|
'password',
|
|
|
|
'password_confirmation',
|
|
|
|
];
|
|
|
|
|
2015-12-06 18:58:49 +00:00
|
|
|
/**
|
2021-01-23 20:09:16 +00:00
|
|
|
* Registers the exception handling callbacks for the application. This
|
|
|
|
* will capture specific exception types that we do not want to include
|
|
|
|
* the detailed stack traces for since they could reveal credentials to
|
|
|
|
* whoever can read the logs.
|
2017-07-22 02:17:42 +00:00
|
|
|
*
|
2021-01-23 20:09:16 +00:00
|
|
|
* @noinspection PhpUnusedLocalVariableInspection
|
2015-12-06 18:58:49 +00:00
|
|
|
*/
|
2021-01-23 20:09:16 +00:00
|
|
|
public function register()
|
2015-12-06 18:58:49 +00:00
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
if (config('app.exceptions.report_all', false)) {
|
|
|
|
$this->dontReport = [];
|
2018-02-04 19:31:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
$this->reportable(function (PDOException $ex) {
|
|
|
|
$ex = $this->generateCleanedExceptionStack($ex);
|
|
|
|
});
|
2018-02-18 20:31:40 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
$this->reportable(function (TransportException $ex) {
|
2021-01-23 20:09:16 +00:00
|
|
|
$ex = $this->generateCleanedExceptionStack($ex);
|
|
|
|
});
|
2018-02-18 20:31:40 +00:00
|
|
|
}
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
private function generateCleanedExceptionStack(Throwable $exception): string
|
2018-02-18 20:31:40 +00:00
|
|
|
{
|
|
|
|
$cleanedStack = '';
|
|
|
|
foreach ($exception->getTrace() as $index => $item) {
|
|
|
|
$cleanedStack .= sprintf(
|
|
|
|
"#%d %s(%d): %s%s%s\n",
|
|
|
|
$index,
|
2021-01-23 20:09:16 +00:00
|
|
|
Arr::get($item, 'file'),
|
|
|
|
Arr::get($item, 'line'),
|
|
|
|
Arr::get($item, 'class'),
|
|
|
|
Arr::get($item, 'type'),
|
|
|
|
Arr::get($item, 'function')
|
2018-02-18 20:31:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = sprintf(
|
|
|
|
'%s: %s in %s:%d',
|
|
|
|
class_basename($exception),
|
|
|
|
$exception->getMessage(),
|
|
|
|
$exception->getFile(),
|
|
|
|
$exception->getLine()
|
|
|
|
);
|
|
|
|
|
|
|
|
return $message . "\nStack trace:\n" . trim($cleanedStack);
|
2015-12-06 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render an exception into an HTTP response.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2021-01-23 20:33:34 +00:00
|
|
|
*
|
2020-06-24 04:24:37 +00:00
|
|
|
* @throws \Throwable
|
2015-12-06 18:58:49 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function render($request, Throwable $e): Response
|
2015-12-06 18:58:49 +00:00
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$connections = $this->container->make(Connection::class);
|
2019-03-03 02:28:28 +00:00
|
|
|
|
|
|
|
// If we are currently wrapped up inside a transaction, we will roll all the way
|
|
|
|
// back to the beginning. This needs to happen, otherwise session data does not
|
|
|
|
// get properly persisted.
|
|
|
|
//
|
|
|
|
// This is kind of a hack, and ideally things like this should be handled as
|
|
|
|
// much as possible at the code level, but there are a lot of spots that do a
|
|
|
|
// ton of actions and were written before this bug discovery was made.
|
|
|
|
//
|
|
|
|
// @see https://github.com/pterodactyl/panel/pull/1468
|
|
|
|
if ($connections->transactionLevel()) {
|
|
|
|
$connections->rollBack(0);
|
|
|
|
}
|
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
return parent::render($request, $e);
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
2017-04-02 04:11:52 +00:00
|
|
|
|
2017-12-17 20:57:05 +00:00
|
|
|
/**
|
2018-02-04 19:31:39 +00:00
|
|
|
* Transform a validation exception into a consistent format to be returned for
|
|
|
|
* calls to the API.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2017-12-17 20:57:05 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function invalidJson($request, ValidationException $exception): JsonResponse
|
2017-12-17 20:57:05 +00:00
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$codes = Collection::make($exception->validator->failed())->mapWithKeys(function ($reasons, $field) {
|
2020-08-28 04:15:23 +00:00
|
|
|
$cleaned = [];
|
|
|
|
foreach ($reasons as $reason => $attrs) {
|
2021-01-23 20:09:16 +00:00
|
|
|
$cleaned[] = Str::snake($reason);
|
2020-08-28 04:15:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return [str_replace('.', '_', $field) => $cleaned];
|
|
|
|
})->toArray();
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
$errors = Collection::make($exception->errors())->map(function ($errors, $field) use ($codes, $exception) {
|
2017-12-17 20:57:05 +00:00
|
|
|
$response = [];
|
|
|
|
foreach ($errors as $key => $error) {
|
2020-08-28 04:15:23 +00:00
|
|
|
$meta = [
|
2020-08-28 02:26:52 +00:00
|
|
|
'source_field' => $field,
|
2021-01-23 20:09:16 +00:00
|
|
|
'rule' => str_replace(self::PTERODACTYL_RULE_STRING, 'p_', Arr::get(
|
2021-01-23 20:33:34 +00:00
|
|
|
$codes,
|
|
|
|
str_replace('.', '_', $field) . '.' . $key
|
2020-08-28 04:15:23 +00:00
|
|
|
)),
|
|
|
|
];
|
|
|
|
|
2022-05-22 20:50:36 +00:00
|
|
|
$converted = $this->convertExceptionToArray($exception)['errors'][0];
|
2020-08-28 04:15:23 +00:00
|
|
|
$converted['detail'] = $error;
|
2022-05-22 20:50:36 +00:00
|
|
|
$converted['meta'] = array_merge($converted['meta'] ?? [], $meta);
|
2020-08-28 04:15:23 +00:00
|
|
|
|
|
|
|
$response[] = $converted;
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
})->flatMap(function ($errors) {
|
|
|
|
return $errors;
|
|
|
|
})->toArray();
|
|
|
|
|
|
|
|
return response()->json(['errors' => $errors], $exception->status);
|
|
|
|
}
|
2015-12-06 18:58:49 +00:00
|
|
|
|
2017-12-17 20:57:05 +00:00
|
|
|
/**
|
|
|
|
* Return the exception as a JSONAPI representation for use on API requests.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected function convertExceptionToArray(Throwable $e, array $override = []): array
|
2017-12-17 20:57:05 +00:00
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
$match = self::$exceptionResponseCodes[get_class($e)] ?? null;
|
2022-05-22 20:05:58 +00:00
|
|
|
|
2017-12-17 20:57:05 +00:00
|
|
|
$error = [
|
2022-10-14 16:59:20 +00:00
|
|
|
'code' => class_basename($e),
|
|
|
|
'status' => method_exists($e, 'getStatusCode')
|
|
|
|
? strval($e->getStatusCode())
|
2022-05-22 20:05:58 +00:00
|
|
|
: strval($match ?? '500'),
|
2022-10-14 16:59:20 +00:00
|
|
|
'detail' => $e instanceof HttpExceptionInterface || !is_null($match)
|
|
|
|
? $e->getMessage()
|
2020-10-03 19:14:39 +00:00
|
|
|
: 'An unexpected error was encountered while processing this request, please try again.',
|
2017-12-17 20:57:05 +00:00
|
|
|
];
|
2017-07-23 01:15:01 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
if ($e instanceof ModelNotFoundException || $e->getPrevious() instanceof ModelNotFoundException) {
|
2020-08-20 03:21:12 +00:00
|
|
|
// Show a nicer error message compared to the standard "No query results for model"
|
|
|
|
// response that is normally returned. If we are in debug mode this will get overwritten
|
|
|
|
// with a more specific error message to help narrow down things.
|
|
|
|
$error['detail'] = 'The requested resource could not be found on the server.';
|
|
|
|
}
|
|
|
|
|
2017-12-17 20:57:05 +00:00
|
|
|
if (config('app.debug')) {
|
|
|
|
$error = array_merge($error, [
|
2022-10-14 16:59:20 +00:00
|
|
|
'detail' => $e->getMessage(),
|
2017-12-17 20:57:05 +00:00
|
|
|
'source' => [
|
2022-10-14 16:59:20 +00:00
|
|
|
'line' => $e->getLine(),
|
|
|
|
'file' => str_replace(Application::getInstance()->basePath(), '', $e->getFile()),
|
2017-12-17 20:57:05 +00:00
|
|
|
],
|
|
|
|
'meta' => [
|
2022-10-14 16:59:20 +00:00
|
|
|
'trace' => Collection::make($e->getTrace())
|
2022-05-22 20:23:22 +00:00
|
|
|
->map(fn ($trace) => Arr::except($trace, ['args']))
|
|
|
|
->all(),
|
2022-10-14 16:59:20 +00:00
|
|
|
'previous' => Collection::make($this->extractPrevious($e))
|
|
|
|
->map(fn ($exception) => $e->getTrace())
|
2022-05-22 20:23:22 +00:00
|
|
|
->map(fn ($trace) => Arr::except($trace, ['args']))
|
|
|
|
->all(),
|
2017-12-17 20:57:05 +00:00
|
|
|
],
|
|
|
|
]);
|
2015-12-06 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
2017-12-17 20:57:05 +00:00
|
|
|
return ['errors' => [array_merge($error, $override)]];
|
2015-12-06 18:58:49 +00:00
|
|
|
}
|
2016-09-03 21:09:00 +00:00
|
|
|
|
2018-03-10 19:02:41 +00:00
|
|
|
/**
|
|
|
|
* Return an array of exceptions that should not be reported.
|
|
|
|
*/
|
|
|
|
public static function isReportable(Exception $exception): bool
|
|
|
|
{
|
|
|
|
return (new static(Container::getInstance()))->shouldReport($exception);
|
|
|
|
}
|
|
|
|
|
2016-09-03 21:09:00 +00:00
|
|
|
/**
|
|
|
|
* Convert an authentication exception into an unauthenticated response.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2016-09-03 21:09:00 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected function unauthenticated($request, AuthenticationException $exception): JsonResponse|RedirectResponse
|
2016-09-03 21:09:00 +00:00
|
|
|
{
|
|
|
|
if ($request->expectsJson()) {
|
2022-05-22 20:23:22 +00:00
|
|
|
return new JsonResponse($this->convertExceptionToArray($exception), JsonResponse::HTTP_UNAUTHORIZED);
|
2016-09-03 21:09:00 +00:00
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2022-05-22 20:23:22 +00:00
|
|
|
return redirect()->guest('/auth/login');
|
2016-09-03 21:09:00 +00:00
|
|
|
}
|
2017-12-17 20:57:05 +00:00
|
|
|
|
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* Extracts all the previous exceptions that lead to the one passed into this
|
2022-05-22 20:23:22 +00:00
|
|
|
* function being thrown.
|
2017-12-17 20:57:05 +00:00
|
|
|
*
|
2022-05-22 20:23:22 +00:00
|
|
|
* @return \Throwable[]
|
2017-12-17 20:57:05 +00:00
|
|
|
*/
|
2022-05-22 20:23:22 +00:00
|
|
|
protected function extractPrevious(Throwable $e): array
|
2017-12-17 20:57:05 +00:00
|
|
|
{
|
2022-05-22 20:23:22 +00:00
|
|
|
$previous = [];
|
|
|
|
while ($value = $e->getPrevious()) {
|
|
|
|
if (!$value instanceof Throwable) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$previous[] = $value;
|
|
|
|
$e = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $previous;
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
2022-05-22 22:21:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to allow reaching into the handler to convert an exception
|
|
|
|
* into the expected array response type.
|
|
|
|
*/
|
|
|
|
public static function toArray(Throwable $e): array
|
|
|
|
{
|
|
|
|
return (new self(app()))->convertExceptionToArray($e);
|
|
|
|
}
|
2015-12-06 18:58:49 +00:00
|
|
|
}
|