Skip to content

Commit 5fbf7c5

Browse files
authored
Merge pull request #1895 from yuvipanda/secrets
Switch to using secrets module from os.urandom
2 parents fab419e + 83da55d commit 5fbf7c5

File tree

3 files changed

+5
-7
lines changed

3 files changed

+5
-7
lines changed

binderhub/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ def initialize(self, *args, **kwargs):
950950
"enable_api_only_mode": self.enable_api_only_mode,
951951
}
952952
)
953-
self.tornado_settings["cookie_secret"] = os.urandom(32)
953+
self.tornado_settings["cookie_secret"] = secrets.token_bytes(32)
954954
if self.cors_allow_origin:
955955
self.tornado_settings.setdefault("headers", {})[
956956
"Access-Control-Allow-Origin"

binderhub/tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import inspect
44
import json
55
import os
6+
import secrets
67
import subprocess
78
import time
8-
from binascii import b2a_hex
99
from collections import defaultdict
1010
from unittest import mock
1111
from urllib.parse import urlparse
@@ -379,7 +379,7 @@ def always_build(app, request):
379379
if REMOTE_BINDER:
380380
return
381381
# make it long to ensure we run into max build slug length
382-
session_id = b2a_hex(os.urandom(16)).decode("ascii")
382+
session_id = secrets.token_hex(16)
383383

384384
def patch_provider(Provider):
385385
original_slug = Provider.get_build_slug

binderhub/tests/test_registry.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import base64
44
import json
5-
import os
5+
import secrets
66
from random import randint
77

88
import pytest
@@ -129,9 +129,7 @@ def get(self):
129129
raise HTTPError(403, "Bad username %r" % username)
130130
if password != self.test_handle["password"]:
131131
raise HTTPError(403, "Bad password %r" % password)
132-
self.test_handle["token"] = token = (
133-
base64.encodebytes(os.urandom(5)).decode("ascii").rstrip()
134-
)
132+
self.test_handle["token"] = token = secrets.token_hex(8)
135133
self.set_header("Content-Type", "application/json")
136134
self.write(json.dumps({"token": token}))
137135

0 commit comments

Comments
 (0)