PEP 506 – Adding A Secrets Module To The Standard Library
- Steven D’Aprano <steve at pearwood.info>
- Standards Track
This PEP proposes the addition of a module for common security-related functions such as generating tokens to the Python standard library.
Some common abbreviations used in this proposal:
Pseudo Random Number Generator. A deterministic algorithm used to produce random-looking numbers with certain desirable statistical properties.
Cryptographically Strong Pseudo Random Number Generator. An algorithm used to produce random-looking numbers which are resistant to prediction.
Mersenne Twister. An extensively studied PRNG which is currently used by the
randommodule as the default.
This proposal is motivated by concerns that Python’s standard library makes it too easy for developers to inadvertently make serious security errors. Theo de Raadt, the founder of OpenBSD, contacted Guido van Rossum and expressed some concern  about the use of MT for generating sensitive information such as passwords, secure tokens, session keys and similar.
Although the documentation for the
random module explicitly states that
the default is not suitable for security purposes , it is strongly
believed that this warning may be missed, ignored or misunderstood by
many Python developers. In particular:
- developers may not have read the documentation and consequently not seen the warning;
- they may not realise that their specific use of the module has security implications; or
- not realising that there could be a problem, they have copied code (or learned techniques) from websites which don’t offer best practises.
The first  hit when searching for “python how to generate passwords” on
Google is a tutorial that uses the default functions from the
module . Although it is not intended for use in web applications, it is
likely that similar techniques find themselves used in that situation.
The second hit is to a StackOverflow question about generating
passwords . Most of the answers given, including the accepted one, use
the default functions. When one user warned that the default could be
easily compromised, they were told “I think you worry too much.” 
This strongly suggests that the existing
random module is an attractive
nuisance when it comes to generating (for example) passwords or secure
Additional motivation (of a more philosophical bent) can be found in the post which first proposed this idea .
Alternative proposals have focused on the default PRNG in the
module, with the aim of providing “secure by default” cryptographically
strong primitives that developers can build upon without thinking about
security. (See Alternatives below.) This proposes a different approach:
- The standard library already provides cryptographically strong primitives, but many users don’t know they exist or when to use them.
- Instead of requiring crypto-naive users to write secure code, the standard library should include a set of ready-to-use “batteries” for the most common needs, such as generating secure tokens. This code will both directly satisfy a need (“How do I generate a password reset token?”), and act as an example of acceptable practises which developers can learn from .
To do this, this PEP proposes that we add a new module to the standard
library, with the suggested name
secrets. This module will contain a
set of ready-to-use functions for common activities with security
implications, together with some lower-level primitives.
The suggestion is that
secrets becomes the go-to module for dealing
with anything which should remain secret (passwords, tokens, etc.)
random module remains backward-compatible.
API and Implementation
This PEP proposes the following functions for the
- Functions for generating tokens suitable for use in (e.g.) password
recovery, as session keys, etc., in the following formats:
- as bytes,
- as text, using hexadecimal digits,
- as text, using URL-safe base-64 encoding,
- as bytes,
- A limited interface to the system CSPRNG, using either
random.SystemRandom. Unlike the
randommodule, this does not need to provide methods for seeding, getting or setting the state, or any non-uniform distributions. It should provide the following:
- A function for choosing items from a sequence,
- A function for generating a given number of random bits and/or bytes
as an integer,
- A function for returning a random integer in the half-open range
0 to the given upper limit,
- A function for choosing items from a sequence,
- A function for comparing text or bytes digests for equality while being
resistant to timing attacks,
The consensus appears to be that there is no need to add a new CSPRNG to
random module to support these uses,
SystemRandom will be
Some illustrative implementations have been given by Nick Coghlan  and a minimalist API by Tim Peters . This idea has also been discussed on the issue tracker for the “cryptography” module . The following pseudo-code should be taken as the starting point for the real implementation:
from random import SystemRandom from hmac import compare_digest _sysrand = SystemRandom() randbits = _sysrand.getrandbits choice = _sysrand.choice def randbelow(exclusive_upper_bound): return _sysrand._randbelow(exclusive_upper_bound) DEFAULT_ENTROPY = 32 # bytes def token_bytes(nbytes=None): if nbytes is None: nbytes = DEFAULT_ENTROPY return os.urandom(nbytes) def token_hex(nbytes=None): return binascii.hexlify(token_bytes(nbytes)).decode('ascii') def token_urlsafe(nbytes=None): tok = token_bytes(nbytes) return base64.urlsafe_b64encode(tok).rstrip(b'=').decode('ascii')
secrets module itself will be pure Python, and other Python
implementations can easily make use of it unchanged, or adapt it as
necessary. An implementation can be found on BitBucket .
One difficult question is “How many bytes should my token be?”. We can
help with this question by providing a default amount of entropy for the
“token_*” functions. If the
nbytes argument is None or not given, the
default entropy will be used. This default value should be large enough
to be expected to be secure for medium-security uses, but is expected to
change in the future, possibly even in a maintenance release .
One question is the naming conventions used in the module , whether to use C-like naming conventions such as “randrange” or more Pythonic names such as “random_range”.
Functions which are simply bound methods of the private
randrange), or a thin wrapper around such, should keep
the familiar names. Those which are something new (such as the various
token_* functions) will use more Pythonic names.
One alternative is to change the default PRNG provided by the
module . This received considerable scepticism and outright opposition:
- There is fear that a CSPRNG may be slower than the current PRNG (which in the case of MT is already quite slow).
- Some applications (such as scientific simulations, and replaying gameplay) require the ability to seed the PRNG into a known state, which a CSPRNG lacks by design.
- Another major use of the
randommodule is for simple “guess a number” games written by beginners, and many people are loath to make any change to the
randommodule which may make that harder.
- Although there is no proposal to remove MT from the
randommodule, there was considerable hostility to the idea of having to opt-in to a non-CSPRNG or any backwards-incompatible changes.
- Demonstrated attacks against MT are typically against PHP applications. It is believed that PHP’s version of MT is a significantly softer target than Python’s version, due to a poor seeding technique . Consequently, without a proven attack against Python applications, many people object to a backwards-incompatible change.
Nick Coghlan made an earlier suggestion for a globally configurable PRNG which uses the system CSPRNG by default, but has since withdrawn it in favour of this proposal.
Comparison To Other Languages
PHP includes a function
uniqid which by default returns a thirteen character string based on the current time in microseconds. Translated into Python syntax, it has the following signature:
def uniqid(prefix='', more_entropy=False)->str
The PHP documentation warns that this function is not suitable for security purposes. Nevertheless, various mature, well-known PHP applications use it for that purpose (citation needed).
PHP 5.3 and better also includes a function
openssl_random_pseudo_bytes. Translated into Python syntax, it has roughly the following signature:
def openssl_random_pseudo_bytes(length:int)->Tuple[str, bool]
This function returns a pseudo-random string of bytes of the given length, and a boolean flag giving whether the string is considered cryptographically strong. The PHP manual suggests that returning anything but True should be rare except for old or broken platforms.
Math.randomis often used, despite serious weaknesses making it unsuitable for cryptographic purposes . In recent years the majority of browsers have gained support for
Node.js offers a rich cryptographic module,
crypto, most of which is beyond the scope of this PEP. It does include a single function for generating random bytes,
The Ruby standard library includes a module
SecureRandom which includes the following methods:
- base64 - returns a Base64 encoded random string.
- hex - returns a random hexadecimal string.
- random_bytes - returns a random byte string.
- random_number - depending on the argument, returns either a random integer in the range(0, n), or a random float between 0.0 and 1.0.
- urlsafe_base64 - returns a random URL-safe Base64 encoded string.
- uuid - return a version 4 random Universally Unique IDentifier.
What Should Be The Name Of The Module?
There was a proposal to add a “random.safe” submodule, quoting the Zen of Python “Namespaces are one honking great idea” koan. However, the author of the Zen, Tim Peters, has come out against this idea , and recommends a top-level module.
In discussion on the python-ideas mailing list so far, the name “secrets” has received some approval, and no strong opposition.
There is already an existing third-party module with the same name , but it appears to be unused and abandoned.
Frequently Asked Questions
- Q: Is this a real problem? Surely MT is random enough that nobody can
predict its output.
A: The consensus among security professionals is that MT is not safe in security contexts. It is not difficult to reconstruct the internal state of MT   and so predict all past and future values. There are a number of known, practical attacks on systems using MT for randomness .
- Q: Attacks on PHP are one thing, but are there any known attacks on
A: Yes. There have been vulnerabilities in Zope and Plone at the very least. Hanno Schlichting commented :
"In the context of Plone and Zope a practical attack was demonstrated, but I can't find any good non-broken links about this anymore. IIRC Plone generated a random number and exposed this on each error page along the lines of 'Sorry, you encountered an error, your problem has been filed as <random number>, please include this when you contact us'. This allowed anyone to do large numbers of requests to this page and get enough random values to reconstruct the MT state. A couple of security related modules used random instead of system random (cookie session ids, password reset links, auth token), so the attacker could break all of those."
- Q: Is this an alternative to specialist cryptographic software such as SSL?
A: No. This is a “batteries included” solution, not a full-featured “nuclear reactor”. It is intended to mitigate against some basic security errors, not be a solution to all security-related issues. To quote Nick Coghlan referring to his earlier proposal :
"...folks really are better off learning to use things like cryptography.io for security sensitive software, so this change is just about harm mitigation given that it's inevitable that a non-trivial proportion of the millions of current and future Python developers won't do that."
- Q: What about a password generator?
A: The consensus is that the requirements for password generators are too variable for it to be a good match for the standard library . No password generator will be included in the initial release of the module, instead it will be given in the documentation as a recipe (à la the recipes in the
- Q: Will
secretsuse /dev/random (which blocks) or /dev/urandom (which doesn’t block) on Linux? What about other platforms?
secretswill be based on
random.SystemRandom, which are interfaces to your operating system’s best source of cryptographic randomness. On Linux, that may be
/dev/urandom, on Windows it may be
CryptGenRandom(), but see the documentation and/or source code for the detailed implementation details.
This document has been placed in the public domain.
Last modified: 2022-01-21 11:03:51 GMT