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.
curl -s -H "Content-Type: application/json" -d '{"query":"{ posts { title } }"}' http://graphql:4000/graphqlShown here, not run. Press Start lab to type it into your own lab’s terminal.
curl -s -H "Content-Type: application/json" -d '{"query":"{ posts { title author { name } } }"}' http://graphql:4000/graphqlShown 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.
curl -s -H "Content-Type: application/json" -d '{"query":"{ __schema { types { name fields { name } } } }"}' http://graphql:4000/graphqlShown here, not run. Press Start lab to type it into your own lab’s terminal.
curl -s -H "Content-Type: application/json" -d '{"query":"{ __schema { types { name } } }"}' http://graphql:4000/fixed/graphqlShown 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.
curl -s -H "Content-Type: application/json" -d '{"query":"{ user(id: 4) { name privateNote } }"}' http://graphql:4000/graphqlShown here, not run. Press Start lab to type it into your own lab’s terminal.
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/graphqlShown 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.