Debian Codegraph @

t/message-decode-brotlibomb.t

raw
1 # https://rt.cpan.org/Public/Bug/Display.html?id=52572
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use HTTP::Headers qw();
9 use HTTP::Response qw();
10
11 use Test::Needs 'IO::Compress::Brotli', 'IO::Uncompress::Brotli';
12
13 plan tests => 9;
14
15 # Create a nasty brotli stream:
16 my $size = 16 * 1024 * 1024;
17 my $stream = "\0" x $size;
18
19 # Compress that stream one time (since it won't compress it twice?!):
20 my $compressed = $stream;
21 my $bro = IO::Compress::Brotli->create;
22
23 for( 1 ) {
24 my $last = $compressed;
25 $compressed = $bro->compress( $compressed );
26 $compressed .= $bro->finish();
27 note sprintf "Encoded size %d bytes after round %d", length $compressed, $_;
28 };
29
30 my $body = $compressed;
31
32 my $headers = HTTP::Headers->new(
33 Content_Type => "application/xml",
34 Content_Encoding => 'br', # only one round needed for Brotli
35 );
36 my $response = HTTP::Response->new(200, "OK", $headers, $body);
37
38 my $len = length $response->decoded_content;
39 is($len, 16 * 1024 * 1024, "Self-test: The decoded content length is 16M as expected" );
40
41 # Manual decompression check
42 my $output = $compressed;
43 for( 1 ) {
44 my $unbro = IO::Uncompress::Brotli->create();
45 $output = $unbro->decompress($compressed);
46 };
47
48 $headers = HTTP::Headers->new(
49 Content_Type => "application/xml",
50 Content_Encoding => 'br' # say my name, but only once
51 );
52
53 $HTTP::Message::MAXIMUM_BODY_SIZE = 1024 * 1024;
54
55 $response = HTTP::Response->new(200, "OK", $headers, $body);
56 is $response->max_body_size, 1024*1024, "The default maximum body size holds";
57
58 $response->max_body_size( 512*1024 );
59 is $response->max_body_size, 512*1024, "We can change the maximum body size";
60
61 my $content;
62 my $lives = eval {
63 $content = $response->decoded_content( raise_error => 1 );
64 1;
65 };
66 my $err = $@;
67 is $lives, undef, "We die when trying to decode something larger than our global limit of 512k"
68 or diag "... using IO::Uncompress::Brotli version $IO::Uncompress::Brotli::VERSION";
69
70 $response->max_body_size(undef);
71 is $response->max_body_size, undef, "We can remove the maximum size restriction";
72 $lives = eval {
73 $content = $response->decoded_content( raise_error => 0 );
74 1;
75 };
76 is $lives, 1, "We don't die when trying to decode something larger than our global limit of 1M";
77 is length $content, 16 * 1024*1024, "We get the full content";
78 is $content, $stream, "We really get the full content";
79
80 # The best usage of ->decoded_content:
81 $lives = eval {
82 $content = $response->decoded_content(
83 raise_error => 1,
84 max_body_size => 512 * 1024 );
85 1;
86 };
87 $err = $@;
88 is $lives, undef, "We die when trying to decode something larger than our limit of 512k using a parameter"
89 or diag "... using IO::Uncompress::Brotli version $IO::Uncompress::Brotli::VERSION";
90
91 =head1 SEE ALSO
92
93 L<https://security.stackexchange.com/questions/51071/zlib-deflate-decompression-bomb>
94
95 L<http://www.aerasec.de/security/advisories/decompression-bomb-vulnerability.html>
96
97 =cut