Skip to content

Commit ede2bd1

Browse files
committed
Name SESSION_KEY in the error when its value is not a Fernet key
A malformed value used to fail inside EncryptedCookieStorage with a message that did not say which variable was wrong. Also tightens the readme wording and drops an import the tests no longer need.
1 parent 687712c commit ede2bd1

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

‎blurb_it/__main__.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,14 @@ def get_session_key() -> str:
319319
"""
320320
session_key = os.environ.get("SESSION_KEY")
321321
if session_key:
322+
try:
323+
fernet.Fernet(session_key)
324+
except ValueError as exc:
325+
raise ValueError(
326+
"SESSION_KEY is not a valid Fernet key. Generate one with "
327+
"`python -c \"from cryptography.fernet import Fernet; "
328+
"print(Fernet.generate_key().decode())\"`."
329+
) from exc
322330
return session_key
323331
logging.warning(
324332
"SESSION_KEY is not set; using a random session key, "

‎readme.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Configuration variables, set in the cabotage UI:
6565
it once with ``python -c "from cryptography.fernet import Fernet;
6666
print(Fernet.generate_key().decode())"`` and keep it the same across
6767
deploys; changing it signs everyone out. Without it the app generates a
68-
random key at startup, so every restart signs everyone out.
68+
random key at startup, with the same effect on every restart.
6969
- ``SENTRY_DSN``: where errors are reported.
7070

7171

‎tests/test_main.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import asyncio
44
import base64
55
import logging
6-
import os
76
import pathlib
87

98
import aiohttp
@@ -618,11 +617,12 @@ async def get_installation(gh, jwt, username):
618617

619618

620619
def test_get_session_key_from_env(monkeypatch, caplog) -> None:
621-
monkeypatch.setenv("SESSION_KEY", fernet.Fernet.generate_key().decode())
620+
expected = fernet.Fernet.generate_key().decode()
621+
monkeypatch.setenv("SESSION_KEY", expected)
622622

623623
session_key = __main__.get_session_key()
624624

625-
assert session_key == os.environ["SESSION_KEY"]
625+
assert session_key == expected
626626
assert "SESSION_KEY is not set" not in caplog.text
627627
# The key must be usable as-is by the cookie storage.
628628
EncryptedCookieStorage(session_key)
@@ -637,3 +637,10 @@ def test_get_session_key_random_when_unset(monkeypatch, caplog) -> None:
637637
assert session_key != __main__.get_session_key()
638638
assert "SESSION_KEY is not set" in caplog.text
639639
EncryptedCookieStorage(session_key)
640+
641+
642+
def test_get_session_key_rejects_invalid_value(monkeypatch) -> None:
643+
monkeypatch.setenv("SESSION_KEY", "not-a-fernet-key")
644+
645+
with pytest.raises(ValueError, match="SESSION_KEY is not a valid Fernet key"):
646+
__main__.get_session_key()

0 commit comments

Comments
 (0)