Skip to content
Myr

Session falls back to a dummy object now if secret_key is missing.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This makes it possible to still read-only access the empty session but requires the secret key to be set for write access. The error message raised explains that. This closes #10.

authored by Armin Ronacher · committed by Armin Ronacher · parent 791cdb2 · Browse files · 2 files+34 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+34 added−3 removed

  • tests:+14 added−0 removed
  • other:+20 added−3 removed

2 files+34 added−3 removed

modified:tests/flask_tests.py+14 added
70 unmodified lines
71 c = app.test_client()72 assert c.post('/set', data={'value': '42'}).data == 'value set'73 assert c.get('/get').data == '42'7475 def test_missing_session(self):76 app = flask.Flask(__name__)77 def expect_exception(f, *args, **kwargs):78 try:79 f(*args, **kwargs)80 except RuntimeError, e:81 assert e.args and 'session is unavailable' in e.args[0]82 else:83 assert False, 'expected exception'84 with app.test_request_context():85 assert flask.session.get('missing_key') is None86 expect_exception(flask.session.__setitem__, 'foo', 42)87 expect_exception(flask.session.pop, 'foo')8889 def test_request_processing(self):90 app = flask.Flask(__name__)91 evts = []92 @app.before_request
110 unmodified lines
modified:flask.py+20 added-3 removed
66 unmodified lines
67class _RequestGlobals(object):68 pass697071class _NullSession(SecureCookie):72 """Class used to generate nicer error messages if sessions are not73 available. Will still allow read-only access to the empty session74 but fail on setting.75 """7677 def _fail(self, *args, **kwargs):78 raise RuntimeError('the session is unavailable because no secret '79 'key was set. Set the secret_key on the '80 'application to something unique and secret')81 __setitem__ = __delitem__ = clear = pop = popitem = \82 update = setdefault = _fail83 del _fail84858687class _RequestContext(object):88 """The request context contains all request relevant information. It is89 created at the beginning of the request and pushed to the90 `_request_ctx_stack` and removed at the end of it. It will create the
4 unmodified lines
95 self.app = app96 self.url_adapter = app.url_map.bind_to_environ(environ)97 self.request = app.request_class(environ)98 self.session = app.open_session(self.request)99 if self.session is None:100 self.session = _NullSession()101 self.g = _RequestGlobals()102 self.flashes = None103104 def __enter__(self):
296 unmodified lines
401 :class:`~werkzeug.contrib.securecookie.SecureCookie`402 object)403 :param response: an instance of :attr:`response_class`404 """387 if session is not None:388 session.save_cookie(response, self.session_cookie_name)405 session.save_cookie(response, self.session_cookie_name)406407 def add_url_rule(self, rule, endpoint, **options):408 """Connects a URL rule. Works exactly like the :meth:`route`409 decorator but does not register the view function for the endpoint.
209 unmodified lines
619 :return: a new response object or the same, has to be an620 instance of :attr:`response_class`.621 """622 session = _request_ctx_stack.top.session606 if session is not None:623 if not isinstance(session, _NullSession):624 self.save_session(session, response)625 for handler in self.after_request_funcs:626 response = handler(response)627 return response
53 unmodified lines