Skip to content

Commit 58ad83f

Browse files
committed
Added support for bytes in sessions back
1 parent c502dfb commit 58ad83f

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Pending bugfix release.
1212
made the filter not work properly in HTML attributes. Now it's
1313
possible to use that filter in single quoted attributes. This should
1414
make using that filter with angular.js easier.
15+
- Added support for byte strings back to the session system. This broke
16+
compatibility with the common case of people putting binary data for
17+
token verification into the session.
1518

1619
Version 0.10
1720
------------

flask/sessions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import uuid
1313
import hashlib
14+
from base64 import b64encode, b64decode
1415
from datetime import datetime
1516
from werkzeug.http import http_date, parse_date
1617
from werkzeug.datastructures import CallbackDict
@@ -62,6 +63,8 @@ def _tag(value):
6263
return {' t': [_tag(x) for x in value]}
6364
elif isinstance(value, uuid.UUID):
6465
return {' u': value.hex}
66+
elif isinstance(value, bytes):
67+
return {' b': b64encode(value).decode('ascii')}
6568
elif callable(getattr(value, '__html__', None)):
6669
return {' m': text_type(value.__html__())}
6770
elif isinstance(value, list):
@@ -90,6 +93,8 @@ def object_hook(obj):
9093
return tuple(the_value)
9194
elif the_key == ' u':
9295
return uuid.UUID(the_value)
96+
elif the_key == ' b':
97+
return b64decode(the_value)
9398
elif the_key == ' m':
9499
return Markup(the_value)
95100
elif the_key == ' d':

flask/testsuite/basic.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ def modify_session(response):
326326
flask.session['m'] = flask.Markup('Hello!')
327327
flask.session['u'] = the_uuid
328328
flask.session['dt'] = now
329+
flask.session['b'] = b'\xff'
329330
flask.session['t'] = (1, 2, 3)
330331
return response
331332

@@ -340,6 +341,8 @@ def dump_session_contents():
340341
self.assert_equal(type(rv['m']), flask.Markup)
341342
self.assert_equal(rv['dt'], now)
342343
self.assert_equal(rv['u'], the_uuid)
344+
self.assert_equal(rv['b'], b'\xff')
345+
self.assert_equal(type(rv['b']), bytes)
343346
self.assert_equal(rv['t'], (1, 2, 3))
344347

345348
def test_flashes(self):

0 commit comments

Comments
 (0)