@@ -110,6 +110,7 @@ def __init__(self, redis, key_prefix, use_signer=False, permanent=True):
110110 self .key_prefix = key_prefix
111111 self .use_signer = use_signer
112112 self .permanent = permanent
113+ self .has_same_site_capability = hasattr (self , "get_cookie_samesite" )
113114
114115 def open_session (self , app , request ):
115116 sid = request .cookies .get (app .session_cookie_name )
@@ -157,9 +158,11 @@ def save_session(self, app, session, response):
157158 # the permanent flag on the session itself.
158159 # if not self.should_set_cookie(app, session):
159160 # return
160-
161+ conditional_cookie_kwargs = {}
161162 httponly = self .get_cookie_httponly (app )
162163 secure = self .get_cookie_secure (app )
164+ if self .has_same_site_capability :
165+ conditional_cookie_kwargs ["samesite" ] = self .get_cookie_samesite (app )
163166 expires = self .get_expiration_time (app , session )
164167 val = self .serializer .dumps (dict (session ))
165168 self .redis .setex (name = self .key_prefix + session .sid , value = val ,
@@ -170,7 +173,8 @@ def save_session(self, app, session, response):
170173 session_id = session .sid
171174 response .set_cookie (app .session_cookie_name , session_id ,
172175 expires = expires , httponly = httponly ,
173- domain = domain , path = path , secure = secure )
176+ domain = domain , path = path , secure = secure ,
177+ ** conditional_cookie_kwargs )
174178
175179
176180class MemcachedSessionInterface (SessionInterface ):
@@ -197,6 +201,7 @@ def __init__(self, client, key_prefix, use_signer=False, permanent=True):
197201 self .key_prefix = key_prefix
198202 self .use_signer = use_signer
199203 self .permanent = permanent
204+ self .has_same_site_capability = hasattr (self , "get_cookie_samesite" )
200205
201206 def _get_preferred_memcache_client (self ):
202207 servers = ['127.0.0.1:11211' ]
@@ -272,8 +277,11 @@ def save_session(self, app, session, response):
272277 domain = domain , path = path )
273278 return
274279
280+ conditional_cookie_kwargs = {}
275281 httponly = self .get_cookie_httponly (app )
276282 secure = self .get_cookie_secure (app )
283+ if self .has_same_site_capability :
284+ conditional_cookie_kwargs ["samesite" ] = self .get_cookie_samesite (app )
277285 expires = self .get_expiration_time (app , session )
278286 if not PY2 :
279287 val = self .serializer .dumps (dict (session ), 0 )
@@ -287,7 +295,8 @@ def save_session(self, app, session, response):
287295 session_id = session .sid
288296 response .set_cookie (app .session_cookie_name , session_id ,
289297 expires = expires , httponly = httponly ,
290- domain = domain , path = path , secure = secure )
298+ domain = domain , path = path , secure = secure ,
299+ ** conditional_cookie_kwargs )
291300
292301
293302class FileSystemSessionInterface (SessionInterface ):
@@ -314,6 +323,7 @@ def __init__(self, cache_dir, threshold, mode, key_prefix,
314323 self .key_prefix = key_prefix
315324 self .use_signer = use_signer
316325 self .permanent = permanent
326+ self .has_same_site_capability = hasattr (self , "get_cookie_samesite" )
317327
318328 def open_session (self , app , request ):
319329 sid = request .cookies .get (app .session_cookie_name )
@@ -346,8 +356,11 @@ def save_session(self, app, session, response):
346356 domain = domain , path = path )
347357 return
348358
359+ conditional_cookie_kwargs = {}
349360 httponly = self .get_cookie_httponly (app )
350361 secure = self .get_cookie_secure (app )
362+ if self .has_same_site_capability :
363+ conditional_cookie_kwargs ["samesite" ] = self .get_cookie_samesite (app )
351364 expires = self .get_expiration_time (app , session )
352365 data = dict (session )
353366 self .cache .set (self .key_prefix + session .sid , data ,
@@ -358,7 +371,8 @@ def save_session(self, app, session, response):
358371 session_id = session .sid
359372 response .set_cookie (app .session_cookie_name , session_id ,
360373 expires = expires , httponly = httponly ,
361- domain = domain , path = path , secure = secure )
374+ domain = domain , path = path , secure = secure ,
375+ ** conditional_cookie_kwargs )
362376
363377
364378class MongoDBSessionInterface (SessionInterface ):
@@ -388,6 +402,7 @@ def __init__(self, client, db, collection, key_prefix, use_signer=False,
388402 self .key_prefix = key_prefix
389403 self .use_signer = use_signer
390404 self .permanent = permanent
405+ self .has_same_site_capability = hasattr (self , "get_cookie_samesite" )
391406
392407 def open_session (self , app , request ):
393408 sid = request .cookies .get (app .session_cookie_name )
@@ -431,8 +446,11 @@ def save_session(self, app, session, response):
431446 domain = domain , path = path )
432447 return
433448
449+ conditional_cookie_kwargs = {}
434450 httponly = self .get_cookie_httponly (app )
435451 secure = self .get_cookie_secure (app )
452+ if self .has_same_site_capability :
453+ conditional_cookie_kwargs ["samesite" ] = self .get_cookie_samesite (app )
436454 expires = self .get_expiration_time (app , session )
437455 val = self .serializer .dumps (dict (session ))
438456 self .store .update ({'id' : store_id },
@@ -445,7 +463,8 @@ def save_session(self, app, session, response):
445463 session_id = session .sid
446464 response .set_cookie (app .session_cookie_name , session_id ,
447465 expires = expires , httponly = httponly ,
448- domain = domain , path = path , secure = secure )
466+ domain = domain , path = path , secure = secure ,
467+ ** conditional_cookie_kwargs )
449468
450469
451470class SqlAlchemySessionInterface (SessionInterface ):
@@ -473,6 +492,7 @@ def __init__(self, app, db, table, key_prefix, use_signer=False,
473492 self .key_prefix = key_prefix
474493 self .use_signer = use_signer
475494 self .permanent = permanent
495+ self .has_same_site_capability = hasattr (self , "get_cookie_samesite" )
476496
477497 class Session (self .db .Model ):
478498 __tablename__ = table
@@ -541,8 +561,11 @@ def save_session(self, app, session, response):
541561 domain = domain , path = path )
542562 return
543563
564+ conditional_cookie_kwargs = {}
544565 httponly = self .get_cookie_httponly (app )
545566 secure = self .get_cookie_secure (app )
567+ if self .has_same_site_capability :
568+ conditional_cookie_kwargs ["samesite" ] = self .get_cookie_samesite (app )
546569 expires = self .get_expiration_time (app , session )
547570 val = self .serializer .dumps (dict (session ))
548571 if saved_session :
@@ -559,4 +582,5 @@ def save_session(self, app, session, response):
559582 session_id = session .sid
560583 response .set_cookie (app .session_cookie_name , session_id ,
561584 expires = expires , httponly = httponly ,
562- domain = domain , path = path , secure = secure )
585+ domain = domain , path = path , secure = secure ,
586+ ** conditional_cookie_kwargs )
0 commit comments