Attack an API
bughunt api sends thousands of generated requests to a running REST, GraphQL or gRPC API and reports the ones that crash it, get in without a login, or let one user read another's data.
Run it against your own machine or a test environment, never production. It sends malformed input on purpose. bughunt refuses any address that is not this machine unless you name one with --allow-host.
Run it
Start your app locally, then tell bughunt where it is and how it is described.
# REST, from an OpenAPI file
bughunt api --url http://localhost:3000 --spec openapi.json --token-file token
# REST, from a Rails app's routes
bin/rails routes > routes.txt
bughunt api --url http://localhost:3000 --rails-routes routes.txt --token-file token
# GraphQL: the schema is read from the endpoint (or give a .graphql file with --spec)
bughunt api --url http://localhost:3000 --graphql /graphql --token-file token
# gRPC: the server describes itself through reflection (or give a descriptor set with --spec)
bughunt api --url grpc://localhost:50051 --write
The description is read again on every run, so endpoints added since the last run are covered without any setting.
The login token
Put a test user's token in a file and pass --token-file, or set BUGHUNT_TOKEN. It is sent as Authorization: Bearer …, or as call metadata for gRPC. It never appears on a command line, in the log or in the report.
How far a run goes
The first line of every report says which of these it was.
| REST | GraphQL | gRPC | |
|---|---|---|---|
| default | GET only; no probing with methods a path does not list | queries only, no mutations | refused: no way to tell a read from a write |
--write | every listed method, plus probes with unlisted ones (QUERY, OPTIONS, TRACE, …) | queries and mutations | every method |
--full | --write, plus every check and every phase, including multi-step sequences such as create, read, delete | ||
--write and --full create, change and delete records. Use them only against a database you can throw away.
Each of these is proven on every change against test servers that write down every request they receive: a read-only REST run must send GET and nothing else, a read-only GraphQL run no mutation, and --full every method.
One user reading another's data
The most common serious hole in an API is a record that any signed-in user can read by changing an id in the address (OWASP API1). One login cannot find it. Give two or more:
bughunt api --url http://localhost:3000 --spec openapi.json \
--peer alice=alice.token --peer bob=bob.token
HIGH other-users-data-readable GET /api/orders/{id} | [200] OK
`bob` received the `Order` that `alice` owns
curl -X GET -H 'Authorization: [Filtered]' http://localhost:3000/api/orders/1
fix: Check that the record belongs to the signed-in user before returning it, in the query itself
(scope it to the user or their organisation), with a test that a second user gets 404.
When one user reads a record of their own, it is read again as each other user. Only reads are repeated, so this works on a read-only run. It needs a way to tell whose a record is: a list that shows each user their own, or (with --write) records the users create.
What it reports
| Finding | Means | When |
|---|---|---|
| server-error (high) | A 5xx answer, a GraphQL error, or a gRPC INTERNAL/UNKNOWN, to a request it should have handled or refused. | always |
| login-not-checked (high) | An operation that needs a login answered without one. | always |
| other-users-data-readable (high) | One user received another user's record. | with --peer |
| deleted-resource-still-available (high) | A record answered after it was deleted. | --full |
| schema-violating-request-accepted | Input the description forbids was accepted. | --full |
| wrong-response-to-undeclared-method | A method the operation does not have was not refused with 405. | --full |
| slow-response | An answer took longer than the limit you set with -- --max-response-time. | --full |
| answers that break the description | A status code, header, content type or body the description does not allow. | --full |
Each finding comes with the command that reproduces it (curl, or grpcurl for gRPC), what to do about it, and its weakness ids.
If an operation could not be tested at all (the server did not answer, the description is broken), it is listed as not tested and the run ends with exit status 2. That is never counted as clean.
Read the first lines before you celebrate
120 operations tested with 1,430 generated cases
41 only ever answered "not found": no matching records in this database, so barely tested
12 only answered "not allowed": this login cannot reach them
at most 67 were really exercised
"No failures" is not "the API is clean". A generated id rarely matches a real record, so an endpoint that looks things up may never get past "not found". To reach more:
- fill the test database with records;
- use a login with wider permissions, or give several with
--peer; - pass a real description with examples, and run
--fullon a throwaway database, where multi-step sequences create the records later steps read.
What it leaves out
- A 501 answer to an HTTP method the operation does not have. Refusing a made-up method is correct.
- A 429 answer. A rate-limited check proves nothing. The report counts them; slow the run down with
-- --rate-limit 400/m. - WebSockets and streaming gRPC calls: the engine does not test them.
Options
| Option | What it does |
|---|---|
--url URL | Where the API is running: http(s)://, or grpc(s):// for gRPC. |
--spec FILE | A description: OpenAPI (.json, .yaml), GraphQL (.graphql) or gRPC (.binpb, .pb, .protoset). |
--rails-routes FILE | Output of bin/rails routes, used instead of a description. |
--graphql PATH | A GraphQL endpoint on --url; its schema is read from the server. |
--token-file FILE | A test user's login token. |
--peer NAME=FILE | One of two or more users, to check none can read another's data. |
--examples N | Generated requests per operation (default 8). |
--write | Also send every method or mutation that changes data. |
--full | Every method, every check, every phase. |
--allow-host HOST | Allow one host that is not this machine. |
-- … | Flags for Schemathesis that pace or narrow the run, such as --rate-limit, --max-time, --exclude-path-regex and --workers. Flags that would change the address, the methods or the headers are refused. |