Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the common security pitfalls when using whenLoaded() in GraphQL


What are the common security pitfalls when using whenLoaded() in GraphQL


When using `whenLoaded()` in GraphQL, which is typically a method to conditionally load related data or associations (e.g., in ORM-based GraphQL resolvers), several common security pitfalls can arise. These pitfalls mostly relate to broader GraphQL security issues but are particularly relevant because `whenLoaded()` often involves fetching additional nested or related data, increasing complexity and risk.

Common Security Pitfalls with `whenLoaded()` in GraphQL

**1. Inconsistent or Missing Authorization Checks
`whenLoaded()` often triggers loading of nested or related data after the initial resolver runs. If authorization logic is not consistently applied at every resolver or data loading point (including those triggered by `whenLoaded()`), unauthorized users may gain access to sensitive data. This is a common issue where some paths to the same data are protected, but others (like those via `whenLoaded()`) are not, allowing attackers to bypass access controls[4][6]. Authorization must be enforced both at the query level and within all nested resolvers or data loaders.

**2. Over-fetching and Complex Queries Leading to Denial of Service (DoS)
Using `whenLoaded()` can cause deeply nested queries that fetch large amounts of related data. Attackers can exploit this by crafting queries that cause excessive data loading and processing, leading to resource exhaustion and DoS. GraphQL’s ability to fetch multiple related objects in a single query makes it challenging to predict resource usage, and `whenLoaded()` can exacerbate this by loading additional data conditionally[4][5]. Mitigations include query depth limiting, complexity scoring, and rate limiting.

**3. Exposure of Sensitive Data Through Improper Field Selection
If `whenLoaded()` loads related data without careful control over which fields are exposed, sensitive information such as passwords, tokens, or private user details may be inadvertently returned in the GraphQL response[5]. This risk is heightened if authorization checks are missing or if error messages leak sensitive information. Developers must ensure that sensitive fields are never exposed, even when nested data is loaded.

**4. Injection Attacks via Improper Input Validation
When `whenLoaded()` is used with dynamic arguments or filters, improper input validation can lead to injection vulnerabilities, such as GraphQL query injection or even SQL injection if the underlying data fetching is not properly parameterized[6]. Attackers might inject malicious queries or commands that manipulate the data fetching logic. Proper input validation and use of parameterized queries are essential.

**5. Bypassing Rate Limiting and Brute Force Protections
Because `whenLoaded()` can cause multiple nested data fetches within a single query, attackers can exploit this to bypass traditional rate limiting. For example, they may send a single complex query that triggers many data fetches or mutations, effectively performing brute force or enumeration attacks without triggering per-request limits[4][7]. Rate limiting strategies must consider query complexity and nested operations.

**6. Increased Attack Surface Due to Schema Introspection and Tooling
The use of `whenLoaded()` often reflects a complex schema with multiple nested relationships. Attackers can use GraphQL introspection and tools like GraphiQL to discover these relationships and craft queries that exploit `whenLoaded()` to access unintended data[1][8]. Disabling introspection in production or restricting it to authorized users can reduce this risk.

Summary

The main security pitfalls when using `whenLoaded()` in GraphQL revolve around:

- Failing to enforce consistent authorization on all nested data loading paths.
- Allowing overly complex or deep queries that cause DoS.
- Exposing sensitive data through nested fields.
- Insufficient input validation leading to injection attacks.
- Bypassing rate limits due to complex nested queries.
- Increased exposure due to schema introspection and tooling.

Mitigations include rigorous authorization checks at every resolver, query complexity and depth limiting, strict input validation, careful schema design, disabling introspection in production, and comprehensive rate limiting tailored for GraphQL’s query structure[3][4][5][6][8].

Citations:
[1] https://portswigger.net/web-security/graphql
[2] https://www.imperva.com/blog/graphql-vulnerabilities-common-attacks/
[3] https://tyk.io/blog/graphql-security-7-common-vulnerabilities-and-how-to-mitigate-the-risks/
[4] https://www.brightsec.com/blog/graphql-security/
[5] https://blog.convisoappsec.com/en/security-in-graphql/
[6] https://wundergraph.com/blog/the_complete_graphql_security_guide_fixing_the_13_most_common_graphql_vulnerabilities_to_make_your_api_production_ready
[7] https://beaglesecurity.com/blog/article/graphql-attacks-vulnerabilities.html
[8] https://www.fastly.com/blog/exploring-the-security-implications-of-graphql