---
description: >-
  GraphQL is a query language for APIs and a runtime for fulfilling those
  queries with existing data.
---

# GRAPHQL

{% embed url="https://portswigger.net/web-security/graphql" %}

{% embed url="https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/GraphQL%20Injection" %}

**END POINTS:**

* Introspection feature left active
* GraphQL APIs use the same endpoint for all requests. If these common endpoints don't return a GraphQL response, you could also try appending /v1 to the path.&#x20;

```
/graphql 
/api 
/api/graphql 
/graphql/api 
/graphql/graphql
```

* If you can't find the GraphQL endpoint by sending POST requests to common endpoints, try resending the universal query (query{\_\_typename}) using alternative HTTP methods.
* Even if introspection is entirely disabled, you can sometimes use suggestions to glean information on an API's structure in error messages (Clairvoyance).



**VULNERABILITIES:**

* Information disclosure
* Admin privileges
* CSRF
* Access control vulnerabilities: insecure direct object reference (IDOR)



**TOOLS:**

* Burp Suite's Vulnerability Scanner

{% embed url="https://portswigger.net/burp/vulnerability-scanner" %}

* Burp Suite's InQL extension.

{% embed url="https://portswigger.net/burp/documentation/desktop/testing-workflow/session-management/working-with-graphql" %}

* Clairvoyance

{% embed url="https://github.com/nikitastupin/clairvoyance" %}

* GraphQL Visualizer

{% embed url="http://nathanrandal.com/graphql-visualizer/" %}

## &#x20;                                         What is GraphQL?

GraphQL is an API query language that is designed to facilitate efficient communication between clients and servers. It enables the user to specify exactly what data they want in the response, helping to avoid the large response objects and multiple calls that can sometimes be seen with REST APIs.

GraphQL services define a contract through which a client can communicate with a server. The client doesn't need to know where the data resides. Instead, clients send queries to a GraphQL server, which fetches data from the relevant places. As GraphQL is platform-agnostic, it can be implemented with a wide range of programming languages and can be used to communicate with virtually any data store.

### &#x20;                                                    How GraphQL works <a href="#how-graphql-works" id="how-graphql-works"></a>

GraphQL schemas define the structure of the service's data, listing the available objects (known as types), fields, and relationships.

The data described by a GraphQL schema can be manipulated using three types of operation:

* Queries fetch data.
* Mutations add, change, or remove data.
* Subscriptions are similar to queries, but set up a permanent connection by which a server can proactively push data to a client in the specified format.

All GraphQL operations use the same endpoint, and are generally sent as a POST request. This is significantly different to REST APIs, which use operation-specific endpoints across a range of HTTP methods. With GraphQL, the type and name of the operation define how the query is handled, rather than the endpoint it is sent to or the HTTP method used.

GraphQL services generally respond to operations with a JSON object in the structure requested.

### &#x20;                                           What is a GraphQL schema? <a href="#what-is-a-graphql-schema" id="what-is-a-graphql-schema"></a>

In GraphQL, the schema represents a contract between the frontend and backend of the service. It defines the data available as a series of types, using a human-readable schema definition language. These types can then be implemented by a service.

Most of the types defined are object types. which define the objects available and the fields and arguments they have. Each field has its own type, which can either be another object or a scalar, enum, union, interface, or custom type.

The example below shows a simple schema definition for a Product type. The `!` operator indicates that the field is non-nullable when called (that is, mandatory).

```
    #Example schema definition

    type Product {
        id: ID!
        name: String!
        description: String!
        price: Int
    }
```

Schemas must also include at least one available query. Usually, they also contain details of available mutations.

### &#x20;                                          What are GraphQL queries? <a href="#what-are-graphql-queries" id="what-are-graphql-queries"></a>

GraphQL queries retrieve data from the data store. They are roughly equivalent to GET requests in a REST API.

Queries usually have the following key components:

* A `query` operation type. This is technically optional but encouraged, as it explicitly tells the server that the incoming request is a query.
* A query name. This can be anything you want. The query name is optional, but encouraged as it can help with debugging.
* A data structure. This is the data that the query should return.
* Optionally, one or more arguments. These are used to create queries that return details of a specific object (for example "give me the name and description of the product that has the ID 123").

The example below shows a query called `myGetProductQuery` that requests the name, and description fields of a product with the `id` of `123`.

```
    #Example query

    query myGetProductQuery {
        getProduct(id: 123) {
            name
            description
        }
    }
```

Note that the product type may contain more fields in the schema than those requested here. The ability to request only the data you need is a significant part of the flexibility of GraphQL.

### &#x20;                                      What are GraphQL mutations? <a href="#what-are-graphql-mutations" id="what-are-graphql-mutations"></a>

Mutations change data in some way, either adding, deleting, or editing it. They are roughly equivalent to a REST API's POST, PUT, and DELETE methods.

Like queries, mutations have an operation type, name, and structure for the returned data. However, mutations always take an input of some type. This can be an inline value, but in practice is generally provided as a variable.

The example below shows a mutation to create a new product and its associated response. In this case, the service is configured to automatically assign an ID to new products, which has been returned as requested.

```
    #Example mutation request

    mutation {
        createProduct(name: "Flamin' Cocktail Glasses", listed: "yes") {
            id
            name
            listed
        }
    }
```

```
    #Example mutation response

    {
        "data": {
            "createProduct": {
                "id": 123,
                "name": "Flamin' Cocktail Glasses",
                "listed": "yes"
            }
        }
    }
```

### &#x20;                               Components of queries and mutations <a href="#components-of-queries-and-mutations" id="components-of-queries-and-mutations"></a>

The GraphQL syntax includes several common components for queries and mutations.

#### Fields <a href="#fields" id="fields"></a>

All GraphQL types contain items of queryable data called fields. When you send a query or mutation, you specify which of the fields you want the API to return. The response mirrors the content specified in the request.

The example below shows a query to get ID and name details for all employees, and its associated response. In this case, `id`, `name.firstname`, and `name.lastname` are the fields requested.

```
    #Request

    query myGetEmployeeQuery {
        getEmployees {
            id
            name {
                firstname
                lastname
            }
        }
    }
```

```
    #Response

    {
        "data": {
            "getEmployees": [
                {
                    "id": 1,
                    "name" {
                        "firstname": "Carlos",
                        "lastname": "Montoya"
                    }
                },
                {
                    "id": 2,
                    "name" {
                        "firstname": "Peter",
                        "lastname": "Wiener"
                    }
                }
            ]
        }
    }
```

#### Arguments <a href="#arguments" id="arguments"></a>

Arguments are values that are provided for specific fields. The arguments that can be accepted for a type are defined in the schema.

When you send a query or mutation that contains arguments, the GraphQL server determines how to respond based on its configuration. For example, it might return a specific object rather than details of all objects.

The example below shows a `getEmployee` request that takes an employee ID as an argument. In this case, the server responds with only the details of the employee who matches that ID.

```
    #Example query with arguments

    query myGetEmployeeQuery {
        getEmployees(id:1) {
            name {
                firstname
                lastname
            }
        }
    }
```

```
    #Response to query

    {
        "data": {
            "getEmployees": [
            {
                "name" {
                    "firstname": Carlos,
                    "lastname": Montoya
                    }
                }
            ]
        }
    }
```

**Note**

If user-supplied arguments are used to access objects directly then a GraphQL API can be vulnerable to [access control](https://portswigger.net/web-security/access-control) vulnerabilities such as [insecure direct object references](https://portswigger.net/web-security/access-control/idor) ([IDOR](https://portswigger.net/web-security/access-control/idor)).

#### Variables <a href="#variables" id="variables"></a>

Variables enable you to pass dynamic arguments, rather than having arguments directly within the query itself.

Variable-based queries use the same structure as queries using inline arguments, but certain aspects of the query are taken from a separate JSON-based variables dictionary. They enable you to reuse a common structure among multiple queries, with only the value of the variable itself changing.

When building a query or mutation that uses variables, you need to:

* Declare the variable and type.
* Add the variable name in the appropriate place in the query.
* Pass the variable key and value from the variable dictionary.

The example below shows the same query as in the previous example, but with the ID passed as a variable instead of as a direct part of the query string.

```
    #Example query with variable

    query getEmployeeWithVariable($id: ID!) {
        getEmployees(id:$id) {
            name {
                firstname
                lastname
            }
         }
    }

    Variables:
    {
        "id": 1
    }
```

In this example, the variable is declared in the first line with (`$id: ID!`). The `!` indicates that this is a required field for this query. It is then used as an argument in the second line with (`id:$id`). Finally, the value of the variable itself is set in the variable JSON dictionary. For information on how to test for these vulnerabilities, see [GraphQL API vulnerabilities](https://portswigger.net/web-security/graphql).

#### Aliases <a href="#aliases" id="aliases"></a>

GraphQL objects can't contain multiple properties with the same name. For example, the following query is invalid because it tries to return the `product` type twice.

```
    #Invalid query

    query getProductDetails {
        getProduct(id: 1) {
            id
            name
        }
        getProduct(id: 2) {
            id
            name
        }
    }
```

Aliases enable you to bypass this restriction by explicitly naming the properties you want the API to return. You can use aliases to return multiple instances of the same type of object in one request. This helps to reduce the number of API calls needed.

In the example below, the query uses aliases to specify a unique name for both products. This query now passes validation, and the details are returned.

```
    #Valid query using aliases

    query getProductDetails {
        product1: getProduct(id: "1") {
            id
            name
        }
        product2: getProduct(id: "2") {
            id
            name
        }
    }
```

```
    #Response to query

    {
        "data": {
            "product1": {
                "id": 1,
                "name": "Juice Extractor"
             },
            "product2": {
                "id": 2,
                "name": "Fruit Overlays"
            }
        }
    }
```

**Note**

Using aliases with mutations effectively enables you to send multiple GraphQL messages in one HTTP request.

For more information on how to use this technique to bypass some rate limit controls, see [Bypassing rate limiting using aliases](https://portswigger.net/web-security/graphql#bypassing-rate-limiting-using-aliases).



#### Fragments <a href="#fragments" id="fragments"></a>

Fragments are reusable parts of queries or mutations. They contain a subset of the fields belonging to the associated type.

Once defined, they can be included in queries or mutations. If they are subsequently changed, the change is included in every query or mutation that calls the fragment.

The example below shows a `getProduct` query in which the details of the product are contained in a `productInfo` fragment.

```
    #Example fragment

    fragment productInfo on Product {
        id
        name
        listed
    }
```

```
    #Query calling the fragment

    query {
        getProduct(id: 1) {
            ...productInfo
            stock
        }
    }
```

```
    #Response including fragment fields

    {
        "data": {
            "getProduct": {
                "id": 1,
                "name": "Juice Extractor",
                "listed": "no",
                "stock": 5
            }
        }
    }
```

### &#x20;                                                        Subscriptions <a href="#subscriptions" id="subscriptions"></a>

Subscriptions are a special type of query. They enable clients to establish a long-lived connection with a server so that the server can then push real-time updates to the client without the need to continually poll for data. They are primarily useful for small changes to large objects and for functionality that requires small real-time updates (like chat systems or collaborative editing).

As with regular queries and mutations, the subscription request defines the shape of the data to be returned.

Subscriptions are commonly implemented using [WebSockets](https://portswigger.net/web-security/websockets).

### &#x20;                                                      Introspection <a href="#introspection" id="introspection"></a>

Introspection is a built-in GraphQL function that enables you to query a server for information about the schema. It is commonly used by applications such as GraphQL IDEs and documentation generation tools.

Like regular queries, you can specify the fields and structure of the response you want to be returned. For example, you might want the response to only contain the names of available mutations.

Introspection can represent a serious [information disclosure](https://portswigger.net/web-security/information-disclosure) risk, as it can be used to access potentially sensitive information (such as field descriptions) and help an attacker to learn how they can interact with the API. It is best practice for introspection to be disabled in production environments.



&#x20;                                                   **GraphQL API vulnerabilities**

GraphQL vulnerabilities generally arise due to implementation and design flaws. For example, the introspection feature may be left active, enabling attackers to query the API in order to glean information about its schema.

GraphQL attacks usually take the form of malicious requests that can enable an attacker to obtain data or perform unauthorized actions. These attacks can have a severe impact, especially if the user is able to gain admin privileges by manipulating queries or executing a [CSRF exploit](https://portswigger.net/web-security/csrf). Vulnerable GraphQL APIs can also lead to [information disclosure](https://portswigger.net/web-security/information-disclosure) issues.

In this section we'll look at how to test GraphQL APIs. Don't worry if you're not familiar with GraphQL - we'll cover the relevant details as we go. We've also provided some labs so you can practice what you've learned.



&#x20;                                              **Finding GraphQL endpoints**

Before you can test a GraphQL API, you first need to find its endpoint. As GraphQL APIs use the same endpoint for all requests, this is a valuable piece of information.

**Note**

This section explains how to probe for GraphQL endpoints manually. However, [Burp Scanner](https://portswigger.net/burp/vulnerability-scanner) can automatically test for GraphQL endpoints as part of its scans. It raises a "GraphQL endpoint found" issue if any such endpoints are discovered.

#### Universal queries <a href="#universal-queries" id="universal-queries"></a>

If you send `query{__typename}` to any GraphQL endpoint, it will include the string `{"data": {"__typename": "query"}}` somewhere in its response. This is known as a universal query, and is a useful tool in probing whether a URL corresponds to a GraphQL service.

The query works because every GraphQL endpoint has a reserved field called `__typename` that returns the queried object's type as a string.

#### Common endpoint names <a href="#common-endpoint-names" id="common-endpoint-names"></a>

GraphQL services often use similar endpoint suffixes. When testing for GraphQL endpoints, you should look to send universal queries to the following locations:

* `/graphql`
* `/api`
* `/api/graphql`
* `/graphql/api`
* `/graphql/graphql`

If these common endpoints don't return a GraphQL response, you could also try appending `/v1` to the path.

**Note**

GraphQL services will often respond to any non-GraphQL request with a "query not present" or similar error. You should bear this in mind when testing for GraphQL endpoints.

#### &#x20;                                                                  Request methods <a href="#request-methods" id="request-methods"></a>

The next step in trying to find GraphQL endpoints is to test using different request methods.

It is best practice for production GraphQL endpoints to only accept POST requests that have a content-type of `application/json`, as this helps to protect against CSRF vulnerabilities. However, some endpoints may accept alternative methods, such as GET requests or POST requests that use a content-type of `x-www-form-urlencoded`.

If you can't find the GraphQL endpoint by sending POST requests to common endpoints, try resending the universal query using alternative HTTP methods.

#### Initial testing <a href="#initial-testing" id="initial-testing"></a>

Once you have discovered the endpoint, you can send some test requests to understand a little more about how it works. If the endpoint is powering a website, try exploring the web interface in Burp's browser and use the HTTP history to examine the queries that are sent.



&#x20;                                    **Exploiting unsanitized arguments**

At this point, you can start to look for vulnerabilities. Testing query arguments is a good place to start.

If the API uses arguments to access objects directly, it may be vulnerable to [access control](https://portswigger.net/web-security/access-control) vulnerabilities. A user could potentially access information they should not have simply by supplying an argument that corresponds to that information. This is sometimes known as an insecure direct object reference (IDOR).

**More information**

* For a general explanation of GraphQL arguments, see [Arguments](https://portswigger.net/web-security/graphql/what-is-graphql#arguments).
* For further information on IDORs, see [Insecure direct object references (IDOR)](https://portswigger.net/web-security/access-control/idor).

For example, the query below requests a product list for an online shop:

```
    #Example product query

    query {
        products {
            id
            name
            listed
        }
    }
```

The product list returned contains only listed products.

```
    #Example product response

    {
        "data": {
            "products": [
                {
                    "id": 1,
                    "name": "Product 1",
                    "listed": true
                },
                {
                    "id": 2,
                    "name": "Product 2",
                    "listed": true
                },
                {
                    "id": 4,
                    "name": "Product 4",
                    "listed": true
                }
            ]
        }
    }
```

From this information, we can infer the following:

* Products are assigned a sequential ID.
* Product ID 3 is missing from the list, possibly because it has been delisted.

By querying the ID of the missing product, we can get its details, even though it is not listed on the shop and was not returned by the original product query.

```
    #Query to get missing product

    query {
        product(id: 3) {
            id
            name
            listed
        }
    }
```

```
    #Missing product response

    {
        "data": {
            "product": {
            "id": 3,
            "name": "Product 3",
            "listed": no
            }
        }
    }
```



&#x20;                                             **Discovering schema information**

The next step in testing the API is to piece together information about the underlying schema.

The best way to do this is to use introspection queries. Introspection is a built-in GraphQL function that enables you to query a server for information about the schema.

Introspection helps you to understand how you can interact with a GraphQL API. It can also disclose potentially sensitive data, such as description fields.

#### Using introspection <a href="#using-introspection" id="using-introspection"></a>

To use introspection to discover schema information, query the `__schema` field. This field is available on the root type of all queries.

Like regular queries, you can specify the fields and structure of the response you want to be returned when running an introspection query. For example, you might want the response to contain only the names of available mutations.

#### Probing for introspection <a href="#probing-for-introspection" id="probing-for-introspection"></a>

It is best practice for introspection to be disabled in production environments, but this advice is not always followed.

You can probe for introspection using the following simple query. If introspection is enabled, the response returns the names of all available queries.

```
    #Introspection probe request

    {
        "query": "{__schema{queryType{name}}}"
    }
```

**Note**

Burp Scanner can automatically test for introspection during its scans. If it finds that introspection is enabled, it reports a "GraphQL introspection enabled" issue.

#### Running a full introspection query <a href="#running-a-full-introspection-query" id="running-a-full-introspection-query"></a>

The next step is to run a full introspection query against the endpoint so that you can get as much information on the underlying schema as possible.

The example query below returns full details on all queries, mutations, subscriptions, types, and fragments.

```
    #Full introspection query

    query IntrospectionQuery {
        __schema {
            queryType {
                name
            }
            mutationType {
                name
            }
            subscriptionType {
                name
            }
            types {
             ...FullType
            }
            directives {
                name
                description
                args {
                    ...InputValue
            }
            onOperation  #Often needs to be deleted to run query
            onFragment   #Often needs to be deleted to run query
            onField      #Often needs to be deleted to run query
            }
        }
    }

    fragment FullType on __Type {
        kind
        name
        description
        fields(includeDeprecated: true) {
            name
            description
            args {
                ...InputValue
            }
            type {
                ...TypeRef
            }
            isDeprecated
            deprecationReason
        }
        inputFields {
            ...InputValue
        }
        interfaces {
            ...TypeRef
        }
        enumValues(includeDeprecated: true) {
            name
            description
            isDeprecated
            deprecationReason
        }
        possibleTypes {
            ...TypeRef
        }
    }

    fragment InputValue on __InputValue {
        name
        description
        type {
            ...TypeRef
        }
        defaultValue
    }

    fragment TypeRef on __Type {
        kind
        name
        ofType {
            kind
            name
            ofType {
                kind
                name
                ofType {
                    kind
                    name
                }
            }
        }
    }
```

**Note**

If introspection is enabled but the above query doesn't run, try removing the `onOperation`, `onFragment`, and `onField` directives from the query structure. Many endpoints do not accept these directives as part of an introspection query, and you can often have more success with introspection by removing them.

#### Visualizing introspection results <a href="#visualizing-introspection-results" id="visualizing-introspection-results"></a>

Responses to introspection queries can be full of information, but are often very long and hard to process.

You can view relationships between schema entities more easily using a [GraphQL visualizer](http://nathanrandal.com/graphql-visualizer/). This is an online tool that takes the results of an introspection query and produces a visual representation of the returned data, including the relationships between operations and types.

#### Using InQL <a href="#using-inql" id="using-inql"></a>

As an alternative to running an introspection query manually and visualizing the results, you can use Burp Suite's InQL extension.

InQL is a Burp Suite extension that helps you to audit GraphQL APIs securely. When you pass a URL to it (either by providing a live endpoint link or by uploading a JSON file), it issues an introspection query requesting all queries and mutations, and presents a structured view to make it easy to explore the results.

**More information**

For more information on using InQL in Burp Suite, see [Working with GraphQL in Burp Suite](https://portswigger.net/burp/documentation/desktop/testing-workflow/session-management/working-with-graphql).

#### Suggestions <a href="#suggestions" id="suggestions"></a>

Even if introspection is entirely disabled, you can sometimes use suggestions to glean information on an API's structure.

Suggestions are a feature of the Apollo GraphQL platform in which the server can suggest query amendments in error messages. These are generally used where a query is slightly incorrect but still recognizable (for example, `There is no entry for 'productInfo'. Did you mean 'productInformation' instead?`).

You can potentially glean useful information from this, as the response is effectively giving away valid parts of the schema.

[Clairvoyance](https://github.com/nikitastupin/clairvoyance) is a tool that uses suggestions to automatically recover all or part of a GraphQL schema, even when introspection is disabled. This makes it significantly less time consuming to piece together information from suggestion responses.

You cannot disable suggestions directly in Apollo. See [this GitHub thread](https://github.com/apollographql/apollo-server/issues/3919#issuecomment-836503305) for a workaround.

**Note**

Burp Scanner can automatically test for suggestions as part of its scans. If active suggestions are found, Burp Scanner reports a "GraphQL suggestions enabled" issue.

&#x20;                                       **Lab: Accessing private GraphQL posts**

{% embed url="https://portswigger.net/web-security/graphql/lab-graphql-reading-private-posts" %}

&#x20;                               **Lab: Accidental exposure of private GraphQL fields**

{% embed url="https://portswigger.net/web-security/graphql/lab-graphql-accidental-field-exposure" %}



&#x20;                                  **Bypassing GraphQL introspection defences**

If you cannot get introspection queries to run for the API you are testing, try inserting a special character after the `__schema` keyword.

When developers disable introspection, they could use a regex to exclude the `__schema` keyword in queries. You should try characters like spaces, new lines and commas, as they are ignored by GraphQL but not by flawed regex.

As such, if the developer has only excluded `__schema{`, then the below introspection query would not be excluded.

```
    #Introspection query with newline

    {
        "query": "query{__schema
        {queryType{name}}}"
    }
```

If this doesn't work, try running the probe over an alternative request method, as introspection may only be disabled over POST. Try a GET request, or a POST request with a content-type of `x-www-form-urlencoded`.

The example below shows an introspection probe sent via GET, with URL-encoded parameters.

```
If this doesn't work, try running the probe over an alternative request method, as introspection may only be disabled over POST. Try a GET request, or a POST request with a content-type of x-www-form-urlencoded.
The example below shows an introspection probe sent via GET, with URL-encoded parameters.
```

**Note**

If an endpoint will only accept introspection queries over GET and you want to analyze the results of the query using InQL Scanner, you first need to save the query results to a file. You can then load this file into InQL, where it will be parsed as normal.

&#x20;                                          **Lab: Finding a hidden GraphQL endpoint**

{% embed url="https://portswigger.net/web-security/graphql/lab-graphql-find-the-endpoint" %}

&#x20;                                            **Bypassing rate limiting using aliases**

Ordinarily, GraphQL objects can't contain multiple properties with the same name. Aliases enable you to bypass this restriction by explicitly naming the properties you want the API to return. You can use aliases to return multiple instances of the same type of object in one request.

**More information**

For more information on GraphQL aliases, see [Aliases](https://portswigger.net/web-security/graphql/what-is-graphql#aliases).

While aliases are intended to limit the number of API calls you need to make, they can also be used to brute force a GraphQL endpoint.

Many endpoints will have some sort of rate limiter in place to prevent brute force attacks. Some rate limiters work based on the number of HTTP requests received rather than the number of operations performed on the endpoint. Because aliases effectively enable you to send multiple queries in a single HTTP message, they can bypass this restriction.

The simplified example below shows a series of aliased queries checking whether store discount codes are valid. This operation could potentially bypass rate limiting as it is a single HTTP request, even though it could potentially be used to check a vast number of discount codes at once.

```
    #Request with aliased queries

    query isValidDiscount($code: Int) {
        isvalidDiscount(code:$code){
            valid
        }
        isValidDiscount2:isValidDiscount(code:$code){
            valid
        }
        isValidDiscount3:isValidDiscount(code:$code){
            valid
        }
    }
```

&#x20;                               **Lab: Bypassing GraphQL brute force protections**

{% embed url="https://portswigger.net/web-security/graphql/lab-graphql-brute-force-protection-bypass" %}



&#x20;                                                      **GraphQL CSRF**

Cross-site request forgery (CSRF) vulnerabilities enable an attacker to induce users to perform actions that they do not intend to perform. This is done by creating a malicious website that forges a cross-domain request to the vulnerable application.

**More information**

For more information on CSRF vulnerabilities in general, see the [CSRF academy topic](https://portswigger.net/web-security/csrf).

GraphQL can be used as a vector for CSRF attacks, whereby an attacker creates an exploit that causes a victim's browser to send a malicious query as the victim user.

#### How do CSRF over GraphQL vulnerabilities arise? <a href="#how-do-csrf-over-graphql-vulnerabilities-arise" id="how-do-csrf-over-graphql-vulnerabilities-arise"></a>

CSRF vulnerabilities can arise where a GraphQL endpoint does not validate the content type of the requests sent to it and no CSRF tokens are implemented.

POST requests that use a content type of `application/json` are secure against forgery as long as the content type is validated. In this case, an attacker wouldn't be able to make the victim's browser send this request even if the victim were to visit a malicious site.

However, alternative methods such as GET, or any request that has a content type of `x-www-form-urlencoded`, can be sent by a browser and so may leave users vulnerable to attack if the endpoint accepts these requests. Where this is the case, attackers may be able to craft exploits to send malicious requests to the API.

The steps to construct a [CSRF attack](https://portswigger.net/web-security/csrf) and deliver an exploit are the same for GraphQL-based CSRF vulnerabilities as they are for "regular" CSRF vulnerabilities. For more information on this process, see [How to construct a CSRF attack](https://portswigger.net/web-security/csrf#how-to-construct-a-csrf-attack).

&#x20;                                **Lab: Performing CSRF exploits over GraphQL**

{% embed url="https://portswigger.net/web-security/graphql/lab-graphql-csrf-via-graphql-api" %}

### &#x20;                                      Preventing GraphQL attacks <a href="#preventing-graphql-attacks" id="preventing-graphql-attacks"></a>

To prevent many common GraphQL attacks, take the following steps when you deploy your API to production:

*   If your API is not intended for use by the general public, disable introspection on it. This makes it harder for an attacker to gain information about how the API works, and reduces the risk of unwanted information disclosure.

    For information on how to disable introspection in the Apollo GraphQL platform, see [this blog post](https://www.apollographql.com/blog/graphql/security/why-you-should-disable-graphql-introspection-in-production/#turning-off-introspection-in-production).
* If your API is intended for use by the general public then you will likely need to leave introspection enabled. However, you should review the API's schema to make sure that it does not expose unintended fields to the public.
*   Make sure that suggestions are disabled. This prevents attackers from being able to use Clairvoyance or similar tools to glean information about the underlying schema.

    You cannot disable suggestions directly in Apollo. See [this GitHub thread](https://github.com/apollographql/apollo-server/issues/3919#issuecomment-836503305) for a workaround.
* Make sure that your API's schema does not expose any private user fields, such as email addresses or user IDs.

#### Preventing GraphQL brute force attacks <a href="#preventing-graphql-brute-force-attacks" id="preventing-graphql-brute-force-attacks"></a>

It is sometimes possible to bypass standard rate limiting when using GraphQL APIs. For an example of this, see the [Bypassing rate limiting using aliases](https://portswigger.net/web-security/graphql#bypassing-rate-limiting-using-aliases) section.

With this in mind, there are design steps that you can take to defend your API against brute force attacks. This generally involves restricting the complexity of queries accepted by the API, and reducing the opportunity for attackers to execute denial-of-service (DoS) attacks.

To defend against brute force attacks:

* Limit the query depth of your API's queries. The term "query depth" refers to the number of levels of nesting within a query. Heavily-nested queries can have significant performance implications, and can potentially provide an opportunity for DoS attacks if they are accepted. By limiting the query depth your API accepts, you can reduce the chances of this happening.
* Configure operation limits. Operation limits enable you to configure the maximum number of unique fields, aliases, and root fields that your API can accept.
* Configure the maximum amount of bytes a query can contain.
* Consider implementing cost analysis on your API. Cost analysis is a process whereby a library application identifies the resource cost associated with running queries as they are received. If a query would be too computationally complex to run, the API drops it.

**More information**

For information on how to implement these features in Apollo, see [this blog post](https://www.apollographql.com/blog/graphql/security/securing-your-graphql-api-from-malicious-queries/).

#### Preventing CSRF over GraphQL <a href="#preventing-csrf-over-graphql" id="preventing-csrf-over-graphql"></a>

To defend against GraphQL CSRF vulnerabilities specifically, make sure of the following when designing your API:

* Your API only accepts queries over JSON-encoded POST.
* The API validates that content provided matches the supplied content type.
* The API has a secure CSRF token mechanism.
