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
Provenance
- Commit
0c0b31a- Tree
4996815- Author
Doron Horwitz <doron@milktek.com>- Committer
David Lord <davidism@gmail.com>- Parent
4bceecc
3 files changed+73 added−5 removed
modified:src/flask/sessions.py+11 added-5 removed
171 unmodified lines172 by default.173 """174 return isinstance(obj, self.null_session_class)175 176+ def get_cookie_name(self, app):177+ """Returns the name of the session cookie.178+179+ Uses ``app.session_cookie_name`` which is set to ``SESSION_COOKIE_NAME``180+ """181+ return app.session_cookie_name182+183 def get_cookie_domain(self, app):184 """Returns the domain that should be set for the session cookie.185 186 Uses ``SESSION_COOKIE_DOMAIN`` if it is configured, otherwise159 unmodified lines346 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 lines357 except BadSignature:358 return self.session_class()359 360 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)364 365 # 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)370 371 return372 373 # Add a "Vary: Cookie" header if the session was accessed at all.8 unmodified lines382 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 linesmodified:tests/test_reqctx.py+53 added
9 unmodified lines10 """11 import pytest12 13 import flask14+from flask.sessions import SecureCookieSessionInterface15 from flask.sessions import SessionInterface16 17 try:18 from greenlet import greenlet210 unmodified lines229 assert not flask.request230 assert not flask.current_app231 232 233+def test_session_dynamic_cookie_name():234+235+ # 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)243+244+ class CustomFlask(flask.Flask):245+ session_interface = PathAwareSessionInterface()246+247+ app = CustomFlask(__name__)248+ app.secret_key = "secret_key"249+250+ @app.route("/set", methods=["POST"])251+ def set():252+ flask.session["value"] = flask.request.form["value"]253+ return "value set"254+255+ @app.route("/get")256+ def get():257+ v = flask.session.get("value", "None")258+ return v259+260+ @app.route("/set_dynamic_cookie", methods=["POST"])261+ def set_dynamic_cookie():262+ flask.session["value"] = flask.request.form["value"]263+ return "value set"264+265+ @app.route("/get_dynamic_cookie")266+ def get_dynamic_cookie():267+ v = flask.session.get("value", "None")268+ return v269+270+ test_client = app.test_client()271+272+ # 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+ )278+279+ # 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"283+284+285 def test_bad_environ_raises_bad_request():286 app = flask.Flask(__name__)287 288 # We cannot use app.test_client() for the Unicode-rich Host header,50 unmodified linesmodified:CHANGES.rst+9 added
1 .. currentmodule:: flask2 3+Version 2.0.04+-------------5+6+Unreleased7+8+- Add :meth:`sessions.SessionInterface.get_cookie_name` to allow9+ setting the session cookie name dynamically. :pr:`3369`10+11+12 Version 1.1.213 -------------14 15 Unreleased1002 unmodified lines