2020-07-01 03:05:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Middleware\Api;
|
|
|
|
|
|
|
|
use Closure;
|
2022-05-22 18:10:01 +00:00
|
|
|
use JsonException;
|
2020-07-01 03:05:11 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
|
|
|
|
|
|
|
class IsValidJson
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Throw an exception if the request should be valid JSON data but there is an error while
|
|
|
|
* parsing the data. This avoids confusing validation errors where every field is flagged and
|
|
|
|
* it is not immediately clear that there is an issue with the JSON being passed.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle(Request $request, Closure $next)
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if ($request->isJson() && !empty($request->getContent())) {
|
2022-05-22 18:10:01 +00:00
|
|
|
try {
|
|
|
|
json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
|
|
|
|
} catch (JsonException $exception) {
|
|
|
|
throw new BadRequestHttpException('The JSON data passed in the request appears to be malformed: ' . $exception->getMessage());
|
2020-07-01 03:05:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|