Document Cache Poisoning via Client-Supplied Document IDs
Description
Hot Chocolate 16 caches a document that a request sends under the document ID the request carries (id or documentId), without checking that the ID belongs to the document. The document cache is shared by all clients, and a request that sends only a query is looked up in it by the hash of the query text, which is MD5 by default. A request that sends another query's hash as its document ID, together with a different query, therefore replaces that query: every client that later sends it executes the replacement, with its own identity and permissions.
The same ID also keys other state:
- The operation and cost caches. A client can have an operation executed while cost analysis applies the result computed for another document.
- The authorization directives collected for
ApplyPolicy.Validation. A client can execute an operation that selects types or fields protected at validation without the authorization handler being called. - The operation document storage of automatic persisted operations (
UseAutomaticPersistedOperationPipeline()). A document can be stored under another document's hash. A request that names a stored document and sends a different query also writes that query to the storage, which replaces the stored document in the Redis and Azure Blob Storage implementations.
A query long enough to arrive in more than one buffer segment also takes the hash in its persistedQuery extension as its document ID, so the same applies to requests without an id.
An application is affected when it serves GraphQL over HTTP or WebSockets with Hot Chocolate 16: an endpoint mapped with MapGraphQL(), MapGraphQLHttp(), or MapGraphQLWebSocket(), a Fusion gateway (AddGraphQLGateway()), or the Azure Functions integration (AddGraphQLFunction(), in-process or isolated worker). The default configuration is affected. Versions before 16.0.0 are not affected.
Fixed Versions
- 16.7.3
Impact
An unauthenticated client can choose the operation that other clients execute when they send a query whose text the attacker knows, which for a web or mobile client is usually public. The operation runs with the identity of the client that sends the query, so a client with more rights than the attacker can be made to run a mutation of the attacker's choice, with arguments the attacker wrote into it. Every client that sends the replaced query receives the result of the replacement until the entry leaves the cache or the process restarts, and their own requests keep it in the cache. The attacker does not see the responses.
The document cache does not replace an existing entry, so for a query that is already cached the attacker first has to push it out of the cache, which holds 256 documents by default (OperationDocumentCacheSize). For a query that is not in constant use, a few hundred requests with new documents are enough. Right after the server starts, none are needed.
With automatic persisted operations, the replacement is written to the operation document storage and survives restarts.
Two effects need no other client:
- An operation above the cost limits of the default security policy can be executed while cost analysis reports the cost of another document.
- In an application that protects types or fields with
ApplyPolicy.Validation, an unauthenticated client can read them with two requests, whichever authorization handler is used.ApplyPolicy.BeforeResolver, the default of[Authorize], andApplyPolicy.AfterResolverare not affected.
This issue is classified as CWE-345: Insufficient Verification of Data Authenticity.
Recommendation
Upgrade to 16.7.3 or later. A document sent with a request is then identified by its hash. A request that sends only a document ID still finds a cached or persisted document by that ID. In-process requests that set both a document ID and a document are cached under the document's hash, except warmup requests (MarkAsWarmupRequest()), which keep the ID they set. There are no new options.
If an immediate upgrade is not possible, refuse requests that send a document together with a document ID that is not the document's hash, before they reach the document cache:
builder.Services
.AddGraphQLServer()
.UseRequest(
next => context =>
{
var request = context.Request;
if (request.Document is not null
&& !request.DocumentId.IsEmpty
&& !request.DocumentId.Value.Equals(
request.DocumentHash.Value,
StringComparison.Ordinal)
&& !context.IsWarmupRequest())
{
context.Result = OperationResult.FromError(
ErrorBuilder.New()
.SetMessage("A document ID cannot be sent together with a document.")
.Build());
return default;
}
return next(context);
},
key: "RejectDocumentIdWithDocument",
before: WellKnownRequestMiddleware.DocumentCacheMiddleware);A request that sends a query without an id is not refused, because the server uses the hash of the query as its ID. A client that sends an id other than the query's hash together with the query receives the error instead of a result. On a Fusion gateway, call UseRequest on the builder returned by AddGraphQLGateway(). With Azure Functions, call it on the request executor builder that configures the function's schema.