This is a bug in Flask-Login that has been addressed. See the pull request with the fix.
flask_login: UnicodeDecodeError: 'ascii' codec can't decode byte
-
24-06-2022 - |
Question
I am aware of the ascii/unicode problem and have looked at posts like this.
However the error message I am getting on GAE, doesn't give me any clue where this problem is happening in order to fix it. It seems it happening on cookie_digest in flask_login? But why there?
Can someone point me into the right direction please.
Exception on /login [POST]
Traceback (most recent call last):
File "/base/data/home/apps/s~service/1-0-1-0.369408465659150157/lib/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/base/data/home/apps/s~service/1-0-1-0.369408465659150157/lib/flask/app.py", line 1362, in full_dispatch_request
response = self.process_response(response)
File "/base/data/home/apps/s~service/1-0-1-0.369408465659150157/lib/flask/app.py", line 1564, in process_response
response = handler(response)
File "/base/data/home/apps/s~service/1-0-1-0.369408465659150157/lib/flask_login.py", line 344, in _update_remember_cookie
self._set_cookie(response)
File "/base/data/home/apps/s~service/1-0-1-0.369408465659150157/lib/flask_login.py", line 363, in _set_cookie
data = encode_cookie(str(session['user_id']))
File "/base/data/home/apps/s~service/1-0-1-0.369408465659150157/lib/flask_login.py", line 444, in encode_cookie
return u'{0}|{1}'.format(payload, _cookie_digest(payload))
File "/base/data/home/apps/s~service/1-0-1-0.369408465659150157/lib/flask_login.py", line 688, in _cookie_digest
key = key.encode('utf-8') # ensure bytes
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc9 in position 0: ordinal not in range(128)
EDIT:
After digging in the flask_login, it seems it is happening with the secret key. How strange!!! I have put some logging around it to see why it is happening for only one user of our app. And its reproduce-able, every time she tries to login, she gets an exception. But other than her, I can't reproduce it.
def _cookie_digest(payload, key=None):
if key is None:
key = current_app.config['SECRET_KEY']
logging.info(u'key - cookie - {0}'.format(key))
if hasattr(key, 'encode'):
key = key.encode('utf-8') # ensure bytes
return hmac.new(key, payload.encode('utf-8'), sha1).hexdigest()
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow