Skip to content

Commit 1d6b100

Browse files
Changes from PerlDancer#1217
1 parent a24da35 commit 1d6b100

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

lib/Dancer/Cookie.pm

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ package Dancer::Cookie;
44
use strict;
55
use warnings;
66

7+
use Carp;
78
use URI::Escape;
89

910
use base 'Dancer::Object';
10-
__PACKAGE__->attributes( qw/name expires domain path secure http_only/ );
11+
__PACKAGE__->attributes( qw/name expires domain path same_site secure http_only/ );
1112

1213
sub init {
1314
my ($self, %args) = @_;
@@ -22,6 +23,16 @@ sub init {
2223
$self->expires($time);
2324
}
2425
$self->path('/') unless defined $self->path;
26+
27+
# If we have a same_site attribute, ensure it's sane:
28+
if (my $same_site = $self->same_site) {
29+
if ($same_site !~ m{^(Strict|Lax|None)$}) {
30+
Carp::croak(
31+
"Invalid same_site value '$same_site'"
32+
. " - must be 'Strict', 'Lax' or 'None', see RFC6265bis"
33+
);
34+
}
35+
}
2536
}
2637

2738
sub to_header {
@@ -35,10 +46,11 @@ sub to_header {
3546
$name =~ s/[=,; \t\r\n\013\014]//mg;
3647

3748
my @headers = $name . '=' . $value;
38-
push @headers, "path=" . $self->path if $self->path;
39-
push @headers, "expires=" . $self->expires if $self->expires;
40-
push @headers, "domain=" . $self->domain if $self->domain;
41-
push @headers, "Secure" if $self->secure;
49+
push @headers, "path=" . $self->path if $self->path;
50+
push @headers, "expires=" . $self->expires if $self->expires;
51+
push @headers, "domain=" . $self->domain if $self->domain;
52+
push @headers, "Secure" if $self->secure;
53+
push @headers, "SameSite=" . $self->same_site if $self->same_site;
4254
push @headers, 'HttpOnly' unless $no_httponly;
4355

4456
return join '; ', @headers;

lib/Dancer/Session/Abstract.pm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ sub write_session_id {
144144
secure => setting('session_secure'),
145145
http_only => defined(setting("session_is_http_only")) ?
146146
setting("session_is_http_only") : 1,
147+
same_site => defined(setting("session_same_site")) ?
148+
setting("session_same_site") ? 'None',
147149
);
148150
if (my $expires = setting('session_expires')) {
149151
# It's # of seconds from the current time

t/09_cookies/05_api.t

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ use Dancer ':syntax';
33

44
my @tests = (
55
{ name => 'foo', value => 42 , opts => {}},
6-
{ name => 'foo', value => 42 , opts => { http_only => 1 } },
7-
{ name => 'msg', value => 'hello; world', opts => {} },
8-
{ name => 'msg', value => 'hello; world', opts => { http_only => 0 } },
6+
{ name => 'foo', value => 42 , opts => { http_only => 1 } },
7+
{ name => 'msg', value => 'hello; world', opts => {} },
8+
{ name => 'msg', value => 'hello; world', opts => { http_only => 0 } },
9+
{ name => 'ss', value => 'samesitetest', opts => { same_site => 'Lax' } },
910
);
1011

1112
plan tests => scalar (@tests * 5) + 12;
@@ -21,6 +22,9 @@ foreach my $test (@tests) {
2122
is $c->http_only,
2223
(exists($test->{opts}{http_only}) ? $test->{opts}{http_only} : undef),
2324
"HttpOnly is correctly set";
25+
is $c->same_site,
26+
(exists($test->{opts}{same_site}) ? $test->{opts}{same_site} : undef),
27+
"SameSite is correctly set";
2428
}
2529

2630
{

0 commit comments

Comments
 (0)