Skip to content
Myr

get_cookie_name in SessionInterface for easier overriding in SecureCookieSessionInterface

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

authored by Doron Horwitz · committed by David Lord · parent 4bceecc · Browse files · 3 files+73 added−5 removed
This commit

State

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

Diff stats

3 files+73 added−5 removed

  • tests:+53 added−0 removed
  • other:+20 added−5 removed

3 files+73 added−5 removed

modified:src/flask/sessions.py+11 added-5 removed
171 unmodified lines
172 by default.173 """174 return isinstance(obj, self.null_session_class)175176 def get_cookie_name(self, app):177 """Returns the name of the session cookie.178179 Uses ``app.session_cookie_name`` which is set to ``SESSION_COOKIE_NAME``180 """181 return app.session_cookie_name182183 def get_cookie_domain(self, app):184 """Returns the domain that should be set for the session cookie.185186 Uses ``SESSION_COOKIE_DOMAIN`` if it is configured, otherwise
159 unmodified lines
346 def open_session(self, app, request):347 s = self.get_signing_serializer(app)348 if s is None:349 return None343 val = request.cookies.get(app.session_cookie_name)350 val = request.cookies.get(self.get_cookie_name(app))351 if not val:352 return self.session_class()353 max_age = total_seconds(app.permanent_session_lifetime)354 try:
2 unmodified lines
357 except BadSignature:358 return self.session_class()359360 def save_session(self, app, session, response):361 name = self.get_cookie_name(app)362 domain = self.get_cookie_domain(app)363 path = self.get_cookie_path(app)364365 # If the session is modified to be empty, remove the cookie.366 # If the session is empty, return without setting the cookie.367 if not session:368 if session.modified:361 response.delete_cookie(362 app.session_cookie_name, domain=domain, path=path363 )369 response.delete_cookie(name, domain=domain, path=path)370371 return372373 # Add a "Vary: Cookie" header if the session was accessed at all.
8 unmodified lines
382 samesite = self.get_cookie_samesite(app)383 expires = self.get_expiration_time(app, session)384 val = self.get_signing_serializer(app).dumps(dict(session))385 response.set_cookie(380 app.session_cookie_name,386 name,387 val,388 expires=expires,389 httponly=httponly,390 domain=domain,
4 unmodified lines
modified:tests/test_reqctx.py+53 added
9 unmodified lines
10"""11import pytest1213import flask14from flask.sessions import SecureCookieSessionInterface15from flask.sessions import SessionInterface1617try:18 from greenlet import greenlet
210 unmodified lines
229 assert not flask.request230 assert not flask.current_app231232233def test_session_dynamic_cookie_name():234235 # This session interface will use a cookie with a different name if the236 # requested url ends with the string "dynamic_cookie"237 class PathAwareSessionInterface(SecureCookieSessionInterface):238 def get_cookie_name(self, app):239 if flask.request.url.endswith("dynamic_cookie"):240 return "dynamic_cookie_name"241 else:242 return super(PathAwareSessionInterface, self).get_cookie_name(app)243244 class CustomFlask(flask.Flask):245 session_interface = PathAwareSessionInterface()246247 app = CustomFlask(__name__)248 app.secret_key = "secret_key"249250 @app.route("/set", methods=["POST"])251 def set():252 flask.session["value"] = flask.request.form["value"]253 return "value set"254255 @app.route("/get")256 def get():257 v = flask.session.get("value", "None")258 return v259260 @app.route("/set_dynamic_cookie", methods=["POST"])261 def set_dynamic_cookie():262 flask.session["value"] = flask.request.form["value"]263 return "value set"264265 @app.route("/get_dynamic_cookie")266 def get_dynamic_cookie():267 v = flask.session.get("value", "None")268 return v269270 test_client = app.test_client()271272 # first set the cookie in both /set urls but each with a different value273 assert test_client.post("/set", data={"value": "42"}).data == b"value set"274 assert (275 test_client.post("/set_dynamic_cookie", data={"value": "616"}).data276 == b"value set"277 )278279 # now check that the relevant values come back - meaning that different280 # cookies are being used for the urls that end with "dynamic cookie"281 assert test_client.get("/get").data == b"42"282 assert test_client.get("/get_dynamic_cookie").data == b"616"283284285def test_bad_environ_raises_bad_request():286 app = flask.Flask(__name__)287288 # We cannot use app.test_client() for the Unicode-rich Host header,
50 unmodified lines
modified:CHANGES.rst+9 added
1.. currentmodule:: flask23Version 2.0.04-------------56Unreleased78- Add :meth:`sessions.SessionInterface.get_cookie_name` to allow9 setting the session cookie name dynamically. :pr:`3369`101112Version 1.1.213-------------1415Unreleased
1002 unmodified lines