Skip to content
This repository was archived by the owner on Nov 26, 2026. It is now read-only.

fix(security): use random secret key and salt when none is configured - #28

Open
PttCodingMan wants to merge 1 commit into
mainfrom
fix/random-default-secrets
Open

PttCodingMan wants to merge 1 commit into
mainfrom
fix/random-default-secrets

Conversation

@PttCodingMan

Copy link
Copy Markdown
Member

這個 PR 的目的 / Purpose of this PR

util_flask._init_config() 在沒有設定 flask_secret_file / flask_security_salt_file 時,會使用寫死在原始碼中的 'default_flask_secret_key' 與 'default_flask_secret_salt'。production.docker.ini 與 production.ini.template 預設都沒有設定這兩項,所以照 README 用 docker-compose 部署的服務,其 Flask SECRET_KEY 是公開已知的字串,攻擊者可以偽造 Flask session cookie 與 flask-security 的 token。

本 PR 改成:沒有設定檔案時,在啟動時用 secrets.token_hex(32) 產生隨機值,並記錄 warning;有設定檔案時行為不變。代價是未設定時,重啟後既有 session 會失效;要跨重啟或多個 process 共用,請照原本的方式設定 flask_secret_file / flask_security_salt_file。

When flask_secret_file / flask_security_salt_file are not configured, util_flask._init_config() falls back to the hard-coded 'default_flask_secret_key' / 'default_flask_secret_salt'. Neither production.docker.ini nor production.ini.template sets them, so a service deployed with the documented docker-compose setup runs with a publicly known Flask SECRET_KEY, allowing forged session cookies and flask-security tokens.

This PR generates random values with secrets.token_hex(32) at startup when the files are not configured, and logs a warning. Behaviour is unchanged when the files are configured. The trade-off: without the files, existing sessions are invalidated on restart; to persist them across restarts or share them between processes, set flask_secret_file / flask_security_salt_file as before.

驗證 / Verification

環境:Python 3.10 venv,依 setup.py 安裝依賴(另外 pin mongoengine==0.20.0、pymongo<4),測試使用 mongomock。nose 1.3.7 在 Python 3.10 無法執行,所以改用 pytest<8 並以暫時的 conftest.py(不 commit)呼叫 tests.setup()。

Environment: Python 3.10 venv with the setup.py dependencies (plus mongoengine==0.20.0, pymongo<4); tests use mongomock. nose 1.3.7 does not run on Python 3.10, so I used pytest<8 with a temporary, uncommitted conftest.py that calls tests.setup().

  • 新增 test_default_secrets_are_not_well_known(tests/test.ini 沒有設定這兩個檔案);修正前失敗:AssertionError: 'default_flask_secret_key' == 'default_flask_secret_key'

  • 修正後 python -m pytest -q tests:50 passed

  • 設定 flask_secret_file 的路徑沒有改動,未另外測試。

  • Added test_default_secrets_are_not_well_known (tests/test.ini sets neither file); before the fix it fails with AssertionError: 'default_flask_secret_key' == 'default_flask_secret_key'.

  • After the fix, python -m pytest -q tests: 50 passed.

  • The flask_secret_file code path is unchanged and not separately tested.

相關的 issue / Related Issues

無 / None

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant