Type-Level Authorization Bypass via Union Types
Description
A type-level @authorize directive, or an [Authorize] attribute on a class, makes Hot Chocolate authorize every field that returns the protected object type. Fields that return certain unions that contain the protected type got no authorization. Their resolvers ran for any caller and returned every field of the protected type.
This applies to the following unions:
- Unions declared in a schema document, such as
union SearchResult = Invoice | Customer, including members added withextend union. This also applies when the member types are defined in C#. - Unions built with
IUnionTypeDescriptor.Type(NamedTypeNode). - The result unions that
AddQueryConventions()creates for resolvers with[Error]attributes or aFieldResult<...>return type (14.0.0 and later).
Unions declared with descriptor.Type<TObjectType>(), descriptor.Type(objectType) or a [UnionType] marker interface are not affected. Interfaces are not affected.
An application is affected when all of the following hold:
- An object type is protected by a type-level
@authorizedirective or[Authorize]attribute that usesapply: BEFORE_RESOLVER(the default) orapply: AFTER_RESOLVER. A directive withapply: VALIDATIONis enforced. In 13.0.x the default wasVALIDATION, so those versions are affected only whenBEFORE_RESOLVERorAFTER_RESOLVERis set explicitly. - A field returns that object type through one of the unions listed above, as the union itself or as a list of it.
- That field has no field-level
@authorizeor[Authorize]of its own that requires the same authorization.
No option beyond AddAuthorization() is needed. A schema document with such a union is affected in the default configuration.
Fixed Versions
- 16.7.3
- 15.1.20
- 14.3.4
- 13.9.19
Impact
A caller that does not meet the protected type's requirements can read every field of that type through a field that returns an affected union. With a plain @authorize, an anonymous caller is enough. With roles or a policy, an authenticated caller without them is enough. The attacker needs to know the union-typed field, which introspection or a client usually reveals.
The resolver of the union-typed field also runs for that caller, so a mutation that returns an affected union executes.
Field-level authorization and type-level directives with apply: VALIDATION are still enforced. Fields of the protected type that return other protected types still apply those types' authorization.
This issue is classified as CWE-863: Incorrect Authorization.
Recommendation
Upgrade to one of the fixed releases. A field that returns a union is then authorized with the requirements of every protected member type before its resolver runs, however the union refers to its members. This is how unions declared with descriptor.Type<TObjectType>() already behave. As a result, such a field is refused for a caller that does not meet those requirements even when it would return an unprotected member, for example the error member of a query conventions result union. When several members are protected, the caller must meet the requirements of all of them.
If an immediate upgrade is not possible, add authorization with the same requirements to every field that returns an affected union. In a schema document:
type Query {
search(term: String!): SearchResult @authorize(policy: "Invoices")
}On a resolver that uses query conventions:
[Authorize(Policy = "Invoices")]
[Error(typeof(InvoiceNotFoundException))]
public Invoice GetInvoice(int id) => ...;Alternatively, declare the union in C# with descriptor.Type<TObjectType>() for each member.
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.0.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.0.0 · fixed in 13.9.19