Skip to content

Commit

Permalink
fixed limit bug in Mojo::Message
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 13, 2013
1 parent a490003 commit 382fe58
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -52,6 +52,7 @@
- Fixed layout bugs in Mojolicious::Renderer.
- Fixed support for HEAD request method in Mojo::Server::CGI and
Mojo::Server::PSGI.
- Fixed small limit bug in Mojo::Message.

3.97 2013-04-25
- Added data attribute to Mojo::URL.
Expand Down
17 changes: 10 additions & 7 deletions lib/Mojo/Message.pm
Expand Up @@ -145,10 +145,7 @@ sub headers { shift->content->headers }

sub is_finished { (shift->{state} // '') eq 'finished' }

sub is_limit_exceeded {
return undef unless my $code = (shift->error)[1];
return !!($code eq 413 || $code eq 431);
}
sub is_limit_exceeded { !!shift->{limit} }

sub json {
my ($self, $pointer) = @_;
Expand All @@ -163,7 +160,7 @@ sub parse {
my ($self, $chunk) = @_;

# Check message size
return $self->error('Maximum message size exceeded', 413)
return $self->_limit('Maximum message size exceeded', 413)
if ($self->{raw_size} += length($chunk //= '')) > $self->max_message_size;

$self->{buffer} .= $chunk;
Expand All @@ -174,7 +171,7 @@ sub parse {
# Check line size
my $len = index $self->{buffer}, "\x0a";
$len = length $self->{buffer} if $len < 0;
return $self->error('Maximum line size exceeded', 431)
return $self->_limit('Maximum line size exceeded', 431)
if $len > $self->max_line_size;

$self->{state} = 'content' if $self->extract_start_line(\$self->{buffer});
Expand All @@ -186,7 +183,7 @@ sub parse {
if $state eq 'content' || $state eq 'finished';

# Check line size
return $self->error('Maximum line size exceeded', 431)
return $self->_limit('Maximum line size exceeded', 431)
if $self->headers->is_limit_exceeded;

# Check buffer size
Expand Down Expand Up @@ -260,6 +257,12 @@ sub _cache {
return wantarray ? @$objects : $objects->[0];
}

sub _limit {
my $self = shift;
$self->{limit} = 1;
$self->error(@_);
}

sub _parse_formdata {
my $self = shift;

Expand Down
8 changes: 8 additions & 0 deletions t/mojo/request.t
Expand Up @@ -329,6 +329,8 @@ is $req->headers->content_length, undef, 'no "Content-Length" value';
# Parse HTTP 1.0 start line (with line size limit)
{
$req = Mojo::Message::Request->new;
my $limit;
$req->on(finish => sub { $limit = shift->is_limit_exceeded });
ok !$req->is_limit_exceeded, 'limit is not exceeded';
local $ENV{MOJO_MAX_LINE_SIZE} = 5;
is $req->headers->max_line_size, 5, 'right size';
Expand All @@ -337,6 +339,9 @@ is $req->headers->content_length, undef, 'no "Content-Length" value';
is(($req->error)[0], 'Maximum line size exceeded', 'right error');
is(($req->error)[1], 431, 'right status');
ok $req->is_limit_exceeded, 'limit is exceeded';
ok $limit, 'limit is exceeded';
ok $req->error('Nothing important.')->is_limit_exceeded,
'limit is still exceeded';
}

# Parse HTTP 1.0 start line and headers (with line size limit)
Expand All @@ -355,13 +360,16 @@ is $req->headers->content_length, undef, 'no "Content-Length" value';
# Parse HTTP 1.0 start line (with message size limit)
{
$req = Mojo::Message::Request->new;
my $limit;
$req->on(finish => sub { $limit = shift->is_limit_exceeded });
local $ENV{MOJO_MAX_MESSAGE_SIZE} = 5;
is $req->max_message_size, 5, 'right size';
$req->parse('GET /foo/bar/baz.html HTTP/1');
ok $req->is_finished, 'request is finished';
is(($req->error)[0], 'Maximum message size exceeded', 'right error');
is(($req->error)[1], 413, 'right status');
ok $req->is_limit_exceeded, 'limit is exceeded';
ok $limit, 'limit is exceeded';
}

# Parse HTTP 1.0 start line and headers (with message size limit)
Expand Down

0 comments on commit 382fe58

Please sign in to comment.