use secrets instead of os.urandom
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kevin Kirsche · committed by David Lord · parent 8ddf80c · Browse files · 3 files·+9 added−9 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+9 added−9 removed
- other:+9 added−9 removed
Provenance
- Commit
58a08a1- Tree
4de03dd- Author
Kevin Kirsche <kevin.kirsche@one.verizon.com>- Committer
David Lord <davidism@gmail.com>- Parent
8ddf80c
3 files changed+9 added−9 removed
modified:docs/tutorial/deploy.rst+3 added-3 removed
86 unmodified lines87 You can use the following command to output a random secret key:88 89 .. code-block:: none90 91- $ python -c 'import os; print(os.urandom(16))'91+ $ python -c 'import secrets; print(secrets.token_hex())'92 93- b'_5#y2L"F4Q8z\n\xec]/'93+ '192b9bdd22ab9ed4d12e236c78afcb9a393ec15f71bbf5dc987d54727823bcbf'94 95 Create the ``config.py`` file in the instance folder, which the factory96 will read from if it exists. Copy the generated value into it.97 98 .. code-block:: python99 :caption: ``venv/var/flaskr-instance/config.py``100 101- SECRET_KEY = b'_5#y2L"F4Q8z\n\xec]/'101+ SECRET_KEY = '192b9bdd22ab9ed4d12e236c78afcb9a393ec15f71bbf5dc987d54727823bcbf'102 103 You can also set any other necessary configuration here, although104 ``SECRET_KEY`` is the only one needed for Flaskr.105 34 unmodified linesmodified:docs/config.rst+4 added-4 removed
36 unmodified lines37 method::38 39 app.config.update(40 TESTING=True,41- SECRET_KEY=b'_5#y2L"F4Q8z\n\xec]/'41+ SECRET_KEY='192b9bdd22ab9ed4d12e236c78afcb9a393ec15f71bbf5dc987d54727823bcbf'42 )43 44 45 Environment and Debug Features133 unmodified lines179 and can be used for any other security related needs by extensions or your180 application. It should be a long random ``bytes`` or ``str``. For181 example, copy the output of this to your config::182 183- $ python -c 'import os; print(os.urandom(16))'184- b'_5#y2L"F4Q8z\n\xec]/'183+ $ python -c 'import secrets; print(secrets.token_hex()))'184+ '192b9bdd22ab9ed4d12e236c78afcb9a393ec15f71bbf5dc987d54727823bcbf'185 186 **Do not reveal the secret key when posting questions or committing code.**187 188 Default: ``None``278 unmodified lines467 468 Here is an example of a configuration file::469 470 # Example configuration471- SECRET_KEY = b'_5#y2L"F4Q8z\n\xec]/'471+ SECRET_KEY = '192b9bdd22ab9ed4d12e236c78afcb9a393ec15f71bbf5dc987d54727823bcbf'472 473 Make sure to load the configuration very early on, so that extensions have474 the ability to access the configuration when starting up. There are other475 methods on the config object as well to load from individual files. For a293 unmodified linesmodified:docs/quickstart.rst+2 added-2 removed
845 unmodified lines846 ways to generate pretty random data based on a cryptographic random847 generator. Use the following command to quickly generate a value for848 :attr:`Flask.secret_key` (or :data:`SECRET_KEY`)::849 850- $ python -c 'import os; print(os.urandom(16))'851- b'_5#y2L"F4Q8z\n\xec]/'850+ $ python -c 'import secrets; print(secrets.token_hex())'851+ '192b9bdd22ab9ed4d12e236c78afcb9a393ec15f71bbf5dc987d54727823bcbf'852 853 A note on cookie-based sessions: Flask will take the values you put into the854 session object and serialize them into a cookie. If you are finding some855 values do not persist across requests, cookies are indeed enabled, and you are83 unmodified lines