Skip to content

Commit 0528f60

Browse files
authored
Merge pull request #2731 from drgrice1/session-tangling-fix
Reset the cookie session before Mojolicious saves it.
2 parents 03a6de0 + 936520d commit 0528f60

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

lib/WeBWorK/Authen.pm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,10 @@ sub store_session {
844844
}
845845
}
846846

847+
# The session parameters need to be set again, because another request may have occured during this
848+
# request in which case the session parameters for the app will now be set for that request.
849+
$self->{c}->setSessionParams;
850+
847851
return;
848852
}
849853

lib/WeBWorK/Controller.pm

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ sub param ($c, @opts) {
4545
return wantarray ? @{ $c->{paramcache}{$name} } : $c->{paramcache}{$name}[0];
4646
}
4747

48+
sub setSessionParams ($c) {
49+
$c->app->sessions->cookie_name(
50+
$c->stash('courseID') ? 'WeBWorKCourseSession.' . $c->stash('courseID') : 'WeBWorKGeneralSession');
51+
52+
# If the hostname is 'localhost' or '127.0.0.1', then the cookie domain must be omitted.
53+
my $hostname = $c->req->url->to_abs->host;
54+
$c->app->sessions->cookie_domain($hostname) if $hostname ne 'localhost' && $hostname ne '127.0.0.1';
55+
56+
$c->app->sessions->cookie_path($c->ce->{webworkURLRoot});
57+
$c->app->sessions->secure($c->ce->{CookieSecure});
58+
59+
# If this is a session for LTI content selection, then always use SameSite None. Otherwise cookies will not be
60+
# sent since this is in an iframe embedded in the LMS.
61+
$c->app->sessions->samesite($c->stash->{isContentSelection} ? 'None' : $c->ce->{CookieSameSite});
62+
63+
return;
64+
}
65+
4866
# Override the Mojolicious::Controller session method to set the cookie parameters
4967
# from the course environment the first time it is called.
5068
sub session ($c, @args) {
@@ -53,20 +71,7 @@ sub session ($c, @args) {
5371
# Initialize the cookie session the first time this is called.
5472
unless ($c->stash->{'webwork2.cookie_session_initialized'}) {
5573
$c->stash->{'webwork2.cookie_session_initialized'} = 1;
56-
57-
$c->app->sessions->cookie_name(
58-
$c->stash('courseID') ? 'WeBWorKCourseSession.' . $c->stash('courseID') : 'WeBWorKGeneralSession');
59-
60-
# If the hostname is 'localhost' or '127.0.0.1', then the cookie domain must be omitted.
61-
my $hostname = $c->req->url->to_abs->host;
62-
$c->app->sessions->cookie_domain($hostname) if $hostname ne 'localhost' && $hostname ne '127.0.0.1';
63-
64-
$c->app->sessions->cookie_path($c->ce->{webworkURLRoot});
65-
$c->app->sessions->secure($c->ce->{CookieSecure});
66-
67-
# If this is a session for LTI content selection, then always use SameSite None. Otherwise cookies will not be
68-
# sent since this is in an iframe embedded in the LMS.
69-
$c->app->sessions->samesite($c->stash->{isContentSelection} ? 'None' : $c->ce->{CookieSameSite});
74+
$c->setSessionParams;
7075
}
7176

7277
return $c->SUPER::session(@args);

0 commit comments

Comments
 (0)