Added finer control over the session cookie parameters
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 23bf263 · Browse files · 5 files·+69 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+69 added−6 removed
- other:+69 added−6 removed
Provenance
- Commit
ccf4641- Tree
5b8ba0d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
23bf263
5 files changed+69 added−6 removed
modified:docs/config.rst+17 added-1 removed
68 unmodified lines69 debug production applications (but also70 very risky).71 ``SECRET_KEY`` the secret key72 ``SESSION_COOKIE_NAME`` the name of the session cookie73+``SESSION_COOKIE_DOMAIN`` the domain for the session cookie. If74+ this is not set, the cookie will be75+ valid for all subdomains of76+ ``SERVER_NAME``.77+``SESSION_COOKIE_PATH`` the path for the session cookie. If78+ this is not set the cookie will be valid79+ for all of ``APPLICATION_ROOT`` or if80+ that is not set for ``'/'``.81+``SESSION_COOKIE_HTTPONLY`` controls if the cookie should be set82+ with the httponly flag. Defaults to83+ `True`.84+``SESSION_COOKIE_SECURE`` controls if the cookie should be set85+ with the secure flag. Defaults to86+ `False`.87 ``PERMANENT_SESSION_LIFETIME`` the lifetime of a permanent session as88 :class:`datetime.timedelta` object.89 ``USE_X_SENDFILE`` enable/disable x-sendfile90 ``LOGGER_NAME`` the name of the logger64 unmodified lines155 ``PROPAGATE_EXCEPTIONS``, ``PRESERVE_CONTEXT_ON_EXCEPTION``156 157 .. versionadded:: 0.8158 ``TRAP_BAD_REQUEST_ERRORS``, ``TRAP_HTTP_EXCEPTIONS``,145- ``APPLICATION_ROOT``159+ ``APPLICATION_ROOT``, ``SESSION_COOKIE_DOMAIN``,160+ ``SESSION_COOKIE_PATH``, ``SESSION_COOKIE_HTTPONLY``,161+ ``SESSION_COOKIE_SECURE``162 163 Configuring from Files164 ----------------------165 214 unmodified linesmodified:flask/testsuite/basic.py+22 added
205 unmodified lines206 return 'Hello World'207 rv = app.test_client().get('/', 'http://example.com:8080/')208 self.assert_('path=/bar' in rv.headers['set-cookie'].lower())209 210+ def test_session_using_session_settings(self):211+ app = flask.Flask(__name__)212+ app.config.update(213+ SECRET_KEY='foo',214+ SERVER_NAME='www.example.com:8080',215+ APPLICATION_ROOT='/test',216+ SESSION_COOKIE_DOMAIN='.example.com',217+ SESSION_COOKIE_HTTPONLY=False,218+ SESSION_COOKIE_SECURE=True,219+ SESSION_COOKIE_PATH='/'220+ )221+ @app.route('/')222+ def index():223+ flask.session['testing'] = 42224+ return 'Hello World'225+ rv = app.test_client().get('/', 'http://www.example.com:8080/test/')226+ cookie = rv.headers['set-cookie'].lower()227+ self.assert_('domain=.example.com' in cookie)228+ self.assert_('path=/;' in cookie)229+ self.assert_('secure' in cookie)230+ self.assert_('httponly' not in cookie)231+232 def test_missing_session(self):233 app = flask.Flask(__name__)234 def expect_exception(f, *args, **kwargs):235 try:790 unmodified linesmodified:flask/app.py+5 added-1 removed
229 unmodified lines230 'TESTING': False,231 'PROPAGATE_EXCEPTIONS': None,232 'PRESERVE_CONTEXT_ON_EXCEPTION': None,233 'SECRET_KEY': None,234- 'SESSION_COOKIE_NAME': 'session',234 'PERMANENT_SESSION_LIFETIME': timedelta(days=31),235 'USE_X_SENDFILE': False,236 'LOGGER_NAME': None,237 'SERVER_NAME': None,238 'APPLICATION_ROOT': None,239+ 'SESSION_COOKIE_NAME': 'session',240+ 'SESSION_COOKIE_DOMAIN': None,241+ 'SESSION_COOKIE_PATH': None,242+ 'SESSION_COOKIE_HTTPONLY': True,243+ 'SESSION_COOKIE_SECURE': False,244 'MAX_CONTENT_LENGTH': None,245 'TRAP_BAD_REQUEST_ERRORS': False,246 'TRAP_HTTP_EXCEPTIONS': False247 })1300 unmodified linesmodified:flask/sessions.py+24 added-4 removed
121 unmodified lines122 def get_cookie_domain(self, app):123 """Helpful helper method that returns the cookie domain that should124 be used for the session cookie if session cookies are used.125 """126+ if app.config['SESSION_COOKIE_DOMAIN'] is not None:127+ return app.config['SESSION_COOKIE_DOMAIN']128 if app.config['SERVER_NAME'] is not None:129 # chop of the port which is usually not supported by browsers130 return '.' + app.config['SERVER_NAME'].rsplit(':', 1)[0]131 132 def get_cookie_path(self, app):133 """Returns the path for which the cookie should be valid. The132- default implementation uses the value from the ``APPLICATION_ROOT``133- configuration variable or uses ``/`` if it's `None`.134+ default implementation uses the value from the SESSION_COOKIE_PATH``135+ config var if it's set, and falls back to ``APPLICATION_ROOT`` or136+ uses ``/`` if it's `None`.137 """135- return app.config['APPLICATION_ROOT'] or '/'138+ return app.config['SESSION_COOKIE_PATH'] or \139+ app.config['APPLICATION_ROOT'] or '/'140 141+ def get_cookie_httponly(self, app):142+ """Returns True if the session cookie should be httponly. This143+ currently just returns the value of the ``SESSION_COOKIE_HTTPONLY``144+ config var.145+ """146+ return app.config['SESSION_COOKIE_HTTPONLY']147+148+ def get_cookie_secure(self, app):149+ """Returns True if the cookie should be secure. This currently150+ just returns the value of the ``SESSION_COOKIE_SECURE`` setting.151+ """152+ return app.config['SESSION_COOKIE_SECURE']153+154 def get_expiration_time(self, app, session):155 """A helper method that returns an expiration date for the session156 or `None` if the session is linked to the browser session. The157 default implementation returns now + the permanent session35 unmodified lines193 def save_session(self, app, session, response):194 expires = self.get_expiration_time(app, session)195 domain = self.get_cookie_domain(app)196 path = self.get_cookie_path(app)197+ httponly = self.get_cookie_httponly(app)198+ secure = self.get_cookie_secure(app)199 if session.modified and not session:200 response.delete_cookie(app.session_cookie_name, path=path,201 domain=domain)202 else:203 session.save_cookie(response, app.session_cookie_name, path=path,185- expires=expires, httponly=True, domain=domain)204+ expires=expires, httponly=httponly,205+ secure=secure, domain=domain)modified:CHANGES+1 added
40 unmodified lines41 - Added :attr:`flask.views.View.decorators` to support simpler decorating of42 pluggable (class based) views.43 - Fixed an issue where the test client if used with the with statement did not44 trigger the execution of the teardown handlers.45+- Added finer control over the session cookie parameters.46 47 Version 0.7.348 -------------49 238 unmodified lines