Repeated Variable References Multiply Argument Coercion and Cost Analysis Work, Allowing Resource Exhaustion
Description
GraphQL allows an argument to reference the same variable any number of times, for example products: [$product $product $product]. In affected versions, Hot Chocolate turns every reference into a complete copy of the variable's value when it coerces the field's arguments, and parses each copy into new runtime objects. No limit applies to these copies, and cost analysis reports the same cost however many there are, so a request of a few kilobytes can make the server build tens of millions of values.
In 16.7.0 to 16.7.7, cost analysis also walks the whole value of a variable again for every reference to it, whether the references are list items, input object fields, or the arguments of aliased fields. It does so before it compares the cost with the limit, so the work is spent on requests that are then refused.
Neither step reads the request's cancellation token, so the execution timeout and a client disconnect do not stop them. Hot Chocolate servers on AddGraphQLServer() are affected with no option changed, and Fusion gateways on 16.7.x through cost analysis. Servers that accept only persisted operations are not affected.
The fixed versions add MaxAllowedRepeatedVariableNodes to the request executor options, 100,000 by default. The first reference to each variable in an argument is free, and every further reference counts the nodes of the variable's value; an argument over the limit returns a field error with the error code HC0144. On 16.x, cost analysis computes the cost of each variable's value once per request.
Fixed Versions
- 16.7.8
- 15.1.22
- 14.3.6
- 13.9.21
Applications on earlier 16.x releases should upgrade to 16.7.8.
Impact
An unauthenticated client can make a server spend seconds of CPU and gigabytes of memory on a single small request, and the cost grows with the square of the request's size. In our tests, with no option changed, a 48 KB request made 16.7.7 allocate 3 GB and build 64 million values, and a 120 KB request took 22 seconds and 3 GB of memory. A 36 KB request against a filter spent over a second in cost analysis on 16.7.7 before it was refused. Repeated requests can exhaust the server's CPU and memory and degrade or stop the service.
No data is disclosed or changed.
This issue is classified as CWE-770: Allocation of Resources Without Limits or Throttling.
Recommendation
Upgrade to a fixed version.
If legitimate requests repeat large list or object variables within one argument, raise the limit:
builder.Services
.AddGraphQLServer()
.ModifyRequestOptions(o => o.MaxAllowedRepeatedVariableNodes = 200_000);If an immediate upgrade is not possible:
- Accept only persisted operations, which limits the documents a client can send to the application's own.
- Lower the server's maximum request size, which narrows the cost, since it grows with the square of the request's size.
Affected versions
- HotChocolate.AspNetCoreFrom 16.0.0 · fixed in 16.7.8
- HotChocolate.TypesFrom 16.0.0 · fixed in 16.7.8
- HotChocolate.CostAnalysis.CoreFrom 16.7.0 · fixed in 16.7.8
- HotChocolate.Fusion.AspNetCoreFrom 16.7.0 · fixed in 16.7.8
- HotChocolate.AspNetCoreFrom 15.0.0 · fixed in 15.1.22
- HotChocolate.ExecutionFrom 15.0.0 · fixed in 15.1.22
- HotChocolate.AspNetCoreFrom 14.0.0 · fixed in 14.3.6
- HotChocolate.ExecutionFrom 14.0.0 · fixed in 14.3.6
- HotChocolate.AspNetCoreFrom 13.0.0 · fixed in 13.9.21
- HotChocolate.ExecutionFrom 13.0.0 · fixed in 13.9.21