-
Recently we upgraded our legacy cherrypy-8.1 to 18.3 along with python-3 upgrade. Post upgrade, we are facing an issue with a Trying to understand, what is the hook on my application side where I can modify the incoming cookie! I have tried doing this in class CookieModifierTool(cherrypy.Tool):
def __init__(self):
cherrypy.Tool.__init__(self, 'on_start_resource',
self.modify_cookie,
priority=30)
def modify_cookie(self):
cookie_header = cherrypy.request.headers.get("Cookie")
Log.info('Request Cookie header string = %s', cookie_header)
if cookie_header:
modified_cookie_header = cookie_header.replace(" ", "")
cherrypy.request.headers["Cookie"] = modified_cookie_header
updated_cookie_header = cherrypy.request.headers.get("Cookie")
Log.info('Request Cookie header string = %s', updated_cookie_header)
request_cookies = cherrypy.request.cookie
Log.info('Checking direct cookie value of direct session id = %s', request_cookies.get('session_id'))
Log.info('Checking direct cookie value of cmapi_cookie_privacy = %s', request_cookies.get('cmapi_cookie_privacy'))
cherrypy.tools.modify_cookie = CookieModifierTool() And in the config, I added this - config = {
"global": {
"tools.modify_cookie.on": True,
....
},
"/": {...}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
https://docs.cherrypy.dev/en/latest/extend.html#hook-point:
So I think it's too late. Also, HTTP parsing happens in Cheroot, a layer below CherryPy. I don't think you can easily mess with the HTTP parser there. |
Beta Was this translation helpful? Give feedback.
-
Thanks @webknjaz for your review comments. :) class CookieModifierTool(cherrypy.Tool):
"""
Modifying the request header cookie value if it has space.
"""
def __init__(self):
super().__init__(
'on_start_resource',
self.modify_cookie,
priority=1,
)
def modify_cookie(self):
cookie_header = cherrypy.request.headers.get("Cookie")
if not cookie_header:
return
modified_cookie_header = cookie_header.replace(" ", "")
cherrypy.request.headers["Cookie"] = modified_cookie_header
# Parse the modified cookie header manually
parsed_cookies = cookies.SimpleCookie()
parsed_cookies.load(modified_cookie_header)
# Update the cherrypy.request.cookie object with the modified cookies
cherrypy.request.cookie = parsed_cookies
cherrypy.tools.modify_cookie = CookieModifierTool()
config = {
"global": {
"tools.modify_cookie.on": True,
....
},
"/": {...}
} |
Beta Was this translation helpful? Give feedback.
Thanks @webknjaz for your review comments. :)
This is the final snippet as per your review comments. Hope this is fine, I'm just testing it out.