Skip to content
Myr

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

3 files+30 added−4 removed

modified:src/flask/sessions.py+6 added-4 removed
381 unmodified lines
382 secure = self.get_cookie_secure(app)383 samesite = self.get_cookie_samesite(app)384 httponly = self.get_cookie_httponly(app)385386 # Add a "Vary: Cookie" header if the session was accessed at all.387 if session.accessed:388 response.vary.add("Cookie")389390 # 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 lines
398 secure=secure,399 samesite=samesite,400 httponly=httponly,401 )402 response.vary.add("Cookie")403404 return405401 # Add a "Vary: Cookie" header if the session was accessed at all.402 if session.accessed:403 response.vary.add("Cookie")404406 if not self.should_set_cookie(app, session):407 return408409 expires = self.get_expiration_time(app, session)
7 unmodified lines
417 path=path,418 secure=secure,419 samesite=samesite,420 )421 response.vary.add("Cookie")
modified:tests/test_basic.py+23 added
558 unmodified lines
559 @app.route("/setdefault")560 def setdefault():561 return flask.session.setdefault("test", "default")562563 @app.route("/clear")564 def clear():565 flask.session.clear()566 return ""567568 @app.route("/vary-cookie-header-set")569 def vary_cookie_header_set():570 response = flask.Response()571 response.vary.add("Cookie")
24 unmodified lines
596 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)604605606def test_session_refresh_vary(app, client):607 @app.get("/login")608 def login():609 flask.session["user_id"] = 1610 flask.session.permanent = True611 return ""612613 @app.get("/ignored")614 def ignored():615 return ""616617 rv = client.get("/login")618 assert rv.headers["Vary"] == "Cookie"619 rv = client.get("/ignored")620 assert rv.headers["Vary"] == "Cookie"621622623def test_flashes(app, req_ctx):624 assert not flask.session.modified625 flask.flash("Zap")626 flask.session.modified = False
1359 unmodified lines
modified:CHANGES.rst+1 added
2 unmodified lines
34Unreleased56- Update for compatibility with Werkzeug 2.3.3.7- Set ``Vary: Cookie`` header when the session is accessed, modified, or refreshed.8910Version 2.2.411-------------
1423 unmodified lines