Skip to content
Myr

Merge pull request #4301 from kkirsche/secret_key_via_secrets

Before adoption: this commit came in with the history the project adopted, so no landing records it.

use secrets module for SECRET_KEY generation in docs

authored by David Lord · committed by GitHub · parents 8ddf80c, 58a08a1 · 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

3 files+9 added−9 removed

modified:docs/tutorial/deploy.rst+3 added-3 removed
86 unmodified lines
87You can use the following command to output a random secret key:8889.. code-block:: none9091 $ python -c 'import os; print(os.urandom(16))'91 $ python -c 'import secrets; print(secrets.token_hex())'9293 b'_5#y2L"F4Q8z\n\xec]/'93 '192b9bdd22ab9ed4d12e236c78afcb9a393ec15f71bbf5dc987d54727823bcbf'9495Create the ``config.py`` file in the instance folder, which the factory96will read from if it exists. Copy the generated value into it.9798.. code-block:: python99 :caption: ``venv/var/flaskr-instance/config.py``100101 SECRET_KEY = b'_5#y2L"F4Q8z\n\xec]/'101 SECRET_KEY = '192b9bdd22ab9ed4d12e236c78afcb9a393ec15f71bbf5dc987d54727823bcbf'102103You can also set any other necessary configuration here, although104``SECRET_KEY`` is the only one needed for Flaskr.105
34 unmodified lines
modified:docs/config.rst+4 added-4 removed
36 unmodified lines
37method::3839 app.config.update(40 TESTING=True,41 SECRET_KEY=b'_5#y2L"F4Q8z\n\xec]/'41 SECRET_KEY='192b9bdd22ab9ed4d12e236c78afcb9a393ec15f71bbf5dc987d54727823bcbf'42 )434445Environment and Debug Features
133 unmodified lines
179 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::182183 $ 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'185186 **Do not reveal the secret key when posting questions or committing code.**187188 Default: ``None``
278 unmodified lines
467468Here is an example of a configuration file::469470 # Example configuration471 SECRET_KEY = b'_5#y2L"F4Q8z\n\xec]/'471 SECRET_KEY = '192b9bdd22ab9ed4d12e236c78afcb9a393ec15f71bbf5dc987d54727823bcbf'472473Make sure to load the configuration very early on, so that extensions have474the ability to access the configuration when starting up. There are other475methods on the config object as well to load from individual files. For a
293 unmodified lines
modified:docs/quickstart.rst+2 added-2 removed
845 unmodified lines
846 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`)::849850 $ 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'852853A note on cookie-based sessions: Flask will take the values you put into the854session object and serialize them into a cookie. If you are finding some855values do not persist across requests, cookies are indeed enabled, and you are
83 unmodified lines