Query Type Authorization Not Applied to the node and nodes Fields
Description
When [Authorize] or @authorize is placed on the Query type with ApplyPolicy.BeforeResolver (the default) or ApplyPolicy.AfterResolver, Hot Chocolate applies the policy to every field of the Query type except node and nodes. For node types whose node resolver is not a field of the Query type, the policy of the Query type is not checked, so a client that is denied on every other query field can read such objects through node and nodes.
An application is affected when all of the following hold:
- It enables global object identification with
AddGlobalObjectIdentification(), which adds thenodeandnodesfields. - It protects queries with
[Authorize]on the Query class,descriptor.Authorize(...)on the Query type, or@authorizeontype Query, usingApplyPolicy.BeforeResolver(the default) orApplyPolicy.AfterResolver. A policy withApplyPolicy.Validationon the Query type is enforced onnodeandnodesand is not affected. - A type that implements
Nodehas a node resolver that is not a field of the Query type. This includes a[NodeResolver]method on the node type or on a type extension of it, a staticGet,GetAsyncorGet<TypeName>method found by convention on a[Node]type,[Node(NodeResolverType = ...)], andImplementsNode().ResolveNode(...)orResolveNodeWith(...)in code-first types. Node types whose resolver is a[NodeResolver]field of the Query type are not affected. - That node type does not carry the same policies itself.
@authorizedirectives on the node type and on its fields are still enforced, so only clients that pass those, and fail the policy of the Query type, gain access.
Fixed Versions
- 16.7.3
- 15.1.20
- 14.3.4
- 13.9.19
Impact
A client that the policy of the Query type denies, including an unauthenticated client when the policy requires authentication, can read every object of an affected node type and every field on it that has no authorization of its own. The client needs the global ID of the object. The node and nodes fields only read data, so the client cannot change data this way.
This issue is classified as CWE-862: Missing Authorization.
Recommendation
Upgrade to one of the fixed releases. The BeforeResolver and AfterResolver policies of the Query type are then evaluated once for every object that node and nodes resolve. A denied node returns null with an authorization error. With nodes, each ID is checked on its own: a denied ID returns null at its position with an error whose path points to that item, and the other IDs are still resolved. An application that relied on node and nodes staying open while the Query type is protected must move the @authorize directive from the Query type to the individual query fields.
If an immediate upgrade is not possible, apply the policies of the Query type to the node and nodes fields with ConfigureNodeFields. For a Query class annotated with [Authorize("RequireEmployee")]:
builder.Services
.AddGraphQLServer()
.AddAuthorization()
.AddGlobalObjectIdentification()
.ModifyAuthorizationOptions(o =>
o.ConfigureNodeFields = d => d.Authorize("RequireEmployee"))
.AddQueryType<Query>();Repeat each policy, role and ApplyPolicy value of the Query type in ConfigureNodeFields. Alternatively, add the same [Authorize] attribute to each affected node type.
Affected versions
- HotChocolate.AuthorizationFrom 16.0.0 · fixed in 16.7.3
- HotChocolate.AuthorizationFrom 15.0.0 · fixed in 15.1.20
- HotChocolate.AuthorizationFrom 14.0.0 · fixed in 14.3.4
- HotChocolate.AuthorizationFrom 13.1.0 · fixed in 13.9.19
- HotChocolate.AspNetCoreFrom 16.0.0 · fixed in 16.7.3
- HotChocolate.AspNetCoreFrom 15.0.0 · fixed in 15.1.20
- HotChocolate.AspNetCoreFrom 14.0.0 · fixed in 14.3.4
- HotChocolate.AspNetCoreFrom 13.1.0 · fixed in 13.9.19