From 049d6207893a125fec9bb327371014ac629a01bb Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 9 Sep 2020 18:53:26 -0700 Subject: [PATCH] Convert cookies to werkzeug MultiDict instead of plain dict. I discovered that werkzeug stores cookies in lists, with its .get returning only the first item of the list. By converting the cookies to a plain dict, I was breaking that functionality of cookies.get. So, using werkzeug's MultiDict is the correct choice. --- frontends/etiquette_flask/backend/sessions.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/frontends/etiquette_flask/backend/sessions.py b/frontends/etiquette_flask/backend/sessions.py index e6e3d78..16554c0 100644 --- a/frontends/etiquette_flask/backend/sessions.py +++ b/frontends/etiquette_flask/backend/sessions.py @@ -1,6 +1,7 @@ import flask; from flask import request import functools import werkzeug.wrappers +import werkzeug.datastructures from voussoirkit import cacheclass @@ -62,7 +63,13 @@ class SessionManager: token = request.cookies.get('etiquette_session', None) if not token or token not in self.sessions: token = _generate_token() - request.cookies = dict(request.cookies) + # cookies is currently an ImmutableMultiDict, but in order to + # trick the wrapped function I'm gonna have to mutate it. + # It is important to use a werkzeug MultiDict and not a plain + # Python dict, because werkzeug puts cookies into lists like + # {name: [value]} and then cookies.get pulls the first item out + # of that list. A plain dict wouldn't have this .get behavior. + request.cookies = werkzeug.datastructures.MultiDict(request.cookies) request.cookies['etiquette_session'] = token try: