Skip to content
Myr

secret key rotation: fix key list ordering

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The `itsdangerous` serializer interface[1] expects keys to be provided with the oldest key at index zero and the active signing key at the end of the list. We document[2] that `SECRET_KEY_FALLBACKS` should be configured with the most recent first (at index zero), so to achieve the expected behaviour, those should be inserted in reverse-order at the head of the list. [1] - https://itsdangerous.palletsprojects.com/en/stable/serializer/#itsdangerous.serializer.Serializer [2] - https://flask.palletsprojects.com/en/stable/config/#SECRET_KEY_FALLBACKS

authored by James Addison · committed by David Lord · parent 941efd4 · Browse files · 3 files+15 added−5 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+15 added−5 removed

  • tests:+11 added−4 removed
  • other:+4 added−1 removed

3 files+15 added−5 removed

modified:src/flask/sessions.py+2 added-1 removed
316 unmodified lines
317 def get_signing_serializer(self, app: Flask) -> URLSafeTimedSerializer | None:318 if not app.secret_key:319 return None320321 keys: list[str | bytes] = [app.secret_key]321 keys: list[str | bytes] = []322323 if fallbacks := app.config["SECRET_KEY_FALLBACKS"]:324 keys.extend(fallbacks)325326 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 lines
modified:tests/test_basic.py+11 added-4 removed
379 unmodified lines
380 @app.get("/")381 def get_session() -> dict[str, t.Any]:382 return dict(flask.session)383384 # 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}400401402def test_session_expiration(app, client):
1542 unmodified lines
modified:CHANGES.rst+2 added
1 unmodified line
2-------------34Unreleased56- 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 return
1602 unmodified lines