This post is part of a series where I explore the GraphQL related features in Azure API Management.
- Part 1 - Expose an existing GraphQL API
- Part 2 - Expose an existing SOAP or REST API through GraphQL using the 'Synthetic GraphQL' feature
- Part 3 (this post) - Secure your GraphQL API using a GraphQL validation policy
Because of the way that GraphQL is setup, it is typically not sufficient to secure our GraphQL api at the endpoint level. Therefore Azure API management introduces a GraphQL validation policy to secure and protect against GraphQL-specific attacks.
Validate our GraphQL request
Some typical GraphQL specific security issues are:
- Abusing the introspection API of our GraphQL endpoint to explore the full schema. (This is really handy during development but maybe something you don’t want to expose in production)
- Stressing our server by loading too much data in one go, for example trying to fetch the full graph in one request.
Let’s see how we can mitigate some of these issues using Azure API Management.
- Go to your Azure API Management instance in the Azure portal.
- From the side menu, select APIs in the APIs section:
- Select an existing GraphQL api and go to the Design tab:
- In the Inbound processing section, click on + Add policy. Choose Other policies to go to the code editor:
- In the code editor we add a
validate-graphql-request
policy in the inbound section. We configure the policy using the following rules: - We reject requests to the introspection endpoint by setting an
authorize
rule - We limit the amount of data returned in one query by setting the
max-depth
- Click on Save to apply the policy.