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 bc14349, bbaf133 · Browse files · 4 files·+24 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+24 added−11 removed
- tests:+11 added−4 removed
- other:+13 added−7 removed
Provenance
- Commit
e7e5380- Tree
aff4342- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent 1
bc14349- Parent 2
bbaf133
4 files changed+24 added−11 removed
modified:docs/config.rst+6 added-3 removed
125 unmodified lines126 Default: ``None``127 128 .. py:data:: SECRET_KEY_FALLBACKS129 130- A list of old secret keys that can still be used for unsigning, most recent131- first. This allows a project to implement key rotation without invalidating132- active sessions or other recently-signed secrets.130+ A list of old secret keys that can still be used for unsigning. This allows131+ a project to implement key rotation without invalidating active sessions or132+ other recently-signed secrets.133 134 Keys should be removed after an appropriate period of time, as checking each135 additional key adds some overhead.136 137+ Order should not matter, but the default implementation will test the last138+ key in the list first, so it might make sense to order oldest to newest.139+140 Flask's built-in secure cookie session supports this. Extensions that use141 :data:`SECRET_KEY` may not support this yet.142 143 Default: ``None``696 unmodified linesmodified:src/flask/sessions.py+2 added-1 removed
316 unmodified lines317 def get_signing_serializer(self, app: Flask) -> URLSafeTimedSerializer | None:318 if not app.secret_key:319 return None320 321- keys: list[str | bytes] = [app.secret_key]321+ keys: list[str | bytes] = []322 323 if fallbacks := app.config["SECRET_KEY_FALLBACKS"]:324 keys.extend(fallbacks)325 326+ keys.append(app.secret_key) # itsdangerous expects current key at top327 return URLSafeTimedSerializer(328 keys, # type: ignore[arg-type]329 salt=self.salt,330 serializer=self.serializer,69 unmodified linesmodified:tests/test_basic.py+11 added-4 removed
379 unmodified lines380 @app.get("/")381 def get_session() -> dict[str, t.Any]:382 return dict(flask.session)383 384- # Set session with initial secret key384+ # Set session with initial secret key, and two valid expiring keys385+ app.secret_key, app.config["SECRET_KEY_FALLBACKS"] = (386+ "0 key",387+ ["-1 key", "-2 key"],388+ )389 client.post()390 assert client.get().json == {"a": 1}391 # Change secret key, session can't be loaded and appears empty388- app.secret_key = "new test key"392+ app.secret_key = "? key"393 assert client.get().json == {}390- # Add initial secret key as fallback, session can be loaded391- app.config["SECRET_KEY_FALLBACKS"] = ["test key"]394+ # Rotate the valid keys, session can be loaded395+ app.secret_key, app.config["SECRET_KEY_FALLBACKS"] = (396+ "+1 key",397+ ["0 key", "-1 key"],398+ )399 assert client.get().json == {"a": 1}400 401 402 def test_session_expiration(app, client):1542 unmodified linesmodified:CHANGES.rst+5 added-3 removed
7 unmodified lines8 9 Version 3.1.110 -------------11 12-Unreleased12+Released 2025-05-1313 14+- Fix signing key selection order when key rotation is enabled via15+ ``SECRET_KEY_FALLBACKS``. :ghsa:`4grg-w6v8-c28g`14-- Fix type hint for `cli_runner.invoke`. :issue:`5645`16+- Fix type hint for ``cli_runner.invoke``. :issue:`5645`17 - ``flask --help`` loads the app and plugins first to make sure all commands16- are shown. :issue:5673`18+ are shown. :issue:`5673`19 - Mark sans-io base class as being able to handle views that return20 ``AsyncIterable``. This is not accurate for Flask, but makes typing easier21 for Quart. :pr:`5659`22 1599 unmodified lines