CookieError: Invalid key value

I use web.py , which internally uses cookie.SimpleCookie to load cookies coming from a user browser.

Sometimes I get exceptions like:

 ... File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/Cookie.py", line 455, in set raise CookieError("Illegal key value: %s" % key) CookieError: Illegal key value: SinaRot/g/news.sina.com.cn 

The offensive character is apparently a slash ( / ), which, according to my reading of RFC 2109 (cookies) and RFC 2068 (HTTP 1.1) should be disabled, so OK.

I do not set this cookie, and I'm not sure why and how it is configured for my domain (perhaps a proxy server), but it does not matter; the big problem is that simplecookie fails when it encounters this cookie and returns an error to the user.

So my question is: is there a way to ask SimpleCookie just ignore invalid cookies but return the rest? I could not find anything obvious in the docs to do this.

+6
source share
2 answers

This works for me.

 def get_cookies(): import Cookie ans = Cookie.SimpleCookie() for bit in os.environ.get('HTTP_COOKIE', '').split('; '): try: ans.load(bit) except Cookie.CookieError: pass return ans 
+3
source

CookieError: Illegal key value: )|utmcmd installed by Google Analytics in Firefox was installed in my web application. To fix this, I issue a redirect trying to set the correct value.

 def myinternalerror(): try: web.cookies() except CookieError: if not "cookie_err" in web.input(): web.setcookie("__utmz", None, domain=web.ctx.host) raise web.seeother(web.changequery(cookie_err=1)) return web.internalerror(render.site.e500()) if not web.config.debug: app.internalerror = myinternalerror 
0
source

Source: https://habr.com/ru/post/895591/


All Articles