2024-10-05 13:23:46 发布
网友
我正在用CherryPy编写一个软件。我正在使用普通会话,使用“cherrypy.session”。 现在我注意到Firefox抱怨我使用了“错误的”samesite属性,并且它可能在将来不再可用
有没有办法将CherryPy会话cookies的samesite属性设置为另一个值
这有点棘手,已经讨论过好几次了(例如,lookhere) 这意味着python显然没有解决方案<;3.8. 但是,您仍然可以使用monkeypatch。 因此,请执行以下操作以解决问题:
打开../python3.x/site-packages/cherrypy//u cprequest.py
在文件开头添加以下代码
from http import cookies cookies.Morsel._reserved.setdefault('samesite', 'SameSite')
关闭并保存它
打开../python3.x/site-packages/cherrypy/lib/sessions.py
更改以下函数定义
def init(storage_type=None, path=None, path_header=None, name='session_id', timeout=60, domain=None, secure=False, clean_freq=5, persistent=True, httponly=False, debug=False, # Py27 compat # *, storage_class=RamSession, **kwargs):
到
def init(storage_type=None, path=None, path_header=None, name='session_id', timeout=60, domain=None, secure=False, clean_freq=5, persistent=True, httponly=False,samesite='lax' debug=False, # Py27 compat # *, storage_class=RamSession, **kwargs):
更改以下内容:
set_response_cookie(path=path, path_header=path_header, name=name, timeout=cookie_timeout, domain=domain, secure=secure, httponly=httponly)
致:
set_response_cookie(path=path, path_header=path_header, name=name, timeout=cookie_timeout, domain=domain, secure=secure, httponly=httponly,samesite=samesite)
def set_response_cookie(path=None, path_header=None, name='session_id', timeout=60, domain=None, secure=False, httponly=False):
def set_response_cookie(path=None, path_header=None, name='session_id', timeout=60, domain=None, secure=False, httponly=False,samesite='lax'):
将以下代码添加到set_response_cookie()方法的末尾:
if samesite in ['lax', 'strict', None]: cookie[name]['samesite'] = str(samesite)
保存文件并关闭它
现在,您可以在代码(驱动程序)中使用“samesite”属性,如下所示:
'tools.sessions.samesite': 'strict'
或
'tools.sessions.samesite': 'lax' # This is the default value
祝你好运
这有点棘手,已经讨论过好几次了(例如,lookhere) 这意味着python显然没有解决方案<;3.8. 但是,您仍然可以使用monkeypatch。 因此,请执行以下操作以解决问题:
打开../python3.x/site-packages/cherrypy//u cprequest.py
在文件开头添加以下代码
关闭并保存它
打开../python3.x/site-packages/cherrypy/lib/sessions.py
更改以下函数定义
到
更改以下内容:
致:
更改以下内容:
致:
将以下代码添加到set_response_cookie()方法的末尾:
保存文件并关闭它
现在,您可以在代码(驱动程序)中使用“samesite”属性,如下所示:
或
祝你好运
相关问题 更多 >
编程相关推荐