Skip to content

Commit 24c3ec3

Browse files
authored
Merge pull request #184953 from dlepow/gqlpol
[APIM] Update to GraphQL validation
2 parents dd1696e + e5ba283 commit 24c3ec3

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed

articles/api-management/graphql-validation-policies.md

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ documentationcenter: ''
66
author: dlepow
77
ms.service: api-management
88
ms.topic: article
9-
ms.date: 10/21/2021
9+
ms.date: 01/21/2022
1010
ms.author: danlep
1111
ms.custom: ignite-fall-2021
1212
---
1313

1414
# API Management policy to validate and authorize GraphQL requests (preview)
1515

16-
This article provides a reference for a new API Management policy to validate and authorize requests to a [GraphQL API](graphql-api.md) imported to API Management.
16+
This article provides a reference for an API Management policy to validate and authorize requests to a [GraphQL API](graphql-api.md) imported to API Management.
1717

1818
For more information on adding and configuring policies, see [Policies in API Management](./api-management-policies.md).
1919

@@ -40,34 +40,51 @@ Because GraphQL queries use a flattened schema:
4040
* Interfaces
4141
* The schema element
4242

43-
**Authorization elements**
44-
You can use multiple authorization elements. The most specific path is used to select the appropriate authorization rule for each leaf node in the query.
45-
* Each authorization can optionally provide a different action.
46-
* `if` clauses allow the admin to specify conditional actions.
43+
**Authorize element**
44+
Configure the `authorize` element to set an appropriate authorization rule for one or more paths.
45+
* Each rule can optionally provide a different action.
46+
* Use policy expressions to specify conditional actions.
4747

4848
**Introspection system**
4949
The policy for path=`/__*` is the [introspection](https://graphql.org/learn/introspection/) system. You can use it to reject introspection requests (`__schema`, `__type`, etc.).
5050

5151
### Policy statement
5252

5353
```xml
54-
<validate-graphql-request error-variable-name="variable name" max-size="size in bytes" max-depth="query depth">
55-
<authorize-path="query path, for example: /Query/list Users or /__*" action="allow|remove|reject" />
56-
<if condition="policy expression" action="allow|remove|reject" />
54+
<validate-graphql-request error-variable-name="variable name" max-size="size in bytes" max-depth="query depth">
55+
<authorize>
56+
<rule path="query path, for example: '/listUsers' or '/__*'" action="string or policy expression that evaluates to 'allow|remove|reject|ignore'" />
57+
</authorize>
5758
</validate-graphql-request>
5859
```
5960

60-
### Example
61+
### Example: Query validation
6162

62-
In the following example, we validate a GraphQL query and reject:
63-
* Requests larger than 100 kb or with query depth greater than 4.
64-
* Access to the introspection system and the `list Users` query.
63+
This example applies the following validation and authorization rules to a GraphQL query:
64+
* Requests larger than 100 kb or with query depth greater than 4 are rejected.
65+
* Requests to the introspection system are rejected.
66+
* The `/Missions/name` field is removed from requests containing more than two headers.
6567

6668
```xml
6769
<validate-graphql-request error-variable-name="name" max-size="102400" max-depth="4">
68-
<authorize path="/" action="allow" />
69-
<authorize path="/__*" action="reject" />
70-
<authorize path="/Query/list Users" action="reject" />
70+
<authorize>
71+
<rule path="/__*" action="reject" />
72+
<rule path="/Missions/name" action="@(context.Request.Headers.Count > 2 ? "remove" : "allow")" />
73+
</authorize>
74+
</validate-graphql-request>
75+
```
76+
77+
### Example: Mutation validation
78+
79+
This example applies the following validation and authorization rules to a GraphQL mutation:
80+
* Requests larger than 100 kb or with query depth greater than 4 are rejected.
81+
* Requests to mutate the `deleteUser` field are denied except when the request is from IP address `198.51.100.1`.
82+
83+
```xml
84+
<validate-graphql-request error-variable-name="name" max-size="102400" max-depth="4">
85+
<authorize>
86+
<rule path="/Mutation/deleteUser" action="@(context.Request.IpAddress <> "198.51.100.1" ? "deny" : "allow")" />
87+
</authorize>
7188
</validate-graphql-request>
7289
```
7390

@@ -76,8 +93,8 @@ In the following example, we validate a GraphQL query and reject:
7693
| Name | Description | Required |
7794
| ------------ | --------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
7895
| `validate-graphql-request` | Root element. | Yes |
79-
| `authorize` | Add one or more of these elements to provides field-level authorization with both request- and field-level errors. | Yes |
80-
| `if` | Add one or more of these elements for conditional changes to the action for a field-level authorization. | No |
96+
| `authorize` | Add this element to provide field-level authorization with both request- and field-level errors. | No |
97+
| `rule` | Add one or more of these elements to authorize specific query paths. Each rule can optionally specify a different [action](#request-actions). | No |
8198

8299
### Attributes
83100

@@ -86,19 +103,19 @@ In the following example, we validate a GraphQL query and reject:
86103
| `error-variable-name` | Name of the variable in `context.Variables` to log validation errors to. | No | N/A |
87104
| `max-size` | Maximum size of the request payload in bytes. Maximum allowed value: 102,400 bytes (100 KB). (Contact [support](https://azure.microsoft.com/support/options/) if you need to increase this limit.) | Yes | N/A |
88105
| `max-depth` | An integer. Maximum query depth. | No | 6 |
89-
| `path` | Query path to execute authorization validation on. | Yes | N/A |
90-
| `action` | [Action](#request-actions) to perform for the matching field. May be changed if a matching condition is specified. | Yes | N/A |
91-
| `condition` | Boolean value that determines if the [policy expression](api-management-policy-expressions.md) matches. The first matching condition is used. | No | N/A |
106+
| `path` | Path to execute authorization validation on. It must follow the pattern: `/type/field`. | Yes | N/A |
107+
| `action` | [Action](#request-actions) to perform if the rule applies. May be specified conditionally using a policy expression. | No | allow |
92108

93109
### Request actions
94110

95111
Available actions are described in the following table.
96112

97113
|Action |Description |
98114
|---------|---------|
99-
|`reject` | A request error happens, and the request is not sent to the back end. |
115+
|`reject` | A request error happens, and the request is not sent to the back end. Additional rules if configured are not applied. |
100116
|`remove` | A field error happens, and the field is removed from the request. |
101117
|`allow` | The field is passed to the back end. |
118+
|`ignore` | The rule is not valid for this case and the next rule is applied. |
102119

103120
### Usage
104121

0 commit comments

Comments
 (0)