Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the randomness of the tokens, slightly #111

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions anaconda_anon_usage/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
WRITE_DEFER = 1
WRITE_FAIL = 2

# Number of bits of randomness to include in the token
MIN_ENTROPY = 128
# Number of base64-encoded characters required to contain
# at least MIN_ENTROPY bits of randomness
TOKEN_LENGTH = (MIN_ENTROPY - 1) // 6 + 1


def cached(func):
def call_if_needed(*args, **kwargs):
Expand Down Expand Up @@ -73,8 +79,10 @@ def _debug(s, *args, error=False):


def _random_token(what="random"):
data = os.urandom(16)
result = base64.urlsafe_b64encode(data).strip(b"=").decode("ascii")
# base64 encoding captures 6 bits per character.
# Generate enough random bytes to ensure all characters are random
data = os.urandom((TOKEN_LENGTH * 6 - 1) // 8 + 1)
result = base64.urlsafe_b64encode(data).decode("ascii")[:TOKEN_LENGTH]
_debug("Generated %s token: %s", what, result)
return result

Expand Down Expand Up @@ -172,7 +180,7 @@ def _saved_token(fpath, what, must_exist=None):
_debug("Retrieved %s token: %s", what, client_token)
except Exception as exc:
_debug("Unexpected error reading: %s\n %s", fpath, exc, error=True)
if len(client_token) < 22:
if len(client_token) < TOKEN_LENGTH:
if len(client_token) > 0:
_debug("Generating longer %s token", what)
client_token = _random_token(what)
Expand Down
Loading