GraphQL lab: attack a GraphQL API, then fix it
This lab gives each student a college noticeboard's GraphQL API to attack and to fix. The same API is served twice: at /graphql the way many first GraphQL APIs are written, and at /fixed/graphql with each weakness fixed, so every attack can be tried on both.
It is a small server written for the lab in Python's standard library, following the GraphQL query language for fields, arguments, aliases, nested selections, introspection and batches. Its four accounts and twelve posts are made up and made fresh at every start. Every copy has no route to the internet and none to any other student.
What students learn
What a GraphQL API gives away and how each weakness is fixed. Introspection hands out the schema, a misspelt field is answered with a suggestion, and a private note is returned with no token because no resolver checks who is asking.
A query nested eight levels returns 324 titles from 12 posts, and 100 PIN guesses travel in one request as aliases, past a limit that counts requests. The fixed API refuses each: introspection off, depth 5, 10 aliases, no batches and login attempts counted per account.
Run it in your browser
The lab starts one service, graphql, on port 4000. The terminal beside the lab reaches it with curl, sending a JSON body that holds the query. Open in the browser shows a short page saying the same.
- Open the Container page. Open Build on the bar and choose Run a container. Anybody may open it, and the Git lab and the SQL lab start with no account. Every other lab needs a free account: create one, sign in, then press Start.
- Find the lab. The Sample labs card lists every sample. Find GraphQL: attack an API and fix it.
- Press Start. Start runs it as it is, in one press. Edit the file first opens the compose file so you can change it before you start. The API is ready in a few seconds.
- Open it. On the lab's page, press Open terminal and send a query, for example: curl -s -H "Content-Type: application/json" -d '{"query":"{ posts { title } }"}' http://graphql:4000/graphql
What to ask students to hand in
For each attack they try, what /graphql answered, what /fixed/graphql answered and the fix that made the difference. Nothing is marked automatically: naming the fix shows whether it was understood.
A basic lesson on this lab
GraphQL Basics is a small lesson worked in this lab. In it a student sends a query and a nested one, asks for the schema on both APIs, and reads a private field with no token that the fixed API refuses.
Free personal accounts have it in the Basics module, which is on Home, and every college can copy it from the Library's Basics folder. It is deliberately small: the module API security goes further in its GraphQL lesson, with deep queries, aliases and batches.
The code on GitHub
The compose file has no Dockerfile: it runs the stock Python 3.12 image on Alpine (the PSF licence), pinned by digest and not distributed by us, as an unprivileged account. The file writes the server from fixed text when it starts, so nothing is downloaded. Nothing is saved when the lab stops.
Set it as work
Say in the task what to hand in.
- Make the lab. In your module add a task and choose the Container lab card, then In a GitHub repository. Give https://github.com/mitsurutech/lab-graphql-lab with the branch main. The compose file is the default, compose.yaml. Fork the repository first if you want to change it, for example to add a field or a limit of your own.
- Publish and try it. Publish pins the commit you were shown, so a later change in the repository does not alter a lab you have already set. Start your own copy first.
- Hand it out. Assign it like any task, with a due date if you want one, and say in the task what to do and what to hand in.
- Mark what is handed in. Open a student’s copy and the work they handed in, mark it, and send it back with a reason if it needs more.
Try this lab, and what it costs
A free personal account can work through the basic lessons in this lab, with enough free credit each month to follow most lessons and no card. For a class, a school or college has its first 365 days free for up to 50 students, then pays per student a year, with every lab included and staff free.
Questions people ask
- Is this a real GraphQL server?
- It is a small server written for this lab, so every line can be read. It follows the GraphQL query language for the parts the lessons use and answers in the same shape as real servers, but it has no fragments or variables.
- Why not Juice Shop or NodeGoat?
- Neither has a GraphQL API. The projects that do are large images on old bases, so one small service was the honest way to teach GraphQL's own weaknesses.
- Is any of the data real?
- No. The four accounts, their notes and their posts are made up, and made fresh every time the lab starts.
- Why does the login limit sometimes answer 429 sooner?
- The old API allows five login requests a minute from one address, and every Run that logs in counts. Wait a minute and it starts again.
- Does it cost anything or use credits for AI?
- No AI is used, so nothing is charged for it. The lab runs like any other container lab.