Skip to content
Myr

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

6 files+64 added−47 removed

modified:src/flask/app.py+2 added-2 removed
1409 unmodified lines
1410 if name in self.after_request_funcs:1411 for func in reversed(self.after_request_funcs[name]):1412 response = self.ensure_sync(func)(response)14131414 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)14161417 return response14181419 def do_teardown_request(
187 unmodified lines
modified:src/flask/ctx.py+2 added-1 removed
379 unmodified lines
380 @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 lines
392393 if self._session is None:394 self._session = si.make_null_session(self.app)395396 self._session.accessed = True397 return self._session398399 def match_request(self) -> None:400 """Apply routing to the current request, storing either the matched
117 unmodified lines
modified:src/flask/sessions.py+10 added-24 removed
41 unmodified lines
42 #: this when that happens. The mixin default is hard coded to43 #: ``True``.44 modified = True4546 #: 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``.5152 .. versionchanged:: 3.1.353 This is tracked by the request context.54 """555657class SecureCookieSession(CallbackDict[str, t.Any], SessionMixin):58 """Base class for sessions based on signed cookies.5960 This session backend will set the :attr:`modified` and
8 unmodified lines
69 #: ``True`` manually when modifying that data. The session cookie70 #: will only be written to the response if this is ``True``.71 modified = False7268 #: 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 = False7373 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 = True7980 super().__init__(initial, on_update)8184 def __getitem__(self, key: str) -> t.Any:85 self.accessed = True86 return super().__getitem__(key)8288 def get(self, key: str, default: t.Any = None) -> t.Any:89 self.accessed = True90 return super().get(key, default)9192 def setdefault(self, key: str, default: t.Any = None) -> t.Any:93 self.accessed = True94 return super().setdefault(key, default)959683class 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 lines
modified:src/flask/templating.py+4 added-3 removed
17 unmodified lines
18 from .sansio.scaffold import Scaffold192021def _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}2728 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.3233 return rv3435
177 unmodified lines
modified:tests/test_basic.py+37 added-17 removed
18 unmodified lines
19from werkzeug.routing import BuildError20from werkzeug.routing import RequestRedirect2122import flask23from flask.globals import app_ctx24from flask.testing import FlaskClient2526require_cpython_gc = pytest.mark.skipif(27 python_implementation() != "CPython",
202 unmodified lines
230 assert client.get("/foo/").data == b"index"231 assert client.get("/foo/bar").data == b"bar"232233233def test_session(app, client):234 @app.route("/set", methods=["POST"])235 def set():236 assert not flask.session.accessed237 assert not flask.session.modified234def 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"239243 @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")243252 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 ""247248 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.modified253254 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.modified260261 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.modified267268 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.modified274275276def test_session_path(app, client):277 app.config.update(APPLICATION_ROOT="/foo")278279 @app.route("/")
1689 unmodified lines
modified:CHANGES.rst+9 added
25 unmodified lines
26- ``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`282930Version 3.1.331-------------3233Released 2026-02-183435- 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`373839Version 3.1.240-------------4142Released 2025-08-19
1620 unmodified lines