Skip to content

Commit 778b2ae

Browse files
authored
GraphQL Module (#48)
1 parent c92af20 commit 778b2ae

File tree

1 file changed

+112
-32
lines changed

1 file changed

+112
-32
lines changed

modules/4-graphql.livemd

Lines changed: 112 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,89 +2,169 @@
22

33
## Introduction
44

5-
> ### 🛠 <span style="color:goldenrod;">MODULE UNDER CONSTRUCTION - Please move to next module</span>
5+
GraphQL is a query language used to interact with and retrieve data from an application's data sources. Its structure is designed for flexible and precise queries that efficiently interact with complex, highly nested data sets. Using GraphQL, information is retrieved by stepping through data as if it were arranged as a group of connected nodes instead of a strictly hierarchical set up. Think more of a labyrinth than a tree.
66

7-
*TODO: Write Introduction*
7+
Since GraphQL can be implemented as a component of an application's API, there are security issues common to all APIs present, as well as concerns related to characteristics of the query language itself. This module will highlight several security issues associated with GraphQL and recommendations for how to address them.
88

99
## Table of Contents
1010

1111
* [Disabling Introspection](#disabling-introspection)
1212
* [Error Disclosure](#error-disclosure)
13-
* [Resource Exhaustion](#resource-disclosure)
14-
* [Cost Theory](#cost-theory)
13+
* [Resource Exhaustion](#resource-exhaustion)
14+
* [Cost Theory](#cost-theory)
1515

1616
## Disabling Introspection
1717

1818
### Description
1919

20-
*TODO: Write Description*
20+
Introspection queries are a way of enumerating a particular GraphQL implementation to discover details about the queries supported, data types available, and other information. This includes mutation names, fields specific to an organization or dataset, query parameters, and types of objects in the data source. Obtaining this information can help a user, including a malicious one, deduce and discover specifics about the data being stored.
21+
22+
If you are familiar with databases, this is similar to gathering info on the [database schema]( https://en.wikipedia.org/wiki/Database_schema) that includes information about table names, fields, database, structure etc.
23+
24+
Malicious actors in their information gathering/reconnaissnce efforts can leverage this information as they look for ways to attack your application and construct malicious queries and requests to expose and compromise data.
25+
26+
Excessive Data Exposure is number 3 on OWASP's API Security Top 2019 and APIs with this issue return too much and/or sensitive information in response to incoming requests and queries. Although it provides a useful function for GraphQL developers, the information returned by introspection can help facilitate attack.
2127

2228
### Prevention
2329

24-
*TODO: Write Prevention*
30+
The less an attacker can learn about your system or application, the more difficult (though, of course, not impossible given time and resources) it will be to identify vulnerablilities and craft exploits that could result in a successful compromise.
31+
32+
Taking every opportunity to add a layer of difficulty (see defense in depth section in Module 3) for malicious actors is one aspect of securing data and applications.
33+
34+
Imagine trying to surprise a friend who lives in another part of the country with a complete home makeover. You've never been to their place but imagine trying to make arrangements, in secret, without having a map to their city, knowing their address, having access to a website with a street view, or having the floorplans. What if they live on the 30th floor of an apartment building, there's no street parking, and they have 4 large pet dragons?
35+
36+
Since introspection queries provide the floorplans to your data, one step in making an attacker's job more difficult is to, if an evaluation of your application development processes determines it is not needed, disable introspection. If it is not possible to completely disable introspection, the next best defense is to limit access, by following least privilege, or implement other controls to limit exposure. Please see references for more details.
2537

26-
### <span style="color:blue;">Example</span> / <span style="color:red;">Quiz</span>
38+
### <span style="color:blue;">Example</span>
2739

28-
*TODO: Make Example or Quiz Question*
40+
[Vigil](https://github.com/podium/vigil) is an elixir package that when added to your application's dependencies, can intercept incoming GraphQL introspection requests and return an error/forbidden message to the client, instead of information about the schema.
2941

30-
```elixir
42+
It can also intercept responses to ensure no schema data is being leaked in any error messages shown to the client.
43+
44+
### Resources
45+
1. https://owasp.org/www-project-web-security-testing-guide/latest/4-Web_Application_Security_Testing/12-API_Testing/01-Testing_GraphQL
46+
2. https://cybervelia.com/?p=736
47+
3. https://github.com/podium/vigil
48+
49+
### <span style="color:red;">Quiz</span>
50+
51+
**Which of the OWASP API Security Top 10 2019 issues does disabling introspection queries address?**
52+
53+
*Uncomment the line with your answer.*
54+
55+
```
56+
# answer = :API6_2019_Mass_Assignment
57+
# answer = :API10_2019_Insufficient_Logging_Monitoring
58+
# answer = :API3_2019_Excessive_Data_Exposure
59+
# answer = :API4_2019_Lack_of_Resources_Rate_Limiting
3160
61+
IO.puts(answer)
3262
```
3363

3464
## Error Disclosure
3565

3666
### Description
3767

38-
*TODO: Write Description*
68+
When an application responds with overly verbose error messages, it runs the risk of providing vital information to an attacker seeking to exploit the service. It is a best practice to limit the amount of meaningful information that gets sent back to any user in the event there is an issue with a service, or other application component, including APIs.
69+
70+
Within the context of a GraphQL implementation, when errors occur, the server could send error messages that reveal internal details, application configurations, or data which could be used to further an attack on the application.
3971

4072
### Prevention
4173

42-
*TODO: Write Prevention*
74+
Any errors disclosed from the server and displayed to the user should be limited- boring is good when it comes to error messages displayed to users!
4375

44-
### <span style="color:blue;">Example</span> / <span style="color:red;">Quiz</span>
76+
OWASP recommends explicitly defining and enforcing all API response payload schemas including error messages; one might be able to accomplish this in an Elixir GraphQL context through the use of [Absinthe.Middleware](https://hexdocs.pm/absinthe/Absinthe.Middleware.html).
4577

46-
*TODO: Make Example or Quiz Question*
78+
### <span style="color:red;">Quiz</span>
4779

48-
```elixir
80+
**Select the best example of a “good” error message, from the perspective of developer who is writing code that is intended to inform a user (who may or may not be a malicious actor) that the action they have attempted was unsuccessful:**
4981

82+
*Uncomment the item number (1-4) with your answer*
83+
```
84+
# -------------------------------------------------------------
85+
# answer = :1
86+
#
87+
#HTTP/2 401 Unauthorized
88+
#Date: Tues, 16 Aug 2022 21:06:42 GMT
89+
#…
90+
#{
91+
# “error”:”token expired”
92+
#{
93+
# -------------------------------------------------------------
94+
# answer = :2
95+
#
96+
#HTTP/2 200 OK
97+
#Date: Tues, 16 Aug 2021 22:06:42 GMT
98+
#…
99+
#{
100+
# “errors”:[
101+
# {
102+
# “locations”:[
103+
# {
104+
# “column”:2,
105+
# :line”:2
106+
# }
107+
# ],
108+
# “message”: “Parsing failed at
109+
# }
110+
# ]
111+
#}
112+
# --------------------------------------------------------------
113+
# answer = :3
114+
#
115+
#HTTP/2 200 OK
116+
#Date: Tues, 16 Aug 2022 21:06:42 GMT
117+
#…
118+
#{
119+
# “error”:”ID token for user 55e4cb07 at org 1234 expired”
120+
#{
121+
# ---------------------------------------------------------------
122+
# answer = :4
123+
#
124+
#HTTP/2 404 File Not Found
125+
#Date: Tues, 16 Aug 2022 21:06:42 GMT
126+
#…
127+
#{
128+
# “error”:”/www/home/file.txt not found ”
129+
#{
130+
# ---------------------------------------------------------------
131+
132+
IO.puts(answer)
50133
```
134+
### Resources
135+
1. https://github.com/OWASP/API-Security/blob/master/2019/en/src/0xa7-security-misconfiguration.md
136+
2. https://owasp.org/www-project-web-security-testing-guide/latest/4-Web_Application_Security_Testing/12-API_Testing/01-Testing_GraphQL
137+
3. https://cheatsheetseries.owasp.org/cheatsheets/GraphQL_Cheat_Sheet.html
51138

52139
## Resource Exhaustion
53140

54141
### Description
55142

56-
*TODO: Write Description*
57-
58-
### Prevention
59-
60-
*TODO: Write Prevention*
143+
When building an application, it is necessary to manage the access and use of all relevant internal and external resources involved in the context of the application. This will help ensure the continued availablilty of the application and its functionality for all legitimate users and entities.
61144

62-
### <span style="color:blue;">Example</span> / <span style="color:red;">Quiz</span>
145+
Resource exhaustion occurs when memory, processes handling application requests, network traffic transmissions, server capacity, storage, and other host operating system or device limitations are exceeded while an application is running. When resource allocation is not well managed, applications become vulnerable to negative impacts in performance, unintentional service failures, and denial of service attacks, in which a malicious actor takes advantage of resource limitations to intentionally overwhelm and crash a system.
63146

64-
*TODO: Make Example or Quiz Question*
147+
Resource exhaustion can occur inadvertently through legitimate use or could be triggered intentionally in a DoS attack by a malicious actor who sends a large number or resource intensive requests to overload the application.
65148

66-
```elixir
149+
The structure of GraphQL queries make it particularly succeptible to this type of attack as they can be crafted to perform long running and extensive operations, depending on the data being queried.
67150

68-
```
69-
70-
<!-- livebook:{"branch_parent_index":4} -->
151+
In addition to strategies like rate limiting to protect APIs in general, another approach to protecting GraphQL from resource exhaustion involves anticipating the cost of a query and allocating resources based on known available capacity. The next section introduces this approach.
71152

72153
## Cost Theory
73154

74155
### Description
75156

76-
*TODO: Write Description*
157+
Resource intensive queries, like those where a GraphQL query tries to traverse and then return a significant amount of highly nest data can cause a server/service to expend a significant amount of it's processing power and other resources. These high cost queries can render a server and therefore the application useless.
77158

78-
### Prevention
159+
One approach for implementing validation on incoming queries to determine their "cost" in terms of the resources the use. Queries are defined by how much load they place on the server/service processing the request, allowing developers to plan for how best to manage resources. This is a little like making a budget.
79160

80-
*TODO: Write Prevention*
161+
This approach also helps implement rate limiting by establishing a query cost based on the type, operation, and expected performance of each unique GraphQL request for data, and by anticipating the load on the server.
81162

82-
### <span style="color:blue;">Example</span> / <span style="color:red;">Quiz</span>
83163

84-
*TODO: Make Example or Quiz Question*
164+
### Resources
165+
1. https://shopify.engineering/rate-limiting-graphql-apis-calculating-query-complexity
85166

86-
```elixir
167+
<!-- livebook:{"branch_parent_index":4} -->
87168

88-
```
89169

90170
[**<- Previous Module: Secure SDLC Concepts**](./3-ssdlc.livemd) || [**Next Module: Elixir Security ->**](./5-elixir.livemd)

0 commit comments

Comments
 (0)