fix(graphql): rate limit on parse error (#998)

This commit is contained in:
Kyrch
2025-11-10 10:34:43 -03:00
committed by GitHub
parent 7a9dc62022
commit f75765f27e
2 changed files with 11 additions and 10 deletions
@@ -6,6 +6,7 @@ namespace App\Http\Middleware\GraphQL;
use App\Enums\Auth\SpecialPermission;
use Closure;
use GraphQL\Error\SyntaxError;
use GraphQL\Language\AST\OperationDefinitionNode;
use GraphQL\Language\Parser;
use Illuminate\Http\JsonResponse;
@@ -41,18 +42,18 @@ class RateLimitPerQuery
}
if ($query) {
try {
$ast = Parser::parse($query);
} catch (JsonException) {
return $next($request);
}
$rootFields = 0;
foreach ($ast->definitions as $definition) {
if ($definition instanceof OperationDefinitionNode) {
$rootFields += count($definition->selectionSet->selections ?? []);
try {
$ast = Parser::parse($query);
foreach ($ast->definitions as $definition) {
if ($definition instanceof OperationDefinitionNode) {
$rootFields += count($definition->selectionSet->selections ?? []);
}
}
} catch (JsonException|SyntaxError) {
// Do nothing
}
$hits = max(1, $rootFields);
+1 -1
View File
@@ -6,7 +6,7 @@ use App\GraphQL\Schema\Schemas\DefaultSchema;
use App\GraphQL\Schema\Types\Base\PaginationInfoType;
return [
'rate_limit' => env('GRAPHQL_RATE_LIMIT', 80),
'rate_limit' => (int) env('GRAPHQL_RATE_LIMIT', 80),
'route' => [
// The prefix for routes; do NOT use a leading slash!