Files

37 lines
1.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\Api;
2026-10-05 18:10:18 -04:00
use Closure;
use Illuminate\Http\Request;
2026-10-05 18:10:18 -04:00
use JsonException;
use Symfony\Component\HttpFoundation\Response;
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.
*/
2026-10-05 18:10:18 -04:00
/**
* @param Closure(Request):Response $next
* @return Response
*/
public function handle(Request $request, Closure $next): mixed
{
2026-10-05 18:10:18 -04:00
if ($request->isJson() && ! empty($request->getContent())) {
2022-05-22 14:10:01 -04:00
try {
json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
2026-10-05 18:10:18 -04:00
} catch (JsonException $exception) {
throw new BadRequestHttpException('The JSON data passed in the request appears to be malformed: '.$exception->getMessage(), $exception);
}
}
return $next($request);
}
}