Authorization Bypass via Source-Generated Node Resolvers
Description
The Hot Chocolate source generator (HotChocolate.Types.Analyzers) ignores the attributes on a node resolver declared in an [ObjectType<T>] class. When such a [NodeResolver] method carries [Authorize], the generated code does not apply it, so the node and nodes fields return the object to any client, including anonymous clients and users without the required role or policy. There is no compiler, analyzer or startup warning. The same [Authorize] on a node resolver in a reflection-based type, such as an [ExtendObjectType<T>] class with [Node], is enforced.
An application is affected when all of the following hold:
- It uses the source generator from
HotChocolate.Types.Analyzersand declares an object type with astatic partialclass marked[ObjectType<T>]. - That class declares a node resolver method marked
[NodeResolver], and the method carries[Authorize](with or without roles or a policy). - It enables global object identification with
AddGlobalObjectIdentification(), so the schema has thenodeandnodesfields. - The object type itself has no authorization, for example no
descriptor.Authorize(...)in itsConfiguremethod. Authorization on the type is applied tonodeandnodesand is not affected.
Other attributes on such a node resolver method, such as custom descriptor attributes that add field middleware, are ignored for node and nodes in the same way. Node resolvers in reflection-based types, node resolvers declared in a [QueryType] class, and fields of the type with their own [Authorize] are not affected.
Fixed Versions
- 16.7.3
- 15.1.20
- 14.3.4
Impact
A client without the authentication, role or policy that the node resolver requires can read objects of the affected type through node and nodes. If the endpoint accepts anonymous requests, no credentials are needed. The client needs the global ID of the object. Fields of the type that have their own authorization stay protected, and the access is read-only.
This issue is classified as CWE-862: Missing Authorization.
Recommendation
Upgrade HotChocolate.Types.Analyzers together with the other Hot Chocolate packages to one of the fixed releases. After upgrading, the attributes of a node resolver in an [ObjectType<T>] class apply to node and nodes, so a client without the required authorization gets an authorization error and a null result for that object.
The fix also changes how a node resolver configured on an ObjectType<T> with ImplementsNode() is completed. In affected versions, a schema failed to build when that node resolver method carried an attribute that adds a directive, such as [Authorize]. Such a schema now builds and the directive applies to node and nodes.
If an immediate upgrade is not possible, add the authorization to the object type in the generated Configure method. This protects node and nodes, and also every other field that returns the type:
[ObjectType<Order>]
public static partial class OrderNode
{
static partial void Configure(IObjectTypeDescriptor<Order> descriptor)
{
descriptor.Authorize("OrdersPolicy");
}
[NodeResolver]
[Authorize("OrdersPolicy")]
public static Task<Order?> GetOrderByIdAsync(int id, OrderService orders)
=> orders.GetByIdAsync(id);
}Use the same policy or roles as the [Authorize] attribute on the node resolver.
Affected versions
- HotChocolate.Types.AnalyzersFrom 16.0.0 · fixed in 16.7.3
- HotChocolate.Types.AnalyzersFrom 15.0.0 · fixed in 15.1.20
- HotChocolate.Types.AnalyzersFrom 14.0.0 · fixed in 14.3.4
- 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