Skip to main content

Improve the security of your GraphQL API’s - Part 4–Persisted queries

As a GraphQL API gives you a lot of extra power and possibilities, it also introduces some new attack vectors. Nothing prevents the user of your (web) application to open the developer console and start creating and sending other queries to your GraphQL backend. By using the authentication token already available, he/she can call your API. So without further mitigations a user can create and run any query he/she can think of.

Luckily there are multiple ways to control this attack vector. I already talked about

In this post I want to focus on a specific feature supported by most GraphQL api’s: persisted queries. 

When using persisted queries, a client only sends a query identifier (and optionally variables) to the server instead of the full query text. This approach provides several benefits, such as reducing bandwidth usage, makes caching a lot easier and allows extra optimizations on the server.

From a security perspective you can configure the server to only accept persisted queries and refuse any other queries provided by a client. This gets rid of a whole suite of potential attack vectors, since malicious actors can no longer craft and execute harmful queries against your GraphQL server.

Let me show you how to implement this.

Persisted queries in the HotChocolate GraphQL Server

On the server-side we need to update our GraphQL configuration.

We first enable the persisted query pipeline and specify where HotChocolate should look for the persisted queries.

Remark: In the example above I load the persisted queries from disk. Other options exist(Redis, inmemory). I’ll explain later how we get the persisted query files on disk.

This is sufficient to start using the persisted queries feature and benefit from the performance advantage it offers. If we also want to use the security feature, we can block dynamic queries by adding the following line:

That’s all we need to do on the server-side. Let’s move on to the client…

Persisted queries in the Strawberry Shake GraphQL Client

On the client we need to update the csproj file and include the following:

Once we have done this, a query hash will be calculated and the content of the query will be copied to the specified folder.  It is the content of this folder that is required on the server to check which persisted queries are available and can be used.

Remark: I’ll leave it up to you how you get the content of this folder to the server. We typically do this as a part of the build process.

If we now  execute a query through the client:

the following information is send over the line:

If I try to execute another query that is not part of the persisted queries I get the following error message:

More information:

Popular posts from this blog

Azure DevOps/ GitHub emoji

I’m really bad at remembering emoji’s. So here is cheat sheet with all emoji’s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.

Podman– Command execution failed with exit code 125

After updating WSL on one of the developer machines, Podman failed to work. When we took a look through Podman Desktop, we noticed that Podman had stopped running and returned the following error message: Error: Command execution failed with exit code 125 Here are the steps we tried to fix the issue: We started by running podman info to get some extra details on what could be wrong: >podman info OS: windows/amd64 provider: wsl version: 5.3.1 Cannot connect to Podman. Please verify your connection to the Linux system using `podman system connection list`, or try `podman machine init` and `podman machine start` to manage a new Linux VM Error: unable to connect to Podman socket: failed to connect: dial tcp 127.0.0.1:2655: connectex: No connection could be made because the target machine actively refused it. That makes sense as the podman VM was not running. Let’s check the VM: >podman machine list NAME         ...

Cleaner switch expressions with pattern matching in C#

Ever find yourself mapping multiple string values to the same result? Being a C# developer for a long time, I sometimes forget that the C# has evolved so I still dare to chain case labels or reach for a dictionary. Of course with pattern matching this is no longer necessary. With pattern matching, you can express things inline, declaratively, and with zero repetition. A small example I was working on a small script that should invoke different actions depending on the environment. As our developers were using different variations for the same environment e.g.  "tst" alongside "test" , "prd" alongside "prod" .  We asked to streamline this a long time ago, but as these things happen, we still see variations in the wild. This brought me to the following code that is a perfect example for pattern matching: The or keyword here is a logical pattern combinator , not a boolean operator. It matches if either of the specified pattern...