Skip to content

Add support for partitioned cookies #51

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

Merged
merged 1 commit into from
Sep 23, 2024
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
4 changes: 2 additions & 2 deletions lib/Plack/Middleware/Session/Cookie.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use strict;
use parent qw(Plack::Middleware::Session);

use Plack::Util::Accessor qw(secret session_key domain expires path secure httponly
samesite serializer deserializer);
partitioned samesite serializer deserializer);

use Digest::HMAC_SHA1;
use MIME::Base64 ();
Expand All @@ -28,7 +28,7 @@ sub prepare_app {
unless $self->deserializer;

$self->state( Plack::Session::State::Cookie->new );
for my $attr (qw(session_key path domain expires secure httponly samesite)) {
for my $attr (qw(session_key path domain expires secure partitioned httponly samesite)) {
$self->state->$attr($self->$attr);
}
}
Expand Down
23 changes: 16 additions & 7 deletions lib/Plack/Session/State/Cookie.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use Plack::Util::Accessor qw[
secure
httponly
samesite
partitioned
];

sub get_session_id {
Expand All @@ -28,17 +29,25 @@ sub merge_options {

delete $options{id};

$options{path} = $self->path || '/' if !exists $options{path};
$options{domain} = $self->domain if !exists $options{domain} && defined $self->domain;
$options{secure} = $self->secure if !exists $options{secure} && defined $self->secure;
$options{httponly} = $self->httponly if !exists $options{httponly} && defined $self->httponly;
$options{samesite} = $self->samesite if !exists $options{samesite} && defined $self->samesite;
$options{path} = $self->path || '/' if !exists $options{path};
$options{domain} = $self->domain if !exists $options{domain} && defined $self->domain;
$options{secure} = $self->secure if !exists $options{secure} && defined $self->secure;
$options{httponly} = $self->httponly if !exists $options{httponly} && defined $self->httponly;
$options{samesite} = $self->samesite if !exists $options{samesite} && defined $self->samesite;

# https://developer.mozilla.org/en-US/docs/Web/Privacy/Privacy_sandbox/Partitioned_cookies
$options{partitioned} = $self->partitioned if !exists $options{partitioned} && defined $self->partitioned;


if (!exists $options{expires} && defined $self->expires) {
$options{expires} = time + $self->expires;
}

if ($options{partitioned}) {
$options{secure} = 1;
$options{samesite} = 'None';
}

return %options;
}

Expand All @@ -57,10 +66,10 @@ sub finalize {
sub _set_cookie {
my($self, $id, $res, %options) = @_;

my $cookie = bake_cookie(
my $cookie = bake_cookie(
$self->session_key, {
value => $id,
%options,
%options,
}
);
Plack::Util::header_push($res->[1], 'Set-Cookie', $cookie);
Expand Down
15 changes: 12 additions & 3 deletions t/014_cookie_options.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ $st->expires(3600);
$st->path('/cgi-bin');

is_deeply +{ $st->merge_options(id => 123) },
{ domain => '.example.com', secure => 1, expires => $time + 3600, path => '/cgi-bin' };
{ domain => '.example.com', secure => 1, expires => $time + 3600, path => '/cgi-bin' };

is_deeply +{ $st->merge_options(id => 123, path => '/', domain => '.perl.org') },
{ domain => '.perl.org', secure => 1, expires => $time + 3600, path => '/' };
{ domain => '.perl.org', secure => 1, expires => $time + 3600, path => '/' };

is_deeply +{ $st->merge_options(id => 123, expires => $time + 1, secure => 0, partitioned => 0) },
{ domain => '.example.com', secure => 0, expires => $time + 1, path => '/cgi-bin', partitioned => 0 };

is_deeply +{ $st->merge_options(id => 123, expires => $time + 1, secure => 0, partitioned => 1) },
{ domain => '.example.com', secure => 1, samesite => 'None', expires => $time + 1, path => '/cgi-bin', partitioned => 1 };

$st->partitioned(1);

is_deeply +{ $st->merge_options(id => 123, expires => $time + 1, secure => 0) },
{ domain => '.example.com', secure => 0, expires => $time + 1, path => '/cgi-bin' };
{ domain => '.example.com', secure => 1, samesite => 'None', expires => $time + 1, path => '/cgi-bin', partitioned => 1 };


done_testing;
21 changes: 21 additions & 0 deletions t/015_cookie_options_mw.t
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,26 @@ test_psgi $app, sub {
like $res->header('Set-Cookie'), qr/plack_session=\S+; domain=.example.com; path=\/foo; SameSite=Lax; HttpOnly/;
};

# Partitioned Cookies
# https://developer.mozilla.org/en-US/docs/Web/Privacy/Privacy_sandbox/Partitioned_cookies
$app = Plack::Middleware::Session::Cookie->wrap(
$app,
secret => 'foobar',
httponly => 1,
partitioned => 1
);

test_psgi $app, sub {
my $cb = shift;

# Partitioned cookies are secure, and always have SameSite=None
# Lowercase "secure" provided by Cookie::Baker when using Partitioned.
my $res = $cb->(GET "http://localhost/");
like $res->header('Set-Cookie'), qr/plack_session=\S+; domain=.example.com; SameSite=None; secure; HttpOnly; Partitioned/;

$res = $cb->(GET "http://localhost/with_path");
like $res->header('Set-Cookie'), qr/plack_session=\S+; domain=.example.com; path=\/foo; SameSite=None; secure; HttpOnly; Partitioned/;
};

done_testing;