GRAPHQL
GraphQL is a query language for APIs and a runtime for fulfilling those queries with existing data.
Last updated
GraphQL is a query language for APIs and a runtime for fulfilling those queries with existing data.
Last updated
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.
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
Burp Suite's InQL extension.
Clairvoyance
GraphQL Visualizer
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.
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.
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).
Schemas must also include at least one available query. Usually, they also contain details of available mutations.
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
.
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.
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.
The GraphQL syntax includes several common components for queries and mutations.
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.
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.
Note
If user-supplied arguments are used to access objects directly then a GraphQL API can be vulnerable to access control vulnerabilities such as insecure direct object references (IDOR).
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.
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.
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.
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.
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.
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.
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.
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 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.
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. Vulnerable GraphQL APIs can also lead to 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.
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 can automatically test for GraphQL endpoints as part of its scans. It raises a "GraphQL endpoint found" issue if any such endpoints are discovered.
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.
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.
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.
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.
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 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.
For further information on IDORs, see Insecure direct object references (IDOR).
For example, the query below requests a product list for an online shop:
The product list returned contains only listed products.
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.
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.
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.
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.
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.
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.
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.
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. 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.
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.
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 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 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.
Lab: Accessing private GraphQL posts
Lab: Accidental exposure of private GraphQL fields
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.
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.
Lab: Finding a hidden GraphQL endpoint
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.
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.
Lab: Bypassing GraphQL brute force protections
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.
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.
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 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.
Lab: Performing CSRF exploits over GraphQL
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.
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 for a workaround.
Make sure that your API's schema does not expose any private user fields, such as email addresses or user IDs.
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 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.
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.