| 1 | package Perlbal::Plugin::MaxContentLength; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | Perlbal::Plugin::MaxContentLength - Reject large requests | ||||||||||
| 6 | |||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||
| 8 | |||||||||||
| 9 | LOAD MaxContentLength | ||||||||||
| 10 | CREATE SERVICE cgilike | ||||||||||
| 11 | # define a service... | ||||||||||
| 12 | SET max_content_length = 100000 | ||||||||||
| 13 | SET plugins = MaxContentLength | ||||||||||
| 14 | ENABLE cgilike | ||||||||||
| 15 | |||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||
| 17 | |||||||||||
| 18 | This module rejects requests that are larger than a configured limit. If a | ||||||||||
| 19 | request bears a Content-Length header whose value exceeds the | ||||||||||
| 20 | max_content_length value, the request will be rejected with a 413 "Request | ||||||||||
| 21 | Entity Too Large" error. | ||||||||||
| 22 | |||||||||||
| 23 | =head1 AUTHOR | ||||||||||
| 24 | |||||||||||
| 25 | Adam Thomason, E<lt>athomason@sixapart.comE<gt> | ||||||||||
| 26 | |||||||||||
| 27 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 28 | |||||||||||
| 29 | Copyright 2008 Six Apart Ltd. | ||||||||||
| 30 | |||||||||||
| 31 | This module is part of the Perlbal distribution, and as such can be distributed | ||||||||||
| 32 | under the same licence terms as the rest of Perlbal. | ||||||||||
| 33 | |||||||||||
| 34 | =cut | ||||||||||
| 35 | |||||||||||
| 36 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | |||||||||||
| 39 | 1 | ▶ | use Perlbal; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | |||||||||||
| 41 | 0 | ▶ | sub load { | ||||||||
|
Subroutine
load: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 42 | 0 | Perlbal::Service::add_tunable( | |||||||||
| 43 | max_content_length => { | ||||||||||
| 44 | check_role => '*', | ||||||||||
| 45 | check_type => 'int', | ||||||||||
| 46 | des => "maximum Content-Length allowed, in bytes. 0 for no limit", | ||||||||||
| 47 | default => 0, | ||||||||||
| 48 | }, | ||||||||||
| 49 | ); | ||||||||||
| 50 | 0 | return 1; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 1 | ▶ | use constant HANDLE_REQUEST => 0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 54 | 1 | ▶ | use constant IGNORE_REQUEST => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | sub register { | ||||||||
|
Subroutine
register: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 57 | 0 | my ($class, $svc) = @_; | |||||||||
| 58 | |||||||||||
| 59 | 0 | my $cfg = $svc->{extra_config}; | |||||||||
| 60 | 0 | ▶ | return unless $cfg; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | $svc->register_hook('MaxContentLength', 'start_http_request' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||
| 63 | 0 | my $client = shift; | |||||||||
| 64 | 0 | ▶ | return IGNORE_REQUEST unless $client; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | |||||||||||
| 66 | # allow request if max is disabled | ||||||||||
| 67 | 0 | ▶ | return HANDLE_REQUEST unless $cfg->{max_content_length}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | |||||||||||
| 69 | 0 | my $headers = $client->{req_headers}; | |||||||||
| 70 | 0 | ▶ | return HANDLE_REQUEST unless $headers; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | |||||||||||
| 72 | # allow requests which don't have a Content-Length header | ||||||||||
| 73 | 0 | my $length = $headers->header('content-length'); | |||||||||
| 74 | 0 | ▶ | return HANDLE_REQUEST unless $length; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | |||||||||||
| 76 | # allow requests under the cap | ||||||||||
| 77 | 0 | ▶ | return HANDLE_REQUEST if $length <= $cfg->{max_content_length}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | |||||||||||
| 79 | 0 | $client->send_response(413, "Content too long.\n"); | |||||||||
| 80 | 0 | return IGNORE_REQUEST; | |||||||||
| 81 | 0 | }); | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 0 | ▶ | sub unregister { | ||||||||
|
Subroutine
unregister: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 85 | 0 | my ($class, $svc) = @_; | |||||||||
| 86 | |||||||||||
| 87 | 0 | $svc->unregister_hooks('MaxContentLength'); | |||||||||
| 88 | 0 | return 1; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 1 | 1; | |||||||||