-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SecureCookie: serialization issues #287
Comments
I just ran your first example and it seems to run fine. What is your output? $ python -i werkzeug/datastructures.py
>>> from werkzeug.contrib.securecookie import SecureCookie
>>> c1 = SecureCookie(
... MultiDict([('multikey', u'x'), ('multikey', u'xx'), ('monokey', 'y')]), secret_key='xxx'
... )
>>> c1
<SecureCookie {'multikey': [u'x', u'xx'], 'monokey': ['y']}>
>>> c2 = SecureCookie(
... {'data': MultiDict([('multikey', u'x'), ('multikey', u'xx'), ('monokey', 'y')])}, secret_key='xxx'
... )
>>> c2
<SecureCookie {'data': MultiDict([('multikey', u'x'), ('multikey', u'xx'), ('monokey', 'y')])}>
>>> SecureCookie.unserialize(c1.serialize(), secret_key='xxx')
<SecureCookie {'multikey': [u'x', u'xx'], 'monokey': ['y']}>
>>> SecureCookie.unserialize(c2.serialize(), secret_key='xxx')
<SecureCookie {'data': MultiDict([('multikey', u'x'), ('multikey', u'xx'), ('monokey', 'y')])}>
>>> |
Example 1. >>> c1
<SecureCookie {'multikey': [u'x', u'xx'], 'monokey': ['y']}> I.e. list instead of MultiDict. Example 2. >>> c2
<SecureCookie {'data': {u'multikey': u'x', u'monokey': u'y'}}> I.e. only first value of multikey remained. In your Example 2. you've obtain diverse output (list vs MultiDict) as well. I wonder why default behavior of As I've said, i see no preference of MultiDict over simple dicts with list values as it done in other languages like PHP. My version of Werkzeug is the last that installs with pip. |
That does make sense to me, since SecureCookie implements actually a dict interface and therefore has to convert the MultiDict's keys into a normal dict first.
That is different from mine, i guess that has already been fixed between the latest release and the current HEAD.
It seems sensible to me that getting a value via MultiDict.getitem actually returns an item (as the method name implies) and not a list of those. Which third-party library are you talking about? If it expects a normal dictionary, you shouldn't be surprised that passing MultiDict isn't going to end that well. |
Also: 1.) Referencing to PHP is not such a good idea |
I refer PHP for only one single reason: it uses notably better (newer) convention for serialization which is strongly connected to the bug we discuss. http://api.jquery.com/jQuery.param/#jQuery-param-obj-traditional
|
You did read my comment about your output? Does the original issue still exist with an installation from master? |
Yes. The same output with development branch. |
Can you write a test case that fails for you? I am not sure atm if we are talking about |
Also i think we are talking past each other, so here goes: >>> cookie1 = SecureCookie(
... MultiDict([('multikey', u'x'), ('multikey', u'xx'), ('monokey', 'y')]), secret_key='xxx'
...)
>> cookie1
<SecureCookie {'multikey': [u'x', u'xx'], 'monokey': ['y']}> # reasonable, as SecureCookie has to transform the values of MultiDict into a normal dict (SecureCookie is a dict itself)
>>> cookie2 = SecureCookie(
... {'data': MultiDict([('multikey', u'x'), ('multikey', u'xx'), ('monokey', 'y')])}, secret_key='xxx'
... )
>>> cookie2
<SecureCookie {'data': {u'multikey': u'x', u'monokey': u'y'}}> # you said this behavior exists in the dev branch?
>>> SecureCookie.unserialize(cookie1.serialize(), secret_key='xxx') # post the output for this
# ???
>>> SecureCookie.unserialize(cookie2.serialize(), secret_key='xxx') # and this
# ??? |
Updated example: import json
import simplejson
import werkzeug
from werkzeug.datastructures import MultiDict
from werkzeug.contrib.securecookie import SecureCookie
class SecureCookie(SecureCookie):
serialization_method = json
# serialization_method = simplejson — same result :(
>>> werkzeug.__version__
'0.9-dev'
>>> cookie = SecureCookie(
... {'data': MultiDict([('multikey', u'x'), ('multikey', u'xx'), ('monokey', 'y')])}, secret_key='xxx'
... )
>>> cookie
<SecureCookie {'data': MultiDict([('multikey', u'x'), ('multikey', u'xx'), ('monokey', 'y')])}>
>>> SecureCookie.unserialize(cookie.serialize(), secret_key='xxx')
<SecureCookie {'data': {u'multikey': u'x', u'monokey': u'y'}}> |
As bad as it sounds: works as intended. MultiDict's do not work with JSON. |
See #389 |
P.S.
MultiDict provokes plenty of problems around. Especially with 3-rd party libs. At least, it causes dependency breeding.
To be fair, I believe that adding all those XxxMultiDict was design mistake and I would consider total removal.
The text was updated successfully, but these errors were encountered: