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 It is up to the ASGI server to guard against this attack, typically
35 via a configurable keep alive or timeout setting.
36
37 Large request body
38 ------------------
39
40 This attack is of the second type and aims to exhaust the server's
41 memory by inviting it to receive a large request body (and hence write
42 the body to memory). A poorly configured server would have no limit on
43 the request body size and potentially allow a single request to
44 exhaust the server.
45
46 To mitigate this Quart limits the request body size to the value set
47 in the application config['MAX_CONTENT_LENGTH']. Any request with a
48 body larger than this limit will trigger a Request Entity Too Large,
49 413, response.
50
51 The default value for MAX_CONTENT_LENGTH is 16 MB, which is chosen as
52 it is the limit discussed in the Flask documentation.
53
54 Technically this limit refers to the maximum amount of data Quart will
55 allow in memory for a request body. This allows larger bodies to be
56 received and consumed if desired. The key being that the data is
57 consumed and not otherwise stored in memory. An example is,
58
59 .. code-block:: python
60
61 async def route():
62 async for data in request.body:
63 # Do something with the data
64 ...
65
66 it is advisable to add a timeout within each chunk if streaming the
67 request.
68
69 Slow request body
70 -----------------
71
72 This attack is of the first type and aims to exhaust the server's
73 connections by inviting it to wait a long time for the request's
74 body. A poorly configured server would wait indefinitely for the
75 request body.
76
77 To mitigate this Quart by default will not wait for the request body,
78 allowing the route handler to response if possible, for example if the
79 request does not have the correct authorisation. If the body is
80 needed, then the application config['BODY_TIMEOUT'] defines the number
81 of seconds to wait for the body to be completely received. If the
82 timeout is exhausted Quart would respond with RequestTimeout, 408.
83
84 The default value is 60 seconds, which is chosen as it is the typical
85 default timeout value.
86
87 Technically the timeout is only applied to the Response data methods
88 (get_data, data, json, get_json, form, and files) and not the body
89 attribute. This allows for known streaming requests to be consumed
90 as desired, see :ref:`request_body`.
91
92 No response consumption
93 -----------------------
94
95 This attack is of the second type and aims to exhaust the server's
96 memory by failing to consume the data sent to the client. This failure
97 results in backpressure on the server that leads to the response being
98 written to memory rather than the connection. A poorly configured
99 server would ignore the backpressure and exhaust its memory. (Note
100 this requires a route that responds with a lot of data, e.g. video
101 streaming).
102
103 It is up to the ASGI server to guard against this attack.
104
105 Slow response consumption
106 -------------------------
107
108 This attack is of the first type and aims to exhaust the server's
109 connections by inviting the server to take a long time sending the
110 response, for example by applying backpressure indefinitely. A poorly
111 configured server would simply wait indefinitely trying to send the
112 response.
113
114 To mitigate this Quart will limit the time it will try to send the
115 response to that set in the application config['RESPONSE_TIMEOUT'].
116 If this time is exhausted the connection is closed abruptly.
117
118 The default value is 60 seconds, which is chosen as it is the typical
119 default timeout value.
120
121 Technically this limit can be configured per response if desired,
122 e.g. if there is a single route that returns large files. This is an
123 example how,
124
125 .. code-block:: python
126
127 async def route():
128 response = await make_response(...)
129 response.timeout = 120
130 return response
131
132 Large websocket message
133 -----------------------
134
135 This attack is of the second type and aims to exhaust the server's
136 memory by inviting it to receive very large websocket messages. A
137 poorly configured server would have no limit on the message size
138 and potentially allow a single message to exhaust the server.
139
140 It is up to the ASGI server to guard against the attack.