Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

GraphQL Basics

The whole lesson is free and interactive. Read it here, or press Start lab to run it for real in a live lab, with no account needed.

Container lab8 min read6 commands

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

One address for every question

A phone app wants a post's title and its author's name, and nothing else. A GraphQL API lets the app ask for exactly those fields, at one address, in one request. That freedom also lets an attacker ask for more than the app ever would.

The one rule

Warning

This lab runs an API that is insecure on purpose. Query it, and nothing else. It is your own private copy with no way out to the internet. Never try these things on an API that is not yours: in the UK that is a crime under the Computer Misuse Act 1990.

By the end of this lesson

Note

I can send a GraphQL query and read what the API gives away.

Key words

  • GraphQL: a query language for an API with one address.
  • Query: the fields you ask for, written in braces.
  • Schema: every type and field the API offers.
  • Introspection: asking the API for its own schema.
  • Resolver: the small function that fetches one field.

Starter: what you already know

Note

Answer in your head. 1. What is JSON? 2. Which HTTP method sends data in a body? 3. Why must a server check who may read a record? Answers: text that holds names and values; POST; because anybody can ask for any record.

Before you start

Note

Press Start the lab on the bar at the top of this lesson and wait until it says it is running. The API is the service called graphql, on port 4000. Each Run button below types its command into your lab's terminal, one after another in the same session. To watch them, or to type your own, press Open terminal on the bar.

1. Ask for exactly the fields you want

The first command asks for every post's title. The reply holds twelve titles and nothing else. The second nests a question: each post's author's name. The reply has the same shape, starting with Robotics club moved by ana.

Command
curl -s -H "Content-Type: application/json" -d '{"query":"{ posts { title } }"}' http://graphql:4000/graphql

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Command
curl -s -H "Content-Type: application/json" -d '{"query":"{ posts { title author { name } } }"}' http://graphql:4000/graphql

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

2. The API describes itself

Introspection asks for the schema. The reply lists five object types, Query, Mutation, User, Post and LoginResult, and every field. User has email and privateNote. The second command asks the fixed API, which answers Introspection is turned off on this API.

Command
curl -s -H "Content-Type: application/json" -d '{"query":"{ __schema { types { name fields { name } } } }"}' http://graphql:4000/graphql

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Command
curl -s -H "Content-Type: application/json" -d '{"query":"{ __schema { types { name } } }"}' http://graphql:4000/fixed/graphql

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

3. A private field with no token

The first command asks for dev's private note and sends no token at all. The API hands it over: Backup password is on the server room door. The second asks the fixed API as ana, a student. Her name comes back, the note is null, and the error says Not allowed to read privateNote of user 4. Each field checks who is asking.

Command
curl -s -H "Content-Type: application/json" -d '{"query":"{ user(id: 4) { name privateNote } }"}' http://graphql:4000/graphql

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Command
curl -s -H "Content-Type: application/json" -H "Authorization: Bearer tok-ana-4062" -d '{"query":"{ me { name } user(id: 4) { privateNote } }"}' http://graphql:4000/fixed/graphql

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

A common mistake

Warning

Many people think that because the app only asks for safe fields, the API only gives safe fields. Anybody can send any query. Every field that holds private data needs its own check in its resolver, and the schema should not be handed out in production.

Exit check

Tip

Answer without looking back. 1. How does a query say which fields it wants? 2. What did introspection show on User? 3. What did the fixed API say to introspection? 4. Where does the check on a private field belong? Answers: it names them in braces; email and privateNote; that it is turned off; in that field's resolver.

For teachers

Tip

This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. The lab is one small service written for it, and nothing here marks itself: the student hands in the old and fixed replies for each attack. The module API security goes further in lesson 5: deep queries, a hundred guesses in one request with aliases, and batches. Key learning points: 1. A query names the fields it wants. 2. Introspection hands out the schema. 3. Each private field needs its own check. Common mistake: trusting that the app only asks for safe fields. Equipment: the lab and its terminal. Tip: ask students which field they would hide before the introspection Run. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.

Teaching a class? Set these lessons for your students with marking and a lab for every student.