This repository was archived by the owner on Nov 26, 2026. It is now read-only.
Repository navigation
fix(security): use random secret key and salt when none is configured - #28
Open
PttCodingMan wants to merge 1 commit into
Open
PttCodingMan wants to merge 1 commit into
PttCodingMan wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
這個 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 部署的服務,其 FlaskSECRET_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_fileare not configured,util_flask._init_config()falls back to the hard-coded'default_flask_secret_key'/'default_flask_secret_salt'. Neitherproduction.docker.ininorproduction.ini.templatesets them, so a service deployed with the documented docker-compose setup runs with a publicly known FlaskSECRET_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, setflask_secret_file/flask_security_salt_fileas before.驗證 / Verification
環境:Python 3.10 venv,依
setup.py安裝依賴(另外 pinmongoengine==0.20.0、pymongo<4),測試使用 mongomock。nose1.3.7 在 Python 3.10 無法執行,所以改用pytest<8並以暫時的conftest.py(不 commit)呼叫tests.setup()。Environment: Python 3.10 venv with the
setup.pydependencies (plusmongoengine==0.20.0,pymongo<4); tests use mongomock.nose1.3.7 does not run on Python 3.10, so I usedpytest<8with a temporary, uncommittedconftest.pythat callstests.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.inisets neither file); before the fix it fails withAssertionError: 'default_flask_secret_key' == 'default_flask_secret_key'.After the fix,
python -m pytest -q tests:50 passed.The
flask_secret_filecode path is unchanged and not separately tested.相關的 issue / Related Issues
無 / None