Merge branch 'stable'
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 · parents d98eb69, f00ad42 · Browse files · 6 files·+64 added−47 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+64 added−47 removed
- tests:+37 added−17 removed
- other:+27 added−30 removed
Provenance
- Commit
daca74d- Tree
397ef24- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent 1
d98eb69- Parent 2
f00ad42
6 files changed+64 added−47 removed
modified:src/flask/app.py+2 added-2 removed
1409 unmodified lines1410 if name in self.after_request_funcs:1411 for func in reversed(self.after_request_funcs[name]):1412 response = self.ensure_sync(func)(response)1413 1414- if not self.session_interface.is_null_session(ctx.session):1415- self.session_interface.save_session(self, ctx.session, response)1414+ if not self.session_interface.is_null_session(ctx._session):1415+ self.session_interface.save_session(self, ctx._session, response)1416 1417 return response1418 1419 def do_teardown_request(187 unmodified linesmodified:src/flask/ctx.py+2 added-1 removed
379 unmodified lines380 @property381 def session(self) -> SessionMixin:382 """The session object associated with this context. Accessed through383 :data:`.session`. Only available in request contexts, otherwise raises384- :exc:`RuntimeError`.384+ :exc:`RuntimeError`. Accessing this sets :attr:`.SessionMixin.accessed`.385 """386 if self._request is None:387 raise RuntimeError("There is no request in this context.")388 3 unmodified lines392 393 if self._session is None:394 self._session = si.make_null_session(self.app)395 396+ self._session.accessed = True397 return self._session398 399 def match_request(self) -> None:400 """Apply routing to the current request, storing either the matched117 unmodified linesmodified:src/flask/sessions.py+10 added-24 removed
41 unmodified lines42 #: this when that happens. The mixin default is hard coded to43 #: ``True``.44 modified = True45 46- #: Some implementations can detect when session data is read or47- #: written and set this when that happens. The mixin default is hard48- #: coded to ``True``.49- accessed = True46+ accessed = False47+ """Indicates if the session was accessed, even if it was not modified. This48+ is set when the session object is accessed through the request context,49+ including the global :data:`.session` proxy. A ``Vary: cookie`` header will50+ be added if this is ``True``.51 52+ .. versionchanged:: 3.1.353+ This is tracked by the request context.54+ """55 56+57 class SecureCookieSession(CallbackDict[str, t.Any], SessionMixin):58 """Base class for sessions based on signed cookies.59 60 This session backend will set the :attr:`modified` and8 unmodified lines69 #: ``True`` manually when modifying that data. The session cookie70 #: will only be written to the response if this is ``True``.71 modified = False72 68- #: When data is read or written, this is set to ``True``. Used by69- # :class:`.SecureCookieSessionInterface` to add a ``Vary: Cookie``70- #: header, which allows caching proxies to cache different pages for71- #: different users.72- accessed = False73-73 def __init__(74 self,76- initial: c.Mapping[str, t.Any] | c.Iterable[tuple[str, t.Any]] | None = None,75+ initial: c.Mapping[str, t.Any] | None = None,76 ) -> None:77 def on_update(self: te.Self) -> None:78 self.modified = True80- self.accessed = True79 80 super().__init__(initial, on_update)81 84- def __getitem__(self, key: str) -> t.Any:85- self.accessed = True86- return super().__getitem__(key)82 88- def get(self, key: str, default: t.Any = None) -> t.Any:89- self.accessed = True90- return super().get(key, default)91-92- def setdefault(self, key: str, default: t.Any = None) -> t.Any:93- self.accessed = True94- return super().setdefault(key, default)95-96-83 class NullSession(SecureCookieSession):84 """Class used to generate nicer error messages if sessions are not85 available. Will still allow read-only access to the empty session86 but fail on setting.299 unmodified linesmodified:src/flask/templating.py+4 added-3 removed
17 unmodified lines18 from .sansio.scaffold import Scaffold19 20 21 def _default_template_ctx_processor() -> dict[str, t.Any]:22- """Default template context processor. Injects `request`,23- `session` and `g`.22+ """Default template context processor. Replaces the ``request`` and ``g``23+ proxies with their concrete objects for faster access.24 """25 ctx = app_ctx._get_current_object()26 rv: dict[str, t.Any] = {"g": ctx.g}27 28 if ctx.has_request:29 rv["request"] = ctx.request30- rv["session"] = ctx.session30+ # The session proxy cannot be replaced, accessing it gets31+ # RequestContext.session, which sets session.accessed.32 33 return rv34 35 177 unmodified linesmodified:tests/test_basic.py+37 added-17 removed
18 unmodified lines19 from werkzeug.routing import BuildError20 from werkzeug.routing import RequestRedirect21 22 import flask23+from flask.globals import app_ctx24 from flask.testing import FlaskClient25 26 require_cpython_gc = pytest.mark.skipif(27 python_implementation() != "CPython",202 unmodified lines230 assert client.get("/foo/").data == b"index"231 assert client.get("/foo/bar").data == b"bar"232 233 233-def test_session(app, client):234- @app.route("/set", methods=["POST"])235- def set():236- assert not flask.session.accessed237- assert not flask.session.modified234+def test_session_accessed(app: flask.Flask, client: FlaskClient) -> None:235+ @app.post("/")236+ def do_set():237 flask.session["value"] = flask.request.form["value"]239- assert flask.session.accessed240- assert flask.session.modified238 return "value set"239 243- @app.route("/get")244- def get():245- assert not flask.session.accessed246- assert not flask.session.modified247- v = flask.session.get("value", "None")248- assert flask.session.accessed249- assert not flask.session.modified250- return v240+ @app.get("/")241+ def do_get():242+ return flask.session.get("value", "None")243 252- assert client.post("/set", data={"value": "42"}).data == b"value set"253- assert client.get("/get").data == b"42"244+ @app.get("/nothing")245+ def do_nothing() -> str:246+ return ""247 248+ with client:249+ rv = client.get("/nothing")250+ assert "cookie" not in rv.vary251+ assert not app_ctx._session.accessed252+ assert not app_ctx._session.modified253 254+ with client:255+ rv = client.post(data={"value": "42"})256+ assert rv.text == "value set"257+ assert "cookie" in rv.vary258+ assert app_ctx._session.accessed259+ assert app_ctx._session.modified260+261+ with client:262+ rv = client.get()263+ assert rv.text == "42"264+ assert "cookie" in rv.vary265+ assert app_ctx._session.accessed266+ assert not app_ctx._session.modified267+268+ with client:269+ rv = client.get("/nothing")270+ assert rv.text == ""271+ assert "cookie" not in rv.vary272+ assert not app_ctx._session.accessed273+ assert not app_ctx._session.modified274+275+276 def test_session_path(app, client):277 app.config.update(APPLICATION_ROOT="/foo")278 279 @app.route("/")1689 unmodified linesmodified:CHANGES.rst+9 added
25 unmodified lines26 - ``provide_automatic_options=True`` can be used to enable it for a view when27 it's disabled in config. Previously, only disabling worked. :issue:`5916`28 29 30+Version 3.1.331+-------------32+33+Released 2026-02-1834+35+- The session is marked as accessed for operations that only access the keys36+ but not the values, such as ``in`` and ``len``. :ghsa:`68rp-wp8r-4726`37+38+39 Version 3.1.240 -------------41 42 Released 2025-08-191620 unmodified lines