@@ -309,10 +309,9 @@ def new(self, environ, request, uri):
309
309
# notify extension, that the new comment has been successfully saved
310
310
self .signal ("comments.new:after-save" , thread , rv )
311
311
312
- cookie = functools .partial (dump_cookie ,
313
- value = self .isso .sign (
314
- [rv ["id" ], sha1 (rv ["text" ])]),
315
- max_age = self .conf .getint ('max-age' ))
312
+ cookie = self .create_cookie (
313
+ value = self .isso .sign ([rv ["id" ], sha1 (rv ["text" ])]),
314
+ max_age = self .conf .getint ('max-age' ))
316
315
317
316
rv ["text" ] = self .isso .render (rv ["text" ])
318
317
rv ["hash" ] = self .hash (rv ['email' ] or rv ['remote_addr' ])
@@ -348,6 +347,24 @@ def _remote_addr(self, request):
348
347
if addr not in self .trusted_proxies ), remote_addr )
349
348
return utils .anonymize (str (remote_addr ))
350
349
350
+ def create_cookie (self , ** kwargs ):
351
+ """
352
+ Setting cookies to SameSite=None requires "Secure" attribute.
353
+ For http-only, we need to override the dump_cookie() default SameSite=None
354
+ or the cookie will be rejected.
355
+ https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite#samesitenone_requires_secure
356
+ """
357
+ isso_host_script = self .isso .conf .get ("server" , "public-endpoint" ) or local .host
358
+ samesite = self .isso .conf .get ("server" , "samesite" )
359
+ if isso_host_script .startswith ("https://" ):
360
+ secure = True
361
+ samesite = samesite or "None"
362
+ else :
363
+ secure = False
364
+ samesite = samesite or "Lax"
365
+ return functools .partial (dump_cookie , ** kwargs ,
366
+ secure = secure , samesite = samesite )
367
+
351
368
"""
352
369
@api {get} /id/:id view
353
370
@apiGroup Comment
@@ -458,10 +475,9 @@ def edit(self, environ, request, id):
458
475
459
476
self .signal ("comments.edit" , rv )
460
477
461
- cookie = functools .partial (dump_cookie ,
462
- value = self .isso .sign (
463
- [rv ["id" ], sha1 (rv ["text" ])]),
464
- max_age = self .conf .getint ('max-age' ))
478
+ cookie = self .create_cookie (
479
+ value = self .isso .sign ([rv ["id" ], sha1 (rv ["text" ])]),
480
+ max_age = self .conf .getint ('max-age' ))
465
481
466
482
rv ["text" ] = self .isso .render (rv ["text" ])
467
483
@@ -474,7 +490,7 @@ def edit(self, environ, request, id):
474
490
@api {delete} '/id/:id' delete
475
491
@apiGroup Comment
476
492
@apiDescription
477
- Delte an existing comment. Deleting a comment is only possible for a short period of time after it was created and only if the requestor has a valid cookie for it. See the [isso server documentation](https://posativ.org/isso/docs/configuration/server) for details.
493
+ Delete an existing comment. Deleting a comment is only possible for a short period of time after it was created and only if the requestor has a valid cookie for it. See the [isso server documentation](https://posativ.org/isso/docs/configuration/server) for details.
478
494
479
495
@apiParam {number} id
480
496
Id of the comment to delete.
@@ -518,7 +534,8 @@ def delete(self, environ, request, id, key=None):
518
534
self .signal ("comments.delete" , id )
519
535
520
536
resp = JSON (rv , 200 )
521
- cookie = functools .partial (dump_cookie , expires = 0 , max_age = 0 )
537
+ cookie = self .create_cookie (expires = 0 , max_age = 0 )
538
+
522
539
resp .headers .add ("Set-Cookie" , cookie (str (id )))
523
540
resp .headers .add ("X-Set-Cookie" , cookie ("isso-%i" % id ))
524
541
return resp
@@ -1107,9 +1124,8 @@ def login(self, env, req):
1107
1124
'/admin' ,
1108
1125
get_current_url (env , strip_querystring = True )
1109
1126
))
1110
- cookie = functools .partial (dump_cookie ,
1111
- value = self .isso .sign ({"logged" : True }),
1112
- expires = datetime .now () + timedelta (1 ))
1127
+ cookie = self .create_cookie (value = self .isso .sign ({"logged" : True }),
1128
+ expires = datetime .now () + timedelta (1 ))
1113
1129
response .headers .add ("Set-Cookie" , cookie ("admin-session" ))
1114
1130
response .headers .add ("X-Set-Cookie" , cookie ("isso-admin-session" ))
1115
1131
return response
0 commit comments