Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pad session values to 1025 bytes by default #1791

Merged
merged 1 commit into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/Mojolicious/Sessions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ has [qw(cookie_domain secure)];
has cookie_name => 'mojolicious';
has cookie_path => '/';
has default_expiration => 3600;
has deserialize => sub { \&Mojo::JSON::j };
has deserialize => sub { \&_deserialize };
has samesite => 'Lax';
has serialize => sub { \&Mojo::JSON::encode_json };
has serialize => sub { \&_serialize };

sub load {
my ($self, $c) = @_;
Expand Down Expand Up @@ -61,6 +61,14 @@ sub store {
$c->signed_cookie($self->cookie_name, $value, $options);
}

sub _deserialize { Mojo::JSON::decode_json($_[0] =~ s/\}\KZ*$//r) }

sub _serialize {
no warnings 'numeric';
my $out = Mojo::JSON::encode_json($_[0]);
return $out . 'Z' x (1025 - length $out);
}

1;

=encoding utf8
Expand Down
30 changes: 30 additions & 0 deletions t/mojolicious/lite_app.t
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ get '/session_cookie/2' => sub {
$c->render(text => "Session is $value!");
};

get '/session_length' => sub {
my $c = shift;
$c->session->{q} = $c->param('q');
$c->rendered(204);
};

get '/foo' => sub {
my $c = shift;
$c->render(text => 'Yea baby!');
Expand Down Expand Up @@ -799,6 +805,30 @@ ok !$t->tx, 'session reset';
$t->get_ok('/session_cookie/2')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')
->content_is('Session is missing!');


subtest 'Session length' => sub {
my $extract = sub {
my $value = $_[0]->tx->res->cookie('mojolicious')->value;
$value =~ s/--([^\-]+)$//;
$value =~ y/-/=/;
return Mojo::Util::b64_decode($value);
};

subtest 'Short session' => sub {
$t->reset_session;
my $value = $t->get_ok('/session_length?q=a')->status_is(204)->$extract;
cmp_ok length($value), '>', 1024, 'session is long enough';
ok $value =~ /Z+$/, 'session is padded';
};

subtest 'Long session' => sub {
$t->reset_session;
my $value = $t->get_ok('/session_length?q=' . 'a' x 1025)->status_is(204)->$extract;
cmp_ok length($value), '>', 1024, 'session is long enough';
ok $value !~ /Z+$/, 'session is not padded';
};
};

# Text
$t->get_ok('/foo')->status_is(200)->header_is(Server => 'Mojolicious (Perl)')->content_is('Yea baby!');

Expand Down