GraphQl API Security - Part 2: GraphQL Attack Surface

February 9, 2024
API SecuritySecurityApiGraphQl

GraphQL Attack Surface ๐Ÿ•ณ๏ธ๐Ÿ”

What Is an Attack Surface?

An attack surface is the set of all points where an unauthorized user can try to access or affect the system. Think of it like the doors, windows, and vents in a building that an intruder might exploit.

In GraphQL:

  • ๐Ÿงฌ Every field, argument, type, and directive increases the attack surface.
  • ๐Ÿ’ฅ The complexity and flexibility of GraphQL = greater potential for abuse.

๐Ÿง  The GraphQL Language: From Client to Server

The GraphQL spec is divided into two parts:

  1. Language โ€“ what clients write (queries, mutations, etc.)
  2. Type System โ€“ what the server defines and validates against

Anatomy of a GraphQL Query

1query Anatomy {
2pastes(filter: "some title", public: true) {
3  title
4  content
5  ipAdrr @show_network(style: "cidr")
6}
7users {
8  username(capitalize: true)
9}
10}
#ComponentDescription
1Operation typequery, mutation, or subscription
2Operation nameArbitrary label like getPastes, can mislead logs
3Top-level fieldFunction returning object/data
4ArgumentParameter passed to field (e.g., filter)
5ValueValue assigned to argument
6FieldNested information requests
7DirectiveExecutes conditionally or alters behavior (e.g., @skip)
8Arg of DirectiveArgument passed to the directive
9Arg of FieldField-specific parameters

๐Ÿ”„ Operation Types

1. query

  • Basic data retrieval. Often abused to extract sensitive fields.

2. mutation

  • Allows data modification. Risk: tampering with logic/data.
1mutation {
2editPaste(id: 1, content: "Hacked!") {
3  paste { id title content }
4}
5}

3. subscription

  • Real-time data over WebSocket. Vulnerable to:
    • CSWSH (Cross-Site WebSocket Hijacking)
    • MITM attacks if not using TLS

๐ŸŽญ Operation Names: Masking Malicious Intents

Operation names are arbitrary:

1query getPastes { pastes { title } }
2query deleteAll { pastes { content } }

A malicious actor can name destructive queries with innocent names to mislead defenders.

Dangerous: These names might be stored in logs or third-party tools, making them a vector for injection or confusion.


๐Ÿงฉ Fields and Arguments

Fields = what we request. Arguments = how we control that request.

1query {
2users(active: true) {
3  email
4}
5}
  • Arguments are often user-controlled โžค susceptible to injection, type confusion, or validation bypasses.
  • Strong typing does not replace input validation.

๐Ÿงช Aliases: Double the Trouble

Aliases rename fields to avoid conflicts, but they also:

  • Allow response key manipulation
  • Obfuscate request intent
  • Enable DoS tricks (like overloading or duplication)
1query {
2publicPastes: pastes(public: true) { title }
3privatePastes: pastes(public: false) { title }
4}

๐Ÿ”— Fragments: Circular DoS Landmines

Fragments group reusable fields:

1fragment CommonFields on Paste {
2title
3content
4}
5
6query {
7pastes {
8  ...CommonFields
9}
10}

๐Ÿ’ฃ Fragments referencing each other (circular) = DoS potential.


๐Ÿ’ผ Variables

Variables help optimize query reuse and reduce dynamic string building:

1query getPastes($public: Boolean!) {
2pastes(public: $public) {
3  title
4}
5}

Like arguments, they are user-input vectors.


๐Ÿงฌ Directives

1query {
2user @include(if: true) {
3  username
4}
5}

Used for conditional logic. Custom directives = powerful but risky if poorly validated.

DirectiveDescriptionLevel
@skipConditionally skip fieldQuery
@includeConditionally include fieldQuery
@deprecatedMarks schema elements as deprecatedSchema

โš ๏ธ Vulnerable or unvalidated custom directives can introduce RCE or logic flaws.


๐Ÿงฑ The Type System: Objects, Scalars, Enums, Unions

  • Object Types โ€“ app-specific data structures (e.g., User, Paste)
  • Scalars โ€“ built-in types like String, ID, Boolean
  • Custom Scalars โ€“ IP, DateTime, etc. Beware of insecure libraries (e.g., CVE-2021-29921 in Python ipaddress lib).
  • Unions / Interfaces / Inputs โ€“ Add flexibility, but more edge cases to abuse

๐Ÿ” Introspection

The __schema and __type fields let clients see the entire schema.

1{
2__schema {
3  types {
4    name
5  }
6}
7}
  • Great for devs and pentesters
  • ๐Ÿ”ฅ Horrible if left enabled in production

๐Ÿ›  Validation & Execution

GraphQL servers have different validation implementations, leading to:

  • ๐Ÿ“Š Fingerprinting
  • ๐Ÿ” Bug discovery

Use tools like:

  • GraphQL Threat Matrix
  • Graphw00f

โš ๏ธ Common Weaknesses Summary

Vulnerability TypeExplanation
DoSAliases, circular fragments, deep nesting
Information DisclosureIntrospection, debug mode, schema leaks
Authn/Authz BypassBroken logic, batch queries
InjectionUnescaped input in arguments, variables