Merge commit from fork
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by GitHub · parents 941efd4, cbb6c36 · Browse files · 4 files·+21 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Sessions: fix signing key selection when key rotation is enabled
Diff stats
4 files+21 added−8 removed
- tests:+11 added−4 removed
- other:+10 added−4 removed
Provenance
- Commit
73d6504- Tree
1a32ec7- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
941efd4- Parent 2
cbb6c36
4 files changed+21 added−8 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+2 added
1 unmodified line2 -------------3 4 Unreleased5 6+- Fix signing key selection order when key rotation is enabled via7+ ``SECRET_KEY_FALLBACKS``. :ghsa:`4grg-w6v8-c28g`8 - Fix type hint for `cli_runner.invoke`. :issue:`5645`9 - ``flask --help`` loads the app and plugins first to make sure all commands10 are shown. :issue:5673`11 - Mark sans-io base class as being able to handle views that return1602 unmodified lines