Debian Codegraph @

docs/discussion/dos_mitigations.rst

raw
1 .. _dos_mitigations:
2
3 Denial Of Service mitigations
4 =============================
5
6 There are multiple ways a client can overload a server and deny
7 service to other clients. A simple example can simply be to call an
8 expensive route at a rate high enough that the server's resources are
9 exhausted. Whilst this could happen innocently (if a route was
10 popular) it takes a lot of client resource, there are malicious
11 methods to exhaust server resources.
12
13 There are two attack methods mitigated and discussed here, the first
14 aims to open as many connections to the server as possible without
15 freeing them, thereby eventually exhausting all the connections and
16 preventing other clients from connecting. As most request response
17 cycles last milliseconds before the connection is closed the key is to
18 somehow hold the connection open.
19
20 The second aims to exhaust the server's memory and hence either slow
21 the server to a crawl or kill the server application, thereby
22 preventing the server from replying to other clients. The key here is
23 to somehow make the server write a lot of information to memory.
24
25 Inactive connection
26 -------------------
27
28 This attack is of the first type and aims to exhaust the server's
29 connections. It works by opening connections to the server and then
30 doing nothing with the connection. A poorly configured server would
31 simply wait for the client to do something therefore holding the
32 connection open.
33
34 To mitigate this Hypercorn has a keep alive timeout that will keep an
35 inactive connection alive for this time. It can be configured via the
36 configuration ``keep_alive_timeout`` setting.
37
38 The default value for the keep alive timeout is 5 seconds which is the
39 max recommended default in the Gunicorn settings.
40
41 .. note::
42
43 Connections are not considered inactive whilst the request is being
44 processed. So this will only timeout connections inactive before a
45 request, or after a response and before the next request. This does
46 not affect websocket connections.
47
48 Large request body
49 ------------------
50
51 This attack is of the second type and aims to exhaust the server's
52 memory by inviting it to receive a large request body (and hence write
53 the body to memory). A poorly configured server would have no limit on
54 the request body size and potentially allow a single request to
55 exhaust the server.
56
57 It is up to the framework to guard against this attack. This is to
58 allow the framework to consume the request body if desired.
59
60 Slow request body
61 -----------------
62
63 This attack is of the first type and aims to exhaust the server's
64 connections by inviting it to wait a long time for the request's
65 body. A poorly configured server would wait indefinitely for the
66 request body.
67
68 It is up to the framework to guard against this attack. This is to
69 allow the framework to consume the request body if desired.
70
71 No response consumption
72 -----------------------
73
74 This attack is of the second type and aims to exhaust the server's
75 memory by failing to consume the data sent to the client. This failure
76 results in backpressure on the server that leads to the response being
77 written to memory rather than the connection. A poorly configured
78 server would ignore the backpressure and exhaust its memory. (Note
79 this requires a route that respondes with a lot of data, e.g. video
80 streaming).
81
82 To mitigate this Hypercorn responds to the backpressure and pauses
83 (blocks) the coroutine writing the response.
84
85 Slow response consumption
86 -------------------------
87
88 This attack is of the first type and aims to exhaust the server's
89 connections by inviting the server to take a long time sending the
90 response, for example by applying backpressure indefinetly. A poorly
91 configured server would simply wait indefinetly trying to send the
92 response.
93
94 It is up to the framework to guard against this attack. This is to
95 allow for responses that purposely take a long time, e.g. server sent
96 events.
97
98 Large websocket message
99 -----------------------
100
101 This attack is of the second type and aims to exhaust the server's
102 memory by inviting it to receive very large websocket messages. A
103 poorly configured server would have no limit on the message size
104 and potentially allow a single message to exhaust the server.
105
106 To mitigate this Hypercorn limits the message size to the value set in
107 the application config ``websocket_max_message_size``. Any message
108 larger than this limit will trigger the websocket to be closed
109 abruptly.
110
111 The default value for ``websocket_max_message_size`` is 16 MB, which
112 is chosen as it is the limit discussed in the Flask documentation.
113
114 Slow SSL handshake
115 ------------------
116
117 This attack is of the first type and aims to exhaust the server's
118 connections by failing to complete (or potentially even start) the SSL
119 handshake. A poorly configured server would simply wait indefinetly
120 for the SSL handshake to complete.
121
122 To mitigate this Hypercorn has a ssl handshake timeout that will close
123 connections that take longer to complete the ssl handshake. It can be
124 configured via the configuration ``ssl_handshake_timeout`` setting.
125
126 The default value for ``ssl_handshake_timeout`` is 60 seconds, which
127 is chosen as it is the limit used in NGINX.
128
129 HTTP/2 attacks
130 --------------
131
132 There are a number of specific attacks against the HTTP/2 protocol
133 that are designed to exhuast the server's resources. These are only
134 feasible against a server serving over HTTP/2, and hence apply to
135 Hypercorn by default.
136
137 Flood attacks
138 ^^^^^^^^^^^^^
139
140 This attack aims to exhaust the server's resources by requesting ack
141 frames whilst not consuming them. It is conceptually similar to the
142 "No response consumption" attack discussed above however this attack
143 slowly consumes frames that are unrelated to the HTTP response such as
144 ping, setting, and reset frames.
145
146 To mitigate this Hypercorn responds to the backpressure and pauses
147 (blocks) the reading data from the client. Depending on the state of
148 the connection a timeout should then close the connection.
149
150 Data Dribble
151 ^^^^^^^^^^^^
152
153 This attack aims to exhaust the server's resources by requesting a
154 response whilst setting the window size to a very small value e.g. 1
155 byte. The server could then exhaust memory buffering the response. It
156 is conceptually similar to the "No response consumption" attack
157 discussed above.
158
159 It is up to the framework to guard against this attack. This is to
160 allow for responses that purposely take a long time, e.g. server sent
161 events.
162
163 Internal Data Buffering
164 ^^^^^^^^^^^^^^^^^^^^^^^
165
166 This attack is conceptually similar to the "Flood attack" and "No
167 response consumption" attack in that it requires the server to buffer
168 data that it cannot send to the client.
169
170 To mitigate this Hypercorn responds to the backpressure and pauses
171 (blocks) the coroutine writing the response.
172
173 Rapid reset
174 ^^^^^^^^^^^
175
176 This attack works by opening and closing streams in quick succession
177 in the expectation that this is more costly for the server than the
178 client.
179
180 To mitigate Hypercorn will only allow a maximum number of requests per
181 kept-alive connection before closing it. This ensures that cost of the
182 attack is equally born by the client.