Debian Codegraph @

test/security_SUITE.erl

raw
1 %% Copyright (c) Loïc Hoguin <[email protected]>
2 %%
3 %% Permission to use, copy, modify, and/or distribute this software for any
4 %% purpose with or without fee is hereby granted, provided that the above
5 %% copyright notice and this permission notice appear in all copies.
6 %%
7 %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15 -module(security_SUITE).
16 -compile(export_all).
17 -compile(nowarn_export_all).
18
19 -import(ct_helper, [config/2]).
20 -import(ct_helper, [doc/1]).
21 -import(cowboy_test, [gun_open/1]).
22 -import(cowboy_test, [raw_open/1]).
23 -import(cowboy_test, [raw_send/2]).
24 -import(cowboy_test, [raw_recv_head/1]).
25 -import(cowboy_test, [raw_recv/3]).
26
27 %% ct.
28
29 all() ->
30 cowboy_test:common_all().
31
32 groups() ->
33 Tests = [nc_rand, nc_zero],
34 H1Tests = [slowloris, slowloris_chunks],
35 H2CTests = [
36 http2_cancel_flood,
37 http2_data_dribble,
38 http2_empty_continuation_frames,
39 http2_empty_frame_flooding_data,
40 http2_empty_frame_flooding_headers_continuation,
41 http2_empty_frame_flooding_push_promise,
42 http2_infinite_continuations,
43 http2_hpack_bomb,
44 http2_ping_flood,
45 http2_reset_flood,
46 http2_settings_flood,
47 http2_zero_length_header_leak
48 ],
49 [
50 {http, [parallel], Tests ++ H1Tests},
51 {https, [parallel], Tests ++ H1Tests},
52 {h2, [parallel], Tests},
53 {h2c, [parallel], Tests ++ H2CTests},
54 {h3, [], Tests},
55 {http_compress, [parallel], Tests ++ H1Tests},
56 {https_compress, [parallel], Tests ++ H1Tests},
57 {h2_compress, [parallel], Tests},
58 {h2c_compress, [parallel], Tests ++ H2CTests},
59 {h3_compress, [], Tests}
60 ].
61
62 init_per_suite(Config) ->
63 ct_helper:create_static_dir(config(priv_dir, Config) ++ "/static"),
64 Config.
65
66 end_per_suite(Config) ->
67 ct_helper:delete_static_dir(config(priv_dir, Config) ++ "/static").
68
69 init_per_group(Name, Config) when Name =:= h2; Name =:= h2c;
70 Name =:= h2_compress; Name =:= h2c_compress ->
71 cowboy_test:init_common_groups(Name, Config, ?MODULE,
72 #{max_concurrent_streams => infinity});
73 init_per_group(Name, Config) ->
74 cowboy_test:init_common_groups(Name, Config, ?MODULE).
75
76 end_per_group(Name, _) ->
77 cowboy_test:stop_group(Name).
78
79 %% Routes.
80
81 init_dispatch(_) ->
82 cowboy_router:compile([{"localhost", [
83 {"/", hello_h, []},
84 {"/echo/:key", echo_h, []},
85 {"/delay_hello", delay_hello_h, 1000},
86 {"/long_polling", long_polling_h, []},
87 {"/resp/:key[/:arg]", resp_h, []}
88 ]}]).
89
90 %% Tests.
91
92 http2_cancel_flood(Config) ->
93 doc("Confirm that Cowboy detects the rapid reset attack. (CVE-2023-44487)"),
94 do_http2_cancel_flood(Config, 1, 500),
95 do_http2_cancel_flood(Config, 10, 50),
96 do_http2_cancel_flood(Config, 500, 1),
97 ok.
98
99 do_http2_cancel_flood(Config, NumStreamsPerBatch, NumBatches) ->
100 {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
101 {HeadersBlock, _} = cow_hpack:encode([
102 {<<":method">>, <<"GET">>},
103 {<<":scheme">>, <<"http">>},
104 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
105 {<<":path">>, <<"/delay_hello">>}
106 ]),
107 AllStreamIDs = lists:seq(1, NumBatches * NumStreamsPerBatch * 2, 2),
108 _ = lists:foldl(
109 fun (_BatchNumber, AvailableStreamIDs) ->
110 %% Take a bunch of IDs from the available stream IDs.
111 %% Send HEADERS for all these and then cancel them.
112 {IDs, RemainingStreamIDs} = lists:split(NumStreamsPerBatch, AvailableStreamIDs),
113 _ = gen_tcp:send(Socket, [cow_http2:headers(ID, fin, HeadersBlock) || ID <- IDs]),
114 _ = gen_tcp:send(Socket, [<<4:24, 3:8, 0:8, ID:32, 8:32>> || ID <- IDs]),
115 RemainingStreamIDs
116 end,
117 AllStreamIDs,
118 lists:seq(1, NumBatches, 1)),
119 %% When Cowboy detects a flood it must close the connection.
120 case gen_tcp:recv(Socket, 17, 6000) of
121 {ok, <<_:24, 7:8, 0:8, 0:32, _LastStreamId:32, 11:32>>} ->
122 %% GOAWAY with error code 11 = ENHANCE_YOUR_CALM.
123 ok;
124 %% We also accept the connection being closed immediately,
125 %% which may happen because we send the GOAWAY right before closing.
126 {error, closed} ->
127 ok
128 end.
129
130 http2_data_dribble(Config) ->
131 doc("Request a very large response then update the window 1 byte at a time. (CVE-2019-9511)"),
132 {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
133 %% Send a GET request for a very large response.
134 {HeadersBlock, _} = cow_hpack:encode([
135 {<<":method">>, <<"GET">>},
136 {<<":scheme">>, <<"http">>},
137 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
138 {<<":path">>, <<"/resp/stream_body/loop">>}
139 ]),
140 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
141 %% Receive a response with a few DATA frames draining the window.
142 {ok, <<SkipLen:24, 1:8, _:8, 1:32>>} = gen_tcp:recv(Socket, 9, 1000),
143 {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
144 {ok, <<16384:24, 0:8, 0:8, 1:32, _:16384/unit:8>>} = gen_tcp:recv(Socket, 9 + 16384, 1000),
145 {ok, <<16384:24, 0:8, 0:8, 1:32, _:16384/unit:8>>} = gen_tcp:recv(Socket, 9 + 16384, 1000),
146 {ok, <<16384:24, 0:8, 0:8, 1:32, _:16384/unit:8>>} = gen_tcp:recv(Socket, 9 + 16384, 1000),
147 {ok, <<16383:24, 0:8, 0:8, 1:32, _:16383/unit:8>>} = gen_tcp:recv(Socket, 9 + 16383, 1000),
148 %% Send WINDOW_UPDATE frames with a value of 1. The server should
149 %% not attempt to send data until the window is over a configurable threshold.
150 ok = gen_tcp:send(Socket, [
151 cow_http2:window_update(1),
152 cow_http2:window_update(1, 1)
153 ]),
154 {error, timeout} = gen_tcp:recv(Socket, 0, 1000),
155 ok.
156
157 http2_empty_frame_flooding_data(Config) ->
158 doc("Confirm that Cowboy detects empty DATA frame flooding. (CVE-2019-9518)"),
159 {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
160 %% Send a POST request followed by many empty DATA frames.
161 {HeadersBlock, _} = cow_hpack:encode([
162 {<<":method">>, <<"POST">>},
163 {<<":scheme">>, <<"http">>},
164 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
165 {<<":path">>, <<"/echo/read_body">>}
166 ]),
167 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
168 _ = [gen_tcp:send(Socket, cow_http2:data(1, nofin, <<>>)) || _ <- lists:seq(1, 20000)],
169 %% When Cowboy detects a flood it must close the connection.
170 %% We skip WINDOW_UPDATE frames sent when Cowboy starts to read the body.
171 case gen_tcp:recv(Socket, 43, 6000) of
172 {ok, <<_:26/unit:8, _:24, 7:8, _:72, 11:32>>} ->
173 ok;
174 %% We also accept the connection being closed immediately,
175 %% which may happen because we send the GOAWAY right before closing.
176 {error, closed} ->
177 ok;
178 %% At least on Windows this might also occur.
179 {error, enotconn} ->
180 ok
181 end.
182
183 http2_empty_continuation_frames(Config) ->
184 doc("Confirm that Cowboy rejects empty CONTINUATION frames that do not "
185 "have the END_HEADERS flag set."),
186 {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
187 %% Start a fragmented header block with an empty HEADERS frame.
188 %% Empty HEADERS is allowed because there can only be one.
189 ok = gen_tcp:send(Socket, <<0:24, 1:8, 0:9, 1:31>>),
190 %% Send a single empty CONTINUATION without END_HEADERS.
191 ok = gen_tcp:send(Socket, <<0:24, 9:8, 0:9, 1:31>>),
192 %% Receive an ENHANCE_YOUR_CALM connection error.
193 {ok, <<_:24, 7:8, _:72, 11:32>>} = gen_tcp:recv(Socket, 17, 6000),
194 ok.
195
196 http2_empty_frame_flooding_headers_continuation(Config) ->
197 doc("Confirm that Cowboy detects empty HEADERS/CONTINUATION frame flooding. (CVE-2019-9518)"),
198 {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
199 %% Send many empty HEADERS/CONTINUATION frames before the headers.
200 ok = gen_tcp:send(Socket, <<0:24, 1:8, 0:9, 1:31>>),
201 _ = [gen_tcp:send(Socket, <<0:24, 9:8, 0:9, 1:31>>) || _ <- lists:seq(1, 20000)],
202 {HeadersBlock, _} = cow_hpack:encode([
203 {<<":method">>, <<"POST">>},
204 {<<":scheme">>, <<"http">>},
205 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
206 {<<":path">>, <<"/">>}
207 ]),
208 Len = iolist_size(HeadersBlock),
209 _ = gen_tcp:send(Socket, [<<Len:24, 9:8, 0:5, 1:1, 0:1, 1:1, 0:1, 1:31>>, HeadersBlock]),
210 %% When Cowboy detects a flood it must close the connection.
211 case gen_tcp:recv(Socket, 17, 6000) of
212 {ok, <<_:24, 7:8, _:72, 11:32>>} ->
213 ok;
214 %% We also accept the connection being closed immediately,
215 %% which may happen because we send the GOAWAY right before closing.
216 {error, closed} ->
217 ok;
218 %% At least on Windows this might also occur.
219 {error, enotconn} ->
220 ok
221 end.
222
223 http2_empty_frame_flooding_push_promise(Config) ->
224 doc("Confirm that Cowboy detects empty PUSH_PROMISE frame flooding. (CVE-2019-9518)"),
225 {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
226 %% Send a HEADERS frame to which we will attach a PUSH_PROMISE.
227 %% We use nofin in order to keep the stream alive.
228 {HeadersBlock, _} = cow_hpack:encode([
229 {<<":method">>, <<"GET">>},
230 {<<":scheme">>, <<"http">>},
231 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
232 {<<":path">>, <<"/long_polling">>}
233 ]),
234 ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
235 %% Send nofin PUSH_PROMISE frame without any data.
236 ok = gen_tcp:send(Socket, <<4:24, 5:8, 0:8, 0:1, 1:31, 0:1, 3:31>>),
237 %% Receive a PROTOCOL_ERROR connection error.
238 %%
239 %% Cowboy rejects all PUSH_PROMISE frames therefore no flooding
240 %% can take place.
241 {ok, <<_:24, 7:8, _:72, 1:32>>} = gen_tcp:recv(Socket, 17, 6000),
242 ok.
243
244 http2_hpack_bomb(Config) ->
245 doc("Send many repeated request headers that get compressed "
246 "as 1 byte per header field line. Confirm that the "
247 "connection gets closed. (CVE-2026-49975)"),
248 {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
249 {HeadersBlock, _} = cow_hpack:encode([
250 {<<":method">>, <<"GET">>},
251 {<<":scheme">>, <<"http">>},
252 {<<":authority">>, <<"localhost">>},
253 {<<":path">>, <<"/">>}
254 |lists:duplicate(10000, {<<"cookie">>, <<"a=b">>})
255 ]),
256 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
257 %% Receive an ENHANCE_YOUR_CALM connection error.
258 {ok, <<_:24, 7:8, _:72, 11:32>>} = gen_tcp:recv(Socket, 17, 6000),
259 ok.
260
261 http2_infinite_continuations(Config) ->
262 doc("Confirm that Cowboy rejects CONTINUATION frames when the "
263 "total size of HEADERS + CONTINUATION(s) exceeds the limit. (VU#421644)"),
264 {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
265 %% Send a HEADERS frame followed by a large number
266 %% of continuation frames.
267 {HeadersBlock, _} = cow_hpack:encode([
268 {<<":method">>, <<"GET">>},
269 {<<":scheme">>, <<"http">>},
270 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
271 {<<":path">>, <<"/">>}
272 ]),
273 HeadersBlockLen = iolist_size(HeadersBlock),
274 ok = gen_tcp:send(Socket, [
275 %% HEADERS frame.
276 <<
277 HeadersBlockLen:24, 1:8, 0:5,
278 0:1, %% END_HEADERS
279 0:1,
280 1:1, %% END_STREAM
281 0:1,
282 1:31 %% Stream ID.
283 >>,
284 HeadersBlock,
285 %% CONTINUATION frames.
286 [<<1024:24, 9:8, 0:8, 0:1, 1:31, 0:1024/unit:8>>
287 || _ <- lists:seq(1, 100)]
288 ]),
289 %% Receive an ENHANCE_YOUR_CALM connection error.
290 {ok, <<_:24, 7:8, _:72, 11:32>>} = gen_tcp:recv(Socket, 17, 6000),
291 ok.
292
293 %% @todo http2_internal_data_buffering(Config) -> I do not know how to test this.
294 % doc("Request many very large responses, with a larger than necessary window size, "
295 % "but do not attempt to read from the socket. (CVE-2019-9517)"),
296
297 http2_ping_flood(Config) ->
298 doc("Confirm that Cowboy detects PING floods. (CVE-2019-9512)"),
299 {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
300 %% Flood the server with PING frames.
301 _ = [gen_tcp:send(Socket, cow_http2:ping(0)) || _ <- lists:seq(1, 20000)],
302 %% Receive a number of PING ACK frames in return, following by the closing of the connection.
303 try
304 [case gen_tcp:recv(Socket, 17, 6000) of
305 {ok, <<8:24, 6:8, _:7, 1:1, _:32, 0:64>>} -> ok;
306 {ok, <<_:24, 7:8, _:72, 11:32>>} -> throw(goaway);
307 %% We also accept the connection being closed immediately,
308 %% which may happen because we send the GOAWAY right before closing.
309 {error, closed} -> throw(goaway)
310 end || _ <- lists:seq(1, 20000)],
311 error(flood_successful)
312 catch throw:goaway ->
313 ok
314 end.
315
316 http2_reset_flood(Config) ->
317 doc("Confirm that Cowboy detects reset floods. (CVE-2019-9514)"),
318 {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
319 %% Flood the server with HEADERS frames without a :method pseudo-header.
320 {HeadersBlock, _} = cow_hpack:encode([
321 {<<":scheme">>, <<"http">>},
322 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
323 {<<":path">>, <<"/">>}
324 ]),
325 _ = [gen_tcp:send(Socket, cow_http2:headers(ID, fin, HeadersBlock)) || ID <- lists:seq(1, 100, 2)],
326 %% Receive a number of RST_STREAM frames in return, following by the closing of the connection.
327 try
328 [case gen_tcp:recv(Socket, 13, 6000) of
329 {ok, <<_:24, 3:8, _:8, ID:32, 1:32>>} -> ok;
330 {ok, <<_:24, 7:8, _:72>>} ->
331 {ok, <<11:32>>} = gen_tcp:recv(Socket, 4, 1000),
332 throw(goaway);
333 %% We also accept the connection being closed immediately,
334 %% which may happen because we send the GOAWAY right before closing.
335 {error, closed} ->
336 throw(goaway)
337 end || ID <- lists:seq(1, 100, 2)],
338 error(flood_successful)
339 catch throw:goaway ->
340 ok
341 end.
342
343 %% @todo If we ever implement the PRIORITY mechanism, this test should
344 %% be implemented as well. CVE-2019-9513 https://www.kb.cert.org/vuls/id/605641/
345 %% http2_resource_loop
346
347 http2_settings_flood(Config) ->
348 doc("Confirm that Cowboy detects SETTINGS floods. (CVE-2019-9515)"),
349 {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
350 %% Flood the server with empty SETTINGS frames.
351 _ = [gen_tcp:send(Socket, cow_http2:settings(#{})) || _ <- lists:seq(1, 20000)],
352 %% Receive a number of SETTINGS ACK frames in return, following by the closing of the connection.
353 try
354 [case gen_tcp:recv(Socket, 9, 6000) of
355 {ok, <<0:24, 4:8, 0:7, 1:1, 0:32>>} -> ok;
356 {ok, <<_:24, 7:8, _:40>>} ->
357 {ok, <<_:32, 11:32>>} = gen_tcp:recv(Socket, 8, 1000),
358 throw(goaway);
359 %% We also accept the connection being closed immediately,
360 %% which may happen because we send the GOAWAY right before closing.
361 {error, closed} ->
362 throw(goaway)
363 end || _ <- lists:seq(1, 20000)],
364 error(flood_successful)
365 catch throw:goaway ->
366 ok
367 end.
368
369 http2_zero_length_header_leak(Config) ->
370 doc("Confirm that Cowboy rejects HEADERS frame with a 0-length header name. (CVE-2019-9516)"),
371 {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
372 %% Send a GET request with a 0-length header name.
373 {HeadersBlock, _} = cow_hpack:encode([
374 {<<":method">>, <<"GET">>},
375 {<<":scheme">>, <<"http">>},
376 {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
377 {<<":path">>, <<"/">>},
378 {<<>>, <<"CVE-2019-9516">>}
379 ]),
380 ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
381 %% Receive a PROTOCOL_ERROR stream error.
382 {ok, <<_:24, 3:8, _:8, 1:32, 1:32>>} = gen_tcp:recv(Socket, 13, 6000),
383 ok.
384
385 nc_rand(Config) ->
386 doc("Throw random garbage at the server, then check if it's still up."),
387 do_nc(Config, "/dev/urandom").
388
389 nc_zero(Config) ->
390 doc("Throw zeroes at the server, then check if it's still up."),
391 do_nc(Config, "/dev/zero").
392
393 do_nc(Config, Input) ->
394 Cat = os:find_executable("cat"),
395 Nc = os:find_executable("nc"),
396 case {Cat, Nc} of
397 {false, _} ->
398 {skip, "The cat executable was not found."};
399 {_, false} ->
400 {skip, "The nc executable was not found."};
401 _ ->
402 StrPort = integer_to_list(config(port, Config)),
403 os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort),
404 ConnPid = gun_open(Config),
405 Ref = gun:get(ConnPid, "/"),
406 {response, _, 200, _} = gun:await(ConnPid, Ref),
407 ok
408 end.
409
410 slowloris(Config) ->
411 doc("Send request headers one byte at a time. "
412 "Confirm that the connection gets closed."),
413 Client = raw_open(Config),
414 try
415 [begin
416 ok = raw_send(Client, [C]),
417 timer:sleep(250)
418 end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
419 "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
420 "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
421 error(failure)
422 catch error:{badmatch, _} ->
423 ok
424 end.
425
426 slowloris_chunks(Config) ->
427 doc("Send request headers one line at a time. "
428 "Confirm that the connection gets closed."),
429 Client = raw_open(Config),
430 ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
431 timer:sleep(300),
432 ok = raw_send(Client, "Host: localhost\r\n"),
433 timer:sleep(300),
434 Data = raw_recv_head(Client),
435 {'HTTP/1.1', 408, _, Rest} = cow_http:parse_status_line(Data),
436 {Headers, _} = cow_http:parse_headers(Rest),
437 {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
438 {error, closed} = raw_recv(Client, 0, 1000).