Skip to content
CSRF Write-up Intermediate

Exploiting CSRF in GraphQL APIs: Achieving Unauthorized CRUD Operations ( $$ Bounty )

The GraphQL API lacked CSRF tokens and used cookies with SameSite=None, and the server accepted form-encoded requests and GET requests for mutations. An attacker could craft a page that, when visited by a logged-in user, performed CRUD operations such as updating the user's bio.

divyanksitapara088 Follow

Published on medium.com

Cybersecurity

𝐀𝐒𝐔𝐑𝐀, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 6 min read.

Context

The target was an application that made extensive use of GraphQL APIs. The GraphQL endpoint was reachable through paths such as /api/graphql and /graphql. Normal data fetching went through GraphQL queries, while every state-changing feature went through GraphQL mutations: comments, collection management, profile data, user preferences and general CRUD operations through the API were all handled by mutations in the request body.

The feature used for the proof of concept was the profile update, specifically the mutation that modifies the user's bio field. The write-up opens with a short primer on GraphQL: a query language and runtime for APIs, with a single endpoint, a strongly typed schema, introspection and a hierarchical response that mirrors the request. Queries read data without changing server state; mutations create, update or delete data.

Setup

The researcher worked from Burp Suite, using its history and Repeater, and generated the HTML proof-of-concept files with Burp's built-in CSRF PoC generator. The PoC was opened in the same browser where a user was already logged in to the application, so the test relied on a live authenticated session. No account roles, software versions or specific target hostnames are given in the report.

Finding it

The first attempts followed the usual GraphQL playbook: IDOR, access control issues and injection-related vulnerabilities. Strict validation blocked all of them.

The researcher then went back through the Burp Suite history and reviewed the GraphQL requests already captured, both queries and mutations. Two things stood out:

  1. Requests to the GraphQL endpoint carried no CSRF token, neither in headers nor in the body.
  2. Responses to GraphQL requests set the cookie attribute SameSite=None.
cookie attribute From the original on medium.com

The screenshot shows the response cookie attribute set to SameSite=None. With no anti-CSRF token and a cookie that is sent on cross-site requests, any mutation became a candidate for cross-site request forgery.

During reconnaissance the researcher also found an API documentation PDF listing the GraphQL queries and mutations used across the application, mapped to the features they belong to. That document widened the visible attack surface and made it clear which features could be targeted. The report notes that when introspection is enabled, the schema itself provides the same map of types, queries and mutations.

Exploitation

Step 1: capture the mutation

The profile update request that changes the bio field was captured and sent to Burp Repeater. The body was JSON:

Text
{  "query":"\n    mutation UpdateAboutMe($about: String!, $userId: ID)      {\n  updateAboutMe(about: $about, userId: $userId)       {\n    success\n  }\n}\n    ","variables":{"about":"# hello worlds"},    "operationName":"UpdateAboutMe"}

The query field holds the mutation text, variables supplies the about value and the optional userId, and operationName names the operation. The request used Content-Type: application/json. JSON bodies are normally a poor fit for CSRF because a browser will not send a cross-origin request with that content type without a preflight, so the researcher looked for another way to deliver the same body.

From the original on medium.com

Step 2: change the content type

The Content-Type header was changed from application/json to application/x-www-form-urlencoded, and the body was rewritten to match:

Text
query=mutation UpdateAboutMe($about: String!, $userId: ID) {  updateAboutMe(about: $about, userId: $userId) {    success  }}&variables={"about":"# hello worlds"}

The server accepted this body without complaint. That is a content-type handling misconfiguration: the endpoint parsed form-encoded input as readily as JSON, which is exactly the content type a cross-site HTML form can produce.

Due to improper handling of the Content-Type header, the server accepted requests with multiple content types. From the original on medium.com

Step 3: URL-encode and build the PoC

All characters in the body, including the values of the query and variables parameters, were URL-encoded with Burp Suite. GraphQL text contains {}, ", : and spaces, which would otherwise be misread during transmission. A csrf.html PoC was generated with Burp's help and opened in the browser holding the authenticated session.

The first attempt failed: the CSRF attack did not execute.

From the original on medium.com

Step 4: switch POST to GET

To get past the restriction, the researcher changed the HTTP method. Mutations are normally sent with POST; the request was converted to GET, with the parameters moved into the query string:

A final HTML CSRF PoC was generated with Burp Suite and opened in the same authenticated browser. This time the CSRF exploitation was successful.

The behaviour indicated that the server handled authorisation and authentication differently depending on the HTTP method. Moving from POST to GET bypassed the check that had stopped the first attempt and let the payload run.

From the original on medium.com

The screenshots show the successful CSRF exploitation.

Root cause

Three weaknesses combined:

  • No CSRF token on GraphQL requests, in headers or body, so nothing tied a mutation to a page the user actually intended to submit.
  • Session cookies issued with SameSite=None, so the browser attached them to cross-site requests.
  • A content-type handling flaw: the endpoint accepted application/x-www-form-urlencoded bodies, which a plain HTML form can send cross-origin without a preflight. The JSON-only assumption that usually protects GraphQL endpoints did not hold here.

The method-dependent behaviour is the fourth piece. The server applied its authorisation and authentication handling differently for POST and GET, and the GET path did not enforce the check that blocked the first PoC. the exact reason the check was skipped is not shown.

Impact

Because the flaw sat in the GraphQL layer rather than in one feature, every function backed by a GraphQL mutation for CRUD operations was reachable this way: comments, collections, profile data, user preferences and other API operations. An attacker who got a logged-in user to load a crafted page could perform those operations as that user. The report states that manipulating multiple functionalities at once could have severely affected the system's integrity, availability and confidentiality. The issue was reported to the technical team and a bounty was awarded.

Fix

The report does not describe the remediation that was applied. Based on the root cause, the changes that would close it are: enforce an anti-CSRF token on GraphQL requests regardless of content type and method, reject state-changing operations sent with GET, accept only the expected content type on the GraphQL endpoint, and avoid SameSite=None where the application does not genuinely need cross-site cookies.

Variants to test

  1. Content-type switching on GraphQL endpoints. Take a captured mutation and resend it as application/x-www-form-urlencoded with query and variables parameters. A positive result is the server returning the same success response as the JSON version.
  2. Method switching. Resend the same mutation as GET with the parameters URL-encoded in the query string. A positive result is the mutation executing where the POST version was rejected, which points to divergent authorisation paths.
  3. Cookie attribute review. Check SameSite on session cookies set by GraphQL responses. SameSite=None without a compensating token is the precondition for the whole chain.
  4. Token coverage. Compare requests with and without the CSRF token, and across content types and methods, to see whether the token is validated everywhere or only on the paths the frontend uses.
  5. Schema discovery. Look for API documentation and test whether introspection is enabled; either gives the full list of mutations to try against the CSRF conditions above.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 15 min read.

What happened, in plain words

A security researcher was testing a website that uses a special kind of API called GraphQL. He found that the website did not have a common protection against a type of attack called CSRF (Cross-Site Request Forgery). Because of this, an attacker could trick a logged-in user into performing actions on the website without the user knowing, such as changing their profile bio. The researcher proved this by creating a malicious web page that, when opened by a logged-in user, automatically sent requests to the website. He reported the issue and was awarded a bounty.

This article explains the whole story from scratch, assuming you know nothing about security. We will go step by step through what GraphQL is, what CSRF is, how cookies work, and exactly how the researcher pulled off the attack. Every technical term will be explained in plain language, and every request and payload from the original write-up will be shown and explained line by line.

What is an API?

Before we dive into GraphQL, let's understand what an API is. API stands for Application Programming Interface. Think of it as a waiter in a restaurant. You (the customer) don't go into the kitchen to cook your food. Instead, you tell the waiter what you want, the waiter goes to the kitchen, and brings back your order. In the same way, an API is a middleman between your browser (the customer) and the server (the kitchen). Your browser sends a request to the API, the API talks to the server, and the server sends back the data you asked for.

When you use a website or a mobile app, your browser or app is constantly making API calls. For example, when you open your profile page, the app sends an API request to fetch your name, email, and bio. When you update your bio, the app sends another API request to save the new bio.

What is GraphQL?

GraphQL is a specific way of building APIs. It was created by Facebook in 2012 and released publicly in 2015. Unlike traditional REST APIs, which have many different endpoints (URLs) for different things, GraphQL has a single endpoint. An endpoint is just a URL where the API lives, like /api/graphql or /graphql.

With GraphQL, the client (your browser) sends a query that describes exactly what data it wants. The server responds with exactly that data, no more and no less. This is different from REST, where the server decides what data to send back, and you often get more data than you need (over-fetching) or less data than you need (under-fetching).

GraphQL is also strongly typed, meaning every piece of data has a defined type (like String, Integer, Boolean). The entire structure of the API is defined in something called a schema. The schema is like a blueprint that lists all the types, queries, and mutations the API supports. GraphQL also supports introspection, which means you can ask the API to tell you about its own schema. This is useful for developers but can also be useful for attackers.

GraphQL uses two main types of operations:

  • Queries: These are used to read data. They do not change anything on the server. For example, fetching your profile information is a query.
  • Mutations: These are used to create, update, or delete data. They change the state of the server. For example, updating your bio, posting a comment, or deleting a collection are mutations.

In the application the researcher was testing, GraphQL was used extensively. The GraphQL endpoint was reachable at paths like /api/graphql and /graphql. Normal data fetching used queries, while all state-changing features used mutations. This included comments, collection management, profile data, user preferences, and general CRUD operations (Create, Read, Update, Delete) through the API.

What is CSRF?

CSRF stands for Cross-Site Request Forgery. It's a type of attack where an attacker tricks a logged-in user's browser into sending a request to a website where the user is authenticated, without the user's knowledge or consent.

Here's an everyday comparison: Imagine you have a bank account and you're logged into your bank's website. The bank's website uses a cookie (a small piece of data stored in your browser) to remember that you're logged in. Now, imagine you visit a malicious website that has a hidden form or image that sends a request to your bank's website to transfer money to the attacker's account. Because your browser automatically includes your bank's cookie with the request, the bank thinks the request came from you and processes it. You never intended to transfer money, but the attacker tricked your browser into doing it.

For a CSRF attack to work, a few conditions must be met:

  1. The user must be logged in to the target website (so their browser has a valid session cookie).
  2. The target website must not have a CSRF token (a secret value that the website includes in forms and checks on the server to ensure the request came from its own pages).
  3. The target website's cookies must be sent with cross-site requests. This depends on the cookie's SameSite attribute.

What is a cookie and the SameSite attribute?

A cookie is a small piece of data that a website stores in your browser. It's used to remember things like your login session, preferences, or shopping cart. When you log in to a website, the server sends a cookie to your browser. Your browser stores it and sends it back with every subsequent request to that website. This is how the website knows who you are.

The SameSite attribute on a cookie controls whether the cookie is sent with cross-site requests. Cross-site means a request from a different website. There are three values:

  • SameSite=Strict: The cookie is only sent with requests from the same site. This is the most secure.
  • SameSite=Lax: The cookie is sent with same-site requests and with top-level navigation (like clicking a link) from other sites, but not with embedded requests (like images or forms).
  • SameSite=None: The cookie is sent with all requests, including cross-site requests. This is the least secure and is often used when a website needs to be embedded in other sites or when it uses third-party services.

In the application the researcher was testing, the GraphQL responses set the cookie attribute to SameSite=None. This means the session cookie would be sent with cross-site requests, which is a precondition for CSRF.

cookie attribute From the original on medium.com

The screenshot shows the response cookie attribute set to SameSite=None. This is a key observation because it means the browser will attach the session cookie to requests coming from other websites.

The researcher's initial attempts

Before finding the CSRF vulnerability, the researcher tried other common GraphQL attacks:

  • IDOR (Insecure Direct Object Reference): Trying to access data belonging to other users by changing an ID in the request.
  • Access control issues: Trying to perform actions that require higher privileges.
  • Injection-related vulnerabilities: Trying to inject malicious code into queries.

However, the application had strict validation, and all these attempts failed. So the researcher went back to the Burp Suite history (Burp Suite is a tool that records all HTTP requests and responses) and reviewed the requests he had already captured.

The discovery

While reviewing the Burp Suite history, the researcher focused on requests to the GraphQL API, including both queries and mutations. He noticed two important things:

  1. The requests to the GraphQL endpoint did not include any CSRF token, neither in the request headers nor in the request body. A CSRF token is a unique, unpredictable value that the server generates and includes in forms. When the form is submitted, the server checks that the token matches the one it expects. If an attacker tries to forge a request from another site, they won't know the token, so the request will be rejected. Without a CSRF token, there's nothing tying a request to a page the user actually intended to submit.

  2. The responses to GraphQL requests had the cookie attribute SameSite=None. This means the session cookie would be sent with cross-site requests.

These two observations together suggested that CSRF might be possible, especially for features that rely on GraphQL mutations to perform CRUD operations.

Finding the API documentation

During reconnaissance (the information-gathering phase), the researcher found an API documentation PDF that contained a complete list of GraphQL queries and mutations used across the application. The document clearly mapped which queries and mutations were associated with specific features and functionalities. This significantly increased the researcher's visibility into the attack surface and helped him understand the potential impact. He could see exactly which features could be targeted and how they were implemented.

If GraphQL introspection is enabled, you can also ask the API to return its own schema, which lists all types, mutations, queries, and objects. This is another way to map the attack surface.

Choosing a target feature

To perform the CSRF attack, the researcher chose a feature that relied heavily on a GraphQL mutation: the profile update, specifically the mutation that modifies the user's bio field. He captured a profile update request and sent it to Burp Repeater (a tool that lets you modify and resend requests).

The request body was in JSON format and looked like this:

Text
{  "query":"\n    mutation UpdateAboutMe($about: String!, $userId: ID)      {\n  updateAboutMe(about: $about, userId: $userId)       {\n    success\n  }\n}\n    ","variables":{"about":"# hello worlds"},    "operationName":"UpdateAboutMe"}

Let's break this down line by line:

  • The entire body is a JSON object (a way of structuring data with curly braces and key-value pairs).
  • "query": This key holds the GraphQL mutation as a string. The string contains newline characters (\n) and spaces for readability. The mutation is named UpdateAboutMe and takes two variables: $about (a String, required) and $userId (an ID, optional). It calls the updateAboutMe mutation with these variables and asks for the success field in return.
  • "variables": This key holds the values for the variables used in the mutation. Here, about is set to "# hello worlds". The userId is not provided, so it's optional.
  • "operationName": This key specifies the name of the operation to execute, which is UpdateAboutMe.

The request used the Content-Type: application/json header. This tells the server that the body is in JSON format. JSON bodies are generally safer from CSRF because browsers do not automatically send cross-origin requests with that content type without a preflight request (a preliminary request that asks the server if the actual request is allowed). So the researcher wondered if CSRF was even possible here.

From the original on medium.com

Step 1: Changing the content type

The researcher changed the Content-Type header from application/json to application/x-www-form-urlencoded. This is the content type that a regular HTML form uses when it submits data. Unlike JSON, form-encoded data can be sent cross-origin without a preflight request. He then rewrote the body to match this content type:

Text
query=mutation UpdateAboutMe($about: String!, $userId: ID) {  updateAboutMe(about: $about, userId: $userId) {    success  }}&variables={"about":"# hello worlds"}

Let's break this down:

  • The body is now a series of key-value pairs separated by &, just like a URL query string.
  • query= is the key, and the value is the GraphQL mutation text. The mutation is the same as before, but without the JSON escaping.
  • &variables= is the second key, and its value is a JSON object containing the about variable.

The server accepted this modified request body without any issues. This is a content-type handling misconfiguration: the endpoint parsed form-encoded input as readily as JSON. This is exactly the content type a cross-site HTML form can produce, which makes CSRF possible.

Due to improper handling of the Content-Type header, the server accepted requests with multiple content types. From the original on medium.com

The screenshot shows that due to improper handling of the Content-Type header, the server accepted requests with multiple content types. This is a critical finding because it means the JSON-only assumption that usually protects GraphQL endpoints did not hold here.

Step 2: URL-encoding and building the PoC

After changing the content type, the researcher URL-encoded all characters in the request body, including the values of the query and variables parameters, using Burp Suite. URL encoding replaces special characters with a percent sign followed by two hexadecimal digits. For example, a space becomes %20, { becomes %7B, and " becomes %22. This is necessary because GraphQL queries contain special characters like {}, ", :, and spaces that could be misinterpreted during transmission.

He then generated a csrf.html PoC file with Burp's help. A PoC (Proof of Concept) is a demonstration that the vulnerability exists. The HTML file contains a form that automatically submits itself when opened. He opened this file in the same browser where the user was already logged in to the application.

What happened next was unexpected: the first attempt failed. The CSRF attack did not execute.

From the original on medium.com

Step 3: Switching from POST to GET

To bypass the restriction, the researcher changed the HTTP method. HTTP methods are verbs that tell the server what action to perform. The most common are:

  • GET: Used to retrieve data. Parameters are sent in the URL.
  • POST: Used to submit data. Parameters are sent in the request body.

GraphQL mutations are typically performed using the POST method. The researcher changed the request method from POST to GET, moving the parameters into the query string (the part of the URL after the ?). The final request looked like this:

Let's break this down:

  • example.com/api/graphql is the endpoint.
  • ?query= starts the query string. The value is the URL-encoded GraphQL mutation. If you decode it, it's the same mutation as before: mutation UpdateAboutMe($about: String!, $userId: ID) { updateAboutMe(about: $about, userId: $userId) { success } }.
  • &variables= is the second parameter. Its value is the URL-encoded JSON object {"about":"# hello worlds"}.

After making these changes, the researcher generated a final HTML CSRF PoC using Burp Suite and opened it in the same browser where the application was already running in an authenticated session. This time, the CSRF exploitation was successful.

From the original on medium.com

The screenshots show the successful CSRF exploitation. The behaviour indicated that the server handled authorization and authentication differently based on the HTTP method. Changing the request from POST to GET bypassed the expected check and allowed the payload to execute.

Why did this work?

The attack succeeded because of a combination of weaknesses:

  1. No CSRF token: The GraphQL requests did not include any CSRF token, either in headers or body. This meant there was nothing tying a mutation to a page the user actually intended to submit.
  2. SameSite=None: The session cookies were issued with SameSite=None, so the browser attached them to cross-site requests.
  3. Content-type handling flaw: The endpoint accepted application/x-www-form-urlencoded bodies, which a plain HTML form can send cross-origin without a preflight. The JSON-only assumption that usually protects GraphQL endpoints did not hold here.
  4. Method-dependent behaviour: The server applied its authorization and authentication handling differently for POST and GET. The GET path did not enforce the check that blocked the first PoC.

Impact

Because the flaw sat in the GraphQL layer rather than in one feature, every function backed by a GraphQL mutation for CRUD operations was reachable this way: comments, collections, profile data, user preferences, and other API operations. An attacker who got a logged-in user to load a crafted page could perform those operations as that user. The report states that manipulating multiple functionalities at once could have severely affected the system's integrity, availability, and confidentiality.

The researcher responsibly reported the issue to the system's technical team, and as a result of disclosing this vulnerability, he was awarded a bounty.

From the original on medium.com

How to fix it

The report does not describe the remediation that was applied. Based on the root cause, the changes that would close it are:

  • Enforce an anti-CSRF token on GraphQL requests regardless of content type and method.
  • Reject state-changing operations sent with GET.
  • Accept only the expected content type on the GraphQL endpoint.
  • Avoid SameSite=None where the application does not genuinely need cross-site cookies.

Variants to test

If you want to test for similar issues, here are some approaches:

  1. Content-type switching on GraphQL endpoints: Take a captured mutation and resend it as application/x-www-form-urlencoded with query and variables parameters. A positive result is the server returning the same success response as the JSON version.
  2. Method switching: Resend the same mutation as GET with the parameters URL-encoded in the query string. A positive result is the mutation executing where the POST version was rejected, which points to divergent authorization paths.
  3. Cookie attribute review: Check SameSite on session cookies set by GraphQL responses. SameSite=None without a compensating token is the precondition for the whole chain.
  4. Token coverage: Compare requests with and without the CSRF token, and across content types and methods, to see whether the token is validated everywhere or only on the paths the frontend uses.
  5. Schema discovery: Look for API documentation and test whether introspection is enabled; either gives the full list of mutations to try against the CSRF conditions above.

Glossary

  • API: Application Programming Interface; a way for two software programs to talk to each other. Think of it as a waiter between your browser and a server.
  • GraphQL: A query language for APIs that lets clients request exactly the data they need from a single endpoint.
  • Endpoint: A URL where an API can be accessed, like /api/graphql.
  • Query: In GraphQL, an operation that reads data without changing the server's state.
  • Mutation: In GraphQL, an operation that creates, updates, or deletes data, changing the server's state.
  • CRUD: Create, Read, Update, Delete; the four basic operations for managing data.
  • Schema: A blueprint that defines the structure of a GraphQL API, including all types, queries, and mutations.
  • Introspection: A GraphQL feature that lets clients ask the API to describe its own schema.
  • CSRF: Cross-Site Request Forgery; an attack where a malicious website tricks a logged-in user's browser into sending requests to another website.
  • CSRF token: A unique, unpredictable value that a website includes in forms and checks on the server to ensure the request came from its own pages.
  • Cookie: A small piece of data stored in your browser by a website, often used to remember your login session.
  • SameSite: A cookie attribute that controls whether the cookie is sent with cross-site requests. Values are Strict, Lax, and None.
  • SameSite=None: A cookie setting that allows the cookie to be sent with all requests, including cross-site requests.
  • Content-Type: An HTTP header that tells the server what format the request body is in, like application/json or application/x-www-form-urlencoded.
  • JSON: JavaScript Object Notation; a common format for structuring data using key-value pairs.
  • URL encoding: A way of encoding special characters in a URL by replacing them with a percent sign followed by two hexadecimal digits.
  • HTTP method: A verb that tells the server what action to perform, like GET (retrieve) or POST (submit).
  • Burp Suite: A tool used by security researchers to intercept, modify, and resend HTTP requests.
  • Burp Repeater: A Burp Suite tool that lets you modify and resend individual requests.
  • PoC: Proof of Concept; a demonstration that a vulnerability exists.
  • Preflight request: A preliminary request that a browser sends to check if a cross-origin request is allowed.
  • IDOR: Insecure Direct Object Reference; a vulnerability where an attacker can access data belonging to other users by changing an identifier.
  • Bounty: A reward, often monetary, given to a security researcher for responsibly disclosing a vulnerability.
  • Attack surface: The total number of points where an attacker could try to enter or extract data from a system.
  • Reconnaissance: The information-gathering phase of a security test.
  • Integrity, availability, and confidentiality: The three pillars of information security. Integrity means data is accurate and unaltered; availability means the system is accessible when needed; confidentiality means data is only accessible to authorized parties.

More by divyanksitapara088

View all

1 more disclosed report, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site request forgery (CSRF)

The lesson behind this report.

Another site makes a signed-in user's browser perform an action they did not intend.