Debian Codegraph @

t/message-decode-bzipbomb.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 plan tests => 10;
8
9 use HTTP::Headers qw( );
10 use HTTP::Response qw( );
11
12 # Create a nasty bzip2 stream:
13 my $size = 16 * 1024 * 1024;
14 my $stream = "\0" x $size;
15
16 # Compress that stream three times:
17 my $compressed = $stream;
18 for( 1..3 ) {
19 require IO::Compress::Bzip2;
20 my $last = $compressed;
21 IO::Compress::Bzip2::bzip2(\$last, \$compressed)
22 or die "Can't bzip2 content: $IO::Compress::Bzip2::Bzip2Error";
23 #diag sprintf "Encoded size %d bytes after round %d", length $compressed, $_;
24 };
25
26 my $body = $compressed;
27
28 my $headers = HTTP::Headers->new(
29 Content_Type => "application/xml",
30 Content_Encoding => 'bzip2,bzip2,bzip2', # say my name three times
31 );
32 my $response = HTTP::Response->new(200, "OK", $headers, $body);
33
34 my $len = length $response->decoded_content( raise_error => 1 );
35 is($len, 16 * 1024 * 1024, "Self-test: The decoded content length is 16M as expected" );
36
37 # Manual decompression check
38 my $output = $compressed;
39 for( 1..3 ) {
40 my $last = $output;
41 require Compress::Raw::Bzip2;
42 my ($i, $status) = Compress::Raw::Bunzip2->new(
43 1, # appendInput
44 0, # consumeInput
45 0, # small
46 1,
47 );
48 $output = "\0" x (1024*1024);
49 # Will modify $last, but we made a copy above
50 my $res = $i->bzinflate( \$last, \$output );
51 };
52 is length $output, 1024*1024, "We manually recreate the limited original stream";
53
54 $headers = HTTP::Headers->new(
55 Content_Type => "application/xml",
56 Content_Encoding => 'bzip2,bzip2,bzip2', # say my name three times
57 );
58
59 $HTTP::Message::MAXIMUM_BODY_SIZE = 1024 * 1024;
60
61 $response = HTTP::Response->new(200, "OK", $headers, $body);
62 is $response->max_body_size, 1024*1024, "The default maximum body size holds";
63
64 $response->max_body_size( 512*1024 );
65 is $response->max_body_size, 512*1024, "We can change the maximum body size";
66
67 my $content;
68 my $lives = eval {
69 $content = $response->decoded_content( raise_error => 1 );
70 1;
71 };
72 my $err = $@;
73 is $lives, undef, "We die when trying to decode something larger than our limit of 512k";
74
75 $response->max_body_size(undef);
76 is $response->max_body_size, undef, "We can remove the maximum size restriction";
77 $lives = eval {
78 $content = $response->decoded_content( raise_error => 0 );
79 1;
80 };
81 is $lives, 1, "We don't die when trying to decode something larger than our global limit of 1M";
82 is length $content, 16 * 1024*1024, "We get the full content";
83 is $content, $stream, "We really get the full content";
84
85 # The best usage of ->decoded_content:
86 $lives = eval {
87 $content = $response->decoded_content(
88 raise_error => 1,
89 max_body_size => 512 * 1024 );
90 1;
91 };
92 $err = $@;
93 is $lives, undef, "We die when trying to decode something larger than our limit of 512k";
94
95 =head1 SEE ALSO
96
97 L<https://security.stackexchange.com/questions/51071/zlib-deflate-decompression-bomb>
98
99 L<http://www.aerasec.de/security/advisories/decompression-bomb-vulnerability.html>
100
101 =cut