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