aboutsummaryrefslogtreecommitdiffstats
path: root/test/decompress_SUITE.erl
blob: f61bb5df025c2779b51c1334dcd75cd9ae71d8ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
%% Copyright (c) 2024, jdamanalo <[email protected]>
%% Copyright (c) 2024, Loïc Hoguin <[email protected]>
%%
%% Permission to use, copy, modify, and/or distribute this software for any
%% purpose with or without fee is hereby granted, provided that the above
%% copyright notice and this permission notice appear in all copies.
%%
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

-module(decompress_SUITE).
-compile(export_all).
-compile(nowarn_export_all).

-import(ct_helper, [config/2]).
-import(ct_helper, [doc/1]).
-import(cowboy_test, [gun_open/1]).

%% ct.

all() ->
	cowboy_test:common_all().

groups() ->
	cowboy_test:common_groups(ct_helper:all(?MODULE)).

init_per_group(Name = http, Config) ->
	cowboy_test:init_http(Name, init_plain_opts(Config), Config);
init_per_group(Name = https, Config) ->
	cowboy_test:init_http(Name, init_plain_opts(Config), Config);
init_per_group(Name = h2, Config) ->
	cowboy_test:init_http2(Name, init_plain_opts(Config), Config);
init_per_group(Name = h2c, Config) ->
	Config1 = cowboy_test:init_http(Name, init_plain_opts(Config), Config),
	lists:keyreplace(protocol, 1, Config1, {protocol, http2});
init_per_group(Name = h3, Config) ->
	cowboy_test:init_http3(Name, init_plain_opts(Config), Config);
init_per_group(Name = http_compress, Config) ->
	cowboy_test:init_http(Name, init_compress_opts(Config), Config);
init_per_group(Name = https_compress, Config) ->
	cowboy_test:init_http(Name, init_compress_opts(Config), Config);
init_per_group(Name = h2_compress, Config) ->
	cowboy_test:init_http2(Name, init_compress_opts(Config), Config);
init_per_group(Name = h2c_compress, Config) ->
	Config1 = cowboy_test:init_http(Name, init_compress_opts(Config), Config),
	lists:keyreplace(protocol, 1, Config1, {protocol, http2});
init_per_group(Name = h3_compress, Config) ->
	cowboy_test:init_http3(Name, init_compress_opts(Config), Config).

end_per_group(Name, _) ->
	cowboy:stop_listener(Name).

init_plain_opts(Config) ->
	#{
		env => #{dispatch => cowboy_router:compile(init_routes(Config))},
		stream_handlers => [cowboy_decompress_h, cowboy_stream_h]
	}.

init_compress_opts(Config) ->
	#{
		env => #{dispatch => cowboy_router:compile(init_routes(Config))},
		stream_handlers => [cowboy_decompress_h, cowboy_compress_h, cowboy_stream_h]
	}.

init_routes(_) ->
	[{'_', [
		{"/echo/:what", decompress_h, echo},
		{"/test/:what", decompress_h, test}
	]}].

%% Internal.

do_post(Path, ReqHeaders, Body, Config) ->
	ConnPid = gun_open(Config),
	Ref = gun:post(ConnPid, Path, ReqHeaders, Body),
	{response, IsFin, Status, RespHeaders} = gun:await(ConnPid, Ref),
	{ok, ResponseBody} = case IsFin of
		nofin -> gun:await_body(ConnPid, Ref);
		fin -> {ok, <<>>}
	end,
	gun:close(ConnPid),
	{Status, RespHeaders, ResponseBody}.

create_gzip_bomb() ->
	Z = zlib:open(),
	zlib:deflateInit(Z, 9, deflated, 31, 8, default),
	%% 1000 chunks of 100000 zeroes (100MB).
	Bomb = do_create_gzip_bomb(Z, 1000),
	zlib:deflateEnd(Z),
	zlib:close(Z),
	iolist_to_binary(Bomb).

do_create_gzip_bomb(Z, 0) ->
	zlib:deflate(Z, << >>, finish);
do_create_gzip_bomb(Z, N) ->
	Data = <<0:800000>>,
	Deflate = zlib:deflate(Z, Data),
	[Deflate | do_create_gzip_bomb(Z, N - 1)].

%% Tests.

content_encoding_none(Config) ->
	doc("Requests without content-encoding are processed normally."),
	Body = <<"test">>,
	{200, _, Body} = do_post("/echo/normal", [], Body, Config),
	%% The content-encoding header would be propagated,
	%% but there was no content-encoding header to propagate.
	{200, _, <<"undefined">>} = do_post("/test/content-encoding", [], Body, Config),
	%% The content_decoded list is empty.
	{200, _, <<"[]">>} = do_post("/test/content-decoded", [], Body, Config),
	ok.

content_encoding_malformed(Config) ->
	doc("Requests with a malformed content-encoding are processed "
		"as if no content-encoding was sent."),
	Body = <<"test">>,
	{200, _, Body} = do_post("/echo/normal",
		[{<<"content-encoding">>, <<";">>}], Body, Config),
	%% The content-encoding header is propagated.
	{200, _, <<";">>} = do_post("/test/content-encoding",
		[{<<"content-encoding">>, <<";">>}], Body, Config),
	%% The content_decoded list is empty.
	{200, _, <<"[]">>} = do_post("/test/content-decoded",
		[{<<"content-encoding">>, <<";">>}], Body, Config),
	ok.

content_encoding_not_supported(Config) ->
	doc("Requests with an unsupported content-encoding are processed "
		"as if no content-encoding was sent."),
	Body = <<"test">>,
	{200, _, Body} = do_post("/echo/normal",
		[{<<"content-encoding">>, <<"compress">>}], Body, Config),
	%% The content-encoding header is propagated.
	{200, _, <<"compress">>} = do_post("/test/content-encoding",
		[{<<"content-encoding">>, <<"compress">>}], Body, Config),
	%% The content_decoded list is empty.
	{200, _, <<"[]">>} = do_post("/test/content-decoded",
		[{<<"content-encoding">>, <<"compress">>}], Body, Config),
	ok.

content_encoding_multiple(Config) ->
	doc("Requests with multiple content-encoding values are processed "
		"as if no content-encoding was sent."),
	Body = <<"test">>,
	{200, _, Body} = do_post("/echo/normal",
		[{<<"content-encoding">>, <<"gzip, compress">>}], Body, Config),
	%% The content-encoding header is propagated.
	{200, _, <<"gzip, compress">>} = do_post("/test/content-encoding",
		[{<<"content-encoding">>, <<"gzip, compress">>}], Body, Config),
	%% The content_decoded list is empty.
	{200, _, <<"[]">>} = do_post("/test/content-decoded",
		[{<<"content-encoding">>, <<"gzip, compress">>}], Body, Config),
	ok.

decompress(Config) ->
	doc("Requests with content-encoding set to gzip and gzipped data "
		"are transparently decompressed."),
	Data = <<"test">>,
	Body = zlib:gzip(Data),
	{200, _, Data} = do_post("/echo/normal",
		[{<<"content-encoding">>, <<"gzip">>}], Body, Config),
	%% The content-encoding header is NOT propagated.
	{200, _, <<"undefined">>} = do_post("/test/content-encoding",
		[{<<"content-encoding">>, <<"gzip">>}], Body, Config),
	%% The content_decoded list contains <<"gzip">>.
	{200, _, <<"[<<\"gzip\">>]">>} = do_post("/test/content-decoded",
		[{<<"content-encoding">>, <<"gzip">>}], Body, Config),
	ok.

decompress_error(Config) ->
	doc("Requests with content-encoding set to gzip but the data "
		"cannot be decoded are rejected with a 400 Bad Request error."),
	Body = <<"test">>,
	{400, _, _} = do_post("/echo/normal",
		[{<<"content-encoding">>, <<"gzip">>}], Body, Config),
	ok.

decompress_stream(Config) ->
	doc("Requests with content-encoding set to gzip and gzipped data "
		"are transparently decompressed, even when the data is streamed."),
	%% Handler read length 1KB. Compressing 3KB should be enough to trigger more.
	Data = crypto:strong_rand_bytes(3000),
	Body = zlib:gzip(Data),
	Size = byte_size(Body),
	ConnPid = gun_open(Config),
	Ref = gun:post(ConnPid, "/echo/normal",
		[{<<"content-encoding">>, <<"gzip">>}]),
	gun:data(ConnPid, Ref, nofin, binary:part(Body, 0, Size div 2)),
	timer:sleep(1000),
	gun:data(ConnPid, Ref, fin, binary:part(Body, Size div 2, Size div 2 + Size rem 2)),
	{response, IsFin, 200, _} = gun:await(ConnPid, Ref),
	{ok, Data} = case IsFin of
		nofin -> gun:await_body(ConnPid, Ref);
		fin -> {ok, <<>>}
	end,
	gun:close(ConnPid),
	%% The content-encoding header is NOT propagated.
	ConnPid2 = gun_open(Config),
	Ref2 = gun:post(ConnPid2, "/test/content-encoding",
		[{<<"content-encoding">>, <<"gzip">>}]),
	{response, nofin, 200, _} = gun:await(ConnPid2, Ref2),
	{ok, <<"undefined">>} = gun:await_body(ConnPid2, Ref2),
	gun:close(ConnPid2),
	%% The content_decoded list contains <<"gzip">>.
	ConnPid3 = gun_open(Config),
	Ref3 = gun:post(ConnPid3, "/test/content-decoded",
		[{<<"content-encoding">>, <<"gzip">>}]),
	{response, nofin, 200, _} = gun:await(ConnPid3, Ref3),
	{ok, <<"[<<\"gzip\">>]">>} = gun:await_body(ConnPid3, Ref3),
	gun:close(ConnPid3).

opts_decompress_enabled_false(Config0) ->
	doc("Confirm that the decompress_enabled option can be set."),
	Fun = case config(ref, Config0) of
		HTTPS when HTTPS =:= https_compress; HTTPS =:= https -> init_https;
		H2 when H2 =:= h2_compress; H2 =:= h2 -> init_http2;
		_ -> init_http
	end,
	Config = cowboy_test:Fun(?FUNCTION_NAME, #{
		env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
		stream_handlers => [cowboy_decompress_h, cowboy_stream_h],
		decompress_enabled => false
	}, Config0),
	Data = <<"test">>,
	Body = zlib:gzip(Data),
	try
		{200, Headers, Body} = do_post("/echo/normal",
			[{<<"content-encoding">>, <<"gzip">>}], Body, Config),
		%% We do not set accept-encoding when we are disabled.
		false = lists:keyfind(<<"accept-encoding">>, 1, Headers)
	after
		cowboy:stop_listener(?FUNCTION_NAME)
	end.

set_options_decompress_enabled_false(Config) ->
	doc("Confirm that the decompress_enabled option can be dynamically "
		"set to false and the data received is not decompressed."),
	Data = <<"test">>,
	Body = zlib:gzip(Data),
	{200, Headers, Body} = do_post("/echo/decompress_disable",
		[{<<"content-encoding">>, <<"gzip">>}], Body, Config),
	%% We do not set accept-encoding when we are disabled.
	false = lists:keyfind(<<"accept-encoding">>, 1, Headers),
	ok.

set_options_decompress_disable_in_the_middle(Config) ->
	doc("Confirm that setting the decompress_enabled option dynamically "
		"to false after starting to read the body does not disable decompression "
		"and the data received is decompressed."),
	Data = rand:bytes(1000000),
	Body = zlib:gzip(Data),
	%% Since we were not ignoring before starting to read,
	%% we receive the entire body decompressed.
	{200, Headers, Data} = do_post("/test/disable-in-the-middle",
		[{<<"content-encoding">>, <<"gzip">>}], Body, Config),
	%% We do set accept-encoding when we are enabled,
	%% even if an attempt to disable in the middle is ignored.
	{_, _} = lists:keyfind(<<"accept-encoding">>, 1, Headers),
	ok.

set_options_decompress_enable_in_the_middle(Config0) ->
	doc("Confirm that setting the decompress_enabled option dynamically "
		"to true after starting to read the body does not enable decompression "
		"and the data received is not decompressed."),
	Fun = case config(ref, Config0) of
		HTTPS when HTTPS =:= https_compress; HTTPS =:= https -> init_https;
		H2 when H2 =:= h2_compress; H2 =:= h2 -> init_http2;
		_ -> init_http
	end,
	Config = cowboy_test:Fun(?FUNCTION_NAME, #{
		env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
		stream_handlers => [cowboy_decompress_h, cowboy_stream_h],
		decompress_enabled => false
	}, Config0),
	Data = rand:bytes(1000000),
	Body = zlib:gzip(Data),
	try
		%% Since we were ignoring before starting to read,
		%% we receive the entire body compressed.
		{200, Headers, Body} = do_post("/test/enable-in-the-middle",
			[{<<"content-encoding">>, <<"gzip">>}], Body, Config),
		%% We do not set accept-encoding when we are disabled,
		%% even if an attempt to enable in the middle is ignored.
		false = lists:keyfind(<<"accept-encoding">>, 1, Headers)
	after
		cowboy:stop_listener(?FUNCTION_NAME)
	end.

opts_decompress_ratio_limit(Config0) ->
	doc("Confirm that the decompress_ratio_limit option can be set."),
	Fun = case config(ref, Config0) of
		HTTPS when HTTPS =:= https_compress; HTTPS =:= https -> init_https;
		H2 when H2 =:= h2_compress; H2 =:= h2 -> init_http2;
		_ -> init_http
	end,
	Config = cowboy_test:Fun(?FUNCTION_NAME, #{
		env => #{dispatch => cowboy_router:compile(init_routes(Config0))},
		stream_handlers => [cowboy_decompress_h, cowboy_stream_h],
		decompress_ratio_limit => 1
	}, Config0),
	%% Data must be big enough for compression to be effective,
	%% so that ratio_limit=1 will fail.
	Data = <<0:800>>,
	Body = zlib:gzip(Data),
	try
		{413, _, _} = do_post("/echo/normal",
			[{<<"content-encoding">>, <<"gzip">>}], Body, Config)
	after
		cowboy:stop_listener(?FUNCTION_NAME)
	end.

set_options_decompress_ratio_limit(Config) ->
	doc("Confirm that the decompress_ratio_limit option can be dynamically set."),
	%% Data must be big enough for compression to be effective,
	%% so that ratio_limit=1 will fail.
	Data = <<0:800>>,
	Body = zlib:gzip(Data),
	{413, _, _} = do_post("/echo/decompress_ratio_limit",
		[{<<"content-encoding">>, <<"gzip">>}], Body, Config),
	ok.

gzip_bomb(Config) ->
	doc("Confirm that requests are rejected with a 413 Payload Too Large "
		"error when the ratio limit is exceeded."),
	Body = create_gzip_bomb(),
	{413, _, _} = do_post("/echo/normal",
		[{<<"content-encoding">>, <<"gzip">>}], Body, Config),
	ok.

set_accept_encoding_response(Config) ->
	doc("Header accept-encoding must be set on valid response command. "
		"(RFC9110 12.5.3)"),
	Data = <<"test">>,
	Body = zlib:gzip(Data),
	{200, Headers, Data} = do_post("/echo/normal",
		[{<<"content-encoding">>, <<"gzip">>}], Body, Config),
	{_, <<"gzip">>} = lists:keyfind(<<"accept-encoding">>, 1, Headers),
	ok.

set_accept_encoding_header(Config) ->
	doc("Header accept-encoding must be set on valid header command. "
		"(RFC9110 12.5.3)"),
	Data = <<"test">>,
	Body = zlib:gzip(Data),
	{200, Headers, Data} = do_post("/test/header-command",
		[{<<"content-encoding">>, <<"gzip">>}], Body, Config),
	{_, <<"gzip">>} = lists:keyfind(<<"accept-encoding">>, 1, Headers),
	ok.

add_accept_encoding_header_valid(Config) ->
	doc("Supported content codings must be added to the accept-encoding "
		"header if it already exists. (RFC9110 12.5.3)"),
	Data = <<"test">>,
	Body = zlib:gzip(Data),
	{200, Headers, Data} = do_post("/test/accept-identity",
		[{<<"content-encoding">>, <<"gzip">>}], Body, Config),
	{_, <<"identity, gzip">>} = lists:keyfind(<<"accept-encoding">>, 1, Headers),
	ok.

override_accept_encoding_header_invalid(Config) ->
	doc("When the stream handler cannot parse the accept-encoding header "
		"found in the response, it overrides it."),
	Data = <<"test">>,
	Body = zlib:gzip(Data),
	{200, Headers, Data} = do_post("/test/invalid-header",
		[{<<"content-encoding">>, <<"gzip">>}], Body, Config),
	{_, <<"gzip">>} = lists:keyfind(<<"accept-encoding">>, 1, Headers),
	ok.

override_accept_encoding_excluded(Config) ->
	doc("The stream handler must ensure that the content encodings "
		"it supports are not marked as unsupported in response headers. "
		"The stream handler enables gzip when explicitly excluded. "
		"(RFC9110 12.5.3)"),
	Data = <<"test">>,
	Body = zlib:gzip(Data),
	{200, Headers, Data} = do_post("/test/reject-explicit-header",
		[{<<"content-encoding">>, <<"gzip">>}], Body, Config),
	{_, <<"identity;q=1, gzip;q=1">>} = lists:keyfind(<<"accept-encoding">>, 1, Headers),
	ok.

%% *;q=0 will reject codings that are not listed. Supported codings
%% must always be enabled when the handler is used.
add_accept_encoding_excluded(Config) ->
	doc("The stream handler must ensure that the content encodings "
		"it supports are not marked as unsupported in response headers. "
		"The stream handler enables gzip when implicitly excluded (*;q=0). "
		"(RFC9110 12.5.3)"),
	Data = <<"test">>,
	Body = zlib:gzip(Data),
	{200, Headers, Data} = do_post("/test/reject-implicit-header",
		[{<<"content-encoding">>, <<"gzip">>}], Body, Config),
	{_, <<"gzip;q=1, identity;q=1, *;q=0">>} = lists:keyfind(<<"accept-encoding">>, 1, Headers),
	ok.

no_override_accept_coding_set_explicit(Config) ->
	doc("Confirm that accept-encoding is not overridden when the "
		"content encodings it supports are explicitly set. "
		"(RFC9110 12.5.3)"),
	Data = <<"test">>,
	Body = zlib:gzip(Data),
	{200, Headers, Data} = do_post("/test/accept-explicit-header",
		[{<<"content-encoding">>, <<"gzip">>}], Body, Config),
	{_, <<"identity, gzip;q=0.5">>} = lists:keyfind(<<"accept-encoding">>, 1, Headers),
	ok.

no_override_accept_coding_set_implicit(Config) ->
	doc("Confirm that accept-encoding is not overridden when the "
		"content encodings it supports are implicitly set. "
		"(RFC9110 12.5.3)"),
	Data = <<"test">>,
	Body = zlib:gzip(Data),
	{200, Headers, Data} = do_post("/test/accept-implicit-header",
		[{<<"content-encoding">>, <<"gzip">>}], Body, Config),
	{_, <<"identity, *;q=0.5">>} = lists:keyfind(<<"accept-encoding">>, 1, Headers),
	ok.