fix: generate Flask SECRET_KEY with a CSPRNG - #963
Open
rodriguescarson wants to merge 1 commit into
Open
Conversation
build_secret_key() built the Flask SECRET_KEY from numpy's Mersenne-Twister PRNG (np.random.choice) over 10 characters of a 36-symbol alphabet (~2**51.7 entropy). numpy's PRNG is not cryptographically secure and the key space is small enough to brute-force offline, which weakens the HMAC that signs Flask session cookies (session forgery when auth is enabled). Use os.urandom (the OS CSPRNG) for 256 bits of entropy, hex-encoded. This stays Python 2/3 compatible (the stdlib secrets module is 3.6+, but dtale still supports 2.7). Drops the now-unused numpy and string imports from app.py.
rodriguescarson
force-pushed
the
fix/secure-flask-secret-key
branch
from
July 2, 2026 16:49
fcc3ddf to
e3322ee
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #963 +/- ##
==========================================
- Coverage 94.34% 94.34% -0.01%
==========================================
Files 59 59
Lines 10569 10568 -1
==========================================
- Hits 9971 9970 -1
Misses 598 598
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #960
Problem
build_secret_key()generated the FlaskSECRET_KEYlike this:Two issues make this weak for a signing key:
np.randomis a Mersenne-Twister generator, not a CSPRNG. Its output is predictable given enough observed values.36**10 ≈ 2**51.7, small enough to brute-force offline.Flask uses
SECRET_KEYto HMAC-sign session cookies, so a weak key enables session forgery / authentication bypass when auth is enabled ([auth] active = true).Fix
Use the standard library
secretsmodule (OS CSPRNG) with 256 bits of entropy:Also drops the
numpyandstringimports fromapp.py, which this was their only remaining use (keeps the file flake8-clean under the repo'sselect = E, W, F).Verifiable by inspection; no behavior change beyond a stronger key.
🤖 Generated with Claude Code