set `Vary: Cookie` header consistently for session
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent a6367da · Browse files · 3 files·+30 added−4 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+30 added−4 removed
- tests:+23 added−0 removed
- other:+7 added−4 removed
Provenance
- Commit
8646edc- Tree
9d11b13- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
a6367da
3 files changed+30 added−4 removed
modified:src/flask/sessions.py+6 added-4 removed
381 unmodified lines382 secure = self.get_cookie_secure(app)383 samesite = self.get_cookie_samesite(app)384 httponly = self.get_cookie_httponly(app)385 386+ # Add a "Vary: Cookie" header if the session was accessed at all.387+ if session.accessed:388+ response.vary.add("Cookie")389+390 # If the session is modified to be empty, remove the cookie.391 # If the session is empty, return without setting the cookie.392 if not session:393 if session.modified:4 unmodified lines398 secure=secure,399 samesite=samesite,400 httponly=httponly,401 )402+ response.vary.add("Cookie")403 404 return405 401- # Add a "Vary: Cookie" header if the session was accessed at all.402- if session.accessed:403- response.vary.add("Cookie")404-406 if not self.should_set_cookie(app, session):407 return408 409 expires = self.get_expiration_time(app, session)7 unmodified lines417 path=path,418 secure=secure,419 samesite=samesite,420 )421+ response.vary.add("Cookie")modified:tests/test_basic.py+23 added
558 unmodified lines559 @app.route("/setdefault")560 def setdefault():561 return flask.session.setdefault("test", "default")562 563+ @app.route("/clear")564+ def clear():565+ flask.session.clear()566+ return ""567+568 @app.route("/vary-cookie-header-set")569 def vary_cookie_header_set():570 response = flask.Response()571 response.vary.add("Cookie")24 unmodified lines596 expect("/set")597 expect("/get")598 expect("/getitem")599 expect("/setdefault")600+ expect("/clear")601 expect("/vary-cookie-header-set")602 expect("/vary-header-set", "Accept-Encoding, Accept-Language, Cookie")603 expect("/no-vary-header", None)604 605 606+def test_session_refresh_vary(app, client):607+ @app.get("/login")608+ def login():609+ flask.session["user_id"] = 1610+ flask.session.permanent = True611+ return ""612+613+ @app.get("/ignored")614+ def ignored():615+ return ""616+617+ rv = client.get("/login")618+ assert rv.headers["Vary"] == "Cookie"619+ rv = client.get("/ignored")620+ assert rv.headers["Vary"] == "Cookie"621+622+623 def test_flashes(app, req_ctx):624 assert not flask.session.modified625 flask.flash("Zap")626 flask.session.modified = False1359 unmodified linesmodified:CHANGES.rst+1 added
2 unmodified lines3 4 Unreleased5 6 - Update for compatibility with Werkzeug 2.3.3.7+- Set ``Vary: Cookie`` header when the session is accessed, modified, or refreshed.8 9 10 Version 2.2.411 -------------1423 unmodified lines