Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Apollo Server rejects a request as a potential CSRF

ErrorThis operation has been blocked as a potential Cross-Site Request Forgery (CSRF).

Short answer

Apollo Server's CSRF prevention is on by default. It runs an operation only if the request has a JSON content type, or an Apollo-Require-Preflight header, or an X-Apollo-Operation-Name header. Make your client send one of them. Do not switch csrfPrevention off; fix the client instead, as the Apollo docs advise.

  • Applies to Apollo Server 4, Apollo Client 3, Next.js 15, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Send the same query yourself with a JSON content type, and then without one. If only the second is rejected, the client is not sending an accepted header.

    curl -i -X POST http://localhost:4000/graphql -H "content-type: application/json" -d '{"query":"{ __typename }"}'
    curl -i "http://localhost:4000/graphql?query=%7B__typename%7D"
  2. 2Match what you see, then jump to its fix

    1. 1400 on GET queries (useGETForQueries, link prefetch, or a plain browser URL)A GET request without any header Apollo acceptsFix
    2. 2Queries work, but mutations with a File variable return 400A file upload sends multipart/form-dataFix
    3. 3Works from Apollo Client Web, fails from your own fetch call or a server-side scriptA custom client or tool sends a non-JSON content typeFix

What does the error look like?

  • This operation has been blocked as a potential Cross-Site Request Forgery (CSRF).
  • HTTP 400 Bad Request from the GraphQL endpoint

Cause 1 of 3

A GET request without any header Apollo accepts

1Check

The docs say a request runs only if it has a JSON-type Content-Type, a non-empty X-Apollo-Operation-Name, or a non-empty Apollo-Require-Preflight header. From v5.5.0 a GET with a non-empty Content-Type other than application/json is also rejected.

curl -i "http://localhost:4000/graphql?query=%7B__typename%7D" -H "Apollo-Require-Preflight: true"

2Fix

Configure the client to send a non-empty Apollo-Require-Preflight header on every request.

import { HttpLink } from "@apollo/client";

const link = new HttpLink({
  uri: "http://localhost:4000/graphql",
  headers: { "Apollo-Require-Preflight": "true" },
});

3Verify

The curl command above returns data instead of a 400.

curl -i "http://localhost:4000/graphql?query=%7B__typename%7D" -H "Apollo-Require-Preflight: true"

Confirmed by apollographql.com

Did this fix it?

Cause 2 of 3

A file upload sends multipart/form-data

1Check

multipart/form-data counts as a simple content type, so the JSON content-type rule does not apply. The docs give the client-side fix for apollo-upload-client.

grep -rn "createUploadLink" src app lib 2>/dev/null

2Fix

Pass the preflight header to createUploadLink.

// createUploadLink comes from the apollo-upload-client package
const link = createUploadLink({
  uri: "http://localhost:4000/graphql",
  headers: { "Apollo-Require-Preflight": "true" },
});

3Verify

Upload a small file again and check the response status is 200.

# repeat the upload in the app and watch the Network tab

Confirmed by apollographql.com

Did this fix it?

Cause 3 of 3

A custom client or tool sends a non-JSON content type

1Check

Apollo Client Web sends Content-Type: application/json, so it passes. A hand-written fetch or another tool may send text/plain or form encoding.

grep -rn "fetch(.*graphql" src app lib 2>/dev/null

2Fix

Set content-type: application/json on the request, or add the Apollo-Require-Preflight header. If you must accept other headers, csrfPrevention can list your own, which replaces the two header checks.

await fetch("http://localhost:4000/graphql", {
  method: "POST",
  headers: { "content-type": "application/json" },
  body: JSON.stringify({ query: "{ __typename }" }),
});

3Verify

Repeat the failing call; it should return a JSON body.

curl -i -X POST http://localhost:4000/graphql -H "content-type: application/json" -d '{"query":"{ __typename }"}'

Confirmed by apollographql.com

Did this fix it?

How do I stop it coming back?

  • Add the Apollo-Require-Preflight header in one shared link so every client call has it.
  • Keep csrfPrevention on in every environment, as the docs recommend.
  • Test one GET query and one upload in your integration tests.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.