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

DevToys–A swiss army knife for developers

As a developer there are a lot of small tasks you need to do as part of your coding, debugging and testing activities.  DevToys is an offline windows app that tries to help you with these tasks. Instead of using different websites you get a fully offline experience offering help for a large list of tasks. Many tools are available. Here is the current list: Converters JSON <> YAML Timestamp Number Base Cron Parser Encoders / Decoders HTML URL Base64 Text & Image GZip JWT Decoder Formatters JSON SQL XML Generators Hash (MD5, SHA1, SHA256, SHA512) UUID 1 and 4 Lorem Ipsum Checksum Text Escape / Unescape Inspector & Case Converter Regex Tester Text Comparer XML Validator Markdown Preview Graphic Color B

Help! I accidently enabled HSTS–on localhost

I ran into an issue after accidently enabling HSTS for a website on localhost. This was not an issue for the original website that was running in IIS and had a certificate configured. But when I tried to run an Angular app a little bit later on http://localhost:4200 the browser redirected me immediately to https://localhost . Whoops! That was not what I wanted in this case. To fix it, you need to go the network settings of your browser, there are available at: chrome://net-internals/#hsts edge://net-internals/#hsts brave://net-internals/#hsts Enter ‘localhost’ in the domain textbox under the Delete domain security policies section and hit Delete . That should do the trick…

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.