| 1 | package Langertha::HTTP::BoundedDecode; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Bounded Content-Encoding inflate shared by the response-body decoders | ||||||||||||||||||||||||||||
| 3 | 28 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||
| 4 | 28 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 28 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | # The Content-Encodings this decoder can undo, mapped to the inflate kind. | ||||||||||||||||||||||||||||
| 9 | # identity is a no-op; gzip/deflate/bzip2 (plus the x- aliases HTTP::Message | ||||||||||||||||||||||||||||
| 10 | # accepts) inflate; anything else is refused by the caller's | ||||||||||||||||||||||||||||
| 11 | # unbounded_encoding handler, because it cannot be bounded here. | ||||||||||||||||||||||||||||
| 12 | 28 | my %INFLATE = ( | |||||||||||||||||||||||||||
| 13 | identity => undef, | ||||||||||||||||||||||||||||
| 14 | gzip => 'gzip', 'x-gzip' => 'gzip', | ||||||||||||||||||||||||||||
| 15 | deflate => 'zlib', 'x-deflate' => 'zlib', | ||||||||||||||||||||||||||||
| 16 | bzip2 => 'bzip2', 'x-bzip2' => 'bzip2', | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Undo $content_encoding on $body within $max decoded bytes. $content_encoding | ||||||||||||||||||||||||||||
| 20 | # is the raw Content-Encoding header value (a comma list is applied in order, | ||||||||||||||||||||||||||||
| 21 | # so undone in reverse). $handlers holds the three croak points; each is | ||||||||||||||||||||||||||||
| 22 | # expected to croak (its return value is ignored): | ||||||||||||||||||||||||||||
| 23 | # too_big => sub { my ($max) = @_; ... } # decoded past $max | ||||||||||||||||||||||||||||
| 24 | # undecodable => sub { my ($encoding) = @_; ... } # corrupt / not that encoding | ||||||||||||||||||||||||||||
| 25 | # unbounded_encoding => sub { my ($encoding) = @_; ... } # an encoding we cannot bound | ||||||||||||||||||||||||||||
| 26 | 217 | ▶ | sub decode_within { | ||||||||||||||||||||||||||
|
Subroutine
decode_within: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 217 | my ( $body, $content_encoding, $max, $handlers ) = @_; | |||||||||||||||||||||||||||
| 28 | 217 | ▶ | my @encodings = grep { length } map { s/\A\s+|\s+\z//gr } | ||||||||||||||||||||||||||
|
Condition100%2/2
$content_encoding // ""
MC/DC100%1/1
$content_encoding // ""
$content_encoding
Truth table100%2/2
A
A= $content_encoding
| |||||||||||||||||||||||||||||
| 29 | split /,/, lc( $content_encoding // '' ); | ||||||||||||||||||||||||||||
| 30 | 217 | for my $encoding ( reverse @encodings ) { # listed in the order applied | |||||||||||||||||||||||||||
| 31 | 44 | ▶ | $handlers->{unbounded_encoding}->($encoding) unless exists $INFLATE{$encoding}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 40 | ▶ | my $kind = $INFLATE{$encoding} or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 40 | ▶ | $body = $kind eq 'bzip2' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | ? _bunzip_capped( $body, $max, $encoding, $handlers ) | ||||||||||||||||||||||||||||
| 35 | : _inflate_capped( $body, $max, $encoding, $kind, $handlers ); | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 192 | return $body; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # zlib inflate in output blocks of at most 64 KiB. deflate is the zlib format, | ||||||||||||||||||||||||||||
| 42 | # with raw deflate as the fallback that HTTP::Message allows too. | ||||||||||||||||||||||||||||
| 43 | 36 | ▶ | sub _inflate_capped { | ||||||||||||||||||||||||||
|
Subroutine
_inflate_capped: called CC=11 SCAR=27.2
| |||||||||||||||||||||||||||||
| 44 | 36 | my ( $in, $max, $encoding, $kind, $handlers ) = @_; | |||||||||||||||||||||||||||
| 45 | 36 | require Compress::Raw::Zlib; | |||||||||||||||||||||||||||
| 46 | 36 | ▶ | my @bits = $kind eq 'gzip' ? ( Compress::Raw::Zlib::WANT_GZIP() ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | : ( Compress::Raw::Zlib::MAX_WBITS(), -Compress::Raw::Zlib::MAX_WBITS() ); | ||||||||||||||||||||||||||||
| 48 | 36 | for my $bits (@bits) { | |||||||||||||||||||||||||||
| 49 | 39 | my $input = $in; | |||||||||||||||||||||||||||
| 50 | 39 | my ( $inflater, $status ) = Compress::Raw::Zlib::Inflate->new( | |||||||||||||||||||||||||||
| 51 | -WindowBits => $bits, -Bufsize => 65_536, -LimitOutput => 1, | ||||||||||||||||||||||||||||
| 52 | -ConsumeInput => 1, -AppendOutput => 1 ); | ||||||||||||||||||||||||||||
| 53 | 39 | ▶ | $handlers->{undecodable}->($encoding) unless $status == Compress::Raw::Zlib::Z_OK(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 39 | my $out = ''; | |||||||||||||||||||||||||||
| 55 | 39 | while (1) { | |||||||||||||||||||||||||||
| 56 | 39 | my @before = ( length $input, length $out ); | |||||||||||||||||||||||||||
| 57 | 39 | $status = $inflater->inflate( $input, $out ); | |||||||||||||||||||||||||||
| 58 | 39 | ▶ | $handlers->{too_big}->($max) if length $out > $max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 22 | ▶ | return $out if $status == Compress::Raw::Zlib::Z_STREAM_END(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 3 | ▶ | last unless $status == Compress::Raw::Zlib::Z_OK() | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$status == Compress::Raw::Zlib::Z_OK() or $status == Compress::Raw::Zlib::Z_BUF_ERROR()
MC/DC0%0/2
$status == Compress::Raw::Zlib::Z_OK() or $status == Compress::Raw::Zlib::Z_BUF_ERROR()
$status == Compress::Raw::Zlib::Z_OK()
$status == Compress::Raw::Zlib::Z_BUF_ERROR()
Truth table33%1/3
A or B
A= $status == Compress::Raw::Zlib::Z_OK()B= $status == Compress::Raw::Zlib::Z_BUF_ERROR()
| |||||||||||||||||||||||||||||
| 61 | || $status == Compress::Raw::Zlib::Z_BUF_ERROR(); | ||||||||||||||||||||||||||||
| 62 | # No progress: the stream ends before its end marker. | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $handlers->{undecodable}->($encoding) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $input == $before[0] and length $out == $before[1]
MC/DC0%0/2
length $input == $before[0] and length $out == $before[1]
length $input == $before[0]
length $out == $before[1]
Truth table0%0/3
A and B
A= length $input == $before[0]B= length $out == $before[1]
| |||||||||||||||||||||||||||||
| 64 | if length $input == $before[0] && length $out == $before[1]; | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 3 | ▶ | $handlers->{undecodable}->($encoding) unless $status == Compress::Raw::Zlib::Z_DATA_ERROR(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 0 | return $handlers->{undecodable}->($encoding); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 4 | ▶ | sub _bunzip_capped { | ||||||||||||||||||||||||||
|
Subroutine
_bunzip_capped: called CC=7 SCAR=27.5
| |||||||||||||||||||||||||||||
| 72 | 4 | my ( $input, $max, $encoding, $handlers ) = @_; | |||||||||||||||||||||||||||
| 73 | 4 | require Compress::Raw::Bzip2; | |||||||||||||||||||||||||||
| 74 | # appendOutput, consumeInput, small, verbosity, limitOutput | ||||||||||||||||||||||||||||
| 75 | 4 | my ( $bunzip, $status ) = Compress::Raw::Bunzip2->new( 1, 1, 0, 0, 1 ); | |||||||||||||||||||||||||||
| 76 | 4 | ▶ | $handlers->{undecodable}->($encoding) unless $status == Compress::Raw::Bzip2::BZ_OK(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 4 | my $out = ''; | |||||||||||||||||||||||||||
| 78 | 4 | while (1) { | |||||||||||||||||||||||||||
| 79 | 4 | my @before = ( length $input, length $out ); | |||||||||||||||||||||||||||
| 80 | 4 | $status = $bunzip->bzinflate( $input, $out ); | |||||||||||||||||||||||||||
| 81 | 4 | ▶ | $handlers->{too_big}->($max) if length $out > $max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | return $out if $status == Compress::Raw::Bzip2::BZ_STREAM_END(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $handlers->{undecodable}->($encoding) unless $status == Compress::Raw::Bzip2::BZ_OK(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | $handlers->{undecodable}->($encoding) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $input == $before[0] and length $out == $before[1]
MC/DC0%0/2
length $input == $before[0] and length $out == $before[1]
length $input == $before[0]
length $out == $before[1]
Truth table0%0/3
A and B
A= length $input == $before[0]B= length $out == $before[1]
| |||||||||||||||||||||||||||||
| 85 | if length $input == $before[0] && length $out == $before[1]; | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 28 | 1; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | __END__ | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =pod | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head1 NAME | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Langertha::HTTP::BoundedDecode - Bounded Content-Encoding inflate shared by the response-body decoders | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head1 VERSION | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | version 0.503 | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | use Langertha::HTTP::BoundedDecode; | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | my $bytes = Langertha::HTTP::BoundedDecode::decode_within( | ||||||||||||||||||||||||||||
| 111 | $response->content, # the raw body | ||||||||||||||||||||||||||||
| 112 | scalar $response->header('Content-Encoding'), | ||||||||||||||||||||||||||||
| 113 | $max_decoded_bytes, | ||||||||||||||||||||||||||||
| 114 | { | ||||||||||||||||||||||||||||
| 115 | too_big => sub { croak "body exceeds $_[0]" }, | ||||||||||||||||||||||||||||
| 116 | undecodable => sub { croak "cannot decode '$_[0]'" }, | ||||||||||||||||||||||||||||
| 117 | unbounded_encoding => sub { croak "cannot bound '$_[0]'" }, | ||||||||||||||||||||||||||||
| 118 | }, | ||||||||||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | The bounded Content-Encoding inflater behind Langertha's response decoders. | ||||||||||||||||||||||||||||
| 124 | L<HTTP::Message/decoded_content> undoes a C<Content-Encoding> (C<gzip>, | ||||||||||||||||||||||||||||
| 125 | C<deflate>, C<bzip2>) with B<no size bound>, so a small compressed body can | ||||||||||||||||||||||||||||
| 126 | inflate to gigabytes in memory — a decompression bomb from a hostile or broken | ||||||||||||||||||||||||||||
| 127 | endpoint. This module inflates in bounded blocks and refuses a body once its | ||||||||||||||||||||||||||||
| 128 | decoded size passes C<$max>, so the memory is capped. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | It carries no error text of its own: each caller passes the three croak points, | ||||||||||||||||||||||||||||
| 131 | so L<Langertha::Content::Image> (image fetches, karr k342) and | ||||||||||||||||||||||||||||
| 132 | L<Langertha::Role::HTTP> (provider/metrics response bodies, karr k346) keep | ||||||||||||||||||||||||||||
| 133 | their own messages while sharing one inflate path. It returns the decoded | ||||||||||||||||||||||||||||
| 134 | B<bytes>; charset decoding is the caller's concern. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head2 decode_within | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | my $bytes = Langertha::HTTP::BoundedDecode::decode_within( | ||||||||||||||||||||||||||||
| 139 | $raw_body, $content_encoding_header, $max, \%handlers ); | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Undoes the C<Content-Encoding> of C<$raw_body> within C<$max> decoded bytes and | ||||||||||||||||||||||||||||
| 142 | returns the decoded bytes. A comma-listed encoding is undone in reverse of the | ||||||||||||||||||||||||||||
| 143 | order it was applied. Calls C<< $handlers->{too_big}->($max) >> as soon as the | ||||||||||||||||||||||||||||
| 144 | decoded size passes C<$max>, C<< $handlers->{undecodable}->($encoding) >> when a | ||||||||||||||||||||||||||||
| 145 | supported encoding is corrupt, and | ||||||||||||||||||||||||||||
| 146 | C<< $handlers->{unbounded_encoding}->($encoding) >> for an encoding it cannot | ||||||||||||||||||||||||||||
| 147 | bound (anything but C<gzip> / C<deflate> / C<bzip2> and their C<x-> aliases and | ||||||||||||||||||||||||||||
| 148 | C<identity>). Each handler is expected to croak. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =over | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =item * L<Langertha::Content::Image> - Bounds inline image fetches (karr k342) | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =item * L<Langertha::Role::HTTP> - Bounds provider/metrics response bodies (karr k346) | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =back | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head2 Issues | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 165 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head2 IRC | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 184 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =cut | ||||||||||||||||||||||||||||