Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix handling of invalid max age in Mojo::UserAgent::CookieJar
  • Loading branch information
kraih committed Dec 25, 2015
1 parent d66c32c commit a46fc7c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,6 +1,7 @@

6.39 2015-12-25
6.39 2015-12-26
- Fixed small html_unescape bug in Mojo::Util.
- Fixed handling of invalid max age in Mojo::UserAgent::CookieJar.

6.38 2015-12-19
- Updated prettify.js to version 8-Dec-2015.
Expand Down
4 changes: 3 additions & 1 deletion lib/Mojo/UserAgent/CookieJar.pm
Expand Up @@ -3,6 +3,7 @@ use Mojo::Base -base;

use Mojo::Cookie::Request;
use Mojo::Path;
use Scalar::Util 'looks_like_number';

has 'ignore';
has max_cookie_size => 4096;
Expand All @@ -14,7 +15,8 @@ sub add {
for my $cookie (@cookies) {

# Convert max age to expires
if (my $age = $cookie->max_age) { $cookie->expires($age + time) }
my $age = $cookie->max_age;
$cookie->expires($age + time) if looks_like_number $age;

# Check cookie size
next if length($cookie->value // '') > $size;
Expand Down
20 changes: 20 additions & 0 deletions t/mojo/cookiejar.t
Expand Up @@ -437,6 +437,26 @@ is $tx->req->cookie('foo')->value, 'valid', 'right value';
is $tx->req->cookie('bar')->name, 'bar', 'right name';
is $tx->req->cookie('bar')->value, 'too', 'right value';

# Gather cookies with invalid expiration
$jar = Mojo::UserAgent::CookieJar->new;
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://example.com');
$tx->res->cookies(
Mojo::Cookie::Response->new(
name => 'foo',
value => 'bar',
max_age => 'invalid'
),
Mojo::Cookie::Response->new(name => 'bar', value => 'baz', max_age => 86400)
);
$jar->collect($tx);
is $jar->all->[0]->name, 'foo', 'right name';
is $jar->all->[0]->value, 'bar', 'right value';
ok !$jar->all->[0]->expires, 'does not expire';
is $jar->all->[1]->name, 'bar', 'right name';
is $jar->all->[1]->value, 'baz', 'right value';
ok $jar->all->[1]->expires, 'expires';

# Gather cookies with invalid domain
$jar = Mojo::UserAgent::CookieJar->new;
$tx = Mojo::Transaction::HTTP->new;
Expand Down

0 comments on commit a46fc7c

Please sign in to comment.