website/src/docs/hotchocolate/v16/securing-your-api/index.md
Securing a GraphQL API requires more than authentication and authorization. Unlike REST, where each endpoint has a predictable cost, a single GraphQL query can traverse deep relationships and request large datasets. You need a strategy that matches your API's threat model.
Hot Chocolate provides two golden paths depending on whether your API is public or private.
Public APIs face unpredictable clients. You do not control who sends queries or how complex those queries are. An attacker can craft a deeply nested query that consumes significant server resources.
Cost analysis is your primary defense for public APIs. It assigns a weight to each field and list in your schema, then calculates the total cost of an incoming query before executing it. Queries that exceed your cost budget are rejected.
Combine cost analysis with:
Learn more about cost analysis
Private APIs serve known clients that you control, such as your own web or mobile applications. For these APIs, trusted documents (also called persisted operations) provide the strongest security guarantee.
With trusted documents, you extract all GraphQL operations from your client at build time and register them with the server. At runtime, the server only accepts operations it recognizes. This eliminates the risk of arbitrary queries entirely.
Learn more about trusted documents
Regardless of whether your API is public or private, apply these additional protections:
Authentication determines who is making a request. Hot Chocolate integrates with the ASP.NET Core authentication system, supporting JWT, cookies, and other authentication schemes.
Learn more about authentication
Authorization controls what an authenticated user can access. Hot Chocolate provides the @authorize directive for field-level and type-level access control, integrating with ASP.NET Core roles and policies.
Learn more about authorization
Hot Chocolate enforces limits at every stage of request processing -- parsing, validation, and execution -- to keep resource consumption bounded. This includes limits on fields, directives, nesting depth, execution depth, timeouts, and more.
Learn more about request limits
Introspection powers developer tools but can also reveal your schema to attackers. You can restrict or disable introspection in production.
Learn more about introspection
Hot Chocolate uses MD5 for document hashing by default. If you need FIPS compliance, switch to SHA256:
// Program.cs
builder
.AddGraphQL()
.AddSha256DocumentHashProvider();
Learn more about hashing providers