Skip to content

Commit 3644d5b

Browse files
committed
Add partitioned cookies
1 parent ecb44cf commit 3644d5b

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

lib/Mojo/Cookie/Response.pm

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use Mojo::Base 'Mojo::Cookie';
44
use Mojo::Date;
55
use Mojo::Util qw(quote split_cookie_header);
66

7-
has [qw(domain expires host_only httponly max_age path samesite secure)];
7+
has [qw(domain expires host_only httponly max_age path samesite secure partitioned)];
88

9-
my %ATTRS = map { $_ => 1 } qw(domain expires httponly max-age path samesite secure);
9+
my %ATTRS = map { $_ => 1 } qw(domain expires httponly max-age path samesite secure partitioned);
1010

1111
sub parse {
1212
my ($self, $str) = @_;
@@ -21,7 +21,7 @@ sub parse {
2121
next unless $ATTRS{my $attr = lc $name};
2222
$value =~ s/^\.// if $attr eq 'domain' && defined $value;
2323
$value = Mojo::Date->new($value // '')->epoch if $attr eq 'expires';
24-
$value = 1 if $attr eq 'secure' || $attr eq 'httponly';
24+
$value = 1 if $attr eq 'secure' || $attr eq 'httponly' || $attr eq 'partitioned';
2525
$cookies[-1]{$attr eq 'max-age' ? 'max_age' : $attr} = $value;
2626
}
2727
}
@@ -53,6 +53,9 @@ sub to_string {
5353
# "HttpOnly"
5454
$cookie .= "; HttpOnly" if $self->httponly;
5555

56+
# "Partitioned"
57+
$cookie .= "; Partitioned" if $self->partitioned;
58+
5659
# "Same-Site"
5760
if (my $samesite = $self->samesite) { $cookie .= "; SameSite=$samesite" }
5861

lib/Mojolicious/Sessions.pm

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ has default_expiration => 3600;
1111
has deserialize => sub { \&_deserialize };
1212
has samesite => 'Lax';
1313
has serialize => sub { \&_serialize };
14+
has partitioned => 0;
1415

1516
sub load {
1617
my ($self, $c) = @_;
@@ -51,12 +52,13 @@ sub store {
5152
my $value = b64_encode $self->serialize->($session), '';
5253
$value =~ y/=/-/;
5354
my $options = {
54-
domain => $self->cookie_domain,
55-
expires => $session->{expires},
56-
httponly => 1,
57-
path => $self->cookie_path,
58-
samesite => $self->samesite,
59-
secure => $self->secure
55+
domain => $self->cookie_domain,
56+
expires => $session->{expires},
57+
httponly => 1,
58+
path => $self->cookie_path,
59+
samesite => $self->samesite,
60+
secure => $self->secure,
61+
partitioned => $self->partitioned
6062
};
6163
$c->signed_cookie($self->cookie_name, $value, $options);
6264
}

t/mojo/cookie.t

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,9 @@ subtest 'Full response cookie as string' => sub {
173173
$cookie->secure(1);
174174
$cookie->httponly(1);
175175
$cookie->samesite('Lax');
176+
$cookie->partitioned(1);
176177
is $cookie->to_string, '0="ba r"; expires=Thu, 07 Aug 2008 07:07:59 GMT; domain=example.com;'
177-
. ' path=/test; secure; HttpOnly; SameSite=Lax; Max-Age=60', 'right format';
178+
. ' path=/test; secure; HttpOnly; Partitioned; SameSite=Lax; Max-Age=60', 'right format';
178179
};
179180

180181
subtest 'Empty response cookie' => sub {
@@ -216,6 +217,18 @@ subtest 'Parse response cookie (RFC 6265)' => sub {
216217
is $cookies->[1], undef, 'no more cookies';
217218
};
218219

220+
subtest 'Partitioned cookie (RFC 6265 CHIPS)' => sub {
221+
my $cookies
222+
= Mojo::Cookie::Response->parse(
223+
'foo="bar"; Domain=example.com; Path=/test; Max-Age=60; Partitioned; Expires=Thu, 07 Aug 2008 07:07:59 GMT; Secure;'
224+
);
225+
is $cookies->[0]->partitioned, 1, 'partitioned set?';
226+
227+
$cookies = Mojo::Cookie::Response->parse(
228+
'foo="bar"; Domain=example.com; Path=/test; Max-Age=60; Expires=Thu, 07 Aug 2008 07:07:59 GMT; Secure;');
229+
is $cookies->[0]->partitioned, undef, 'partitioned not set?';
230+
};
231+
219232
subtest 'Parse response cookie with invalid flag (RFC 6265)' => sub {
220233
my $cookies = Mojo::Cookie::Response->parse(
221234
'foo="ba r"; Domain=.example.com; Path=/test; Max-Age=60;' . ' Expires=Thu, 07 Aug 2008 07:07:59 GMT; InSecure;');

0 commit comments

Comments
 (0)