Debian Codegraph @

test/decompress_SUITE.erl

raw
1 %% Copyright (c) jdamanalo <[email protected]>
2 %% Copyright (c) Loïc Hoguin <[email protected]>
3 %%
4 %% Permission to use, copy, modify, and/or distribute this software for any
5 %% purpose with or without fee is hereby granted, provided that the above
6 %% copyright notice and this permission notice appear in all copies.
7 %%
8 %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16 -module(decompress_SUITE).
17 -compile(export_all).
18 -compile(nowarn_export_all).
19
20 -import(ct_helper, [config/2]).
21 -import(ct_helper, [doc/1]).
22 -import(cowboy_test, [gun_open/1]).
23
24 %% ct.
25
26 all() ->
27 cowboy_test:common_all().
28
29 groups() ->
30 cowboy_test:common_groups(ct_helper:all(?MODULE)).
31
32 init_per_group(Name = http, Config) ->
33 cowboy_test:init_http(Name, init_plain_opts(Config), Config);
34 init_per_group(Name = https, Config) ->
35 cowboy_test:init_http(Name, init_plain_opts(Config), Config);
36 init_per_group(Name = h2, Config) ->
37 cowboy_test:init_http2(Name, init_plain_opts(Config), Config);
38 init_per_group(Name = h2c, Config) ->
39 Config1 = cowboy_test:init_http(Name, init_plain_opts(Config), Config),
40 lists:keyreplace(protocol, 1, Config1, {protocol, http2});
41 init_per_group(Name = h3, Config) ->
42 cowboy_test:init_http3(Name, init_plain_opts(Config), Config);
43 init_per_group(Name = http_compress, Config) ->
44 cowboy_test:init_http(Name, init_compress_opts(Config), Config);
45 init_per_group(Name = https_compress, Config) ->
46 cowboy_test:init_http(Name, init_compress_opts(Config), Config);
47 init_per_group(Name = h2_compress, Config) ->
48 cowboy_test:init_http2(Name, init_compress_opts(Config), Config);
49 init_per_group(Name = h2c_compress, Config) ->
50 Config1 = cowboy_test:init_http(Name, init_compress_opts(Config), Config),
51 lists:keyreplace(protocol, 1, Config1, {protocol, http2});
52 init_per_group(Name = h3_compress, Config) ->
53 cowboy_test:init_http3(Name, init_compress_opts(Config), Config).
54
55 end_per_group(Name, _) ->
56 cowboy:stop_listener(Name).
57
58 init_plain_opts(Config) ->
59 #{
60 env => #{dispatch => cowboy_router:compile(init_routes(Config))},
61 stream_handlers => [cowboy_decompress_h, cowboy_stream_h]
62 }.
63
64 init_compress_opts(Config) ->
65 #{
66 env => #{dispatch => cowboy_router:compile(init_routes(Config))},
67 stream_handlers => [cowboy_decompress_h, cowboy_compress_h, cowboy_stream_h]
68 }.
69
70 init_routes(_) ->
71 [{'_', [
72 {"/echo/:what", decompress_h, echo},
73 {"/test/:what", decompress_h, test}
74 ]}].
75
76 %% Internal.
77
78 do_post(Path, ReqHeaders, Body, Config) ->
79 ConnPid = gun_open(Config),
80 Ref = gun:post(ConnPid, Path, ReqHeaders, Body),
81 {response, IsFin, Status, RespHeaders} = gun:await(ConnPid, Ref),
82 {ok, ResponseBody} = case IsFin of
83 nofin -> gun:await_body(ConnPid, Ref);
84 fin -> {ok, <<>>}
85 end,
86 gun:close(ConnPid),
87 {Status, RespHeaders, ResponseBody}.
88
89 create_gzip_bomb() ->
90 Z = zlib:open(),
91 zlib:deflateInit(Z, 9, deflated, 31, 8, default),
92 %% 1000 chunks of 100000 zeroes (100MB).
93 Bomb = do_create_gzip_bomb(Z, 1000),
94 zlib:deflateEnd(Z),
95 zlib:close(Z),
96 iolist_to_binary(Bomb).
97
98 do_create_gzip_bomb(Z, 0) ->
99 zlib:deflate(Z, << >>, finish);
100 do_create_gzip_bomb(Z, N) ->
101 Data = <<0:800000>>,
102 Deflate = zlib:deflate(Z, Data),
103 [Deflate | do_create_gzip_bomb(Z, N - 1)].
104
105 %% Tests.
106
107 content_encoding_none(Config) ->
108 doc("Requests without content-encoding are processed normally."),
109 Body = <<"test">>,
110 {200, _, Body} = do_post("/echo/normal", [], Body, Config),
111 %% The content-encoding header would be propagated,
112 %% but there was no content-encoding header to propagate.
113 {200, _, <<"undefined">>} = do_post("/test/content-encoding", [], Body, Config),
114 %% The content_decoded list is empty.
115 {200, _, <<"[]">>} = do_post("/test/content-decoded", [], Body, Config),
116 ok.
117
118 content_encoding_malformed(Config) ->
119 doc("Requests with a malformed content-encoding are processed "
120 "as if no content-encoding was sent."),
121 Body = <<"test">>,
122 {200, _, Body} = do_post("/echo/normal",
123 [{<<"content-encoding">>, <<";">>}], Body, Config),
124 %% The content-encoding header is propagated.
125 {200, _, <<";">>} = do_post("/test/content-encoding",
126 [{<<"content-encoding">>, <<";">>}], Body, Config),
127 %% The content_decoded list is empty.
128 {200, _, <<"[]">>} = do_post("/test/content-decoded",
129 [{<<"content-encoding">>, <<";">>}], Body, Config),
130 ok.
131
132 content_encoding_not_supported(Config) ->
133 doc("Requests with an unsupported content-encoding are processed "
134 "as if no content-encoding was sent."),
135 Body = <<"test">>,
136 {200, _, Body} = do_post("/echo/normal",
137 [{<<"content-encoding">>, <<"compress">>}], Body, Config),
138 %% The content-encoding header is propagated.
139 {200, _, <<"compress">>} = do_post("/test/content-encoding",
140 [{<<"content-encoding">>, <<"compress">>}], Body, Config),
141 %% The content_decoded list is empty.
142 {200, _, <<"[]">>} = do_post("/test/content-decoded",
143 [{<<"content-encoding">>, <<"compress">>}], Body, Config),
144 ok.
145
146 content_encoding_multiple(Config) ->
147 doc("Requests with multiple content-encoding values are processed "
148 "as if no content-encoding was sent."),
149 Body = <<"test">>,
150 {200, _, Body} = do_post("/echo/normal",
151 [{<<"content-encoding">>, <<"gzip, compress">>}], Body, Config),
152 %% The content-encoding header is propagated.
153 {200, _, <<"gzip, compress">>} = do_post("/test/content-encoding",
154 [{<<"content-encoding">>, <<"gzip, compress">>}], Body, Config),
155 %% The content_decoded list is empty.
156 {200, _, <<"[]">>} = do_post("/test/content-decoded",
157 [{<<"content-encoding">>, <<"gzip, compress">>}], Body, Config),
158 ok.
159
160 decompress(Config) ->
161 doc("Requests with content-encoding set to gzip and gzipped data "
162 "are transparently decompressed."),
163 Data = <<"test">>,
164 Body = zlib:gzip(Data),
165 {200, _, Data} = do_post("/echo/normal",
166 [{<<"content-encoding">>, <<"gzip">>}], Body, Config),
167 %% The content-encoding header is NOT propagated.
168 {200, _, <<"undefined">>} = do_post("/test/content-encoding",
169 [{<<"content-encoding">>, <<"gzip">>}], Body, Config),
170 %% The content_decoded list contains <<"gzip">>.
171 {200, _, <<"[<<\"gzip\">>]">>} = do_post("/test/content-decoded",
172 [{<<"content-encoding">>, <<"gzip">>}], Body, Config),
173 ok.
174
175 decompress_error(Config) ->
176 doc("Requests with content-encoding set to gzip but the data "
177 "cannot be decoded are rejected with a 400 Bad Request error."),
178 Body = <<"test">>,
179 {400, _, _} = do_post("/echo/normal",
180 [{<<"content-encoding">>, <<"gzip">>}], Body, Config),
181 ok.
182
183 decompress_stream(Config) ->
184 doc("Requests with content-encoding set to gzip and gzipped data "
185 "are transparently decompressed, even when the data is streamed."),
186 %% Handler read length 1KB. Compressing 3KB should be enough to trigger more.
187 Data = crypto:strong_rand_bytes(3000),
188 Body = zlib:gzip(Data),
189 Size = byte_size(Body),
190 ConnPid = gun_open(Config),
191 Ref = gun:post(ConnPid, "/echo/normal",
192 [{<<"content-encoding">>, <<"gzip">>}]),
193 gun:data(ConnPid, Ref, nofin, binary:part(Body, 0, Size div 2)),
194 timer:sleep(1000),
195 gun:data(ConnPid, Ref, fin, binary:part(Body, Size div 2, Size div 2 + Size rem 2)),
196 {response, IsFin, 200, _} = gun:await(ConnPid, Ref),
197 {ok, Data} = case IsFin of
198 nofin -> gun:await_body(ConnPid, Ref);
199 fin -> {ok, <<>>}
200 end,
201 gun:close(ConnPid),
202 %% The content-encoding header is NOT propagated.
203 ConnPid2 = gun_open(Config),
204 Ref2 = gun:post(ConnPid2, "/test/content-encoding",
205 [{<<"content-encoding">>, <<"gzip">>}]),
206 {response, nofin, 200, _} = gun:await(ConnPid2, Ref2),
207 {ok, <<"undefined">>} = gun:await_body(ConnPid2, Ref2),
208 gun:close(ConnPid2),
209 %% The content_decoded list contains <<"gzip">>.
210 ConnPid3 = gun_open(Config),
211 Ref3 = gun:post(ConnPid3, "/test/content-decoded",
212 [{<<"content-encoding">>, <<"gzip">>}]),
213 {response, nofin, 200, _} = gun:await(ConnPid3, Ref3),
214 {ok, <<"[<<\"gzip\">>]">>} = gun:await_body(ConnPid3, Ref3),
215 gun:close(ConnPid3).
216
217 opts_decompress_enabled_false(Config0) ->
218 doc("Confirm that the decompress_enabled option can be set."),
219 Fun = case config(ref, Config0) of
220 HTTPS when HTTPS =:= https_compress; HTTPS =:= https -> init_https;
221 H2 when H2 =:= h2_compress; H2 =:= h2 -> init_http2;
222 _ -> init_http
223 end,
224 Config = cowboy_test:Fun(?FUNCTION_NAME, #{
225 env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
226 stream_handlers => [cowboy_decompress_h, cowboy_stream_h],
227 decompress_enabled => false
228 }, Config0),
229 Data = <<"test">>,
230 Body = zlib:gzip(Data),
231 try
232 {200, Headers, Body} = do_post("/echo/normal",
233 [{<<"content-encoding">>, <<"gzip">>}], Body, Config),
234 %% We do not set accept-encoding when we are disabled.
235 false = lists:keyfind(<<"accept-encoding">>, 1, Headers)
236 after
237 cowboy:stop_listener(?FUNCTION_NAME)
238 end.
239
240 set_options_decompress_enabled_false(Config) ->
241 doc("Confirm that the decompress_enabled option can be dynamically "
242 "set to false and the data received is not decompressed."),
243 Data = <<"test">>,
244 Body = zlib:gzip(Data),
245 {200, Headers, Body} = do_post("/echo/decompress_disable",
246 [{<<"content-encoding">>, <<"gzip">>}], Body, Config),
247 %% We do not set accept-encoding when we are disabled.
248 false = lists:keyfind(<<"accept-encoding">>, 1, Headers),
249 ok.
250
251 set_options_decompress_disable_in_the_middle(Config) ->
252 doc("Confirm that setting the decompress_enabled option dynamically "
253 "to false after starting to read the body does not disable decompression "
254 "and the data received is decompressed."),
255 Data = rand:bytes(1000000),
256 Body = zlib:gzip(Data),
257 %% Since we were not ignoring before starting to read,
258 %% we receive the entire body decompressed.
259 {200, Headers, Data} = do_post("/test/disable-in-the-middle",
260 [{<<"content-encoding">>, <<"gzip">>}], Body, Config),
261 %% We do set accept-encoding when we are enabled,
262 %% even if an attempt to disable in the middle is ignored.
263 {_, _} = lists:keyfind(<<"accept-encoding">>, 1, Headers),
264 ok.
265
266 set_options_decompress_enable_in_the_middle(Config0) ->
267 doc("Confirm that setting the decompress_enabled option dynamically "
268 "to true after starting to read the body does not enable decompression "
269 "and the data received is not decompressed."),
270 Fun = case config(ref, Config0) of
271 HTTPS when HTTPS =:= https_compress; HTTPS =:= https -> init_https;
272 H2 when H2 =:= h2_compress; H2 =:= h2 -> init_http2;
273 _ -> init_http
274 end,
275 Config = cowboy_test:Fun(?FUNCTION_NAME, #{
276 env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
277 stream_handlers => [cowboy_decompress_h, cowboy_stream_h],
278 decompress_enabled => false
279 }, Config0),
280 Data = rand:bytes(1000000),
281 Body = zlib:gzip(Data),
282 try
283 %% Since we were ignoring before starting to read,
284 %% we receive the entire body compressed.
285 {200, Headers, Body} = do_post("/test/enable-in-the-middle",
286 [{<<"content-encoding">>, <<"gzip">>}], Body, Config),
287 %% We do not set accept-encoding when we are disabled,
288 %% even if an attempt to enable in the middle is ignored.
289 false = lists:keyfind(<<"accept-encoding">>, 1, Headers)
290 after
291 cowboy:stop_listener(?FUNCTION_NAME)
292 end.
293
294 opts_decompress_ratio_limit(Config0) ->
295 doc("Confirm that the decompress_ratio_limit option can be set."),
296 Fun = case config(ref, Config0) of
297 HTTPS when HTTPS =:= https_compress; HTTPS =:= https -> init_https;
298 H2 when H2 =:= h2_compress; H2 =:= h2 -> init_http2;
299 _ -> init_http
300 end,
301 Config = cowboy_test:Fun(?FUNCTION_NAME, #{
302 env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
303 stream_handlers => [cowboy_decompress_h, cowboy_stream_h],
304 decompress_ratio_limit => 1
305 }, Config0),
306 %% Data must be big enough for compression to be effective,
307 %% so that ratio_limit=1 will fail.
308 Data = <<0:800>>,
309 Body = zlib:gzip(Data),
310 try
311 {413, _, _} = do_post("/echo/normal",
312 [{<<"content-encoding">>, <<"gzip">>}], Body, Config)
313 after
314 cowboy:stop_listener(?FUNCTION_NAME)
315 end.
316
317 set_options_decompress_ratio_limit(Config) ->
318 doc("Confirm that the decompress_ratio_limit option can be dynamically set."),
319 %% Data must be big enough for compression to be effective,
320 %% so that ratio_limit=1 will fail.
321 Data = <<0:800>>,
322 Body = zlib:gzip(Data),
323 {413, _, _} = do_post("/echo/decompress_ratio_limit",
324 [{<<"content-encoding">>, <<"gzip">>}], Body, Config),
325 ok.
326
327 gzip_bomb(Config) ->
328 doc("Confirm that requests are rejected with a 413 Payload Too Large "
329 "error when the ratio limit is exceeded."),
330 Body = create_gzip_bomb(),
331 {413, _, _} = do_post("/echo/normal",
332 [{<<"content-encoding">>, <<"gzip">>}], Body, Config),
333 ok.
334
335 set_accept_encoding_response(Config) ->
336 doc("Header accept-encoding must be set on valid response command. "
337 "(RFC9110 12.5.3)"),
338 Data = <<"test">>,
339 Body = zlib:gzip(Data),
340 {200, Headers, Data} = do_post("/echo/normal",
341 [{<<"content-encoding">>, <<"gzip">>}], Body, Config),
342 {_, <<"gzip">>} = lists:keyfind(<<"accept-encoding">>, 1, Headers),
343 ok.
344
345 set_accept_encoding_header(Config) ->
346 doc("Header accept-encoding must be set on valid header command. "
347 "(RFC9110 12.5.3)"),
348 Data = <<"test">>,
349 Body = zlib:gzip(Data),
350 {200, Headers, Data} = do_post("/test/header-command",
351 [{<<"content-encoding">>, <<"gzip">>}], Body, Config),
352 {_, <<"gzip">>} = lists:keyfind(<<"accept-encoding">>, 1, Headers),
353 ok.
354
355 add_accept_encoding_header_valid(Config) ->
356 doc("Supported content codings must be added to the accept-encoding "
357 "header if it already exists. (RFC9110 12.5.3)"),
358 Data = <<"test">>,
359 Body = zlib:gzip(Data),
360 {200, Headers, Data} = do_post("/test/accept-identity",
361 [{<<"content-encoding">>, <<"gzip">>}], Body, Config),
362 {_, <<"identity, gzip">>} = lists:keyfind(<<"accept-encoding">>, 1, Headers),
363 ok.
364
365 override_accept_encoding_header_invalid(Config) ->
366 doc("When the stream handler cannot parse the accept-encoding header "
367 "found in the response, it overrides it."),
368 Data = <<"test">>,
369 Body = zlib:gzip(Data),
370 {200, Headers, Data} = do_post("/test/invalid-header",
371 [{<<"content-encoding">>, <<"gzip">>}], Body, Config),
372 {_, <<"gzip">>} = lists:keyfind(<<"accept-encoding">>, 1, Headers),
373 ok.
374
375 override_accept_encoding_excluded(Config) ->
376 doc("The stream handler must ensure that the content encodings "
377 "it supports are not marked as unsupported in response headers. "
378 "The stream handler enables gzip when explicitly excluded. "
379 "(RFC9110 12.5.3)"),
380 Data = <<"test">>,
381 Body = zlib:gzip(Data),
382 {200, Headers, Data} = do_post("/test/reject-explicit-header",
383 [{<<"content-encoding">>, <<"gzip">>}], Body, Config),
384 {_, <<"identity;q=1, gzip;q=1">>} = lists:keyfind(<<"accept-encoding">>, 1, Headers),
385 ok.
386
387 %% *;q=0 will reject codings that are not listed. Supported codings
388 %% must always be enabled when the handler is used.
389 add_accept_encoding_excluded(Config) ->
390 doc("The stream handler must ensure that the content encodings "
391 "it supports are not marked as unsupported in response headers. "
392 "The stream handler enables gzip when implicitly excluded (*;q=0). "
393 "(RFC9110 12.5.3)"),
394 Data = <<"test">>,
395 Body = zlib:gzip(Data),
396 {200, Headers, Data} = do_post("/test/reject-implicit-header",
397 [{<<"content-encoding">>, <<"gzip">>}], Body, Config),
398 {_, <<"gzip;q=1, identity;q=1, *;q=0">>} = lists:keyfind(<<"accept-encoding">>, 1, Headers),
399 ok.
400
401 no_override_accept_coding_set_explicit(Config) ->
402 doc("Confirm that accept-encoding is not overridden when the "
403 "content encodings it supports are explicitly set. "
404 "(RFC9110 12.5.3)"),
405 Data = <<"test">>,
406 Body = zlib:gzip(Data),
407 {200, Headers, Data} = do_post("/test/accept-explicit-header",
408 [{<<"content-encoding">>, <<"gzip">>}], Body, Config),
409 {_, <<"identity, gzip;q=0.5">>} = lists:keyfind(<<"accept-encoding">>, 1, Headers),
410 ok.
411
412 no_override_accept_coding_set_implicit(Config) ->
413 doc("Confirm that accept-encoding is not overridden when the "
414 "content encodings it supports are implicitly set. "
415 "(RFC9110 12.5.3)"),
416 Data = <<"test">>,
417 Body = zlib:gzip(Data),
418 {200, Headers, Data} = do_post("/test/accept-implicit-header",
419 [{<<"content-encoding">>, <<"gzip">>}], Body, Config),
420 {_, <<"identity, *;q=0.5">>} = lists:keyfind(<<"accept-encoding">>, 1, Headers),
421 ok.